]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2002-2024 Free Software Foundation, Inc. |
76a50749 | 2 | This file is part of the GNU C Library. |
76a50749 UD |
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 | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
17 | |
18 | #ifndef _LIST_H | |
500b3a49 | 19 | #define _LIST_H 1 |
7a775e6b | 20 | |
500b3a49 | 21 | /* Internal: doubly linked lists. */ |
76a50749 | 22 | |
76a50749 UD |
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 | ||
500b3a49 ZW |
27 | #include <list_t.h> |
28 | #include <atomic.h> | |
76a50749 UD |
29 | |
30 | /* Define a variable with the head and tail of the list. */ | |
500b3a49 | 31 | #define LIST_HEAD(name) \ |
76a50749 UD |
32 | list_t name = { &(name), &(name) } |
33 | ||
34 | /* Initialize a new list head. */ | |
500b3a49 | 35 | #define INIT_LIST_HEAD(ptr) \ |
76a50749 UD |
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 | { | |
76a50749 UD |
43 | newp->next = head->next; |
44 | newp->prev = head; | |
f25c7b08 | 45 | head->next->prev = newp; |
7a775e6b | 46 | atomic_write_barrier (); |
76a50749 UD |
47 | head->next = newp; |
48 | } | |
49 | ||
50 | ||
76a50749 UD |
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. */ | |
500b3a49 | 76 | #define list_entry(ptr, type, member) \ |
76a50749 UD |
77 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
78 | ||
79 | ||
80 | ||
81 | /* Iterate forward over the elements of the list. */ | |
500b3a49 | 82 | #define list_for_each(pos, head) \ |
76a50749 UD |
83 | for (pos = (head)->next; pos != (head); pos = pos->next) |
84 | ||
85 | ||
86 | /* Iterate forward over the elements of the list. */ | |
500b3a49 | 87 | #define list_for_each_prev(pos, head) \ |
76a50749 UD |
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. */ | |
500b3a49 | 93 | #define list_for_each_prev_safe(pos, p, head) \ |
76a50749 UD |
94 | for (pos = (head)->prev, p = pos->prev; \ |
95 | pos != (head); \ | |
96 | pos = p, p = pos->prev) | |
97 | ||
98 | #endif /* list.h */ |