1#ifndef FIFO_H 2#define FIFO_H 3 4#include "migration/vmstate.h" 5 6typedef struct { 7 /* All fields are private */ 8 int width; /* byte width of each element */ 9 uint32_t capacity; /* number of elements */ 10 11 uint8_t *data; 12 uint32_t buffer_size; 13 14 uint32_t head; 15 uint32_t num; 16} Fifo; 17 18/** 19 * fifo_create8: 20 * @fifo: struct Fifo to initialise with new FIFO 21 * @capacity: capacity of the newly created FIFO 22 * 23 * Create a byte FIFO of the specified size. Clients should call fifo_destroy() 24 * when finished using the fifo. The FIFO is initially empty. 25 */ 26 27void fifo_create8(Fifo *fifo, uint32_t capacity); 28void fifo_create16(Fifo *fifo, uint32_t capacity); 29void fifo_create32(Fifo *fifo, uint32_t capacity); 30void fifo_create64(Fifo *fifo, uint32_t capacity); 31 32/** 33 * fifo_destroy: 34 * @fifo: FIFO to cleanup 35 * 36 * Cleanup a FIFO created with fifo_create(). Frees memory created for FIFO 37 *storage. The FIFO is no longer usable after this has been called. 38 */ 39 40void fifo_destroy(Fifo *fifo); 41 42/** 43 * fifo_pushX: 44 * @fifo: FIFO to push to 45 * @data: data value to push 46 * 47 * Push a data value to the FIFO. Behaviour is undefined if the FIFO is full. 48 * Clients are responsible for checking for fullness using fifo_is_full(). 49 * 50 * 8, 16, 32 and 64 bit variants are available. Behaviour is undefined if a 51 * variant mismatched to the FIFO width used (e.g. you cannot use fifo_push8 52 * with a FIFO created with width == 16). 53 */ 54 55void fifo_push8(Fifo *fifo, uint8_t data); 56void fifo_push16(Fifo *fifo, uint16_t data); 57void fifo_push32(Fifo *fifo, uint32_t data); 58void fifo_push64(Fifo *fifo, uint64_t data); 59 60/** 61 * fifo_push_all: 62 * @fifo: FIFO to push to 63 * @data: data to push 64 * @size: number of entries to push 65 * 66 * Push a buffer to the FIFO. Behaviour is undefined if the FIFO is full. 67 * Clients are responsible for checking the space left in the FIFO using 68 * fifo_num_free(). 69 */ 70 71void fifo_push_all(Fifo *fifo, const void *data, uint32_t num); 72 73/** 74 * fifo_popX: 75 * @fifo: fifo to pop from 76 * 77 * Pop a data value from the FIFO. Behaviour is undefined if the FIFO is empty. 78 * Clients are responsible for checking for emptyness using fifo_is_empty(). 79 * 80 * 8, 16, 32 and 64 bit variants are available. Behaviour is undefined if a 81 * variant mismatched to the FIFO width is used (e.g. you cannot use fifo_pop8 82 * with a FIFO created with width == 16). 83 * 84 * Returns: The popped data value. 85 */ 86 87uint8_t fifo_pop8(Fifo *fifo); 88uint16_t fifo_pop16(Fifo *fifo); 89uint32_t fifo_pop32(Fifo *fifo); 90uint64_t fifo_pop64(Fifo *fifo); 91 92/** 93 * fifo_pop_buf: 94 * @fifo: FIFO to pop from 95 * @max: maximum number of elements to pop 96 * @num: actual number of returned elements 97 * 98 * Pop a number of elements from the FIFO up to a maximum of max. The buffer 99 * containing the popped data is returned. This buffer points directly into 100 * the FIFO backing store and data is invalidated once any of the fifo_* APIs 101 * are called on the FIFO. 102 * 103 * The function may return fewer elements than requested when the data wraps 104 * around in the ring buffer; in this case only a contiguous part of the data 105 * is returned. 106 * 107 * The number of valid elements returned is populated in *num; will always 108 * return at least 1 element. max must not be 0 or greater than the number of 109 * elements in the FIFO. 110 * 111 * Clients are responsible for checking the availability of requested data 112 * using fifo_num_used(). 113 * 114 * Returns: A pointer to popped data. 115 */ 116 117const void *fifo_pop_buf(Fifo *fifo, uint32_t max, uint32_t *num); 118 119/** 120 * fifo_reset: 121 * @fifo: FIFO to reset 122 * 123 * Reset a FIFO. All data is discarded and the FIFO is emptied. 124 */ 125 126void fifo_reset(Fifo *fifo); 127 128/** 129 * fifo_is_empty: 130 * @fifo: FIFO to check 131 * 132 * Check if a FIFO is empty. 133 * 134 * Returns: True if the fifo is empty, false otherwise. 135 */ 136 137bool fifo_is_empty(Fifo *fifo); 138 139/** 140 * fifo_is_full: 141 * @fifo: FIFO to check 142 * 143 * Check if a FIFO is full. 144 * 145 * Returns: True if the fifo is full, false otherwise. 146 */ 147 148bool fifo_is_full(Fifo *fifo); 149 150/** 151 * fifo_num_free: 152 * @fifo: FIFO to check 153 * 154 * Return the number of free elements in the FIFO. 155 * 156 * Returns: Number of free elements. 157 */ 158 159uint32_t fifo_num_free(Fifo *fifo); 160 161/** 162 * fifo_num_used: 163 * @fifo: FIFO to check 164 * 165 * Return the number of used elements in the FIFO. 166 * 167 * Returns: Number of used elements. 168 */ 169 170uint32_t fifo_num_used(Fifo *fifo); 171 172extern const VMStateDescription vmstate_fifo; 173 174#define VMSTATE_FIFO(_field, _state) { \ 175 .name = (stringify(_field)), \ 176 .size = sizeof(Fifo), \ 177 .vmsd = &vmstate_fifo, \ 178 .flags = VMS_STRUCT, \ 179 .offset = vmstate_offset_value(_state, _field, Fifo), \ 180} 181 182#endif /* FIFO_H */ 183