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