Skip to content

Commit 11f2b50

Browse files
committed
Update list.h
1 parent 50af9eb commit 11f2b50

File tree

1 file changed

+87
-78
lines changed

1 file changed

+87
-78
lines changed

list.h

Lines changed: 87 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _LINUX_LIST_H
33

44
/*
5-
* Simple doubly linked list implementation.
5+
* Circular doubly linked list implementation.
66
*
77
* Some of the internal functions ("__xxx") are useful when
88
* manipulating whole lists rather than single entries, as
@@ -20,6 +20,13 @@ struct list_head {
2020
#define LIST_HEAD(name) \
2121
struct list_head name = LIST_HEAD_INIT(name)
2222

23+
/**
24+
* INIT_LIST_HEAD - Initialize a list_head structure
25+
* @list: list_head structure to be initialized.
26+
*
27+
* Initializes the list_head to point to itself. If it is a list header,
28+
* the result is an empty list.
29+
*/
2330
static inline void INIT_LIST_HEAD(struct list_head *list)
2431
{
2532
list->next = list;
@@ -32,41 +39,41 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
3239
* This is only for internal list manipulation where we know
3340
* the prev/next entries already!
3441
*/
35-
static inline void __list_add(struct list_head *node,
36-
struct list_head *prev,
37-
struct list_head *next)
42+
static inline void __list_add(struct list_head *entry,
43+
struct list_head *prev,
44+
struct list_head *next)
3845
{
39-
next->prev = node;
40-
node->next = next;
41-
node->prev = prev;
42-
prev->next = node;
46+
next->prev = entry;
47+
entry->next = next;
48+
entry->prev = prev;
49+
prev->next = entry;
4350
}
4451

4552
/**
4653
* list_add - add a new entry
47-
* @new: new entry to be added
54+
* @entry: new entry to be added
4855
* @head: list head to add it after
4956
*
5057
* Insert a new entry after the specified head.
5158
* This is good for implementing stacks.
5259
*/
53-
static inline void list_add(struct list_head *node, struct list_head *head)
60+
static inline void list_add(struct list_head *entry, struct list_head *head)
5461
{
55-
__list_add(node, head, head->next);
62+
__list_add(entry, head, head->next);
5663
}
5764

5865
/**
5966
* list_add_tail - add a new entry
60-
* @new: new entry to be added
67+
* @entry: new entry to be added
6168
* @head: list head to add it before
6269
*
6370
* Insert a new entry before the specified head.
6471
* This is useful for implementing queues.
6572
*/
66-
static inline void list_add_tail(struct list_head *node,
67-
struct list_head *head)
73+
static inline void list_add_tail(struct list_head *entry,
74+
struct list_head *head)
6875
{
69-
__list_add(node, head->prev, head);
76+
__list_add(entry, head->prev, head);
7077
}
7178

7279
/*
@@ -85,7 +92,8 @@ static inline void __list_del(struct list_head *prev, struct list_head *next)
8592
/**
8693
* list_del - deletes entry from list.
8794
* @entry: the element to delete from the list.
88-
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
95+
* Note: list_empty() on entry does not return true after this, the entry is
96+
* in an undefined state.
8997
*/
9098
static inline void list_del(struct list_head *entry)
9199
{
@@ -94,25 +102,25 @@ static inline void list_del(struct list_head *entry)
94102

95103
/**
96104
* list_move - delete from one list and add as another's head
97-
* @list: the entry to move
105+
* @entry: the entry to move
98106
* @head: the head that will precede our entry
99107
*/
100-
static inline void list_move(struct list_head *list, struct list_head *head)
108+
static inline void list_move(struct list_head *entry, struct list_head *head)
101109
{
102-
__list_del(list->prev, list->next);
103-
list_add(list, head);
110+
__list_del(entry->prev, entry->next);
111+
list_add(entry, head);
104112
}
105113

106114
/**
107115
* list_move_tail - delete from one list and add as another's tail
108-
* @list: the entry to move
116+
* @entry: the entry to move
109117
* @head: the head that will follow our entry
110118
*/
111-
static inline void list_move_tail(struct list_head *list,
112-
struct list_head *head)
119+
static inline void list_move_tail(struct list_head *entry,
120+
struct list_head *head)
113121
{
114-
__list_del(list->prev, list->next);
115-
list_add_tail(list, head);
122+
__list_del(entry->prev, entry->next);
123+
list_add_tail(entry, head);
116124
}
117125

118126
/**
@@ -124,29 +132,30 @@ static inline int list_empty(const struct list_head *head)
124132
return head->next == head;
125133
}
126134

127-
static inline void __list_splice(struct list_head *list,
128-
struct list_head *head)
135+
static inline void __list_splice(const struct list_head *list,
136+
struct list_head *prev,
137+
struct list_head *next)
129138
{
130139
struct list_head *first = list->next;
131140
struct list_head *last = list->prev;
132-
struct list_head *at = head->next;
133141

134-
first->prev = head;
135-
head->next = first;
142+
first->prev = prev;
143+
prev->next = first;
136144

137-
last->next = at;
138-
at->prev = last;
145+
last->next = next;
146+
next->prev = last;
139147
}
140148

141149
/**
142150
* list_splice - join two lists
143151
* @list: the new list to add.
144152
* @head: the place to add it in the first list.
145153
*/
146-
static inline void list_splice(struct list_head *list, struct list_head *head)
154+
static inline void list_splice(const struct list_head *list,
155+
struct list_head *head)
147156
{
148157
if (!list_empty(list))
149-
__list_splice(list, head);
158+
__list_splice(list, head, head->next);
150159
}
151160

152161
/**
@@ -157,10 +166,10 @@ static inline void list_splice(struct list_head *list, struct list_head *head)
157166
* The list at @list is reinitialised
158167
*/
159168
static inline void list_splice_init(struct list_head *list,
160-
struct list_head *head)
169+
struct list_head *head)
161170
{
162171
if (!list_empty(list)) {
163-
__list_splice(list, head);
172+
__list_splice(list, head, head->next);
164173
INIT_LIST_HEAD(list);
165174
}
166175
}
@@ -175,44 +184,44 @@ static inline void list_splice_init(struct list_head *list,
175184
((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
176185

177186
/**
178-
* list_for_each - iterate over a list
187+
* list_for_each - iterate over a list
179188
* @pos: the &struct list_head to use as a loop counter.
180189
* @head: the head for your list.
181190
*/
182191
#define list_for_each(pos, head) \
183192
for (pos = (head)->next; pos != (head); pos = pos->next)
184193

185194
/**
186-
* list_for_each_prev - iterate over a list backwards
195+
* list_for_each_prev - iterate over a list backwards
187196
* @pos: the &struct list_head to use as a loop counter.
188197
* @head: the head for your list.
189198
*/
190199
#define list_for_each_prev(pos, head) \
191200
for (pos = (head)->prev; pos != (head); pos = pos->prev)
192201

193202
/**
194-
* list_for_each_safe - iterate over a list safe against removal of list entry
203+
* list_for_each_safe - iterate over a list safe against removal of list entry
195204
* @pos: the &struct list_head to use as a loop counter.
196205
* @n: another &struct list_head to use as temporary storage
197206
* @head: the head for your list.
198207
*/
199208
#define list_for_each_safe(pos, n, head) \
200209
for (pos = (head)->next, n = pos->next; pos != (head); \
201-
pos = n, n = pos->next)
210+
pos = n, n = pos->next)
202211

203212
/**
204-
* list_for_each_entry - iterate over list of given type
213+
* list_for_each_entry - iterate over list of given type
205214
* @pos: the type * to use as a loop counter.
206215
* @head: the head for your list.
207216
* @member: the name of the list_struct within the struct.
208217
*/
209218
#define list_for_each_entry(pos, head, member) \
210219
for (pos = list_entry((head)->next, typeof (*pos), member); \
211-
&pos->member != (head); \
212-
pos = list_entry(pos->member.next, typeof (*pos), member))
220+
&pos->member != (head); \
221+
pos = list_entry(pos->member.next, typeof (*pos), member))
213222

214-
/**
215-
* Single-linked list. Added by Xie Han <xiehan@sogou-inc.com>.
223+
/*
224+
* Singly linked list implementation.
216225
*/
217226

218227
struct slist_node {
@@ -223,7 +232,7 @@ struct slist_head {
223232
struct slist_node first, *last;
224233
};
225234

226-
#define SLIST_HEAD_INIT(name) { { (struct slist_node *)0 }, &(name).first }
235+
#define SLIST_HEAD_INIT(name) { { (struct slist_node *)0 }, &(name).first }
227236

228237
#define SLIST_HEAD(name) \
229238
struct slist_head name = SLIST_HEAD_INIT(name)
@@ -234,32 +243,32 @@ static inline void INIT_SLIST_HEAD(struct slist_head *list)
234243
list->last = &list->first;
235244
}
236245

237-
static inline void slist_add_after(struct slist_node *node,
238-
struct slist_node *prev,
239-
struct slist_head *list)
246+
static inline void slist_add_after(struct slist_node *entry,
247+
struct slist_node *prev,
248+
struct slist_head *list)
240249
{
241-
node->next = prev->next;
242-
prev->next = node;
243-
if (!node->next)
244-
list->last = node;
250+
entry->next = prev->next;
251+
prev->next = entry;
252+
if (!entry->next)
253+
list->last = entry;
245254
}
246255

247-
static inline void slist_add_head(struct slist_node *node,
248-
struct slist_head *list)
256+
static inline void slist_add_head(struct slist_node *entry,
257+
struct slist_head *list)
249258
{
250-
slist_add_after(node, &list->first, list);
259+
slist_add_after(entry, &list->first, list);
251260
}
252261

253-
static inline void slist_add_tail(struct slist_node *node,
254-
struct slist_head *list)
262+
static inline void slist_add_tail(struct slist_node *entry,
263+
struct slist_head *list)
255264
{
256-
node->next = (struct slist_node *)0;
257-
list->last->next = node;
258-
list->last = node;
265+
entry->next = (struct slist_node *)0;
266+
list->last->next = entry;
267+
list->last = entry;
259268
}
260269

261270
static inline void slist_del_after(struct slist_node *prev,
262-
struct slist_head *list)
271+
struct slist_head *list)
263272
{
264273
prev->next = prev->next->next;
265274
if (!prev->next)
@@ -271,35 +280,35 @@ static inline void slist_del_head(struct slist_head *list)
271280
slist_del_after(&list->first, list);
272281
}
273282

274-
static inline int slist_empty(struct slist_head *list)
283+
static inline int slist_empty(const struct slist_head *list)
275284
{
276285
return !list->first.next;
277286
}
278287

279-
static inline void __slist_splice(struct slist_head *list,
280-
struct slist_node *at,
281-
struct slist_head *head)
288+
static inline void __slist_splice(const struct slist_head *list,
289+
struct slist_node *prev,
290+
struct slist_head *head)
282291
{
283-
list->last->next = at->next;
284-
at->next = list->first.next;
292+
list->last->next = prev->next;
293+
prev->next = list->first.next;
285294
if (!list->last->next)
286295
head->last = list->last;
287296
}
288297

289-
static inline void slist_splice(struct slist_head *list,
290-
struct slist_node *at,
291-
struct slist_head *head)
298+
static inline void slist_splice(const struct slist_head *list,
299+
struct slist_node *prev,
300+
struct slist_head *head)
292301
{
293302
if (!slist_empty(list))
294-
__slist_splice(list, at, head);
303+
__slist_splice(list, prev, head);
295304
}
296305

297306
static inline void slist_splice_init(struct slist_head *list,
298-
struct slist_node *at,
299-
struct slist_head *head)
307+
struct slist_node *prev,
308+
struct slist_head *head)
300309
{
301310
if (!slist_empty(list)) {
302-
__slist_splice(list, at, head);
311+
__slist_splice(list, prev, head);
303312
INIT_SLIST_HEAD(list);
304313
}
305314
}
@@ -312,11 +321,11 @@ static inline void slist_splice_init(struct slist_head *list,
312321

313322
#define slist_for_each_safe(pos, prev, head) \
314323
for (prev = &(head)->first, pos = prev->next; pos; \
315-
prev = prev->next == pos ? pos : prev, pos = prev->next)
324+
prev = prev->next == pos ? pos : prev, pos = prev->next)
316325

317326
#define slist_for_each_entry(pos, head, member) \
318327
for (pos = slist_entry((head)->first.next, typeof (*pos), member); \
319-
&pos->member != (struct slist_node *)0; \
320-
pos = slist_entry(pos->member.next, typeof (*pos), member))
328+
&pos->member != (struct slist_node *)0; \
329+
pos = slist_entry(pos->member.next, typeof (*pos), member))
321330

322331
#endif

0 commit comments

Comments
 (0)