xref: /titanic_41/usr/src/cmd/fs.d/cachefs/mdbug/flist.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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  *
24  *		flist.c
25  *
26  * Defines the flist class.
27  */
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 /* Copyright (c) 1994 by Sun Microsystems, Inc. */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "flist.h"
34 #include "mdbug.h"
35 
36 /*
37  *
38  *		flist_create
39  *
40  * Description:
41  *	Constructor for the flist class.
42  * Arguments:
43  * Returns:
44  * Preconditions:
45  */
46 flist_object_t *
flist_create()47 flist_create()
48 {
49 	flist_object_t *flist_object_p;
50 
51 	flist_object_p = (flist_object_t *)calloc(sizeof (flist_object_t), 1);
52 
53 	if (flist_object_p == NULL)
54 		doabort();
55 
56 	flist_object_p->f_count = 0;
57 	flist_object_p->f_index = 0;
58 	return (flist_object_p);
59 }
60 
61 /*
62  *
63  *		flist_destroy
64  *
65  * Description:
66  *	Destructor for the flist class.
67  * Arguments:
68  * Returns:
69  * Preconditions:
70  */
71 void
flist_destroy(flist_object_t * flist_object_p)72 flist_destroy(flist_object_t *flist_object_p)
73 {
74 	free(flist_object_p);
75 }
76 /*
77  *
78  *		fl_push
79  *
80  * Description:
81  *	Adds the specified pointer to the top of the list
82  *	if there is room.  If there is no more room then
83  *	nothing happens.
84  * Arguments:
85  * Returns:
86  * Preconditions:
87  */
88 void
fl_push(flist_object_t * flist_object_p,void * ptr)89 fl_push(flist_object_t *flist_object_p, void *ptr)
90 {
91 	if (flist_object_p->f_count < FLIST_SIZE) {
92 		flist_object_p->f_items[flist_object_p->f_count] = (char *)ptr;
93 		flist_object_p->f_count++;
94 	}
95 }
96 
97 /*
98  *
99  *		fl_pop
100  *
101  * Description:
102  *	Removes the top item from the list.
103  *	No action is taken if the list is empty.
104  * Arguments:
105  * Returns:
106  * Preconditions:
107  */
108 void
fl_pop(flist_object_t * flist_object_p)109 fl_pop(flist_object_t *flist_object_p)
110 {
111 	if (flist_object_p->f_count > 0)
112 		flist_object_p->f_count--;
113 }
114 
115 /*
116  *
117  *		fl_top
118  *
119  * Description:
120  *	Returns the top item on the list.
121  *	Sets the internal state so that a following call to
122  *	next() will return the second item on the list.
123  *	Returns NULL if the list is empty.
124  * Arguments:
125  * Returns:
126  * Preconditions:
127  */
128 void *
fl_top(flist_object_t * flist_object_p)129 fl_top(flist_object_t *flist_object_p)
130 {
131 	flist_object_p->f_index = flist_object_p->f_count;
132 	return (fl_next(flist_object_p));
133 }
134 
135 /*
136  *
137  *		fl_next
138  *
139  * Description:
140  *	Returns the next item on the list.  NULL if there
141  *	is no next item.
142  * Arguments:
143  * Returns:
144  * Preconditions:
145  */
146 void *
fl_next(flist_object_t * flist_object_p)147 fl_next(flist_object_t *flist_object_p)
148 {
149 	if (flist_object_p->f_index > 0) {
150 		flist_object_p->f_index--;
151 		return (flist_object_p->f_items[ flist_object_p->f_index ]);
152 	} else {
153 		return (NULL);
154 	}
155 }
156 
157 /*
158  *
159  *		fl_clear
160  *
161  * Description:
162  *	Removes all items from the list and frees them.
163  * Arguments:
164  * Returns:
165  * Preconditions:
166  */
167 void
fl_clear(flist_object_t * flist_object_p)168 fl_clear(flist_object_t *flist_object_p)
169 {
170 	void *p1;
171 	while ((p1 = fl_top(flist_object_p)) != NULL) {
172 		free(p1);
173 		fl_pop(flist_object_p);
174 	}
175 }
176 /*
177  *
178  *		fl_space
179  *
180  * Description:
181  * Arguments:
182  * Returns:
183  *	Returns the number of free slots on the list.
184  * Errors:
185  * Preconditions:
186  */
187 int
fl_space(flist_object_t * flist_object_p)188 fl_space(flist_object_t *flist_object_p)
189 {
190 	return (FLIST_SIZE - flist_object_p->f_count);
191 }
192