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