1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/group.h>
#define GRP_SET_SIZE_DEFAULT 2
static void group_grow_set(group_t *);
static void group_shrink_set(group_t *);
static void group_pack_set(void **, uint_t);
/*
* Initialize a group_t
*/
void
group_create(group_t *g)
{
bzero(g, sizeof (group_t));
}
/*
* Destroy a group_t
* The group must already be empty
*/
void
group_destroy(group_t *g)
{
ASSERT(g->grp_size == 0);
if (g->grp_capacity > 0) {
kmem_free(g->grp_set, g->grp_capacity * sizeof (void *));
g->grp_capacity = 0;
}
g->grp_set = NULL;
}
/*
* Add element "e" to group "g"
*
* Returns -1 if addition would result in overcapacity, and
* resize operations aren't allowed, and 0 otherwise
*/
int
group_add(group_t *g, void *e, int gflag)
{
int entry;
if ((gflag & GRP_NORESIZE) &&
g->grp_size == g->grp_capacity)
return (-1);
ASSERT(g->grp_size != g->grp_capacity || (gflag & GRP_RESIZE));
entry = g->grp_size++;
if (g->grp_size > g->grp_capacity)
group_grow_set(g);
ASSERT(g->grp_set[entry] == NULL);
g->grp_set[entry] = e;
return (0);
}
/*
* Remove element "e" from group "g"
*
* Returns -1 if "e" was not present in "g" and 0 otherwise
*/
int
group_remove(group_t *g, void *e, int gflag)
{
int i;
/*
* Find the element in the group's set
*/
for (i = 0; i < g->grp_size; i++)
if (g->grp_set[i] == e)
break;
if (g->grp_set[i] != e)
return (-1);
g->grp_set[i] = NULL;
group_pack_set(g->grp_set, g->grp_size);
g->grp_size--;
if ((gflag & GRP_RESIZE) &&
g->grp_size > GRP_SET_SIZE_DEFAULT &&
((g->grp_size - 1) & g->grp_size) == 0)
group_shrink_set(g);
return (0);
}
/*
* Expand the capacity of group "g" so that it may
* contain at least "n" elements
*/
void
group_expand(group_t *g, uint_t n)
{
while (g->grp_capacity < n)
group_grow_set(g);
}
/*
* Upsize a group's holding capacity
*/
static void
group_grow_set(group_t *g)
{
uint_t cap_old, cap_new;
void **set_old, **set_new;
cap_old = g->grp_capacity;
set_old = g->grp_set;
/*
* The array size grows in powers of two
*/
if ((cap_new = (cap_old << 1)) == 0) {
/*
* The set is unallocated.
* Allocate a default sized set.
*/
cap_new = GRP_SET_SIZE_DEFAULT;
g->grp_set = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
g->grp_capacity = cap_new;
} else {
/*
* Allocate a newly sized array,
* copy the data, and free the old array.
*/
set_new = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
(void) kcopy(set_old, set_new, cap_old * sizeof (void *));
g->grp_set = set_new;
g->grp_capacity = cap_new;
kmem_free(set_old, cap_old * sizeof (void *));
}
/*
* The new array size should be a power of two
*/
ASSERT(((cap_new - 1) & cap_new) == 0);
}
/*
* Downsize a group's holding capacity
*/
static void
group_shrink_set(group_t *g)
{
uint_t cap_old, cap_new;
void **set_old, **set_new;
cap_old = g->grp_capacity;
set_old = g->grp_set;
/*
* The group's existing array size must already
* be a power of two
*/
ASSERT(((cap_old - 1) & cap_old) == 0);
cap_new = cap_old >> 1;
/*
* GRP_SET_SIZE_DEFAULT is the minumum set size.
*/
if (cap_new < GRP_SET_SIZE_DEFAULT)
return;
set_new = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
(void) kcopy(set_old, set_new, cap_new * sizeof (void *));
g->grp_capacity = cap_new;
g->grp_set = set_new;
ASSERT(((cap_new - 1) & cap_new) == 0);
kmem_free(set_old, cap_old * sizeof (void *));
}
/*
* Pack a group's set
* Element order is not preserved
*/
static void
group_pack_set(void **set, uint_t sz)
{
uint_t i, j, free;
free = (uint_t)-1;
for (i = 0; i < sz; i++) {
if (set[i] == NULL && free == (uint_t)-1) {
/*
* Found a new free slot.
* Start packing from here.
*/
free = i;
} else if (set[i] != NULL && free != (uint_t)-1) {
/*
* Found a slot to pack into
* an earlier free slot.
*/
ASSERT(set[free] == NULL);
set[free] = set[i];
set[i] = NULL;
/*
* Find the next free slot
*/
for (j = free + 1; set[j] != NULL; j++) {
ASSERT(j <= i);
if (j == i)
break;
}
if (set[j] == NULL)
free = j;
else
free = (uint_t)-1;
}
}
}
/*
* Initialize a group iterator cookie
*/
void
group_iter_init(group_iter_t *iter)
{
*iter = 0;
}
/*
* Iterate over the elements in a group
*/
void *
group_iterate(group_t *g, group_iter_t *iter)
{
uint_t idx = *iter;
void *data = NULL;
while (idx < g->grp_size) {
data = g->grp_set[idx++];
if (data != NULL)
break;
}
*iter = idx;
return (data);
}
/*
* Indexed access to a group's elements
*/
void *
group_access_at(group_t *g, uint_t idx)
{
if (idx >= g->grp_capacity)
return (NULL);
return (g->grp_set[idx]);
}
/*
* Add a new ordered group element at specified
* index. The group must already be of sufficient
* capacity to hold an element at the specified index.
*
* Returns 0 if addition was sucessful, and -1 if the
* addition failed because the table was too small
*/
int
group_add_at(group_t *g, void *e, uint_t idx)
{
if (idx >= g->grp_capacity)
return (-1);
if (idx >= g->grp_size)
g->grp_size = idx + 1;
ASSERT(g->grp_set[idx] == NULL);
g->grp_set[idx] = e;
return (0);
}
/*
* Remove the entry at the specified index
*/
void
group_remove_at(group_t *g, uint_t idx)
{
ASSERT(idx < g->grp_capacity);
g->grp_set[idx] = NULL;
}
|