Change QUEUE_FOREACH macro to use while instead of for

This commit is contained in:
erw7
2019-10-16 16:23:07 +02:00
committed by Jan Edmund Lazo
parent 4c76b1e981
commit 36caafeb28
5 changed files with 26 additions and 22 deletions

View File

@@ -33,11 +33,17 @@ typedef struct _queue {
#define QUEUE_DATA(ptr, type, field) \
((type *)((char *)(ptr) - offsetof(type, field)))
// Important note: mutating the list while QUEUE_FOREACH is
// iterating over its elements results in undefined behavior.
#define QUEUE_FOREACH(q, h) \
for ( /* NOLINT(readability/braces) */ \
(q) = (h)->next; (q) != (h); (q) = (q)->next)
// Important note: the node currently being processed can be safely deleted.
// otherwise, mutating the list while QUEUE_FOREACH is iterating over its
// elements results in undefined behavior.
#define QUEUE_FOREACH(q, h, code) \
(q) = (h)->next; \
while((q) != (h)) { \
QUEUE *next = q->next; \
code \
(q) = next; \
}
// ffi.cdef is unable to swallow `bool` in place of `int` here.
static inline int QUEUE_EMPTY(const QUEUE *const q)