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