diff options
author | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2012-10-12 14:47:07 +0200 |
---|---|---|
committer | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2012-10-12 14:47:07 +0200 |
commit | ef7ff789e9c15f4f4b8c65585de7e7fa4f9c9438 (patch) | |
tree | 2f6a105b6a4c1325b2aba7c1fa3d9a2b26856a7c /tools/gui/ring-buffer.c | |
parent | 6b82a9a175c92c39f25e9b5965b2e9f5c35f65c2 (diff) | |
download | uca-ef7ff789e9c15f4f4b8c65585de7e7fa4f9c9438.tar.gz uca-ef7ff789e9c15f4f4b8c65585de7e7fa4f9c9438.tar.bz2 uca-ef7ff789e9c15f4f4b8c65585de7e7fa4f9c9438.tar.xz uca-ef7ff789e9c15f4f4b8c65585de7e7fa4f9c9438.zip |
Add ring buffer recording for assessment
Diffstat (limited to 'tools/gui/ring-buffer.c')
-rw-r--r-- | tools/gui/ring-buffer.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tools/gui/ring-buffer.c b/tools/gui/ring-buffer.c new file mode 100644 index 0000000..56c7620 --- /dev/null +++ b/tools/gui/ring-buffer.c @@ -0,0 +1,64 @@ + +#include <math.h> +#include "ring-buffer.h" + +RingBuffer * +ring_buffer_new (gsize block_size, + gsize n_blocks) +{ + RingBuffer *buffer; + + buffer = g_new0 (RingBuffer, 1); + buffer->block_size = block_size; + buffer->n_blocks_total = n_blocks; + buffer->n_blocks_used = 0; + buffer->start_index = 0; + buffer->data = g_malloc0 (n_blocks * buffer->block_size); + + return buffer; +} + +void +ring_buffer_free (RingBuffer *buffer) +{ + g_free (buffer->data); + g_free (buffer); +} + +void +ring_buffer_reset (RingBuffer *buffer) +{ + buffer->n_blocks_used = 0; + buffer->start_index = 0; +} + +gpointer +ring_buffer_get_current_pointer (RingBuffer *buffer) +{ + return ring_buffer_get_pointer (buffer, 0); +} + +gpointer +ring_buffer_get_pointer (RingBuffer *buffer, + guint index) +{ + g_assert (index < buffer->n_blocks_total); + return buffer->data + ((buffer->start_index + index) % buffer->n_blocks_total) * buffer->block_size; +} + +guint +ring_buffer_get_num_blocks (RingBuffer *buffer) +{ + return buffer->n_blocks_used; +} + +void +ring_buffer_proceed (RingBuffer *buffer) +{ + buffer->start_index++; + + if (buffer->n_blocks_used < buffer->n_blocks_total) + buffer->n_blocks_used++; + else + buffer->start_index = buffer->start_index % buffer->n_blocks_total; +} |