chris@162: /** chris@162: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@162: * chris@162: * Permission to use, copy, modify, and/or distribute this software for any chris@162: * purpose with or without fee is hereby granted, provided that the above chris@162: * copyright notice and this permission notice appear in all copies. chris@162: * chris@162: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@162: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@162: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@162: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@162: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@162: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@162: * PERFORMANCE OF THIS SOFTWARE. chris@162: */ chris@162: chris@162: #include "HvTable.h" chris@162: chris@162: hv_size_t hTable_init(HvTable *o, int length) { chris@162: o->length = length; chris@162: // true size of the table is always an integer multple of HV_N_SIMD chris@162: o->size = (length + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK; chris@162: // add an extra length for mirroring chris@162: o->allocated = o->size + HV_N_SIMD; chris@162: o->head = 0; chris@162: hv_size_t numBytes = o->allocated * sizeof(float); chris@162: o->buffer = (float *) hv_malloc(numBytes); chris@162: hv_memset(o->buffer, numBytes); chris@162: return numBytes; chris@162: } chris@162: chris@162: hv_size_t hTable_initWithData(HvTable *o, int length, const float *const data) { chris@162: o->length = length; chris@162: o->size = (length + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK; chris@162: o->allocated = o->size + HV_N_SIMD; chris@162: o->head = 0; chris@162: hv_size_t numBytes = o->size * sizeof(float); chris@162: o->buffer = (float *) hv_malloc(numBytes); chris@162: hv_memset(o->buffer, numBytes); chris@162: hv_memcpy(o->buffer, data, length*sizeof(float)); chris@162: return numBytes; chris@162: } chris@162: chris@162: hv_size_t hTable_initWithFinalData(HvTable *o, int length, float *data) { chris@162: o->length = length; chris@162: o->size = length; chris@162: o->allocated = length; chris@162: o->buffer = data; chris@162: o->head = 0; chris@162: return 0; chris@162: } chris@162: chris@162: void hTable_free(HvTable *o) { chris@162: hv_free(o->buffer); chris@162: } chris@162: chris@162: int hTable_resize(HvTable *o, hv_uint32_t newLength) { chris@162: // TODO(mhroth): update context with memory allocated by table chris@162: // NOTE(mhroth): mirrored bytes are not necessarily carried over chris@162: const hv_uint32_t oldBytes = (hv_uint32_t) (o->size * sizeof(float)); chris@162: const hv_uint32_t newSize = (newLength + HV_N_SIMD_MASK) & ~HV_N_SIMD_MASK; chris@162: const hv_uint32_t newAllocated = newSize + HV_N_SIMD; chris@162: const hv_uint32_t newBytes = (hv_uint32_t) (newAllocated * sizeof(float)); chris@162: float *b = (float *) hv_realloc(o->buffer, newBytes); chris@162: hv_assert(b != NULL); // error while reallocing! chris@162: if (newSize > o->size) hv_clear_buffer(b+o->size, newAllocated-o->size); // clear new parts of the buffer chris@162: if (b != o->buffer) { chris@162: // the buffer has been reallocated, ensure that it is on a 32-byte boundary chris@162: if ((((uintptr_t) (const void *) b) & 0x10) == 0) { chris@162: o->buffer = b; chris@162: } else { chris@162: float *c = (float *) hv_malloc(newBytes); chris@162: hv_assert(c != NULL); chris@162: hv_clear_buffer(c, newLength); chris@162: const hv_size_t min = hv_min_ui(o->size, newLength); chris@162: hv_memcpy(c, b, min * sizeof(float)); chris@162: hv_free(b); chris@162: o->buffer = c; chris@162: } chris@162: } chris@162: o->length = newLength; chris@162: o->size = newSize; chris@162: o->allocated = newAllocated; chris@162: return (int) (newBytes - oldBytes); chris@162: } chris@162: chris@162: void hTable_onMessage(HvBase *_c, HvTable *o, int letIn, const HvMessage *const m, chris@162: void (*sendMessage)(HvBase *, int, const HvMessage *const)) { chris@162: if (msg_compareSymbol(m,0,"resize") && msg_isFloat(m,1) && msg_getFloat(m,1) >= 0.0f) { chris@162: hTable_resize(o, (int) hv_ceil_f(msg_getFloat(m,1))); // apply ceil to ensure that tables always have enough space chris@162: chris@162: // send out the new size of the table chris@162: HvMessage *n = HV_MESSAGE_ON_STACK(1); chris@162: msg_initWithFloat(n, msg_getTimestamp(m), (float) hTable_getSize(o)); chris@162: sendMessage(_c, 0, n); chris@162: } chris@162: chris@162: else if (msg_compareSymbol(m,0,"mirror")) { chris@162: hv_memcpy(o->buffer+o->size, o->buffer, HV_N_SIMD*sizeof(float)); chris@162: } chris@162: }