This is a simple Linux kernel-style FIFO (First In, First Out) queue implementation. 😊
The internal structure consists of a circular buffer with thread-safe operations. When the buffer is full, new data will be rejected. The implementation uses power-of-two sizes for efficient wraparound operations.
The following operations are based on the root directory of the current project, please ensure to perform them correctly!
cd z_kfifo
make
cd lib
ls
libtestlib.a libz_kfifo.a // Compilation completed// Step 1: Create and initialize a FIFO
struct z_kfifo_struct fifo;
if (z_kfifo_malloc(&fifo, 1024) == 0) { // Create a 1024-byte FIFO
printf("FIFO created successfully\n");
}
// Step 2: Write data to FIFO
char data[] = "Hello, FIFO!";
uint32_t written = z_kfifo_in(&fifo, data, strlen(data));
printf("Written %u bytes to FIFO\n", written);
// Step 3: Read data from FIFO
char buffer[64];
uint32_t read = z_kfifo_out(&fifo, buffer, sizeof(buffer));
printf("Read %u bytes from FIFO: %.*s\n", read, read, buffer);
// Step 4: Clean up
z_kfifo_free(&fifo);// Initialize FIFO with existing buffer
void z_kfifo_init(struct z_kfifo_struct *p_fifo, void *p_buffer, uint32_t size);
// Allocate and initialize FIFO
int z_kfifo_malloc(struct z_kfifo_struct *p_fifo, uint32_t size);
// Write data to FIFO
uint32_t z_kfifo_in(struct z_kfifo_struct *p_fifo, const void *p_from, uint32_t len);
// Read data from FIFO
uint32_t z_kfifo_out(struct z_kfifo_struct *p_fifo, void *p_to, uint32_t len);
// Peek data without removing it
uint32_t z_kfifo_out_check(struct z_kfifo_struct *p_fifo, void *p_to, uint32_t len);
// Get available space
uint32_t z_kfifo_space(struct z_kfifo_struct *p_fifo);
// Get amount of data in FIFO
uint32_t z_kfifo_data_len(struct z_kfifo_struct *p_fifo);
// Free FIFO resources
void z_kfifo_free(struct z_kfifo_struct *p_fifo);The key files are:
z_kfifo.c # FIFO implementation
z_kfifo.h # FIFO header file
z_debug.h # Debug information toggle
z_tool.h # Utility macros and functions
test.c # Test program
- Kernel-style circular buffer implementation
- Thread-safe operations (when enabled)
- Power-of-two sizes for efficient wraparound
- Memory barrier support for multi-core systems
- Zero-copy peek operations
- Comprehensive error handling
- Clean and maintainable code structure
This project implements a kernel-style FIFO queue similar to Linux kernel's kfifo, 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, when compiled with Z_KFIFO_THREAD_SAFE defined, it uses spinlocks for 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!

