ringbuffer.h
Go to the documentation of this file.
/* Simple Plugin API
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_RINGBUFFER_H
#define SPA_RINGBUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
struct spa_ringbuffer;
#include <string.h>
#include <spa/utils/defs.h>
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 }
static inline void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
{
*rbuf = SPA_RINGBUFFER_INIT();
}
static inline void spa_ringbuffer_set_avail(struct spa_ringbuffer *rbuf, uint32_t size)
{
rbuf->readindex = 0;
rbuf->writeindex = size;
}
static inline 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);
}
static inline void
spa_ringbuffer_read_data(struct spa_ringbuffer *rbuf,
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);
}
static inline void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
{
__atomic_store_n(&rbuf->readindex, index, __ATOMIC_RELEASE);
}
static inline 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));
}
static inline void
spa_ringbuffer_write_data(struct spa_ringbuffer *rbuf,
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);
}
static inline 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
static 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:81
spa_ringbuffer_init
static void spa_ringbuffer_init(struct spa_ringbuffer *rbuf)
Initialize a spa_ringbuffer with size.
Definition: ringbuffer.h:70
spa_ringbuffer_read_update
static void spa_ringbuffer_read_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the read pointer to index.
Definition: ringbuffer.h:131
SPA_RINGBUFFER_INIT
#define SPA_RINGBUFFER_INIT()
Definition: ringbuffer.h:63
spa_ringbuffer_write_update
static void spa_ringbuffer_write_update(struct spa_ringbuffer *rbuf, int32_t index)
Update the write pointer to index.
Definition: ringbuffer.h:181
spa_ringbuffer_read_data
static 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:115
spa_ringbuffer_get_read_index
static 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:97
spa_ringbuffer_write_data
static 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:165
spa_ringbuffer_get_write_index
static 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:147
SPA_MIN
#define SPA_MIN(a, b)
Definition: defs.h:146
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition: defs.h:315
spa_memcpy
#define spa_memcpy(d, s, n)
Definition: defs.h:401
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:183
string.h
spa/utils/string.h
spa_ringbuffer
A ringbuffer type.
Definition: ringbuffer.h:57
spa_ringbuffer::readindex
uint32_t readindex
Definition: ringbuffer.h:58
spa_ringbuffer::writeindex
uint32_t writeindex
Definition: ringbuffer.h:59