2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Initialise a list before use.
21 * The list head's next and previous pointers point back to itself.
23 void dm_list_init(struct dm_list *head)
25 head->n = head->p = head;
29 * Insert an element before 'head'.
30 * If 'head' is the list head, this adds an element to the end of the list.
32 void dm_list_add(struct dm_list *head, struct dm_list *elem)
44 * Insert an element after 'head'.
45 * If 'head' is the list head, this adds an element to the front of the list.
47 void dm_list_add_h(struct dm_list *head, struct dm_list *elem)
59 * Delete an element from its list.
60 * Note that this doesn't change the element itself - it may still be safe
61 * to follow its pointers.
63 void dm_list_del(struct dm_list *elem)
70 * Remove an element from existing list and insert before 'head'.
72 void dm_list_move(struct dm_list *head, struct dm_list *elem)
75 dm_list_add(head, elem);
81 int dm_list_empty(const struct dm_list *head)
83 return head->n == head;
87 * Is this the first element of the list?
89 int dm_list_start(const struct dm_list *head, const struct dm_list *elem)
91 return elem->p == head;
95 * Is this the last element of the list?
97 int dm_list_end(const struct dm_list *head, const struct dm_list *elem)
99 return elem->n == head;
103 * Return first element of the list or NULL if empty
105 struct dm_list *dm_list_first(const struct dm_list *head)
107 return (dm_list_empty(head) ? NULL : head->n);
111 * Return last element of the list or NULL if empty
113 struct dm_list *dm_list_last(const struct dm_list *head)
115 return (dm_list_empty(head) ? NULL : head->p);
119 * Return the previous element of the list, or NULL if we've reached the start.
121 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem)
123 return (dm_list_start(head, elem) ? NULL : elem->p);
127 * Return the next element of the list, or NULL if we've reached the end.
129 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem)
131 return (dm_list_end(head, elem) ? NULL : elem->n);
135 * Return the number of elements in a list by walking it.
137 unsigned int dm_list_size(const struct dm_list *head)
140 const struct dm_list *v;
142 dm_list_iterate(v, head)
149 * Join two lists together.
150 * This moves all the elements of the list 'head1' to the end of the list
151 * 'head', leaving 'head1' empty.
153 void dm_list_splice(struct dm_list *head, struct dm_list *head1)
158 if (dm_list_empty(head1))
162 head1->n->p = head->p;
164 head->p->n = head1->n;