-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathlist.h
469 lines (425 loc) · 15.8 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* Linux-like circular doubly-linked list implementation */
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
/**
* Feature detection for 'typeof':
* - Supported as a GNU extension in GCC/Clang.
* - Part of C23 standard (ISO/IEC 9899:2024).
*
* Reference: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
*/
#if defined(__GNUC__) || defined(__clang__) || \
(defined(__STDC__) && defined(__STDC_VERSION__) && \
(__STDC_VERSION__ >= 202311L)) /* C23 ?*/
#define __LIST_HAVE_TYPEOF 1
#else
#define __LIST_HAVE_TYPEOF 0
#endif
/**
* struct list_head - Node structure for a circular doubly-linked list
* @next: Pointer to the next node in the list.
* @prev: Pointer to the previous node in the list.
*
* Defines both the head and nodes of a circular doubly-linked list. The head's
* @next points to the first node and @prev to the last node; in an empty list,
* both point to the head itself. All nodes, including the head, share this
* structure type.
*
* Nodes are typically embedded within a container structure holding actual
* data, accessible via the list_entry() helper, which computes the container's
* address from a node pointer. The list_* functions and macros provide an API
* for manipulating this data structure efficiently.
*/
struct list_head {
struct list_head *prev;
struct list_head *next;
};
/**
* container_of() - Calculate address of structure that contains address ptr
* @ptr: pointer to member variable
* @type: type of the structure containing ptr
* @member: name of the member variable in struct @type
*
* Return: @type pointer of structure containing ptr
*/
#ifndef container_of
#if __LIST_HAVE_TYPEOF
#define container_of(ptr, type, member) \
__extension__({ \
const typeof(((type *) 0)->member) *__pmember = (ptr); \
(type *) ((char *) __pmember - offsetof(type, member)); \
})
#else
#define container_of(ptr, type, member) \
((type *) ((char *) (ptr) - offsetof(type, member)))
#endif
#endif
/**
* LIST_HEAD - Define and initialize a circular list head
* @head: name of the new list
*/
#define LIST_HEAD(head) struct list_head head = {&(head), &(head)}
/**
* INIT_LIST_HEAD() - Initialize empty list head
* @head: Pointer to the list_head structure to initialize.
*
* It sets both @next and @prev to point to the structure itself. The
* initialization applies to either a list head or an unlinked node that is
* not yet part of a list.
*
* Unlinked nodes may be passed to functions using 'list_del()' or
* 'list_del_init()', which are safe only on initialized nodes. Applying these
* operations to an uninitialized node results in undefined behavior, such as
* memory corruption or crashes.
*/
static inline void INIT_LIST_HEAD(struct list_head *head)
{
head->next = head;
head->prev = head;
}
/**
* list_add - Insert a node after a given node in a circular list
* @node: Pointer to the list_head structure to add.
* @head: Pointer to the list_head structure after which to add the new node.
*
* Adds the specified @node immediately after @head in a circular doubly-linked
* list. The node that previously followed @head will now follow @node, and the
* list's circular structure is maintained.
*/
static inline void list_add(struct list_head *node, struct list_head *head)
{
struct list_head *next = head->next;
next->prev = node;
node->next = next;
node->prev = head;
head->next = node;
}
/**
* list_add_tail() - Add a list node to the end of the list
* @node: pointer to the new node
* @head: pointer to the head of the list
*/
static inline void list_add_tail(struct list_head *node, struct list_head *head)
{
struct list_head *prev = head->prev;
prev->next = node;
node->next = head;
node->prev = prev;
head->prev = node;
}
/**
* list_del - Remove a node from a circular doubly-linked list
* @node: Pointer to the list_head structure to remove.
*
* Removes @node from its list by updating the adjacent nodes’ pointers to
* bypass it. The node’s memory and its containing structure, if any, are not
* freed. After removal, @node is left unlinked and should be treated as
* uninitialized; accessing its @next or @prev pointers is unsafe and may cause
* undefined behavior.
*
* Even previously initialized but unlinked nodes become uninitialized after
* this operation. To reintegrate @node into a list, it must be reinitialized
* (e.g., via INIT_LIST_HEAD).
*
* If LIST_POISONING is enabled at build time, @next and @prev are set to
* invalid addresses to trigger memory access faults on misuse. This feature is
* effective only on systems that restrict access to these specific addresses.
*/
static inline void list_del(struct list_head *node)
{
struct list_head *next = node->next;
struct list_head *prev = node->prev;
next->prev = prev;
prev->next = next;
#ifdef LIST_POISONING
node->next = NULL;
node->prev = NULL;
#endif
}
/**
* list_del_init - Remove a node and reinitialize it as unlinked
* @node: Pointer to the list_head structure to remove and reinitialize.
*
* Removes @node from its circular doubly-linked list using list_del() and then
* reinitializes it as an unlinked node via INIT_LIST_HEAD(). Unlike list_del(),
* which leaves the node uninitialized, this ensures @node is safely reset to an
* empty, standalone state with @next and @prev pointing to itself.
*/
static inline void list_del_init(struct list_head *node)
{
list_del(node);
INIT_LIST_HEAD(node);
}
/**
* list_empty - Test if a circular list has no nodes
* @head: Pointer to the list_head structure representing the list head.
*
* Checks whether the circular doubly-linked list headed by @head is empty.
* A list is empty if @head’s @next points to itself, indicating no nodes are
* attached.
*
* Returns: 0 if the list has nodes, non-zero if the list is empty.
*/
static inline int list_empty(const struct list_head *head)
{
return (head->next == head);
}
/**
* list_is_singular() - Check if list head has exactly one node attached
* @head: pointer to the head of the list
*
* Returns: 0 if the list is not singular, non-zero if the list has exactly one
* entry.
*/
static inline int list_is_singular(const struct list_head *head)
{
return (!list_empty(head) && head->prev == head->next);
}
/**
* list_splice() - Add list nodes from a list to beginning of another list
* @list: pointer to the head of the list with the node entries
* @head: pointer to the head of the list
*
* All nodes from @list are added to the beginning of the list of @head.
* It is similar to list_add but for multiple nodes. The @list head is not
* modified and has to be initialized to be used as a valid list head/node
* again.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
struct list_head *head_first = head->next;
struct list_head *list_first = list->next;
struct list_head *list_last = list->prev;
if (list_empty(list))
return;
head->next = list_first;
list_first->prev = head;
list_last->next = head_first;
head_first->prev = list_last;
}
/**
* list_splice_tail() - Add list nodes from a list to end of another list
* @list: pointer to the head of the list with the node entries
* @head: pointer to the head of the list
*
* All nodes from @list are added to to the end of the list of @head.
* It is similar to list_add_tail but for multiple nodes. The @list head is not
* modified and has to be initialized to be used as a valid list head/node
* again.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
struct list_head *head_last = head->prev;
struct list_head *list_first = list->next;
struct list_head *list_last = list->prev;
if (list_empty(list))
return;
head->prev = list_last;
list_last->next = head;
list_first->prev = head_last;
head_last->next = list_first;
}
/**
* list_splice_init() - Move list nodes from a list to beginning of another list
* @list: pointer to the head of the list with the node entries
* @head: pointer to the head of the list
*
* All nodes from @list are added to to the beginning of the list of @head.
* It is similar to list_add but for multiple nodes.
*
* The @list head will not end up in an uninitialized state like when using
* list_splice. Instead the @list is initialized again to the an empty
* list/unlinked state.
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
list_splice(list, head);
INIT_LIST_HEAD(list);
}
/**
* list_splice_tail_init() - Move list nodes from a list to end of another list
* @list: pointer to the head of the list with the node entries
* @head: pointer to the head of the list
*
* All nodes from @list are added to to the end of the list of @head.
* It is similar to list_add_tail but for multiple nodes.
*
* The @list head will not end up in an uninitialized state like when using
* list_splice. Instead the @list is initialized again to the an empty
* list/unlinked state.
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
list_splice_tail(list, head);
INIT_LIST_HEAD(list);
}
/**
* list_cut_position() - Move beginning of a list to another list
* @head_to: pointer to the head of the list which receives nodes
* @head_from: pointer to the head of the list
* @node: pointer to the node in which defines the cutting point
*
* All entries from the beginning of the list @head_from to (including) the
* @node is moved to @head_to.
*
* @head_to is replaced when @head_from is not empty. @node must be a real
* list node from @head_from or the behavior is undefined.
*/
static inline void list_cut_position(struct list_head *head_to,
struct list_head *head_from,
struct list_head *node)
{
struct list_head *head_from_first = head_from->next;
if (list_empty(head_from))
return;
if (head_from == node) {
INIT_LIST_HEAD(head_to);
return;
}
head_from->next = node->next;
head_from->next->prev = head_from;
head_to->prev = node;
node->next = head_to;
head_to->next = head_from_first;
head_to->next->prev = head_to;
}
/**
* list_move() - Move a list node to the beginning of the list
* @node: pointer to the node
* @head: pointer to the head of the list
*
* The @node is removed from its old position/node and add to the beginning of
* @head
*/
static inline void list_move(struct list_head *node, struct list_head *head)
{
list_del(node);
list_add(node, head);
}
/**
* list_move_tail() - Move a list node to the end of the list
* @node: pointer to the node
* @head: pointer to the head of the list
*
* The @node is removed from its old position/node and add to the end of @head
*/
static inline void list_move_tail(struct list_head *node,
struct list_head *head)
{
list_del(node);
list_add_tail(node, head);
}
/**
* list_entry() - Get the entry for this node
* @node: pointer to list node
* @type: type of the entry containing the list node
* @member: name of the list_head member variable in struct @type
*
* Return: @type pointer of entry containing node
*/
#define list_entry(node, type, member) container_of(node, type, member)
/**
* list_first_entry() - Get first entry of the list
* @head: pointer to the head of the list
* @type: type of the entry containing the list node
* @member: name of the list_head member variable in struct @type
*
* Return: @type pointer of first entry in list
*/
#define list_first_entry(head, type, member) \
list_entry((head)->next, type, member)
/**
* list_last_entry() - Get last entry of the list
* @head: pointer to the head of the list
* @type: type of the entry containing the list node
* @member: name of the list_head member variable in struct @type
*
* Return: @type pointer of last entry in list
*/
#define list_last_entry(head, type, member) \
list_entry((head)->prev, type, member)
/**
* list_for_each - Iterate over list nodes
* @node: list_head pointer used as iterator
* @head: pointer to the head of the list
*
* The nodes and the head of the list must be kept unmodified while
* iterating through it. Any modifications to the the list will cause undefined
* behavior.
*/
#define list_for_each(node, head) \
for (node = (head)->next; node != (head); node = node->next)
/**
* list_for_each_entry - Iterate over a list of entries
* @entry: Pointer to the structure type, used as the loop iterator.
* @head: Pointer to the list_head structure representing the list head.
* @member: Name of the list_head member within the structure type of @entry.
*
* Iterates over a circular doubly-linked list, starting from the first node
* after @head until reaching @head again. The macro assumes the list structure
* remains unmodified during iteration; any changes (e.g., adding/removing
* nodes) may result in undefined behavior.
*/
#if __LIST_HAVE_TYPEOF
#define list_for_each_entry(entry, head, member) \
for (entry = list_entry((head)->next, typeof(*entry), member); \
&entry->member != (head); \
entry = list_entry(entry->member.next, typeof(*entry), member))
#else
/* The negative width bit-field makes a compile-time error for use of this. It
* works in the same way as BUILD_BUG_ON_ZERO macro of Linux kernel.
*/
#define list_for_each_entry(entry, head, member) \
for (entry = (void *) 1; sizeof(struct { int i : -1; }); ++(entry))
#endif
/**
* list_for_each_safe - Iterate over list nodes, allowing removal
* @node: Pointer to a list_head structure, used as the loop iterator.
* @safe: Pointer to a list_head structure, storing the next node for safe
* iteration.
* @head: Pointer to the list_head structure representing the list head.
*
* Iterates over a circular doubly-linked list, starting from the first node
* after @head and continuing until reaching @head again. This macro allows
* safe removal of the current node (@node) during iteration by pre-fetching
* the next node into @safe. Other modifications to the list structure (e.g.,
* adding nodes or altering @head) may result in undefined behavior.
*/
#define list_for_each_safe(node, safe, head) \
for (node = (head)->next, safe = node->next; node != (head); \
node = safe, safe = node->next)
/**
* list_for_each_entry_safe - Iterate over a list, allowing node removal
* @entry: Pointer to the structure type, used as the loop iterator.
* @safe: Pointer to the structure type, storing the next entry for safe
* iteration.
* @head: Pointer to the list_head structure representing the list head.
* @member: Name of the list_head member within the structure type of @entry.
*
* Iterates over a circular doubly-linked list, starting from the first node
* after @head and continuing until reaching @head again. This macro permits
* safe removal of the current node (@entry) during iteration by pre-fetching
* the next node into @safe. Other modifications to the list structure (e.g.,
* adding nodes or altering @head) may result in undefined behavior.
*/
#if __LIST_HAVE_TYPEOF
#define list_for_each_entry_safe(entry, safe, head, member) \
for (entry = list_entry((head)->next, typeof(*entry), member), \
safe = list_entry(entry->member.next, typeof(*entry), member); \
&entry->member != (head); entry = safe, \
safe = list_entry(safe->member.next, typeof(*entry), member))
#else
#define list_for_each_entry_safe(entry, safe, head, member) \
for (entry = safe = (void *) 1; sizeof(struct { int i : -1; }); \
++(entry), ++(safe))
#endif
#undef __LIST_HAVE_TYPEOF
#ifdef __cplusplus
}
#endif