2018-01-11 09:16:28 -08:00
|
|
|
#ifndef RINGBUFFER_H
|
|
|
|
#define RINGBUFFER_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-11-27 19:49:45 -08:00
|
|
|
#include <memory>
|
2018-11-19 04:46:49 -08:00
|
|
|
#include <utility>
|
2018-01-11 09:16:28 -08:00
|
|
|
|
2018-03-09 17:24:03 -08:00
|
|
|
|
2018-11-19 04:46:49 -08:00
|
|
|
struct ll_ringbuffer;
|
|
|
|
using ll_ringbuffer_t = struct ll_ringbuffer;
|
|
|
|
|
|
|
|
struct ll_ringbuffer_data {
|
2018-01-11 09:16:28 -08:00
|
|
|
char *buf;
|
|
|
|
size_t len;
|
2018-11-19 04:46:49 -08:00
|
|
|
};
|
|
|
|
using ll_ringbuffer_data_pair = std::pair<ll_ringbuffer_data,ll_ringbuffer_data>;
|
|
|
|
using ll_ringbuffer_data_t = struct ll_ringbuffer_data;
|
2018-01-11 09:16:28 -08:00
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
|
|
|
|
* The number of elements is rounded up to the next power of two (even if it is
|
|
|
|
* already a power of two, to ensure the requested amount can be written).
|
|
|
|
*/
|
2018-03-01 21:23:13 -08:00
|
|
|
ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes);
|
2018-03-02 12:46:31 -08:00
|
|
|
/** Free all data associated with the ringbuffer `rb'. */
|
2018-01-11 09:16:28 -08:00
|
|
|
void ll_ringbuffer_free(ll_ringbuffer_t *rb);
|
2018-03-02 12:46:31 -08:00
|
|
|
/** Reset the read and write pointers to zero. This is not thread safe. */
|
2018-01-11 09:16:28 -08:00
|
|
|
void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
|
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
/**
|
2018-11-19 04:46:49 -08:00
|
|
|
* The non-copying data reader. Returns two ringbuffer data pointers that hold
|
|
|
|
* the current readable data at `rb'. If the readable data is in one segment
|
|
|
|
* the second segment has zero length.
|
2018-03-02 12:46:31 -08:00
|
|
|
*/
|
2018-11-19 04:46:49 -08:00
|
|
|
ll_ringbuffer_data_pair ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb);
|
2018-03-02 12:46:31 -08:00
|
|
|
/**
|
2018-11-19 04:46:49 -08:00
|
|
|
* The non-copying data writer. Returns two ringbuffer data pointers that hold
|
|
|
|
* the current writeable data at `rb'. If the writeable data is in one segment
|
|
|
|
* the second segment has zero length.
|
2018-03-02 12:46:31 -08:00
|
|
|
*/
|
2018-11-19 04:46:49 -08:00
|
|
|
ll_ringbuffer_data_pair ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb);
|
2018-01-11 09:16:28 -08:00
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
/**
|
|
|
|
* Return the number of elements available for reading. This is the number of
|
|
|
|
* elements in front of the read pointer and behind the write pointer.
|
|
|
|
*/
|
|
|
|
size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
|
|
|
|
/**
|
|
|
|
* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
|
|
|
|
* Returns the actual number of elements copied.
|
|
|
|
*/
|
2018-11-19 04:11:21 -08:00
|
|
|
size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, void *dest, size_t cnt);
|
2018-03-02 12:46:31 -08:00
|
|
|
/**
|
|
|
|
* The copying data reader w/o read pointer advance. Copy at most `cnt'
|
|
|
|
* elements from `rb' to `dest'. Returns the actual number of elements copied.
|
|
|
|
*/
|
2018-11-19 04:11:21 -08:00
|
|
|
size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, void *dest, size_t cnt);
|
2018-03-02 12:46:31 -08:00
|
|
|
/** Advance the read pointer `cnt' places. */
|
2018-01-11 09:16:28 -08:00
|
|
|
void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
|
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
/**
|
|
|
|
* Return the number of elements available for writing. This is the number of
|
|
|
|
* elements in front of the write pointer and behind the read pointer.
|
|
|
|
*/
|
|
|
|
size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
|
|
|
|
/**
|
|
|
|
* The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
|
|
|
|
* Returns the actual number of elements copied.
|
|
|
|
*/
|
2018-11-19 04:11:21 -08:00
|
|
|
size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const void *src, size_t cnt);
|
2018-03-02 12:46:31 -08:00
|
|
|
/** Advance the write pointer `cnt' places. */
|
2018-01-11 09:16:28 -08:00
|
|
|
void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
|
|
|
|
|
2018-11-27 19:49:45 -08:00
|
|
|
|
|
|
|
struct RingBufferDeleter {
|
|
|
|
void operator()(ll_ringbuffer_t *ring) const
|
|
|
|
{ ll_ringbuffer_free(ring); }
|
|
|
|
};
|
|
|
|
using RingBufferPtr = std::unique_ptr<ll_ringbuffer_t,RingBufferDeleter>;
|
|
|
|
|
2018-01-11 09:16:28 -08:00
|
|
|
#endif /* RINGBUFFER_H */
|