blob: 850dfc02ff9ed4f007a9afd817e117bc66f6411d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#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;
}
void
ring_buffer_set_current_pointer (RingBuffer *buffer,
guint index)
{
g_assert (index < buffer->n_blocks_total);
buffer->current_index = index;
}
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;
}
|