2 * SPDX-FileCopyrightText: 2002 Free Software Foundation, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-only
5 * This file is part of the GNU C Library.
6 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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. */
20 /* Basic type for the double-link list. */
23 struct bt_list_head
*next
;
24 struct bt_list_head
*prev
;
28 /* Define a variable with the head and tail of the list. */
29 #define BT_LIST_HEAD(name) \
30 struct bt_list_head name = { &(name), &(name) }
32 /* Initialize a new list head. */
33 #define BT_INIT_LIST_HEAD(ptr) \
34 (ptr)->next = (ptr)->prev = (ptr)
36 #define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
38 /* Add new element at the head of the list. */
40 bt_list_add (struct bt_list_head
*newp
, struct bt_list_head
*head
)
42 head
->next
->prev
= newp
;
43 newp
->next
= head
->next
;
49 /* Add new element at the tail of the list. */
51 bt_list_add_tail (struct bt_list_head
*newp
, struct bt_list_head
*head
)
53 head
->prev
->next
= newp
;
55 newp
->prev
= head
->prev
;
60 /* Remove element from list. */
62 __bt_list_del (struct bt_list_head
*prev
, struct bt_list_head
*next
)
68 /* Remove element from list. */
70 bt_list_del (struct bt_list_head
*elem
)
72 __bt_list_del (elem
->prev
, elem
->next
);
75 /* delete from list, add to another list as head */
77 bt_list_move (struct bt_list_head
*elem
, struct bt_list_head
*head
)
79 __bt_list_del (elem
->prev
, elem
->next
);
80 bt_list_add (elem
, head
);
83 /* replace an old entry.
86 bt_list_replace(struct bt_list_head
*old
, struct bt_list_head
*_new
)
88 _new
->next
= old
->next
;
89 _new
->prev
= old
->prev
;
90 _new
->prev
->next
= _new
;
91 _new
->next
->prev
= _new
;
96 bt_list_splice (struct bt_list_head
*add
, struct bt_list_head
*head
)
98 /* Do nothing if the list which gets added is empty. */
101 add
->next
->prev
= head
;
102 add
->prev
->next
= head
->next
;
103 head
->next
->prev
= add
->prev
;
104 head
->next
= add
->next
;
109 /* Get typed element from list at a given position. */
110 #define bt_list_entry(ptr, type, member) \
111 ((type *) ((char *) (ptr) - (uintptr_t) (&((type *) 0)->member)))
115 /* Iterate forward over the elements of the list. */
116 #define bt_list_for_each(pos, head) \
117 for (pos = (head)->next; pos != (head); pos = pos->next)
120 /* Iterate forward over the elements of the list. */
121 #define bt_list_for_each_prev(pos, head) \
122 for (pos = (head)->prev; pos != (head); pos = pos->prev)
125 /* Iterate backwards over the elements list. The list elements can be
126 removed from the list while doing this. */
127 #define bt_list_for_each_prev_safe(pos, p, head) \
128 for (pos = (head)->prev, p = pos->prev; \
130 pos = p, p = pos->prev)
132 #define bt_list_for_each_entry(pos, head, member) \
133 for (pos = bt_list_entry((head)->next, __typeof__(*pos), member); \
134 &pos->member != (head); \
135 pos = bt_list_entry(pos->member.next, __typeof__(*pos), member))
137 #define bt_list_for_each_entry_reverse(pos, head, member) \
138 for (pos = bt_list_entry((head)->prev, __typeof__(*pos), member); \
139 &pos->member != (head); \
140 pos = bt_list_entry(pos->member.prev, __typeof__(*pos), member))
142 #define bt_list_for_each_entry_safe(pos, p, head, member) \
143 for (pos = bt_list_entry((head)->next, __typeof__(*pos), member), \
144 p = bt_list_entry(pos->member.next, __typeof__(*pos), member); \
145 &pos->member != (head); \
146 pos = p, p = bt_list_entry(pos->member.next, __typeof__(*pos), member))
148 static inline int bt_list_empty(struct bt_list_head
*head
)
150 return head
== head
->next
;
153 static inline void bt_list_replace_init(struct bt_list_head
*old
,
154 struct bt_list_head
*_new
)
156 struct bt_list_head
*head
= old
->next
;
158 bt_list_add_tail(_new
, head
);
159 BT_INIT_LIST_HEAD(old
);
166 #endif /* _BT_LIST_H */
This page took 0.052049 seconds and 4 git commands to generate.