GCC Code Coverage Report


Directory: ./
File: include/list.h
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 68 68 100.0%
Functions: 17 17 100.0%
Branches: 13 14 92.9%

Line Branch Exec Source
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #include <stddef.h>
5
6 #define prefetch(x) ((void)0)
7
8 #define LIST_POISON1 ((void *)0x12345678)
9 #define LIST_POISON2 ((void *)0x87654321)
10
11 /*
12 * Simple doubly linked list implementation.
13 *
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
19 */
20
21 struct list_head {
22 struct list_head *next, *prev;
23 };
24
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27 #define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
29
30 3422474687 static inline void init_list_head(struct list_head *list)
31 {
32 3422474687 list->next = list;
33 3422474687 list->prev = list;
34 3422474687 }
35
36 /**
37 * list_is_first -- tests whether @list is the first entry in list @head
38 * @list: the entry to test
39 * @head: the head of the list
40 */
41 49576 static inline int list_is_first(const struct list_head *list,
42 const struct list_head *head)
43 {
44 49576 return list->prev == head;
45 }
46
47 /*
48 * Insert a new entry between two known consecutive entries.
49 *
50 * This is only for internal list manipulation where we know
51 * the prev/next entries already!
52 */
53 − static inline void __list_add(struct list_head *new,
54 struct list_head *prev,
55 struct list_head *next)
56 {
57 − next->prev = new;
58 − new->next = next;
59 − new->prev = prev;
60 − prev->next = new;
61 − }
62
63 /**
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
67 *
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
70 */
71 29137 static inline void list_add(struct list_head *new, struct list_head *head)
72 {
73 29137 __list_add(new, head, head->next);
74 29137 }
75
76
77 /**
78 * list_add_tail - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it before
81 *
82 * Insert a new entry before the specified head.
83 * This is useful for implementing queues.
84 */
85 33858439 static inline void list_add_tail(struct list_head *new, struct list_head *head)
86 {
87 33858439 __list_add(new, head->prev, head);
88 33858439 }
89
90 /*
91 * Delete a list entry by making the prev/next entries
92 * point to each other.
93 *
94 * This is only for internal list manipulation where we know
95 * the prev/next entries already!
96 */
97 − static inline void __list_del(struct list_head * prev, struct list_head * next)
98 {
99 − next->prev = prev;
100 − prev->next = next;
101 − }
102
103 /**
104 * list_del - deletes entry from list.
105 * @entry: the element to delete from the list.
106 * Note: list_empty() on entry does not return true after this, the entry is
107 * in an undefined state.
108 */
109 10508649 static inline void list_del(struct list_head *entry)
110 {
111 10508649 __list_del(entry->prev, entry->next);
112 10508649 entry->next = LIST_POISON1;
113 10508649 entry->prev = LIST_POISON2;
114 10508649 }
115
116 /**
117 * list_replace - replace old entry by new one
118 * @old : the element to be replaced
119 * @new : the new element to insert
120 *
121 * If @old was empty, it will be overwritten.
122 */
123 13322 static inline void list_replace(struct list_head *old,
124 struct list_head *new)
125 {
126 13322 new->next = old->next;
127 13322 new->next->prev = new;
128 13322 new->prev = old->prev;
129 13322 new->prev->next = new;
130 13322 }
131
132 static inline void list_replace_init(struct list_head *old,
133 struct list_head *new)
134 {
135 list_replace(old, new);
136 init_list_head(old);
137 }
138
139 /**
140 * list_del_init - deletes entry from list and reinitialize it.
141 * @entry: the element to delete from the list.
142 */
143 static inline void list_del_init(struct list_head *entry)
144 {
145 __list_del(entry->prev, entry->next);
146 init_list_head(entry);
147 }
148
149 /**
150 * list_move - delete from one list and add as another's head
151 * @list: the entry to move
152 * @head: the head that will precede our entry
153 */
154 31 static inline void list_move(struct list_head *list, struct list_head *head)
155 {
156 31 __list_del(list->prev, list->next);
157 31 list_add(list, head);
158 31 }
159
160 /**
161 * list_move_tail - delete from one list and add as another's tail
162 * @list: the entry to move
163 * @head: the head that will follow our entry
164 */
165 1351323 static inline void list_move_tail(struct list_head *list,
166 struct list_head *head)
167 {
168 1351323 __list_del(list->prev, list->next);
169 1351323 list_add_tail(list, head);
170 1351323 }
171
172 /**
173 * list_is_last - tests whether @list is the last entry in list @head
174 * @list: the entry to test
175 * @head: the head of the list
176 */
177 194555 static inline int list_is_last(const struct list_head *list,
178 const struct list_head *head)
179 {
180 194555 return list->next == head;
181 }
182
183 /**
184 * list_empty - tests whether a list is empty
185 * @head: the list to test.
186 */
187 16818079 static inline int list_empty(const struct list_head *head)
188 {
189 16818079 return head->next == head;
190 }
191
192 /**
193 * list_empty_careful - tests whether a list is empty and not being modified
194 * @head: the list to test
195 *
196 * Description:
197 * tests whether a list is empty _and_ checks that no other CPU might be
198 * in the process of modifying either member (next or prev)
199 *
200 * NOTE: using list_empty_careful() without synchronization
201 * can only be safe if the only activity that can happen
202 * to the list entry is list_del_init(). Eg. it cannot be used
203 * if another CPU could re-list_add() it.
204 */
205 static inline int list_empty_careful(const struct list_head *head)
206 {
207 struct list_head *next = head->next;
208 return (next == head) && (next == head->prev);
209 }
210
211 /**
212 * list_is_singular - tests whether a list has just one entry.
213 * @head: the list to test.
214 */
215 4083693 static inline int list_is_singular(const struct list_head *head)
216 {
217
3/4
✓ Branch 0 (3→4) taken 4083693 times.
✗ Branch 1 (3→6) not taken.
✓ Branch 2 (4→5) taken 2048110 times.
✓ Branch 3 (4→6) taken 2035583 times.
4083693 return !list_empty(head) && (head->next == head->prev);
218 }
219
220 − static inline void __list_cut_position(struct list_head *list,
221 struct list_head *head, struct list_head *entry)
222 {
223 − struct list_head *new_first = entry->next;
224 − list->next = head->next;
225 − list->next->prev = list;
226 − list->prev = entry;
227 − entry->next = list;
228 − head->next = new_first;
229 − new_first->prev = head;
230 − }
231
232 /**
233 * list_cut_position - cut a list into two
234 * @list: a new list to add all removed entries
235 * @head: a list with entries
236 * @entry: an entry within head, could be the head itself
237 * and if so we won't cut the list
238 *
239 * This helper moves the initial part of @head, up to and
240 * including @entry, from @head to @list. You should
241 * pass on @entry an element you know is on @head. @list
242 * should be an empty list or a list you do not care about
243 * losing its data.
244 *
245 */
246 static inline void list_cut_position(struct list_head *list,
247 struct list_head *head, struct list_head *entry)
248 {
249 if (list_empty(head))
250 return;
251 if (list_is_singular(head) &&
252 (head->next != entry && head != entry))
253 return;
254 if (entry == head)
255 init_list_head(list);
256 else
257 __list_cut_position(list, head, entry);
258 }
259
260 − static inline void __list_splice(const struct list_head *list,
261 struct list_head *prev,
262 struct list_head *next)
263 {
264 − struct list_head *first = list->next;
265 − struct list_head *last = list->prev;
266
267 − first->prev = prev;
268 − prev->next = first;
269
270 − last->next = next;
271 − next->prev = last;
272 − }
273
274 /**
275 * list_splice - join two lists, this is designed for stacks
276 * @list: the new list to add.
277 * @head: the place to add it in the first list.
278 */
279 2239 static inline void list_splice(const struct list_head *list,
280 struct list_head *head)
281 {
282
2/2
✓ Branch 0 (3→4) taken 2111 times.
✓ Branch 1 (3→5) taken 128 times.
2239 if (!list_empty(list))
283 2111 __list_splice(list, head, head->next);
284 2239 }
285
286 /**
287 * list_splice_tail - join two lists, each list being a queue
288 * @list: the new list to add.
289 * @head: the place to add it in the first list.
290 */
291 151098 static inline void list_splice_tail(struct list_head *list,
292 struct list_head *head)
293 {
294
2/2
✓ Branch 0 (3→4) taken 115299 times.
✓ Branch 1 (3→5) taken 35799 times.
151098 if (!list_empty(list))
295 115299 __list_splice(list, head->prev, head);
296 151098 }
297
298 /**
299 * list_splice_init - join two lists and reinitialise the emptied list.
300 * @list: the new list to add.
301 * @head: the place to add it in the first list.
302 *
303 * The list at @list is reinitialised
304 */
305 204235 static inline void list_splice_init(struct list_head *list,
306 struct list_head *head)
307 {
308
2/2
✓ Branch 0 (3→4) taken 50099 times.
✓ Branch 1 (3→6) taken 154136 times.
204235 if (!list_empty(list)) {
309 50099 __list_splice(list, head, head->next);
310 50099 init_list_head(list);
311 }
312 204235 }
313
314 /**
315 * list_splice_tail_init - join two lists and reinitialise the emptied list
316 * @list: the new list to add.
317 * @head: the place to add it in the first list.
318 *
319 * Each of the lists is a queue.
320 * The list at @list is reinitialised
321 */
322 7123983 static inline void list_splice_tail_init(struct list_head *list,
323 struct list_head *head)
324 {
325
2/2
✓ Branch 0 (3→4) taken 184973 times.
✓ Branch 1 (3→6) taken 6939010 times.
7123983 if (!list_empty(list)) {
326 184973 __list_splice(list, head->prev, head);
327 184973 init_list_head(list);
328 }
329 7123983 }
330
331 /**
332 * list_entry - get the struct for this entry
333 * @ptr: the &struct list_head pointer.
334 * @type: the type of the struct this is embedded in.
335 * @member: the name of the list_struct within the struct.
336 */
337 #define list_entry(ptr, type, member) \
338 container_of(ptr, type, member)
339
340 /**
341 * list_first_entry - get the first element from a list
342 * @ptr: the list head to take the element from.
343 * @type: the type of the struct this is embedded in.
344 * @member: the name of the list_struct within the struct.
345 *
346 * Note, that list is expected to be not empty.
347 */
348 #define list_first_entry(ptr, type, member) \
349 list_entry((ptr)->next, type, member)
350
351 /**
352 * list_prev_entry - get the prev element in list
353 * @ptr: the type * to cursor
354 * @member: the name of the list_head within the struct.
355 */
356 #define list_prev_entry(ptr, member) \
357 list_entry((ptr)->member.prev, typeof(*(ptr)), member)
358
359 /**
360 * list_last_entry - get the last element from a list
361 * @ptr: the list head to take the element from.
362 * @type: the type of the struct this is embedded in.
363 * @member: the name of the list_head within the struct.
364 *
365 * Note, that list is expected to be not empty.
366 */
367 #define list_last_entry(ptr, type, member) \
368 list_entry((ptr)->prev, type, member)
369
370 /**
371 * list_next_entry - get the next element in list
372 * @pos: the type * to cursor
373 * @member: the name of the list_head within the struct.
374 */
375 #define list_next_entry(pos, member) \
376 list_entry((pos)->member.next, typeof(*(pos)), member)
377
378 /**
379 * list_for_each_entry - iterate over list of given type
380 * @pos: the type * to use as a loop cursor.
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
383 */
384 #define list_for_each_entry(pos, head, member) \
385 for (pos = list_entry((head)->next, typeof(*pos), member); \
386 prefetch(pos->member.next), &pos->member != (head); \
387 pos = list_entry(pos->member.next, typeof(*pos), member))
388
389 /**
390 * list_for_each_entry_reverse - iterate backwards over list of given type.
391 * @pos: the type * to use as a loop cursor.
392 * @head: the head for your list.
393 * @member: the name of the list_struct within the struct.
394 */
395 #define list_for_each_entry_reverse(pos, head, member) \
396 for (pos = list_entry((head)->prev, typeof(*pos), member); \
397 prefetch(pos->member.prev), &pos->member != (head); \
398 pos = list_entry(pos->member.prev, typeof(*pos), member))
399
400 /**
401 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
402 * @pos: the type * to use as a start point
403 * @head: the head of the list
404 * @member: the name of the list_struct within the struct.
405 *
406 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
407 */
408 #define list_prepare_entry(pos, head, member) \
409 ((pos) ? : list_entry(head, typeof(*pos), member))
410
411 /**
412 * list_for_each_entry_continue - continue iteration over list of given type
413 * @pos: the type * to use as a loop cursor.
414 * @head: the head for your list.
415 * @member: the name of the list_struct within the struct.
416 *
417 * Continue to iterate over list of given type, continuing after
418 * the current position.
419 */
420 #define list_for_each_entry_continue(pos, head, member) \
421 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
422 prefetch(pos->member.next), &pos->member != (head); \
423 pos = list_entry(pos->member.next, typeof(*pos), member))
424
425 /**
426 * list_for_each_entry_continue_reverse - iterate backwards from the given point
427 * @pos: the type * to use as a loop cursor.
428 * @head: the head for your list.
429 * @member: the name of the list_struct within the struct.
430 *
431 * Start to iterate over list of given type backwards, continuing after
432 * the current position.
433 */
434 #define list_for_each_entry_continue_reverse(pos, head, member) \
435 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
436 prefetch(pos->member.prev), &pos->member != (head); \
437 pos = list_entry(pos->member.prev, typeof(*pos), member))
438
439 /**
440 * list_for_each_entry_from - iterate over list of given type from the current point
441 * @pos: the type * to use as a loop cursor.
442 * @head: the head for your list.
443 * @member: the name of the list_struct within the struct.
444 *
445 * Iterate over list of given type, continuing from current position.
446 */
447 #define list_for_each_entry_from(pos, head, member) \
448 for (; prefetch(pos->member.next), &pos->member != (head); \
449 pos = list_entry(pos->member.next, typeof(*pos), member))
450
451 /**
452 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
453 * @pos: the type * to use as a loop cursor.
454 * @n: another type * to use as temporary storage
455 * @head: the head for your list.
456 * @member: the name of the list_struct within the struct.
457 */
458 #define list_for_each_entry_safe(pos, n, head, member) \
459 for (pos = list_entry((head)->next, typeof(*pos), member), \
460 n = list_entry(pos->member.next, typeof(*pos), member); \
461 &pos->member != (head); \
462 pos = n, n = list_entry(n->member.next, typeof(*n), member))
463
464 /**
465 * list_for_each_entry_safe_continue
466 * @pos: the type * to use as a loop cursor.
467 * @n: another type * to use as temporary storage
468 * @head: the head for your list.
469 * @member: the name of the list_struct within the struct.
470 *
471 * Iterate over list of given type, continuing after current point,
472 * safe against removal of list entry.
473 */
474 #define list_for_each_entry_safe_continue(pos, n, head, member) \
475 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
476 n = list_entry(pos->member.next, typeof(*pos), member); \
477 &pos->member != (head); \
478 pos = n, n = list_entry(n->member.next, typeof(*n), member))
479
480 /**
481 * list_for_each_entry_safe_from
482 * @pos: the type * to use as a loop cursor.
483 * @n: another type * to use as temporary storage
484 * @head: the head for your list.
485 * @member: the name of the list_struct within the struct.
486 *
487 * Iterate over list of given type from current point, safe against
488 * removal of list entry.
489 */
490 #define list_for_each_entry_safe_from(pos, n, head, member) \
491 for (n = list_entry(pos->member.next, typeof(*pos), member); \
492 &pos->member != (head); \
493 pos = n, n = list_entry(n->member.next, typeof(*n), member))
494
495 /**
496 * list_for_each_entry_safe_reverse
497 * @pos: the type * to use as a loop cursor.
498 * @n: another type * to use as temporary storage
499 * @head: the head for your list.
500 * @member: the name of the list_struct within the struct.
501 *
502 * Iterate backwards over list of given type, safe against removal
503 * of list entry.
504 */
505 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
506 for (pos = list_entry((head)->prev, typeof(*pos), member), \
507 n = list_entry(pos->member.prev, typeof(*pos), member); \
508 &pos->member != (head); \
509 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
510
511 /*
512 * Double linked lists with a single pointer list head.
513 * Mostly useful for hash tables where the two pointer list head is
514 * too wasteful.
515 * You lose the ability to access the tail in O(1).
516 */
517
518 struct hlist_head {
519 struct hlist_node *first;
520 };
521
522 struct hlist_node {
523 struct hlist_node *next, **pprev;
524 };
525
526 #define HLIST_HEAD_INIT { .first = NULL }
527 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
528
529 #define init_hlist_head(ptr) ((ptr)->first = NULL)
530
531 static inline void init_hlist_node(struct hlist_node *h)
532 {
533 h->next = NULL;
534 h->pprev = NULL;
535 }
536
537 static inline int hlist_unhashed(const struct hlist_node *h)
538 {
539 return !h->pprev;
540 }
541
542 static inline int hlist_empty(const struct hlist_head *h)
543 {
544 return !h->first;
545 }
546
547 − static inline void __hlist_del(struct hlist_node *n)
548 {
549 − struct hlist_node *next = n->next;
550 − struct hlist_node **pprev = n->pprev;
551 − *pprev = next;
552 − if (next)
553 − next->pprev = pprev;
554 − }
555
556 3396 static inline void hlist_del(struct hlist_node *n)
557 {
558 3396 __hlist_del(n);
559 3396 n->next = LIST_POISON1;
560 3396 n->pprev = LIST_POISON2;
561 3396 }
562
563 static inline void hlist_del_init(struct hlist_node *n)
564 {
565 if (!hlist_unhashed(n)) {
566 __hlist_del(n);
567 init_hlist_node(n);
568 }
569 }
570
571 3396 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
572 {
573 3396 struct hlist_node *first = h->first;
574 3396 n->next = first;
575
2/2
✓ Branch 0 (2→3) taken 2488 times.
✓ Branch 1 (2→4) taken 908 times.
3396 if (first)
576 2488 first->pprev = &n->next;
577 3396 h->first = n;
578 3396 n->pprev = &h->first;
579 3396 }
580
581 /* next must be != NULL */
582 static inline void hlist_add_before(struct hlist_node *n,
583 struct hlist_node *next)
584 {
585 n->pprev = next->pprev;
586 n->next = next;
587 next->pprev = &n->next;
588 *(n->pprev) = n;
589 }
590
591 static inline void hlist_add_after(struct hlist_node *n,
592 struct hlist_node *next)
593 {
594 next->next = n->next;
595 n->next = next;
596 next->pprev = &n->next;
597
598 if(next->next)
599 next->next->pprev = &next->next;
600 }
601
602 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
603
604 #define hlist_for_each(pos, head) \
605 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
606 pos = pos->next)
607
608 #define hlist_for_each_safe(pos, n, head) \
609 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
610 pos = n)
611
612 /**
613 * hlist_for_each_entry - iterate over list of given type
614 * @tpos: the type * to use as a loop cursor.
615 * @pos: the &struct hlist_node to use as a loop cursor.
616 * @head: the head for your list.
617 * @member: the name of the hlist_node within the struct.
618 */
619 #define hlist_for_each_entry(tpos, pos, head, member) \
620 for (pos = (head)->first; \
621 pos && ({ prefetch(pos->next); 1;}) && \
622 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
623 pos = pos->next)
624
625 /**
626 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
627 * @tpos: the type * to use as a loop cursor.
628 * @pos: the &struct hlist_node to use as a loop cursor.
629 * @member: the name of the hlist_node within the struct.
630 */
631 #define hlist_for_each_entry_continue(tpos, pos, member) \
632 for (pos = (pos)->next; \
633 pos && ({ prefetch(pos->next); 1;}) && \
634 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
635 pos = pos->next)
636
637 /**
638 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
639 * @tpos: the type * to use as a loop cursor.
640 * @pos: the &struct hlist_node to use as a loop cursor.
641 * @member: the name of the hlist_node within the struct.
642 */
643 #define hlist_for_each_entry_from(tpos, pos, member) \
644 for (; pos && ({ prefetch(pos->next); 1;}) && \
645 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
646 pos = pos->next)
647
648 /**
649 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
650 * @tpos: the type * to use as a loop cursor.
651 * @pos: the &struct hlist_node to use as a loop cursor.
652 * @n: another &struct hlist_node to use as temporary storage
653 * @head: the head for your list.
654 * @member: the name of the hlist_node within the struct.
655 */
656 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
657 for (pos = (head)->first; \
658 pos && ({ n = pos->next; 1; }) && \
659 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
660 pos = n)
661
662 #endif
663