Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / datastruct / list.h
blob77850564fb500e51064b93739317f6d20ca55021
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #ifndef _LVM_LIST_H
19 #define _LVM_LIST_H
21 #include <assert.h>
24 * A list consists of a list head plus elements.
25 * Each element has 'next' and 'previous' pointers.
26 * The list head's pointers point to the first and the last element.
29 struct dm_list {
30 struct dm_list *n, *p;
34 * Initialise a list before use.
35 * The list head's next and previous pointers point back to itself.
37 #define DM_LIST_INIT(name) struct dm_list name = { &(name), &(name) }
38 void dm_list_init(struct dm_list *head);
41 * Insert an element before 'head'.
42 * If 'head' is the list head, this adds an element to the end of the list.
44 void dm_list_add(struct dm_list *head, struct dm_list *elem);
47 * Insert an element after 'head'.
48 * If 'head' is the list head, this adds an element to the front of the list.
50 void dm_list_add_h(struct dm_list *head, struct dm_list *elem);
53 * Delete an element from its list.
54 * Note that this doesn't change the element itself - it may still be safe
55 * to follow its pointers.
57 void dm_list_del(struct dm_list *elem);
60 * Remove an element from existing list and insert before 'head'.
62 void dm_list_move(struct dm_list *head, struct dm_list *elem);
65 * Is the list empty?
67 int dm_list_empty(const struct dm_list *head);
70 * Is this the first element of the list?
72 int dm_list_start(const struct dm_list *head, const struct dm_list *elem);
75 * Is this the last element of the list?
77 int dm_list_end(const struct dm_list *head, const struct dm_list *elem);
80 * Return first element of the list or NULL if empty
82 struct dm_list *dm_list_first(const struct dm_list *head);
85 * Return last element of the list or NULL if empty
87 struct dm_list *dm_list_last(const struct dm_list *head);
90 * Return the previous element of the list, or NULL if we've reached the start.
92 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem);
95 * Return the next element of the list, or NULL if we've reached the end.
97 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem);
100 * Given the address v of an instance of 'struct dm_list' called 'head'
101 * contained in a structure of type t, return the containing structure.
103 #define dm_list_struct_base(v, t, head) \
104 ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->head))
107 * Given the address v of an instance of 'struct dm_list list' contained in
108 * a structure of type t, return the containing structure.
110 #define dm_list_item(v, t) dm_list_struct_base((v), t, list)
113 * Given the address v of one known element e in a known structure of type t,
114 * return another element f.
116 #define dm_struct_field(v, t, e, f) \
117 (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
120 * Given the address v of a known element e in a known structure of type t,
121 * return the list head 'list'
123 #define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
126 * Set v to each element of a list in turn.
128 #define dm_list_iterate(v, head) \
129 for (v = (head)->n; v != head; v = v->n)
132 * Set v to each element in a list in turn, starting from the element
133 * in front of 'start'.
134 * You can use this to 'unwind' a list_iterate and back out actions on
135 * already-processed elements.
136 * If 'start' is 'head' it walks the list backwards.
138 #define dm_list_uniterate(v, head, start) \
139 for (v = (start)->p; v != head; v = v->p)
142 * A safe way to walk a list and delete and free some elements along
143 * the way.
144 * t must be defined as a temporary variable of the same type as v.
146 #define dm_list_iterate_safe(v, t, head) \
147 for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
150 * Walk a list, setting 'v' in turn to the containing structure of each item.
151 * The containing structure should be the same type as 'v'.
152 * The 'struct dm_list' variable within the containing structure is 'field'.
154 #define dm_list_iterate_items_gen(v, head, field) \
155 for (v = dm_list_struct_base((head)->n, typeof(*v), field); \
156 &v->field != (head); \
157 v = dm_list_struct_base(v->field.n, typeof(*v), field))
160 * Walk a list, setting 'v' in turn to the containing structure of each item.
161 * The containing structure should be the same type as 'v'.
162 * The list should be 'struct dm_list list' within the containing structure.
164 #define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
167 * Walk a list, setting 'v' in turn to the containing structure of each item.
168 * The containing structure should be the same type as 'v'.
169 * The 'struct dm_list' variable within the containing structure is 'field'.
170 * t must be defined as a temporary variable of the same type as v.
172 #define dm_list_iterate_items_gen_safe(v, t, head, field) \
173 for (v = dm_list_struct_base((head)->n, typeof(*v), field), \
174 t = dm_list_struct_base(v->field.n, typeof(*v), field); \
175 &v->field != (head); \
176 v = t, t = dm_list_struct_base(v->field.n, typeof(*v), field))
178 * Walk a list, setting 'v' in turn to the containing structure of each item.
179 * The containing structure should be the same type as 'v'.
180 * The list should be 'struct dm_list list' within the containing structure.
181 * t must be defined as a temporary variable of the same type as v.
183 #define dm_list_iterate_items_safe(v, t, head) \
184 dm_list_iterate_items_gen_safe(v, t, (head), list)
187 * Walk a list backwards, setting 'v' in turn to the containing structure
188 * of each item.
189 * The containing structure should be the same type as 'v'.
190 * The 'struct dm_list' variable within the containing structure is 'field'.
192 #define dm_list_iterate_back_items_gen(v, head, field) \
193 for (v = dm_list_struct_base((head)->p, typeof(*v), field); \
194 &v->field != (head); \
195 v = dm_list_struct_base(v->field.p, typeof(*v), field))
198 * Walk a list backwards, setting 'v' in turn to the containing structure
199 * of each item.
200 * The containing structure should be the same type as 'v'.
201 * The list should be 'struct dm_list list' within the containing structure.
203 #define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
206 * Return the number of elements in a list by walking it.
208 unsigned int dm_list_size(const struct dm_list *head);
210 #endif