-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.h
91 lines (74 loc) · 2.06 KB
/
list.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef __LIST_H
#define __LIST_H
#include <stddef.h>
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#undef offsetof
#define offsetof(s, m) (unsigned long)&(((s *) 0)->m)
#define containerof(ptr, type, member) \
((type *)((unsigned long)(ptr) - offsetof(type, member)))
struct list_node {
struct list_node *prev;
struct list_node *next;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_node name = LIST_HEAD_INIT(name)
static inline void list_initialize(struct list_node *list)
{
list->prev = list->next = list;
}
static inline void list_add_head(struct list_node *list, struct list_node *item)
{
item->next = list->next;
item->prev = list;
list->next->prev = item;
list->next = item;
}
static inline void list_add_tail(struct list_node *list, struct list_node *item)
{
item->prev = list->prev;
item->next = list;
list->prev->next = item;
list->prev = item;
}
static inline void list_delete(struct list_node *item)
{
item->next->prev = item->prev;
item->prev->next = item->next;
item->prev = item->next = 0;
}
static inline struct list_node *list_next(struct list_node *list, struct list_node *item)
{
if (item->next != list)
return item->next;
else
return (struct list_node *)0;
}
/* iterates over the list, node should be struct list_node* */
#define list_for_every(list, node) \
for(node = (list)->next; node != (list); node = node->next)
/* iterates over the list, entry should be the container structure type* */
#define list_for_each_entry(list, entry, type, member) \
for((entry) = containerof((list)->next, type, member);\
&(entry)->member != (list);\
(entry) = containerof((entry)->member.next, type, member))
static inline int list_is_empty(struct list_node *list)
{
return (list->next == list) ? 1 : 0;
}
static inline size_t list_length(struct list_node *list)
{
size_t cnt = 0;
struct list_node *node = list;
list_for_every(list, node)
{
cnt++;
}
return cnt;
}
#endif