PipeWire: spa/utils/ringbuffer.h Source File

PipeWire 1.4.2 |

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 */
51 SPA_API_RINGBUFFER void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
52{
53 *rbuf = SPA_RINGBUFFER_INIT();
54}
55
60 * \param size the target size of \a rbuf
61 */
62 SPA_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 */
78 SPA_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 */
95 SPA_API_RINGBUFFER void
96 spa_ringbuffer_read_data(struct spa_ringbuffer *rbuf SPA_UNUSED,
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}
105
112 SPA_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 */
128 SPA_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
137 *
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 */
145 SPA_API_RINGBUFFER void
146 spa_ringbuffer_write_data(struct spa_ringbuffer *rbuf SPA_UNUSED,
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}
155
162 SPA_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
171
172#ifdef __cplusplus
173} /* extern "C" */
174#endif
175
176#endif /* SPA_RINGBUFFER_H */
defs.h
spa/utils/defs.h
spa_ringbuffer_set_avail
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
SPA_API_RINGBUFFER
#define SPA_API_RINGBUFFER
Definition ringbuffer.h:40
SPA_RINGBUFFER_INIT
#define SPA_RINGBUFFER_INIT()
Definition ringbuffer.h:53
spa_ringbuffer_get_read_index
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_ringbuffer_write_update
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_ringbuffer_read_update
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_ringbuffer_get_write_index
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_ringbuffer_write_data
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_ringbuffer_init
SPA_API_RINGBUFFER void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
Initialize a spa_ringbuffer with size.
Definition ringbuffer.h:60
spa_ringbuffer_read_data
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
SPA_MIN
#define SPA_MIN(a, b)
Definition defs.h:165
SPA_UNUSED
#define SPA_UNUSED
Definition defs.h:307
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:394
spa_memcpy
#define spa_memcpy(d, s, n)
Definition defs.h:524
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
string.h
spa/utils/string.h
spa_ringbuffer
A ringbuffer type.
Definition ringbuffer.h:47
spa_ringbuffer::readindex
uint32_t readindex
Definition ringbuffer.h:48
spa_ringbuffer::writeindex
uint32_t writeindex
Definition ringbuffer.h:49