diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2013-12-17 21:03:39 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2014-01-18 13:43:46 +0100 |
commit | 9f25faad5fe732e498942818dc45de78ce7f3766 (patch) | |
tree | ab8a1c13ed05c02a732601d13b1f95b5f2c4e791 /rb.h | |
parent | 6d88b62a73c7c4985d07317e3fe00f84743c4700 (diff) |
rb: add a first version of the library
first working version of the library. the binary stuff is included.
we can write to and read from a ring buffer.
Diffstat (limited to 'rb.h')
-rw-r--r-- | rb.h | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -0,0 +1,49 @@ +#ifndef RING_BUFFER_H +#define RING_BUFFER_H + +#include <stdint.h> +#include <sys/types.h> + +typedef struct t_ring_buffer { + char buffer[8192]; + + uint16_t off_r; + uint16_t off_w; + + size_t size_filled; +} t_ring_buffer; + +typedef t_ring_buffer t_rb; + +static inline size_t rb_get_size(const t_rb *rb) +{ + return sizeof(rb->buffer); +} + +/* + * initializes rb with default values. + * returns 0 on success + */ +int rb_init(t_rb *rb); + +/* returns an initialized newly allocated ring buffer */ +__attribute((malloc)) t_rb *rb_new(void); + +void rb_delete(t_rb **); + +/* put n bytes of src into the ring buffer pointed by rb */ +t_rb *rb_put(t_rb *rb, const void *src, size_t n); + +/* + * take at most n bytes from the ring buffer pointed by rb and + * put them into dest + */ +size_t rb_get(t_rb *rb, void *dest, size_t n); + +/* + * like rb_get but does not modify the ring_buffer (the bytes are kept + * in the ring_buffer) + */ +size_t rb_peek(const t_rb *ring_buffer, void *dest, size_t size); + +#endif /* RING_BUFFER_H */ |