annotate projects/heavy/circularBuffer/MessageQueue.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 "MessageQueue.h"
chris@163 18 #include "Utils.h"
chris@163 19
chris@163 20 hv_size_t mq_init(MessageQueue *q) {
chris@163 21 q->head = NULL;
chris@163 22 q->tail = NULL;
chris@163 23 q->pool = NULL;
chris@163 24 return mp_init(&q->mp, 1);
chris@163 25 }
chris@163 26
chris@163 27 void mq_initWithPoolSize(MessageQueue *q, hv_size_t poolSizeKB) {
chris@163 28 q->head = NULL;
chris@163 29 q->tail = NULL;
chris@163 30 q->pool = NULL;
chris@163 31 mp_init(&q->mp, poolSizeKB);
chris@163 32 }
chris@163 33
chris@163 34 void mq_free(MessageQueue *q) {
chris@163 35 mq_clear(q);
chris@163 36 while (q->pool != NULL) {
chris@163 37 MessageNode *n = q->pool;
chris@163 38 q->pool = q->pool->next;
chris@163 39 hv_free(n);
chris@163 40 }
chris@163 41 mp_free(&q->mp);
chris@163 42 }
chris@163 43
chris@163 44 static MessageNode *mq_getOrCreateNodeFromPool(MessageQueue *q) {
chris@163 45 if (q->pool == NULL) {
chris@163 46 // if necessary, create a new empty node
chris@163 47 q->pool = (MessageNode *) hv_malloc(sizeof(MessageNode));
chris@163 48 q->pool->next = NULL;
chris@163 49 }
chris@163 50 MessageNode *node = q->pool;
chris@163 51 q->pool = q->pool->next;
chris@163 52 return node;
chris@163 53 }
chris@163 54
chris@163 55 int mq_size(MessageQueue *q) {
chris@163 56 int size = 0;
chris@163 57 MessageNode *n = q->head;
chris@163 58 while (n != NULL) {
chris@163 59 ++size;
chris@163 60 n = n->next;
chris@163 61 }
chris@163 62 return size;
chris@163 63 }
chris@163 64
chris@163 65 HvMessage *mq_addMessage(MessageQueue *q, const HvMessage *m, int let,
chris@163 66 void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@163 67 MessageNode *node = mq_getOrCreateNodeFromPool(q);
chris@163 68 node->m = mp_addMessage(&q->mp, m);
chris@163 69 node->let = let;
chris@163 70 node->sendMessage = sendMessage;
chris@163 71 node->prev = NULL;
chris@163 72 node->next = NULL;
chris@163 73
chris@163 74 if (q->tail != NULL) {
chris@163 75 // the list already contains elements
chris@163 76 q->tail->next = node;
chris@163 77 node->prev = q->tail;
chris@163 78 q->tail = node;
chris@163 79 } else {
chris@163 80 // the list is empty
chris@163 81 node->prev = NULL;
chris@163 82 q->head = node;
chris@163 83 q->tail = node;
chris@163 84 }
chris@163 85 return mq_node_getMessage(node);
chris@163 86 }
chris@163 87
chris@163 88 HvMessage *mq_addMessageByTimestamp(MessageQueue *q, HvMessage *m, int let,
chris@163 89 void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@163 90 if (mq_hasMessage(q)) {
chris@163 91 MessageNode *n = mq_getOrCreateNodeFromPool(q);
chris@163 92 n->m = mp_addMessage(&q->mp, m);
chris@163 93 n->let = let;
chris@163 94 n->sendMessage = sendMessage;
chris@163 95
chris@163 96 if (msg_getTimestamp(m) < msg_getTimestamp(q->head->m)) {
chris@163 97 // the message occurs before the current head
chris@163 98 n->next = q->head;
chris@163 99 q->head->prev = n;
chris@163 100 n->prev = NULL;
chris@163 101 q->head = n;
chris@163 102 } else if (msg_getTimestamp(m) >= msg_getTimestamp(q->tail->m)) {
chris@163 103 // the message occurs after the current tail
chris@163 104 n->next = NULL;
chris@163 105 n->prev = q->tail;
chris@163 106 q->tail->next = n;
chris@163 107 q->tail = n;
chris@163 108 } else {
chris@163 109 // the message occurs somewhere between the head and tail
chris@163 110 MessageNode *node = q->head;
chris@163 111 while (node != NULL) {
chris@163 112 if (m->timestamp < msg_getTimestamp(node->next->m)) {
chris@163 113 MessageNode *r = node->next;
chris@163 114 node->next = n;
chris@163 115 n->next = r;
chris@163 116 n->prev = node;
chris@163 117 r->prev = n;
chris@163 118 break;
chris@163 119 }
chris@163 120 node = node->next;
chris@163 121 }
chris@163 122 }
chris@163 123 return n->m;
chris@163 124 } else {
chris@163 125 // add a message to the head
chris@163 126 return mq_addMessage(q, m, let, sendMessage);
chris@163 127 }
chris@163 128 }
chris@163 129
chris@163 130 void mq_pop(MessageQueue *q) {
chris@163 131 if (mq_hasMessage(q)) {
chris@163 132 MessageNode *n = q->head;
chris@163 133
chris@163 134 mp_freeMessage(&q->mp, n->m);
chris@163 135 n->m = NULL;
chris@163 136
chris@163 137 n->let = 0;
chris@163 138 n->sendMessage = NULL;
chris@163 139
chris@163 140 q->head = n->next;
chris@163 141 if (q->head == NULL) {
chris@163 142 q->tail = NULL;
chris@163 143 } else {
chris@163 144 q->head->prev = NULL;
chris@163 145 }
chris@163 146 n->next = q->pool;
chris@163 147 n->prev = NULL;
chris@163 148 q->pool = n;
chris@163 149 }
chris@163 150 }
chris@163 151
chris@163 152 void mq_removeMessage(MessageQueue *q, HvMessage *m, void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@163 153 if (mq_hasMessage(q)) {
chris@163 154 if (mq_node_getMessage(q->head) == m) { // msg in head node
chris@163 155 // only remove the message if sendMessage is the same as the stored one,
chris@163 156 // if the sendMessage argument is NULL, it is not checked and will remove any matching message pointer
chris@163 157 if (sendMessage == NULL || q->head->sendMessage == sendMessage) {
chris@163 158 mq_pop(q);
chris@163 159 }
chris@163 160 } else {
chris@163 161 MessageNode *prevNode = q->head;
chris@163 162 MessageNode *currNode = q->head->next;
chris@163 163 while ((currNode != NULL) && (currNode->m != m)) {
chris@163 164 prevNode = currNode;
chris@163 165 currNode = currNode->next;
chris@163 166 }
chris@163 167 if (currNode != NULL) {
chris@163 168 if (sendMessage == NULL || currNode->sendMessage == sendMessage) {
chris@163 169 mp_freeMessage(&q->mp, m);
chris@163 170 currNode->m = NULL;
chris@163 171 currNode->let = 0;
chris@163 172 currNode->sendMessage = NULL;
chris@163 173 if (currNode == q->tail) { // msg in tail node
chris@163 174 prevNode->next = NULL;
chris@163 175 q->tail = prevNode;
chris@163 176 } else { // msg in middle node
chris@163 177 prevNode->next = currNode->next;
chris@163 178 currNode->next->prev = prevNode;
chris@163 179 }
chris@163 180 currNode->next = (q->pool == NULL) ? NULL : q->pool;
chris@163 181 currNode->prev = NULL;
chris@163 182 q->pool = currNode;
chris@163 183 }
chris@163 184 }
chris@163 185 }
chris@163 186 }
chris@163 187 }
chris@163 188
chris@163 189 void mq_clear(MessageQueue *q) {
chris@163 190 while (mq_hasMessage(q)) {
chris@163 191 mq_pop(q);
chris@163 192 }
chris@163 193 }
chris@163 194
chris@163 195 void mq_clearAfter(MessageQueue *q, const double timestamp) {
chris@163 196 MessageNode *n = q->tail;
chris@163 197 while (n != NULL && timestamp <= msg_getTimestamp(n->m)) {
chris@163 198 // free the node's message
chris@163 199 mp_freeMessage(&q->mp, n->m);
chris@163 200 n->m = NULL;
chris@163 201 n->let = 0;
chris@163 202 n->sendMessage = NULL;
chris@163 203
chris@163 204 // the tail points at the previous node
chris@163 205 q->tail = n->prev;
chris@163 206
chris@163 207 // put the node back in the pool
chris@163 208 n->next = q->pool;
chris@163 209 n->prev = NULL;
chris@163 210 if (q->pool != NULL) q->pool->prev = n;
chris@163 211 q->pool = n;
chris@163 212
chris@163 213 // update the tail node
chris@163 214 n = q->tail;
chris@163 215 }
chris@163 216
chris@163 217 if (q->tail == NULL) q->head = NULL;
chris@163 218 }