Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_RINGBUFFER_H
#define SPA_RINGBUFFER_H
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
struct spa_ringbuffer;
#include <spa/utils/defs.h>
#ifndef SPA_API_RINGBUFFER
#ifdef SPA_API_IMPL
#define SPA_API_RINGBUFFER SPA_API_IMPL
#else
#define SPA_API_RINGBUFFER static inline
#endif
#endif
struct spa_ringbuffer {
uint32_t readindex; /*< the current read index */
uint32_t writeindex; /*< the current write index */
};
#define SPA_RINGBUFFER_INIT() ((struct spa_ringbuffer) { 0, 0 })
* Initialize a spa_ringbuffer with \a size.
*
* \param rbuf a spa_ringbuffer
*/
SPA_API_RINGBUFFER void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
{
*rbuf = SPA_RINGBUFFER_INIT();
}
* \param size the target size of \a rbuf
*/
SPA_API_RINGBUFFER void spa_ringbuffer_set_avail(struct spa_ringbuffer *rbuf, uint32_t size)
{
rbuf->readindex = 0;
rbuf->writeindex = size;
}
* \param rbuf a spa_ringbuffer
* \param index the value of readindex, should be taken modulo the size of the
* ringbuffer memory to get the offset in the ringbuffer memory
* \return number of available bytes to read. values < 0 mean
* there was an underrun. values > rbuf->size means there
* was an overrun.
*/
SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_read_index(struct spa_ringbuffer *rbuf, uint32_t *index)
{
*index = __atomic_load_n(&rbuf->readindex, __ATOMIC_RELAXED);
return (int32_t) (__atomic_load_n(&rbuf->writeindex, __ATOMIC_ACQUIRE) - *index);
}
*
* \param rbuf a struct \ref spa_ringbuffer
* \param buffer memory to read from
* \param size the size of \a buffer
* \param offset offset in \a buffer to read from
* \param data destination memory
* \param len number of bytes to read
*/
SPA_API_RINGBUFFER void
spa_ringbuffer_read_data(struct spa_ringbuffer *rbuf SPA_UNUSED,
const void *buffer, uint32_t size,
uint32_t offset, void *data, uint32_t len)
{
uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
spa_memcpy(data, SPA_PTROFF(buffer, offset, void), l0);
if (SPA_UNLIKELY(l1 > 0))
spa_memcpy(SPA_PTROFF(data, l0, void), buffer, l1);
}
SPA_API_RINGBUFFER void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
{
__atomic_store_n(&rbuf->readindex, index, __ATOMIC_RELEASE);
}
* \param index the value of writeindex, should be taken modulo the size of the
* ringbuffer memory to get the offset in the ringbuffer memory
* \return the fill level of \a rbuf. values < 0 mean
* there was an underrun. values > rbuf->size means there
* was an overrun. Subtract from the buffer size to get
* the number of bytes available for writing.
*/
SPA_API_RINGBUFFER int32_t spa_ringbuffer_get_write_index(struct spa_ringbuffer *rbuf, uint32_t *index)
{
*index = __atomic_load_n(&rbuf->writeindex, __ATOMIC_RELAXED);
return (int32_t) (*index - __atomic_load_n(&rbuf->readindex, __ATOMIC_ACQUIRE));
}
*
* \param rbuf a spa_ringbuffer
* \param buffer memory to write to
* \param size the size of \a buffer
* \param offset offset in \a buffer to write to
* \param data source memory
* \param len number of bytes to write
*/
SPA_API_RINGBUFFER void
spa_ringbuffer_write_data(struct spa_ringbuffer *rbuf SPA_UNUSED,
void *buffer, uint32_t size,
uint32_t offset, const void *data, uint32_t len)
{
uint32_t l0 = SPA_MIN(len, size - offset), l1 = len - l0;
spa_memcpy(SPA_PTROFF(buffer, offset, void), data, l0);
if (SPA_UNLIKELY(l1 > 0))
spa_memcpy(buffer, SPA_PTROFF(data, l0, void), l1);
}
SPA_API_RINGBUFFER void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
{
__atomic_store_n(&rbuf->writeindex, index, __ATOMIC_RELEASE);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#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:309
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:398
spa_memcpy
#define spa_memcpy(d, s, n)
Definition defs.h:528
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