Fix: check for rand_r() in compat-rand.h
[urcu.git] / include / urcu / list.h
CommitLineData
a00718e7
MD
1/*
2 * Copyright (C) 2002 Free Software Foundation, Inc.
3 * (originally part of the GNU C Library)
4 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 *
6 * Copyright (C) 2009 Pierre-Marc Fournier
7 * Conversion to RCU list.
8 * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
7a50dcf7 24
34cfb3e3
MD
25#ifndef _CDS_LIST_H
26#define _CDS_LIST_H 1
7a50dcf7 27
e6decba5
MD
28/*
29 * The definitions of this file are adopted from those which can be
30 * found in the Linux kernel headers to enable people familiar with the
31 * latter find their way in these sources as well.
32 */
7a50dcf7 33
e6decba5
MD
34/* Basic type for the double-link list. */
35struct cds_list_head {
36 struct cds_list_head *next, *prev;
34cfb3e3 37};
7a50dcf7 38
e6decba5 39/* Define a variable with the head and tail of the list. */
16aa9ee8 40#define CDS_LIST_HEAD(name) \
e6decba5 41 struct cds_list_head name = { &(name), &(name) }
7a50dcf7 42
e6decba5 43/* Initialize a new list head. */
16aa9ee8 44#define CDS_INIT_LIST_HEAD(ptr) \
e6decba5 45 (ptr)->next = (ptr)->prev = (ptr)
7a50dcf7 46
16aa9ee8 47#define CDS_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
7a50dcf7 48
e6decba5
MD
49/* Add new element at the head of the list. */
50static inline
51void cds_list_add(struct cds_list_head *newp, struct cds_list_head *head)
7a50dcf7 52{
e6decba5
MD
53 head->next->prev = newp;
54 newp->next = head->next;
55 newp->prev = head;
56 head->next = newp;
7a50dcf7
MD
57}
58
e6decba5
MD
59/* Add new element at the tail of the list. */
60static inline
61void cds_list_add_tail(struct cds_list_head *newp, struct cds_list_head *head)
7a50dcf7 62{
e6decba5
MD
63 head->prev->next = newp;
64 newp->next = head;
65 newp->prev = head->prev;
66 head->prev = newp;
7a50dcf7
MD
67}
68
e6decba5
MD
69/* Remove element from list. */
70static inline
71void __cds_list_del(struct cds_list_head *prev, struct cds_list_head *next)
63ff4873 72{
e6decba5
MD
73 next->prev = prev;
74 prev->next = next;
63ff4873
MD
75}
76
e6decba5
MD
77/* Remove element from list. */
78static inline
79void cds_list_del(struct cds_list_head *elem)
7a50dcf7 80{
e6decba5 81 __cds_list_del(elem->prev, elem->next);
7a50dcf7
MD
82}
83
3ec07d9f 84/* Remove element from list, initializing the element's list pointers. */
e6decba5
MD
85static inline
86void cds_list_del_init(struct cds_list_head *elem)
3ec07d9f
PM
87{
88 cds_list_del(elem);
89 CDS_INIT_LIST_HEAD(elem);
90}
91
e6decba5
MD
92/* Delete from list, add to another list as head. */
93static inline
94void cds_list_move(struct cds_list_head *elem, struct cds_list_head *head)
ac956d00 95{
e6decba5
MD
96 __cds_list_del(elem->prev, elem->next);
97 cds_list_add(elem, head);
ac956d00 98}
7a50dcf7 99
e6decba5
MD
100/* Replace an old entry. */
101static inline
102void cds_list_replace(struct cds_list_head *old, struct cds_list_head *_new)
5db941e8
MD
103{
104 _new->next = old->next;
105 _new->prev = old->prev;
106 _new->prev->next = _new;
107 _new->next->prev = _new;
108}
109
e6decba5
MD
110/* Join two lists. */
111static inline
112void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head)
7a50dcf7 113{
e6decba5
MD
114 /* Do nothing if the list which gets added is empty. */
115 if (add != add->next) {
116 add->next->prev = head;
117 add->prev->next = head->next;
118 head->next->prev = add->prev;
119 head->next = add->next;
120 }
7a50dcf7
MD
121}
122
e6decba5 123/* Get typed element from list at a given position. */
16aa9ee8 124#define cds_list_entry(ptr, type, member) \
e6decba5 125 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
7a50dcf7
MD
126
127
58090b34 128/* Get first entry from a list. */
78654362
MD
129#define cds_list_first_entry(ptr, type, member) \
130 cds_list_entry((ptr)->next, type, member)
58090b34 131
e6decba5 132/* Iterate forward over the elements of the list. */
16aa9ee8 133#define cds_list_for_each(pos, head) \
e6decba5 134 for (pos = (head)->next; pos != (head); pos = pos->next)
7a50dcf7 135
e6decba5
MD
136/*
137 * Iterate forward over the elements list. The list elements can be
138 * removed from the list while doing this.
139 */
7e5b9a4d 140#define cds_list_for_each_safe(pos, p, head) \
e6decba5
MD
141 for (pos = (head)->next, p = pos->next; \
142 pos != (head); \
143 pos = p, p = pos->next)
7a50dcf7 144
e6decba5 145/* Iterate backward over the elements of the list. */
16aa9ee8 146#define cds_list_for_each_prev(pos, head) \
e6decba5 147 for (pos = (head)->prev; pos != (head); pos = pos->prev)
7a50dcf7 148
e6decba5
MD
149/*
150 * Iterate backwards over the elements list. The list elements can be
151 * removed from the list while doing this.
152 */
16aa9ee8 153#define cds_list_for_each_prev_safe(pos, p, head) \
e6decba5
MD
154 for (pos = (head)->prev, p = pos->prev; \
155 pos != (head); \
156 pos = p, p = pos->prev)
7a50dcf7 157
e6decba5 158#define cds_list_for_each_entry(pos, head, member) \
bdffa73a 159 for (pos = cds_list_entry((head)->next, __typeof__(*pos), member); \
e6decba5
MD
160 &pos->member != (head); \
161 pos = cds_list_entry(pos->member.next, __typeof__(*pos), member))
7a50dcf7 162
e6decba5 163#define cds_list_for_each_entry_reverse(pos, head, member) \
bdffa73a 164 for (pos = cds_list_entry((head)->prev, __typeof__(*pos), member); \
e6decba5
MD
165 &pos->member != (head); \
166 pos = cds_list_entry(pos->member.prev, __typeof__(*pos), member))
7a50dcf7 167
e6decba5 168#define cds_list_for_each_entry_safe(pos, p, head, member) \
bdffa73a 169 for (pos = cds_list_entry((head)->next, __typeof__(*pos), member), \
e6decba5
MD
170 p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \
171 &pos->member != (head); \
172 pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member))
7a50dcf7 173
16d8b5df
JL
174/*
175 * Same as cds_list_for_each_entry_safe, but starts from "pos" which should
176 * point to an entry within the list.
177 */
178#define cds_list_for_each_entry_safe_from(pos, p, head, member) \
179 for (p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \
180 &pos->member != (head); \
181 pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member))
182
e6decba5
MD
183static inline
184int cds_list_empty(struct cds_list_head *head)
7a50dcf7
MD
185{
186 return head == head->next;
187}
188
e6decba5
MD
189static inline
190void cds_list_replace_init(struct cds_list_head *old,
191 struct cds_list_head *_new)
7a50dcf7 192{
34cfb3e3 193 struct cds_list_head *head = old->next;
e6decba5 194
16aa9ee8
DG
195 cds_list_del(old);
196 cds_list_add_tail(_new, head);
197 CDS_INIT_LIST_HEAD(old);
7a50dcf7
MD
198}
199
34cfb3e3 200#endif /* _CDS_LIST_H */
This page took 0.044673 seconds and 4 git commands to generate.