]> mj.ucw.cz Git - netgrind.git/blob - lib/lists.h
2f7cea10d6f6d45754fd9567e4961532ca2bea0c
[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_first(list *l)
22 {
23   return (l->head.next != &l->head) ? l->head.next : NULL;
24 }
25
26 static inline void *list_last(list *l)
27 {
28   return (l->head.prev != &l->head) ? l->head.prev : NULL;
29 }
30
31 #define WALK_LIST(n,list) for(n=(void*)(list).head; ((node*)(n))->next != &(list).head; n=(void*)((node*)(n))->next)
32
33 static inline void list_insert(node *what, node *after)
34 {
35   node *before = after->next;
36   what->next = before;
37   what->prev = before->prev;
38   before->prev = what;
39   after->next = what;
40 }
41
42 static inline void list_add_tail(list *l, node *n)
43 {
44   list_insert(n, l->head.prev);
45 }
46
47 static inline void list_add_head(list *l, node *n)
48 {
49   list_insert(n, &l->head);
50 }
51
52 static inline void list_remove(node *n)
53 {
54   node *before = n->prev;
55   node *after = n->next;
56   before->next = after;
57   after->prev = before;
58 }
59
60 static inline void list_init(list *l)
61 {
62   node *head = &l->head;
63   head->next = head->prev = head;
64 }
65
66 #endif