librfn
An ad-hoc utility library
ringbuf.h
Go to the documentation of this file.
1 /*
2  * ringbuf.h
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #ifndef RF_RINGBUF_H_
15 #define RF_RINGBUF_H_
16 
17 #include <stdbool.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include "atomic.h"
22 
38 typedef struct {
39  uint8_t *bufp;
40  size_t buf_len;
41  atomic_uint readi;
42  atomic_uint writei;
43 } ringbuf_t;
44 
48 #define RINGBUF_VAR_INIT(bufp, buf_len) \
49  { \
50  (uint8_t *) (bufp), \
51  (buf_len), \
52  ATOMIC_VAR_INIT(0), \
53  ATOMIC_VAR_INIT(0) \
54  }
55 
59 void ringbuf_init(ringbuf_t *rb, void *bufp, size_t buf_len);
60 
66 int ringbuf_get(ringbuf_t *rb);
67 
73 bool ringbuf_empty(ringbuf_t *rb);
74 
78 bool ringbuf_put(ringbuf_t *rb, uint8_t d);
79 
94 void ringbuf_putchar(void *rb, char c);
95 
97 #endif // RF_RINGBUF_H_
struct charlie c
void ringbuf_putchar(void *rb, char c)
Insert a character into the ring buffer.
Definition: ringbuf.c:75
Ring buffer descriptor.
Definition: ringbuf.h:38
atomic_uint writei
Definition: ringbuf.h:42
int ringbuf_get(ringbuf_t *rb)
Extract a byte from the ring buffer.
Definition: ringbuf.c:31
atomic_uint readi
Definition: ringbuf.h:41
size_t buf_len
Definition: ringbuf.h:40
bool ringbuf_empty(ringbuf_t *rb)
Test whether the ring buffer contains any data.
Definition: ringbuf.c:52
bool ringbuf_put(ringbuf_t *rb, uint8_t d)
Insert a byte into the ring buffer.
Definition: ringbuf.c:58
uint8_t * bufp
Definition: ringbuf.h:39
void ringbuf_init(ringbuf_t *rb, void *bufp, size_t buf_len)
Runtime initializer for a ring buffer descriptor.
Definition: ringbuf.c:22