root / include / list.h @ master
History | View | Annotate | Download (3.8 kB)
1 |
/*
|
---|---|
2 |
* list.h
|
3 |
*
|
4 |
* This is a subset of linux's list.h intended to be used in user-space.
|
5 |
*
|
6 |
*/
|
7 |
|
8 |
#ifndef __LIST_H__
|
9 |
#define __LIST_H__
|
10 |
|
11 |
#include <stddef.h> |
12 |
|
13 |
#define containerof(_ptr, _type, _memb) \
|
14 |
((_type*)((void*)(_ptr) - offsetof(_type, _memb)))
|
15 |
|
16 |
#define LIST_POISON1 ((void *) 0x00100100) |
17 |
#define LIST_POISON2 ((void *) 0x00200200) |
18 |
|
19 |
struct list_head {
|
20 |
struct list_head *next, *prev;
|
21 |
}; |
22 |
|
23 |
#define LIST_HEAD_INIT(name) { &(name), &(name) }
|
24 |
|
25 |
#define LIST_HEAD(name) \
|
26 |
struct list_head name = LIST_HEAD_INIT(name)
|
27 |
|
28 |
static inline void INIT_LIST_HEAD(struct list_head *list) |
29 |
{ |
30 |
list->next = list; |
31 |
list->prev = list; |
32 |
} |
33 |
|
34 |
static inline void __list_add(struct list_head *new, |
35 |
struct list_head *prev,
|
36 |
struct list_head *next)
|
37 |
{ |
38 |
next->prev = new; |
39 |
new->next = next; |
40 |
new->prev = prev; |
41 |
prev->next = new; |
42 |
} |
43 |
|
44 |
static inline void list_add(struct list_head *new, struct list_head *head) |
45 |
{ |
46 |
__list_add(new, head, head->next); |
47 |
} |
48 |
|
49 |
static inline void list_add_tail(struct list_head *new, struct list_head *head) |
50 |
{ |
51 |
__list_add(new, head->prev, head); |
52 |
} |
53 |
|
54 |
static inline void __list_del(struct list_head * prev, struct list_head * next) |
55 |
{ |
56 |
next->prev = prev; |
57 |
prev->next = next; |
58 |
} |
59 |
|
60 |
static inline void list_del(struct list_head *entry) |
61 |
{ |
62 |
__list_del(entry->prev, entry->next); |
63 |
entry->next = (struct list_head *)LIST_POISON1;
|
64 |
entry->prev = (struct list_head *)LIST_POISON2;
|
65 |
} |
66 |
|
67 |
static inline void list_del_init(struct list_head *entry) |
68 |
{ |
69 |
__list_del(entry->prev, entry->next); |
70 |
INIT_LIST_HEAD(entry); |
71 |
} |
72 |
|
73 |
static inline void list_move(struct list_head *list, struct list_head *head) |
74 |
{ |
75 |
__list_del(list->prev, list->next); |
76 |
list_add(list, head); |
77 |
} |
78 |
|
79 |
static inline void list_move_tail(struct list_head *list, |
80 |
struct list_head *head)
|
81 |
{ |
82 |
__list_del(list->prev, list->next); |
83 |
list_add_tail(list, head); |
84 |
} |
85 |
|
86 |
static inline int list_empty(const struct list_head *head) |
87 |
{ |
88 |
return head->next == head;
|
89 |
} |
90 |
|
91 |
static inline int list_is_last(const struct list_head *list, |
92 |
const struct list_head *head) |
93 |
{ |
94 |
return list->next == head;
|
95 |
} |
96 |
|
97 |
static inline void __list_splice(const struct list_head *list, |
98 |
struct list_head *prev,
|
99 |
struct list_head *next)
|
100 |
{ |
101 |
struct list_head *first = list->next;
|
102 |
struct list_head *last = list->prev;
|
103 |
|
104 |
first->prev = prev; |
105 |
prev->next = first; |
106 |
|
107 |
last->next = next; |
108 |
next->prev = last; |
109 |
} |
110 |
|
111 |
static inline void list_splice(const struct list_head *list, |
112 |
struct list_head *head)
|
113 |
{ |
114 |
if (!list_empty(list))
|
115 |
__list_splice(list, head, head->next); |
116 |
} |
117 |
|
118 |
static inline void list_splice_tail(struct list_head *list, |
119 |
struct list_head *head)
|
120 |
{ |
121 |
if (!list_empty(list))
|
122 |
__list_splice(list, head->prev, head); |
123 |
} |
124 |
|
125 |
#define list_entry(_ptr, _type, _memb) containerof(_ptr, _type, _memb)
|
126 |
|
127 |
#define list_for_each_entry(pos, head, member) \
|
128 |
for (pos = list_entry((head)->next, typeof(*pos), member); \
|
129 |
&pos->member != (head); \ |
130 |
pos = list_entry(pos->member.next, typeof(*pos), member)) |
131 |
|
132 |
#define list_for_each_entry_safe(pos, n, head, member) \
|
133 |
for (pos = list_entry((head)->next, typeof(*pos), member), \
|
134 |
n = list_entry(pos->member.next, typeof(*pos), member); \ |
135 |
&pos->member != (head); \ |
136 |
pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
137 |
|
138 |
#define list_for_each_entry_reverse(pos, head, member) \
|
139 |
for (pos = list_entry((head)->prev, typeof(*pos), member); \
|
140 |
&pos->member != (head); \ |
141 |
pos = list_entry(pos->member.prev, typeof(*pos), member)) |
142 |
|
143 |
#define list_for_each_entry_continue(pos, head, member) \
|
144 |
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
|
145 |
&pos->member != (head); \ |
146 |
pos = list_entry(pos->member.next, typeof(*pos), member)) |
147 |
|
148 |
|
149 |
#endif /* __LIST_H__ */ |