PipeWire 1.4.7
Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_RINGBUFFER_H
6#define SPA_RINGBUFFER_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
16
21
22struct spa_ringbuffer;
23
24#include <string.h>
25
26#include <spa/utils/defs.h>
27
28#ifndef SPA_API_RINGBUFFER
29 #ifdef SPA_API_IMPL
30 #define SPA_API_RINGBUFFER SPA_API_IMPL
31 #else
32 #define SPA_API_RINGBUFFER static inline
33 #endif
34#endif
35
39struct spa_ringbuffer {
40 uint32_t readindex; /*< the current read index */
41 uint32_t writeindex; /*< the current write index */
42};
43
44#define SPA_RINGBUFFER_INIT() ((struct spa_ringbuffer) { 0, 0 })
45
47 * Initialize a spa_ringbuffer with \a size.
48 *
49 * \param rbuf a spa_ringbuffer
50 */
52{
54}
55
60 * \param size the target size of \a rbuf
61 */
62SPA_API_RINGBUFFER void spa_ringbuffer_set_avail(struct spa_ringbuffer *rbuf, uint32_t size)
63{
64 rbuf->readindex = 0;
65 rbuf->writeindex = size;
66}
67
71 * \param rbuf a spa_ringbuffer
72 * \param index the value of readindex, should be taken modulo the size of the
73 * ringbuffer memory to get the offset in the ringbuffer memory
74 * \return number of available bytes to read. values < 0 mean
75 * there was an underrun. values > rbuf->size means there
76 * was an overrun.
77 */
78SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_read_index(struct spa_ringbuffer *rbuf, uint32_t *index)
79{
80 *index = __atomic_load_n(&rbuf->readindex, __ATOMIC_RELAXED);
81 return (int32_t) (__atomic_load_n(&rbuf->writeindex, __ATOMIC_ACQUIRE) - *index);
82}
83
87 *
88 * \param rbuf a struct \ref spa_ringbuffer
89 * \param buffer memory to read from
90 * \param size the size of \a buffer
91 * \param offset offset in \a buffer to read from
92 * \param data destination memory
93 * \param len number of bytes to read
94 */
97 const void *buffer, uint32_t size,
98 uint32_t offset, void *data, uint32_t len)
99{
100 uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
101 spa_memcpy(data, SPA_PTROFF(buffer, offset, void), l0);
102 if (SPA_UNLIKELY(l1 > 0))
103 spa_memcpy(SPA_PTROFF(data, l0, void), buffer, l1);
104}
112SPA_API_RINGBUFFER void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
113{
114 __atomic_store_n(&rbuf->readindex, index, __ATOMIC_RELEASE);
115}
116
121 * \param index the value of writeindex, should be taken modulo the size of the
122 * ringbuffer memory to get the offset in the ringbuffer memory
123 * \return the fill level of \a rbuf. values < 0 mean
124 * there was an underrun. values > rbuf->size means there
125 * was an overrun. Subtract from the buffer size to get
126 * the number of bytes available for writing.
127 */
128SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_write_index(struct spa_ringbuffer *rbuf, uint32_t *index)
129{
130 *index = __atomic_load_n(&rbuf->writeindex, __ATOMIC_RELAXED);
131 return (int32_t) (*index - __atomic_load_n(&rbuf->readindex, __ATOMIC_ACQUIRE));
132}
133
138 * \param rbuf a spa_ringbuffer
139 * \param buffer memory to write to
140 * \param size the size of \a buffer
141 * \param offset offset in \a buffer to write to
142 * \param data source memory
143 * \param len number of bytes to write
144 */
147 void *buffer, uint32_t size,
148 uint32_t offset, const void *data, uint32_t len)
149{
150 uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
151 spa_memcpy(SPA_PTROFF(buffer, offset, void), data, l0);
152 if (SPA_UNLIKELY(l1 > 0))
153 spa_memcpy(buffer, SPA_PTROFF(data, l0, void), l1);
154}
162SPA_API_RINGBUFFER void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
163{
164 __atomic_store_n(&rbuf->writeindex, index, __ATOMIC_RELEASE);
165}
166
170
172#ifdef __cplusplus
173} /* extern "C" */
174#endif
175
176#endif /* SPA_RINGBUFFER_H */
spa/utils/defs.h
SPA_API_RINGBUFFER void spa_ringbuffer_set_avail(struct spa_ringbuffer *rbuf, uint32_t size)
Sets the pointers so that the ringbuffer contains size bytes.
Definition ringbuffer.h:71
#define SPA_API_RINGBUFFER
Definition ringbuffer.h:40
#define SPA_RINGBUFFER_INIT()
Definition ringbuffer.h:53
SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_read_index(struct spa_ringbuffer *rbuf, uint32_t *index)
Get the read index and available bytes for reading.
Definition ringbuffer.h:87
SPA_API_RINGBUFFER void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the write pointer to index.
Definition ringbuffer.h:171
SPA_API_RINGBUFFER void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the read pointer to index.
Definition ringbuffer.h:121
SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_write_index(struct spa_ringbuffer *rbuf, uint32_t *index)
Get the write index and the number of bytes inside the ringbuffer.
Definition ringbuffer.h:137
SPA_API_RINGBUFFER void spa_ringbuffer_write_data(struct spa_ringbuffer *rbuf, void *buffer, uint32_t size, uint32_t offset, const void *data, uint32_t len)
Write len bytes to buffer starting offset.
Definition ringbuffer.h:155
SPA_API_RINGBUFFER void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
Initialize a spa_ringbuffer with size.
Definition ringbuffer.h:60
SPA_API_RINGBUFFER void spa_ringbuffer_read_data(struct spa_ringbuffer *rbuf, const void *buffer, uint32_t size, uint32_t offset, void *data, uint32_t len)
Read len bytes from rbuf starting offset.
Definition ringbuffer.h:105
#define SPA_MIN(a, b)
Definition defs.h:165
#define SPA_UNUSED
Definition defs.h:307
#define SPA_UNLIKELY(x)
Definition defs.h:394
#define spa_memcpy(d, s, n)
Definition defs.h:524
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
spa/utils/string.h
A ringbuffer type.
Definition ringbuffer.h:47
uint32_t readindex
Definition ringbuffer.h:48
uint32_t writeindex
Definition ringbuffer.h:49