mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Organize companion code and materials by book part: part-01/ Foundations part-02/ Kernel & driver fundamentals part-03/ Character & block devices part-04/ Networking & storage part-05/ Buses & advanced topics part-06/ Debugging & performance part-07/ Real-world drivers appendices/ Supplementary material README.md Overview and build instructions
29 lines
798 B
C
29 lines
798 B
C
#ifndef CBUF_H
|
|
#define CBUF_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/* Small error codes mirroring common patterns. */
|
|
enum cb_err { CB_OK = 0, CB_EFULL = 28, CB_EEMPTY = 35 };
|
|
|
|
/*
|
|
* head points to the next write position.
|
|
* tail points to the next read position.
|
|
* We leave one slot empty so that head == tail means empty, and
|
|
* (head + 1) % cap == tail means full. This keeps the logic simple.
|
|
*/
|
|
typedef struct {
|
|
size_t cap; /* number of slots in 'data' */
|
|
size_t head; /* next write index */
|
|
size_t tail; /* next read index */
|
|
int *data; /* storage owned by the caller */
|
|
} cbuf_t;
|
|
|
|
int cb_init(cbuf_t *cb, int *storage, size_t n);
|
|
int cb_push(cbuf_t *cb, int v);
|
|
int cb_pop(cbuf_t *cb, int *out);
|
|
int cb_is_empty(const cbuf_t *cb);
|
|
int cb_is_full(const cbuf_t *cb);
|
|
|
|
#endif
|