diff options
author | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2008-02-08 13:00:32 +0000 |
---|---|---|
committer | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2008-02-08 13:00:32 +0000 |
commit | 4d84d14ac1aa13958eaa2971b03f7f929a519105 (patch) | |
tree | 6579d3f2cea5410a10c4baac8d0f372fb0dff372 /desiredata/portmidi_osx/pmutil.c | |
parent | b334d38aefbd8e0e159d7af6c20d63c5d2b64859 (diff) |
reorganized
svn path=/trunk/; revision=9400
Diffstat (limited to 'desiredata/portmidi_osx/pmutil.c')
-rw-r--r-- | desiredata/portmidi_osx/pmutil.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/desiredata/portmidi_osx/pmutil.c b/desiredata/portmidi_osx/pmutil.c new file mode 100644 index 00000000..f3582a42 --- /dev/null +++ b/desiredata/portmidi_osx/pmutil.c @@ -0,0 +1,86 @@ +/* pmutil.c -- some helpful utilities for building midi + applications that use PortMidi + */ +#include "stdlib.h" +#include "memory.h" +#include "portmidi.h" +#include "pmutil.h" +#include "pminternal.h" + + +PmQueue *Pm_QueueCreate(long num_msgs, long bytes_per_msg) +{ + PmQueueRep *queue = (PmQueueRep *) malloc(sizeof(PmQueueRep)); + if (!queue) return NULL; + queue->len = num_msgs * bytes_per_msg; + queue->buffer = malloc(queue->len); + if (!queue->buffer) { + free(queue); + return NULL; + } + queue->head = 0; + queue->tail = 0; + queue->msg_size = bytes_per_msg; + queue->overflow = FALSE; + return queue; +} + + +PmError Pm_QueueDestroy(PmQueue *q) +{ + PmQueueRep *queue = (PmQueueRep *) q; + if (!queue || !queue->buffer) return pmBadPtr; + free(queue->buffer); + free(queue); + return pmNoError; +} + + +PmError Pm_Dequeue(PmQueue *q, void *msg) +{ + long head; + PmQueueRep *queue = (PmQueueRep *) q; + if (queue->overflow) { + queue->overflow = FALSE; + return pmBufferOverflow; + } + head = queue->head; /* make sure this is written after access */ + if (head == queue->tail) return 0; + memcpy(msg, queue->buffer + head, queue->msg_size); + head += queue->msg_size; + if (head == queue->len) head = 0; + queue->head = head; + return 1; /* success */ +} + + +/* source should not enqueue data if overflow is set */ +/**/ +PmError Pm_Enqueue(PmQueue *q, void *msg) +{ + PmQueueRep *queue = (PmQueueRep *) q; + long tail = queue->tail; + memcpy(queue->buffer + tail, msg, queue->msg_size); + tail += queue->msg_size; + if (tail == queue->len) tail = 0; + if (tail == queue->head) { + queue->overflow = TRUE; + /* do not update tail, so message is lost */ + return pmBufferOverflow; + } + queue->tail = tail; + return pmNoError; +} + + +int Pm_QueueFull(PmQueue *q) +{ + PmQueueRep *queue = (PmQueueRep *) q; + long tail = queue->tail; + tail += queue->msg_size; + if (tail == queue->len) { + tail = 0; + } + return (tail == queue->head); +} + |