qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf()
@ 2024-07-22 16:07 Philippe Mathieu-Daudé
  2024-07-22 16:07 ` [PATCH v2 1/7] chardev/char-fe: Document returned value on error Philippe Mathieu-Daudé
                   ` (7 more replies)
  0 siblings, 8 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe =?unknown-8bit?q?Mathieu-Daud=C3=A9?=

Rename current fifo8_pop_buf() as fifo8_pop_constbuf()
and expose ESP's fifo8_pop_buf() which takes care of
wrapped FIFO buffer.

Supersedes: <20240719151628.46253-1-philmd@linaro.org>
  util/fifo8: Introduce fifo8_change_capacity()

Philippe Mathieu-Daudé (7):
  chardev/char-fe: Document returned value on error
  util/fifo8: Fix style
  util/fifo8: Use fifo8_reset() in fifo8_create()
  util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()
  util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()
  util/fifo8: Expose fifo8_pop_buf()
  util/fifo8: Introduce fifo8_discard()

 include/chardev/char-fe.h |  3 +++
 include/qemu/fifo8.h      | 50 ++++++++++++++++++++++++---------------
 chardev/msmouse.c         |  2 +-
 hw/char/goldfish_tty.c    |  4 ++--
 hw/net/allwinner_emac.c   |  2 +-
 hw/scsi/esp.c             | 38 ++++-------------------------
 ui/console-vc.c           |  2 +-
 ui/gtk.c                  |  2 +-
 util/fifo8.c              | 48 +++++++++++++++++++++++++++++++------
 9 files changed, 85 insertions(+), 66 deletions(-)

-- 
2.41.0



^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH v2 1/7] chardev/char-fe: Document returned value on error
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:06   ` Pierrick Bouvier
  2024-07-22 16:07 ` [PATCH v2 2/7] util/fifo8: Fix style Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

qemu_chr_fe_add_watch() and qemu_chr_fe_write[_all]()
return -1 on error. Mention it in the documentation.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
---
 include/chardev/char-fe.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/chardev/char-fe.h b/include/chardev/char-fe.h
index ecef182835..3310449eaf 100644
--- a/include/chardev/char-fe.h
+++ b/include/chardev/char-fe.h
@@ -228,6 +228,7 @@ guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
  * is thread-safe.
  *
  * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
 
@@ -242,6 +243,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
  * attempted to be written.  This function is thread-safe.
  *
  * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
 
