We use "typedef struct Ring Ring" with camel-case for internal objects.
So rename "struct ring" to "Ring".
#define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
#define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
-void ring_flush(struct ring *r) {
+void ring_flush(Ring *r) {
assert(r);
r->start = 0;
r->used = 0;
}
assert(r);
r->start = 0;
r->used = 0;
}
-void ring_clear(struct ring *r) {
+void ring_clear(Ring *r) {
+ assert(r);
+
free(r->buf);
zero(*r);
}
free(r->buf);
zero(*r);
}
* size_t iov_len;
* };
*/
* size_t iov_len;
* };
*/
-size_t ring_peek(struct ring *r, struct iovec *vec) {
+size_t ring_peek(Ring *r, struct iovec *vec) {
assert(r);
if (r->used == 0) {
assert(r);
if (r->used == 0) {
* at most @size bytes. If the ring buffer size is smaller, copy less bytes and
* return the number of bytes copied.
*/
* at most @size bytes. If the ring buffer size is smaller, copy less bytes and
* return the number of bytes copied.
*/
-size_t ring_copy(struct ring *r, void *buf, size_t size) {
+size_t ring_copy(Ring *r, void *buf, size_t size) {
* Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
* ring operations will behave incorrectly.
*/
* Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
* ring operations will behave incorrectly.
*/
-static int ring_resize(struct ring *r, size_t nsize) {
+static int ring_resize(Ring *r, size_t nsize) {
* resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
* success.
*/
* resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
* success.
*/
-static int ring_grow(struct ring *r, size_t add) {
+static int ring_grow(Ring *r, size_t add) {
* Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
* is too small. -ENOMEM is returned on OOM, 0 on success.
*/
* Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
* is too small. -ENOMEM is returned on OOM, 0 on success.
*/
-int ring_push(struct ring *r, const void *u8, size_t size) {
+int ring_push(Ring *r, const void *u8, size_t size) {
* Remove @len bytes from the start of the ring-buffer. Note that we protect
* against overflows so removing more bytes than available is safe.
*/
* Remove @len bytes from the start of the ring-buffer. Note that we protect
* against overflows so removing more bytes than available is safe.
*/
-void ring_pull(struct ring *r, size_t size) {
+void ring_pull(Ring *r, size_t size) {
assert(r);
if (size > r->used)
assert(r);
if (size > r->used)
#include <string.h>
#include <sys/uio.h>
#include <string.h>
#include <sys/uio.h>
+typedef struct Ring Ring;
+
+struct Ring {
uint8_t *buf; /* buffer or NULL */
size_t size; /* actual size of @buf */
size_t start; /* start position of ring */
uint8_t *buf; /* buffer or NULL */
size_t size; /* actual size of @buf */
size_t start; /* start position of ring */
};
/* flush buffer so it is empty again */
};
/* flush buffer so it is empty again */
-void ring_flush(struct ring *r);
+void ring_flush(Ring *r);
/* flush buffer, free allocated data and reset to initial state */
/* flush buffer, free allocated data and reset to initial state */
-void ring_clear(struct ring *r);
+void ring_clear(Ring *r);
/* get pointers to buffer data and their length */
/* get pointers to buffer data and their length */
-size_t ring_peek(struct ring *r, struct iovec *vec);
+size_t ring_peek(Ring *r, struct iovec *vec);
/* copy data into external linear buffer */
/* copy data into external linear buffer */
-size_t ring_copy(struct ring *r, void *buf, size_t size);
+size_t ring_copy(Ring *r, void *buf, size_t size);
/* push data to the end of the buffer */
/* push data to the end of the buffer */
-int ring_push(struct ring *r, const void *u8, size_t size);
+int ring_push(Ring *r, const void *u8, size_t size);
/* pull data from the front of the buffer */
/* pull data from the front of the buffer */
-void ring_pull(struct ring *r, size_t size);
+void ring_pull(Ring *r, size_t size);
/* return size of occupied buffer in bytes */
/* return size of occupied buffer in bytes */
-static inline size_t ring_get_size(struct ring *r)
+static inline size_t ring_get_size(Ring *r)
static void test_ring(void) {
static const char buf[8192];
static void test_ring(void) {
static const char buf[8192];
size_t l;
struct iovec vec[2];
int s;
size_t l;
struct iovec vec[2];
int s;