import cbaos v0.1
[cbaos.git] / include / list.h
blob7338e514517a3e368fe727db3cf497e1dc322dc9
1 #ifndef _LIST_H_
2 #define _LIST_H_
4 #include "magic.h"
6 /* ideas for this picked from linux */
8 /* if you have an apparent problem with lists, check you didn't add the same
9 * member twice */
11 #define DEFINE_LIST(name) \
12 struct list name = { \
13 .next = &name, \
14 .prev = &name, \
17 struct list {
18 struct list *next;
19 struct list *prev;
22 static inline void list_init(struct list *list)
24 list->next = list;
25 list->prev = list;
28 static inline void list_add(struct list *list, struct list *knew)
30 knew->next = list->next;
31 knew->prev = list;
32 list->next->prev = knew;
33 list->next = knew;
36 static inline void list_add_tail(struct list *list, struct list *knew)
38 knew->next = list;
39 knew->prev = list->prev;
40 list->prev->next = knew;
41 list->prev = knew;
44 static inline void list_del(struct list *todel)
46 todel->prev->next = todel->next;
47 todel->next->prev = todel->prev;
48 todel->prev = todel->next = (struct list *)MAGIC_LIST_INVALID;
51 static inline int list_empty(struct list *list)
53 return list == list->next;
56 #define list_entry(list, type, member) \
57 ({ (type*)((char*)list-(char*)&(((type*)0)->member)); })
59 #define list_for_each(list, iterator) \
60 for (iterator=(list)->next; iterator!=list; iterator=iterator->next)
62 #define list_for_each_safe(list, iterator, saved) \
63 for (iterator=(list)->next, saved=iterator->next; iterator!=list; iterator=saved, saved=iterator->next)
65 #endif