tsrb.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
27 #ifndef TSRB_H
28 #define TSRB_H
29 
30 #include <assert.h>
31 #include <stddef.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 typedef struct tsrb {
41  char *buf;
42  unsigned int size;
43  volatile unsigned reads;
44  volatile unsigned writes;
45 } tsrb_t;
46 
50 #define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
51 
58 static inline void tsrb_init(tsrb_t *rb, char *buffer, unsigned bufsize)
59 {
60  /* make sure bufsize is a power of two.
61  * http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
62  */
63  assert((bufsize != 0) && ((bufsize & (~bufsize + 1)) == bufsize));
64 
65  rb->buf = buffer;
66  rb->size = bufsize;
67  rb->reads = 0;
68  rb->writes = 0;
69 }
70 
77 static inline int tsrb_empty(const tsrb_t *rb)
78 {
79  return (rb->reads == rb->writes);
80 }
81 
82 
88 static inline unsigned int tsrb_avail(const tsrb_t *rb)
89 {
90  return (rb->writes - rb->reads);
91 }
92 
99 static inline int tsrb_full(const tsrb_t *rb)
100 {
101  return (rb->writes - rb->reads) == rb->size;
102 }
103 
109 static inline unsigned int tsrb_free(const tsrb_t *rb)
110 {
111  return (rb->size - rb->writes + rb->reads);
112 }
113 
120 int tsrb_get_one(tsrb_t *rb);
121 
129 int tsrb_get(tsrb_t *rb, char *dst, size_t n);
130 
138 int tsrb_add_one(tsrb_t *rb, char c);
139 
147 int tsrb_add(tsrb_t *rb, const char *src, size_t n);
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
154 #endif /* TSRB_H */
unsigned int size
Size of buf.
Definition: tsrb.h:42
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition: tsrb.h:77
int tsrb_get(tsrb_t *rb, char *dst, size_t n)
Get bytes from ringbuffer.
static void tsrb_init(tsrb_t *rb, char *buffer, unsigned bufsize)
Initialize a tsrb.
Definition: tsrb.h:58
POSIX.1-2008 compliant version of the assert macro.
volatile unsigned writes
total number of writes
Definition: tsrb.h:44
struct tsrb tsrb_t
thread-safe ringbuffer struct
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
thread-safe ringbuffer struct
Definition: tsrb.h:40
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition: tsrb.h:99
volatile unsigned reads
total number of reads
Definition: tsrb.h:43
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition: tsrb.h:88
int tsrb_add_one(tsrb_t *rb, char c)
Add a byte to ringbuffer.
char * buf
Buffer to operate on.
Definition: tsrb.h:41
int tsrb_get_one(tsrb_t *rb)
Get a byte from ringbuffer.
int tsrb_add(tsrb_t *rb, const char *src, size_t n)
Add bytes to ringbuffer.
static unsigned int tsrb_free(const tsrb_t *rb)
Get free space in ringbuffer.
Definition: tsrb.h:109