annotate projects/heavy/circularBuffer/MessagePool.c @ 163:20b52283c7b4 heavy-updated

- added circular buffer pd/heavy example (works but process needs to be killed manually if launched via ssh?)
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 15:55:30 +0000
parents
children
rev   line source
chris@163 1 /**
chris@163 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
chris@163 3 *
chris@163 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@163 5 * purpose with or without fee is hereby granted, provided that the above
chris@163 6 * copyright notice and this permission notice appear in all copies.
chris@163 7 *
chris@163 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@163 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@163 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@163 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@163 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@163 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@163 14 * PERFORMANCE OF THIS SOFTWARE.
chris@163 15 */
chris@163 16
chris@163 17 #include "MessagePool.h"
chris@163 18 #include "HvMessage.h"
chris@163 19 #include "Utils.h"
chris@163 20
chris@163 21 // the number of bytes reserved at a time from the pool
chris@163 22 #define MP_BLOCK_SIZE_BYTES 512
chris@163 23
chris@163 24 #if HV_APPLE
chris@163 25 #pragma mark - MessageList
chris@163 26 #endif
chris@163 27
chris@163 28 typedef struct MessageListNode {
chris@163 29 char *p;
chris@163 30 struct MessageListNode *next;
chris@163 31 } MessageListNode;
chris@163 32
chris@163 33 static inline bool ml_hasAvailable(MessagePoolList *ml) {
chris@163 34 return (ml->head != NULL);
chris@163 35 }
chris@163 36
chris@163 37 static char *ml_pop(MessagePoolList *ml) {
chris@163 38 MessageListNode *n = ml->head;
chris@163 39 ml->head = n->next;
chris@163 40 n->next = ml->pool;
chris@163 41 ml->pool = n;
chris@163 42 char *const p = n->p;
chris@163 43 n->p = NULL; // set to NULL to make it clear that this node does not have a valid buffer
chris@163 44 return p;
chris@163 45 }
chris@163 46
chris@163 47 /** Push a MessageListNode with the given pointer onto the head of the queue. */
chris@163 48 static void ml_push(MessagePoolList *ml, void *p) {
chris@163 49 MessageListNode *n = NULL;
chris@163 50 if (ml->pool != NULL) {
chris@163 51 // take an empty MessageListNode from the pool
chris@163 52 n = ml->pool;
chris@163 53 ml->pool = n->next;
chris@163 54 } else {
chris@163 55 // a MessageListNode is not available, allocate one
chris@163 56 n = (MessageListNode *) hv_malloc(sizeof(MessageListNode));
chris@163 57 }
chris@163 58 n->p = (char *) p;
chris@163 59 n->next = ml->head;
chris@163 60 ml->head = n; // push to the front of the queue
chris@163 61 }
chris@163 62
chris@163 63 static void ml_free(MessagePoolList *ml) {
chris@163 64 if (ml != NULL) {
chris@163 65 while (ml_hasAvailable(ml)) {
chris@163 66 ml_pop(ml);
chris@163 67 }
chris@163 68 while (ml->pool != NULL) {
chris@163 69 MessageListNode *n = ml->pool;
chris@163 70 ml->pool = n->next;
chris@163 71 hv_free(n);
chris@163 72 }
chris@163 73 }
chris@163 74 }
chris@163 75
chris@163 76 #if HV_APPLE
chris@163 77 #pragma mark - MessagePool
chris@163 78 #endif
chris@163 79
chris@163 80 static hv_size_t mp_messagelistIndexForSize(hv_size_t byteSize) {
chris@163 81 return (hv_size_t) hv_max_i((hv_min_max_log2((hv_uint32_t) byteSize) - 5), 0);
chris@163 82 }
chris@163 83
chris@163 84 hv_size_t mp_init(MessagePool *mp, hv_size_t numKB) {
chris@163 85 mp->bufferSize = numKB * 1024;
chris@163 86 mp->buffer = (char *) hv_malloc(mp->bufferSize);
chris@163 87 mp->bufferIndex = 0;
chris@163 88
chris@163 89 // initialise all message lists
chris@163 90 for (int i = 0; i < MP_NUM_MESSAGE_LISTS; i++) {
chris@163 91 mp->lists[i].head = NULL;
chris@163 92 mp->lists[i].pool = NULL;
chris@163 93 }
chris@163 94
chris@163 95 return mp->bufferSize;
chris@163 96 }
chris@163 97
chris@163 98 void mp_free(MessagePool *mp) {
chris@163 99 hv_free(mp->buffer);
chris@163 100 for (int i = 0; i < MP_NUM_MESSAGE_LISTS; i++) {
chris@163 101 ml_free(&mp->lists[i]);
chris@163 102 }
chris@163 103 }
chris@163 104
chris@163 105 void mp_freeMessage(MessagePool *mp, HvMessage *m) {
chris@163 106 const hv_size_t b = msg_getNumBytes(m); // the number of bytes that a message occupies in memory
chris@163 107 const hv_size_t i = mp_messagelistIndexForSize(b); // the MessagePoolList index in the pool
chris@163 108 MessagePoolList *ml = &mp->lists[i];
chris@163 109 const hv_size_t chunkSize = 32 << i;
chris@163 110 hv_memset(m, chunkSize); // clear the chunk, just in case
chris@163 111 ml_push(ml, m);
chris@163 112 }
chris@163 113
chris@163 114 HvMessage *mp_addMessage(MessagePool *mp, const HvMessage *m) {
chris@163 115 const hv_size_t b = msg_getNumHeapBytes(m);
chris@163 116 // determine the message list index to allocate data from based on the msg size
chris@163 117 // smallest chunk size is 32 bytes
chris@163 118 const hv_size_t i = mp_messagelistIndexForSize(b);
chris@163 119
chris@163 120 assert(i < MP_NUM_MESSAGE_LISTS); // how many chunk sizes do we want to support? 32, 64, 128, 256 at the moment
chris@163 121 MessagePoolList *ml = &mp->lists[i];
chris@163 122 const hv_size_t chunkSize = 32 << i;
chris@163 123
chris@163 124 if (ml_hasAvailable(ml)) {
chris@163 125 char *buf = ml_pop(ml);
chris@163 126 msg_copyToBuffer(m, buf, chunkSize);
chris@163 127 return (HvMessage *) buf;
chris@163 128 } else {
chris@163 129 // if no appropriately sized buffer is immediately available, increase the size of the used buffer
chris@163 130 const hv_size_t newIndex = mp->bufferIndex + MP_BLOCK_SIZE_BYTES;
chris@163 131 hv_assert(newIndex <= mp->bufferSize); // have we have exceeded the buffer size?
chris@163 132
chris@163 133 for (hv_size_t i = mp->bufferIndex; i < newIndex; i += chunkSize) {
chris@163 134 ml_push(ml, mp->buffer + i); // push new nodes onto the list with chunk pointers
chris@163 135 }
chris@163 136 mp->bufferIndex = newIndex;
chris@163 137 char *buf = ml_pop(ml);
chris@163 138 msg_copyToBuffer(m, buf, chunkSize);
chris@163 139 return (HvMessage *) buf;
chris@163 140 }
chris@163 141 }