Line | Count | Source |
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 | 426k | { |
21 | 426k | fifo->num = 0; |
22 | 426k | fifo->head = 0; |
23 | 426k | } |
24 | | |
25 | | void fifo8_create(Fifo8 *fifo, uint32_t capacity) |
26 | 29 | { |
27 | 29 | fifo->data = g_new(uint8_t, capacity); |
28 | 29 | fifo->capacity = capacity; |
29 | 29 | fifo8_reset(fifo); |
30 | 29 | } |
31 | | |
32 | | void fifo8_destroy(Fifo8 *fifo) |
33 | 0 | { |
34 | 0 | g_free(fifo->data); |
35 | 0 | } |
36 | | |
37 | | void fifo8_push(Fifo8 *fifo, uint8_t data) |
38 | 132k | { |
39 | 132k | assert(fifo->num < fifo->capacity); |
40 | 132k | fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data; |
41 | 132k | fifo->num++; |
42 | 132k | } |
43 | | |
44 | | void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num) |
45 | 264k | { |
46 | 264k | uint32_t start, avail; |
47 | | |
48 | 264k | assert(fifo->num + num <= fifo->capacity); |
49 | | |
50 | 264k | start = (fifo->head + fifo->num) % fifo->capacity; |
51 | | |
52 | 264k | if (start + num <= fifo->capacity) { |
53 | 263k | memcpy(&fifo->data[start], data, num); |
54 | 263k | } else { |
55 | 300 | avail = fifo->capacity - start; |
56 | 300 | memcpy(&fifo->data[start], data, avail); |
57 | 300 | memcpy(&fifo->data[0], &data[avail], num - avail); |
58 | 300 | } |
59 | | |
60 | 264k | fifo->num += num; |
61 | 264k | } |
62 | | |
63 | | uint8_t fifo8_pop(Fifo8 *fifo) |
64 | 55.5k | { |
65 | 55.5k | uint8_t ret; |
66 | | |
67 | 55.5k | assert(fifo->num > 0); |
68 | 55.5k | ret = fifo->data[fifo->head++]; |
69 | 55.5k | fifo->head %= fifo->capacity; |
70 | 55.5k | fifo->num--; |
71 | 55.5k | return ret; |
72 | 55.5k | } |
73 | | |
74 | | uint8_t fifo8_peek(const Fifo8 *fifo) |
75 | 0 | { |
76 | 0 | assert(fifo->num > 0); |
77 | 0 | return fifo->data[fifo->head]; |
78 | 0 | } |
79 | | |
80 | | static const uint8_t *fifo8_peekpop_bufptr(Fifo8 *fifo, uint32_t max, |
81 | | uint32_t skip, uint32_t *numptr, |
82 | | bool do_pop) |
83 | 292k | { |
84 | 292k | uint8_t *ret; |
85 | 292k | uint32_t num, head; |
86 | | |
87 | 292k | assert(max > 0 && max <= fifo->num); |
88 | 292k | assert(skip <= fifo->num); |
89 | 292k | head = (fifo->head + skip) % fifo->capacity; |
90 | 292k | num = MIN(fifo->capacity - head, max); |
91 | 292k | ret = &fifo->data[head]; |
92 | | |
93 | 292k | if (do_pop) { |
94 | 178k | fifo->head = head + num; |
95 | 178k | fifo->head %= fifo->capacity; |
96 | 178k | fifo->num -= num; |
97 | 178k | } |
98 | 292k | if (numptr) { |
99 | 292k | *numptr = num; |
100 | 292k | } |
101 | 292k | return ret; |
102 | 292k | } |
103 | | |
104 | | const uint8_t *fifo8_peek_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr) |
105 | 114k | { |
106 | 114k | return fifo8_peekpop_bufptr(fifo, max, 0, numptr, false); |
107 | 114k | } |
108 | | |
109 | | const uint8_t *fifo8_pop_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr) |
110 | 0 | { |
111 | 0 | return fifo8_peekpop_bufptr(fifo, max, 0, numptr, true); |
112 | 0 | } |
113 | | |
114 | | static uint32_t fifo8_peekpop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen, |
115 | | bool do_pop) |
116 | 248k | { |
117 | 248k | const uint8_t *buf; |
118 | 248k | uint32_t n1, n2 = 0; |
119 | 248k | uint32_t len; |
120 | | |
121 | 248k | if (destlen == 0) { |
122 | 70.5k | return 0; |
123 | 70.5k | } |
124 | | |
125 | 178k | len = destlen; |
126 | 178k | buf = fifo8_peekpop_bufptr(fifo, len, 0, &n1, do_pop); |
127 | 178k | if (dest) { |
128 | 176k | memcpy(dest, buf, n1); |
129 | 176k | } |
130 | | |
131 | | /* Add FIFO wraparound if needed */ |
132 | 178k | len -= n1; |
133 | 178k | len = MIN(len, fifo8_num_used(fifo)); |
134 | 178k | if (len) { |
135 | 316 | buf = fifo8_peekpop_bufptr(fifo, len, do_pop ? 0 : n1, &n2, do_pop); |
136 | 316 | if (dest) { |
137 | 151 | memcpy(&dest[n1], buf, n2); |
138 | 151 | } |
139 | 316 | } |
140 | | |
141 | 178k | return n1 + n2; |
142 | 248k | } |
143 | | |
144 | | uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) |
145 | 248k | { |
146 | 248k | return fifo8_peekpop_buf(fifo, dest, destlen, true); |
147 | 248k | } |
148 | | |
149 | | uint32_t fifo8_peek_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) |
150 | 0 | { |
151 | 0 | return fifo8_peekpop_buf(fifo, dest, destlen, false); |
152 | 0 | } |
153 | | |
154 | | void fifo8_drop(Fifo8 *fifo, uint32_t len) |
155 | 1.11k | { |
156 | 1.11k | len -= fifo8_pop_buf(fifo, NULL, len); |
157 | 1.11k | assert(len == 0); |
158 | 1.11k | } |
159 | | |
160 | | bool fifo8_is_empty(const Fifo8 *fifo) |
161 | 63.0k | { |
162 | 63.0k | return (fifo->num == 0); |
163 | 63.0k | } |
164 | | |
165 | | bool fifo8_is_full(const Fifo8 *fifo) |
166 | 132k | { |
167 | 132k | return (fifo->num == fifo->capacity); |
168 | 132k | } |
169 | | |
170 | | uint32_t fifo8_num_free(const Fifo8 *fifo) |
171 | 472k | { |
172 | 472k | return fifo->capacity - fifo->num; |
173 | 472k | } |
174 | | |
175 | | uint32_t fifo8_num_used(const Fifo8 *fifo) |
176 | 1.15M | { |
177 | 1.15M | return fifo->num; |
178 | 1.15M | } |
179 | | |
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() |
189 | | } |
190 | | }; |