1 1.1 haad /* $NetBSD: list.c,v 1.1.1.1 2008/12/22 00:17:54 haad Exp $ */ 2 1.1 haad 3 1.1 haad /* 4 1.1 haad * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. 5 1.1 haad * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 6 1.1 haad * 7 1.1 haad * This file is part of LVM2. 8 1.1 haad * 9 1.1 haad * This copyrighted material is made available to anyone wishing to use, 10 1.1 haad * modify, copy, or redistribute it subject to the terms and conditions 11 1.1 haad * of the GNU Lesser General Public License v.2.1. 12 1.1 haad * 13 1.1 haad * You should have received a copy of the GNU Lesser General Public License 14 1.1 haad * along with this program; if not, write to the Free Software Foundation, 15 1.1 haad * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 1.1 haad */ 17 1.1 haad 18 1.1 haad #include "lib.h" 19 1.1 haad 20 1.1 haad /* 21 1.1 haad * Initialise a list before use. 22 1.1 haad * The list head's next and previous pointers point back to itself. 23 1.1 haad */ 24 1.1 haad void dm_list_init(struct dm_list *head) 25 1.1 haad { 26 1.1 haad head->n = head->p = head; 27 1.1 haad } 28 1.1 haad 29 1.1 haad /* 30 1.1 haad * Insert an element before 'head'. 31 1.1 haad * If 'head' is the list head, this adds an element to the end of the list. 32 1.1 haad */ 33 1.1 haad void dm_list_add(struct dm_list *head, struct dm_list *elem) 34 1.1 haad { 35 1.1 haad assert(head->n); 36 1.1 haad 37 1.1 haad elem->n = head; 38 1.1 haad elem->p = head->p; 39 1.1 haad 40 1.1 haad head->p->n = elem; 41 1.1 haad head->p = elem; 42 1.1 haad } 43 1.1 haad 44 1.1 haad /* 45 1.1 haad * Insert an element after 'head'. 46 1.1 haad * If 'head' is the list head, this adds an element to the front of the list. 47 1.1 haad */ 48 1.1 haad void dm_list_add_h(struct dm_list *head, struct dm_list *elem) 49 1.1 haad { 50 1.1 haad assert(head->n); 51 1.1 haad 52 1.1 haad elem->n = head->n; 53 1.1 haad elem->p = head; 54 1.1 haad 55 1.1 haad head->n->p = elem; 56 1.1 haad head->n = elem; 57 1.1 haad } 58 1.1 haad 59 1.1 haad /* 60 1.1 haad * Delete an element from its list. 61 1.1 haad * Note that this doesn't change the element itself - it may still be safe 62 1.1 haad * to follow its pointers. 63 1.1 haad */ 64 1.1 haad void dm_list_del(struct dm_list *elem) 65 1.1 haad { 66 1.1 haad elem->n->p = elem->p; 67 1.1 haad elem->p->n = elem->n; 68 1.1 haad } 69 1.1 haad 70 1.1 haad /* 71 1.1 haad * Remove an element from existing list and insert before 'head'. 72 1.1 haad */ 73 1.1 haad void dm_list_move(struct dm_list *head, struct dm_list *elem) 74 1.1 haad { 75 1.1 haad dm_list_del(elem); 76 1.1 haad dm_list_add(head, elem); 77 1.1 haad } 78 1.1 haad 79 1.1 haad /* 80 1.1 haad * Is the list empty? 81 1.1 haad */ 82 1.1 haad int dm_list_empty(const struct dm_list *head) 83 1.1 haad { 84 1.1 haad return head->n == head; 85 1.1 haad } 86 1.1 haad 87 1.1 haad /* 88 1.1 haad * Is this the first element of the list? 89 1.1 haad */ 90 1.1 haad int dm_list_start(const struct dm_list *head, const struct dm_list *elem) 91 1.1 haad { 92 1.1 haad return elem->p == head; 93 1.1 haad } 94 1.1 haad 95 1.1 haad /* 96 1.1 haad * Is this the last element of the list? 97 1.1 haad */ 98 1.1 haad int dm_list_end(const struct dm_list *head, const struct dm_list *elem) 99 1.1 haad { 100 1.1 haad return elem->n == head; 101 1.1 haad } 102 1.1 haad 103 1.1 haad /* 104 1.1 haad * Return first element of the list or NULL if empty 105 1.1 haad */ 106 1.1 haad struct dm_list *dm_list_first(const struct dm_list *head) 107 1.1 haad { 108 1.1 haad return (dm_list_empty(head) ? NULL : head->n); 109 1.1 haad } 110 1.1 haad 111 1.1 haad /* 112 1.1 haad * Return last element of the list or NULL if empty 113 1.1 haad */ 114 1.1 haad struct dm_list *dm_list_last(const struct dm_list *head) 115 1.1 haad { 116 1.1 haad return (dm_list_empty(head) ? NULL : head->p); 117 1.1 haad } 118 1.1 haad 119 1.1 haad /* 120 1.1 haad * Return the previous element of the list, or NULL if we've reached the start. 121 1.1 haad */ 122 1.1 haad struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem) 123 1.1 haad { 124 1.1 haad return (dm_list_start(head, elem) ? NULL : elem->p); 125 1.1 haad } 126 1.1 haad 127 1.1 haad /* 128 1.1 haad * Return the next element of the list, or NULL if we've reached the end. 129 1.1 haad */ 130 1.1 haad struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem) 131 1.1 haad { 132 1.1 haad return (dm_list_end(head, elem) ? NULL : elem->n); 133 1.1 haad } 134 1.1 haad 135 1.1 haad /* 136 1.1 haad * Return the number of elements in a list by walking it. 137 1.1 haad */ 138 1.1 haad unsigned int dm_list_size(const struct dm_list *head) 139 1.1 haad { 140 1.1 haad unsigned int s = 0; 141 1.1 haad const struct dm_list *v; 142 1.1 haad 143 1.1 haad dm_list_iterate(v, head) 144 1.1 haad s++; 145 1.1 haad 146 1.1 haad return s; 147 1.1 haad } 148