ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
ringbuffer.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define RINGBUFFER_CLIENT_ID_BUFFER_SIZE 64
9
10#include <recast/defer.h>
12#include <ascii-chat/common.h>
13#include <ascii-chat/asciichat_errno.h> // For asciichat_errno system
14#include <ascii-chat/buffer.h>
15#include <ascii-chat/util/math.h> // For power-of-two utilities
16#include <ascii-chat/util/bits.h> // For is_power_of_two, next_power_of_two
17#include <libplatform/atomic.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <assert.h>
23
24/* ============================================================================
25 * Ring Buffer Implementation
26 * ============================================================================
27 *
28 * THREAD SAFETY NOTE: This ring buffer is designed for single-producer,
29 * single-consumer (SPSC) use only. The atomic operations provide memory
30 * ordering guarantees but do NOT support concurrent writes from multiple
31 * producers. For multi-writer scenarios, external synchronization is required.
32 */
33
34ringbuffer_t *ringbuffer_create(size_t element_size, size_t capacity) {
35 if (element_size == 0 || capacity == 0) {
36 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ring buffer parameters: element_size=%zu, capacity=%zu", element_size, capacity);
37 return NULL;
38 }
39
40 ringbuffer_t *rb;
41 rb = SAFE_CALLOC(1, sizeof(ringbuffer_t), ringbuffer_t *);
42
43 /* Round capacity up to power of 2 for optimization */
44 size_t actual_capacity = math_next_power_of_two(capacity);
45
46 rb->buffer = SAFE_CALLOC(actual_capacity, element_size, char *);
47
48 rb->element_size = element_size;
49 rb->capacity = actual_capacity;
50 rb->is_power_of_two = true;
51 rb->capacity_mask = actual_capacity - 1;
52 /* Atomic fields are zero-initialized by SAFE_CALLOC */
53 NAMED_REGISTER_ATOMIC(&rb->head, "ringbuffer.head", NULL);
54 NAMED_REGISTER_ATOMIC(&rb->tail, "ringbuffer.tail", NULL);
55 NAMED_REGISTER_ATOMIC(&rb->size, "ringbuffer.size", NULL);
56
57 return rb;
58}
59
61 if (rb) {
65 SAFE_FREE(rb->buffer);
66 SAFE_FREE(rb);
67 }
68}
69
70bool ringbuffer_write(ringbuffer_t *rb, const void *data) {
71 if (!rb || !data)
72 return false;
73
74 size_t current_size = atomic_load_u64(&rb->size);
75 if (current_size >= rb->capacity) {
76 return false; /* Buffer full */
77 }
78
79 size_t head = atomic_load_u64(&rb->head);
80 size_t next_head = (head + 1) & rb->capacity_mask;
81
82 /* Copy data */
83 SAFE_MEMCPY(rb->buffer + (head * rb->element_size), rb->element_size, data, rb->element_size);
84
85 /* Update head and size atomically */
86 atomic_store_u64(&rb->head, next_head);
88
89 return true;
90}
91
92bool ringbuffer_read(ringbuffer_t *rb, void *data) {
93 if (!rb || !data)
94 return false;
95
96 size_t current_size = atomic_load_u64(&rb->size);
97 if (current_size == 0) {
98 return false; /* Buffer empty */
99 }
100
101 size_t tail = atomic_load_u64(&rb->tail);
102 size_t next_tail = (tail + 1) & rb->capacity_mask;
103
104 /* Copy data */
105 SAFE_MEMCPY(data, rb->element_size, rb->buffer + (tail * rb->element_size), rb->element_size);
106
107 /* Update tail and size atomically */
108 atomic_store_u64(&rb->tail, next_tail);
109 atomic_fetch_sub_u64(&rb->size, 1);
110
111 return true;
112}
113
114bool ringbuffer_peek(ringbuffer_t *rb, void *data) {
115 if (!rb || !data)
116 return false;
117
118 size_t current_size = atomic_load_u64(&rb->size);
119 if (current_size == 0) {
120 return false; /* Buffer empty */
121 }
122
123 size_t tail = atomic_load_u64(&rb->tail);
124
125 /* Copy data without updating tail */
126 SAFE_MEMCPY(data, rb->element_size, rb->buffer + (tail * rb->element_size), rb->element_size);
127
128 return true;
129}
130
131size_t ringbuffer_size(const ringbuffer_t *rb) {
132 return rb ? atomic_load_u64(&rb->size) : 0;
133}
134
136 return ringbuffer_size(rb) == 0;
137}
138
140 return rb ? ringbuffer_size(rb) >= rb->capacity : true;
141}
142
144 if (rb) {
145 atomic_store_u64(&rb->head, 0);
146 atomic_store_u64(&rb->tail, 0);
147 atomic_store_u64(&rb->size, 0);
148 }
149}
150
151/* ============================================================================
152 * Frame Buffer Implementation
153 * ============================================================================
154 */
155
157 if (capacity == 0) {
158 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid frame buffer parameters");
159 return NULL;
160 }
161
163
164 // Initialize mutex for thread-safe access
165 if (mutex_init(&fb->mutex, "framebuffer") != 0) {
166 SET_ERRNO(ERROR_THREAD, "Failed to initialize framebuffer mutex");
167 return NULL;
168 }
169
170 defer_named(framebuffer_mutex, mutex_destroy(&fb->mutex));
171
172 // Create ringbuffer to store frame_t structs
173 fb->rb = ringbuffer_create(sizeof(frame_t), capacity);
174 if (!fb->rb) {
175 SET_ERRNO(ERROR_MEMORY, "Failed to allocate frame buffer");
176 return NULL;
177 }
178
179 defer_cancel(framebuffer_mutex);
180 framebuffer_t *result = fb;
181 fb = NULL;
182 return result;
183}
184
186 if (capacity == 0) {
187 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid capacity: %zu", capacity);
188 return NULL;
189 }
190
192
193 // Initialize mutex for thread-safe access
194 if (mutex_init(&fb->mutex, "framebuffer_multi") != 0) {
195 SET_ERRNO(ERROR_THREAD, "Failed to initialize framebuffer mutex");
196 return NULL;
197 }
198
199 defer_named(framebuffer_mutex, mutex_destroy(&fb->mutex));
200
201 // Create ringbuffer to store multi_source_frame_t structs
202 fb->rb = ringbuffer_create(sizeof(multi_source_frame_t), capacity);
203
204 if (!fb->rb) {
205 return NULL;
206 }
207
208 defer_cancel(framebuffer_mutex);
209 framebuffer_t *result = fb;
210 fb = NULL;
211 return result;
212}
213
215 if (!fb)
216 return;
217
218 // Add magic number check to detect double-free using rb pointer
219 if (fb->rb == (ringbuffer_t *)0xDEADBEEF) {
220 SET_ERRNO(ERROR_INVALID_STATE, "DOUBLE-FREE DETECTED: framebuffer %p already destroyed!", fb);
221 return;
222 }
223
226 mutex_destroy(&fb->mutex);
227
228 // Mark as destroyed before freeing
229 fb->rb = (ringbuffer_t *)0xDEADBEEF;
230 SAFE_FREE(fb);
231}
232
233bool framebuffer_write_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size) {
234 if (!fb || !frame_data || frame_size == 0)
235 return false;
236
237 // Validate frame size to prevent overflow
238 if (frame_size > 10 * 1024 * 1024) { // 10MB max for ASCII frames
239 SET_ERRNO(ERROR_INVALID_PARAM, "Rejecting oversized frame: %zu bytes", frame_size);
240 return false;
241 }
242
243 // Thread-safe access to framebuffer (was missing, causing race conditions)
244 mutex_lock(&fb->mutex);
245
246 // Check if buffer is full - if so, we need to drop the oldest frame
247 if (ringbuffer_size(fb->rb) >= fb->rb->capacity) {
248 // Buffer is full, read and free the oldest frame before writing new one
249 frame_t old_frame;
250 if (ringbuffer_read(fb->rb, &old_frame)) {
251 if (IS_FRAME_VALID(&old_frame)) {
252 MARK_FRAME_FREED(&old_frame);
253 // Use buffer_release since data was allocated with buffer_alloc
254 buffer_release(old_frame.data);
255 } else if (!IS_MAGIC_VALID(old_frame.magic, FRAME_MAGIC)) {
256 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid old frame magic 0x%x when dropping", old_frame.magic);
257 }
258 }
259 }
260
261 // Allocate a copy of the frame data using buffer domain for better performance
262 char *frame_copy = (char *)buffer_alloc(NULL, frame_size + 1);
263 if (!frame_copy) {
264 mutex_unlock(&fb->mutex);
265 SET_ERRNO(ERROR_MEMORY, "Failed to allocate %zu bytes from buffer domain for frame", frame_size + 1);
266 return false;
267 }
268
269 SAFE_MEMCPY(frame_copy, frame_size, frame_data, frame_size);
270 frame_copy[frame_size] = '\0'; // Ensure null termination
271
272 // Create a frame_t struct with the copy (store allocated size for proper cleanup)
273 frame_t frame = {.magic = MAGIC_FRAME_VALID, .size = frame_size + 1, .data = frame_copy};
274
275 bool result = ringbuffer_write(fb->rb, &frame);
276
277 if (!result) {
278 // If we still couldn't write to ringbuffer, return the buffer to pool
279 buffer_release(frame_copy);
280 SET_ERRNO(ERROR_INVALID_STATE, "Failed to write frame to ringbuffer even after dropping oldest");
281 }
282
283 mutex_unlock(&fb->mutex);
284 return result;
285}
286
288 if (!fb || !frame) {
289 return false;
290 }
291
292 // Initialize frame to safe values
293 frame->magic = 0;
294 frame->data = NULL;
295 frame->size = 0;
296
297 // Thread-safe access to framebuffer (fixes TOCTOU race condition)
298 mutex_lock(&fb->mutex);
299
300 bool result = ringbuffer_read(fb->rb, frame);
301
302 // Validate the frame we just read
303 if (result) {
304 if (!IS_MAGIC_VALID(frame->magic, FRAME_MAGIC)) {
305 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid frame magic 0x%x (expected 0x%x)", frame->magic, FRAME_MAGIC);
306 frame->data = NULL;
307 frame->size = 0;
308 mutex_unlock(&fb->mutex);
309 return false;
310 }
311
312 if (IS_FRAME_FREED(frame)) {
313 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Reading already-freed frame!");
314 frame->data = NULL;
315 frame->size = 0;
316 mutex_unlock(&fb->mutex);
317 return false;
318 }
319
320 if (frame->size > 10 * 1024 * 1024) {
321 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Frame size too large: %zu", frame->size);
322 buffer_release(frame->data);
323 frame->data = NULL;
324 frame->size = 0;
325 mutex_unlock(&fb->mutex);
326 return false;
327 }
328 }
329
330 mutex_unlock(&fb->mutex);
331 return result;
332}
333
335 if (!fb || !fb->rb)
336 return;
337
338 // Thread-safe access to framebuffer (was missing, causing race conditions)
339 mutex_lock(&fb->mutex);
340
341 // Check the element size to determine frame type
342 if (fb->rb->element_size == sizeof(multi_source_frame_t)) {
343 // Multi-source frame buffer - read and free multi_source_frame_t
344 multi_source_frame_t multi_frame;
345 while (ringbuffer_read(fb->rb, &multi_frame)) {
346 if (IS_FRAME_VALID((frame_t *)&multi_frame)) {
347 MARK_FRAME_FREED((frame_t *)&multi_frame); // Mark as freed to detect use-after-free
348 buffer_release(multi_frame.data);
349 } else if (!IS_MAGIC_VALID(multi_frame.magic, FRAME_MAGIC) && multi_frame.magic != 0) {
350 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid multi-source frame magic 0x%x during clear", multi_frame.magic);
351 }
352 }
353 } else if (fb->rb->element_size == sizeof(frame_t)) {
354 // Single-source frame buffer - read and free frame_t
355 frame_t frame;
356 while (ringbuffer_read(fb->rb, &frame)) {
357 if (IS_FRAME_VALID(&frame)) {
358 MARK_FRAME_FREED(&frame); // Mark as freed to detect use-after-free
359 buffer_release(frame.data);
360 } else if (!IS_MAGIC_VALID(frame.magic, FRAME_MAGIC) && frame.magic != 0) {
361 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid frame magic 0x%x during clear", frame.magic);
362 }
363 }
364 } else {
365 SET_ERRNO(ERROR_INVALID_STATE, "Unknown frame buffer type with element size %zu", fb->rb->element_size);
366 }
367
368 // Clear the ringbuffer indices
369 ringbuffer_clear(fb->rb);
370
371 // Zero out the entire buffer to prevent any dangling pointers
372 if (fb->rb->buffer) {
373 // Check for integer overflow before multiplication
374 if (fb->rb->capacity > SIZE_MAX / fb->rb->element_size) {
375 SET_ERRNO(ERROR_INVALID_PARAM, "Buffer size would overflow: capacity=%zu, element_size=%zu", fb->rb->capacity, fb->rb->element_size);
376 mutex_unlock(&fb->mutex);
377 return;
378 }
379 size_t buffer_size = fb->rb->capacity * fb->rb->element_size;
381 }
382
383 mutex_unlock(&fb->mutex);
384}
385
386// Multi-source frame functions for multi-user support
387
388bool framebuffer_write_multi_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size, uint32_t source_client_id, uint32_t frame_sequence, uint32_t timestamp) {
389 if (!fb || !fb->rb || !frame_data || frame_size == 0) {
390 return false;
391 }
392
393 // Allocate memory for frame data using buffer domain for better performance
394 char *data_copy = (char *)buffer_alloc(NULL, frame_size);
395 if (!data_copy) {
396 SET_ERRNO(ERROR_MEMORY, "Failed to allocate %zu bytes from buffer domain for multi-source frame", frame_size);
397 return false;
398 }
399
400 // Copy frame data
401 SAFE_MEMCPY(data_copy, frame_size, frame_data, frame_size);
402
403 // Convert client ID number to string for storage in frame metadata
404 char client_id_str[RINGBUFFER_CLIENT_ID_BUFFER_SIZE];
405 snprintf(client_id_str, sizeof(client_id_str), "%u", source_client_id);
406
407 // Create multi-source frame
408 multi_source_frame_t multi_frame = {
409 .magic = MAGIC_FRAME_VALID,
410 .source_client_id = {0},
411 .frame_sequence = frame_sequence,
412 .timestamp = timestamp,
413 .size = frame_size,
414 .data = data_copy,
415 };
416 SAFE_MEMCPY(multi_frame.source_client_id, sizeof(multi_frame.source_client_id), client_id_str, strlen(client_id_str) + 1);
417
418 // Thread-safe access to framebuffer
419 mutex_lock(&fb->mutex);
420
421 // Try to write to ring buffer
422 bool success = ringbuffer_write(fb->rb, &multi_frame);
423 if (!success) {
424 // Buffer full, return the buffer to pool
425 buffer_release(data_copy);
426 log_debug("Frame buffer full, dropping multi-source frame from client %u", source_client_id);
427 }
428
429 mutex_unlock(&fb->mutex);
430 return success;
431}
432
434 if (!fb || !fb->rb || !frame) {
435 return false;
436 }
437
438 // Thread-safe access to framebuffer
439 mutex_lock(&fb->mutex);
440
441 bool result = ringbuffer_read(fb->rb, frame);
442
443 if (result) {
444 // Validate frame magic
445 if (!IS_MAGIC_VALID(frame->magic, FRAME_MAGIC)) {
446 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid multi-source frame magic 0x%x (expected 0x%x)", frame->magic, FRAME_MAGIC);
447 frame->data = NULL;
448 frame->size = 0;
449 memset(frame->source_client_id, 0, sizeof(frame->source_client_id));
450 mutex_unlock(&fb->mutex);
451 return false;
452 }
453
454 // Additional validation
455 if (frame->size == 0 || !frame->data) {
456 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid multi-source frame data (size=%zu, data=%p)", frame->size, frame->data);
457 mutex_unlock(&fb->mutex);
458 return false;
459 }
460 }
461
462 mutex_unlock(&fb->mutex);
463 return result;
464}
465
467 if (!fb || !fb->rb || !frame) {
468 return false;
469 }
470
471 // Thread-safe access to framebuffer
472 mutex_lock(&fb->mutex);
473
474 // Use ringbuffer_peek to get the frame without consuming it
475 bool result = ringbuffer_peek(fb->rb, frame);
476
477 if (result) {
478 // Validate frame magic
479 if (!IS_MAGIC_VALID(frame->magic, FRAME_MAGIC)) {
480 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid multi-source frame magic 0x%x (expected 0x%x) in peek", frame->magic, FRAME_MAGIC);
481 frame->data = NULL;
482 frame->size = 0;
483 memset(frame->source_client_id, 0, sizeof(frame->source_client_id));
484 mutex_unlock(&fb->mutex);
485 return false;
486 }
487
488 // Additional validation
489 if (frame->size == 0 || !frame->data) {
490 SET_ERRNO(ERROR_INVALID_STATE, "CORRUPTION: Invalid multi-source frame data (size=%zu, data=%p) in peek", frame->size, frame->data);
491 mutex_unlock(&fb->mutex);
492 return false;
493 }
494
495 // IMPORTANT: We need to make a copy of the data since we're not consuming the frame
496 // The original data pointer will remain valid in the ring buffer
497 // Caller is responsible for freeing this copy with buffer_release()
498 char *data_copy;
499 data_copy = (char *)buffer_alloc(NULL, frame->size);
500 if (!data_copy) {
501 SET_ERRNO(ERROR_MEMORY, "Failed to allocate memory for frame data copy in peek");
502 mutex_unlock(&fb->mutex);
503 return false;
504 }
505 SAFE_MEMCPY(data_copy, frame->size, frame->data, frame->size);
506 frame->data = data_copy;
507 }
508
509 mutex_unlock(&fb->mutex);
510 return result;
511}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
uint64_t atomic_fetch_sub_u64(atomic_t *a, uint64_t delta)
Atomically subtract from a uint64_t and return the previous value.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
uint64_t atomic_load_u64(atomic_t *a)
Atomically load a uint64_t value.
🔢 Bit Manipulation Utilities
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
Named object registry for debugging — log identifiable resource names.
int buffer_size
Size of circular buffer.
Definition grep.c:68
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MEMSET(dest, dest_size, ch, count)
#define SAFE_CALLOC(count, size, cast)
#define SAFE_CALLOC_DEFER_FREE(name, count, size, cast)
#define SAFE_MEMCPY(dest, dest_size, src, count)
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_INVALID_PARAM
@ ERROR_THREAD
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
void ringbuffer_clear(ringbuffer_t *rb)
Clear all elements from the buffer.
Definition ringbuffer.c:143
void framebuffer_destroy(framebuffer_t *fb)
Destroy a frame buffer.
Definition ringbuffer.c:214
void framebuffer_clear(framebuffer_t *fb)
Clear all frames from the buffer, freeing their data.
Definition ringbuffer.c:334
bool framebuffer_read_multi_frame(framebuffer_t *fb, multi_source_frame_t *frame)
Read a multi-source frame from the buffer.
Definition ringbuffer.c:433
framebuffer_t * framebuffer_create_multi(size_t capacity)
Create a multi-source frame buffer for multi-user support.
Definition ringbuffer.c:185
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:34
framebuffer_t * framebuffer_create(size_t capacity)
Mutex-protected ring buffer used to exchange video frames.
Definition ringbuffer.c:156
size_t ringbuffer_size(const ringbuffer_t *rb)
Get current number of elements in the buffer.
Definition ringbuffer.c:131
bool framebuffer_read_frame(framebuffer_t *fb, frame_t *frame)
Read a frame from the buffer.
Definition ringbuffer.c:287
bool ringbuffer_is_full(const ringbuffer_t *rb)
Check if buffer is full.
Definition ringbuffer.c:139
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:60
#define FRAME_MAGIC
Magic number indicating a valid frame (0xDEADBEEF) - Alias for backward compatibility.
Definition ringbuffer.h:441
bool ringbuffer_is_empty(const ringbuffer_t *rb)
Check if buffer is empty.
Definition ringbuffer.c:135
bool framebuffer_write_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size)
Write a frame to the buffer.
Definition ringbuffer.c:233
bool framebuffer_write_multi_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size, uint32_t source_client_id, uint32_t frame_sequence, uint32_t timestamp)
Write a multi-source frame to the buffer (for multi-user support).
Definition ringbuffer.c:388
bool framebuffer_peek_latest_multi_frame(framebuffer_t *fb, multi_source_frame_t *frame)
Peek at the latest multi-source frame without removing it.
Definition ringbuffer.c:466
bool ringbuffer_read(ringbuffer_t *rb, void *data)
Try to read an element from the ring buffer (non-blocking).
Definition ringbuffer.c:92
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking).
Definition ringbuffer.c:70
bool ringbuffer_peek(ringbuffer_t *rb, void *data)
Peek at the next element without removing it.
Definition ringbuffer.c:114
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define IS_MAGIC_VALID(magic, expected)
Check if magic number is valid (true if matches expected).
Definition magic.h:34
#define IS_FRAME_VALID(frame)
Check if frame is valid and not freed.
Definition magic.h:37
#define MARK_FRAME_FREED(frame)
Mark a frame as freed.
Definition magic.h:43
#define IS_FRAME_FREED(frame)
Check if frame has been freed (corruption detection).
Definition magic.h:40
#define MAGIC_FRAME_VALID
Magic number for valid ring buffer frames (0xA5C11C4A1 = "ASCIICHAT" in hex).
Definition magic.h:21
🔢 Mathematical Utility Functions
#define RINGBUFFER_CLIENT_ID_BUFFER_SIZE
Definition ringbuffer.c:8
Lock-Free Ring Buffer and Frame Buffer Management.
🔤 String Manipulation and Shell Escaping Utilities
One variable-length payload in the video ring buffer.
Definition ringbuffer.h:398
char * data
Owning frame payload; release with buffer_release() after reading.
Definition ringbuffer.h:404
uint64_t magic
Magic number to detect corruption (FRAME_MAGIC when valid).
Definition ringbuffer.h:400
size_t size
Actual size of frame data in bytes.
Definition ringbuffer.h:402
Mutex-protected ring buffer used to exchange video frames.
Definition ringbuffer.h:456
mutex_t mutex
Mutex for thread-safe access to framebuffer operations.
Definition ringbuffer.h:460
ringbuffer_t * rb
Underlying ring buffer for frame storage.
Definition ringbuffer.h:458
One variable-length payload in the video ring buffer.
Definition ringbuffer.h:419
char * data
Owning frame payload; release with buffer_release() after reading.
Definition ringbuffer.h:431
uint64_t magic
Magic number to detect corruption (FRAME_MAGIC when valid).
Definition ringbuffer.h:421
size_t size
Actual size of frame data in bytes.
Definition ringbuffer.h:429
char source_client_id[MAX_CLIENT_ID_LEN]
Client ID that sent this frame.
Definition ringbuffer.h:423
Audio ring buffer for real-time audio streaming.
Definition ringbuffer.h:94
bool is_power_of_two
Whether capacity is power of 2 (enables bit masking optimization).
Definition ringbuffer.h:108
size_t capacity
Number of elements that can be stored.
Definition ringbuffer.h:100
atomic_t head
Write position (producer) - atomic for lock-free operations.
Definition ringbuffer.h:102
atomic_t size
Current number of elements - atomic for fast size checks.
Definition ringbuffer.h:106
size_t capacity_mask
Mask for fast modulo when capacity is power of 2.
Definition ringbuffer.h:110
atomic_t tail
Read position (consumer) - atomic for lock-free operations.
Definition ringbuffer.h:104
size_t element_size
Size of each element in bytes.
Definition ringbuffer.h:98
char * buffer
The actual buffer memory.
Definition ringbuffer.h:96