xref: /openbmc/qemu/util/fifo8.c (revision b19bbf2c)
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_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
101 {
102     return fifo8_peekpop_buf(fifo, max, numptr, true);
103 }
104 
105 uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
106 {
107     const uint8_t *buf;
108     uint32_t n1, n2 = 0;
109     uint32_t len;
110 
111     if (destlen == 0) {
112         return 0;
113     }
114 
115     len = destlen;
116     buf = fifo8_pop_bufptr(fifo, len, &n1);
117     if (dest) {
118         memcpy(dest, buf, n1);
119     }
120 
121     /* Add FIFO wraparound if needed */
122     len -= n1;
123     len = MIN(len, fifo8_num_used(fifo));
124     if (len) {
125         buf = fifo8_pop_bufptr(fifo, len, &n2);
126         if (dest) {
127             memcpy(&dest[n1], buf, n2);
128         }
129     }
130 
131     return n1 + n2;
132 }
133 
134 void fifo8_drop(Fifo8 *fifo, uint32_t len)
135 {
136     len -= fifo8_pop_buf(fifo, NULL, len);
137     assert(len == 0);
138 }
139 
140 bool fifo8_is_empty(Fifo8 *fifo)
141 {
142     return (fifo->num == 0);
143 }
144 
145 bool fifo8_is_full(Fifo8 *fifo)
146 {
147     return (fifo->num == fifo->capacity);
148 }
149 
150 uint32_t fifo8_num_free(Fifo8 *fifo)
151 {
152     return fifo->capacity - fifo->num;
153 }
154 
155 uint32_t fifo8_num_used(Fifo8 *fifo)
156 {
157     return fifo->num;
158 }
159 
160 const VMStateDescription vmstate_fifo8 = {
161     .name = "Fifo8",
162     .version_id = 1,
163     .minimum_version_id = 1,
164     .fields = (const VMStateField[]) {
165         VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
166         VMSTATE_UINT32(head, Fifo8),
167         VMSTATE_UINT32(num, Fifo8),
168         VMSTATE_END_OF_LIST()
169     }
170 };
171