Move to kernel style SPDX license identifiers
[babeltrace.git] / src / common / list.h
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2002 Free Software Foundation, Inc.
5 * This file is part of the GNU C Library.
6 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
7 */
8
9#ifndef _BT_LIST_H
10#define _BT_LIST_H 1
11
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. */
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20/* Basic type for the double-link list. */
21struct bt_list_head
22{
23 struct bt_list_head *next;
24 struct bt_list_head *prev;
25};
26
27
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) }
31
32/* Initialize a new list head. */
33#define BT_INIT_LIST_HEAD(ptr) \
34 (ptr)->next = (ptr)->prev = (ptr)
35
36#define BT_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
37
38/* Add new element at the head of the list. */
39static inline void
40bt_list_add (struct bt_list_head *newp, struct bt_list_head *head)
41{
42 head->next->prev = newp;
43 newp->next = head->next;
44 newp->prev = head;
45 head->next = newp;
46}
47
48
49/* Add new element at the tail of the list. */
50static inline void
51bt_list_add_tail (struct bt_list_head *newp, struct bt_list_head *head)
52{
53 head->prev->next = newp;
54 newp->next = head;
55 newp->prev = head->prev;
56 head->prev = newp;
57}
58
59
60/* Remove element from list. */
61static inline void
62__bt_list_del (struct bt_list_head *prev, struct bt_list_head *next)
63{
64 next->prev = prev;
65 prev->next = next;
66}
67
68/* Remove element from list. */
69static inline void
70bt_list_del (struct bt_list_head *elem)
71{
72 __bt_list_del (elem->prev, elem->next);
73}
74
75/* delete from list, add to another list as head */
76static inline void
77bt_list_move (struct bt_list_head *elem, struct bt_list_head *head)
78{
79 __bt_list_del (elem->prev, elem->next);
80 bt_list_add (elem, head);
81}
82
83/* replace an old entry.
84 */
85static inline void
86bt_list_replace(struct bt_list_head *old, struct bt_list_head *_new)
87{
88 _new->next = old->next;
89 _new->prev = old->prev;
90 _new->prev->next = _new;
91 _new->next->prev = _new;
92}
93
94/* Join two lists. */
95static inline void
96bt_list_splice (struct bt_list_head *add, struct bt_list_head *head)
97{
98 /* Do nothing if the list which gets added is empty. */
99 if (add != add->next)
100 {
101 add->next->prev = head;
102 add->prev->next = head->next;
103 head->next->prev = add->prev;
104 head->next = add->next;
105 }
106}
107
108
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)))
112
113
114
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)
118
119
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)
123
124
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; \
129 pos != (head); \
130 pos = p, p = pos->prev)
131
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))
136
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))
141
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))
147
148static inline int bt_list_empty(struct bt_list_head *head)
149{
150 return head == head->next;
151}
152
153static inline void bt_list_replace_init(struct bt_list_head *old,
154 struct bt_list_head *_new)
155{
156 struct bt_list_head *head = old->next;
157 bt_list_del(old);
158 bt_list_add_tail(_new, head);
159 BT_INIT_LIST_HEAD(old);
160}
161
162#ifdef __cplusplus
163}
164#endif
165
166#endif /* _BT_LIST_H */
This page took 0.023615 seconds and 4 git commands to generate.