PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
fifo.h
Go to the documentation of this file.
1#pragma once
2
3#include <errno.h>
4#include <stdint.h>
5#include <string.h>
6
7/**
8 * @brief First-in first-out buffer.
9 * @defgroup kernel_utils_fifo FIFO Buffer
10 * @ingroup kernel_utils
11 *
12 * @{
13 */
14
15/**
16 * @brief FIFO Buffer.
17 * @struct fifo_t
18 */
19typedef struct fifo
20{
21 uint8_t* buffer; ///< Pointer to the buffer memory.
22 size_t size; ///< The total size of the buffer.
23 size_t head; ///< The position to write to.
24 size_t tail; ///< The position to start reading from.
25} fifo_t;
26
27/**
28 * @brief Create a fifo buffer initializer.
29 *
30 * @param _buf Pointer to the buffer memory.
31 * @param _size The size of the buffer in bytes.
32 */
33#define FIFO_CREATE(_buf, _size) {.buffer = (uint8_t*)(_buf), .size = (_size), .head = 0, .tail = 0}
34
35/**
36 * @brief Define and initialize a fifo buffer.
37 *
38 * Helps define a fifo buffer with a backing buffer.
39 *
40 * @param _name The name of the fifo buffer.
41 * @param _size The size of the fifo buffer in bytes.
42 */
43#define FIFO_DEFINE(_name, _size) \
44 uint8_t _name##_buffer[_size]; \
45 fifo_t _name = FIFO_CREATE(_name##_buffer, _size)
46
47/**
48 * @brief Initialize a fifo buffer.
49 *
50 * @param fifo Pointer to the fifo buffer structure.
51 * @param buffer Pointer to the buffer memory.
52 * @param size The size of the buffer in bytes.
53 */
54void fifo_init(fifo_t* fifo, uint8_t* buffer, size_t size);
55
56/**
57 * @brief Reset a fifo buffer.
58 *
59 * @param fifo Pointer to the fifo buffer structure.
60 */
61void fifo_reset(fifo_t* fifo);
62
63/**
64 * @brief Return the number of bytes available for reading in a fifo buffer.
65 *
66 * @param fifo Pointer to the fifo buffer structure.
67 * @return The number of bytes used.
68 */
69size_t fifo_bytes_readable(const fifo_t* fifo);
70
71/**
72 * @brief Return the number of bytes available for writing in a fifo buffer.
73 *
74 * @param fifo Pointer to the fifo buffer structure.
75 * @return The number of bytes available for writing.
76 */
77size_t fifo_bytes_writeable(const fifo_t* fifo);
78
79/**
80 * @brief Read data from a fifo buffer at a specific offset.
81 *
82 * @param fifo The fifo buffer structure.
83 * @param buffer The destination buffer.
84 * @param count The number of bytes to read.
85 * @return The number of bytes read.
86 */
87size_t fifo_read(fifo_t* fifo, void* buffer, size_t count);
88
89/**
90 * @brief Write data to the fifo buffer.
91 *
92 * Will write up to the available space.
93 *
94 * @param fifo Pointer to the fifo buffer structure.
95 * @param buffer The source buffer.
96 * @param count The number of bytes to write.
97 * @return The number of bytes written.
98 */
99size_t fifo_write(fifo_t* fifo, const void* buffer, size_t count);
100
101/**
102 * @brief Advance the head of the fifo buffer.
103 *
104 * @param fifo Pointer to the fifo buffer structure.
105 * @param count The number of bytes to advance the head by.
106 */
107void fifo_advance_head(fifo_t* fifo, size_t count);
108
109/**
110 * @brief Advance the tail of the fifo buffer.
111 *
112 * @param fifo Pointer to the fifo buffer structure.
113 * @param count The number of bytes to advance the tail by.
114 */
115void fifo_advance_tail(fifo_t* fifo, size_t count);
116
117/** @} */
void fifo_init(fifo_t *fifo, uint8_t *buffer, size_t size)
Initialize a fifo buffer.
Definition fifo.c:7
size_t fifo_bytes_writeable(const fifo_t *fifo)
Return the number of bytes available for writing in a fifo buffer.
Definition fifo.c:31
size_t fifo_bytes_readable(const fifo_t *fifo)
Return the number of bytes available for reading in a fifo buffer.
Definition fifo.c:21
void fifo_advance_tail(fifo_t *fifo, size_t count)
Advance the tail of the fifo buffer.
Definition fifo.c:105
size_t fifo_write(fifo_t *fifo, const void *buffer, size_t count)
Write data to the fifo buffer.
Definition fifo.c:73
void fifo_advance_head(fifo_t *fifo, size_t count)
Advance the head of the fifo buffer.
Definition fifo.c:100
void fifo_reset(fifo_t *fifo)
Reset a fifo buffer.
Definition fifo.c:15
size_t fifo_read(fifo_t *fifo, void *buffer, size_t count)
Read data from a fifo buffer at a specific offset.
Definition fifo.c:41
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:11
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
FIFO Buffer.
Definition fifo.h:20
size_t size
The total size of the buffer.
Definition fifo.h:22
size_t head
The position to write to.
Definition fifo.h:23
uint8_t * buffer
Pointer to the buffer memory.
Definition fifo.h:21
size_t tail
The position to start reading from.
Definition fifo.h:24