2 * UCW Library -- Circular Linked Lists
4 * (c) 2003--2010 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 * Common header for list nodes.
16 typedef struct cnode {
17 struct cnode *next, *prev;
21 * Circular doubly linked list.
23 typedef struct clist {
28 * Initialize a new circular linked list. Must be called before any other function.
30 static inline void clist_init(clist *l)
32 cnode *head = &l->head;
33 head->next = head->prev = head;
37 * Return the first node on @l or NULL if @l is empty.
39 static inline void *clist_head(clist *l)
41 return (l->head.next != &l->head) ? l->head.next : NULL;
45 * Return the last node on @l or NULL if @l is empty.
47 static inline void *clist_tail(clist *l)
49 return (l->head.prev != &l->head) ? l->head.prev : NULL;
53 * Find the next node to @n or NULL if @n is the last one.
55 static inline void *clist_next(clist *l, cnode *n)
57 return (n->next != &l->head) ? (void *) n->next : NULL;
61 * Find the previous node to @n or NULL if @n is the first one.
63 static inline void *clist_prev(clist *l, cnode *n)
65 return (n->prev != &l->head) ? (void *) n->prev : NULL;
69 * Return a non-zero value iff @l is empty.
71 static inline int clist_empty(clist *l)
73 return (l->head.next == &l->head);
77 * 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.
78 * The list should not be changed during this loop command.
80 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
83 * Same as @CLIST_WALK(), but allows removal of the current node. This macro requires one more variable to store some temporary pointers.
85 #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)
88 * Same as @CLIST_WALK(), but it defines the variable for the current node in place. @type should be a pointer type.
90 #define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
93 * 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.
95 #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)
98 * Reversed version of @CLIST_FOR_EACH().
100 #define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev)
103 * Insert a new node just after the node @after. To insert at the head of the list, use @clist_add_head() instead.
105 static inline void clist_insert_after(cnode *what, cnode *after)
107 cnode *before = after->next;
115 * Insert a new node just before the node @before. To insert at the tail of the list, use @clist_add_tail() instead.
117 static inline void clist_insert_before(cnode *what, cnode *before)
119 cnode *after = before->prev;
127 * Insert a new node in front of all other nodes.
129 static inline void clist_add_head(clist *l, cnode *n)
131 clist_insert_after(n, &l->head);
135 * Insert a new node after all other nodes.
137 static inline void clist_add_tail(clist *l, cnode *n)
139 clist_insert_before(n, &l->head);
145 static inline void clist_remove(cnode *n)
147 cnode *before = n->prev;
148 cnode *after = n->next;
149 before->next = after;
150 after->prev = before;
154 * Remove the first node in @l, if it exists. Return the pointer to that node or NULL.
156 static inline void *clist_remove_head(clist *l)
158 cnode *n = clist_head(l);
165 * Remove the last node in @l, if it exists. Return the pointer to that node or NULL.
167 static inline void *clist_remove_tail(clist *l)
169 cnode *n = clist_tail(l);
176 * Merge two lists by inserting the list @what just after the node @after in a different list.
177 * The first list is then cleared.
179 static inline void clist_insert_list_after(clist *what, cnode *after)
181 if (!clist_empty(what))
183 cnode *w = &what->head;
184 w->prev->next = after->next;
185 after->next->prev = w->prev;
186 w->next->prev = after;
187 after->next = w->next;
193 * Move all items from a source list to a destination list. The source list
194 * becomes empty, the original contents of the destination list are destroyed.
196 static inline void clist_move(clist *to, clist *from)
199 clist_insert_list_after(from, &to->head);
204 * Compute the number of nodes in @l. Beware of linear time complexity.
206 static inline uns clist_size(clist *l)
209 CLIST_FOR_EACH(cnode *, n, *l)
215 * Remove a node @n and mark it as unlinked by setting the previous and next pointers to NULL.
217 static inline void clist_unlink(cnode *n)
220 n->prev = n->next = NULL;
224 * Remove the first node on @l and mark it as unlinked.
225 * Return the pointer to that node or NULL.
227 static inline void *clist_unlink_head(clist *l)
229 cnode *n = clist_head(l);
236 * Remove the last node on @l and mark it as unlinked.
237 * Return the pointer to that node or NULL.
239 static inline void *clist_unlink_tail(clist *l)
241 cnode *n = clist_tail(l);
248 * Check if a node is linked a list. Unlinked nodes are recognized by having their
249 * previous and next pointers equal to NULL. Returns 0 or 1.
251 * Nodes initialized to all zeroes are unlinked, inserting a node anywhere in a list
252 * makes it linked. Normal removal functions like @clist_remove() do not mark nodes
253 * as unlinked, you need to call @clist_unlink() instead.
255 static inline int clist_is_linked(cnode *n)