2 * UCW Library -- Single-Linked Lists
4 * (c) 2005 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.
13 #ifdef CONFIG_UCW_CLEAN_ABI
14 #define slist_insert_before ucw_slist_insert_before
15 #define slist_prev ucw_slist_prev
16 #define slist_remove ucw_slist_remove
20 * Common header for list nodes.
22 typedef struct snode {
29 typedef struct slist {
30 struct snode head, *last;
34 * Initialize a new single-linked list. Must be called before any other function.
36 static inline void slist_init(slist *l)
38 l->head.next = l->last = NULL;
42 * Return the first node of @l or NULL if @l is empty.
44 static inline void *slist_head(slist *l)
50 * Return the last node of @l or NULL if @l is empty.
52 static inline void *slist_tail(slist *l)
58 * Find the next node to @n or NULL if @n is the last one.
60 static inline void *slist_next(snode *n)
66 * Return a non-zero value iff @l is empty.
68 static inline int slist_empty(slist *l)
74 * Insert a new node in front of all other nodes.
76 static inline void slist_add_head(slist *l, snode *n)
78 n->next = l->head.next;
85 * Insert a new node after all other nodes.
87 static inline void slist_add_tail(slist *l, snode *n)
98 * Insert a new node just after the node @after. To insert a new head, use @slist_add_head() instead.
100 static inline void slist_insert_after(slist *l, snode *what, snode *after)
102 what->next = after->next;
109 * Quickly remove the node next to @after. The node may not exist.
111 static inline void slist_remove_after(slist *l, snode *after)
113 snode *n = after->next;
116 after->next = n->next;
118 l->last = (after == &l->head) ? NULL : after;
123 * Remove the first node in @l. The list can be empty.
125 static inline void *slist_remove_head(slist *l)
127 snode *n = slist_head(l);
129 slist_remove_after(l, &l->head);
136 * 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.
137 * The list should not be changed during this loop command.
139 #define SLIST_WALK(n,list) for(n=(void*)(list).head.next; (n); (n)=(void*)((snode*)(n))->next)
142 * Same as @SLIST_WALK(), but allows removal of the current node. This macro requires one more variable to store the pointer to the previous node (useful for @slist_remove_after()).
144 #define SLIST_WALK_DELSAFE(n,list,prev) for((prev)=(void*)&(list).head; (n)=(void*)((snode*)prev)->next; (prev)=(((snode*)(prev))->next==(snode*)(n) ? (void*)(n) : (void*)(prev)))
147 * Same as @SLIST_WALK(), but it defines the variable for the current node in place. @type should be a pointer type.
149 #define SLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; n; n=(void*)((snode*)(n))->next)
151 /* Non-trivial functions */
154 * Find the previous node to @n or NULL if @n is the first one. Beware linear time complexity.
156 void *slist_prev(slist *l, snode *n);
159 * Insert a new node just before the node @before. To insert a new tail, use @slist_add_tail(). Beware linear time complexity.
161 void slist_insert_before(slist *l, snode *what, snode *before);
164 * Remove node @n. Beware linear time complexity.
166 void slist_remove(slist *l, snode *n);
169 * Remove the last node in @l. The list can be empty.
171 static inline void slist_remove_tail(slist *l)
173 slist_remove(l, l->last);
177 * Compute the number of nodes in @l. Beware linear time complexity.
179 static inline uint slist_size(slist *l)
182 SLIST_FOR_EACH(snode *, n, *l)