Fix: update missing copyrights and ifdef protection mismatch
[babeltrace.git] / include / babeltrace / 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
29 /* Basic type for the double-link list. */
30 struct bt_list_head
31 {
32 struct bt_list_head *next;
33 struct bt_list_head *prev;
34 };
35
36
37 /* Define a variable with the head and tail of the list. */
38 #define BT_LIST_HEAD(name) \
39 struct bt_list_head name = { &(name), &(name) }
40
41 /* Initialize a new list head. */
42 #define BT_INIT_LIST_HEAD(ptr) \
43 (ptr)->next = (ptr)->prev = (ptr)
44
45 #define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
46
47 /* Add new element at the head of the list. */
48 static inline void
49 bt_list_add (struct bt_list_head *newp, struct bt_list_head *head)
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. */
59 static inline void
60 bt_list_add_tail (struct bt_list_head *newp, struct bt_list_head *head)
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. */
70 static inline void
71 __bt_list_del (struct bt_list_head *prev, struct bt_list_head *next)
72 {
73 next->prev = prev;
74 prev->next = next;
75 }
76
77 /* Remove element from list. */
78 static inline void
79 bt_list_del (struct bt_list_head *elem)
80 {
81 __bt_list_del (elem->prev, elem->next);
82 }
83
84 /* delete from list, add to another list as head */
85 static inline void
86 bt_list_move (struct bt_list_head *elem, struct bt_list_head *head)
87 {
88 __bt_list_del (elem->prev, elem->next);
89 bt_list_add (elem, head);
90 }
91
92 /* replace an old entry.
93 */
94 static inline void
95 bt_list_replace(struct bt_list_head *old, struct bt_list_head *_new)
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. */
104 static inline void
105 bt_list_splice (struct bt_list_head *add, struct bt_list_head *head)
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. */
119 #define bt_list_entry(ptr, type, member) \
120 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
121
122
123
124 /* Iterate forward over the elements of the list. */
125 #define bt_list_for_each(pos, head) \
126 for (pos = (head)->next; pos != (head); pos = pos->next)
127
128
129 /* Iterate forward over the elements of the list. */
130 #define bt_list_for_each_prev(pos, head) \
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. */
136 #define bt_list_for_each_prev_safe(pos, p, head) \
137 for (pos = (head)->prev, p = pos->prev; \
138 pos != (head); \
139 pos = p, p = pos->prev)
140
141 #define bt_list_for_each_entry(pos, head, member) \
142 for (pos = bt_list_entry((head)->next, typeof(*pos), member); \
143 &pos->member != (head); \
144 pos = bt_list_entry(pos->member.next, typeof(*pos), member))
145
146 #define bt_list_for_each_entry_reverse(pos, head, member) \
147 for (pos = bt_list_entry((head)->prev, typeof(*pos), member); \
148 &pos->member != (head); \
149 pos = bt_list_entry(pos->member.prev, typeof(*pos), member))
150
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); \
154 &pos->member != (head); \
155 pos = p, p = bt_list_entry(pos->member.next, typeof(*pos), member))
156
157 static inline int bt_list_empty(struct bt_list_head *head)
158 {
159 return head == head->next;
160 }
161
162 static inline void bt_list_replace_init(struct bt_list_head *old,
163 struct bt_list_head *_new)
164 {
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);
169 }
170
171 #endif /* _BT_LIST_H */
This page took 0.046699 seconds and 4 git commands to generate.