From 0ef77576760e3b384ff15f6155c6073ab96fc8c7 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Wed, 10 Apr 2013 09:02:09 +0200 Subject: grab takes -n and -t parameters now --- tools/ring-buffer.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tools/ring-buffer.c (limited to 'tools/ring-buffer.c') diff --git a/tools/ring-buffer.c b/tools/ring-buffer.c new file mode 100644 index 0000000..039024f --- /dev/null +++ b/tools/ring-buffer.c @@ -0,0 +1,70 @@ + +#include +#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; +} -- cgit v1.2.3