-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringBuffer.h
75 lines (67 loc) · 3.18 KB
/
ringBuffer.h
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
typedef struct {
size_t head;
size_t tail;
size_t len;
size_t size;
char A[];
} ringBuffer;
ringBuffer* rb_create(size_t len, size_t size);
// Create a ringBuffer.
/* Arguments --------------------------------------------------------------
size_t len : Max number of elements the ringBuffer can hold.
If len is not a power of 2, it will be automatically
rounded up to the next greater power of 2 for speed.
size_t size : Size in bytes of each element.
Value ------------------------------------------------------------------
Returns pointer to new ringBuffer. Otherwise returns NULL if memory
allocation failed.
*/
void rb_destroy(ringBuffer* r);
// Free memory allocated for a ringBuffer.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
*/
void rb_write(ringBuffer* r, void* src);
// Write an element into a ringBuffer, without overwriting.
// If the ringBuffer is full, rb_write() does nothing.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
void* src : Pointer to data that will be copied into the ringBuffer.
r->size number of bytes will be copied.
*/
void rb_overWrite(ringBuffer* r, void* src);
// Write an element into a ringBuffer, with overwriting.
// If the ringBuffer is full, the oldest element will be overwritten.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
void* src : Pointer to data that will be copied into the ringBuffer.
r->size number of bytes will be copied.
*/
void rb_read(ringBuffer* r, void* dest);
// Read oldest element from a ringBuffer.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
void* dest : Pointer to location where the element will be copied to.
r->size number of bytes will be copied.
*/
void rb_dump(ringBuffer* r, void* dest);
// Parallelized read of all elements stored in a ringBuffer.
// Unlike after calling rb_read(), elements remain in the buffer.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
void* dest : Pointer to location where the elements will be copied to.
r->size * rb_fillLevel(r) number of bytes will be copied.
Therefore, r->size determines data alignment at dest.
*/
void rb_reset(ringBuffer* r);
// Remove all elements held in a ringBuffer.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
*/
size_t rb_fillLevel(ringBuffer* r);
// Get number of elements held in a ringBuffer.
/* Arguments --------------------------------------------------------------
ringBuffer* r : Pointer returned by rb_create().
Value ------------------------------------------------------------------
Returns size_t x, where 0 <= x <= r->len.
*/