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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 #pragma ident "%Z%%M% %I% %E% SMI"
28 * utility for vntsd queue handling
31 #include <sys/types.h>
36 #include <sys/socket.h>
42 #include <netinet/in.h>
47 /* alloc_que_el() allocates a queue element */
49 alloc_que_el(void *handle
)
53 /* allocate a queue element */
54 el
= (vntsd_que_t
*)malloc(sizeof (vntsd_que_t
));
67 /* vntsd_que_append() appends a element to a queue */
69 vntsd_que_append(vntsd_que_t
**que_hd
, void *handle
)
77 /* allocate a queue element */
78 el
= alloc_que_el(handle
);
81 return (VNTSD_ERR_NO_MEM
);
90 /* walk to the last one */
91 while (p
->nextp
!= NULL
)
98 return (VNTSD_SUCCESS
);
101 /* vntsd_que_insert_after() inserts element arter the handle */
103 vntsd_que_insert_after(vntsd_que_t
*que
, void *handle
, void *next
)
112 if (q
->handle
== handle
) {
121 return (VNTSD_ERR_EL_NOT_FOUND
);
124 el
= alloc_que_el(next
);
127 return (VNTSD_ERR_NO_MEM
);
130 el
->nextp
= q
->nextp
;
134 return (VNTSD_SUCCESS
);
139 /* vntsd_que_rm() removes an element from a queue */
141 vntsd_que_rm(vntsd_que_t
**que_hd
, void *handle
)
143 vntsd_que_t
*p
= *que_hd
;
144 vntsd_que_t
*prevp
= NULL
;
149 if (p
->handle
== handle
) {
158 return (VNTSD_ERR_EL_NOT_FOUND
);
166 prevp
->nextp
= p
->nextp
;
169 if (p
->nextp
!= NULL
) {
170 p
->nextp
->prevp
= prevp
;
177 return (VNTSD_SUCCESS
);
181 /* vntsd_que_walk() - walk queue and apply function to each element */
183 vntsd_que_walk(vntsd_que_t
*que_hd
, el_func_t el_func
)
185 vntsd_que_t
*p
= que_hd
;
188 if ((*el_func
)(p
->handle
)) {
194 return (VNTSD_SUCCESS
);
198 /* vntsd_que_find() finds first match */
200 vntsd_que_find(vntsd_que_t
*que_hd
, compare_func_t compare_func
, void *data
)
202 vntsd_que_t
*p
= que_hd
;
204 assert(compare_func
!= NULL
);
206 if ((*compare_func
)(p
->handle
, data
)) {
218 /* vntsd_free_que() frees entire queue */
220 vntsd_free_que(vntsd_que_t
**q
, clean_func_t clean_func
)
230 /* clean func will free the handle */
231 (*clean_func
)(p
->handle
);
241 * vntsd_que_pos() matches a handle and returns a handle located at "pos"
242 * relative to the matched handle. pos supported are 1 or -1.
245 vntsd_que_pos(vntsd_que_t
*que_hd
, void *handle
, int pos
)
247 vntsd_que_t
*p
= que_hd
;
249 assert((pos
== 1) || (pos
== -1));
253 if (p
->handle
== handle
) {
257 if (p
->nextp
!= NULL
) {
258 return (p
->nextp
->handle
);
261 /* last one go to first */
262 return (que_hd
->handle
);
266 if (p
->prevp
!= NULL
) {
267 return (p
->prevp
->handle
);
270 /* first one, return last one */
271 while (p
->nextp
!= NULL
) {
276 assert(p
->handle
!= NULL
);
284 DERR(stderr
, "t@%d vntsd_que_pos can not find handle \n",