2 * Sherlock Library -- Circular Linked Lists
4 * (c) 2003 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.
10 #ifndef _SHERLOCK_CLISTS_H
11 #define _SHERLOCK_CLISTS_H
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 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
43 static inline void clist_insert(cnode *what, cnode *after)
45 cnode *before = after->next;
47 what->prev = before->prev;
52 static inline void clist_add_tail(clist *l, cnode *n)
54 clist_insert(n, l->head.prev);
57 static inline void clist_add_head(clist *l, cnode *n)
59 clist_insert(n, &l->head);
62 static inline void clist_remove(cnode *n)
64 cnode *before = n->prev;
65 cnode *after = n->next;
70 static inline void clist_init(clist *l)
72 cnode *head = &l->head;
73 head->next = head->prev = head;