annotate projects/heavy/envelopeTrigger/MessagePool.c @ 162:c3e8226a5651 heavy-updated

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