PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
fifo.c
Go to the documentation of this file.
1#include <kernel/utils/fifo.h>
2
3#include <errno.h>
4#include <stdint.h>
5#include <string.h>
6
7void fifo_init(fifo_t* fifo, uint8_t* buffer, size_t size)
8{
9 fifo->buffer = buffer;
10 fifo->size = size;
11 fifo->head = 0;
12 fifo->tail = 0;
13}
14
15void fifo_reset(fifo_t* fifo)
16{
17 fifo->head = 0;
18 fifo->tail = 0;
19}
20
21size_t fifo_bytes_readable(const fifo_t* fifo)
22{
23 if (fifo->head >= fifo->tail)
24 {
25 return fifo->head - fifo->tail;
26 }
27
28 return fifo->size - (fifo->tail - fifo->head);
29}
30
31size_t fifo_bytes_writeable(const fifo_t* fifo)
32{
33 if (fifo->tail > fifo->head)
34 {
35 return fifo->tail - fifo->head - 1;
36 }
37
38 return fifo->size - (fifo->head - fifo->tail) - 1;
39}
40
41size_t fifo_read(fifo_t* fifo, void* buffer, size_t count)
42{
43 size_t readable = fifo_bytes_readable(fifo);
44 if (readable == 0)
45 {
46 return 0;
47 }
48
49 if (count > readable)
50 {
52 }
53
54 size_t firstSize = fifo->size - fifo->tail;
55 if (firstSize > count)
56 {
58 }
59
60 memcpy(buffer, fifo->buffer + fifo->tail, firstSize);
61 fifo->tail = (fifo->tail + firstSize) % fifo->size;
62
63 size_t remaining = count - firstSize;
64 if (remaining > 0)
65 {
66 memcpy((uint8_t*)buffer + firstSize, fifo->buffer + fifo->tail, remaining);
67 fifo->tail = (fifo->tail + remaining) % fifo->size;
68 }
69
70 return count;
71}
72
73size_t fifo_write(fifo_t* fifo, const void* buffer, size_t count)
74{
75 size_t writeable = fifo_bytes_writeable(fifo);
76 if (count > writeable)
77 {
79 }
80
81 size_t firstSize = fifo->size - fifo->head;
82 if (firstSize > count)
83 {
85 }
86
87 memcpy(fifo->buffer + fifo->head, buffer, firstSize);
88 fifo->head = (fifo->head + firstSize) % fifo->size;
89
90 size_t remaining = count - firstSize;
91 if (remaining > 0)
92 {
93 memcpy(fifo->buffer + fifo->head, (uint8_t*)buffer + firstSize, remaining);
94 fifo->head = (fifo->head + remaining) % fifo->size;
95 }
96
97 return count;
98}
99
100void fifo_advance_head(fifo_t* fifo, size_t count)
101{
102 fifo->head = (fifo->head + count) % fifo->size;
103}
104
105void fifo_advance_tail(fifo_t* fifo, size_t count)
106{
107 fifo->tail = (fifo->tail + count) % fifo->size;
108}
int64_t y
Definition main.c:153
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
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:61
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