@@ -253,6 +255,7 @@ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
  * Read data to a buffer from the back end.
  *
  * Returns: the number of bytes read (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 2/7] util/fifo8: Fix style
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
  2024-07-22 16:07 ` [PATCH v2 1/7] chardev/char-fe: Document returned value on error Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:04   ` Pierrick Bouvier
  2024-07-22 21:17   ` Mark Cave-Ayland
  2024-07-22 16:07 ` [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create() Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/fifo8.h | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
index c6295c6ff0..2692d6bfda 100644
--- a/include/qemu/fifo8.h
+++ b/include/qemu/fifo8.h
@@ -15,10 +15,9 @@ typedef struct {
  * @fifo: struct Fifo8 to initialise with new FIFO
  * @capacity: capacity of the newly created FIFO
  *
- * Create a FIFO of the specified size. Clients should call fifo8_destroy()
+ * Create a FIFO of the specified capacity. Clients should call fifo8_destroy()
  * when finished using the fifo. The FIFO is initially empty.
  */
-
 void fifo8_create(Fifo8 *fifo, uint32_t capacity);
 
 /**
@@ -26,9 +25,8 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
  * @fifo: FIFO to cleanup
  *
  * Cleanup a FIFO created with fifo8_create(). Frees memory created for FIFO
-  *storage. The FIFO is no longer usable after this has been called.
+ * storage. The FIFO is no longer usable after this has been called.
  */
-
 void fifo8_destroy(Fifo8 *fifo);
 
 /**
@@ -39,7 +37,6 @@ void fifo8_destroy(Fifo8 *fifo);
  * Push a data byte to the FIFO. Behaviour is undefined if the FIFO is full.
  * Clients are responsible for checking for fullness using fifo8_is_full().
  */
-
 void fifo8_push(Fifo8 *fifo, uint8_t data);
 
 /**
@@ -52,7 +49,6 @@ void fifo8_push(Fifo8 *fifo, uint8_t data);
  * Clients are responsible for checking the space left in the FIFO using
  * fifo8_num_free().
  */
-
 void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
 
 /**
@@ -64,7 +60,6 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
  *
  * Returns: The popped data byte.
  */
-
 uint8_t fifo8_pop(Fifo8 *fifo);
 
 /**
@@ -73,7 +68,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
  * @max: maximum number of bytes to pop
  * @numptr: pointer filled with number of bytes returned (can be NULL)
  *
- * Pop a number of elements from the FIFO up to a maximum of max. The buffer
+ * Pop a number of elements from the FIFO up to a maximum of @max. The buffer
  * containing the popped data is returned. This buffer points directly into
  * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
  * are called on the FIFO.
@@ -82,7 +77,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
  * around in the ring buffer; in this case only a contiguous part of the data
  * is returned.
  *
- * The number of valid bytes returned is populated in *numptr; will always
+ * The number of valid bytes returned is populated in *@numptr; will always
  * return at least 1 byte. max must not be 0 or greater than the number of
  * bytes in the FIFO.
  *
@@ -99,7 +94,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
  * @max: maximum number of bytes to peek
  * @numptr: pointer filled with number of bytes returned (can be NULL)
  *
- * Peek into a number of elements from the FIFO up to a maximum of max.
+ * Peek into a number of elements from the FIFO up to a maximum of @max.
  * The buffer containing the data peeked into is returned. This buffer points
  * directly into the FIFO backing store. Since data is invalidated once any
  * of the fifo8_* APIs are called on the FIFO, it is the caller responsibility
@@ -109,7 +104,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
  * around in the ring buffer; in this case only a contiguous part of the data
  * is returned.
  *
- * The number of valid bytes returned is populated in *numptr; will always
+ * The number of valid bytes returned is populated in *@numptr; will always
  * return at least 1 byte. max must not be 0 or greater than the number of
  * bytes in the FIFO.
  *
@@ -126,7 +121,6 @@ const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
  *
  * Reset a FIFO. All data is discarded and the FIFO is emptied.
  */
-
 void fifo8_reset(Fifo8 *fifo);
 
 /**
@@ -137,7 +131,6 @@ void fifo8_reset(Fifo8 *fifo);
  *
  * Returns: True if the fifo is empty, false otherwise.
  */
-
 bool fifo8_is_empty(Fifo8 *fifo);
 
 /**
@@ -148,7 +141,6 @@ bool fifo8_is_empty(Fifo8 *fifo);
  *
  * Returns: True if the fifo is full, false otherwise.
  */
-
 bool fifo8_is_full(Fifo8 *fifo);
 
 /**
@@ -159,7 +151,6 @@ bool fifo8_is_full(Fifo8 *fifo);
  *
  * Returns: Number of free bytes.
  */
-
 uint32_t fifo8_num_free(Fifo8 *fifo);
 
 /**
@@ -170,7 +161,6 @@ uint32_t fifo8_num_free(Fifo8 *fifo);
  *
  * Returns: Number of used bytes.
  */
-
 uint32_t fifo8_num_used(Fifo8 *fifo);
 
 extern const VMStateDescription vmstate_fifo8;
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
  2024-07-22 16:07 ` [PATCH v2 1/7] chardev/char-fe: Document returned value on error Philippe Mathieu-Daudé
  2024-07-22 16:07 ` [PATCH v2 2/7] util/fifo8: Fix style Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:06   ` Pierrick Bouvier
  2024-07-22 16:07 ` [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf() Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

Avoid open-coding fifo8_reset() in fifo8_create().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
---
 util/fifo8.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/util/fifo8.c b/util/fifo8.c
index 4e01b532d9..2925fe5611 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -16,12 +16,17 @@
 #include "migration/vmstate.h"
 #include "qemu/fifo8.h"
 
+void fifo8_reset(Fifo8 *fifo)
+{
+    fifo->num = 0;
+    fifo->head = 0;
+}
+
 void fifo8_create(Fifo8 *fifo, uint32_t capacity)
 {
     fifo->data = g_new(uint8_t, capacity);
     fifo->capacity = capacity;
-    fifo->head = 0;
-    fifo->num = 0;
+    fifo8_reset(fifo);
 }
 
 void fifo8_destroy(Fifo8 *fifo)
@@ -97,12 +102,6 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
     return fifo8_peekpop_buf(fifo, max, numptr, true);
 }
 
-void fifo8_reset(Fifo8 *fifo)
-{
-    fifo->num = 0;
-    fifo->head = 0;
-}
-
 bool fifo8_is_empty(Fifo8 *fifo)
 {
     return (fifo->num == 0);
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2024-07-22 16:07 ` [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create() Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:04   ` Pierrick Bouvier
  2024-07-22 21:22   ` Mark Cave-Ayland
  2024-07-22 16:07 ` [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf() Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  7 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

Since fifo8_peek_buf() return a const buffer (which points
directly into the FIFO backing store), rename it using the
'constbuf' suffix. This will help differentiate with methods
*copying* the FIFO data.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/fifo8.h | 4 ++--
 hw/scsi/esp.c        | 2 +-
 util/fifo8.c         | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
index 2692d6bfda..79450f4583 100644
--- a/include/qemu/fifo8.h
+++ b/include/qemu/fifo8.h
@@ -89,7 +89,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
 const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
 
 /**
- * fifo8_peek_buf: read upto max bytes from the fifo
+ * fifo8_peek_constbuf: read upto max bytes from the fifo
  * @fifo: FIFO to read from
  * @max: maximum number of bytes to peek
  * @numptr: pointer filled with number of bytes returned (can be NULL)
@@ -113,7 +113,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
  *
  * Returns: A pointer to peekable data.
  */
-const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
+const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
 
 /**
  * fifo8_reset:
diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
index 8504dd30a0..526ed91bef 100644
--- a/hw/scsi/esp.c
+++ b/hw/scsi/esp.c
@@ -486,7 +486,7 @@ static bool esp_cdb_ready(ESPState *s)
         return false;
     }
 
-    pbuf = fifo8_peek_buf(&s->cmdfifo, len, &n);
+    pbuf = fifo8_peek_constbuf(&s->cmdfifo, len, &n);
     if (n < len) {
         /*
          * In normal use the cmdfifo should never wrap, but include this check
diff --git a/util/fifo8.c b/util/fifo8.c
index 2925fe5611..21943c6032 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -92,7 +92,7 @@ static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max,
     return ret;
 }
 
-const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
+const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
 {
     return fifo8_peekpop_buf(fifo, max, numptr, false);
 }
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2024-07-22 16:07 ` [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf() Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:05   ` Pierrick Bouvier
  2024-07-22 21:24   ` Mark Cave-Ayland
  2024-07-22 16:07 ` [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  7 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

Since fifo8_pop_buf() return a const buffer (which points
directly into the FIFO backing store), rename it using the
'constbuf' suffix. This will help differentiate with methods
*copying* the FIFO data.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/fifo8.h    | 4 ++--
 chardev/msmouse.c       | 2 +-
 hw/char/goldfish_tty.c  | 4 ++--
 hw/net/allwinner_emac.c | 2 +-
 hw/scsi/esp.c           | 4 ++--
 ui/console-vc.c         | 2 +-
 ui/gtk.c                | 2 +-
 util/fifo8.c            | 2 +-
 8 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
index 79450f4583..686918a3a4 100644
--- a/include/qemu/fifo8.h
+++ b/include/qemu/fifo8.h
@@ -63,7 +63,7 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
 uint8_t fifo8_pop(Fifo8 *fifo);
 
 /**
- * fifo8_pop_buf:
+ * fifo8_pop_constbuf:
  * @fifo: FIFO to pop from
  * @max: maximum number of bytes to pop
  * @numptr: pointer filled with number of bytes returned (can be NULL)
@@ -86,7 +86,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
  *
  * Returns: A pointer to popped data.
  */
-const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
+const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
 
 /**
  * fifo8_peek_constbuf: read upto max bytes from the fifo
diff --git a/chardev/msmouse.c b/chardev/msmouse.c
index a774c397b4..08836d92e8 100644
--- a/chardev/msmouse.c
+++ b/chardev/msmouse.c
@@ -81,7 +81,7 @@ static void msmouse_chr_accept_input(Chardev *chr)
         const uint8_t *buf;
         uint32_t size;
 
-        buf = fifo8_pop_buf(&mouse->outbuf, MIN(len, avail), &size);
+        buf = fifo8_pop_constbuf(&mouse->outbuf, MIN(len, avail), &size);
         qemu_chr_be_write(chr, buf, size);
         len = qemu_chr_be_can_write(chr);
         avail -= size;
diff --git a/hw/char/goldfish_tty.c b/hw/char/goldfish_tty.c
index f8ff043c39..2c5004851d 100644
--- a/hw/char/goldfish_tty.c
+++ b/hw/char/goldfish_tty.c
@@ -69,7 +69,7 @@ static uint64_t goldfish_tty_read(void *opaque, hwaddr addr,
 static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
 {
     uint32_t to_copy;
-    uint8_t *buf;
+    const uint8_t *buf;
     uint8_t data_out[GOLFISH_TTY_BUFFER_SIZE];
     int len;
     uint64_t ptr;
@@ -109,7 +109,7 @@ static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
         len = s->data_len;
         ptr = s->data_ptr;
         while (len && !fifo8_is_empty(&s->rx_fifo)) {
-            buf = (uint8_t *)fifo8_pop_buf(&s->rx_fifo, len, &to_copy);
+            buf = fifo8_pop_constbuf(&s->rx_fifo, len, &to_copy);
             address_space_rw(&address_space_memory, ptr,
                             MEMTXATTRS_UNSPECIFIED, buf, to_copy, 1);
 
diff --git a/hw/net/allwinner_emac.c b/hw/net/allwinner_emac.c
index 989839784a..3b0a2ee07e 100644
--- a/hw/net/allwinner_emac.c
+++ b/hw/net/allwinner_emac.c
@@ -349,7 +349,7 @@ static void aw_emac_write(void *opaque, hwaddr offset, uint64_t value,
                               "allwinner_emac: TX length > fifo data length\n");
             }
             if (len > 0) {
-                data = fifo8_pop_buf(fifo, len, &ret);
+                data = fifo8_pop_constbuf(fifo, len, &ret);
                 qemu_send_packet(nc, data, ret);
                 aw_emac_tx_reset(s, chan);
                 /* Raise TX interrupt */
diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
index 526ed91bef..64384f9b0e 100644
--- a/hw/scsi/esp.c
+++ b/hw/scsi/esp.c
@@ -208,7 +208,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
     }
 
     len = maxlen;
-    buf = fifo8_pop_buf(fifo, len, &n);
+    buf = fifo8_pop_constbuf(fifo, len, &n);
     if (dest) {
         memcpy(dest, buf, n);
     }
@@ -217,7 +217,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
     len -= n;
     len = MIN(len, fifo8_num_used(fifo));
     if (len) {
-        buf = fifo8_pop_buf(fifo, len, &n2);
+        buf = fifo8_pop_constbuf(fifo, len, &n2);
         if (dest) {
             memcpy(&dest[n], buf, n2);
         }
diff --git a/ui/console-vc.c b/ui/console-vc.c
index 899fa11c94..e9906aae59 100644
--- a/ui/console-vc.c
+++ b/ui/console-vc.c
@@ -287,7 +287,7 @@ static void kbd_send_chars(QemuTextConsole *s)
         const uint8_t *buf;
         uint32_t size;
 
-        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
+        buf = fifo8_pop_constbuf(&s->out_fifo, MIN(len, avail), &size);
         qemu_chr_be_write(s->chr, buf, size);
         len = qemu_chr_be_can_write(s->chr);
         avail -= size;
diff --git a/ui/gtk.c b/ui/gtk.c
index bc29f7a1b4..a4db90e8cb 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -1820,7 +1820,7 @@ static void gd_vc_send_chars(VirtualConsole *vc)
         const uint8_t *buf;
         uint32_t size;
 
-        buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
+        buf = fifo8_pop_constbuf(&vc->vte.out_fifo, MIN(len, avail), &size);
         qemu_chr_be_write(vc->vte.chr, buf, size);
         len = qemu_chr_be_can_write(vc->vte.chr);
         avail -= size;
diff --git a/util/fifo8.c b/util/fifo8.c
index 21943c6032..31f0d34c0c 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -97,7 +97,7 @@ const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
     return fifo8_peekpop_buf(fifo, max, numptr, false);
 }
 
-const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
+const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
 {
     return fifo8_peekpop_buf(fifo, max, numptr, true);
 }
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2024-07-22 16:07 ` [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf() Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 21:06   ` Pierrick Bouvier
  2024-07-22 21:26   ` Mark Cave-Ayland
  2024-07-22 16:07 ` [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard() Philippe Mathieu-Daudé
  2024-07-23 18:02 ` [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
  7 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

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>
---
 include/qemu/fifo8.h | 14 ++++++++++++++
 hw/scsi/esp.c        | 36 +++---------------------------------
 util/fifo8.c         | 29 +++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
index 686918a3a4..21c7a22937 100644
--- a/include/qemu/fifo8.h
+++ b/include/qemu/fifo8.h
@@ -62,6 +62,20 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
  */
 uint8_t fifo8_pop(Fifo8 *fifo);
 
+/**
+ * fifo8_pop_buf:
+ * @fifo: FIFO to pop from
+ * @dest: the buffer to write the data into (can be NULL)
+ * @destlen: size of @dest and maximum number of bytes to pop
+ *
+ * Pop a number of elements from the FIFO up to a maximum of @destlen.
+ * The popped data is copied into the @dest buffer.
+ * Care is taken when the data wraps around in the ring buffer.
+ *
+ * Returns: number of bytes popped.
+ */
+uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen);
+
 /**
  * fifo8_pop_constbuf:
  * @fifo: FIFO to pop from
diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
index 64384f9b0e..cec847b54a 100644
--- a/hw/scsi/esp.c
+++ b/hw/scsi/esp.c
@@ -197,39 +197,9 @@ static uint8_t esp_fifo_pop(ESPState *s)
     return val;
 }
 
-static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
-{
-    const uint8_t *buf;
-    uint32_t n, n2;
-    int len;
-
-    if (maxlen == 0) {
-        return 0;
-    }
-
-    len = maxlen;
-    buf = fifo8_pop_constbuf(fifo, len, &n);
-    if (dest) {
-        memcpy(dest, buf, n);
-    }
-
-    /* Add FIFO wraparound if needed */
-    len -= n;
-    len = MIN(len, fifo8_num_used(fifo));
-    if (len) {
-        buf = fifo8_pop_constbuf(fifo, len, &n2);
-        if (dest) {
-            memcpy(&dest[n], buf, n2);
-        }
-        n += n2;
-    }
-
-    return n;
-}
-
 static uint32_t esp_fifo_pop_buf(ESPState *s, uint8_t *dest, int maxlen)
 {
-    uint32_t len = esp_fifo8_pop_buf(&s->fifo, dest, maxlen);
+    uint32_t len = fifo8_pop_buf(&s->fifo, dest, maxlen);
 
     esp_update_drq(s);
     return len;
@@ -335,7 +305,7 @@ static void do_command_phase(ESPState *s)
     if (!cmdlen || !s->current_dev) {
         return;
     }
-    esp_fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
+    fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
 
     current_lun = scsi_device_find(&s->bus, 0, s->current_dev->id, s->lun);
     if (!current_lun) {
@@ -381,7 +351,7 @@ static void do_message_phase(ESPState *s)
     /* Ignore extended messages for now */
     if (s->cmdfifo_cdb_offset) {
         int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
-        esp_fifo8_pop_buf(&s->cmdfifo, NULL, len);
+        fifo8_pop_buf(&s->cmdfifo, NULL, len);
         s->cmdfifo_cdb_offset = 0;
     }
 }
diff --git a/util/fifo8.c b/util/fifo8.c
index 31f0d34c0c..6610b79182 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -102,6 +102,35 @@ const uint8_t *fifo8_pop_constbuf(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_constbuf(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_constbuf(fifo, len, &n2);
+        if (dest) {
+            memcpy(&dest[n1], buf, n2);
+        }
+    }
+
+    return n1 + n2;
+}
+
 bool fifo8_is_empty(Fifo8 *fifo)
 {
     return (fifo->num == 0);
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2024-07-22 16:07 ` [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf() Philippe Mathieu-Daudé
@ 2024-07-22 16:07 ` Philippe Mathieu-Daudé
  2024-07-22 16:12   ` Philippe Mathieu-Daudé
  2024-07-22 21:52   ` Mark Cave-Ayland
  2024-07-23 18:02 ` [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
  7 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm,
	Philippe Mathieu-Daudé

Add the fifo8_discard() helper for clarity.
It is a simple wrapper over fifo8_pop_buf().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/fifo8.h | 8 ++++++++
 hw/scsi/esp.c        | 2 +-
 util/fifo8.c         | 6 ++++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
index 21c7a22937..53bafabd25 100644
--- a/include/qemu/fifo8.h
+++ b/include/qemu/fifo8.h
@@ -129,6 +129,14 @@ const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
  */
 const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
 
+/**
+ * fifo8_discard:
+ * @fifo: FIFO to consume bytes
+ *
+ * Discard (consume) bytes from a FIFO.
+ */
+void fifo8_discard(Fifo8 *fifo, uint32_t len);
+
 /**
  * fifo8_reset:
  * @fifo: FIFO to reset
diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
index cec847b54a..c703fa7351 100644
--- a/hw/scsi/esp.c
+++ b/hw/scsi/esp.c
@@ -351,7 +351,7 @@ static void do_message_phase(ESPState *s)
     /* Ignore extended messages for now */
     if (s->cmdfifo_cdb_offset) {
         int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
-        fifo8_pop_buf(&s->cmdfifo, NULL, len);
+        fifo8_discard(&s->cmdfifo, len);
         s->cmdfifo_cdb_offset = 0;
     }
 }
diff --git a/util/fifo8.c b/util/fifo8.c
index 6610b79182..ea39ca2552 100644
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -131,6 +131,12 @@ uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
     return n1 + n2;
 }
 
+void fifo8_consume(Fifo8 *fifo, uint32_t len)
+{
+    len -= fifo8_pop_buf(fifo, NULL, len);
+    assert(len == 0);
+}
+
 bool fifo8_is_empty(Fifo8 *fifo)
 {
     return (fifo->num == 0);
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()
  2024-07-22 16:07 ` [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard() Philippe Mathieu-Daudé
@ 2024-07-22 16:12   ` Philippe Mathieu-Daudé
  2024-07-22 21:07     ` Pierrick Bouvier
  2024-07-22 21:52   ` Mark Cave-Ayland
  1 sibling, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 16:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 22/7/24 18:07, Philippe Mathieu-Daudé wrote:
> Add the fifo8_discard() helper for clarity.
> It is a simple wrapper over fifo8_pop_buf().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 8 ++++++++
>   hw/scsi/esp.c        | 2 +-
>   util/fifo8.c         | 6 ++++++
>   3 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 21c7a22937..53bafabd25 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -129,6 +129,14 @@ const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    */
>   const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
> +/**
> + * fifo8_discard:
> + * @fifo: FIFO to consume bytes
> + *
> + * Discard (consume) bytes from a FIFO.
> + */
> +void fifo8_discard(Fifo8 *fifo, uint32_t len);
> +
>   /**
>    * fifo8_reset:
>    * @fifo: FIFO to reset
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index cec847b54a..c703fa7351 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -351,7 +351,7 @@ static void do_message_phase(ESPState *s)
>       /* Ignore extended messages for now */
>       if (s->cmdfifo_cdb_offset) {
>           int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
> -        fifo8_pop_buf(&s->cmdfifo, NULL, len);
> +        fifo8_discard(&s->cmdfifo, len);
>           s->cmdfifo_cdb_offset = 0;
>       }
>   }
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 6610b79182..ea39ca2552 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -131,6 +131,12 @@ uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
>       return n1 + n2;
>   }
>   
> +void fifo8_consume(Fifo8 *fifo, uint32_t len)

Sorry, forgot to s/fifo8_consume/fifo8_discard/.

> +{
> +    len -= fifo8_pop_buf(fifo, NULL, len);
> +    assert(len == 0);
> +}
> +
>   bool fifo8_is_empty(Fifo8 *fifo)
>   {
>       return (fifo->num == 0);



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 2/7] util/fifo8: Fix style
  2024-07-22 16:07 ` [PATCH v2 2/7] util/fifo8: Fix style Philippe Mathieu-Daudé
@ 2024-07-22 21:04   ` Pierrick Bouvier
  2024-07-22 21:17   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:04 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 22 ++++++----------------
>   1 file changed, 6 insertions(+), 16 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index c6295c6ff0..2692d6bfda 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -15,10 +15,9 @@ typedef struct {
>    * @fifo: struct Fifo8 to initialise with new FIFO
>    * @capacity: capacity of the newly created FIFO
>    *
> - * Create a FIFO of the specified size. Clients should call fifo8_destroy()
> + * Create a FIFO of the specified capacity. Clients should call fifo8_destroy()
>    * when finished using the fifo. The FIFO is initially empty.
>    */
> -
>   void fifo8_create(Fifo8 *fifo, uint32_t capacity);
>   
>   /**
> @@ -26,9 +25,8 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
>    * @fifo: FIFO to cleanup
>    *
>    * Cleanup a FIFO created with fifo8_create(). Frees memory created for FIFO
> -  *storage. The FIFO is no longer usable after this has been called.
> + * storage. The FIFO is no longer usable after this has been called.
>    */
> -
>   void fifo8_destroy(Fifo8 *fifo);
>   
>   /**
> @@ -39,7 +37,6 @@ void fifo8_destroy(Fifo8 *fifo);
>    * Push a data byte to the FIFO. Behaviour is undefined if the FIFO is full.
>    * Clients are responsible for checking for fullness using fifo8_is_full().
>    */
> -
>   void fifo8_push(Fifo8 *fifo, uint8_t data);
>   
>   /**
> @@ -52,7 +49,6 @@ void fifo8_push(Fifo8 *fifo, uint8_t data);
>    * Clients are responsible for checking the space left in the FIFO using
>    * fifo8_num_free().
>    */
> -
>   void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>   
>   /**
> @@ -64,7 +60,6 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>    *
>    * Returns: The popped data byte.
>    */
> -
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
>   /**
> @@ -73,7 +68,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    * @max: maximum number of bytes to pop
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
>    *
> - * Pop a number of elements from the FIFO up to a maximum of max. The buffer
> + * Pop a number of elements from the FIFO up to a maximum of @max. The buffer
>    * containing the popped data is returned. This buffer points directly into
>    * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
>    * are called on the FIFO.
> @@ -82,7 +77,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    * around in the ring buffer; in this case only a contiguous part of the data
>    * is returned.
>    *
> - * The number of valid bytes returned is populated in *numptr; will always
> + * The number of valid bytes returned is populated in *@numptr; will always
>    * return at least 1 byte. max must not be 0 or greater than the number of
>    * bytes in the FIFO.
>    *
> @@ -99,7 +94,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    * @max: maximum number of bytes to peek
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
>    *
> - * Peek into a number of elements from the FIFO up to a maximum of max.
> + * Peek into a number of elements from the FIFO up to a maximum of @max.
>    * The buffer containing the data peeked into is returned. This buffer points
>    * directly into the FIFO backing store. Since data is invalidated once any
>    * of the fifo8_* APIs are called on the FIFO, it is the caller responsibility
> @@ -109,7 +104,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    * around in the ring buffer; in this case only a contiguous part of the data
>    * is returned.
>    *
> - * The number of valid bytes returned is populated in *numptr; will always
> + * The number of valid bytes returned is populated in *@numptr; will always
>    * return at least 1 byte. max must not be 0 or greater than the number of
>    * bytes in the FIFO.
>    *
> @@ -126,7 +121,6 @@ const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    *
>    * Reset a FIFO. All data is discarded and the FIFO is emptied.
>    */
> -
>   void fifo8_reset(Fifo8 *fifo);
>   
>   /**
> @@ -137,7 +131,6 @@ void fifo8_reset(Fifo8 *fifo);
>    *
>    * Returns: True if the fifo is empty, false otherwise.
>    */
> -
>   bool fifo8_is_empty(Fifo8 *fifo);
>   
>   /**
> @@ -148,7 +141,6 @@ bool fifo8_is_empty(Fifo8 *fifo);
>    *
>    * Returns: True if the fifo is full, false otherwise.
>    */
> -
>   bool fifo8_is_full(Fifo8 *fifo);
>   
>   /**
> @@ -159,7 +151,6 @@ bool fifo8_is_full(Fifo8 *fifo);
>    *
>    * Returns: Number of free bytes.
>    */
> -
>   uint32_t fifo8_num_free(Fifo8 *fifo);
>   
>   /**
> @@ -170,7 +161,6 @@ uint32_t fifo8_num_free(Fifo8 *fifo);
>    *
>    * Returns: Number of used bytes.
>    */
> -
>   uint32_t fifo8_num_used(Fifo8 *fifo);
>   
>   extern const VMStateDescription vmstate_fifo8;

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()
  2024-07-22 16:07 ` [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf() Philippe Mathieu-Daudé
@ 2024-07-22 21:04   ` Pierrick Bouvier
  2024-07-22 21:22   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:04 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> Since fifo8_peek_buf() return a const buffer (which points
> directly into the FIFO backing store), rename it using the
> 'constbuf' suffix. This will help differentiate with methods
> *copying* the FIFO data.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 4 ++--
>   hw/scsi/esp.c        | 2 +-
>   util/fifo8.c         | 2 +-
>   3 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 2692d6bfda..79450f4583 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -89,7 +89,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>   const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
> - * fifo8_peek_buf: read upto max bytes from the fifo
> + * fifo8_peek_constbuf: read upto max bytes from the fifo
>    * @fifo: FIFO to read from
>    * @max: maximum number of bytes to peek
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
> @@ -113,7 +113,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    *
>    * Returns: A pointer to peekable data.
>    */
> -const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
> +const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
>    * fifo8_reset:
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 8504dd30a0..526ed91bef 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -486,7 +486,7 @@ static bool esp_cdb_ready(ESPState *s)
>           return false;
>       }
>   
> -    pbuf = fifo8_peek_buf(&s->cmdfifo, len, &n);
> +    pbuf = fifo8_peek_constbuf(&s->cmdfifo, len, &n);
>       if (n < len) {
>           /*
>            * In normal use the cmdfifo should never wrap, but include this check
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 2925fe5611..21943c6032 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -92,7 +92,7 @@ static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max,
>       return ret;
>   }
>   
> -const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
> +const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>   {
>       return fifo8_peekpop_buf(fifo, max, numptr, false);
>   }

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()
  2024-07-22 16:07 ` [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf() Philippe Mathieu-Daudé
@ 2024-07-22 21:05   ` Pierrick Bouvier
  2024-07-22 21:24   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:05 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> Since fifo8_pop_buf() return a const buffer (which points
> directly into the FIFO backing store), rename it using the
> 'constbuf' suffix. This will help differentiate with methods
> *copying* the FIFO data.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h    | 4 ++--
>   chardev/msmouse.c       | 2 +-
>   hw/char/goldfish_tty.c  | 4 ++--
>   hw/net/allwinner_emac.c | 2 +-
>   hw/scsi/esp.c           | 4 ++--
>   ui/console-vc.c         | 2 +-
>   ui/gtk.c                | 2 +-
>   util/fifo8.c            | 2 +-
>   8 files changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 79450f4583..686918a3a4 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -63,7 +63,7 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
>   /**
> - * fifo8_pop_buf:
> + * fifo8_pop_constbuf:
>    * @fifo: FIFO to pop from
>    * @max: maximum number of bytes to pop
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
> @@ -86,7 +86,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    *
>    * Returns: A pointer to popped data.
>    */
> -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
> +const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
>    * fifo8_peek_constbuf: read upto max bytes from the fifo
> diff --git a/chardev/msmouse.c b/chardev/msmouse.c
> index a774c397b4..08836d92e8 100644
> --- a/chardev/msmouse.c
> +++ b/chardev/msmouse.c
> @@ -81,7 +81,7 @@ static void msmouse_chr_accept_input(Chardev *chr)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&mouse->outbuf, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&mouse->outbuf, MIN(len, avail), &size);
>           qemu_chr_be_write(chr, buf, size);
>           len = qemu_chr_be_can_write(chr);
>           avail -= size;
> diff --git a/hw/char/goldfish_tty.c b/hw/char/goldfish_tty.c
> index f8ff043c39..2c5004851d 100644
> --- a/hw/char/goldfish_tty.c
> +++ b/hw/char/goldfish_tty.c
> @@ -69,7 +69,7 @@ static uint64_t goldfish_tty_read(void *opaque, hwaddr addr,
>   static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
>   {
>       uint32_t to_copy;
> -    uint8_t *buf;
> +    const uint8_t *buf;
>       uint8_t data_out[GOLFISH_TTY_BUFFER_SIZE];
>       int len;
>       uint64_t ptr;
> @@ -109,7 +109,7 @@ static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
>           len = s->data_len;
>           ptr = s->data_ptr;
>           while (len && !fifo8_is_empty(&s->rx_fifo)) {
> -            buf = (uint8_t *)fifo8_pop_buf(&s->rx_fifo, len, &to_copy);
> +            buf = fifo8_pop_constbuf(&s->rx_fifo, len, &to_copy);
>               address_space_rw(&address_space_memory, ptr,
>                               MEMTXATTRS_UNSPECIFIED, buf, to_copy, 1);
>   
> diff --git a/hw/net/allwinner_emac.c b/hw/net/allwinner_emac.c
> index 989839784a..3b0a2ee07e 100644
> --- a/hw/net/allwinner_emac.c
> +++ b/hw/net/allwinner_emac.c
> @@ -349,7 +349,7 @@ static void aw_emac_write(void *opaque, hwaddr offset, uint64_t value,
>                                 "allwinner_emac: TX length > fifo data length\n");
>               }
>               if (len > 0) {
> -                data = fifo8_pop_buf(fifo, len, &ret);
> +                data = fifo8_pop_constbuf(fifo, len, &ret);
>                   qemu_send_packet(nc, data, ret);
>                   aw_emac_tx_reset(s, chan);
>                   /* Raise TX interrupt */
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 526ed91bef..64384f9b0e 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -208,7 +208,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
>       }
>   
>       len = maxlen;
> -    buf = fifo8_pop_buf(fifo, len, &n);
> +    buf = fifo8_pop_constbuf(fifo, len, &n);
>       if (dest) {
>           memcpy(dest, buf, n);
>       }
> @@ -217,7 +217,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
>       len -= n;
>       len = MIN(len, fifo8_num_used(fifo));
>       if (len) {
> -        buf = fifo8_pop_buf(fifo, len, &n2);
> +        buf = fifo8_pop_constbuf(fifo, len, &n2);
>           if (dest) {
>               memcpy(&dest[n], buf, n2);
>           }
> diff --git a/ui/console-vc.c b/ui/console-vc.c
> index 899fa11c94..e9906aae59 100644
> --- a/ui/console-vc.c
> +++ b/ui/console-vc.c
> @@ -287,7 +287,7 @@ static void kbd_send_chars(QemuTextConsole *s)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&s->out_fifo, MIN(len, avail), &size);
>           qemu_chr_be_write(s->chr, buf, size);
>           len = qemu_chr_be_can_write(s->chr);
>           avail -= size;
> diff --git a/ui/gtk.c b/ui/gtk.c
> index bc29f7a1b4..a4db90e8cb 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -1820,7 +1820,7 @@ static void gd_vc_send_chars(VirtualConsole *vc)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&vc->vte.out_fifo, MIN(len, avail), &size);
>           qemu_chr_be_write(vc->vte.chr, buf, size);
>           len = qemu_chr_be_can_write(vc->vte.chr);
>           avail -= size;
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 21943c6032..31f0d34c0c 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -97,7 +97,7 @@ const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>       return fifo8_peekpop_buf(fifo, max, numptr, false);
>   }
>   
> -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
> +const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>   {
>       return fifo8_peekpop_buf(fifo, max, numptr, true);
>   }

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()
  2024-07-22 16:07 ` [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf() Philippe Mathieu-Daudé
@ 2024-07-22 21:06   ` Pierrick Bouvier
  2024-07-22 21:26   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:06 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> 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>
> ---
>   include/qemu/fifo8.h | 14 ++++++++++++++
>   hw/scsi/esp.c        | 36 +++---------------------------------
>   util/fifo8.c         | 29 +++++++++++++++++++++++++++++
>   3 files changed, 46 insertions(+), 33 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 686918a3a4..21c7a22937 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -62,6 +62,20 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>    */
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
> +/**
> + * fifo8_pop_buf:
> + * @fifo: FIFO to pop from
> + * @dest: the buffer to write the data into (can be NULL)
> + * @destlen: size of @dest and maximum number of bytes to pop
> + *
> + * Pop a number of elements from the FIFO up to a maximum of @destlen.
> + * The popped data is copied into the @dest buffer.
> + * Care is taken when the data wraps around in the ring buffer.
> + *
> + * Returns: number of bytes popped.
> + */
> +uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen);
> +
>   /**
>    * fifo8_pop_constbuf:
>    * @fifo: FIFO to pop from
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 64384f9b0e..cec847b54a 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -197,39 +197,9 @@ static uint8_t esp_fifo_pop(ESPState *s)
>       return val;
>   }
>   
> -static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
> -{
> -    const uint8_t *buf;
> -    uint32_t n, n2;
> -    int len;
> -
> -    if (maxlen == 0) {
> -        return 0;
> -    }
> -
> -    len = maxlen;
> -    buf = fifo8_pop_constbuf(fifo, len, &n);
> -    if (dest) {
> -        memcpy(dest, buf, n);
> -    }
> -
> -    /* Add FIFO wraparound if needed */
> -    len -= n;
> -    len = MIN(len, fifo8_num_used(fifo));
> -    if (len) {
> -        buf = fifo8_pop_constbuf(fifo, len, &n2);
> -        if (dest) {
> -            memcpy(&dest[n], buf, n2);
> -        }
> -        n += n2;
> -    }
> -
> -    return n;
> -}
> -
>   static uint32_t esp_fifo_pop_buf(ESPState *s, uint8_t *dest, int maxlen)
>   {
> -    uint32_t len = esp_fifo8_pop_buf(&s->fifo, dest, maxlen);
> +    uint32_t len = fifo8_pop_buf(&s->fifo, dest, maxlen);
>   
>       esp_update_drq(s);
>       return len;
> @@ -335,7 +305,7 @@ static void do_command_phase(ESPState *s)
>       if (!cmdlen || !s->current_dev) {
>           return;
>       }
> -    esp_fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
> +    fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
>   
>       current_lun = scsi_device_find(&s->bus, 0, s->current_dev->id, s->lun);
>       if (!current_lun) {
> @@ -381,7 +351,7 @@ static void do_message_phase(ESPState *s)
>       /* Ignore extended messages for now */
>       if (s->cmdfifo_cdb_offset) {
>           int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
> -        esp_fifo8_pop_buf(&s->cmdfifo, NULL, len);
> +        fifo8_pop_buf(&s->cmdfifo, NULL, len);
>           s->cmdfifo_cdb_offset = 0;
>       }
>   }
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 31f0d34c0c..6610b79182 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -102,6 +102,35 @@ const uint8_t *fifo8_pop_constbuf(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_constbuf(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_constbuf(fifo, len, &n2);
> +        if (dest) {
> +            memcpy(&dest[n1], buf, n2);
> +        }
> +    }
> +
> +    return n1 + n2;
> +}
> +
>   bool fifo8_is_empty(Fifo8 *fifo)
>   {
>       return (fifo->num == 0);

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create()
  2024-07-22 16:07 ` [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create() Philippe Mathieu-Daudé
@ 2024-07-22 21:06   ` Pierrick Bouvier
  0 siblings, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:06 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> Avoid open-coding fifo8_reset() in fifo8_create().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> ---
>   util/fifo8.c | 15 +++++++--------
>   1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 4e01b532d9..2925fe5611 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -16,12 +16,17 @@
>   #include "migration/vmstate.h"
>   #include "qemu/fifo8.h"
>   
> +void fifo8_reset(Fifo8 *fifo)
> +{
> +    fifo->num = 0;
> +    fifo->head = 0;
> +}
> +
>   void fifo8_create(Fifo8 *fifo, uint32_t capacity)
>   {
>       fifo->data = g_new(uint8_t, capacity);
>       fifo->capacity = capacity;
> -    fifo->head = 0;
> -    fifo->num = 0;
> +    fifo8_reset(fifo);
>   }
>   
>   void fifo8_destroy(Fifo8 *fifo)
> @@ -97,12 +102,6 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>       return fifo8_peekpop_buf(fifo, max, numptr, true);
>   }
>   
> -void fifo8_reset(Fifo8 *fifo)
> -{
> -    fifo->num = 0;
> -    fifo->head = 0;
> -}
> -
>   bool fifo8_is_empty(Fifo8 *fifo)
>   {
>       return (fifo->num == 0);

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 1/7] chardev/char-fe: Document returned value on error
  2024-07-22 16:07 ` [PATCH v2 1/7] chardev/char-fe: Document returned value on error Philippe Mathieu-Daudé
@ 2024-07-22 21:06   ` Pierrick Bouvier
  0 siblings, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:06 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:07, Philippe Mathieu-Daudé wrote:
> qemu_chr_fe_add_watch() and qemu_chr_fe_write[_all]()
> return -1 on error. Mention it in the documentation.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> ---
>   include/chardev/char-fe.h | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/include/chardev/char-fe.h b/include/chardev/char-fe.h
> index ecef182835..3310449eaf 100644
> --- a/include/chardev/char-fe.h
> +++ b/include/chardev/char-fe.h
> @@ -228,6 +228,7 @@ guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
>    * is thread-safe.
>    *
>    * Returns: the number of bytes consumed (0 if no associated Chardev)
> + *          or -1 on error.
>    */
>   int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
>   
> @@ -242,6 +243,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
>    * attempted to be written.  This function is thread-safe.
>    *
>    * Returns: the number of bytes consumed (0 if no associated Chardev)
> + *          or -1 on error.
>    */
>   int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
>   
> @@ -253,6 +255,7 @@ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
>    * Read data to a buffer from the back end.
>    *
>    * Returns: the number of bytes read (0 if no associated Chardev)
> + *          or -1 on error.
>    */
>   int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
>   

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()
  2024-07-22 16:12   ` Philippe Mathieu-Daudé
@ 2024-07-22 21:07     ` Pierrick Bouvier
  0 siblings, 0 replies; 24+ messages in thread
From: Pierrick Bouvier @ 2024-07-22 21:07 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 7/22/24 09:12, Philippe Mathieu-Daudé wrote:
> On 22/7/24 18:07, Philippe Mathieu-Daudé wrote:
>> Add the fifo8_discard() helper for clarity.
>> It is a simple wrapper over fifo8_pop_buf().
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>    include/qemu/fifo8.h | 8 ++++++++
>>    hw/scsi/esp.c        | 2 +-
>>    util/fifo8.c         | 6 ++++++
>>    3 files changed, 15 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
>> index 21c7a22937..53bafabd25 100644
>> --- a/include/qemu/fifo8.h
>> +++ b/include/qemu/fifo8.h
>> @@ -129,6 +129,14 @@ const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>>     */
>>    const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>>    
>> +/**
>> + * fifo8_discard:
>> + * @fifo: FIFO to consume bytes
>> + *
>> + * Discard (consume) bytes from a FIFO.
>> + */
>> +void fifo8_discard(Fifo8 *fifo, uint32_t len);
>> +
>>    /**
>>     * fifo8_reset:
>>     * @fifo: FIFO to reset
>> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
>> index cec847b54a..c703fa7351 100644
>> --- a/hw/scsi/esp.c
>> +++ b/hw/scsi/esp.c
>> @@ -351,7 +351,7 @@ static void do_message_phase(ESPState *s)
>>        /* Ignore extended messages for now */
>>        if (s->cmdfifo_cdb_offset) {
>>            int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
>> -        fifo8_pop_buf(&s->cmdfifo, NULL, len);
>> +        fifo8_discard(&s->cmdfifo, len);
>>            s->cmdfifo_cdb_offset = 0;
>>        }
>>    }
>> diff --git a/util/fifo8.c b/util/fifo8.c
>> index 6610b79182..ea39ca2552 100644
>> --- a/util/fifo8.c
>> +++ b/util/fifo8.c
>> @@ -131,6 +131,12 @@ uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
>>        return n1 + n2;
>>    }
>>    
>> +void fifo8_consume(Fifo8 *fifo, uint32_t len)
> 
> Sorry, forgot to s/fifo8_consume/fifo8_discard/.
> 
>> +{
>> +    len -= fifo8_pop_buf(fifo, NULL, len);
>> +    assert(len == 0);
>> +}
>> +
>>    bool fifo8_is_empty(Fifo8 *fifo)
>>    {
>>        return (fifo->num == 0);
> 
> 

with this fix,
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 2/7] util/fifo8: Fix style
  2024-07-22 16:07 ` [PATCH v2 2/7] util/fifo8: Fix style Philippe Mathieu-Daudé
  2024-07-22 21:04   ` Pierrick Bouvier
