xref: /qemu/include/qemu/fifo8.h (revision cdd30f36)
1 #ifndef QEMU_FIFO8_H
2 #define QEMU_FIFO8_H
3 
4 
5 typedef 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 
22 void 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 
32 void 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 
43 void fifo8_push(Fifo8 *fifo, uint8_t data);
44 
45 /**
46  * fifo8_push_all:
47  * @fifo: FIFO to push to
48  * @data: data to push
49  * @num: number of bytes to push
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 
56 void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
57 
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 
68 uint8_t fifo8_pop(Fifo8 *fifo);
69 
70 /**
71  * fifo8_pop_buf:
72  * @fifo: FIFO to pop from
73  * @max: maximum number of bytes to pop
74  * @numptr: pointer filled with number of bytes returned (can be NULL)
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  *
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.
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  */
94 const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
95 
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  */
121 const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
122 
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 
130 void 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 
141 bool 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 
152 bool fifo8_is_full(Fifo8 *fifo);
153 
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 
163 uint32_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 
174 uint32_t fifo8_num_used(Fifo8 *fifo);
175 
176 extern const VMStateDescription vmstate_fifo8;
177 
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),     \
185 }
186 
187 #define VMSTATE_FIFO8(_field, _state)                                \
188     VMSTATE_FIFO8_TEST(_field, _state, NULL)
189 
190 #endif /* QEMU_FIFO8_H */
191