fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * CDDL HEADER START
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The contents of this file are subject to the terms of the
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Common Development and Distribution License (the "License").
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * You may not use this file except in compliance with the License.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * See the License for the specific language governing permissions
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * and limitations under the License.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * When distributing Covered Code, include this CDDL HEADER in each
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * If applicable, add the following below this CDDL HEADER, with the
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * fields enclosed by brackets "[]" replaced with your own identifying
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * information: Portions Copyright [yyyy] [name of copyright owner]
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * CDDL HEADER END
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Use is subject to license terms.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Initialize a group_t
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Destroy a group_t
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The group must already be empty
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (g->grp_capacity > 0) {
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe kmem_free(g->grp_set, g->grp_capacity * sizeof (void *));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Empty a group_t
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Capacity is preserved.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe for (i = 0; i < sz; i++)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Add element "e" to group "g"
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Returns -1 if addition would result in overcapacity, and
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * resize operations aren't allowed, and 0 otherwise
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (-1);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe ASSERT(g->grp_size != g->grp_capacity || (gflag & GRP_RESIZE));
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (0);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Remove element "e" from group "g"
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Returns -1 if "e" was not present in "g" and 0 otherwise
1a77c24bc3a54fb48592de0041508561c5781501Eric Saxe return (-1);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Find the element in the group's set
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe for (i = 0; i < g->grp_size; i++)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (g->grp_set[i] == e)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (g->grp_set[i] != e)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (-1);
de710d24d2fae4468e64da999e1d952a247f142cJosef 'Jeff' Sipek g->grp_size > GRP_SET_SIZE_DEFAULT && ISP2(g->grp_size))
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (0);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Expand the capacity of group "g" so that it may
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * contain at least "n" elements
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe while (g->grp_capacity < n)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Upsize a group's holding capacity
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The array size grows in powers of two
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The set is unallocated.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Allocate a default sized set.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe g->grp_set = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Allocate a newly sized array,
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * copy the data, and free the old array.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe set_new = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe (void) kcopy(set_old, set_new, cap_old * sizeof (void *));
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The new array size should be a power of two
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Downsize a group's holding capacity
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * The group's existing array size must already
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * be a power of two
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * GRP_SET_SIZE_DEFAULT is the minumum set size.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe set_new = kmem_zalloc(cap_new * sizeof (void *), KM_SLEEP);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe (void) kcopy(set_old, set_new, cap_new * sizeof (void *));
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Pack a group's set
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Element order is not preserved
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe for (i = 0; i < sz; i++) {
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Found a new free slot.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Start packing from here.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Found a slot to pack into
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * an earlier free slot.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Find the next free slot
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (j == i)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Initialize a group iterator cookie
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Iterate over the elements in a group
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Indexed access to a group's elements
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Add a new ordered group element at specified
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * index. The group must already be of sufficient
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * capacity to hold an element at the specified index.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Returns 0 if addition was sucessful, and -1 if the
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * addition failed because the table was too small
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (-1);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (0);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Remove the element at the specified index
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Find an element in the group, and return its index
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Returns -1 if the element could not be found.
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Return a string in a given buffer with list of integer entries in a group.
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * The string concatenates consecutive integer ranges ax x-y.
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * The resulting string looks like "1,2-5,8"
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * The convert argument is used to map group elements to integer IDs.
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasovgroup2intlist(group_t *group, char *buffer, size_t len, int (convert)(void*))
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Allow for the terminating NULL-byte
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov while ((v = group_iterate(group, &iter)) != NULL && len > 0) {
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Got consecutive ID, so extend end of range without
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * doing anything since the range may extend further
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Next ID is not consecutive, so dump IDs gotten so
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov else if (end > start) /* different values */
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov else /* same value */
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov nbytes = snprintf(ptr, len, "%d", start);
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Advance position in the string
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Try finding consecutive range starting from current
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov * Print last ID(s)
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov (void) snprintf(ptr, len, "%d-%d", start, end);
b885580b43755ee4ea1e280b85428893d2ba9291Alexander Kolbasov (void) snprintf(ptr, len, "%d,%d", start, end);