34 lines
835 B
C
34 lines
835 B
C
#include "ring_buffer.h"
|
|
#include "string.h"
|
|
|
|
void ring_buffer_init(struct ring_buffer *buffer, u32 el_size)
|
|
{
|
|
buffer->start = 0;
|
|
buffer->end = 0;
|
|
buffer->element_size = el_size;
|
|
buffer->size = 0;
|
|
}
|
|
|
|
void ring_buffer_push(struct ring_buffer *buffer, void *el)
|
|
{
|
|
if (buffer->size >= RING_BUFFER_SIZE)
|
|
return;
|
|
|
|
char *dst = buffer->buffer + buffer->start;
|
|
memcpy(dst, el, buffer->element_size);
|
|
buffer->start = (buffer->start + buffer->element_size) % RING_BUFFER_SIZE;
|
|
buffer->size += buffer->element_size;
|
|
}
|
|
|
|
void *ring_buffer_pop(struct ring_buffer *buffer)
|
|
{
|
|
if (buffer->size == 0)
|
|
return NULL;
|
|
|
|
void *res = buffer->buffer + buffer->end;
|
|
buffer->end = (buffer->end + buffer->element_size) % RING_BUFFER_SIZE;
|
|
buffer->size -= buffer->element_size;
|
|
|
|
return res;
|
|
}
|