]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu/fifo8.h
Merge tag 'pull-target-arm-20240111' of https://git.linaro.org/people/pmaydell/qemu...
[mirror_qemu.git] / include / qemu / fifo8.h
CommitLineData
121d0712
MA
1#ifndef QEMU_FIFO8_H
2#define QEMU_FIFO8_H
a3578d4a 3
a3578d4a
PC
4
5typedef struct {
6 /* All fields are private */
7 uint8_t *data;
8 uint32_t capacity;
9 uint32_t head;
10 uint32_t num;
11} Fifo8;
12
13/**
14 * fifo8_create:
15 * @fifo: struct Fifo8 to initialise with new FIFO
16 * @capacity: capacity of the newly created FIFO
17 *
18 * Create a FIFO of the specified size. Clients should call fifo8_destroy()
19 * when finished using the fifo. The FIFO is initially empty.
20 */
21
22void fifo8_create(Fifo8 *fifo, uint32_t capacity);
23
24/**
25 * fifo8_destroy:
26 * @fifo: FIFO to cleanup
27 *
28 * Cleanup a FIFO created with fifo8_create(). Frees memory created for FIFO
29 *storage. The FIFO is no longer usable after this has been called.
30 */
31
32void fifo8_destroy(Fifo8 *fifo);
33
34/**
35 * fifo8_push:
36 * @fifo: FIFO to push to
37 * @data: data byte to push
38 *
39 * Push a data byte to the FIFO. Behaviour is undefined if the FIFO is full.
40 * Clients are responsible for checking for fullness using fifo8_is_full().
41 */
42
43void fifo8_push(Fifo8 *fifo, uint8_t data);
44
c4e57af8
BG
45/**
46 * fifo8_push_all:
47 * @fifo: FIFO to push to
48 * @data: data to push
c342a5d3 49 * @num: number of bytes to push
c4e57af8
BG
50 *
51 * Push a byte array to the FIFO. Behaviour is undefined if the FIFO is full.
52 * Clients are responsible for checking the space left in the FIFO using
53 * fifo8_num_free().
54 */
55
56void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
57
a3578d4a
PC
58/**
59 * fifo8_pop:
60 * @fifo: fifo to pop from
61 *
62 * Pop a data byte from the FIFO. Behaviour is undefined if the FIFO is empty.
63 * Clients are responsible for checking for emptyness using fifo8_is_empty().
64 *
65 * Returns: The popped data byte.
66 */
67
68uint8_t fifo8_pop(Fifo8 *fifo);
69
c4e57af8
BG
70/**
71 * fifo8_pop_buf:
72 * @fifo: FIFO to pop from
73 * @max: maximum number of bytes to pop
cd04033d 74 * @numptr: pointer filled with number of bytes returned (can be NULL)
c4e57af8
BG
75 *
76 * Pop a number of elements from the FIFO up to a maximum of max. The buffer
77 * containing the popped data is returned. This buffer points directly into
78 * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
79 * are called on the FIFO.
80 *
81 * The function may return fewer bytes than requested when the data wraps
82 * around in the ring buffer; in this case only a contiguous part of the data
83 * is returned.
84 *
cd04033d
PMD
85 * The number of valid bytes returned is populated in *numptr; will always
86 * return at least 1 byte. max must not be 0 or greater than the number of
87 * bytes in the FIFO.
c4e57af8
BG
88 *
89 * Clients are responsible for checking the availability of requested data
90 * using fifo8_num_used().
91 *
92 * Returns: A pointer to popped data.
93 */
cd04033d 94const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
c4e57af8 95
995d8348
PMD
96/**
97 * fifo8_peek_buf: read upto max bytes from the fifo
98 * @fifo: FIFO to read from
99 * @max: maximum number of bytes to peek
100 * @numptr: pointer filled with number of bytes returned (can be NULL)
101 *
102 * Peek into a number of elements from the FIFO up to a maximum of max.
103 * The buffer containing the data peeked into is returned. This buffer points
104 * directly into the FIFO backing store. Since data is invalidated once any
105 * of the fifo8_* APIs are called on the FIFO, it is the caller responsibility
106 * to access it before doing further API calls.
107 *
108 * The function may return fewer bytes than requested when the data wraps
109 * around in the ring buffer; in this case only a contiguous part of the data
110 * is returned.
111 *
112 * The number of valid bytes returned is populated in *numptr; will always
113 * return at least 1 byte. max must not be 0 or greater than the number of
114 * bytes in the FIFO.
115 *
116 * Clients are responsible for checking the availability of requested data
117 * using fifo8_num_used().
118 *
119 * Returns: A pointer to peekable data.
120 */
121const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
122
a3578d4a
PC
123/**
124 * fifo8_reset:
125 * @fifo: FIFO to reset
126 *
127 * Reset a FIFO. All data is discarded and the FIFO is emptied.
128 */
129
130void fifo8_reset(Fifo8 *fifo);
131
132/**
133 * fifo8_is_empty:
134 * @fifo: FIFO to check
135 *
136 * Check if a FIFO is empty.
137 *
138 * Returns: True if the fifo is empty, false otherwise.
139 */
140
141bool fifo8_is_empty(Fifo8 *fifo);
142
143/**
144 * fifo8_is_full:
145 * @fifo: FIFO to check
146 *
147 * Check if a FIFO is full.
148 *
149 * Returns: True if the fifo is full, false otherwise.
150 */
151
152bool fifo8_is_full(Fifo8 *fifo);
153
c4e57af8
BG
154/**
155 * fifo8_num_free:
156 * @fifo: FIFO to check
157 *
158 * Return the number of free bytes in the FIFO.
159 *
160 * Returns: Number of free bytes.
161 */
162
163uint32_t fifo8_num_free(Fifo8 *fifo);
164
165/**
166 * fifo8_num_used:
167 * @fifo: FIFO to check
168 *
169 * Return the number of used bytes in the FIFO.
170 *
171 * Returns: Number of used bytes.
172 */
173
174uint32_t fifo8_num_used(Fifo8 *fifo);
175
a3578d4a
PC
176extern const VMStateDescription vmstate_fifo8;
177
cdf01ca4
MCA
178#define VMSTATE_FIFO8_TEST(_field, _state, _test) { \
179 .name = (stringify(_field)), \
180 .field_exists = (_test), \
181 .size = sizeof(Fifo8), \
182 .vmsd = &vmstate_fifo8, \
183 .flags = VMS_STRUCT, \
184 .offset = vmstate_offset_value(_state, _field, Fifo8), \
a3578d4a
PC
185}
186
cdf01ca4
MCA
187#define VMSTATE_FIFO8(_field, _state) \
188 VMSTATE_FIFO8_TEST(_field, _state, NULL)
189
121d0712 190#endif /* QEMU_FIFO8_H */