annotate projects/heavy/hello-world/MessagePool.c @ 160:5bcf04234f80 heavy-updated

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