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
22 * Initialise a list before use.
23 * The list head's next and previous pointers point back to itself.
25 void dm_list_init(struct dm_list
*head
)
27 head
->n
= head
->p
= head
;
31 * Insert an element before 'head'.
32 * If 'head' is the list head, this adds an element to the end of the list.
34 void dm_list_add(struct dm_list
*head
, struct dm_list
*elem
)
46 * Insert an element after 'head'.
47 * If 'head' is the list head, this adds an element to the front of the list.
49 void dm_list_add_h(struct dm_list
*head
, struct dm_list
*elem
)
61 * Delete an element from its list.
62 * Note that this doesn't change the element itself - it may still be safe
63 * to follow its pointers.
65 void dm_list_del(struct dm_list
*elem
)
72 * Remove an element from existing list and insert before 'head'.
74 void dm_list_move(struct dm_list
*head
, struct dm_list
*elem
)
77 dm_list_add(head
, elem
);
83 int dm_list_empty(const struct dm_list
*head
)
85 return head
->n
== head
;
89 * Is this the first element of the list?
91 int dm_list_start(const struct dm_list
*head
, const struct dm_list
*elem
)
93 return elem
->p
== head
;
97 * Is this the last element of the list?
99 int dm_list_end(const struct dm_list
*head
, const struct dm_list
*elem
)
101 return elem
->n
== head
;
105 * Return first element of the list or NULL if empty
107 struct dm_list
*dm_list_first(const struct dm_list
*head
)
109 return (dm_list_empty(head
) ? NULL
: head
->n
);
113 * Return last element of the list or NULL if empty
115 struct dm_list
*dm_list_last(const struct dm_list
*head
)
117 return (dm_list_empty(head
) ? NULL
: head
->p
);
121 * Return the previous element of the list, or NULL if we've reached the start.
123 struct dm_list
*dm_list_prev(const struct dm_list
*head
, const struct dm_list
*elem
)
125 return (dm_list_start(head
, elem
) ? NULL
: elem
->p
);
129 * Return the next element of the list, or NULL if we've reached the end.
131 struct dm_list
*dm_list_next(const struct dm_list
*head
, const struct dm_list
*elem
)
133 return (dm_list_end(head
, elem
) ? NULL
: elem
->n
);
137 * Return the number of elements in a list by walking it.
139 unsigned int dm_list_size(const struct dm_list
*head
)
142 const struct dm_list
*v
;
144 dm_list_iterate(v
, head
)