Statistics
| Branch: | Revision:

iof-bird-daemon / lib / lists.h @ f4a60a9b

History | View | Annotate | Download (2.26 KB)

1
/*
2
 *        BIRD Library -- Linked Lists
3
 *
4
 *        (c) 1998 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#ifndef _BIRD_LISTS_H_
10
#define _BIRD_LISTS_H_
11

    
12
/*
13
 * I admit the list structure is very tricky and also somewhat awkward,
14
 * but it's both efficient and easy to manipulate once one understands the
15
 * basic trick: The list head always contains two synthetic nodes which are
16
 * always present in the list: the head and the tail. But as the `next'
17
 * entry of the tail and the `prev' entry of the head are both NULL, the
18
 * nodes can overlap each other:
19
 *
20
 *     head    head_node.next
21
 *     null    head_node.prev  tail_node.next
22
 *     tail                    tail_node.prev
23
 */
24

    
25
typedef struct node {
26
  struct node *next, *prev;
27
} node;
28

    
29
typedef struct list {                        /* In fact two overlayed nodes */
30
  struct node *head, *null, *tail;
31
} list;
32

    
33
#define NODE (node *)
34
#define HEAD(list) ((void *)((list).head))
35
#define TAIL(list) ((void *)((list).tail))
36
#define NODE_NEXT(n) ((void *)((NODE (n))->next))
37
#define NODE_VALID(n) ((NODE (n))->next)
38
#define WALK_LIST(n,list) for(n=HEAD(list); NODE_VALID(n); n=NODE_NEXT(n))
39
#define WALK_LIST2(n,nn,list,pos) \
40
  for(nn=(list).head; NODE_VALID(nn) && (n=SKIP_BACK(typeof(*n),pos,nn)); nn=nn->next)
41
#define WALK_LIST_DELSAFE(n,nxt,list) \
42
  for(n=HEAD(list); nxt=NODE_NEXT(n); n=(void *) nxt)
43
#define WALK_LIST2_DELSAFE(n,nn,nxt,list,pos) \
44
  for(nn=HEAD(list); (nxt=nn->next) && (n=SKIP_BACK(typeof(*n),pos,nn)); nn=nxt)
45

    
46
/* WALK_LIST_FIRST supposes that called code removes each processed node */
47
#define WALK_LIST_FIRST(n,list) \
48
     while(n=HEAD(list), (NODE (n))->next)
49
#define WALK_LIST_BACKWARDS(n,list) for(n=TAIL(list);(NODE (n))->prev; \
50
                                n=(void *)((NODE (n))->prev))
51
#define WALK_LIST_BACKWARDS_DELSAFE(n,prv,list) \
52
     for(n=TAIL(list); prv=(void *)((NODE (n))->prev); n=(void *) prv)
53

    
54
#define EMPTY_LIST(list) (!(list).head->next)
55

    
56

    
57
#ifndef _BIRD_LISTS_C_
58
#define LIST_INLINE static inline
59
#include "lib/lists.c"
60
#undef LIST_INLINE
61

    
62
#else /* _BIRD_LISTS_C_ */
63
#define LIST_INLINE
64
void add_tail(list *, node *);
65
void add_head(list *, node *);
66
void rem_node(node *);
67
void rem2_node(node *);
68
void add_tail_list(list *, list *);
69
void init_list(list *);
70
void insert_node(node *, node *);
71
#endif
72

    
73
#endif