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
)
25 void fifo8_create(Fifo8
*fifo
, uint32_t capacity
)
27 fifo
->data
= g_new(uint8_t, capacity
);
28 fifo
->capacity
= capacity
;
32 void fifo8_destroy(Fifo8
*fifo
)
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
;
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
);
55 avail
= fifo
->capacity
- start
;
56 memcpy(&fifo
->data
[start
], data
, avail
);
57 memcpy(&fifo
->data
[0], &data
[avail
], num
- avail
);
63 uint8_t fifo8_pop(Fifo8
*fifo
)
67 assert(fifo
->num
> 0);
68 ret
= fifo
->data
[fifo
->head
++];
69 fifo
->head
%= fifo
->capacity
;
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
,
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
];
94 fifo
->head
= head
+ num
;
95 fifo
->head
%= fifo
->capacity
;
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
,
126 buf
= fifo8_peekpop_bufptr(fifo
, len
, 0, &n1
, do_pop
);
128 memcpy(dest
, buf
, n1
);
131 /* Add FIFO wraparound if needed */
133 len
= MIN(len
, fifo8_num_used(fifo
));
135 buf
= fifo8_peekpop_bufptr(fifo
, len
, do_pop
? 0 : n1
, &n2
, do_pop
);
137 memcpy(&dest
[n1
], buf
, 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
);
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
)
180 const VMStateDescription vmstate_fifo8
= {
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()