Fumofumotris/source/datastructures/ringbuffer.c

67 lines
1.6 KiB
C
Raw Normal View History

2024-04-25 20:08:24 +00:00
#include "ringbuffer.h"
#include <string.h>
2024-04-29 20:01:48 +00:00
struct ring_buf {
2024-04-25 20:08:24 +00:00
struct RingBufferHead head;
2024-04-29 20:01:48 +00:00
u8 bytes[];
2024-04-25 20:08:24 +00:00
};
2024-04-29 20:01:48 +00:00
void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i)
2024-04-25 20:08:24 +00:00
{
2024-04-29 20:01:48 +00:00
struct ring_buf *ring = (struct ring_buf *)head;
return ring->bytes + T->SIZE * i;
2024-04-25 20:08:24 +00:00
}
2024-04-29 20:01:48 +00:00
size_t RingBufferEmpty(RingBufferT T, struct RingBufferHead *head)
2024-04-25 20:08:24 +00:00
{
2024-04-29 20:01:48 +00:00
return T->LEN - head->len;
2024-04-25 20:08:24 +00:00
}
2024-04-29 20:01:48 +00:00
void *RingBufferGet(RingBufferT T, struct RingBufferHead *head, size_t i)
{
size_t wrap_i = (head->start + i) % T->LEN;
return get_ptr(T, head, wrap_i);
}
void *RingBufferNext(RingBufferT T, struct RingBufferHead *head)
{
size_t wrap_i = (head->start + head->len) % T->LEN;
return get_ptr(T, head, wrap_i);
}
void RingBufferAdd(RingBufferT T, struct RingBufferHead *dest, void *item)
{
memcpy(RingBufferNext(T, dest), item, T->SIZE);
}
void RingBufferTransfer(
RingBufferT T,
struct RingBufferHead *dest,
struct RingBufferHead *src
) {
size_t copy_max = min_size(T->LEN - dest->len, src->len);
for (size_t i = 0; i < copy_max; i++) {
void *to = RingBufferGet(T, dest_head, dest->len + i);
void *from = RingBufferGet(T, src_head, i);
memcpy(to, from, T->SIZE);
}
dest->len += copy_max;
src->len -= copy_max;
}
size_t RingBufferOut(RingBufferT T, size_t n, void *dest, void *src_head)
{
struct ring_buf *src = src_head;
2024-04-25 20:08:24 +00:00
2024-04-29 20:01:48 +00:00
size_t copy_max = min_size(n, src->head.len);
2024-04-25 20:08:24 +00:00
for (size_t i = 0; i < copy_max; i++) {
2024-04-29 20:01:48 +00:00
void *to = (char *)dest + i * T->SIZE;
void *from = RingBufferGet(T, src, i);
memcpy(to, from, T->SIZE);
2024-04-25 20:08:24 +00:00
}
2024-04-29 20:01:48 +00:00
return copy_max;
2024-04-25 20:08:24 +00:00
}