@ 2024-07-22 21:17   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 21:17 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:

> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 22 ++++++----------------
>   1 file changed, 6 insertions(+), 16 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index c6295c6ff0..2692d6bfda 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -15,10 +15,9 @@ typedef struct {
>    * @fifo: struct Fifo8 to initialise with new FIFO
>    * @capacity: capacity of the newly created FIFO
>    *
> - * Create a FIFO of the specified size. Clients should call fifo8_destroy()
> + * Create a FIFO of the specified capacity. Clients should call fifo8_destroy()
>    * when finished using the fifo. The FIFO is initially empty.
>    */
> -
>   void fifo8_create(Fifo8 *fifo, uint32_t capacity);
>   
>   /**
> @@ -26,9 +25,8 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
>    * @fifo: FIFO to cleanup
>    *
>    * Cleanup a FIFO created with fifo8_create(). Frees memory created for FIFO
> -  *storage. The FIFO is no longer usable after this has been called.
> + * storage. The FIFO is no longer usable after this has been called.
>    */
> -
>   void fifo8_destroy(Fifo8 *fifo);
>   
>   /**
> @@ -39,7 +37,6 @@ void fifo8_destroy(Fifo8 *fifo);
>    * Push a data byte to the FIFO. Behaviour is undefined if the FIFO is full.
>    * Clients are responsible for checking for fullness using fifo8_is_full().
>    */
> -
>   void fifo8_push(Fifo8 *fifo, uint8_t data);
>   
>   /**
> @@ -52,7 +49,6 @@ void fifo8_push(Fifo8 *fifo, uint8_t data);
>    * Clients are responsible for checking the space left in the FIFO using
>    * fifo8_num_free().
>    */
> -
>   void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>   
>   /**
> @@ -64,7 +60,6 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>    *
>    * Returns: The popped data byte.
>    */
> -
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
>   /**
> @@ -73,7 +68,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    * @max: maximum number of bytes to pop
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
>    *
> - * Pop a number of elements from the FIFO up to a maximum of max. The buffer
> + * Pop a number of elements from the FIFO up to a maximum of @max. The buffer
>    * containing the popped data is returned. This buffer points directly into
>    * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
>    * are called on the FIFO.
> @@ -82,7 +77,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    * around in the ring buffer; in this case only a contiguous part of the data
>    * is returned.
>    *
> - * The number of valid bytes returned is populated in *numptr; will always
> + * The number of valid bytes returned is populated in *@numptr; will always
>    * return at least 1 byte. max must not be 0 or greater than the number of
>    * bytes in the FIFO.
>    *
> @@ -99,7 +94,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    * @max: maximum number of bytes to peek
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
>    *
> - * Peek into a number of elements from the FIFO up to a maximum of max.
> + * Peek into a number of elements from the FIFO up to a maximum of @max.
>    * The buffer containing the data peeked into is returned. This buffer points
>    * directly into the FIFO backing store. Since data is invalidated once any
>    * of the fifo8_* APIs are called on the FIFO, it is the caller responsibility
> @@ -109,7 +104,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    * around in the ring buffer; in this case only a contiguous part of the data
>    * is returned.
>    *
> - * The number of valid bytes returned is populated in *numptr; will always
> + * The number of valid bytes returned is populated in *@numptr; will always
>    * return at least 1 byte. max must not be 0 or greater than the number of
>    * bytes in the FIFO.
>    *
> @@ -126,7 +121,6 @@ const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    *
>    * Reset a FIFO. All data is discarded and the FIFO is emptied.
>    */
> -
>   void fifo8_reset(Fifo8 *fifo);
>   
>   /**
> @@ -137,7 +131,6 @@ void fifo8_reset(Fifo8 *fifo);
>    *
>    * Returns: True if the fifo is empty, false otherwise.
>    */
> -
>   bool fifo8_is_empty(Fifo8 *fifo);
>   
>   /**
> @@ -148,7 +141,6 @@ bool fifo8_is_empty(Fifo8 *fifo);
>    *
>    * Returns: True if the fifo is full, false otherwise.
>    */
> -
>   bool fifo8_is_full(Fifo8 *fifo);
>   
>   /**
> @@ -159,7 +151,6 @@ bool fifo8_is_full(Fifo8 *fifo);
>    *
>    * Returns: Number of free bytes.
>    */
> -
>   uint32_t fifo8_num_free(Fifo8 *fifo);
>   
>   /**
> @@ -170,7 +161,6 @@ uint32_t fifo8_num_free(Fifo8 *fifo);
>    *
>    * Returns: Number of used bytes.
>    */
> -
>   uint32_t fifo8_num_used(Fifo8 *fifo);
>   
>   extern const VMStateDescription vmstate_fifo8;

Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>


ATB,

Mark.



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()
  2024-07-22 16:07 ` [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf() Philippe Mathieu-Daudé
  2024-07-22 21:04   ` Pierrick Bouvier
@ 2024-07-22 21:22   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 21:22 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:

> Since fifo8_peek_buf() return a const buffer (which points
> directly into the FIFO backing store), rename it using the
> 'constbuf' suffix. This will help differentiate with methods
> *copying* the FIFO data.

Perhaps fifo8_peek_bufptr() is a better reflection that it is a pointer to the 
internal buffer that is being returned here?

Still:

Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>


ATB,

Mark.

> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 4 ++--
>   hw/scsi/esp.c        | 2 +-
>   util/fifo8.c         | 2 +-
>   3 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 2692d6bfda..79450f4583 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -89,7 +89,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>   const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
> - * fifo8_peek_buf: read upto max bytes from the fifo
> + * fifo8_peek_constbuf: read upto max bytes from the fifo
>    * @fifo: FIFO to read from
>    * @max: maximum number of bytes to peek
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
> @@ -113,7 +113,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    *
>    * Returns: A pointer to peekable data.
>    */
> -const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
> +const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
>    * fifo8_reset:
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 8504dd30a0..526ed91bef 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -486,7 +486,7 @@ static bool esp_cdb_ready(ESPState *s)
>           return false;
>       }
>   
> -    pbuf = fifo8_peek_buf(&s->cmdfifo, len, &n);
> +    pbuf = fifo8_peek_constbuf(&s->cmdfifo, len, &n);
>       if (n < len) {
>           /*
>            * In normal use the cmdfifo should never wrap, but include this check
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 2925fe5611..21943c6032 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -92,7 +92,7 @@ static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max,
>       return ret;
>   }
>   
> -const uint8_t *fifo8_peek_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
> +const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>   {
>       return fifo8_peekpop_buf(fifo, max, numptr, false);
>   }



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()
  2024-07-22 16:07 ` [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf() Philippe Mathieu-Daudé
  2024-07-22 21:05   ` Pierrick Bouvier
@ 2024-07-22 21:24   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 21:24 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:

> Since fifo8_pop_buf() return a const buffer (which points
> directly into the FIFO backing store), rename it using the
> 'constbuf' suffix. This will help differentiate with methods
> *copying* the FIFO data.

Similar comment re: fifo8_pop_bufptr() as before, but still:

Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>


ATB,

Mark.

> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h    | 4 ++--
>   chardev/msmouse.c       | 2 +-
>   hw/char/goldfish_tty.c  | 4 ++--
>   hw/net/allwinner_emac.c | 2 +-
>   hw/scsi/esp.c           | 4 ++--
>   ui/console-vc.c         | 2 +-
>   ui/gtk.c                | 2 +-
>   util/fifo8.c            | 2 +-
>   8 files changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 79450f4583..686918a3a4 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -63,7 +63,7 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
>   /**
> - * fifo8_pop_buf:
> + * fifo8_pop_constbuf:
>    * @fifo: FIFO to pop from
>    * @max: maximum number of bytes to pop
>    * @numptr: pointer filled with number of bytes returned (can be NULL)
> @@ -86,7 +86,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
>    *
>    * Returns: A pointer to popped data.
>    */
> -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
> +const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
>   /**
>    * fifo8_peek_constbuf: read upto max bytes from the fifo
> diff --git a/chardev/msmouse.c b/chardev/msmouse.c
> index a774c397b4..08836d92e8 100644
> --- a/chardev/msmouse.c
> +++ b/chardev/msmouse.c
> @@ -81,7 +81,7 @@ static void msmouse_chr_accept_input(Chardev *chr)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&mouse->outbuf, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&mouse->outbuf, MIN(len, avail), &size);
>           qemu_chr_be_write(chr, buf, size);
>           len = qemu_chr_be_can_write(chr);
>           avail -= size;
> diff --git a/hw/char/goldfish_tty.c b/hw/char/goldfish_tty.c
> index f8ff043c39..2c5004851d 100644
> --- a/hw/char/goldfish_tty.c
> +++ b/hw/char/goldfish_tty.c
> @@ -69,7 +69,7 @@ static uint64_t goldfish_tty_read(void *opaque, hwaddr addr,
>   static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
>   {
>       uint32_t to_copy;
> -    uint8_t *buf;
> +    const uint8_t *buf;
>       uint8_t data_out[GOLFISH_TTY_BUFFER_SIZE];
>       int len;
>       uint64_t ptr;
> @@ -109,7 +109,7 @@ static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd)
>           len = s->data_len;
>           ptr = s->data_ptr;
>           while (len && !fifo8_is_empty(&s->rx_fifo)) {
> -            buf = (uint8_t *)fifo8_pop_buf(&s->rx_fifo, len, &to_copy);
> +            buf = fifo8_pop_constbuf(&s->rx_fifo, len, &to_copy);
>               address_space_rw(&address_space_memory, ptr,
>                               MEMTXATTRS_UNSPECIFIED, buf, to_copy, 1);
>   
> diff --git a/hw/net/allwinner_emac.c b/hw/net/allwinner_emac.c
> index 989839784a..3b0a2ee07e 100644
> --- a/hw/net/allwinner_emac.c
> +++ b/hw/net/allwinner_emac.c
> @@ -349,7 +349,7 @@ static void aw_emac_write(void *opaque, hwaddr offset, uint64_t value,
>                                 "allwinner_emac: TX length > fifo data length\n");
>               }
>               if (len > 0) {
> -                data = fifo8_pop_buf(fifo, len, &ret);
> +                data = fifo8_pop_constbuf(fifo, len, &ret);
>                   qemu_send_packet(nc, data, ret);
>                   aw_emac_tx_reset(s, chan);
>                   /* Raise TX interrupt */
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 526ed91bef..64384f9b0e 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -208,7 +208,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
>       }
>   
>       len = maxlen;
> -    buf = fifo8_pop_buf(fifo, len, &n);
> +    buf = fifo8_pop_constbuf(fifo, len, &n);
>       if (dest) {
>           memcpy(dest, buf, n);
>       }
> @@ -217,7 +217,7 @@ static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
>       len -= n;
>       len = MIN(len, fifo8_num_used(fifo));
>       if (len) {
> -        buf = fifo8_pop_buf(fifo, len, &n2);
> +        buf = fifo8_pop_constbuf(fifo, len, &n2);
>           if (dest) {
>               memcpy(&dest[n], buf, n2);
>           }
> diff --git a/ui/console-vc.c b/ui/console-vc.c
> index 899fa11c94..e9906aae59 100644
> --- a/ui/console-vc.c
> +++ b/ui/console-vc.c
> @@ -287,7 +287,7 @@ static void kbd_send_chars(QemuTextConsole *s)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&s->out_fifo, MIN(len, avail), &size);
>           qemu_chr_be_write(s->chr, buf, size);
>           len = qemu_chr_be_can_write(s->chr);
>           avail -= size;
> diff --git a/ui/gtk.c b/ui/gtk.c
> index bc29f7a1b4..a4db90e8cb 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -1820,7 +1820,7 @@ static void gd_vc_send_chars(VirtualConsole *vc)
>           const uint8_t *buf;
>           uint32_t size;
>   
> -        buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
> +        buf = fifo8_pop_constbuf(&vc->vte.out_fifo, MIN(len, avail), &size);
>           qemu_chr_be_write(vc->vte.chr, buf, size);
>           len = qemu_chr_be_can_write(vc->vte.chr);
>           avail -= size;
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 21943c6032..31f0d34c0c 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -97,7 +97,7 @@ const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>       return fifo8_peekpop_buf(fifo, max, numptr, false);
>   }
>   
> -const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
> +const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
>   {
>       return fifo8_peekpop_buf(fifo, max, numptr, true);
>   }



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()
  2024-07-22 16:07 ` [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf() Philippe Mathieu-Daudé
  2024-07-22 21:06   ` Pierrick Bouvier
