Loading...
Searching...
No Matches
tsrb.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
21
22#include <assert.h>
23#include <stddef.h>
24#include <stdint.h>
25
26#include "irq.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
35typedef struct tsrb {
36 uint8_t *buf;
37 unsigned int size;
38 unsigned reads;
39 unsigned writes;
41
49#define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
50
60static inline void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
61{
62 /* make sure bufsize is a power of two.
63 * http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
64 */
65 assert((bufsize != 0) && ((bufsize & (~bufsize + 1)) == bufsize));
66
67 rb->buf = buffer;
68 rb->size = bufsize;
69 rb->reads = 0;
70 rb->writes = 0;
71}
72
77static inline void tsrb_clear(tsrb_t *rb)
78{
79 unsigned irq_state = irq_disable();
80 rb->reads = rb->writes;
81 irq_restore(irq_state);
82}
83
90static inline int tsrb_empty(const tsrb_t *rb)
91{
92 unsigned irq_state = irq_disable();
93 int retval = (rb->reads == rb->writes);
94 irq_restore(irq_state);
95 return retval;
96}
97
103static inline unsigned int tsrb_avail(const tsrb_t *rb)
104{
105 unsigned irq_state = irq_disable();
106 unsigned int retval = (rb->writes - rb->reads);
107 irq_restore(irq_state);
108 return retval;
109}
110
117static inline int tsrb_full(const tsrb_t *rb)
118{
119 unsigned irq_state = irq_disable();
120 int retval = (rb->writes - rb->reads) == rb->size;
121 irq_restore(irq_state);
122 return retval;
123}
124
130static inline unsigned int tsrb_free(const tsrb_t *rb)
131{
132 unsigned irq_state = irq_disable();
133 unsigned int retval = (rb->size - rb->writes + rb->reads);
134 irq_restore(irq_state);
135 return retval;
136}
137
145
153
161int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
162
170int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n);
171
178int tsrb_drop(tsrb_t *rb, size_t n);
179
187int tsrb_add_one(tsrb_t *rb, uint8_t c);
188
196int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
197
198#ifdef __cplusplus
199}
200#endif
201
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
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:130
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition tsrb.h:103
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:77
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:60
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition tsrb.h:117
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition tsrb.h:90
IRQ driver interface.
thread-safe ringbuffer struct
Definition tsrb.h:35
unsigned writes
total number of writes
Definition tsrb.h:39
unsigned reads
total number of reads
Definition tsrb.h:38
uint8_t * buf
Buffer to operate on.
Definition tsrb.h:36
unsigned int size
Size of buffer, must be power of 2.
Definition tsrb.h:37