2 * UCW Library -- Circular Linked Lists
4 * (c) 2003--2010 Martin Mares <mj@ucw.cz>
5 * (c) 2017 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
15 * Common header for list nodes.
17 typedef struct cnode {
18 struct cnode *next, *prev;
22 * Circular doubly linked list.
24 typedef struct clist {
29 * Initialize a new circular linked list. Must be called before any other function.
31 static inline void clist_init(clist *l)
33 cnode *head = &l->head;
34 head->next = head->prev = head;
38 * Return the first node on @l or NULL if @l is empty.
40 static inline void *clist_head(clist *l)
42 return (l->head.next != &l->head) ? l->head.next : NULL;
46 * Return the last node on @l or NULL if @l is empty.
48 static inline void *clist_tail(clist *l)
50 return (l->head.prev != &l->head) ? l->head.prev : NULL;
54 * Find the next node to @n or NULL if @n is the last one.
56 static inline void *clist_next(clist *l, cnode *n)
58 return (n->next != &l->head) ? (void *) n->next : NULL;
62 * Find the previous node to @n or NULL if @n is the first one.
64 static inline void *clist_prev(clist *l, cnode *n)
66 return (n->prev != &l->head) ? (void *) n->prev : NULL;
70 * Return a non-zero value iff @l is empty.
72 static inline int clist_empty(clist *l)
74 return (l->head.next == &l->head);
78 * Loop over all nodes in the @list and perform the next C statement on them. The current node is stored in @n which must be defined before as pointer to any type.
79 * The list should not be changed during this loop command.
81 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
84 * Same as @CLIST_WALK(), but allows removal of the current node. This macro requires one more variable to store some temporary pointers.
86 #define CLIST_WALK_DELSAFE(n,list,tmp) for(n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
89 * Same as @CLIST_WALK(), but it defines the variable for the current node in place. @type should be a pointer type.
91 #define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
94 * Same as @CLIST_WALK_DELSAFE(), but it defines the variable for the current node in place. @type should be a pointer type. The temporary variable must be still known before.
96 #define CLIST_FOR_EACH_DELSAFE(type,n,list,tmp) for(type n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
99 * Reversed version of @CLIST_FOR_EACH().
101 #define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev)
104 * Insert a new node just after the node @after. To insert at the head of the list, use @clist_add_head() instead.
106 static inline void clist_insert_after(cnode *what, cnode *after)
108 cnode *before = after->next;
116 * Insert a new node just before the node @before. To insert at the tail of the list, use @clist_add_tail() instead.
118 static inline void clist_insert_before(cnode *what, cnode *before)
120 cnode *after = before->prev;
128 * Insert a new node in front of all other nodes.
130 static inline void clist_add_head(clist *l, cnode *n)
132 clist_insert_after(n, &l->head);
136 * Insert a new node after all other nodes.
138 static inline void clist_add_tail(clist *l, cnode *n)
140 clist_insert_before(n, &l->head);
146 static inline void clist_remove(cnode *n)
148 cnode *before = n->prev;
149 cnode *after = n->next;
150 before->next = after;
151 after->prev = before;
155 * Remove the first node in @l, if it exists. Return the pointer to that node or NULL.
157 static inline void *clist_remove_head(clist *l)
159 cnode *n = clist_head(l);
166 * Remove the last node in @l, if it exists. Return the pointer to that node or NULL.
168 static inline void *clist_remove_tail(clist *l)
170 cnode *n = clist_tail(l);
177 * Merge two lists by inserting the list @what just after the node @after in a different list.
178 * The first list is then cleared.
180 static inline void clist_insert_list_after(clist *what, cnode *after)
182 if (!clist_empty(what))
184 cnode *w = &what->head;
185 w->prev->next = after->next;
186 after->next->prev = w->prev;
187 w->next->prev = after;
188 after->next = w->next;
194 * Merge two lists by inserting the list @what in front of all other nodes in a different list @l.
195 * The first list is then cleared.
197 static inline void clist_add_list_head(clist *l, clist *what)
199 clist_insert_list_after(what, &l->head);
203 * Merge two lists by inserting the list @what after all other nodes in a different list @l.
204 * The first list is then cleared.
206 static inline void clist_add_list_tail(clist *l, clist *what)
208 clist_insert_list_after(what, l->head.prev);
212 * Move all items from a source list to a destination list. The source list
213 * becomes empty, the original contents of the destination list are destroyed.
215 static inline void clist_move(clist *to, clist *from)
218 clist_insert_list_after(from, &to->head);
223 * Compute the number of nodes in @l. Beware of linear time complexity.
225 static inline uint clist_size(clist *l)
228 CLIST_FOR_EACH(cnode *, n, *l)
234 * Remove a node @n and mark it as unlinked by setting the previous and next pointers to NULL.
236 static inline void clist_unlink(cnode *n)
239 n->prev = n->next = NULL;
243 * Remove the first node on @l and mark it as unlinked.
244 * Return the pointer to that node or NULL.
246 static inline void *clist_unlink_head(clist *l)
248 cnode *n = clist_head(l);
255 * Remove the last node on @l and mark it as unlinked.
256 * Return the pointer to that node or NULL.
258 static inline void *clist_unlink_tail(clist *l)
260 cnode *n = clist_tail(l);
267 * Check if a node is linked a list. Unlinked nodes are recognized by having their
268 * previous and next pointers equal to NULL. Returns 0 or 1.
270 * Nodes initialized to all zeroes are unlinked, inserting a node anywhere in a list
271 * makes it linked. Normal removal functions like @clist_remove() do not mark nodes
272 * as unlinked, you need to call @clist_unlink() instead.
274 static inline int clist_is_linked(cnode *n)