@ 2024-07-22 21:26   ` Mark Cave-Ayland
  2024-07-22 21:39     ` Philippe Mathieu-Daudé
  1 sibling, 1 reply; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 21:26 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:

> 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).

I wonder if it is also worth updating the comment for fifo8_pop_bufptr() indicating 
that it returns a pointer to the internal buffer without checking for overflow, and 
that in general fifo8_pop_buf() is recommended instead? Otherwise:

Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>


ATB,

Mark.

> Suggested-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 14 ++++++++++++++
>   hw/scsi/esp.c        | 36 +++---------------------------------
>   util/fifo8.c         | 29 +++++++++++++++++++++++++++++
>   3 files changed, 46 insertions(+), 33 deletions(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 686918a3a4..21c7a22937 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -62,6 +62,20 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
>    */
>   uint8_t fifo8_pop(Fifo8 *fifo);
>   
> +/**
> + * fifo8_pop_buf:
> + * @fifo: FIFO to pop from
> + * @dest: the buffer to write the data into (can be NULL)
> + * @destlen: size of @dest and maximum number of bytes to pop
> + *
> + * Pop a number of elements from the FIFO up to a maximum of @destlen.
> + * The popped data is copied into the @dest buffer.
> + * Care is taken when the data wraps around in the ring buffer.
> + *
> + * Returns: number of bytes popped.
> + */
> +uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen);
> +
>   /**
>    * fifo8_pop_constbuf:
>    * @fifo: FIFO to pop from
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index 64384f9b0e..cec847b54a 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -197,39 +197,9 @@ static uint8_t esp_fifo_pop(ESPState *s)
>       return val;
>   }
>   
> -static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
> -{
> -    const uint8_t *buf;
> -    uint32_t n, n2;
> -    int len;
> -
> -    if (maxlen == 0) {
> -        return 0;
> -    }
> -
> -    len = maxlen;
> -    buf = fifo8_pop_constbuf(fifo, len, &n);
> -    if (dest) {
> -        memcpy(dest, buf, n);
> -    }
> -
> -    /* Add FIFO wraparound if needed */
> -    len -= n;
> -    len = MIN(len, fifo8_num_used(fifo));
> -    if (len) {
> -        buf = fifo8_pop_constbuf(fifo, len, &n2);
> -        if (dest) {
> -            memcpy(&dest[n], buf, n2);
> -        }
> -        n += n2;
> -    }
> -
> -    return n;
> -}
> -
>   static uint32_t esp_fifo_pop_buf(ESPState *s, uint8_t *dest, int maxlen)
>   {
> -    uint32_t len = esp_fifo8_pop_buf(&s->fifo, dest, maxlen);
> +    uint32_t len = fifo8_pop_buf(&s->fifo, dest, maxlen);
>   
>       esp_update_drq(s);
>       return len;
> @@ -335,7 +305,7 @@ static void do_command_phase(ESPState *s)
>       if (!cmdlen || !s->current_dev) {
>           return;
>       }
> -    esp_fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
> +    fifo8_pop_buf(&s->cmdfifo, buf, cmdlen);
>   
>       current_lun = scsi_device_find(&s->bus, 0, s->current_dev->id, s->lun);
>       if (!current_lun) {
> @@ -381,7 +351,7 @@ static void do_message_phase(ESPState *s)
>       /* Ignore extended messages for now */
>       if (s->cmdfifo_cdb_offset) {
>           int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
> -        esp_fifo8_pop_buf(&s->cmdfifo, NULL, len);
> +        fifo8_pop_buf(&s->cmdfifo, NULL, len);
>           s->cmdfifo_cdb_offset = 0;
>       }
>   }
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 31f0d34c0c..6610b79182 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -102,6 +102,35 @@ const uint8_t *fifo8_pop_constbuf(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_constbuf(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_constbuf(fifo, len, &n2);
> +        if (dest) {
> +            memcpy(&dest[n1], buf, n2);
> +        }
> +    }
> +
> +    return n1 + n2;
> +}
> +
>   bool fifo8_is_empty(Fifo8 *fifo)
>   {
>       return (fifo->num == 0);



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()
  2024-07-22 21:26   ` Mark Cave-Ayland
@ 2024-07-22 21:39     ` Philippe Mathieu-Daudé
  2024-07-22 22:23       ` Mark Cave-Ayland
  0 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-22 21:39 UTC (permalink / raw)
  To: Mark Cave-Ayland, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

Hi Mark,

On 22/7/24 23:26, Mark Cave-Ayland wrote:
> On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:
> 
>> 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).
> 
> I wonder if it is also worth updating the comment for fifo8_pop_bufptr() 
> indicating that it returns a pointer to the internal buffer without 
> checking for overflow,

We document:

  * The function may return fewer bytes than requested when the data wraps
  * around in the ring buffer; in this case only a contiguous part of 
the data
  * is returned.

but I'll try to reword a bit.

> and that in general fifo8_pop_buf() is 
> recommended instead?

Yes, this was my first motivation but then I forgot to write it :)

BTW I now see fifo8_pop/peek_bufptr() as dangerous API and am thinking
of deprecating them (after release). AFAICT the difference is a pair of
memcpy(), when I expect to not be that important performance wise.

> Otherwise:
> 
> Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>

Thanks!

BTW I'll respin this series including the fifo8_peek_buf() patch that
I forgot and is the one I need in PL011. Preview:

+uint32_t fifo8_peek_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
+{
+    uint32_t tail_count, head_count = 0;
+
+    if (destlen == 0) {
+        return 0;
+    }
+
+    destlen = MIN(destlen, fifo->num);
+    tail_count = MIN(fifo->capacity - fifo->head, destlen);
+
+    if (dest) {
+        memcpy(dest, &fifo->data[fifo->head], tail_count);
+    }
+
+    /* Add FIFO wraparound if needed */
+    destlen -= tail_count;
+    head_count = MIN(destlen, fifo->head);
+    if (head_count && dest) {
+        memcpy(&dest[tail_count], &fifo->data[0], head_count);
+    }
+
+    return tail_count + head_count;
+}

---


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()
  2024-07-22 16:07 ` [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard() Philippe Mathieu-Daudé
  2024-07-22 16:12   ` Philippe Mathieu-Daudé
@ 2024-07-22 21:52   ` Mark Cave-Ayland
  1 sibling, 0 replies; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 21:52 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:

> Add the fifo8_discard() helper for clarity.
> It is a simple wrapper over fifo8_pop_buf().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/fifo8.h | 8 ++++++++
>   hw/scsi/esp.c        | 2 +-
>   util/fifo8.c         | 6 ++++++
>   3 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h
> index 21c7a22937..53bafabd25 100644
> --- a/include/qemu/fifo8.h
> +++ b/include/qemu/fifo8.h
> @@ -129,6 +129,14 @@ const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>    */
>   const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr);
>   
> +/**
> + * fifo8_discard:
> + * @fifo: FIFO to consume bytes
> + *

Missing a reference to len in the comment above?

> + * Discard (consume) bytes from a FIFO.
> + */
> +void fifo8_discard(Fifo8 *fifo, uint32_t len);

Perhaps fifo8_drop() is a more descriptive name here? Regardless:

Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>


ATB,

Mark.

>   /**
>    * fifo8_reset:
>    * @fifo: FIFO to reset
> diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
> index cec847b54a..c703fa7351 100644
> --- a/hw/scsi/esp.c
> +++ b/hw/scsi/esp.c
> @@ -351,7 +351,7 @@ static void do_message_phase(ESPState *s)
>       /* Ignore extended messages for now */
>       if (s->cmdfifo_cdb_offset) {
>           int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
> -        fifo8_pop_buf(&s->cmdfifo, NULL, len);
> +        fifo8_discard(&s->cmdfifo, len);
>           s->cmdfifo_cdb_offset = 0;
>       }
>   }
> diff --git a/util/fifo8.c b/util/fifo8.c
> index 6610b79182..ea39ca2552 100644
> --- a/util/fifo8.c
> +++ b/util/fifo8.c
> @@ -131,6 +131,12 @@ uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
>       return n1 + n2;
>   }
>   
> +void fifo8_consume(Fifo8 *fifo, uint32_t len)
> +{
> +    len -= fifo8_pop_buf(fifo, NULL, len);
> +    assert(len == 0);
> +}
> +
>   bool fifo8_is_empty(Fifo8 *fifo)
>   {
>       return (fifo->num == 0);



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()
  2024-07-22 21:39     ` Philippe Mathieu-Daudé
@ 2024-07-22 22:23       ` Mark Cave-Ayland
  0 siblings, 0 replies; 24+ messages in thread
From: Mark Cave-Ayland @ 2024-07-22 22:23 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Marc-André Lureau, Alex Bennée,
	Peter Maydell, qemu-arm

On 22/07/2024 22:39, Philippe Mathieu-Daudé wrote:

> Hi Mark,
> 
> On 22/7/24 23:26, Mark Cave-Ayland wrote:
>> On 22/07/2024 17:07, Philippe Mathieu-Daudé wrote:
>>
>>> 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).
>>
>> I wonder if it is also worth updating the comment for fifo8_pop_bufptr() indicating 
>> that it returns a pointer to the internal buffer without checking for overflow,
> 
> We document:
> 
>   * The function may return fewer bytes than requested when the data wraps
>   * around in the ring buffer; in this case only a contiguous part of the data
>   * is returned.
> 
> but I'll try to reword a bit.
> 
>> and that in general fifo8_pop_buf() is recommended instead?
> 
> Yes, this was my first motivation but then I forgot to write it :)
> 
> BTW I now see fifo8_pop/peek_bufptr() as dangerous API and am thinking
> of deprecating them (after release). AFAICT the difference is a pair of
> memcpy(), when I expect to not be that important performance wise.

