1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 1999 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 30 #include <sys/types.h> 31 #include <sys/param.h> 32 #include <sys/debug.h> 33 #include "ghd_queue.h" 34 35 36 37 void 38 L1_add(L1_t *lp, L1el_t *lep, void *datap) 39 { 40 /* init the list element */ 41 lep->le_nextp = NULL; 42 lep->le_datap = datap; 43 44 if (!lp->l1_tailp) { 45 /* list is empty */ 46 lp->l1_headp = lep; 47 } else { 48 /* add it to the tailend */ 49 lp->l1_tailp->le_nextp = lep; 50 } 51 52 lp->l1_tailp = lep; 53 } 54 55 56 /* 57 * L1Delete() 58 * 59 * Remove a specific entry from a singly-linked list. 60 * 61 */ 62 63 void 64 L1_delete(L1_t *lp, L1el_t *lep) 65 { 66 L1el_t *prevp; 67 68 if (lp->l1_headp == lep) { 69 /* it's the first entry in the list */ 70 if ((lp->l1_headp = lep->le_nextp) == NULL) { 71 /* the list is now empty */ 72 lp->l1_tailp = NULL; 73 } 74 return; 75 } 76 77 for (prevp = lp->l1_headp; prevp != NULL; prevp = prevp->le_nextp) { 78 if (prevp->le_nextp == lep) { 79 if ((prevp->le_nextp = lep->le_nextp) == NULL) 80 lp->l1_tailp = prevp; 81 return; 82 } 83 } 84 /* its not on this list */ 85 } 86 87 88 /* 89 * L1_remove() 90 * 91 * Remove the entry at the head of the list (if any). 92 * 93 */ 94 95 void * 96 L1_remove(L1_t *lp) 97 { 98 L1el_t *lep; 99 100 /* pop the first one off the list head */ 101 if ((lep = lp->l1_headp) == NULL) { 102 return (NULL); 103 } 104 105 /* if the list is now empty fix the tail pointer */ 106 if ((lp->l1_headp = lep->le_nextp) == NULL) 107 lp->l1_tailp = NULL; 108 109 lep->le_nextp = NULL; 110 111 return (lep->le_datap); 112 } 113 114 115 void 116 L2_add(L2el_t *headp, L2el_t *elementp, void *private) 117 { 118 119 ASSERT(headp != NULL && elementp != NULL); 120 ASSERT(headp->l2_nextp != NULL); 121 ASSERT(headp->l2_prevp != NULL); 122 123 elementp->l2_private = private; 124 125 elementp->l2_nextp = headp; 126 elementp->l2_prevp = headp->l2_prevp; 127 headp->l2_prevp->l2_nextp = elementp; 128 headp->l2_prevp = elementp; 129 } 130 131 void 132 L2_delete(L2el_t *elementp) 133 { 134 135 ASSERT(elementp != NULL); 136 ASSERT(elementp->l2_nextp != NULL); 137 ASSERT(elementp->l2_prevp != NULL); 138 ASSERT(elementp->l2_nextp->l2_prevp == elementp); 139 ASSERT(elementp->l2_prevp->l2_nextp == elementp); 140 141 elementp->l2_prevp->l2_nextp = elementp->l2_nextp; 142 elementp->l2_nextp->l2_prevp = elementp->l2_prevp; 143 144 /* link it to itself in case someone does a double delete */ 145 elementp->l2_nextp = elementp; 146 elementp->l2_prevp = elementp; 147 } 148 149 150 void 151 L2_add_head(L2el_t *headp, L2el_t *elementp, void *private) 152 { 153 154 ASSERT(headp != NULL && elementp != NULL); 155 ASSERT(headp->l2_nextp != NULL); 156 ASSERT(headp->l2_prevp != NULL); 157 158 elementp->l2_private = private; 159 160 elementp->l2_prevp = headp; 161 elementp->l2_nextp = headp->l2_nextp; 162 headp->l2_nextp->l2_prevp = elementp; 163 headp->l2_nextp = elementp; 164 } 165 166 167 168 /* 169 * L2_remove() 170 * 171 * Remove the entry from the head of the list (if any). 172 * 173 */ 174 175 void * 176 L2_remove_head(L2el_t *headp) 177 { 178 L2el_t *elementp; 179 180 ASSERT(headp != NULL); 181 182 if (L2_EMPTY(headp)) 183 return (NULL); 184 185 elementp = headp->l2_nextp; 186 187 headp->l2_nextp = elementp->l2_nextp; 188 elementp->l2_nextp->l2_prevp = headp; 189 190 /* link it to itself in case someone does a double delete */ 191 elementp->l2_nextp = elementp; 192 elementp->l2_prevp = elementp; 193 194 return (elementp->l2_private); 195 } 196 197 void * 198 L2_next(L2el_t *elementp) 199 { 200 201 ASSERT(elementp != NULL); 202 203 if (L2_EMPTY(elementp)) 204 return (NULL); 205 return (elementp->l2_nextp->l2_private); 206 } 207