xref: /titanic_44/usr/src/cmd/tnf/prex/queue.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Includes
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "queue.h"
33*7c478bd9Sstevel@tonic-gate #include "new.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * queue_init() - initializes a queue_node to be unlinked.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate void
queue_init(queue_node_t * q)41*7c478bd9Sstevel@tonic-gate queue_init(queue_node_t * q)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	q->next_p = q->prev_p = q;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate }				/* end queue_init */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * queue_prepend() - prepends a queue_node to another in a list
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate queue_node_t   *
queue_prepend(queue_node_t * h,queue_node_t * q)53*7c478bd9Sstevel@tonic-gate queue_prepend(queue_node_t * h, queue_node_t * q)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	if (!h)
56*7c478bd9Sstevel@tonic-gate 		return ((q) ? q : NULL);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if (q) {
59*7c478bd9Sstevel@tonic-gate 		queue_node_t   *qtail_p = q->prev_p;
60*7c478bd9Sstevel@tonic-gate 		queue_node_t   *hnode_p = h->next_p;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 		hnode_p->prev_p = qtail_p;
63*7c478bd9Sstevel@tonic-gate 		h->next_p = q;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 		q->prev_p = h;
66*7c478bd9Sstevel@tonic-gate 		qtail_p->next_p = hnode_p;
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 	return (h);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate }				/* end queue_prepend */
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * queue_append() - appends a queue_node to another in a list
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate queue_node_t   *
queue_append(queue_node_t * h,queue_node_t * q)78*7c478bd9Sstevel@tonic-gate queue_append(queue_node_t * h, queue_node_t * q)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	if (!h)
81*7c478bd9Sstevel@tonic-gate 		return ((q) ? q : NULL);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (q) {
84*7c478bd9Sstevel@tonic-gate 		queue_node_t   *htail_p = h->prev_p;
85*7c478bd9Sstevel@tonic-gate 		queue_node_t   *qtail_p = q->prev_p;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		h->prev_p = qtail_p;
88*7c478bd9Sstevel@tonic-gate 		htail_p->next_p = q;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		q->prev_p = htail_p;
91*7c478bd9Sstevel@tonic-gate 		qtail_p->next_p = h;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	return (h);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate }				/* end queue_append */
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * queue_remove() - removes a node from a list, returns a pointer to the next
100*7c478bd9Sstevel@tonic-gate  * node in the list.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate queue_node_t   *
queue_remove(queue_node_t * q)104*7c478bd9Sstevel@tonic-gate queue_remove(queue_node_t * q)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	queue_node_t   *n;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	n = q->next_p;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (queue_isempty(q))
111*7c478bd9Sstevel@tonic-gate 		return (NULL);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	q->next_p->prev_p = q->prev_p;
114*7c478bd9Sstevel@tonic-gate 	q->prev_p->next_p = q->next_p;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	q->next_p = q->prev_p = q;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	return (n);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate }				/* end queue_remove */
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * queue_isempty()
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate boolean_t
queue_isempty(queue_node_t * q)128*7c478bd9Sstevel@tonic-gate queue_isempty(queue_node_t * q)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	return ((q->next_p == q));
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate }				/* queue_isempty */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * queue_next() - returns the next element in a queue, or NULL if the
137*7c478bd9Sstevel@tonic-gate  * supplied previous item was the last.
138*7c478bd9Sstevel@tonic-gate  */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate queue_node_t   *
queue_next(queue_node_t * h,queue_node_t * q)141*7c478bd9Sstevel@tonic-gate queue_next(queue_node_t * h, queue_node_t * q)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	if (!h)
144*7c478bd9Sstevel@tonic-gate 		return (NULL);
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (!q)
147*7c478bd9Sstevel@tonic-gate 		return (h);
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (q->next_p == h)
150*7c478bd9Sstevel@tonic-gate 		return (NULL);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	return (q->next_p);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate }				/* end queue_next */
155