]> git.proxmox.com Git - mirror_qemu.git/blame - util/fifo8.c
util/fifo8: Allow fifo8_pop_buf() to not populate popped length
[mirror_qemu.git] / util / fifo8.c
CommitLineData
a3578d4a
PC
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
aafd7584 15#include "qemu/osdep.h"
d6454270 16#include "migration/vmstate.h"
fd7f0d66 17#include "qemu/fifo8.h"
a3578d4a
PC
18
19void fifo8_create(Fifo8 *fifo, uint32_t capacity)
20{
21 fifo->data = g_new(uint8_t, capacity);
22 fifo->capacity = capacity;
23 fifo->head = 0;
24 fifo->num = 0;
25}
26
27void fifo8_destroy(Fifo8 *fifo)
28{
29 g_free(fifo->data);
30}
31
32void fifo8_push(Fifo8 *fifo, uint8_t data)
33{
f71407ed 34 assert(fifo->num < fifo->capacity);
a3578d4a
PC
35 fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data;
36 fifo->num++;
37}
38
c4e57af8
BG
39void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
40{
41 uint32_t start, avail;
42
f71407ed 43 assert(fifo->num + num <= fifo->capacity);
c4e57af8
BG
44
45 start = (fifo->head + fifo->num) % fifo->capacity;
46
47 if (start + num <= fifo->capacity) {
48 memcpy(&fifo->data[start], data, num);
49 } else {
50 avail = fifo->capacity - start;
51 memcpy(&fifo->data[start], data, avail);
52 memcpy(&fifo->data[0], &data[avail], num - avail);
53 }
54
55 fifo->num += num;
56}
57
a3578d4a
PC
58uint8_t fifo8_pop(Fifo8 *fifo)
59{
60 uint8_t ret;
61
f71407ed 62 assert(fifo->num > 0);
a3578d4a
PC
63 ret = fifo->data[fifo->head++];
64 fifo->head %= fifo->capacity;
65 fifo->num--;
66 return ret;
67}
68
cd04033d 69const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
c4e57af8
BG
70{
71 uint8_t *ret;
cd04033d 72 uint32_t num;
c4e57af8 73
f71407ed 74 assert(max > 0 && max <= fifo->num);
cd04033d 75 num = MIN(fifo->capacity - fifo->head, max);
c4e57af8 76 ret = &fifo->data[fifo->head];
cd04033d 77 fifo->head += num;
c4e57af8 78 fifo->head %= fifo->capacity;
cd04033d
PMD
79 fifo->num -= num;
80 if (numptr) {
81 *numptr = num;
82 }
c4e57af8
BG
83 return ret;
84}
85
a3578d4a
PC
86void fifo8_reset(Fifo8 *fifo)
87{
88 fifo->num = 0;
58892d47 89 fifo->head = 0;
a3578d4a
PC
90}
91
92bool fifo8_is_empty(Fifo8 *fifo)
93{
94 return (fifo->num == 0);
95}
96
97bool fifo8_is_full(Fifo8 *fifo)
98{
99 return (fifo->num == fifo->capacity);
100}
101
c4e57af8
BG
102uint32_t fifo8_num_free(Fifo8 *fifo)
103{
104 return fifo->capacity - fifo->num;
105}
106
107uint32_t fifo8_num_used(Fifo8 *fifo)
108{
109 return fifo->num;
110}
111
a3578d4a
PC
112const VMStateDescription vmstate_fifo8 = {
113 .name = "Fifo8",
114 .version_id = 1,
115 .minimum_version_id = 1,
59c2dded 116 .fields = (const VMStateField[]) {
59046ec2 117 VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
a3578d4a
PC
118 VMSTATE_UINT32(head, Fifo8),
119 VMSTATE_UINT32(num, Fifo8),
120 VMSTATE_END_OF_LIST()
121 }
122};