Loading...
Searching...
No Matches
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
33extern "C" {
34#endif
35
39typedef struct tsrb {
40 uint8_t *buf;
41 unsigned int size;
42 unsigned reads;
43 unsigned writes;
45
49#define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
50
57static 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
74static inline void tsrb_clear(tsrb_t *rb)
75{
76 unsigned irq_state = irq_disable();
77 rb->reads = rb->writes;
78 irq_restore(irq_state);
79}
80
87static inline int tsrb_empty(const tsrb_t *rb)
88{
89 unsigned irq_state = irq_disable();
90 int retval = (rb->reads == rb->writes);
91 irq_restore(irq_state);
92 return retval;
93}
94
100static inline unsigned int tsrb_avail(const tsrb_t *rb)
101{
102 unsigned irq_state = irq_disable();
103 int retval = (rb->writes - rb->reads);
104 irq_restore(irq_state);
105 return retval;
106}
107
114static inline int tsrb_full(const tsrb_t *rb)
115{
116 unsigned irq_state = irq_disable();
117 int retval = (rb->writes - rb->reads) == rb->size;
118 irq_restore(irq_state);
119 return retval;
120}
121
127static inline unsigned int tsrb_free(const tsrb_t *rb)
128{
129 unsigned irq_state = irq_disable();
130 int retval = (rb->size - rb->writes + rb->reads);
131 irq_restore(irq_state);
132 return retval;
133}
134
142
150
158int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
159
167int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n);
168
175int tsrb_drop(tsrb_t *rb, size_t n);
176
184int tsrb_add_one(tsrb_t *rb, uint8_t c);
185
193int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
194
195#ifdef __cplusplus
196}
197#endif
198
199#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:136
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:127
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition tsrb.h:100
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:74
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:57
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition tsrb.h:114
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition tsrb.h:87
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