Fix trace-collection.h: No such file or directory that build code with libbabeltrace
[babeltrace.git] / include / babeltrace / list.h
CommitLineData
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
28
29/* Basic type for the double-link list. */
3122e6f0 30struct bt_list_head
8b9d5b5e 31{
3122e6f0
JD
32 struct bt_list_head *next;
33 struct bt_list_head *prev;
f259f07b 34};
8b9d5b5e
MD
35
36
37/* Define a variable with the head and tail of the list. */
3122e6f0
JD
38#define BT_LIST_HEAD(name) \
39 struct bt_list_head name = { &(name), &(name) }
8b9d5b5e
MD
40
41/* Initialize a new list head. */
3122e6f0 42#define BT_INIT_LIST_HEAD(ptr) \
8b9d5b5e
MD
43 (ptr)->next = (ptr)->prev = (ptr)
44
3122e6f0 45#define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
8b9d5b5e
MD
46
47/* Add new element at the head of the list. */
48static inline void
3122e6f0 49bt_list_add (struct bt_list_head *newp, struct bt_list_head *head)
8b9d5b5e
MD
50{
51 head->next->prev = newp;
52 newp->next = head->next;
53 newp->prev = head;
54 head->next = newp;
55}
56
57
58/* Add new element at the tail of the list. */
59static inline void
3122e6f0 60bt_list_add_tail (struct bt_list_head *newp, struct bt_list_head *head)
8b9d5b5e
MD
61{
62 head->prev->next = newp;
63 newp->next = head;
64 newp->prev = head->prev;
65 head->prev = newp;
66}
67
68
69/* Remove element from list. */
70static inline void
3122e6f0 71__bt_list_del (struct bt_list_head *prev, struct bt_list_head *next)
8b9d5b5e
MD
72{
73 next->prev = prev;
74 prev->next = next;
75}
76
77/* Remove element from list. */
78static inline void
3122e6f0 79bt_list_del (struct bt_list_head *elem)
8b9d5b5e 80{
3122e6f0 81 __bt_list_del (elem->prev, elem->next);
8b9d5b5e
MD
82}
83
84/* delete from list, add to another list as head */
85static inline void
3122e6f0 86bt_list_move (struct bt_list_head *elem, struct bt_list_head *head)
8b9d5b5e 87{
3122e6f0
JD
88 __bt_list_del (elem->prev, elem->next);
89 bt_list_add (elem, head);
8b9d5b5e
MD
90}
91
92/* replace an old entry.
93 */
94static inline void
3122e6f0 95bt_list_replace(struct bt_list_head *old, struct bt_list_head *_new)
8b9d5b5e
MD
96{
97 _new->next = old->next;
98 _new->prev = old->prev;
99 _new->prev->next = _new;
100 _new->next->prev = _new;
101}
102
103/* Join two lists. */
104static inline void
3122e6f0 105bt_list_splice (struct bt_list_head *add, struct bt_list_head *head)
8b9d5b5e
MD
106{
107 /* Do nothing if the list which gets added is empty. */
108 if (add != add->next)
109 {
110 add->next->prev = head;
111 add->prev->next = head->next;
112 head->next->prev = add->prev;
113 head->next = add->next;
114 }
115}
116
117
118/* Get typed element from list at a given position. */
3122e6f0 119#define bt_list_entry(ptr, type, member) \
8b9d5b5e
MD
120 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
121
122
123
124/* Iterate forward over the elements of the list. */
3122e6f0 125#define bt_list_for_each(pos, head) \
8b9d5b5e
MD
126 for (pos = (head)->next; pos != (head); pos = pos->next)
127
128
129/* Iterate forward over the elements of the list. */
3122e6f0 130#define bt_list_for_each_prev(pos, head) \
8b9d5b5e
MD
131 for (pos = (head)->prev; pos != (head); pos = pos->prev)
132
133
134/* Iterate backwards over the elements list. The list elements can be
135 removed from the list while doing this. */
3122e6f0 136#define bt_list_for_each_prev_safe(pos, p, head) \
8b9d5b5e
MD
137 for (pos = (head)->prev, p = pos->prev; \
138 pos != (head); \
139 pos = p, p = pos->prev)
140
3122e6f0
JD
141#define bt_list_for_each_entry(pos, head, member) \
142 for (pos = bt_list_entry((head)->next, typeof(*pos), member); \
8b9d5b5e 143 &pos->member != (head); \
3122e6f0 144 pos = bt_list_entry(pos->member.next, typeof(*pos), member))
8b9d5b5e 145
3122e6f0
JD
146#define bt_list_for_each_entry_reverse(pos, head, member) \
147 for (pos = bt_list_entry((head)->prev, typeof(*pos), member); \
8b9d5b5e 148 &pos->member != (head); \
3122e6f0 149 pos = bt_list_entry(pos->member.prev, typeof(*pos), member))
8b9d5b5e 150
3122e6f0
JD
151#define bt_list_for_each_entry_safe(pos, p, head, member) \
152 for (pos = bt_list_entry((head)->next, typeof(*pos), member), \
153 p = bt_list_entry(pos->member.next,typeof(*pos), member); \
8b9d5b5e 154 &pos->member != (head); \
3122e6f0 155 pos = p, p = bt_list_entry(pos->member.next, typeof(*pos), member))
8b9d5b5e 156
3122e6f0 157static inline int bt_list_empty(struct bt_list_head *head)
8b9d5b5e
MD
158{
159 return head == head->next;
160}
161
3122e6f0
JD
162static inline void bt_list_replace_init(struct bt_list_head *old,
163 struct bt_list_head *_new)
8b9d5b5e 164{
3122e6f0
JD
165 struct bt_list_head *head = old->next;
166 bt_list_del(old);
167 bt_list_add_tail(_new, head);
168 BT_INIT_LIST_HEAD(old);
8b9d5b5e
MD
169}
170
3122e6f0 171#endif /* _BT_LIST_H */
This page took 0.033001 seconds and 4 git commands to generate.