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