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 
23 #ifndef TSRB_H
24 #define TSRB_H
25 
26 #include <assert.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #include "irq.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 typedef struct tsrb {
40  uint8_t *buf;
41  unsigned int size;
42  unsigned reads;
43  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, uint8_t *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  unsigned irq_state = irq_disable();
79  int retval = (rb->reads == rb->writes);
80  irq_restore(irq_state);
81  return retval;
82 }
83 
84 
90 static inline unsigned int tsrb_avail(const tsrb_t *rb)
91 {
92  unsigned irq_state = irq_disable();
93  int retval = (rb->writes - rb->reads);
94  irq_restore(irq_state);
95  return retval;
96 }
97 
104 static inline int tsrb_full(const tsrb_t *rb)
105 {
106  unsigned irq_state = irq_disable();
107  int retval = (rb->writes - rb->reads) == rb->size;
108  irq_restore(irq_state);
109  return retval;
110 }
111 
117 static inline unsigned int tsrb_free(const tsrb_t *rb)
118 {
119  unsigned irq_state = irq_disable();
120  int retval = (rb->size - rb->writes + rb->reads);
121  irq_restore(irq_state);
122  return retval;
123 }
124 
131 int tsrb_get_one(tsrb_t *rb);
132 
140 int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
141 
148 int tsrb_drop(tsrb_t *rb, size_t n);
149 
157 int tsrb_add_one(tsrb_t *rb, uint8_t c);
158 
166 int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* TSRB_H */
173 
tsrb::reads
unsigned reads
total number of reads
Definition: tsrb.h:42
tsrb::size
unsigned int size
Size of buffer, must be power of 2.
Definition: tsrb.h:41
tsrb::buf
uint8_t * buf
Buffer to operate on.
Definition: tsrb.h:40
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
irq_disable
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
tsrb_t
struct tsrb tsrb_t
thread-safe ringbuffer struct
assert.h
POSIX.1-2008 compliant version of the assert macro.
tsrb_free
static unsigned int tsrb_free(const tsrb_t *rb)
Get free space in ringbuffer.
Definition: tsrb.h:117
tsrb_add
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
Add bytes to ringbuffer.
tsrb_empty
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition: tsrb.h:76
tsrb::writes
unsigned writes
total number of writes
Definition: tsrb.h:43
irq_restore
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
tsrb_avail
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition: tsrb.h:90
tsrb_drop
int tsrb_drop(tsrb_t *rb, size_t n)
Drop bytes from ringbuffer.
irq.h
IRQ driver interface.
tsrb_init
static void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
Initialize a tsrb.
Definition: tsrb.h:57
tsrb_get
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
Get bytes from ringbuffer.
tsrb_get_one
int tsrb_get_one(tsrb_t *rb)
Get a byte from ringbuffer.
tsrb_full
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition: tsrb.h:104
tsrb_add_one
int tsrb_add_one(tsrb_t *rb, uint8_t c)
Add a byte to ringbuffer.
tsrb
thread-safe ringbuffer struct
Definition: tsrb.h:39