HvMessage *mp_addMessage(MessagePool *mp, const HvMessage *m) { const hv_size_t b = msg_getSize(m); // determine the message list index to allocate data from based on the msg size // smallest chunk size is 32 bytes const hv_size_t i = mp_messagelistIndexForSize(b); hv_assert(i < MP_NUM_MESSAGE_LISTS); // how many chunk sizes do we want to support? 32, 64, 128, 256 at the moment MessagePoolList *ml = &mp->lists[i]; const hv_size_t chunkSize = 32 << i; if (ml_hasAvailable(ml)) { char *buf = ml_pop(ml); msg_copyToBuffer(m, buf, chunkSize); return (HvMessage *) buf; } else { // if no appropriately sized buffer is immediately available, increase the size of the used buffer const hv_size_t newIndex = mp->bufferIndex + MP_BLOCK_SIZE_BYTES; hv_assert((newIndex <= mp->bufferSize) && "The message pool buffer size has been exceeded. The context cannot store more messages. " "Try using the new_with_options() initialiser with a larger pool size (default is 10KB)."); for (hv_size_t j = mp->bufferIndex; j < newIndex; j += chunkSize) { ml_push(ml, mp->buffer + j); // push new nodes onto the list with chunk pointers } mp->bufferIndex = newIndex; char *buf = ml_pop(ml); msg_copyToBuffer(m, buf, chunkSize); return (HvMessage *) buf; } }
HvMessage *mp_addMessage(MessagePool *mp, const HvMessage *m) { const hv_size_t b = msg_getNumHeapBytes(m); // determine the message list index to allocate data from based on the msg size // smallest chunk size is 32 bytes const hv_size_t i = mp_messagelistIndexForSize(b); assert(i < MP_NUM_MESSAGE_LISTS); // how many chunk sizes do we want to support? 32, 64, 128, 256 at the moment MessagePoolList *ml = &mp->lists[i]; const hv_size_t chunkSize = 32 << i; if (ml_hasAvailable(ml)) { char *buf = ml_pop(ml); msg_copyToBuffer(m, buf, chunkSize); return (HvMessage *) buf; } else { // if no appropriately sized buffer is immediately available, increase the size of the used buffer const hv_size_t newIndex = mp->bufferIndex + MP_BLOCK_SIZE_BYTES; hv_assert(newIndex <= mp->bufferSize); // have we have exceeded the buffer size? for (hv_size_t i = mp->bufferIndex; i < newIndex; i += chunkSize) { ml_push(ml, mp->buffer + i); // push new nodes onto the list with chunk pointers } mp->bufferIndex = newIndex; char *buf = ml_pop(ml); msg_copyToBuffer(m, buf, chunkSize); return (HvMessage *) buf; } }
static void ml_free(MessagePoolList *ml) { if (ml != NULL) { while (ml_hasAvailable(ml)) { ml_pop(ml); } while (ml->pool != NULL) { MessageListNode *n = ml->pool; ml->pool = n->next; hv_free(n); } } }