This is a high-performance ring buffer implementation with record-based storage and thread safety. 😊
The internal structure consists of a circular buffer with record-based storage and thread-safe operations. The implementation uses power-of-two sizes for efficient wraparound operations and memory barriers for data consistency.
- Record-based storage with headers
- Thread-safe operations
- Power-of-2 size for efficient wraparound
- Memory barrier support
- Zero-copy operations
- Comprehensive error handling
// Create a ring buffer
struct z_ringbuf *rb = z_ringbuf_create(1024, 0);
if (!rb) {
printf("Failed to create ring buffer\n");
return -1;
}
// Write data
const char *data = "Hello, Ring Buffer!";
size_t written = z_ringbuf_write(rb, data, strlen(data));
printf("Written %zu bytes\n", written);
// Read data
char buffer[64];
size_t read = z_ringbuf_read(rb, buffer, sizeof(buffer));
printf("Read %zu bytes: %.*s\n", read, (int)read, buffer);
// Free resources
z_ringbuf_free(rb);
// Create and initialize a ring buffer
struct z_ringbuf *z_ringbuf_create(size_t size, int flags);
// Write data to ring buffer
size_t z_ringbuf_write(struct z_ringbuf *rb, const void *data, size_t size);
// Read data from ring buffer
size_t z_ringbuf_read(struct z_ringbuf *rb, void *data, size_t size);
// Get available data size
size_t z_ringbuf_data_size(const struct z_ringbuf *rb);
// Get available space
size_t z_ringbuf_space(const struct z_ringbuf *rb);
// Free ring buffer resources
void z_ringbuf_free(struct z_ringbuf *rb);
inc/z_ringbuf.h # Ring buffer header file
src/z_ringbuf.c # Ring buffer implementation
src/test.c # Test program
inc/z_debug.h # Debug utilities
inc/z_tool.h # Common tools and macros
# Build the project
make
# Run tests
./build/z_kfifo_test
This project implements a high-performance ring buffer with record-based storage, providing an efficient and thread-safe circular buffer implementation for various applications.
Q: Why use power-of-two sizes? A: Power-of-two sizes allow efficient modulo operations using bitwise AND, improving performance.
Q: Is it thread-safe? A: Yes, the implementation uses memory barriers and atomic operations to ensure thread safety.
Thank you for taking the time to read our project documentation. If you find this project helpful, please support us with a Star. Thank you!