xref: /dflybsd-src/contrib/lvm2/dist/lib/datastruct/list.h (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$NetBSD: list.h,v 1.1.1.1 2008/12/22 00:17:54 haad Exp $	*/
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5*86d7f5d3SJohn Marino  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6*86d7f5d3SJohn Marino  *
7*86d7f5d3SJohn Marino  * This file is part of LVM2.
8*86d7f5d3SJohn Marino  *
9*86d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
10*86d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
11*86d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
12*86d7f5d3SJohn Marino  *
13*86d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
14*86d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
15*86d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16*86d7f5d3SJohn Marino  */
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino #ifndef _LVM_LIST_H
19*86d7f5d3SJohn Marino #define _LVM_LIST_H
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #include <assert.h>
22*86d7f5d3SJohn Marino 
23*86d7f5d3SJohn Marino /*
24*86d7f5d3SJohn Marino  * A list consists of a list head plus elements.
25*86d7f5d3SJohn Marino  * Each element has 'next' and 'previous' pointers.
26*86d7f5d3SJohn Marino  * The list head's pointers point to the first and the last element.
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino struct dm_list {
30*86d7f5d3SJohn Marino 	struct dm_list *n, *p;
31*86d7f5d3SJohn Marino };
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino /*
34*86d7f5d3SJohn Marino  * Initialise a list before use.
35*86d7f5d3SJohn Marino  * The list head's next and previous pointers point back to itself.
36*86d7f5d3SJohn Marino  */
37*86d7f5d3SJohn Marino #define DM_LIST_INIT(name)	struct dm_list name = { &(name), &(name) }
38*86d7f5d3SJohn Marino void dm_list_init(struct dm_list *head);
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino /*
41*86d7f5d3SJohn Marino  * Insert an element before 'head'.
42*86d7f5d3SJohn Marino  * If 'head' is the list head, this adds an element to the end of the list.
43*86d7f5d3SJohn Marino  */
44*86d7f5d3SJohn Marino void dm_list_add(struct dm_list *head, struct dm_list *elem);
45*86d7f5d3SJohn Marino 
46*86d7f5d3SJohn Marino /*
47*86d7f5d3SJohn Marino  * Insert an element after 'head'.
48*86d7f5d3SJohn Marino  * If 'head' is the list head, this adds an element to the front of the list.
49*86d7f5d3SJohn Marino  */
50*86d7f5d3SJohn Marino void dm_list_add_h(struct dm_list *head, struct dm_list *elem);
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino /*
53*86d7f5d3SJohn Marino  * Delete an element from its list.
54*86d7f5d3SJohn Marino  * Note that this doesn't change the element itself - it may still be safe
55*86d7f5d3SJohn Marino  * to follow its pointers.
56*86d7f5d3SJohn Marino  */
57*86d7f5d3SJohn Marino void dm_list_del(struct dm_list *elem);
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino /*
60*86d7f5d3SJohn Marino  * Remove an element from existing list and insert before 'head'.
61*86d7f5d3SJohn Marino  */
62*86d7f5d3SJohn Marino void dm_list_move(struct dm_list *head, struct dm_list *elem);
63*86d7f5d3SJohn Marino 
64*86d7f5d3SJohn Marino /*
65*86d7f5d3SJohn Marino  * Is the list empty?
66*86d7f5d3SJohn Marino  */
67*86d7f5d3SJohn Marino int dm_list_empty(const struct dm_list *head);
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino /*
70*86d7f5d3SJohn Marino  * Is this the first element of the list?
71*86d7f5d3SJohn Marino  */
72*86d7f5d3SJohn Marino int dm_list_start(const struct dm_list *head, const struct dm_list *elem);
73*86d7f5d3SJohn Marino 
74*86d7f5d3SJohn Marino /*
75*86d7f5d3SJohn Marino  * Is this the last element of the list?
76*86d7f5d3SJohn Marino  */
77*86d7f5d3SJohn Marino int dm_list_end(const struct dm_list *head, const struct dm_list *elem);
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino /*
80*86d7f5d3SJohn Marino  * Return first element of the list or NULL if empty
81*86d7f5d3SJohn Marino  */
82*86d7f5d3SJohn Marino struct dm_list *dm_list_first(const struct dm_list *head);
83*86d7f5d3SJohn Marino 
84*86d7f5d3SJohn Marino /*
85*86d7f5d3SJohn Marino  * Return last element of the list or NULL if empty
86*86d7f5d3SJohn Marino  */
87*86d7f5d3SJohn Marino struct dm_list *dm_list_last(const struct dm_list *head);
88*86d7f5d3SJohn Marino 
89*86d7f5d3SJohn Marino /*
90*86d7f5d3SJohn Marino  * Return the previous element of the list, or NULL if we've reached the start.
91*86d7f5d3SJohn Marino  */
92*86d7f5d3SJohn Marino struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem);
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino /*
95*86d7f5d3SJohn Marino  * Return the next element of the list, or NULL if we've reached the end.
96*86d7f5d3SJohn Marino  */
97*86d7f5d3SJohn Marino struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem);
98*86d7f5d3SJohn Marino 
99*86d7f5d3SJohn Marino /*
100*86d7f5d3SJohn Marino  * Given the address v of an instance of 'struct dm_list' called 'head'
101*86d7f5d3SJohn Marino  * contained in a structure of type t, return the containing structure.
102*86d7f5d3SJohn Marino  */
103*86d7f5d3SJohn Marino #define dm_list_struct_base(v, t, head) \
104*86d7f5d3SJohn Marino     ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->head))
105*86d7f5d3SJohn Marino 
106*86d7f5d3SJohn Marino /*
107*86d7f5d3SJohn Marino  * Given the address v of an instance of 'struct dm_list list' contained in
108*86d7f5d3SJohn Marino  * a structure of type t, return the containing structure.
109*86d7f5d3SJohn Marino  */
110*86d7f5d3SJohn Marino #define dm_list_item(v, t) dm_list_struct_base((v), t, list)
111*86d7f5d3SJohn Marino 
112*86d7f5d3SJohn Marino /*
113*86d7f5d3SJohn Marino  * Given the address v of one known element e in a known structure of type t,
114*86d7f5d3SJohn Marino  * return another element f.
115*86d7f5d3SJohn Marino  */
116*86d7f5d3SJohn Marino #define dm_struct_field(v, t, e, f) \
117*86d7f5d3SJohn Marino     (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
118*86d7f5d3SJohn Marino 
119*86d7f5d3SJohn Marino /*
120*86d7f5d3SJohn Marino  * Given the address v of a known element e in a known structure of type t,
121*86d7f5d3SJohn Marino  * return the list head 'list'
122*86d7f5d3SJohn Marino  */
123*86d7f5d3SJohn Marino #define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
124*86d7f5d3SJohn Marino 
125*86d7f5d3SJohn Marino /*
126*86d7f5d3SJohn Marino  * Set v to each element of a list in turn.
127*86d7f5d3SJohn Marino  */
128*86d7f5d3SJohn Marino #define dm_list_iterate(v, head) \
129*86d7f5d3SJohn Marino 	for (v = (head)->n; v != head; v = v->n)
130*86d7f5d3SJohn Marino 
131*86d7f5d3SJohn Marino /*
132*86d7f5d3SJohn Marino  * Set v to each element in a list in turn, starting from the element
133*86d7f5d3SJohn Marino  * in front of 'start'.
134*86d7f5d3SJohn Marino  * You can use this to 'unwind' a list_iterate and back out actions on
135*86d7f5d3SJohn Marino  * already-processed elements.
136*86d7f5d3SJohn Marino  * If 'start' is 'head' it walks the list backwards.
137*86d7f5d3SJohn Marino  */
138*86d7f5d3SJohn Marino #define dm_list_uniterate(v, head, start) \
139*86d7f5d3SJohn Marino 	for (v = (start)->p; v != head; v = v->p)
140*86d7f5d3SJohn Marino 
141*86d7f5d3SJohn Marino /*
142*86d7f5d3SJohn Marino  * A safe way to walk a list and delete and free some elements along
143*86d7f5d3SJohn Marino  * the way.
144*86d7f5d3SJohn Marino  * t must be defined as a temporary variable of the same type as v.
145*86d7f5d3SJohn Marino  */
146*86d7f5d3SJohn Marino #define dm_list_iterate_safe(v, t, head) \
147*86d7f5d3SJohn Marino 	for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
148*86d7f5d3SJohn Marino 
149*86d7f5d3SJohn Marino /*
150*86d7f5d3SJohn Marino  * Walk a list, setting 'v' in turn to the containing structure of each item.
151*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
152*86d7f5d3SJohn Marino  * The 'struct dm_list' variable within the containing structure is 'field'.
153*86d7f5d3SJohn Marino  */
154*86d7f5d3SJohn Marino #define dm_list_iterate_items_gen(v, head, field) \
155*86d7f5d3SJohn Marino 	for (v = dm_list_struct_base((head)->n, typeof(*v), field); \
156*86d7f5d3SJohn Marino 	     &v->field != (head); \
157*86d7f5d3SJohn Marino 	     v = dm_list_struct_base(v->field.n, typeof(*v), field))
158*86d7f5d3SJohn Marino 
159*86d7f5d3SJohn Marino /*
160*86d7f5d3SJohn Marino  * Walk a list, setting 'v' in turn to the containing structure of each item.
161*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
162*86d7f5d3SJohn Marino  * The list should be 'struct dm_list list' within the containing structure.
163*86d7f5d3SJohn Marino  */
164*86d7f5d3SJohn Marino #define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
165*86d7f5d3SJohn Marino 
166*86d7f5d3SJohn Marino /*
167*86d7f5d3SJohn Marino  * Walk a list, setting 'v' in turn to the containing structure of each item.
168*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
169*86d7f5d3SJohn Marino  * The 'struct dm_list' variable within the containing structure is 'field'.
170*86d7f5d3SJohn Marino  * t must be defined as a temporary variable of the same type as v.
171*86d7f5d3SJohn Marino  */
172*86d7f5d3SJohn Marino #define dm_list_iterate_items_gen_safe(v, t, head, field) \
173*86d7f5d3SJohn Marino 	for (v = dm_list_struct_base((head)->n, typeof(*v), field), \
174*86d7f5d3SJohn Marino 	     t = dm_list_struct_base(v->field.n, typeof(*v), field); \
175*86d7f5d3SJohn Marino 	     &v->field != (head); \
176*86d7f5d3SJohn Marino 	     v = t, t = dm_list_struct_base(v->field.n, typeof(*v), field))
177*86d7f5d3SJohn Marino /*
178*86d7f5d3SJohn Marino  * Walk a list, setting 'v' in turn to the containing structure of each item.
179*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
180*86d7f5d3SJohn Marino  * The list should be 'struct dm_list list' within the containing structure.
181*86d7f5d3SJohn Marino  * t must be defined as a temporary variable of the same type as v.
182*86d7f5d3SJohn Marino  */
183*86d7f5d3SJohn Marino #define dm_list_iterate_items_safe(v, t, head) \
184*86d7f5d3SJohn Marino 	dm_list_iterate_items_gen_safe(v, t, (head), list)
185*86d7f5d3SJohn Marino 
186*86d7f5d3SJohn Marino /*
187*86d7f5d3SJohn Marino  * Walk a list backwards, setting 'v' in turn to the containing structure
188*86d7f5d3SJohn Marino  * of each item.
189*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
190*86d7f5d3SJohn Marino  * The 'struct dm_list' variable within the containing structure is 'field'.
191*86d7f5d3SJohn Marino  */
192*86d7f5d3SJohn Marino #define dm_list_iterate_back_items_gen(v, head, field) \
193*86d7f5d3SJohn Marino 	for (v = dm_list_struct_base((head)->p, typeof(*v), field); \
194*86d7f5d3SJohn Marino 	     &v->field != (head); \
195*86d7f5d3SJohn Marino 	     v = dm_list_struct_base(v->field.p, typeof(*v), field))
196*86d7f5d3SJohn Marino 
197*86d7f5d3SJohn Marino /*
198*86d7f5d3SJohn Marino  * Walk a list backwards, setting 'v' in turn to the containing structure
199*86d7f5d3SJohn Marino  * of each item.
200*86d7f5d3SJohn Marino  * The containing structure should be the same type as 'v'.
201*86d7f5d3SJohn Marino  * The list should be 'struct dm_list list' within the containing structure.
202*86d7f5d3SJohn Marino  */
203*86d7f5d3SJohn Marino #define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
204*86d7f5d3SJohn Marino 
205*86d7f5d3SJohn Marino /*
206*86d7f5d3SJohn Marino  * Return the number of elements in a list by walking it.
207*86d7f5d3SJohn Marino  */
208*86d7f5d3SJohn Marino unsigned int dm_list_size(const struct dm_list *head);
209*86d7f5d3SJohn Marino 
210*86d7f5d3SJohn Marino #endif
211