Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / util / fifo8.c
bloba26da66ad2c87e5281a1b6135a18b1e6f062ce1d
1 /*
2 * Generic FIFO component, implemented as a circular buffer.
4 * Copyright (c) 2012 Peter A. G. Crosthwaite
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.
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/>.
15 #include "qemu/osdep.h"
16 #include "migration/vmstate.h"
17 #include "qemu/fifo8.h"
19 void fifo8_reset(Fifo8 *fifo)
21 fifo->num = 0;
22 fifo->head = 0;
25 void fifo8_create(Fifo8 *fifo, uint32_t capacity)
27 fifo->data = g_new(uint8_t, capacity);
28 fifo->capacity = capacity;
29 fifo8_reset(fifo);
32 void fifo8_destroy(Fifo8 *fifo)
34 g_free(fifo->data);
37 void fifo8_push(Fifo8 *fifo, uint8_t data)
39 assert(fifo->num < fifo->capacity);
40 fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data;
41 fifo->num++;
44 void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
46 uint32_t start, avail;
48 assert(fifo->num + num <= fifo->capacity);
50 start = (fifo->head + fifo->num) % fifo->capacity;
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);
60 fifo->num += num;
63 uint8_t fifo8_pop(Fifo8 *fifo)
65 uint8_t ret;
67 assert(fifo->num > 0);
68 ret = fifo->data[fifo->head++];
69 fifo->head %= fifo->capacity;
70 fifo->num--;
71 return ret;
74 uint8_t fifo8_peek(Fifo8 *fifo)
76 assert(fifo->num > 0);
77 return fifo->data[fifo->head];
80 static const uint8_t *fifo8_peekpop_bufptr(Fifo8 *fifo, uint32_t max,
81 uint32_t skip, uint32_t *numptr,
82 bool do_pop)
84 uint8_t *ret;
85 uint32_t num, head;
87 assert(max > 0 && max <= fifo->num);
88 assert(skip <= fifo->num);
89 head = (fifo->head + skip) % fifo->capacity;
90 num = MIN(fifo->capacity - head, max);
91 ret = &fifo->data[head];
93 if (do_pop) {
94 fifo->head = head + num;
95 fifo->head %= fifo->capacity;
96 fifo->num -= num;
98 if (numptr) {
99 *numptr = num;
101 return ret;
104 const uint8_t *fifo8_peek_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
106 return fifo8_peekpop_bufptr(fifo, max, 0, numptr, false);
109 const uint8_t *fifo8_pop_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
111 return fifo8_peekpop_bufptr(fifo, max, 0, numptr, true);
114 static uint32_t fifo8_peekpop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen,
115 bool do_pop)
117 const uint8_t *buf;
118 uint32_t n1, n2 = 0;
119 uint32_t len;
121 if (destlen == 0) {
122 return 0;
125 len = destlen;
126 buf = fifo8_peekpop_bufptr(fifo, len, 0, &n1, do_pop);
127 if (dest) {
128 memcpy(dest, buf, n1);
131 /* Add FIFO wraparound if needed */
132 len -= n1;
133 len = MIN(len, fifo8_num_used(fifo));
134 if (len) {
135 buf = fifo8_peekpop_bufptr(fifo, len, do_pop ? 0 : n1, &n2, do_pop);
136 if (dest) {
137 memcpy(&dest[n1], buf, n2);
141 return n1 + n2;
144 uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
146 return fifo8_peekpop_buf(fifo, dest, destlen, true);
149 uint32_t fifo8_peek_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
151 return fifo8_peekpop_buf(fifo, dest, destlen, false);
154 void fifo8_drop(Fifo8 *fifo, uint32_t len)
156 len -= fifo8_pop_buf(fifo, NULL, len);
157 assert(len == 0);
160 bool fifo8_is_empty(Fifo8 *fifo)
162 return (fifo->num == 0);
165 bool fifo8_is_full(Fifo8 *fifo)
167 return (fifo->num == fifo->capacity);
170 uint32_t fifo8_num_free(Fifo8 *fifo)
172 return fifo->capacity - fifo->num;
175 uint32_t fifo8_num_used(Fifo8 *fifo)
177 return fifo->num;
180 const VMStateDescription vmstate_fifo8 = {
181 .name = "Fifo8",
182 .version_id = 1,
183 .minimum_version_id = 1,
184 .fields = (const VMStateField[]) {
185 VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
186 VMSTATE_UINT32(head, Fifo8),
187 VMSTATE_UINT32(num, Fifo8),
188 VMSTATE_END_OF_LIST()