28 #define my_offsetof(st, m) ((size_t)( (unsigned char *)&((st)->m ) - (unsigned char *)(st)))
31 #define list(type) type *
39 #define new_list(type) \
42 __lmptr = malloc(sizeof(struct list)); \
43 memset(__lmptr, 0, sizeof(struct list));\
48 #define list_sizeof(list) ((struct list *)list)->size
55 #define list_head(li) ((__typeof__ (li))(((struct list *)(li))->head))
62 #define list_tail(li) ((__typeof__ (li))(((struct list *)(li))->tail))
69 #define list_next(ptr) ((ptr)->next)
76 #define list_prev(ptr) ((ptr)->prev)
79 #define get_key(data) ({\
80 char *__key_ptr = ((char *)(data) + __key_position);\
81 double *__key_double_ptr = (double *)__key_ptr;\
91 #define list_empty(list) (((struct list *)list)->size == 0)
93 #define list_insert_tail(li, data) \
95 __typeof__(data) __new_n = (data); \
97 __new_n->next = NULL;\
98 __new_n->prev = NULL;\
100 __l = (struct list *)(li);\
102 if(__l->size == 0) { \
103 __l->head = __new_n;\
104 __l->tail = __new_n;\
107 __new_n->next = NULL; \
108 __new_n->prev = __l->tail;\
109 ((__typeof__(data))(__l->tail))->next = __new_n;\
110 __l->tail = __new_n;\
115 #define list_insert_head(li, data) \
117 __typeof__(data) __new_n = (data); \
119 __new_n->next = NULL;\
120 __new_n->prev = NULL;\
121 __l = (struct list *)(li);\
123 if(__l->size == 0) { \
124 __l->head = __new_n;\
125 __l->tail = __new_n;\
127 __new_n->prev = NULL; \
128 __new_n->next = __l->head;\
129 ((__typeof(data))__l->head)->prev = __new_n;\
130 __l->head = __new_n;\
136 #define list_insert(li, key_name, data)\
138 __typeof__(data) __n; \
139 __typeof__(data) __new_n = (data);\
140 size_t __key_position = my_offsetof((li), key_name);\
142 size_t __size_before;\
145 __l = (struct list *)(li);\
147 __size_before = __l->size;\
148 if(__l->size == 0) { \
149 __new_n->prev = NULL;\
150 __new_n->next = NULL;\
151 __l->head = __new_n;\
152 __l->tail = __new_n;\
155 __key = get_key(__new_n); \
158 while(__n != NULL && __key < get_key(__n)) {\
162 if(__n == __l->tail) { \
163 __new_n->next = NULL;\
164 ((__typeof(data))__l->tail)->next = __new_n;\
165 __new_n->prev = __l->tail;\
166 __l->tail = __new_n;\
167 } else if(__n == NULL) { \
168 __new_n->prev = NULL;\
169 __new_n->next = __l->head;\
170 ((__typeof(data))__l->head)->prev = __new_n;\
171 __l->head = __new_n;\
173 __new_n->prev = __n;\
174 __new_n->next = __n->next;\
175 __n->next->prev = __new_n;\
176 __n->next = __new_n;\
180 assert(__l->size == (__size_before + 1));\
183 #define list_detach_by_content(li, node) \
185 __typeof__(node) __n = (node); \
187 __l = (struct list *)(li); \
190 if(__l->head == __n) { \
191 __l->head = __n->next; \
192 if(__l->head != NULL) { \
193 ((__typeof(node))__l->head)->prev = NULL; \
196 if(__l->tail == __n) {\
197 __l->tail = __n->prev;\
198 if(__l->tail != NULL) {\
199 ((__typeof(node))__l->tail)->next = NULL;\
202 if(__n->next != NULL) {\
203 __n->next->prev = __n->prev;\
205 if(__n->prev != NULL) {\
206 __n->prev->next = __n->next;\
208 __n->next = (void *)0xBEEFC0DE;\
209 __n->prev = (void *)0xDEADC0DE;\
213 #define list_pop(list)\
216 size_t __size_before;\
217 __typeof__ (list) __n;\
218 __typeof__ (list) __n_next;\
219 __l = (struct list *)(list);\
221 __size_before = __l->size;\
224 __l->head = __n->next;\
225 if(__n->next != NULL) {\
226 __n->next->prev = NULL;\
228 __n_next = __n->next;\
229 __n->next = (void *)0xDEFEC8ED;\
230 __n->prev = (void *)0xDEFEC8ED;\
233 assert(__l->size == (__size_before - 1));\
238 #define list_trunc(list, key_name, key_value, release_fn) \
240 struct list *__l = (struct list *)(list);\
241 __typeof__(list) __n;\
242 __typeof__(list) __n_adjacent;\
243 unsigned int __deleted = 0;\
244 size_t __key_position = my_offsetof((list), key_name);\
246 size_t __size_before = __l->size;\
250 while(__n != NULL && get_key(__n) < (key_value)) {\
252 __n_adjacent = __n->next;\
253 __n->next = (void *)0xBAADF00D;\
254 __n->prev = (void *)0xBAADF00D;\
259 if(__l->head != NULL)\
260 ((__typeof__(list))__l->head)->prev = NULL;\
262 __l->size -= __deleted;\
263 assert(__l->size == (__size_before - __deleted));\
267 #define list_size(li) ((struct list *)(li))->size
This structure defines a generic list. Nodes of the list must have a next/prev pointer properly typed...
Definition: list.h:18
void * head
pointer to the first element
Definition: list.h:22
void * tail
pointer to the last element
Definition: list.h:24
size_t size
the size of the list
Definition: list.h:20