Funny - I had exactly the same thought ;)

>> Otherwise:
>>
>> Reviewed-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> 
> Thanks!
> 
> BTW I'll respin this series including the fifo8_peek_buf() patch that
> I forgot and is the one I need in PL011. Preview:
> 
> +uint32_t fifo8_peek_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen)
> +{
> +    uint32_t tail_count, head_count = 0;
> +
> +    if (destlen == 0) {
> +        return 0;
> +    }
> +
> +    destlen = MIN(destlen, fifo->num);
> +    tail_count = MIN(fifo->capacity - fifo->head, destlen);
> +
> +    if (dest) {
> +        memcpy(dest, &fifo->data[fifo->head], tail_count);
> +    }
> +
> +    /* Add FIFO wraparound if needed */
> +    destlen -= tail_count;
> +    head_count = MIN(destlen, fifo->head);
> +    if (head_count && dest) {
> +        memcpy(&dest[tail_count], &fifo->data[0], head_count);
> +    }
> +
> +    return tail_count + head_count;
> +}

Looks good at first glance, although it's getting late here now. If you're looking at 
making a few more changes before a respin, is it worth considering to add a new file 
with qtests for the updated Fifo8 implementation?


ATB,

Mark.



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf()
  2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2024-07-22 16:07 ` [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard() Philippe Mathieu-Daudé
@ 2024-07-23 18:02 ` Philippe Mathieu-Daudé
  7 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-07-23 18:02 UTC (permalink / raw)
  To: qemu-devel
  Cc: Mark Cave-Ayland, Paolo Bonzini, Marc-André Lureau,
	Alex Bennée, Peter Maydell, qemu-arm

