48d9bbb9c1b75e6d7024dab59f7ea56179972d19
[babeltrace.git] / src / common / list.h
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 */
20
21 #ifndef _BT_LIST_H
22 #define _BT_LIST_H 1
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
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /* Basic type for the double-link list. */
33 struct bt_list_head
34 {
35 struct bt_list_head *next;
36 struct bt_list_head *prev;
37 };
38
39
40 /* Define a variable with the head and tail of the list. */
41 #define BT_LIST_HEAD(name) \
42 struct bt_list_head name = { &(name), &(name) }
43
44 /* Initialize a new list head. */
45 #define BT_INIT_LIST_HEAD(ptr) \
46 (ptr)->next = (ptr)->prev = (ptr)
47
48 #define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
49
50 /* Add new element at the head of the list. */
51 static inline void
52 bt_list_add (struct bt_list_head *newp, struct bt_list_head *head)
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
63 bt_list_add_tail (struct bt_list_head *newp, struct bt_list_head *head)
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
74 __bt_list_del (struct bt_list_head *prev, struct bt_list_head *next)
75 {
76 next->prev = prev;
77 prev->next = next;
78 }
79
80 /* Remove element from list. */
81 static inline void
82 bt_list_del (struct bt_list_head *elem)
83 {
84 __bt_list_del (elem->prev, elem->next);
85 }
86
87 /* delete from list, add to another list as head */
88 static inline void
89 bt_list_move (struct bt_list_head *elem, struct bt_list_head *head)
90 {
91 __bt_list_del (elem->prev, elem->next);
92 bt_list_add (elem, head);
93 }
94
95 /* replace an old entry.
96 */
97 static inline void
98 bt_list_replace(struct bt_list_head *old, struct bt_list_head *_new)
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
108 bt_list_splice (struct bt_list_head *add, struct bt_list_head *head)
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. */
122 #define bt_list_entry(ptr, type, member) \
123 ((type *) ((char *) (ptr) - (uintptr_t) (&((type *) 0)->member)))
124
125
126
127 /* Iterate forward over the elements of the list. */
128 #define bt_list_for_each(pos, head) \
129 for (pos = (head)->next; pos != (head); pos = pos->next)
130
131
132 /* Iterate forward over the elements of the list. */
133 #define bt_list_for_each_prev(pos, head) \
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. */
139 #define bt_list_for_each_prev_safe(pos, p, head) \
140 for (pos = (head)->prev, p = pos->prev; \
141 pos != (head); \
142 pos = p, p = pos->prev)
143
144 #define bt_list_for_each_entry(pos, head, member) \
145 for (pos = bt_list_entry((head)->next, typeof(*pos), member); \
146 &pos->member != (head); \
147 pos = bt_list_entry(pos->member.next, typeof(*pos), member))
148
149 #define bt_list_for_each_entry_reverse(pos, head, member) \
150 for (pos = bt_list_entry((head)->prev, typeof(*pos), member); \
151 &pos->member != (head); \
152 pos = bt_list_entry(pos->member.prev, typeof(*pos), member))
153
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); \
157 &pos->member != (head); \
158 pos = p, p = bt_list_entry(pos->member.next, typeof(*pos), member))
159
160 static inline int bt_list_empty(struct bt_list_head *head)
161 {
162 return head == head->next;
163 }
164
165 static inline void bt_list_replace_init(struct bt_list_head *old,
166 struct bt_list_head *_new)
167 {
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);
172 }
173
174 #ifdef __cplusplus
175 }
176 #endif
177
178 #endif /* _BT_LIST_H */
This page took 0.035293 seconds and 3 git commands to generate.