An opaque FIFO module: fifo.h and fifo.c
Implement a byte FIFO whose layout only fifo.c knows: callers hold a pointer and use four functions. A medium embedded C problem (Buffers and data…
medium · Buffers and data structures · buffers · headers · encapsulation
fifo.h below is the interface of a byte FIFO module that two drivers share, one FIFO each. It declares fifo_t without saying what is inside: the type is opaque, so the rest of the firmware can only hold a fifo_t * and call the functions. Write fifo.c:
struct fifoitself:FIFO_CAPACITYbytes, and whatever you need to track them.fifo_create()hands out a FIFO from a pool ofFIFO_INSTANCES, defined infifo.c(there is no heap), and returnsNULLonce they are all taken.fifo_putadds a byte and returnsfalseif the FIFO is full;fifo_gettakes the oldest byte and returnsfalseif it is empty;fifo_countsays how many bytes it holds.
The tests include fifo.h only, so they cannot see inside a fifo_t, and neither should anything else.
Starter code
// ===== fifo.h =====
// fifo.h: a byte FIFO whose layout is private to fifo.c.
#ifndef FIFO_H
#define FIFO_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define FIFO_CAPACITY 8u
#define FIFO_INSTANCES 2u
typedef struct fifo fifo_t; // defined in fifo.c: callers only hold pointers
fifo_t *fifo_create(void);
bool fifo_put(fifo_t *f, uint8_t byte);
bool fifo_get(fifo_t *f, uint8_t *byte);
size_t fifo_count(const fifo_t *f);
#endif
// ===== fifo.c =====
#include "fifo.h"
// struct fifo, the pool of FIFO_INSTANCES and the four functions.Example tests
First in, first out
fifo_t *f = fifo_create();
fifo_put(f, 1);
fifo_put(f, 2);
fifo_put(f, 3);
uint8_t b;
while (fifo_get(f, &b)) printf("%u ", b);
printf("count %u\n", (unsigned)fifo_count(f));Expected output:
1 2 3 count 0Learn it first
- Buffers and data structures · Ring buffers, pools, lists and bitmaps on static memory.
More Buffers and data structures problems
- Ring buffer · Implement put and get for a fixed-size FIFO.
- Fill level from two indices · Compute how many items a FIFO holds from its head and tail.
- Overwriting log buffer · Keep only the most recent entries, replacing the oldest when full.
- Fixed-size stack · Push and pop on an array-backed stack with overflow and underflow checks.
- Sorted insert into a linked list · Insert a caller-provided node into an ascending singly linked list.
- Reverse a linked list · Reverse a singly linked list in place.
- Fixed-block memory pool · Allocate and free fixed-size blocks from a static pool.
- Small priority queue · Keep pending jobs ordered by priority in a fixed array.
- Bitmap operations · Set, clear and test individual bits in a byte array.
- First free slot · Find the first zero bit in a bitmap of a given length.
- Sample history by age · Store the last few samples and read them back by how old they are.
- Bump allocator · Hand out aligned chunks from a static buffer.
- Fix: the console drops characters · A serial console echoes the wrong characters once its receive buffer wraps.
- Bluetooth speaker: never overwrite unplayed audio · A sample ring buffer that refuses new samples when full instead of overwriting the ones not yet played.