libtools
list.h
Go to the documentation of this file.
1 
20 #ifndef __TOOLS_LIST_H
21 #define __TOOLS_LIST_H
22 
23 #include <stddef.h>
24 
35 struct list {
37  struct list *prev;
38 
40  struct list *next;
41 };
42 
43 /*------------------------------------------------------------------------*/
44 
46 #define list_initializer(list) { .prev = list, .next = list, }
47 
48 /*------------------------------------------------------------------------*/
49 
51 #define list_init(list) \
52  do { \
53  (list)->prev = (list)->next = (list); \
54  } while (0)
55 
56 /*------------------------------------------------------------------------*/
57 
59 #define containerof(ptr, type, field) \
60  ((type*)((size_t)(ptr) - offsetof(type, field)))
61 
62 /*------------------------------------------------------------------------*/
63 
65 #define list_foreach(list, node, type, field) \
66  for((node) = containerof((list)->next, type, field); \
67  &(node)->field != (list); \
68  (node) = containerof((node)->field.next, type, field))
69 
70 /*------------------------------------------------------------------------*/
71 
81 #define list_foreach_safe(l, i, t, type, f) \
82  for ( \
83  (i) = containerof((l)->next, type, f), \
84  (t) = containerof((i)->f.next, type, f); \
85  &(i)->f != (l); \
86  i = (t), (t) = containerof((t)->f.next, type, f) \
87  )
88 
89 /*------------------------------------------------------------------------*/
90 
92 #define list_add_head(list, node) \
93  do { \
94  (node)->next = (list)->next; \
95  (node)->prev = (list); \
96  (list)->next->prev = (node); \
97  (list)->next = (node); \
98  } while(0); \
99 
100 /*------------------------------------------------------------------------*/
101 
103 #define list_add_tail(list, node) \
104  do { \
105  (node)->prev = (list)->prev; \
106  (node)->next = (list); \
107  (list)->prev->next = (node); \
108  (list)->prev = (node); \
109  } while(0); \
110 
111 /*------------------------------------------------------------------------*/
112 
114 #define list_del_node(node) \
115  do { \
116  (node)->next->prev = (node)->prev; \
117  (node)->prev->next = (node)->next; \
118  (node)->prev = (node)->next = NULL; \
119  } while(0);
120 
121 /*------------------------------------------------------------------------*/
122 
124 #define list_is_empty(list) \
125  ((list)->prev == (list) && (list) == (list)->next)
126 
129 #endif /* __TOOLS_LIST_H */
node of double-linked list
Definition: list.h:35
struct list * prev
pointer to previous node
Definition: list.h:37
struct list * next
pointer to next node
Definition: list.h:40