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