-
Notifications
You must be signed in to change notification settings - Fork 7
/
slist.h
50 lines (40 loc) · 1.29 KB
/
slist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef _SLIST_H_
#define _SLIST_H_
#define slist_init(list) do { \
(list)->head = NULL; \
(list)->tail = &(list)->head; \
} while(0)
#define SLIST_DECLARE(type, list) struct { type *head, **tail; } list
#define SLIST_STACK_DECLARE(type, list) struct { type *head; } list
#define SLIST_INITIALIZER(list) { .head = NULL, .tail = &(list)->head }
#define SLIST_STACK_INITIALIZER(list) { .head = NULL }
#define SLIST_DEFINE(type, list) \
SLIST_DECLARE(type, list) = SLIST_INITIALIZER(&list)
#define SLIST_STACK_DEFINE(type, list) \
SLIST_STACK_DECLARE(type, list) = SLIST_STACK_INITIALIZER(&list)
#define slist_empty(list) (!(list)->head)
#define slist_append(list, i) do { \
typeof(*(list)->head) **__tail = (list)->tail; \
(i)->next = NULL; \
(list)->tail = &(i)->next; \
*__tail = i; \
} while(0)
#define slist_push(list, i) do { \
(i)->next = (list)->head; \
(list)->head = (i); \
} while(0)
#define slist_pop(list) ({ \
typeof(*(list)->head) *__head = (list)->head; \
(list)->head = __head->next; \
if (!__head->next) \
(list)->tail = &(list)->head; \
__head; \
})
#define slist_stack_pop(list) ({ \
typeof(*(list)->head) *__head = (list)->head; \
(list)->head = __head->next; \
__head; \
})
#define slist_for_each(i, list) \
for (i = (list)->head; i; i = i->next)
#endif /* _SLIST_H_ */