summaryrefslogtreecommitdiffstats
path: root/tools/gui/ring-buffer.c
diff options
context:
space:
mode:
authorMihael Koep <koep@schneide.com>2012-10-19 17:03:49 +0200
committerMihael Koep <koep@schneide.com>2012-10-19 17:03:49 +0200
commit9866002f3f569f6da886d71964d3298183cd1646 (patch)
tree968097e2f5fa11a094238ead6469739a725d8ce2 /tools/gui/ring-buffer.c
parent37f48ae1b67b1205ba4884ec350afdf5ab7dd2d3 (diff)
parent6f12303cf4c4130d544c30f8ad5e2445a1fa3e61 (diff)
downloaduca-9866002f3f569f6da886d71964d3298183cd1646.tar.gz
uca-9866002f3f569f6da886d71964d3298183cd1646.tar.bz2
uca-9866002f3f569f6da886d71964d3298183cd1646.tar.xz
uca-9866002f3f569f6da886d71964d3298183cd1646.zip
Merge remote branch 'origin/master' into dexela
Conflicts: CMakeLists.txt src/CMakeLists.txt src/uca-camera.c
Diffstat (limited to 'tools/gui/ring-buffer.c')
-rw-r--r--tools/gui/ring-buffer.c64
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..ec2638c
--- /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->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;
+}
+
+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;
+}