On 22/7/24 18:07, Philippe Mathieu-Daudé wrote:
> Rename current fifo8_pop_buf() as fifo8_pop_constbuf()
> and expose ESP's fifo8_pop_buf() which takes care of
> wrapped FIFO buffer.
> 
> Supersedes: <20240719151628.46253-1-philmd@linaro.org>
>    util/fifo8: Introduce fifo8_change_capacity()
> 
> Philippe Mathieu-Daudé (7):
>    chardev/char-fe: Document returned value on error
>    util/fifo8: Fix style
>    util/fifo8: Use fifo8_reset() in fifo8_create()
>    util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()
>    util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()
>    util/fifo8: Expose fifo8_pop_buf()
>    util/fifo8: Introduce fifo8_discard()

Series queued, thanks.



^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2024-07-23 18:03 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-22 16:07 [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé
2024-07-22 16:07 ` [PATCH v2 1/7] chardev/char-fe: Document returned value on error Philippe Mathieu-Daudé
2024-07-22 21:06   ` Pierrick Bouvier
2024-07-22 16:07 ` [PATCH v2 2/7] util/fifo8: Fix style Philippe Mathieu-Daudé
2024-07-22 21:04   ` Pierrick Bouvier
2024-07-22 21:17   ` Mark Cave-Ayland
2024-07-22 16:07 ` [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create() Philippe Mathieu-Daudé
2024-07-22 21:06   ` Pierrick Bouvier
2024-07-22 16:07 ` [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf() Philippe Mathieu-Daudé
2024-07-22 21:04   ` Pierrick Bouvier
2024-07-22 21:22   ` Mark Cave-Ayland
2024-07-22 16:07 ` [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf() Philippe Mathieu-Daudé
2024-07-22 21:05   ` Pierrick Bouvier
2024-07-22 21:24   ` Mark Cave-Ayland
2024-07-22 16:07 ` [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf() Philippe Mathieu-Daudé
2024-07-22 21:06   ` Pierrick Bouvier
2024-07-22 21:26   ` Mark Cave-Ayland
2024-07-22 21:39     ` Philippe Mathieu-Daudé
2024-07-22 22:23       ` Mark Cave-Ayland
2024-07-22 16:07 ` [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard() Philippe Mathieu-Daudé
2024-07-22 16:12   ` Philippe Mathieu-Daudé
2024-07-22 21:07     ` Pierrick Bouvier
2024-07-22 21:52   ` Mark Cave-Ayland
2024-07-23 18:02 ` [PATCH v2 0/7] util/fifo8: Rework fifo8_pop_buf() Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).