]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/list.h
update Tor to 0.3.5.8
[people/pmueller/ipfire-2.x.git] / src / misc-progs / list.h
1 /* list.h by Jan Bobrowski. Inspired by list.h from Linux */
2
3 #ifndef LIST_H
4 #define LIST_H
5
6 typedef struct list {
7 struct list *next, *prev;
8 } list_t;
9
10 static inline void list_link(struct list *a, struct list *b)
11 {
12 a->next = b;
13 b->prev = a;
14 }
15
16 static inline void list_add(struct list *head, struct list *item)
17 {
18 struct list *first = head->next;
19 list_link(head, item);
20 list_link(item, first);
21 }
22
23 static inline void list_add_end(struct list *head, struct list *item)
24 {
25 struct list *last = head->prev;
26 list_link(item, head);
27 list_link(last, item);
28 }
29
30 static inline list_t *list_del(struct list *item)
31 {
32 struct list *prev = item->prev, *next = item->next;
33 list_link(prev, next);
34 return next;
35 }
36
37 static inline void list_init(struct list *head)
38 {
39 list_link(head, head);
40 }
41
42 /* delete item from one list and add it to another */
43 static inline void list_del_add(list_t *head, list_t *item)
44 {
45 list_t *prev = item->prev, *next = item->next;
46 list_link(prev, next);
47 next = head->next;
48 list_link(head, item);
49 list_link(item, next);
50 }
51
52 /*static inline list_check(list_t *l)
53 {
54 list_t *a = l;
55 list_t *b;
56 do {
57 b = a->next;
58 assert(b->prev == a);
59 if(a==l) break;
60 a = b;
61 } while(1);
62 }*/
63
64 static inline void list_del_add_end(list_t *head, list_t *item)
65 {
66 list_t *prev = item->prev, *next = item->next;
67 list_link(prev, next);
68 prev = head->prev;
69 list_link(item, head);
70 item->prev = prev;
71 prev->next = item;
72 }
73
74 static inline void list_del_init(struct list *item)
75 {
76 struct list *prev = item->prev, *next = item->next;
77 list_link(item, item);
78 list_link(prev, next);
79 }
80
81 static inline void list_join(struct list *a, struct list *b)
82 {
83 list_t *ae = a->prev;
84 list_t *be = b->prev;
85 b->prev = ae;
86 a->prev = be;
87 ae->next = b;
88 be->next = a;
89 }
90
91 static inline int list_empty(struct list *head)
92 {
93 return head->next == head;
94 }
95
96 #define LIST(L) struct list L = {&L, &L}
97
98 #define list_entry(L, T, M) ((T*)((char*)(L) - (long)(&((T*)0)->M)))
99 #define list_item(L, T, M) ((T*)((char*)(L) - (long)(&((T*)0)->M)))
100
101 #define list_first(H, T, M) list_item((H)->next, T, M)
102 #define list_last(H, T, M) list_item((H)->prev, T, M)
103 #define list_next(O, M) list_item((O)->M.next, typeof(*(O)), M)
104
105 /* remove first element and return it */
106 static inline struct list *list_get(struct list *head)
107 {
108 struct list *item = head->next;
109 struct list *next = item->next;
110 list_link(head, next);
111 return item;
112 }
113
114 /* remove first element, initialize and return it */
115 static inline struct list *list_get_init(struct list *head)
116 {
117 struct list *item = head->next;
118 struct list *next = item->next;
119 list_link(item, item);
120 list_link(head, next);
121 return item;
122 }
123
124 #define list_get_entry(H, T, M) list_item(list_get((H)), T, M)
125 #define list_get_init_entry(H, T, M) list_item(list_get_init((H)), T, M)
126 #define list_get_item(H, T, M) list_item(list_get((H)), T, M)
127 #define list_get_init_item(H, T, M) list_item(list_get_init((H)), T, M)
128
129 #endif