Skip to contentSkip to conversation

util/fifo8: Rework fifo8_pop_buf()

PATCH In review Philippe Mathieu-Daudé 3 participants 24 messages Jul 22, 2024 – Jul 23, 2024 Arm/Allwinner sunXi SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover util/fifo8: Rework fifo8_pop_buf()

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

Supersedes: <[email protected]>
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(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/7 chardev/char-fe: Document returned value on error

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

---
 include/chardev/char-fe.h | 3 +++
 1 file changed, 3 insertions(+)
include/chardev/char-fe.h +3 -0
index ecef182835..3310449eaf 100644
@@ -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);
 
Signature · 1 line
2.41.0
Reviewed-by on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] chardev/char-fe: Document returned value on error

Quoted 30 lines · Philippe Mathieu-Daudé

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

---
  include/chardev/char-fe.h | 3 +++
  1 file changed, 3 insertions(+)
include/chardev/char-fe.h +2 -0
index ecef182835..3310449eaf 100644
@@ -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);
 

include/chardev/char-fe.h int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len); · line 261

Quoted 7 lines
include/chardev/char-fe.h +1 -0
@@ -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);
 
#3 lore raw

PATCH v2 2/7 util/fifo8: Fix style

In reply to Philippe Mathieu-Daudé
---
 include/qemu/fifo8.h | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
include/qemu/fifo8.h +6 -16
index c6295c6ff0..2692d6bfda 100644
@@ -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;
Signature · 1 line
2.41.0
Reviewed-by on v2 2/7
#4 lore raw

Re: [PATCH v2 2/7] util/fifo8: Fix style

Quoted 125 lines · Philippe Mathieu-Daudé
---
  include/qemu/fifo8.h | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
include/qemu/fifo8.h +6 -15
index c6295c6ff0..2692d6bfda 100644
@@ -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);
 
  /**

include/qemu/fifo8.h uint32_t fifo8_num_free(Fifo8 *fifo); · line 166

Quoted 7 lines
include/qemu/fifo8.h +0 -1
@@ -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 on v2 2/7
#5 lore raw

Re: [PATCH v2 2/7] util/fifo8: Fix style

In reply to Philippe Mathieu-Daudé
Quoted 125 lines · Philippe Mathieu-Daudé
---
  include/qemu/fifo8.h | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
include/qemu/fifo8.h +6 -15
index c6295c6ff0..2692d6bfda 100644
@@ -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);
 
  /**

include/qemu/fifo8.h uint32_t fifo8_num_free(Fifo8 *fifo); · line 166

Quoted 7 lines
include/qemu/fifo8.h +0 -1
@@ -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;

ATB,

Mark.

#6 lore raw

PATCH v2 3/7 util/fifo8: Use fifo8_reset() in fifo8_create()

In reply to Philippe Mathieu-Daudé

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

---
 util/fifo8.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
util/fifo8.c +7 -8
index 4e01b532d9..2925fe5611 100644
@@ -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);
Signature · 1 line
2.41.0
Reviewed-by on v2 3/7
#7 lore raw

Re: [PATCH v2 3/7] util/fifo8: Use fifo8_reset() in fifo8_create()

Quoted 35 lines · Philippe Mathieu-Daudé

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

---
  util/fifo8.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
util/fifo8.c +7 -2
index 4e01b532d9..2925fe5611 100644
@@ -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);
  }

util/fifo8.c const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) · line 107

Quoted 10 lines
util/fifo8.c +0 -6
 
-void fifo8_reset(Fifo8 *fifo)
-{
-    fifo->num = 0;
-    fifo->head = 0;
-}
-
  bool fifo8_is_empty(Fifo8 *fifo)
  {
      return (fifo->num == 0);
#8 lore raw

PATCH v2 4/7 util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()

In reply to 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.

---
 include/qemu/fifo8.h | 4 ++--
 hw/scsi/esp.c        | 2 +-
 util/fifo8.c         | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
include/qemu/fifo8.h +2 -2
index 2692d6bfda..79450f4583 100644
@@ -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:
hw/scsi/esp.c +1 -1
index 8504dd30a0..526ed91bef 100644
@@ -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
util/fifo8.c +1 -1
index 2925fe5611..21943c6032 100644
@@ -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);
 }
Signature · 1 line
2.41.0
Reviewed-by on v2 4/7
#9 lore raw

Re: [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()

Quoted 52 lines · 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.

---
  include/qemu/fifo8.h | 4 ++--
  hw/scsi/esp.c        | 2 +-
  util/fifo8.c         | 2 +-
  3 files changed, 4 insertions(+), 4 deletions(-)
include/qemu/fifo8.h +2 -2
index 2692d6bfda..79450f4583 100644
@@ -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:
hw/scsi/esp.c +1 -1
index 8504dd30a0..526ed91bef 100644
@@ -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
util/fifo8.c +0 -0
index 2925fe5611..21943c6032 100644

util/fifo8.c static const uint8_t *fifo8_peekpop_buf(Fifo8 *fifo, uint32_t max, · line 98

Quoted 8 lines
util/fifo8.c +1 -1
@@ -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 on v2 4/7
#10 lore raw

Re: [PATCH v2 4/7] util/fifo8: Rename fifo8_peek_buf() -> fifo8_peek_constbuf()

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · 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.

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

Still:

ATB,

Mark.

Quoted 55 lines
---
  include/qemu/fifo8.h | 4 ++--
  hw/scsi/esp.c        | 2 +-
  util/fifo8.c         | 2 +-
  3 files changed, 4 insertions(+), 4 deletions(-)
include/qemu/fifo8.h +2 -2
index 2692d6bfda..79450f4583 100644
@@ -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:
hw/scsi/esp.c +1 -1
index 8504dd30a0..526ed91bef 100644
@@ -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
util/fifo8.c +1 -1
index 2925fe5611..21943c6032 100644
@@ -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);
  }
#11 lore raw

PATCH v2 5/7 util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()

In reply to 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.

---
 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(-)
include/qemu/fifo8.h +2 -2
index 79450f4583..686918a3a4 100644
@@ -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
chardev/msmouse.c +1 -1
index a774c397b4..08836d92e8 100644
@@ -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;
hw/char/goldfish_tty.c +2 -2
index f8ff043c39..2c5004851d 100644
@@ -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);
 
hw/net/allwinner_emac.c +1 -1
index 989839784a..3b0a2ee07e 100644
@@ -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 */
hw/scsi/esp.c +2 -2
index 526ed91bef..64384f9b0e 100644
@@ -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);
         }
