Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Freie Universität Berlin
3 * Copyright (C) 2013 INRIA
4 *
5 * This file is subject to the terms and conditions of the GNU Lesser
6 * General Public License v2.1. See the file LICENSE in the top level
7 * directory for more details.
8 */
9
25#ifndef RINGBUFFER_H
26#define RINGBUFFER_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
36typedef struct {
37 char *buf;
38 unsigned int size;
39 unsigned int start;
40 unsigned int avail;
42
50#define RINGBUFFER_INIT(BUF) { (BUF), sizeof(BUF), 0, 0 }
51
58static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer,
59 unsigned bufsize)
60{
61 rb->buf = buffer;
62 rb->size = bufsize;
63 rb->start = 0;
64 rb->avail = 0;
65}
66
76int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
77
88unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf,
89 unsigned n);
90
96int ringbuffer_get_one(ringbuffer_t *__restrict rb);
97
105unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
106
113unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
114
120static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
121{
122 return rb->avail == 0;
123}
124
130static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
131{
132 return rb->avail == rb->size;
133}
134
140static inline unsigned int ringbuffer_get_free(
141 const ringbuffer_t *__restrict rb)
142{
143 return rb->size - rb->avail;
144}
145
151int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
152
160unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf,
161 unsigned n);
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* RINGBUFFER_H */
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:58
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:140
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition ringbuffer.h:130
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:120
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:36
char * buf
Buffer to operate on.
Definition ringbuffer.h:37
unsigned int start
Current read position in the ring buffer.
Definition ringbuffer.h:39
unsigned int size
Size of buf.
Definition ringbuffer.h:38
unsigned int avail
Number of elements available for reading.
Definition ringbuffer.h:40