1 | /* Copyright (C) 2002-2023 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #ifndef _LIST_H |
19 | #define _LIST_H 1 |
20 | |
21 | /* Internal: doubly linked lists. */ |
22 | |
23 | /* The definitions of this file are adopted from those which can be |
24 | found in the Linux kernel headers to enable people familiar with |
25 | the latter find their way in these sources as well. */ |
26 | |
27 | #include <list_t.h> |
28 | #include <atomic.h> |
29 | |
30 | /* Define a variable with the head and tail of the list. */ |
31 | #define LIST_HEAD(name) \ |
32 | list_t name = { &(name), &(name) } |
33 | |
34 | /* Initialize a new list head. */ |
35 | #define INIT_LIST_HEAD(ptr) \ |
36 | (ptr)->next = (ptr)->prev = (ptr) |
37 | |
38 | |
39 | /* Add new element at the head of the list. */ |
40 | static inline void |
41 | list_add (list_t *newp, list_t *head) |
42 | { |
43 | newp->next = head->next; |
44 | newp->prev = head; |
45 | head->next->prev = newp; |
46 | atomic_write_barrier (); |
47 | head->next = newp; |
48 | } |
49 | |
50 | |
51 | /* Remove element from list. */ |
52 | static inline void |
53 | list_del (list_t *elem) |
54 | { |
55 | elem->next->prev = elem->prev; |
56 | elem->prev->next = elem->next; |
57 | } |
58 | |
59 | |
60 | /* Join two lists. */ |
61 | static inline void |
62 | list_splice (list_t *add, list_t *head) |
63 | { |
64 | /* Do nothing if the list which gets added is empty. */ |
65 | if (add != add->next) |
66 | { |
67 | add->next->prev = head; |
68 | add->prev->next = head->next; |
69 | head->next->prev = add->prev; |
70 | head->next = add->next; |
71 | } |
72 | } |
73 | |
74 | |
75 | /* Get typed element from list at a given position. */ |
76 | #define list_entry(ptr, type, member) \ |
77 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
78 | |
79 | |
80 | |
81 | /* Iterate forward over the elements of the list. */ |
82 | #define list_for_each(pos, head) \ |
83 | for (pos = (head)->next; pos != (head); pos = pos->next) |
84 | |
85 | |
86 | /* Iterate forward over the elements of the list. */ |
87 | #define list_for_each_prev(pos, head) \ |
88 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
89 | |
90 | |
91 | /* Iterate backwards over the elements list. The list elements can be |
92 | removed from the list while doing this. */ |
93 | #define list_for_each_prev_safe(pos, p, head) \ |
94 | for (pos = (head)->prev, p = pos->prev; \ |
95 | pos != (head); \ |
96 | pos = p, p = pos->prev) |
97 | |
98 | #endif /* list.h */ |
99 | |