ui/console-vc.c +1 -1
index 899fa11c94..e9906aae59 100644
@@ -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;
ui/gtk.c +1 -1
index bc29f7a1b4..a4db90e8cb 100644
@@ -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;
util/fifo8.c +1 -1
index 21943c6032..31f0d34c0c 100644
@@ -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);
 }
Signature · 1 line
2.41.0
Reviewed-by on v2 5/7
#12 lore raw

Re: [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()

Quoted 140 lines · 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.

---
  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(-)
include/qemu/fifo8.h +2 -2
index 79450f4583..686918a3a4 100644
@@ -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
chardev/msmouse.c +1 -1
index a774c397b4..08836d92e8 100644
@@ -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;
hw/char/goldfish_tty.c +2 -2
index f8ff043c39..2c5004851d 100644
@@ -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);
 
hw/net/allwinner_emac.c +1 -1
index 989839784a..3b0a2ee07e 100644
@@ -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 */
hw/scsi/esp.c +2 -2
index 526ed91bef..64384f9b0e 100644
@@ -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);
          }
ui/console-vc.c +1 -1
index 899fa11c94..e9906aae59 100644
@@ -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;
ui/gtk.c +1 -1
index bc29f7a1b4..a4db90e8cb 100644
@@ -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;
util/fifo8.c +0 -0
index 21943c6032..31f0d34c0c 100644

util/fifo8.c const uint8_t *fifo8_peek_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) · line 103

Quoted 8 lines
util/fifo8.c +1 -1
@@ -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 on v2 5/7
#13 lore raw

Re: [PATCH v2 5/7] util/fifo8: Rename fifo8_pop_buf() -> fifo8_pop_constbuf()

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · 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.

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

ATB,

Mark.

Quoted 143 lines
---
  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(-)
