2 * UCW Library -- Circular Linked Lists
4 * (c) 2003--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 typedef struct cnode {
14 struct cnode *next, *prev;
17 typedef struct clist {
21 static inline void *clist_head(clist *l)
23 return (l->head.next != &l->head) ? l->head.next : NULL;
26 static inline void *clist_tail(clist *l)
28 return (l->head.prev != &l->head) ? l->head.prev : NULL;
31 static inline void *clist_next(clist *l, cnode *n)
33 return (n->next != &l->head) ? (void *) n->next : NULL;
36 static inline void *clist_prev(clist *l, cnode *n)
38 return (n->prev != &l->head) ? (void *) n->prev : NULL;
41 static inline int clist_empty(clist *l)
43 return (l->head.next == &l->head);
46 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
47 #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)
48 #define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
49 #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)
51 #define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev)
53 static inline void clist_insert_after(cnode *what, cnode *after)
55 cnode *before = after->next;
62 static inline void clist_insert_before(cnode *what, cnode *before)
64 cnode *after = before->prev;
71 static inline void clist_add_tail(clist *l, cnode *n)
73 clist_insert_before(n, &l->head);
76 static inline void clist_add_head(clist *l, cnode *n)
78 clist_insert_after(n, &l->head);
81 static inline void clist_remove(cnode *n)
83 cnode *before = n->prev;
84 cnode *after = n->next;
89 static inline void clist_init(clist *l)
91 cnode *head = &l->head;
92 head->next = head->prev = head;
95 static inline void clist_insert_list_after(clist *what, cnode *after)
97 if (!clist_empty(what))
99 cnode *w = &what->head;
100 w->prev->next = after->next;
101 after->next->prev = w->prev;
102 w->next->prev = after;
103 after->next = w->next;