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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/group.h>
#include <sys/cmn_err.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;
}
/*
* Empty a group_t
* Capacity is preserved.
*/
void
group_empty(group_t *g)
{
int i;
int sz = g->grp_size;
g->grp_size = 0;
for (i = 0; i < sz; i++)
g->grp_set[i] = 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;
if (g->grp_size == 0)
return (-1);
/*
* 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 && ISP2(g->grp_size))
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 element at the specified index
*/
void
group_remove_at(group_t *g, uint_t idx)
{
ASSERT(idx < g->grp_capacity);
g->grp_set[idx] = NULL;
}
/*
* Find an element in the group, and return its index
* Returns -1 if the element could not be found.
*/
uint_t
group_find(group_t *g, void *e)
{
uint_t idx;
for (idx = 0; idx < g->grp_capacity; idx++) {
if (g->grp_set[idx] == e)
return (idx);
}
return ((uint_t)-1);
}
/*
* Return a string in a given buffer with list of integer entries in a group.
* The string concatenates consecutive integer ranges ax x-y.
* The resulting string looks like "1,2-5,8"
*
* The convert argument is used to map group elements to integer IDs.
*/
char *
group2intlist(group_t *group, char *buffer, size_t len, int (convert)(void*))
{
char *ptr = buffer;
void *v;
group_iter_t iter;
boolean_t first_iteration = B_TRUE;
boolean_t first_value = B_TRUE;
int start = 0, end = 0;
/*
* Allow for the terminating NULL-byte
*/
len = len -1;
group_iter_init(&iter);
while ((v = group_iterate(group, &iter)) != NULL && len > 0) {
int id = convert(v);
int nbytes = 0;
if (first_iteration) {
start = end = id;
first_iteration = B_FALSE;
} else if (end + 1 == id) {
/*
* Got consecutive ID, so extend end of range without
* doing anything since the range may extend further
*/
end = id;
} else {
if (first_value) {
first_value = B_FALSE;
} else {
*ptr++ = ',';
len--;
}
if (len == 0)
break;
/*
* Next ID is not consecutive, so dump IDs gotten so
* far.
*/
if (end > start + 1) /* range */
nbytes = snprintf(ptr, len, "%d-%d",
start, end);
else if (end > start) /* different values */
nbytes = snprintf(ptr, len, "%d,%d",
start, end);
else /* same value */
nbytes = snprintf(ptr, len, "%d", start);
if (nbytes <= 0) {
len = 0;
break;
}
/*
* Advance position in the string
*/
ptr += nbytes;
len -= nbytes;
/*
* Try finding consecutive range starting from current
* ID.
*/
start = end = id;
}
}
if (!first_value) {
*ptr++ = ',';
len--;
}
/*
* Print last ID(s)
*/
if (len > 0) {
if (end > start + 1) {
(void) snprintf(ptr, len, "%d-%d", start, end);
} else if (end != start) {
(void) snprintf(ptr, len, "%d,%d", start, end);
} else {
(void) snprintf(ptr, len, "%d", start);
}
}
return (buffer);
}
|