Build system: use SPDX identifiers
[userspace-rcu.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
c7f4490a
MD
28#include <urcu/compiler.h>
29
e6decba5
MD
30/*
31 * The definitions of this file are adopted from those which can be
32 * found in the Linux kernel headers to enable people familiar with the
33 * latter find their way in these sources as well.
34 */
7a50dcf7 35
e6decba5
MD
36/* Basic type for the double-link list. */
37struct cds_list_head {
38 struct cds_list_head *next, *prev;
34cfb3e3 39};
7a50dcf7 40
e6decba5 41/* Define a variable with the head and tail of the list. */
16aa9ee8 42#define CDS_LIST_HEAD(name) \
e6decba5 43 struct cds_list_head name = { &(name), &(name) }
7a50dcf7 44
e6decba5 45/* Initialize a new list head. */
16aa9ee8 46#define CDS_INIT_LIST_HEAD(ptr) \
e6decba5 47 (ptr)->next = (ptr)->prev = (ptr)
7a50dcf7 48
ce4a1f76 49#define CDS_LIST_HEAD_INIT(name) { .next = &(name), .prev = &(name) }
7a50dcf7 50
e6decba5
MD
51/* Add new element at the head of the list. */
52static inline
53void cds_list_add(struct cds_list_head *newp, struct cds_list_head *head)
7a50dcf7 54{
e6decba5
MD
55 head->next->prev = newp;
56 newp->next = head->next;
57 newp->prev = head;
58 head->next = newp;
7a50dcf7
MD
59}
60
e6decba5
MD
61/* Add new element at the tail of the list. */
62static inline
63void cds_list_add_tail(struct cds_list_head *newp, struct cds_list_head *head)
7a50dcf7 64{
e6decba5
MD
65 head->prev->next = newp;
66 newp->next = head;
67 newp->prev = head->prev;
68 head->prev = newp;
7a50dcf7
MD
69}
70
e6decba5
MD
71/* Remove element from list. */
72static inline
73void __cds_list_del(struct cds_list_head *prev, struct cds_list_head *next)
63ff4873 74{
e6decba5
MD
75 next->prev = prev;
76 prev->next = next;
63ff4873
MD
77}
78
e6decba5
MD
79/* Remove element from list. */
80static inline
81void cds_list_del(struct cds_list_head *elem)
7a50dcf7 82{
e6decba5 83 __cds_list_del(elem->prev, elem->next);
7a50dcf7
MD
84}
85
3ec07d9f 86/* Remove element from list, initializing the element's list pointers. */
e6decba5
MD
87static inline
88void cds_list_del_init(struct cds_list_head *elem)
3ec07d9f
PM
89{
90 cds_list_del(elem);
91 CDS_INIT_LIST_HEAD(elem);
92}
93
e6decba5
MD
94/* Delete from list, add to another list as head. */
95static inline
96void cds_list_move(struct cds_list_head *elem, struct cds_list_head *head)
ac956d00 97{
e6decba5
MD
98 __cds_list_del(elem->prev, elem->next);
99 cds_list_add(elem, head);
ac956d00 100}
7a50dcf7 101
e6decba5
MD
102/* Replace an old entry. */
103static inline
104void cds_list_replace(struct cds_list_head *old, struct cds_list_head *_new)
5db941e8
MD
105{
106 _new->next = old->next;
107 _new->prev = old->prev;
108 _new->prev->next = _new;
109 _new->next->prev = _new;
110}
111
e6decba5
MD
112/* Join two lists. */
113static inline
114void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head)
7a50dcf7 115{
e6decba5
MD
116 /* Do nothing if the list which gets added is empty. */
117 if (add != add->next) {
118 add->next->prev = head;
119 add->prev->next = head->next;
120 head->next->prev = add->prev;
121 head->next = add->next;
122 }
7a50dcf7
MD
123}
124
e6decba5 125/* Get typed element from list at a given position. */
c7f4490a 126#define cds_list_entry(ptr, type, member) caa_container_of(ptr, type, member)
7a50dcf7
MD
127
128
58090b34 129/* Get first entry from a list. */
78654362
MD
130#define cds_list_first_entry(ptr, type, member) \
131 cds_list_entry((ptr)->next, type, member)
58090b34 132
e6decba5 133/* Iterate forward over the elements of the list. */
16aa9ee8 134#define cds_list_for_each(pos, head) \
c7f4490a 135 for (pos = (head)->next; (pos) != (head); pos = (pos)->next)
7a50dcf7 136
e6decba5
MD
137/*
138 * Iterate forward over the elements list. The list elements can be
139 * removed from the list while doing this.
140 */
7e5b9a4d 141#define cds_list_for_each_safe(pos, p, head) \
c7f4490a
MD
142 for (pos = (head)->next, p = (pos)->next; \
143 (pos) != (head); \
144 pos = (p), p = (pos)->next)
7a50dcf7 145
e6decba5 146/* Iterate backward over the elements of the list. */
16aa9ee8 147#define cds_list_for_each_prev(pos, head) \
c7f4490a 148 for (pos = (head)->prev; (pos) != (head); pos = (pos)->prev)
7a50dcf7 149
e6decba5
MD
150/*
151 * Iterate backwards over the elements list. The list elements can be
152 * removed from the list while doing this.
153 */
16aa9ee8 154#define cds_list_for_each_prev_safe(pos, p, head) \
c7f4490a
MD
155 for (pos = (head)->prev, p = (pos)->prev; \
156 (pos) != (head); \
157 pos = (p), p = (pos)->prev)
7a50dcf7 158
e6decba5 159#define cds_list_for_each_entry(pos, head, member) \
c7f4490a
MD
160 for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member); \
161 &(pos)->member != (head); \
162 pos = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member))
7a50dcf7 163
e6decba5 164#define cds_list_for_each_entry_reverse(pos, head, member) \
c7f4490a
MD
165 for (pos = cds_list_entry((head)->prev, __typeof__(*(pos)), member); \
166 &(pos)->member != (head); \
167 pos = cds_list_entry((pos)->member.prev, __typeof__(*(pos)), member))
7a50dcf7 168
e6decba5 169#define cds_list_for_each_entry_safe(pos, p, head, member) \
c7f4490a
MD
170 for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member), \
171 p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \
172 &(pos)->member != (head); \
173 pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member))
7a50dcf7 174
16d8b5df
JL
175/*
176 * Same as cds_list_for_each_entry_safe, but starts from "pos" which should
177 * point to an entry within the list.
178 */
179#define cds_list_for_each_entry_safe_from(pos, p, head, member) \
c7f4490a
MD
180 for (p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \
181 &(pos)->member != (head); \
182 pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member))
16d8b5df 183
e6decba5
MD
184static inline
185int cds_list_empty(struct cds_list_head *head)
7a50dcf7
MD
186{
187 return head == head->next;
188}
189
e6decba5
MD
190static inline
191void cds_list_replace_init(struct cds_list_head *old,
192 struct cds_list_head *_new)
7a50dcf7 193{
34cfb3e3 194 struct cds_list_head *head = old->next;
e6decba5 195
16aa9ee8
DG
196 cds_list_del(old);
197 cds_list_add_tail(_new, head);
198 CDS_INIT_LIST_HEAD(old);
7a50dcf7
MD
199}
200
34cfb3e3 201#endif /* _CDS_LIST_H */
This page took 0.056287 seconds and 4 git commands to generate.