Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2013 Freie Universität Berlin
3 * SPDX-FileCopyrightText: 2013 INRIA
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
23
24#include "compiler_hints.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
34typedef struct {
35 char *buf;
36 unsigned int size;
37 unsigned int start;
38 unsigned int avail;
40
48#define RINGBUFFER_INIT(BUF) { (BUF), sizeof(BUF), 0, 0 }
49
56ACCESS(write_only, 2, 3)
57static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer,
58 unsigned bufsize)
59{
60 rb->buf = buffer;
61 rb->size = bufsize;
62 rb->start = 0;
63 rb->avail = 0;
64}
65
75int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
76
87ACCESS(read_only, 2, 3)
88unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf,
89 unsigned n);
90
96int ringbuffer_get_one(ringbuffer_t *__restrict rb);
97
105ACCESS(write_only, 2, 3)
106unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
107
114unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
115
121static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
122{
123 return rb->avail == 0;
124}
125
131static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
132{
133 return rb->avail == rb->size;
134}
135
141static inline unsigned int ringbuffer_get_free(
142 const ringbuffer_t *__restrict rb)
143{
144 return rb->size - rb->avail;
145}
146
152int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
153
161ACCESS(write_only, 2, 3)
162unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf,
163 unsigned n);
164
165#ifdef __cplusplus
166}
167#endif
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read, but don't remove, the a number of element of the buffer.
int ringbuffer_get_one(ringbuffer_t *__restrict rb)
Peek and remove oldest element from the ringbuffer.
static void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
Initialize a ringbuffer.
Definition ringbuffer.h:57
int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c)
Add an element to the ringbuffer.
static unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
Return available space in ringbuffer.
Definition ringbuffer.h:141
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition ringbuffer.h:131
int ringbuffer_peek_one(const ringbuffer_t *__restrict rb)
Read, but don't remove, the oldest element in the buffer.
static int ringbuffer_empty(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is empty.
Definition ringbuffer.h:121
unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read and remove a number of elements from the ringbuffer.
unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n)
Add a number of elements to the ringbuffer.
unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n)
Remove a number of elements from the ringbuffer.
Ringbuffer.
Definition ringbuffer.h:34
char * buf
Buffer to operate on.
Definition ringbuffer.h:35
unsigned int start
Current read position in the ring buffer.
Definition ringbuffer.h:37
unsigned int size
Size of buf.
Definition ringbuffer.h:36
unsigned int avail
Number of elements available for reading.
Definition ringbuffer.h:38