1 #ifndef __LIST_H
2 #define __LIST_H
3 
4 #include <stdbool.h>
5 
6 struct list_head {
7   struct list_head *next, *prev;
8 };
9 
10 #define LIST_HEAD_INIT(name) { &(name), &(name) }
11 
12 #define LIST_HEAD(name) \
13   struct list_head name = LIST_HEAD_INIT(name)
14 
INIT_LIST_HEAD(struct list_head * list)15 static inline void INIT_LIST_HEAD(struct list_head *list)
16 {
17   list->next = list;
18   list->prev = list;
19 }
20 
21 static inline void
list_add(struct list_head * entry,struct list_head * head)22 list_add (struct list_head *entry, struct list_head *head)
23 {
24   struct list_head *next = head->next;
25   entry->prev = head;
26   entry->next = next;
27   next->prev = head->next = entry;
28 }
29 
30 static inline void
list_del(struct list_head * entry)31 list_del (struct list_head *entry)
32 {
33   struct list_head *next = entry->next;
34   struct list_head *prev = entry->prev;
35   next->prev = prev;
36   prev->next = next;
37 }
38 
39 static inline void
list_del_init(struct list_head * entry)40 list_del_init (struct list_head *entry)
41 {
42   list_del(entry);
43   INIT_LIST_HEAD(entry);
44 }
45 
46 static inline bool
list_empty(const struct list_head * head)47 list_empty (const struct list_head *head)
48 {
49   return head->next == head;
50 }
51 
52 #define list_entry(ptr, type, member) \
53   (type *)( (char *)(ptr) - offsetof(type, member) )
54 
55 #endif  /* __LIST_H */
56