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;
45 
53 #define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
54 
64 static inline void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
65 {
66  /* make sure bufsize is a power of two.
67  * http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
68  */
69  assert((bufsize != 0) && ((bufsize & (~bufsize + 1)) == bufsize));
70 
71  rb->buf = buffer;
72  rb->size = bufsize;
73  rb->reads = 0;
74  rb->writes = 0;
75 }
76 
81 static inline void tsrb_clear(tsrb_t *rb)
82 {
83  unsigned irq_state = irq_disable();
84  rb->reads = rb->writes;
85  irq_restore(irq_state);
86 }
87 
94 static inline int tsrb_empty(const tsrb_t *rb)
95 {
96  unsigned irq_state = irq_disable();
97  int retval = (rb->reads == rb->writes);
98  irq_restore(irq_state);
99  return retval;
100 }
101 
107 static inline unsigned int tsrb_avail(const tsrb_t *rb)
108 {
109  unsigned irq_state = irq_disable();
110  int retval = (rb->writes - rb->reads);
111  irq_restore(irq_state);
112  return retval;
113 }
114 
121 static inline int tsrb_full(const tsrb_t *rb)
122 {
123  unsigned irq_state = irq_disable();
124  int retval = (rb->writes - rb->reads) == rb->size;
125  irq_restore(irq_state);
126  return retval;
127 }
128 
134 static inline unsigned int tsrb_free(const tsrb_t *rb)
135 {
136  unsigned irq_state = irq_disable();
137  int retval = (rb->size - rb->writes + rb->reads);
138  irq_restore(irq_state);
139  return retval;
140 }
141 
149 
157 
165 int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
166 
174 int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n);
175 
182 int tsrb_drop(tsrb_t *rb, size_t n);
183 
191 int tsrb_add_one(tsrb_t *rb, uint8_t c);
192 
200 int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
201 
202 #ifdef __cplusplus
203 }
204 #endif
205 
206 #endif /* TSRB_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n)
Get bytes from ringbuffer, without removing them.
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
Get bytes from ringbuffer.
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
Add bytes to ringbuffer.
int tsrb_drop(tsrb_t *rb, size_t n)
Drop bytes from ringbuffer.
static unsigned int tsrb_free(const tsrb_t *rb)
Get free space in ringbuffer.
Definition: tsrb.h:134
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition: tsrb.h:107
int tsrb_get_one(tsrb_t *rb)
Get a byte from ringbuffer.
struct tsrb tsrb_t
thread-safe ringbuffer struct
static void tsrb_clear(tsrb_t *rb)
Clear a tsrb.
Definition: tsrb.h:81
int tsrb_add_one(tsrb_t *rb, uint8_t c)
Add a byte to ringbuffer.
int tsrb_peek_one(tsrb_t *rb)
Get a byte from ringbuffer, without removing it.
static void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
Initialize a tsrb.
Definition: tsrb.h:64
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition: tsrb.h:121
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition: tsrb.h:94
IRQ driver interface.
thread-safe ringbuffer struct
Definition: tsrb.h:39
unsigned writes
total number of writes
Definition: tsrb.h:43
unsigned reads
total number of reads
Definition: tsrb.h:42
uint8_t * buf
Buffer to operate on.
Definition: tsrb.h:40
unsigned int size
Size of buffer, must be power of 2.
Definition: tsrb.h:41