Allocator agnostic ring buffer.
Find a file
Imbus a6651f05a5
All checks were successful
ci/woodpecker/push/test Pipeline was successful
CI
2024-12-25 20:04:46 +01:00
.woodpecker CI 2024-12-25 20:04:46 +01:00
test Basic tests implemented using cmocka 2024-12-25 14:05:22 +01:00
.clang-format clang-format: Short loops on single line 2024-12-25 14:00:38 +01:00
.gitignore gitignore for chache, json and .elf 2024-12-25 14:04:23 +01:00
driver.c Move certain debug related functionality into header/source, feature gated by DEBUG flag 2024-12-25 14:03:47 +01:00
LICENSE MIT license included 2024-06-23 14:30:52 +02:00
Makefile Basic tests implemented using cmocka 2024-12-25 14:05:22 +01:00
README.md Extended readme 2024-12-25 14:03:57 +01:00
ringbuf.c Move certain debug related functionality into header/source, feature gated by DEBUG flag 2024-12-25 14:03:47 +01:00
ringbuf.h Move certain debug related functionality into header/source, feature gated by DEBUG flag 2024-12-25 14:03:47 +01:00

RingBuf

RingBuf is an allocator-agnostic, non-overwriting circular/ring buffer implementation in C99.
See: Circular Buffer (Wikipedia)

Features

  • Space Efficiency The code is designed to be portable and flexible. The inspiration initially came to me when designing a network driver. When operating in memory constrained environments, every byte is of upmost importance. Traditional metaprogramming such as templating in C++ and template metaprogramming in C, although generic has the side effect of expanding into discrete machine code specific for the specialization applied, essentially scaling (spatially) linear with the amount of different datatypes we specialize on. This implementation circumvents this by treating all data as a void pointer with a length. This implies no deep copies.

  • Allocator Agnostic Another common characteristic of memory constrained environments are custom malloc implementations and/or variations.

    • Signatures
    • Arena

Design considerations

  • Holding a reference to malloc internally would make a tidier interface
  • Passing a user-defined function for deep-copies would enable certain applications

Usage

In essence:

int value = 42;
struct RingBuf rb;
rb_init(&rb, 10, malloc, sizeof(int));
rb_push_back(&rb, (void *)&data, memcpy);
rb_pop_front(&rb, &value, memcpy);

Most of these functions return Enum result types. See: ringbuf.h.

Future Improvements

  • Trim the code
  • Reduce boilerplate in tests
  • Reduce the number of tests in exchange for better test fit