Free Electron
ringbuffer.h
1 #ifndef RINGBUFFER_H
2 #define RINGBUFFER_H
3 
4 #include <stddef.h>
5 
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 typedef struct ll_ringbuffer ll_ringbuffer_t;
12 typedef struct ll_ringbuffer_data {
13  char *buf;
14  size_t len;
15 } ll_ringbuffer_data_t;
16 
17 
18 /**
19  * Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
20  * The number of elements is rounded up to the next power of two (even if it is
21  * already a power of two, to ensure the requested amount can be written).
22  */
23 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes);
24 /** Free all data associated with the ringbuffer `rb'. */
25 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
26 /** Reset the read and write pointers to zero. This is not thread safe. */
27 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
28 
29 /**
30  * The non-copying data reader. `vec' is an array of two places. Set the values
31  * at `vec' to hold the current readable data at `rb'. If the readable data is
32  * in one segment the second segment has zero length.
33  */
34 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]);
35 /**
36  * The non-copying data writer. `vec' is an array of two places. Set the values
37  * at `vec' to hold the current writeable data at `rb'. If the writeable data
38  * is in one segment the second segment has zero length.
39  */
40 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]);
41 
42 /**
43  * Return the number of elements available for reading. This is the number of
44  * elements in front of the read pointer and behind the write pointer.
45  */
46 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
47 /**
48  * The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
49  * Returns the actual number of elements copied.
50  */
51 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
52 /**
53  * The copying data reader w/o read pointer advance. Copy at most `cnt'
54  * elements from `rb' to `dest'. Returns the actual number of elements copied.
55  */
56 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
57 /** Advance the read pointer `cnt' places. */
58 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
59 
60 /**
61  * Return the number of elements available for writing. This is the number of
62  * elements in front of the write pointer and behind the read pointer.
63  */
64 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
65 /**
66  * The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
67  * Returns the actual number of elements copied.
68  */
69 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
70 /** Advance the write pointer `cnt' places. */
71 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
72 
73 #ifdef __cplusplus
74 } /* extern "C" */
75 #endif
76 
77 #endif /* RINGBUFFER_H */