+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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 | #include <string.h> |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | /** |
| 15 | * \defgroup spa_ringbuffer Ringbuffer |
| 16 | * Ring buffer implementation |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * \addtogroup spa_ringbuffer |
| 21 | * \{ |
| 22 | */ |
| 23 | |
| 24 | struct spa_ringbuffer; |
| 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 | |
| 36 | /** |
| 37 | * A ringbuffer type. |
| 38 | */ |
| 39 | struct 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 | |
| 46 | /** |
| 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 | |
| 56 | /** |
| 57 | * Sets the pointers so that the ringbuffer contains \a size bytes. |
| 58 | * |
| 59 | * \param rbuf a spa_ringbuffer |
| 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 | |
| 68 | /** |
| 69 | * Get the read index and available bytes for reading. |
| 70 | * |
| 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 | |
| 84 | /** |
| 85 | * Read \a len bytes from \a rbuf starting \a offset. \a offset must be taken |
| 86 | * modulo \a size and len should be smaller than \a size. |
| 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 | |
| 106 | /** |
| 107 | * Update the read pointer to \a index. |
| 108 | * |
| 109 | * \param rbuf a spa_ringbuffer |
| 110 | * \param index new index |
| 111 | */ |
| 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 | |
| 117 | /** |
| 118 | * Get the write index and the number of bytes inside the ringbuffer. |
| 119 | * |
| 120 | * \param rbuf a spa_ringbuffer |
| 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 | |
| 134 | /** |
| 135 | * Write \a len bytes to \a buffer starting \a offset. \a offset must be taken |
| 136 | * modulo \a size and len should be smaller than \a size. |
| 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 | |
| 156 | /** |
| 157 | * Update the write pointer to \a index |
| 158 | * |
| 159 | * \param rbuf a spa_ringbuffer |
| 160 | * \param index new index |
| 161 | */ |
| 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 | |
| 167 | /** |
| 168 | * \} |
| 169 | */ |
| 170 | |
| 171 | |
| 172 | #ifdef __cplusplus |
| 173 | } /* extern "C" */ |
| 174 | #endif |
| 175 | |
| 176 | #endif /* SPA_RINGBUFFER_H */ |