aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorPhilippe Mathieu-Daudé <philmd@linaro.org>2024-07-22 13:27:53 +0200
committerPhilippe Mathieu-Daudé <philmd@linaro.org>2024-07-23 22:34:54 +0200
commit23ad5711730f994d66520a3283aac2979d89389e (patch)
tree1370a75038d2e2b2baf65a9473440a434423738a /util
parent06252bf5122f047ed5c14fb75f3167f11b882352 (diff)
util/fifo8: Expose fifo8_pop_buf()
Extract fifo8_pop_buf() from hw/scsi/esp.c and expose it as part of the <qemu/fifo8.h> API. This function takes care of non-contiguous (wrapped) FIFO buffer (which is an implementation detail). Suggested-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org> Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> Message-Id: <20240722160745.67904-7-philmd@linaro.org>
Diffstat (limited to 'util')
-rw-r--r--util/fifo8.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/util/fifo8.c b/util/fifo8.c
index 5bbb6150b6..a250ea9f80 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -102,6 +102,35 @@ const uint8_t *fifo8_pop_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
return fifo8_peekpop_buf(fifo, max, numptr, true);
}
+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;
+}
+
bool fifo8_is_empty(Fifo8 *fifo)
{
return (fifo->num == 0);