robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1// Queue implemented by circularly-linked list. 2// 3// Adapted from libuv. Simpler and more efficient than klist.h for implementing 4// queues that support arbitrary insertion/removal. 5// 6// Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl> 7// 8// Permission to use, copy, modify, and/or distribute this software for any 9// purpose with or without fee is hereby granted, provided that the above 10// copyright notice and this permission notice appear in all copies. 11// 12// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20#pragma once 21 22#include <stddef.h> 23 24typedef struct queue { 25struct queue * next ; 26struct queue * prev ; 27}QUEUE ; 28 29#include "lib/queue_defs.h.inline.generated.h" 30 31// Public macros. 32#define QUEUE_DATA (ptr ,type ,field ) \ 33 ((type *)((char *)(ptr) - offsetof(type, field))) 34 35// Important note: the node currently being processed can be safely deleted. 36// otherwise, mutating the list while QUEUE_FOREACH is iterating over its 37// elements results in undefined behavior. 38#define QUEUE_FOREACH (q ,h ,code ) \ 39 (q) = (h)->next; \ 40 while ((q) != (h)) { \ 41 QUEUE *next = q->next; \ 42 code \ 43 (q) = next; \ 44 } 45 46// ffi.cdef is unable to swallow `bool` in place of `int` here. 47static inline int QUEUE_EMPTY (const QUEUE * const q ) 48FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 49{ 50return q == q -> next ; 51} 52 53#define QUEUE_HEAD (q ) (q)->next 54 55static inline void QUEUE_INIT (QUEUE * const q ) 56FUNC_ATTR_ALWAYS_INLINE 57{ 58q -> next = q ; 59q -> prev = q ; 60} 61 62static inline void QUEUE_ADD (QUEUE * const h ,QUEUE * const n ) 63FUNC_ATTR_ALWAYS_INLINE 64{ 65h -> prev -> next = n -> next ; 66n -> next -> prev = h -> prev ; 67h -> prev = n -> prev ; 68h -> prev -> next = h ; 69} 70 71static inline void QUEUE_INSERT_HEAD (QUEUE * const h ,QUEUE * const q ) 72FUNC_ATTR_ALWAYS_INLINE 73{ 74q -> next = h -> next ; 75q -> prev = h ; 76q -> next -> prev = q ; 77h -> next = q ; 78} 79 80static inline void QUEUE_INSERT_TAIL (QUEUE * const h ,QUEUE * const q ) 81FUNC_ATTR_ALWAYS_INLINE 82{ 83q -> next = h ; 84q -> prev = h -> prev ; 85q -> prev -> next = q ; 86h -> prev = q ; 87} 88 89static inline void QUEUE_REMOVE (QUEUE * const q ) 90FUNC_ATTR_ALWAYS_INLINE 91{ 92q -> prev -> next = q -> next ; 93q -> next -> prev = q -> prev ; 94}