1 /* 2 * Generic FIFO component, implemented as a circular buffer. 3 * 4 * Copyright (c) 2012 Peter A. G. Crosthwaite 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 10 * 11 * You should have received a copy of the GNU General Public License along 12 * with this program; if not, see <http://www.gnu.org/licenses/>. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "migration/vmstate.h" 17 #include "qemu/fifo8.h" 18 19 void fifo8_reset(Fifo8 *fifo) 20 { 21 fifo->num = 0; 22 fifo->head = 0; 23 } 24 25 void fifo8_create(Fifo8 *fifo, uint32_t capacity) 26 { 27 fifo->data = g_new(uint8_t, capacity); 28 fifo->capacity = capacity; 29 fifo8_reset(fifo); 30 } 31 32 void fifo8_destroy(Fifo8 *fifo) 33 { 34 g_free(fifo->data); 35 } 36 37 void fifo8_push(Fifo8 *fifo, uint8_t data) 38 { 39 assert(fifo->num < fifo->capacity); 40 fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data; 41 fifo->num++; 42 } 43 44 void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num) 45 { 46 uint32_t start, avail; 47 48 assert(fifo->num + num <= fifo->capacity); 49 50 start = (fifo->head + fifo->num) % fifo->capacity; 51 52 if (start + num <= fifo->capacity) { 53 memcpy(&fifo->data[start], data, num); 54 } else { 55 avail = fifo->capacity - start; 56 memcpy(&fifo->data[start], data, avail); 57 memcpy(&fifo->data[0], &data[avail], num - avail); 58 } 59 60 fifo->num += num; 61 } 62 63 uint8_t fifo8_pop(Fifo8 *fifo) 64 { 65 uint8_t ret; 66 67 assert(fifo->num > 0); 68 ret = fifo->data[fifo->head++]; 69 fifo->head %= fifo->capacity; 70 fifo->num--; 71 return ret; 72 } 73 74 static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max, 75 uint32_t *numptr, bool do_pop) 76 { 77 uint8_t *ret; 78 uint32_t num; 79 80 assert(max > 0 && max <= fifo->num); 81 num = MIN(fifo->capacity - fifo->head, max); 82 ret = &fifo->data[fifo->head]; 83 84 if (do_pop) { 85 fifo->head += num; 86 fifo->head %= fifo->capacity; 87 fifo->num -= num; 88 } 89 if (numptr) { 90 *numptr = num; 91 } 92 return ret; 93 } 94 95 const uint8_t *fifo8_peek_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr) 96 { 97 return fifo8_peekpop_buf(fifo, max, numptr, false); 98 } 99 100 const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) 101 { 102 return fifo8_peekpop_buf(fifo, max, numptr, true); 103 } 104 105 bool fifo8_is_empty(Fifo8 *fifo) 106 { 107 return (fifo->num == 0); 108 } 109 110 bool fifo8_is_full(Fifo8 *fifo) 111 { 112 return (fifo->num == fifo->capacity); 113 } 114 115 uint32_t fifo8_num_free(Fifo8 *fifo) 116 { 117 return fifo->capacity - fifo->num; 118 } 119 120 uint32_t fifo8_num_used(Fifo8 *fifo) 121 { 122 return fifo->num; 123 } 124 125 const VMStateDescription vmstate_fifo8 = { 126 .name = "Fifo8", 127 .version_id = 1, 128 .minimum_version_id = 1, 129 .fields = (const VMStateField[]) { 130 VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity), 131 VMSTATE_UINT32(head, Fifo8), 132 VMSTATE_UINT32(num, Fifo8), 133 VMSTATE_END_OF_LIST() 134 } 135 }; 136