include/qemu/fifo8.h +2 -2
index 79450f4583..686918a3a4 100644
@@ -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
chardev/msmouse.c +1 -1
index a774c397b4..08836d92e8 100644
@@ -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;
hw/char/goldfish_tty.c +2 -2
index f8ff043c39..2c5004851d 100644
@@ -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);
 
hw/net/allwinner_emac.c +1 -1
index 989839784a..3b0a2ee07e 100644
@@ -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 */
hw/scsi/esp.c +2 -2
index 526ed91bef..64384f9b0e 100644
@@ -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);
          }
ui/console-vc.c +1 -1
index 899fa11c94..e9906aae59 100644
@@ -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;
ui/gtk.c +1 -1
index bc29f7a1b4..a4db90e8cb 100644
@@ -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;
util/fifo8.c +1 -1
index 21943c6032..31f0d34c0c 100644
@@ -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);
  }
#14 lore raw

PATCH v2 6/7 util/fifo8: Expose fifo8_pop_buf()

In reply to 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).

---
 include/qemu/fifo8.h | 14 ++++++++++++++
 hw/scsi/esp.c        | 36 +++---------------------------------
 util/fifo8.c         | 29 +++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 33 deletions(-)
include/qemu/fifo8.h +14 -0
index 686918a3a4..21c7a22937 100644
@@ -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
hw/scsi/esp.c +3 -33
index 64384f9b0e..cec847b54a 100644
@@ -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;
     }
 }
util/fifo8.c +29 -0
index 31f0d34c0c..6610b79182 100644
@@ -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);
Signature · 1 line
2.41.0
Reviewed-by on v2 6/7
#15 lore raw

Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()

Quoted 131 lines · 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).

---
  include/qemu/fifo8.h | 14 ++++++++++++++
  hw/scsi/esp.c        | 36 +++---------------------------------
  util/fifo8.c         | 29 +++++++++++++++++++++++++++++
  3 files changed, 46 insertions(+), 33 deletions(-)
include/qemu/fifo8.h +14 -0
index 686918a3a4..21c7a22937 100644
@@ -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
hw/scsi/esp.c +3 -33
index 64384f9b0e..cec847b54a 100644
@@ -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;
      }
  }
util/fifo8.c +22 -0
index 31f0d34c0c..6610b79182 100644
@@ -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) {

util/fifo8.c const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) · line 136

Quoted 10 lines
util/fifo8.c +7 -0
+            memcpy(&dest[n1], buf, n2);
+        }
+    }
+
+    return n1 + n2;
+}
+
  bool fifo8_is_empty(Fifo8 *fifo)
  {
      return (fifo->num == 0);
Reviewed-by on v2 6/7
#16 lore raw

Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · 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).

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:

ATB,

Mark.

Quoted 136 lines
---
  include/qemu/fifo8.h | 14 ++++++++++++++
  hw/scsi/esp.c        | 36 +++---------------------------------
  util/fifo8.c         | 29 +++++++++++++++++++++++++++++
  3 files changed, 46 insertions(+), 33 deletions(-)
include/qemu/fifo8.h +14 -0
index 686918a3a4..21c7a22937 100644
@@ -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
hw/scsi/esp.c +3 -33
index 64384f9b0e..cec847b54a 100644
@@ -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;
      }
  }
util/fifo8.c +29 -0
index 31f0d34c0c..6610b79182 100644
@@ -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);
#17 lore raw

Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()

Hi Mark,

Quoted 8 lines · Mark Cave-Ayland
Quoted 4 lines · 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).

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.

Quoted 1 line

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.

Quoted 3 lines

Otherwise:

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;
+}

---

#18 lore raw

Re: [PATCH v2 6/7] util/fifo8: Expose fifo8_pop_buf()

Quoted 20 lines · Philippe Mathieu-Daudé

Hi Mark,

Quoted 8 lines · Mark Cave-Ayland
Quoted 4 lines · 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).

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.

Quoted 7 lines
Quoted 1 line

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

Quoted 23 lines
Quoted 3 lines

Otherwise:

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:

util/fifo8.c +14 -0
+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);
+    }

util/fifo8.c const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)

