Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / datastruct / list.c
blob48ddee136ab655b5115b7b27111aca3bad4a54b7
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 #include "lib.h"
21 * Initialise a list before use.
22 * The list head's next and previous pointers point back to itself.
24 void dm_list_init(struct dm_list *head)
26 head->n = head->p = head;
30 * Insert an element before 'head'.
31 * If 'head' is the list head, this adds an element to the end of the list.
33 void dm_list_add(struct dm_list *head, struct dm_list *elem)
35 assert(head->n);
37 elem->n = head;
38 elem->p = head->p;
40 head->p->n = elem;
41 head->p = elem;
45 * Insert an element after 'head'.
46 * If 'head' is the list head, this adds an element to the front of the list.
48 void dm_list_add_h(struct dm_list *head, struct dm_list *elem)
50 assert(head->n);
52 elem->n = head->n;
53 elem->p = head;
55 head->n->p = elem;
56 head->n = elem;
60 * Delete an element from its list.
61 * Note that this doesn't change the element itself - it may still be safe
62 * to follow its pointers.
64 void dm_list_del(struct dm_list *elem)
66 elem->n->p = elem->p;
67 elem->p->n = elem->n;
71 * Remove an element from existing list and insert before 'head'.
73 void dm_list_move(struct dm_list *head, struct dm_list *elem)
75 dm_list_del(elem);
76 dm_list_add(head, elem);
80 * Is the list empty?
82 int dm_list_empty(const struct dm_list *head)
84 return head->n == head;
88 * Is this the first element of the list?
90 int dm_list_start(const struct dm_list *head, const struct dm_list *elem)
92 return elem->p == head;
96 * Is this the last element of the list?
98 int dm_list_end(const struct dm_list *head, const struct dm_list *elem)
100 return elem->n == head;
104 * Return first element of the list or NULL if empty
106 struct dm_list *dm_list_first(const struct dm_list *head)
108 return (dm_list_empty(head) ? NULL : head->n);
112 * Return last element of the list or NULL if empty
114 struct dm_list *dm_list_last(const struct dm_list *head)
116 return (dm_list_empty(head) ? NULL : head->p);
120 * Return the previous element of the list, or NULL if we've reached the start.
122 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem)
124 return (dm_list_start(head, elem) ? NULL : elem->p);
128 * Return the next element of the list, or NULL if we've reached the end.
130 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem)
132 return (dm_list_end(head, elem) ? NULL : elem->n);
136 * Return the number of elements in a list by walking it.
138 unsigned int dm_list_size(const struct dm_list *head)
140 unsigned int s = 0;
141 const struct dm_list *v;
143 dm_list_iterate(v, head)
144 s++;
146 return s;