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