Quoted 10 lines
util/fifo8.c +10 -0
+
+    /* 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.

#19 lore raw

PATCH v2 7/7 util/fifo8: Introduce fifo8_discard()

In reply to Philippe Mathieu-Daudé

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

---
 include/qemu/fifo8.h | 8 ++++++++
 hw/scsi/esp.c        | 2 +-
 util/fifo8.c         | 6 ++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
include/qemu/fifo8.h +8 -0
index 21c7a22937..53bafabd25 100644
@@ -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
hw/scsi/esp.c +1 -1
index cec847b54a..c703fa7351 100644
@@ -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;
     }
 }
util/fifo8.c +6 -0
index 6610b79182..ea39ca2552 100644
@@ -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);
Signature · 1 line
2.41.0
#20 lore raw

Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()

Quoted 47 lines · Philippe Mathieu-Daudé

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

---
  include/qemu/fifo8.h | 8 ++++++++
  hw/scsi/esp.c        | 2 +-
  util/fifo8.c         | 6 ++++++
  3 files changed, 15 insertions(+), 1 deletion(-)
include/qemu/fifo8.h +8 -0
index 21c7a22937..53bafabd25 100644
@@ -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
hw/scsi/esp.c +1 -1
index cec847b54a..c703fa7351 100644
@@ -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;
      }
  }
util/fifo8.c +0 -0
index 6610b79182..ea39ca2552 100644

util/fifo8.c uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) · line 134

Quoted 4 lines
util/fifo8.c +1 -0
@@ -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/.

Quoted 8 lines
util/fifo8.c +5 -0
+{
+    len -= fifo8_pop_buf(fifo, NULL, len);
+    assert(len == 0);
+}
+
  bool fifo8_is_empty(Fifo8 *fifo)
  {
      return (fifo->num == 0);
Reviewed-by
#21 lore raw

Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()

Quoted 55 lines · Philippe Mathieu-Daudé
Quoted 47 lines · Philippe Mathieu-Daudé

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

---
   include/qemu/fifo8.h | 8 ++++++++
   hw/scsi/esp.c        | 2 +-
   util/fifo8.c         | 6 ++++++
   3 files changed, 15 insertions(+), 1 deletion(-)
include/qemu/fifo8.h +8 -0
index 21c7a22937..53bafabd25 100644
@@ -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
hw/scsi/esp.c +1 -1
index cec847b54a..c703fa7351 100644
@@ -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;
       }
   }
util/fifo8.c +0 -0
index 6610b79182..ea39ca2552 100644

util/fifo8.c uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) · line 134

Quoted 4 lines
util/fifo8.c +1 -0
@@ -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/.

Quoted 10 lines

util/fifo8.c uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) · line 142

Quoted 8 lines
util/fifo8.c +5 -0
+{
+    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 on v2 7/7
#22 lore raw

Re: [PATCH v2 7/7] util/fifo8: Introduce fifo8_discard()

In reply to Philippe Mathieu-Daudé
Quoted 15 lines · Philippe Mathieu-Daudé

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

---
  include/qemu/fifo8.h | 8 ++++++++
  hw/scsi/esp.c        | 2 +-
  util/fifo8.c         | 6 ++++++
  3 files changed, 15 insertions(+), 1 deletion(-)
include/qemu/fifo8.h +0 -0
index 21c7a22937..53bafabd25 100644

include/qemu/fifo8.h const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); · line 135

Quoted 7 lines
include/qemu/fifo8.h +4 -0
@@ -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?

include/qemu/fifo8.h const uint8_t *fifo8_pop_constbuf(Fifo8 *fifo, uint32_t max, uint32_t *numptr); · line 138

Quoted 3 lines
include/qemu/fifo8.h +3 -0
+ * Discard (consume) bytes from a FIFO.
+ */
+void fifo8_discard(Fifo8 *fifo, uint32_t len);

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

ATB,

Mark.

Quoted 33 lines
include/qemu/fifo8.h +0 -0
  /**
   * fifo8_reset:
   * @fifo: FIFO to reset
hw/scsi/esp.c +1 -1
index cec847b54a..c703fa7351 100644
@@ -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;
      }
  }
util/fifo8.c +6 -0
index 6610b79182..ea39ca2552 100644
@@ -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);
#23 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

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

Supersedes: <[email protected]>
util/fifo8: Introduce fifo8_change_capacity()

Quoted 8 lines

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.