2 * Sherlock Library -- Linked Lists
4 * (c) 1997--1999 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_LISTS_H
11 #define _SHERLOCK_LISTS_H
14 * I admit the list structure is very tricky and also somewhat awkward,
15 * but it's both efficient and easy to manipulate once one understands the
16 * basic trick: The list head always contains two synthetic nodes which are
17 * always present in the list: the head and the tail. But as the `next'
18 * entry of the tail and the `prev' entry of the head are both NULL, the
19 * nodes can overlap each other:
22 * null head_node.prev tail_node.next
27 struct node *next, *prev;
30 typedef struct list { /* In fact two overlayed nodes */
31 struct node *head, *null, *tail;
35 #define HEAD(list) ((void *)((list).head))
36 #define TAIL(list) ((void *)((list).tail))
37 #define WALK_LIST(n,list) for(n=HEAD(list);(NODE (n))->next; \
38 n=(void *)((NODE (n))->next))
39 #define DO_FOR_ALL(n,list) WALK_LIST(n,list)
40 #define WALK_LIST_DELSAFE(n,nxt,list) \
41 for(n=HEAD(list); nxt=(void *)((NODE (n))->next); n=(void *) nxt)
42 #define WALK_LIST_BACKWARDS(n,list) for(n=TAIL(list);(NODE (n))->prev; \
43 n=(void *)((NODE (n))->prev))
44 #define WALK_LIST_BACKWARDS_DELSAFE(n,prv,list) \
45 for(n=TAIL(list); prv=(void *)((NODE (n))->prev); n=(void *) prv)
47 #define EMPTY_LIST(list) (!(list).head->next)
49 void add_tail(list *, node *);
50 void add_head(list *, node *);
51 void rem_node(node *);
52 void add_tail_list(list *, list *);
53 void init_list(list *);
54 void insert_node(node *, node *);
56 #if !defined(_SHERLOCK_LISTS_C) && defined(__GNUC__)
57 #define LIST_INLINE extern inline
58 #include "lib/lists.c"