PipeWire  0.3.33
ringbuffer.h
Go to the documentation of this file.
1 /* Simple Plugin API
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_RINGBUFFER_H
26 #define SPA_RINGBUFFER_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
37 struct spa_ringbuffer;
38 
39 #include <string.h>
40 
41 #include <spa/utils/defs.h>
42 
47  uint32_t readindex; /*< the current read index */
48  uint32_t writeindex; /*< the current write index */
49 };
50 
51 #define SPA_RINGBUFFER_INIT() (struct spa_ringbuffer) { 0, 0 }
52 
58 /* static */ inline void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
59 {
60  *rbuf = SPA_RINGBUFFER_INIT();
61 }
62 
69 /* static */ inline void spa_ringbuffer_set_avail(struct spa_ringbuffer *rbuf, uint32_t size)
70 {
71  rbuf->readindex = 0;
72  rbuf->writeindex = size;
73 }
74 
85 /* static */ inline int32_t spa_ringbuffer_get_read_index(struct spa_ringbuffer *rbuf, uint32_t *index)
86 {
87  *index = __atomic_load_n(&rbuf->readindex, __ATOMIC_RELAXED);
88  return (int32_t) (__atomic_load_n(&rbuf->writeindex, __ATOMIC_ACQUIRE) - *index);
89 }
90 
102 /* static */ inline void
104  const void *buffer, uint32_t size,
105  uint32_t offset, void *data, uint32_t len)
106 {
107  uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
108  spa_memcpy(data, SPA_PTROFF(buffer, offset, void), l0);
109  if (SPA_UNLIKELY(l1 > 0))
110  spa_memcpy(SPA_PTROFF(data, l0, void), buffer, l1);
111 }
112 
119 /* static */ inline void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
120 {
121  __atomic_store_n(&rbuf->readindex, index, __ATOMIC_RELEASE);
122 }
123 
135 /* static */ inline int32_t spa_ringbuffer_get_write_index(struct spa_ringbuffer *rbuf, uint32_t *index)
136 {
137  *index = __atomic_load_n(&rbuf->writeindex, __ATOMIC_RELAXED);
138  return (int32_t) (*index - __atomic_load_n(&rbuf->readindex, __ATOMIC_ACQUIRE));
139 }
140 
152 /* static */ inline void
154  void *buffer, uint32_t size,
155  uint32_t offset, const void *data, uint32_t len)
156 {
157  uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
158  spa_memcpy(SPA_PTROFF(buffer, offset, void), data, l0);
159  if (SPA_UNLIKELY(l1 > 0))
160  spa_memcpy(buffer, SPA_PTROFF(data, l0, void), l1);
161 }
162 
169 /* static */ inline void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
170 {
171  __atomic_store_n(&rbuf->writeindex, index, __ATOMIC_RELEASE);
172 }
173 
179 #ifdef __cplusplus
180 } /* extern "C" */
181 #endif
182 
183 #endif /* SPA_RINGBUFFER_H */
string.h
spa_ringbuffer_read_data
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:103
data
user data to add to an object
Definition: filter.c:75
spa_memcpy
#define spa_memcpy(d, s, n)
Definition: defs.h:319
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:159
spa_ringbuffer
A ringbuffer type.
Definition: ringbuffer.h:46
spa_ringbuffer_get_read_index
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:85
spa_ringbuffer::writeindex
uint32_t writeindex
Definition: ringbuffer.h:48
spa_ringbuffer_get_write_index
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:135
spa_ringbuffer_write_data
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:153
buffer
Definition: filter.c:59
spa_ringbuffer_set_avail
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:69
SPA_MIN
#define SPA_MIN(a, b)
Definition: defs.h:123
spa_ringbuffer_write_update
void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the write pointer to index.
Definition: ringbuffer.h:169
spa_ringbuffer_read_update
void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the read pointer to index.
Definition: ringbuffer.h:119
defs.h
SPA_RINGBUFFER_INIT
#define SPA_RINGBUFFER_INIT()
Definition: ringbuffer.h:51
spa_ringbuffer_init
void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
Initialize a spa_ringbuffer with size.
Definition: ringbuffer.h:58
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition: defs.h:235
spa_ringbuffer::readindex
uint32_t readindex
Definition: ringbuffer.h:47