diff options
author | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2013-04-11 08:53:29 +0200 |
---|---|---|
committer | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2013-04-11 08:54:12 +0200 |
commit | 06d5b3438f682307db06074ce9709b949a9aa846 (patch) | |
tree | 99dbeeab1eeab0d890ad1a32842ffb3540c782c5 /bin/common/ring-buffer.c | |
parent | 1cb1a541bedd4e17085ab8ce04d0f5b2fc4d8d1d (diff) | |
download | uca-06d5b3438f682307db06074ce9709b949a9aa846.tar.gz uca-06d5b3438f682307db06074ce9709b949a9aa846.tar.bz2 uca-06d5b3438f682307db06074ce9709b949a9aa846.tar.xz uca-06d5b3438f682307db06074ce9709b949a9aa846.zip |
Move binaries to bin and make ring buffer a lib
Diffstat (limited to 'bin/common/ring-buffer.c')
-rw-r--r-- | bin/common/ring-buffer.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/bin/common/ring-buffer.c b/bin/common/ring-buffer.c new file mode 100644 index 0000000..039024f --- /dev/null +++ b/bin/common/ring-buffer.c @@ -0,0 +1,70 @@ + +#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->current_index = 0; + buffer->data = g_malloc0_n (n_blocks, 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->current_index = 0; +} + +gsize +ring_buffer_get_block_size (RingBuffer *buffer) +{ + return buffer->block_size; +} + +gpointer +ring_buffer_get_current_pointer (RingBuffer *buffer) +{ + return buffer->data + (buffer->current_index % buffer->n_blocks_total) * buffer->block_size; +} + +gpointer +ring_buffer_get_pointer (RingBuffer *buffer, + guint index) +{ + g_assert (index < buffer->n_blocks_total); + return buffer->data + ((buffer->current_index - buffer->n_blocks_used + 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->current_index++; + + if (buffer->n_blocks_used < buffer->n_blocks_total) + buffer->n_blocks_used++; + else + buffer->current_index = buffer->current_index % buffer->n_blocks_total; +} |