42 #ifndef TPCircularBuffer_h
43 #define TPCircularBuffer_h
55 #include <stdatomic.h>
88 #define TPCircularBufferInit(buffer, length) \
89 _TPCircularBufferInit(buffer, length, sizeof(*buffer))
138 *availableBytes = buffer->fillCount;
139 if ( *availableBytes == 0 )
return NULL;
140 return (
void*)((
char*)buffer->buffer + buffer->tail);
151 static __inline__ __attribute__((always_inline)) void TPCircularBufferConsume(
TPCircularBuffer *buffer, int32_t
amount) {
152 buffer->tail = (buffer->tail +
amount) % buffer->length;
153 if ( buffer->atomic ) {
154 atomic_fetch_add(&buffer->fillCount, -amount);
156 buffer->fillCount -=
amount;
158 assert(buffer->fillCount >= 0);
171 static __inline__ __attribute__((always_inline)) void* TPCircularBufferHead(
TPCircularBuffer *buffer, int32_t* availableBytes) {
172 *availableBytes = (buffer->length - buffer->fillCount);
173 if ( *availableBytes == 0 )
return NULL;
174 return (
void*)((
char*)buffer->buffer + buffer->head);
188 buffer->head = (buffer->head +
amount) % buffer->length;
189 if ( buffer->atomic ) {
190 atomic_fetch_add(&buffer->fillCount, amount);
192 buffer->fillCount +=
amount;
194 assert(buffer->fillCount <= buffer->length);
207 static __inline__ __attribute__((always_inline)) bool TPCircularBufferProduceBytes(
TPCircularBuffer *buffer, const
void*
src, int32_t
len) {
209 void *
ptr = TPCircularBufferHead(buffer, &space);
210 if ( space < len )
return false;
220 void TPCircularBufferConsumeNoBarrier(
TPCircularBuffer *buffer, int32_t amount) {
221 buffer->tail = (buffer->tail +
amount) % buffer->length;
222 buffer->fillCount -= amount;
223 assert(buffer->fillCount >= 0);
230 void TPCircularBufferProduceNoBarrier(
TPCircularBuffer *buffer, int32_t amount) {
231 buffer->head = (buffer->head +
amount) % buffer->length;
232 buffer->fillCount += amount;
233 assert(buffer->fillCount <= buffer->length);