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
9#pragma once
10
24
25#include <assert.h>
26#include <stddef.h>
27#include <stdint.h>
28
29#include "irq.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
38typedef struct tsrb {
39 uint8_t *buf;
40 unsigned int size;
41 unsigned reads;
42 unsigned writes;
44
52#define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
53
63static inline void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
64{
65 /* make sure bufsize is a power of two.
66 * http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
67 */
68 assert((bufsize != 0) && ((bufsize & (~bufsize + 1)) == bufsize));
69
70 rb->buf = buffer;
71 rb->size = bufsize;
72 rb->reads = 0;
73 rb->writes = 0;
74}
75
80static inline void tsrb_clear(tsrb_t *rb)
81{
82 unsigned irq_state = irq_disable();
83 rb->reads = rb->writes;
84 irq_restore(irq_state);
85}
86
93static inline int tsrb_empty(const tsrb_t *rb)
94{
95 unsigned irq_state = irq_disable();
96 int retval = (rb->reads == rb->writes);
97 irq_restore(irq_state);
98 return retval;
99}
100
106static inline unsigned int tsrb_avail(const tsrb_t *rb)
107{
108 unsigned irq_state = irq_disable();
109 int retval = (rb->writes - rb->reads);
110 irq_restore(irq_state);
111 return retval;
112}
113
120static inline int tsrb_full(const tsrb_t *rb)
121{
122 unsigned irq_state = irq_disable();
123 int retval = (rb->writes - rb->reads) == rb->size;
124 irq_restore(irq_state);
125 return retval;
126}
127
133static inline unsigned int tsrb_free(const tsrb_t *rb)
134{
135 unsigned irq_state = irq_disable();
136 int retval = (rb->size - rb->writes + rb->reads);
137 irq_restore(irq_state);
138 return retval;
139}
140
148
156
164int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
165
173int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n);
174
181int tsrb_drop(tsrb_t *rb, size_t n);
182
190int tsrb_add_one(tsrb_t *rb, uint8_t c);
191
199int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
200
201#ifdef __cplusplus
202}
203#endif
204
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:133
static unsigned int tsrb_avail(const tsrb_t *rb)
Get number of bytes available for reading.
Definition tsrb.h:106
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:80
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:63
static int tsrb_full(const tsrb_t *rb)
Test if the tsrb is full.
Definition tsrb.h:120
static int tsrb_empty(const tsrb_t *rb)
Test if the tsrb is empty.
Definition tsrb.h:93
IRQ driver interface.
thread-safe ringbuffer struct
Definition tsrb.h:38
unsigned writes
total number of writes
Definition tsrb.h:42
unsigned reads
total number of reads
Definition tsrb.h:41
uint8_t * buf
Buffer to operate on.
Definition tsrb.h:39
unsigned int size
Size of buffer, must be power of 2.
Definition tsrb.h:40