Files
mop3/kernel/libk/ringbuffer.h
kamkow1 690e09339e
All checks were successful
Build documentation / build-and-deploy (push) Successful in 1m55s
procgroup capabilities
2026-02-14 20:48:38 +01:00

37 lines
1.8 KiB
C

#ifndef _KERNEL_LIBK_RINGBUFFER_H
#define _KERNEL_LIBK_RINGBUFFER_H
#include <libk/std.h>
struct ringbuffer {
void* buffer;
size_t tail;
size_t head;
size_t capacity;
size_t count;
bool full;
};
bool ringbuffer_init (struct ringbuffer* rb, size_t capacity, size_t type_size);
void ringbuffer_fini (struct ringbuffer* rb);
#define ringbuffer_pop(type, rb, data) \
do { \
if ((rb)->count != 0) { \
*(data) = ((type*)(rb)->buffer)[(rb)->tail]; \
(rb)->tail = ((rb)->tail + 1) % (rb)->capacity; \
(rb)->count--; \
} \
} while (0)
#define ringbuffer_push(type, rb, data) \
do { \
if ((rb)->count != (rb)->capacity) { \
((type*)(rb)->buffer)[(rb)->head] = (data); \
(rb)->head = ((rb)->head + 1) % (rb)->capacity; \
(rb)->count++; \
} \
} while (0)
#endif // _KERNEL_LIBK_RINGBUFFER_H