[vlc-devel] [PATCH] [TO SQUASH] queue: move offset to vlc_queue_Init()

RĂ©mi Denis-Courmont remi at remlab.net
Sun Apr 12 08:00:20 CEST 2020


This simplifies API usage.
---
 include/vlc_queue.h | 23 +++++++++++------------
 src/misc/queue.c    | 17 ++++++++++-------
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/include/vlc_queue.h b/include/vlc_queue.h
index eed69510dc..44222c1ae0 100644
--- a/include/vlc_queue.h
+++ b/include/vlc_queue.h
@@ -29,6 +29,7 @@
  */
 
 #include <stdbool.h>
+#include <stdint.h>
 #include <vlc_common.h>
 
 /**
@@ -43,14 +44,19 @@ typedef struct vlc_queue
 {
     struct vlc_queue_entry *first;
     struct vlc_queue_entry **lastp;
+    ptrdiff_t next_offset;
     vlc_mutex_t lock;
     vlc_cond_t wait;
 } vlc_queue_t;
 
 /**
  * Initializes a queue.
+ *
+ * @param queue storage space for the queue
+ * @param next_offset offset of the pointer to the next element
+ *                    within a queue entry (as per @c offsetof())
  */
-VLC_API void vlc_queue_Init(vlc_queue_t *);
+VLC_API void vlc_queue_Init(vlc_queue_t *queue, ptrdiff_t next_offset);
 
 /**
  * @defgroup queue_ll Queue internals
@@ -127,10 +133,8 @@ static inline void vlc_queue_Wait(vlc_queue_t *q)
  *
  * @param entry NULL-terminated list of entries to queue
  *              (if NULL, this function has no effects)
- * @param offset offset of the next pointer within a queue entry
  */
-VLC_API void vlc_queue_EnqueueUnlocked(vlc_queue_t *, void *entry,
-                                       ptrdiff_t offset);
+VLC_API void vlc_queue_EnqueueUnlocked(vlc_queue_t *, void *entry);
 
 /**
  * Dequeues the oldest entry (without locking).
@@ -142,12 +146,9 @@ VLC_API void vlc_queue_EnqueueUnlocked(vlc_queue_t *, void *entry,
  * @warning It is assumed that the caller already holds the queue lock;
  * otherwise the behaviour is undefined.
  *
- * @param offset offset of the next pointer within a queue entry
- *
  * @return the first entry in the queue, or NULL if the queue is empty
  */
-VLC_API void *vlc_queue_DequeueUnlocked(vlc_queue_t *, ptrdiff_t offset)
-VLC_USED;
+VLC_API void *vlc_queue_DequeueUnlocked(vlc_queue_t *) VLC_USED;
 
 /**
  * Dequeues all entries (without locking).
@@ -186,9 +187,8 @@ VLC_USED static inline bool vlc_queue_IsEmpty(const vlc_queue_t *q)
  * thread-safe queue.
  *
  * @param entry list of entries (if NULL, this function has no effects)
- * @param offset offset of the next pointer within a queue entry
  */
-VLC_API void vlc_queue_Enqueue(vlc_queue_t *, void *entry, ptrdiff_t offset);
+VLC_API void vlc_queue_Enqueue(vlc_queue_t *, void *entry);
 
 /**
  * Dequeues the oldest entry.
@@ -200,8 +200,7 @@ VLC_API void vlc_queue_Enqueue(vlc_queue_t *, void *entry, ptrdiff_t offset);
  *
  * @return the first entry in the queue, or NULL if the queue is empty
  */
-VLC_API void *vlc_queue_Dequeue(vlc_queue_t *, ptrdiff_t offset)
-VLC_USED;
+VLC_API void *vlc_queue_Dequeue(vlc_queue_t *) VLC_USED;
 
 /**
  * Dequeues all entries.
diff --git a/src/misc/queue.c b/src/misc/queue.c
index 665dfc7031..2ea374a5c1 100644
--- a/src/misc/queue.c
+++ b/src/misc/queue.c
@@ -68,17 +68,19 @@ static struct vlc_queue_entry *next_get(const struct vlc_queue_entry *e,
     return entry_get(next_p(e, offset));
 }
 
-void vlc_queue_Init(vlc_queue_t *q)
+void vlc_queue_Init(vlc_queue_t *q, ptrdiff_t next_offset)
 {
     q->first = NULL;
     q->lastp = &q->first;
+    q->next_offset = next_offset;
     vlc_mutex_init(&q->lock);
     vlc_cond_init(&q->wait);
 }
 
-void vlc_queue_EnqueueUnlocked(vlc_queue_t *q, void *entry, ptrdiff_t offset)
+void vlc_queue_EnqueueUnlocked(vlc_queue_t *q, void *entry)
 {
     struct vlc_queue_entry **lastp;
+    const ptrdiff_t offset = q->next_offset;
 
     vlc_mutex_assert(&q->lock);
     assert(entry_get(q->lastp) == NULL);
@@ -91,11 +93,12 @@ void vlc_queue_EnqueueUnlocked(vlc_queue_t *q, void *entry, ptrdiff_t offset)
     vlc_queue_Signal(q);
 }
 
-void *vlc_queue_DequeueUnlocked(vlc_queue_t *q, ptrdiff_t offset)
+void *vlc_queue_DequeueUnlocked(vlc_queue_t *q)
 {
     vlc_mutex_assert(&q->lock);
 
     void *entry = q->first;
+    const ptrdiff_t offset = q->next_offset;
 
     if (entry != NULL) {
         struct vlc_queue_entry *next = next_get(entry, offset);
@@ -122,14 +125,14 @@ void *vlc_queue_DequeueAllUnlocked(vlc_queue_t *q)
     return entry;
 }
 
-void vlc_queue_Enqueue(vlc_queue_t *q, void *entry, ptrdiff_t offset)
+void vlc_queue_Enqueue(vlc_queue_t *q, void *entry)
 {
     vlc_queue_Lock(q);
-    vlc_queue_EnqueueUnlocked(q, entry, offset);
+    vlc_queue_EnqueueUnlocked(q, entry);
     vlc_queue_Unlock(q);
 }
 
-void *vlc_queue_Dequeue(vlc_queue_t *q, ptrdiff_t offset)
+void *vlc_queue_Dequeue(vlc_queue_t *q)
 {
     void *entry;
 
@@ -139,7 +142,7 @@ void *vlc_queue_Dequeue(vlc_queue_t *q, ptrdiff_t offset)
     while (vlc_queue_IsEmpty(q))
         vlc_queue_Wait(q);
 
-    entry = vlc_queue_DequeueUnlocked(q, offset);
+    entry = vlc_queue_DequeueUnlocked(q);
     vlc_queue_Unlock(q);
 
     return entry;
-- 
2.26.0



More information about the vlc-devel mailing list