]> mj.ucw.cz Git - netgrind.git/blob - lib/lists.h
TODO: A note on IPv6
[netgrind.git] / lib / lists.h
1 /*
2  *      Sherlock Library -- Linked Lists
3  *
4  *      (c) 2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_LISTS_H
11 #define _SHERLOCK_LISTS_H
12
13 typedef struct node {
14   struct node *next, *prev;
15 } node;
16
17 typedef struct list {
18   struct node head;
19 } list;
20
21 static inline void *list_head(list *l)
22 {
23   return (l->head.next != &l->head) ? l->head.next : NULL;
24 }
25
26 static inline void *list_tail(list *l)
27 {
28   return (l->head.prev != &l->head) ? l->head.prev : NULL;
29 }
30
31 static inline void *list_next(list *l, node *n)
32 {
33   return (n->next != &l->head) ? (void *) n->next : NULL;
34 }
35
36 static inline void *list_prev(list *l, node *n)
37 {
38   return (n->prev != &l->head) ? (void *) n->prev : NULL;
39 }
40
41 #define WALK_LIST(n,list) for(n=(void*)(list).head.next; (node*)(n) != &(list).head; n=(void*)((node*)(n))->next)
42
43 static inline void list_insert(node *what, node *after)
44 {
45   node *before = after->next;
46   what->next = before;
47   what->prev = before->prev;
48   before->prev = what;
49   after->next = what;
50 }
51
52 static inline void list_add_tail(list *l, node *n)
53 {
54   list_insert(n, l->head.prev);
55 }
56
57 static inline void list_add_head(list *l, node *n)
58 {
59   list_insert(n, &l->head);
60 }
61
62 static inline void list_remove(node *n)
63 {
64   node *before = n->prev;
65   node *after = n->next;
66   before->next = after;
67   after->prev = before;
68 }
69
70 static inline void list_init(list *l)
71 {
72   node *head = &l->head;
73   head->next = head->prev = head;
74 }
75
76 #endif