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 "MessagePool.h" chris@162: #include "HvMessage.h" chris@162: #include "Utils.h" chris@162: chris@162: // the number of bytes reserved at a time from the pool chris@162: #define MP_BLOCK_SIZE_BYTES 512 chris@162: chris@162: #if HV_APPLE chris@162: #pragma mark - MessageList chris@162: #endif chris@162: chris@162: typedef struct MessageListNode { chris@162: char *p; chris@162: struct MessageListNode *next; chris@162: } MessageListNode; chris@162: chris@162: static inline bool ml_hasAvailable(MessagePoolList *ml) { chris@162: return (ml->head != NULL); chris@162: } chris@162: chris@162: static char *ml_pop(MessagePoolList *ml) { chris@162: MessageListNode *n = ml->head; chris@162: ml->head = n->next; chris@162: n->next = ml->pool; chris@162: ml->pool = n; chris@162: char *const p = n->p; chris@162: n->p = NULL; // set to NULL to make it clear that this node does not have a valid buffer chris@162: return p; chris@162: } chris@162: chris@162: /** Push a MessageListNode with the given pointer onto the head of the queue. */ chris@162: static void ml_push(MessagePoolList *ml, void *p) { chris@162: MessageListNode *n = NULL; chris@162: if (ml->pool != NULL) { chris@162: // take an empty MessageListNode from the pool chris@162: n = ml->pool; chris@162: ml->pool = n->next; chris@162: } else { chris@162: // a MessageListNode is not available, allocate one chris@162: n = (MessageListNode *) hv_malloc(sizeof(MessageListNode)); chris@162: } chris@162: n->p = (char *) p; chris@162: n->next = ml->head; chris@162: ml->head = n; // push to the front of the queue chris@162: } chris@162: chris@162: static void ml_free(MessagePoolList *ml) { chris@162: if (ml != NULL) { chris@162: while (ml_hasAvailable(ml)) { chris@162: ml_pop(ml); chris@162: } chris@162: while (ml->pool != NULL) { chris@162: MessageListNode *n = ml->pool; chris@162: ml->pool = n->next; chris@162: hv_free(n); chris@162: } chris@162: } chris@162: } chris@162: chris@162: #if HV_APPLE chris@162: #pragma mark - MessagePool chris@162: #endif chris@162: chris@162: static hv_size_t mp_messagelistIndexForSize(hv_size_t byteSize) { chris@162: return (hv_size_t) hv_max_i((hv_min_max_log2((hv_uint32_t) byteSize) - 5), 0); chris@162: } chris@162: chris@162: hv_size_t mp_init(MessagePool *mp, hv_size_t numKB) { chris@162: mp->bufferSize = numKB * 1024; chris@162: mp->buffer = (char *) hv_malloc(mp->bufferSize); chris@162: mp->bufferIndex = 0; chris@162: chris@162: // initialise all message lists chris@162: for (int i = 0; i < MP_NUM_MESSAGE_LISTS; i++) { chris@162: mp->lists[i].head = NULL; chris@162: mp->lists[i].pool = NULL; chris@162: } chris@162: chris@162: return mp->bufferSize; chris@162: } chris@162: chris@162: void mp_free(MessagePool *mp) { chris@162: hv_free(mp->buffer); chris@162: for (int i = 0; i < MP_NUM_MESSAGE_LISTS; i++) { chris@162: ml_free(&mp->lists[i]); chris@162: } chris@162: } chris@162: chris@162: void mp_freeMessage(MessagePool *mp, HvMessage *m) { chris@162: const hv_size_t b = msg_getNumBytes(m); // the number of bytes that a message occupies in memory chris@162: const hv_size_t i = mp_messagelistIndexForSize(b); // the MessagePoolList index in the pool chris@162: MessagePoolList *ml = &mp->lists[i]; chris@162: const hv_size_t chunkSize = 32 << i; chris@162: hv_memset(m, chunkSize); // clear the chunk, just in case chris@162: ml_push(ml, m); chris@162: } chris@162: chris@162: HvMessage *mp_addMessage(MessagePool *mp, const HvMessage *m) { chris@162: const hv_size_t b = msg_getNumHeapBytes(m); chris@162: // determine the message list index to allocate data from based on the msg size chris@162: // smallest chunk size is 32 bytes chris@162: const hv_size_t i = mp_messagelistIndexForSize(b); chris@162: chris@162: assert(i < MP_NUM_MESSAGE_LISTS); // how many chunk sizes do we want to support? 32, 64, 128, 256 at the moment chris@162: MessagePoolList *ml = &mp->lists[i]; chris@162: const hv_size_t chunkSize = 32 << i; chris@162: chris@162: if (ml_hasAvailable(ml)) { chris@162: char *buf = ml_pop(ml); chris@162: msg_copyToBuffer(m, buf, chunkSize); chris@162: return (HvMessage *) buf; chris@162: } else { chris@162: // if no appropriately sized buffer is immediately available, increase the size of the used buffer chris@162: const hv_size_t newIndex = mp->bufferIndex + MP_BLOCK_SIZE_BYTES; chris@162: hv_assert(newIndex <= mp->bufferSize); // have we have exceeded the buffer size? chris@162: chris@162: for (hv_size_t i = mp->bufferIndex; i < newIndex; i += chunkSize) { chris@162: ml_push(ml, mp->buffer + i); // push new nodes onto the list with chunk pointers chris@162: } chris@162: mp->bufferIndex = newIndex; chris@162: char *buf = ml_pop(ml); chris@162: msg_copyToBuffer(m, buf, chunkSize); chris@162: return (HvMessage *) buf; chris@162: } chris@162: }