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