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 
26 #ifndef TSRB_H
27 #define TSRB_H
28 
29 #include <assert.h>
30 #include <stddef.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 typedef struct tsrb {
40  char *buf;
41  unsigned int size;
42  volatile unsigned reads;
43  volatile unsigned writes;
44 } tsrb_t;
45 
49 #define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
50 
57 static inline void tsrb_init(tsrb_t *rb, char *buffer, unsigned bufsize)
58 {
59  /* make sure bufsize is a power of two.
60  * http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
61  */
62  assert((bufsize != 0) && ((bufsize & (~bufsize + 1)) == bufsize));
63 
64  rb->buf = buffer;
65  rb->size = bufsize;
66  rb->reads = 0;
67  rb->writes = 0;
68 }
69 
76 static inline int tsrb_empty(const tsrb_t *rb)
77 {
78  return (rb->reads == rb->writes);
79 }
80 
81 
87 static inline unsigned int tsrb_avail(const tsrb_t *rb)
88 {
89  return (rb->writes - rb->reads);
90 }
91 
98 static inline int tsrb_full(const tsrb_t *rb)
99 {
100  return (rb->writes - rb->reads) == rb->size;
101 }
102 
108 static inline unsigned int tsrb_free(const tsrb_t *rb)
109 {
110  return (rb->size - rb->writes + rb->reads);
111 }
112 
119 int tsrb_get_one(tsrb_t *rb);
120 
128 int tsrb_get(tsrb_t *rb, char *dst, size_t n);
129 
137 int tsrb_add_one(tsrb_t *rb, char c);
138 
146 int tsrb_add(tsrb_t *rb, const char *src, size_t n);
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
152 #endif /* TSRB_H */
153 
unsigned int size
Size of buffer, must be power of 2.
Definition: tsrb.h:41
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition: tsrb.h:76
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:57
POSIX.1-2008 compliant version of the assert macro.
volatile unsigned writes
total number of writes
Definition: tsrb.h:43
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:39
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition: tsrb.h:98
volatile unsigned reads
total number of reads
Definition: tsrb.h:42
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition: tsrb.h:87
int tsrb_add_one(tsrb_t *rb, char c)
Add a byte to ringbuffer.
char * buf
Buffer to operate on.
Definition: tsrb.h:40
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:108