2012-08-01 14:26:12 +04:00
|
|
|
/*
|
|
|
|
* Generic FIFO component, implemented as a circular buffer.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Peter A. G. Crosthwaite
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-04 13:57:50 +04:00
|
|
|
#include "qemu/fifo8.h"
|
2012-08-01 14:26:12 +04:00
|
|
|
|
2023-11-10 09:11:32 +03:00
|
|
|
void fifo8_reset(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
fifo->num = 0;
|
|
|
|
fifo->head = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-01 14:26:12 +04:00
|
|
|
void fifo8_create(Fifo8 *fifo, uint32_t capacity)
|
|
|
|
{
|
|
|
|
fifo->data = g_new(uint8_t, capacity);
|
|
|
|
fifo->capacity = capacity;
|
2023-11-10 09:11:32 +03:00
|
|
|
fifo8_reset(fifo);
|
2012-08-01 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fifo8_destroy(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
g_free(fifo->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fifo8_push(Fifo8 *fifo, uint8_t data)
|
|
|
|
{
|
2021-01-29 01:17:27 +03:00
|
|
|
assert(fifo->num < fifo->capacity);
|
2012-08-01 14:26:12 +04:00
|
|
|
fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data;
|
|
|
|
fifo->num++;
|
|
|
|
}
|
|
|
|
|
2014-01-31 02:02:04 +04:00
|
|
|
void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
|
|
|
|
{
|
|
|
|
uint32_t start, avail;
|
|
|
|
|
2021-01-29 01:17:27 +03:00
|
|
|
assert(fifo->num + num <= fifo->capacity);
|
2014-01-31 02:02:04 +04:00
|
|
|
|
|
|
|
start = (fifo->head + fifo->num) % fifo->capacity;
|
|
|
|
|
|
|
|
if (start + num <= fifo->capacity) {
|
|
|
|
memcpy(&fifo->data[start], data, num);
|
|
|
|
} else {
|
|
|
|
avail = fifo->capacity - start;
|
|
|
|
memcpy(&fifo->data[start], data, avail);
|
|
|
|
memcpy(&fifo->data[0], &data[avail], num - avail);
|
|
|
|
}
|
|
|
|
|
|
|
|
fifo->num += num;
|
|
|
|
}
|
|
|
|
|
2012-08-01 14:26:12 +04:00
|
|
|
uint8_t fifo8_pop(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
2021-01-29 01:17:27 +03:00
|
|
|
assert(fifo->num > 0);
|
2012-08-01 14:26:12 +04:00
|
|
|
ret = fifo->data[fifo->head++];
|
|
|
|
fifo->head %= fifo->capacity;
|
|
|
|
fifo->num--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-09 22:28:06 +03:00
|
|
|
static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max,
|
|
|
|
uint32_t *numptr, bool do_pop)
|
2014-01-31 02:02:04 +04:00
|
|
|
{
|
|
|
|
uint8_t *ret;
|
2023-11-09 22:28:05 +03:00
|
|
|
uint32_t num;
|
2014-01-31 02:02:04 +04:00
|
|
|
|
2021-01-29 01:17:27 +03:00
|
|
|
assert(max > 0 && max <= fifo->num);
|
2023-11-09 22:28:05 +03:00
|
|
|
num = MIN(fifo->capacity - fifo->head, max);
|
2014-01-31 02:02:04 +04:00
|
|
|
ret = &fifo->data[fifo->head];
|
2023-11-09 22:28:06 +03:00
|
|
|
|
|
|
|
if (do_pop) {
|
|
|
|
fifo->head += num;
|
|
|
|
fifo->head %= fifo->capacity;
|
|
|
|
fifo->num -= num;
|
|
|
|
}
|
2023-11-09 22:28:05 +03:00
|
|
|
if (numptr) {
|
|
|
|
*numptr = num;
|
|
|
|
}
|
2014-01-31 02:02:04 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-07-22 14:21:44 +03:00
|
|
|
const uint8_t *fifo8_peek_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
|
2023-11-09 22:28:06 +03:00
|
|
|
{
|
|
|
|
return fifo8_peekpop_buf(fifo, max, numptr, false);
|
|
|
|
}
|
|
|
|
|
2024-07-22 14:25:01 +03:00
|
|
|
const uint8_t *fifo8_pop_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
|
2023-11-09 22:28:06 +03:00
|
|
|
{
|
|
|
|
return fifo8_peekpop_buf(fifo, max, numptr, true);
|
|
|
|
}
|
|
|
|
|
2024-07-22 14:27:53 +03:00
|
|
|
uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
|
|
|
|
{
|
|
|
|
const uint8_t *buf;
|
|
|
|
uint32_t n1, n2 = 0;
|
|
|
|
uint32_t len;
|
|
|
|
|
|
|
|
if (destlen == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = destlen;
|
|
|
|
buf = fifo8_pop_bufptr(fifo, len, &n1);
|
|
|
|
if (dest) {
|
|
|
|
memcpy(dest, buf, n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add FIFO wraparound if needed */
|
|
|
|
len -= n1;
|
|
|
|
len = MIN(len, fifo8_num_used(fifo));
|
|
|
|
if (len) {
|
|
|
|
buf = fifo8_pop_bufptr(fifo, len, &n2);
|
|
|
|
if (dest) {
|
|
|
|
memcpy(&dest[n1], buf, n2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n1 + n2;
|
|
|
|
}
|
|
|
|
|
2024-07-22 14:37:48 +03:00
|
|
|
void fifo8_drop(Fifo8 *fifo, uint32_t len)
|
|
|
|
{
|
|
|
|
len -= fifo8_pop_buf(fifo, NULL, len);
|
|
|
|
assert(len == 0);
|
|
|
|
}
|
|
|
|
|
2012-08-01 14:26:12 +04:00
|
|
|
bool fifo8_is_empty(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
return (fifo->num == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fifo8_is_full(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
return (fifo->num == fifo->capacity);
|
|
|
|
}
|
|
|
|
|
2014-01-31 02:02:04 +04:00
|
|
|
uint32_t fifo8_num_free(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
return fifo->capacity - fifo->num;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fifo8_num_used(Fifo8 *fifo)
|
|
|
|
{
|
|
|
|
return fifo->num;
|
|
|
|
}
|
|
|
|
|
2012-08-01 14:26:12 +04:00
|
|
|
const VMStateDescription vmstate_fifo8 = {
|
|
|
|
.name = "Fifo8",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 06:16:50 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-02-03 20:52:17 +03:00
|
|
|
VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
|
2012-08-01 14:26:12 +04:00
|
|
|
VMSTATE_UINT32(head, Fifo8),
|
|
|
|
VMSTATE_UINT32(num, Fifo8),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|