ppc/xive: Always recompute the PIPR when pushing an OS context
[qemu.git] / util / fifo8.c
blobd4d1c135e03b87b76b8979ce6b4275f926599e8e
1 /*
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_create(Fifo8 *fifo, uint32_t capacity)
21 fifo->data = g_new(uint8_t, capacity);
22 fifo->capacity = capacity;
23 fifo->head = 0;
24 fifo->num = 0;
27 void fifo8_destroy(Fifo8 *fifo)
29 g_free(fifo->data);
32 void fifo8_push(Fifo8 *fifo, uint8_t data)
34 assert(fifo->num < fifo->capacity);
35 fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data;
36 fifo->num++;
39 void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
41 uint32_t start, avail;
43 assert(fifo->num + num <= fifo->capacity);
45 start = (fifo->head + fifo->num) % fifo->capacity;
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);
55 fifo->num += num;
58 uint8_t fifo8_pop(Fifo8 *fifo)
60 uint8_t ret;
62 assert(fifo->num > 0);
63 ret = fifo->data[fifo->head++];
64 fifo->head %= fifo->capacity;
65 fifo->num--;
66 return ret;
69 const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num)
71 uint8_t *ret;
73 assert(max > 0 && max <= fifo->num);
74 *num = MIN(fifo->capacity - fifo->head, max);
75 ret = &fifo->data[fifo->head];
76 fifo->head += *num;
77 fifo->head %= fifo->capacity;
78 fifo->num -= *num;
79 return ret;
82 void fifo8_reset(Fifo8 *fifo)
84 fifo->num = 0;
85 fifo->head = 0;
88 bool fifo8_is_empty(Fifo8 *fifo)
90 return (fifo->num == 0);
93 bool fifo8_is_full(Fifo8 *fifo)
95 return (fifo->num == fifo->capacity);
98 uint32_t fifo8_num_free(Fifo8 *fifo)
100 return fifo->capacity - fifo->num;
103 uint32_t fifo8_num_used(Fifo8 *fifo)
105 return fifo->num;
108 const VMStateDescription vmstate_fifo8 = {
109 .name = "Fifo8",
110 .version_id = 1,
111 .minimum_version_id = 1,
112 .fields = (VMStateField[]) {
113 VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
114 VMSTATE_UINT32(head, Fifo8),
115 VMSTATE_UINT32(num, Fifo8),
116 VMSTATE_END_OF_LIST()