Commit | Line | Data |
---|---|---|
eb31c5e6 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: LGPL-2.1-only |
3 | * | |
eb31c5e6 MD |
4 | * Copyright (C) 2002 Free Software Foundation, Inc. |
5 | * This file is part of the GNU C Library. | |
6 | * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
eb31c5e6 | 7 | */ |
8b9d5b5e | 8 | |
3122e6f0 JD |
9 | #ifndef _BT_LIST_H |
10 | #define _BT_LIST_H 1 | |
8b9d5b5e MD |
11 | |
12 | /* The definitions of this file are adopted from those which can be | |
13 | found in the Linux kernel headers to enable people familiar with | |
14 | the latter find their way in these sources as well. */ | |
15 | ||
6946751f JD |
16 | #ifdef __cplusplus |
17 | extern "C" { | |
18 | #endif | |
8b9d5b5e MD |
19 | |
20 | /* Basic type for the double-link list. */ | |
3122e6f0 | 21 | struct bt_list_head |
8b9d5b5e | 22 | { |
3122e6f0 JD |
23 | struct bt_list_head *next; |
24 | struct bt_list_head *prev; | |
f259f07b | 25 | }; |
8b9d5b5e MD |
26 | |
27 | ||
28 | /* Define a variable with the head and tail of the list. */ | |
3122e6f0 JD |
29 | #define BT_LIST_HEAD(name) \ |
30 | struct bt_list_head name = { &(name), &(name) } | |
8b9d5b5e MD |
31 | |
32 | /* Initialize a new list head. */ | |
3122e6f0 | 33 | #define BT_INIT_LIST_HEAD(ptr) \ |
8b9d5b5e MD |
34 | (ptr)->next = (ptr)->prev = (ptr) |
35 | ||
3122e6f0 | 36 | #define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) } |
8b9d5b5e MD |
37 | |
38 | /* Add new element at the head of the list. */ | |
39 | static inline void | |
3122e6f0 | 40 | bt_list_add (struct bt_list_head *newp, struct bt_list_head *head) |
8b9d5b5e MD |
41 | { |
42 | head->next->prev = newp; | |
43 | newp->next = head->next; | |
44 | newp->prev = head; | |
45 | head->next = newp; | |
46 | } | |
47 | ||
48 | ||
49 | /* Add new element at the tail of the list. */ | |
50 | static inline void | |
3122e6f0 | 51 | bt_list_add_tail (struct bt_list_head *newp, struct bt_list_head *head) |
8b9d5b5e MD |
52 | { |
53 | head->prev->next = newp; | |
54 | newp->next = head; | |
55 | newp->prev = head->prev; | |
56 | head->prev = newp; | |
57 | } | |
58 | ||
59 | ||
60 | /* Remove element from list. */ | |
61 | static inline void | |
3122e6f0 | 62 | __bt_list_del (struct bt_list_head *prev, struct bt_list_head *next) |
8b9d5b5e MD |
63 | { |
64 | next->prev = prev; | |
65 | prev->next = next; | |
66 | } | |
67 | ||
68 | /* Remove element from list. */ | |
69 | static inline void | |
3122e6f0 | 70 | bt_list_del (struct bt_list_head *elem) |
8b9d5b5e | 71 | { |
3122e6f0 | 72 | __bt_list_del (elem->prev, elem->next); |
8b9d5b5e MD |
73 | } |
74 | ||
75 | /* delete from list, add to another list as head */ | |
76 | static inline void | |
3122e6f0 | 77 | bt_list_move (struct bt_list_head *elem, struct bt_list_head *head) |
8b9d5b5e | 78 | { |
3122e6f0 JD |
79 | __bt_list_del (elem->prev, elem->next); |
80 | bt_list_add (elem, head); | |
8b9d5b5e MD |
81 | } |
82 | ||
83 | /* replace an old entry. | |
84 | */ | |
85 | static inline void | |
3122e6f0 | 86 | bt_list_replace(struct bt_list_head *old, struct bt_list_head *_new) |
8b9d5b5e MD |
87 | { |
88 | _new->next = old->next; | |
89 | _new->prev = old->prev; | |
90 | _new->prev->next = _new; | |
91 | _new->next->prev = _new; | |
92 | } | |
93 | ||
94 | /* Join two lists. */ | |
95 | static inline void | |
3122e6f0 | 96 | bt_list_splice (struct bt_list_head *add, struct bt_list_head *head) |
8b9d5b5e MD |
97 | { |
98 | /* Do nothing if the list which gets added is empty. */ | |
99 | if (add != add->next) | |
100 | { | |
101 | add->next->prev = head; | |
102 | add->prev->next = head->next; | |
103 | head->next->prev = add->prev; | |
104 | head->next = add->next; | |
105 | } | |
106 | } | |
107 | ||
108 | ||
109 | /* Get typed element from list at a given position. */ | |
3122e6f0 | 110 | #define bt_list_entry(ptr, type, member) \ |
4f0b8c05 | 111 | ((type *) ((char *) (ptr) - (uintptr_t) (&((type *) 0)->member))) |
8b9d5b5e MD |
112 | |
113 | ||
114 | ||
115 | /* Iterate forward over the elements of the list. */ | |
3122e6f0 | 116 | #define bt_list_for_each(pos, head) \ |
8b9d5b5e MD |
117 | for (pos = (head)->next; pos != (head); pos = pos->next) |
118 | ||
119 | ||
120 | /* Iterate forward over the elements of the list. */ | |
3122e6f0 | 121 | #define bt_list_for_each_prev(pos, head) \ |
8b9d5b5e MD |
122 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
123 | ||
124 | ||
125 | /* Iterate backwards over the elements list. The list elements can be | |
126 | removed from the list while doing this. */ | |
3122e6f0 | 127 | #define bt_list_for_each_prev_safe(pos, p, head) \ |
8b9d5b5e MD |
128 | for (pos = (head)->prev, p = pos->prev; \ |
129 | pos != (head); \ | |
130 | pos = p, p = pos->prev) | |
131 | ||
3122e6f0 | 132 | #define bt_list_for_each_entry(pos, head, member) \ |
0ec1ae8b | 133 | for (pos = bt_list_entry((head)->next, __typeof__(*pos), member); \ |
8b9d5b5e | 134 | &pos->member != (head); \ |
0ec1ae8b | 135 | pos = bt_list_entry(pos->member.next, __typeof__(*pos), member)) |
8b9d5b5e | 136 | |
3122e6f0 | 137 | #define bt_list_for_each_entry_reverse(pos, head, member) \ |
0ec1ae8b | 138 | for (pos = bt_list_entry((head)->prev, __typeof__(*pos), member); \ |
8b9d5b5e | 139 | &pos->member != (head); \ |
0ec1ae8b | 140 | pos = bt_list_entry(pos->member.prev, __typeof__(*pos), member)) |
8b9d5b5e | 141 | |
3122e6f0 | 142 | #define bt_list_for_each_entry_safe(pos, p, head, member) \ |
0ec1ae8b SM |
143 | for (pos = bt_list_entry((head)->next, __typeof__(*pos), member), \ |
144 | p = bt_list_entry(pos->member.next, __typeof__(*pos), member); \ | |
8b9d5b5e | 145 | &pos->member != (head); \ |
0ec1ae8b | 146 | pos = p, p = bt_list_entry(pos->member.next, __typeof__(*pos), member)) |
8b9d5b5e | 147 | |
3122e6f0 | 148 | static inline int bt_list_empty(struct bt_list_head *head) |
8b9d5b5e MD |
149 | { |
150 | return head == head->next; | |
151 | } | |
152 | ||
3122e6f0 JD |
153 | static inline void bt_list_replace_init(struct bt_list_head *old, |
154 | struct bt_list_head *_new) | |
8b9d5b5e | 155 | { |
3122e6f0 JD |
156 | struct bt_list_head *head = old->next; |
157 | bt_list_del(old); | |
158 | bt_list_add_tail(_new, head); | |
159 | BT_INIT_LIST_HEAD(old); | |
8b9d5b5e MD |
160 | } |
161 | ||
6946751f JD |
162 | #ifdef __cplusplus |
163 | } | |
164 | #endif | |
165 | ||
3122e6f0 | 166 | #endif /* _BT_LIST_H */ |