10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51618Srie * Common Development and Distribution License (the "License").
61618Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211618Srie
220Sstevel@tonic-gate /*
23*8598SRod.Evans@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
261682Srie #include <sgs.h>
271682Srie #include <string.h>
285892Sab196087 #include <stdio.h>
295892Sab196087 #include <sys/debug.h>
305892Sab196087
315892Sab196087 /*
325892Sab196087 * Alist manipulation. An Alist is a list of elements formed into an array.
335892Sab196087 * Traversal of the list is an array scan, which because of the locality of
345892Sab196087 * each reference is probably more efficient than a link-list traversal.
355892Sab196087 *
365892Sab196087 * See alist.h for more background information about array lists.
375892Sab196087 */
385892Sab196087
391682Srie /*
405892Sab196087 * Insert a value into an array at a specified index:
415892Sab196087 *
425892Sab196087 * alist_insert(): Insert an item into an Alist at the specified index
435892Sab196087 * alist_insert_by_offset(): Insert an item into an Alist at the
445892Sab196087 * specified offset relative to the list address.
455892Sab196087 * aplist_insert() Insert a pointer into an APlist at the specified index
465892Sab196087 *
475892Sab196087 * entry:
485892Sab196087 * Note: All the arguments for all three routines are listed here.
495892Sab196087 * The routine to which a given argument applies is given with
505892Sab196087 * each description.
515892Sab196087 *
525892Sab196087 * llp [all] - Address of a pointer to an Alist/APlist. The pointer should
535892Sab196087 * be initialized to NULL before its first use.
545892Sab196087 * datap [alist_insert / aplist_insert] - Pointer to item data, or
555892Sab196087 * NULL. If non-null the data referenced is copied into the
565892Sab196087 * Alist item. Otherwise, the list item is zeroed, and
575892Sab196087 * further initialization is left to the caller.
585892Sab196087 * ptr [aplist_insert] - Pointer to be inserted.
595892Sab196087 * size [alist_insert / alist_insert_by_offset] - Size of an item
605892Sab196087 * in the array list, in bytes. As with any array, A given
615892Sab196087 * Alist can support any item size, but every item in that
625892Sab196087 * list must have the same size.
635892Sab196087 * init_arritems [all] - Initial allocation size: On the first insertion
645892Sab196087 * into the array list, room for init_arritems items is allocated.
655892Sab196087 * idx [alist_insert / aplist_insert] - Index at which to insert the
665892Sab196087 * new item. This index must lie within the existing list,
675892Sab196087 * or be the next index following.
685892Sab196087 * off [alist_insert_by_offset] - Offset at which to insert the new
695892Sab196087 * item, based from the start of the Alist. The offset of
705892Sab196087 * the first item is ALIST_OFF_DATA.
715892Sab196087 *
725892Sab196087 * exit:
735892Sab196087 * The item is inserted at the specified position. This operation
745892Sab196087 * can cause memory for the list to be allocated, or reallocated,
755892Sab196087 * either of which will cause the value of the list pointer
765892Sab196087 * to change.
775892Sab196087 *
785892Sab196087 * These routines can only fail if unable to allocate memory,
795892Sab196087 * in which case NULL is returned.
805892Sab196087 *
815892Sab196087 * If a pointer list (aplist_insert), then the pointer
825892Sab196087 * is stored in the requested index. On success, the address
835892Sab196087 * of the pointer within the list is returned.
845892Sab196087 *
855892Sab196087 * If the list contains arbitrary data (not aplist_insert): If datap
865892Sab196087 * is non-NULL, the data it references is copied into the item at
875892Sab196087 * the index. If datap is NULL, the specified item is zeroed.
885892Sab196087 * On success, a pointer to the inserted item is returned.
895892Sab196087 *
905892Sab196087 * The caller must not retain the returned pointer from this
915892Sab196087 * routine across calls to the list module. It is only safe to use
925892Sab196087 * it until the next call to this module for the given list.
935892Sab196087 *
941682Srie */
950Sstevel@tonic-gate void *
alist_insert(Alist ** lpp,const void * datap,size_t size,Aliste init_arritems,Aliste idx)965892Sab196087 alist_insert(Alist **lpp, const void *datap, size_t size,
975892Sab196087 Aliste init_arritems, Aliste idx)
980Sstevel@tonic-gate {
995892Sab196087 Alist *lp = *lpp;
1005892Sab196087 char *addr;
1010Sstevel@tonic-gate
1025892Sab196087 /* The size and initial array count need to be non-zero */
1035892Sab196087 ASSERT(init_arritems != 0);
1045892Sab196087 ASSERT(size != 0);
1055892Sab196087
1065892Sab196087 if (lp == NULL) {
1075892Sab196087 Aliste bsize;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1105892Sab196087 * First time here, allocate a new Alist. Note that the
1115892Sab196087 * Alist al_desc[] entry is defined for 1 element,
1125892Sab196087 * but we actually allocate the number we need.
1130Sstevel@tonic-gate */
1145892Sab196087 bsize = size * init_arritems;
1155892Sab196087 bsize = S_ROUND(bsize, sizeof (void *));
1165892Sab196087 bsize = ALIST_OFF_DATA + bsize;
1175892Sab196087 if ((lp = malloc((size_t)bsize)) == NULL)
1185892Sab196087 return (NULL);
1195892Sab196087 lp->al_arritems = init_arritems;
1205892Sab196087 lp->al_nitems = 0;
1215892Sab196087 lp->al_next = ALIST_OFF_DATA;
1225892Sab196087 lp->al_size = size;
1235892Sab196087 *lpp = lp;
1245892Sab196087 } else {
1255892Sab196087 /* We must get the same value for size every time */
1265892Sab196087 ASSERT(size == lp->al_size);
1270Sstevel@tonic-gate
1285892Sab196087 if (lp->al_nitems >= lp->al_arritems) {
1295892Sab196087 /*
1305892Sab196087 * The list is full: Increase the memory allocation
1315892Sab196087 * by doubling it.
1325892Sab196087 */
1335892Sab196087 Aliste bsize;
1340Sstevel@tonic-gate
1355892Sab196087 bsize = lp->al_size * lp->al_arritems * 2;
1365892Sab196087 bsize = S_ROUND(bsize, sizeof (void *));
1375892Sab196087 bsize = ALIST_OFF_DATA + bsize;
1385892Sab196087 if ((lp = realloc((void *)lp, (size_t)bsize)) == 0)
1395892Sab196087 return (NULL);
1405892Sab196087 lp->al_arritems *= 2;
1415892Sab196087 *lpp = lp;
1425892Sab196087 }
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1451682Srie /*
1465892Sab196087 * The caller is not supposed to use an index that
1475892Sab196087 * would introduce a "hole" in the array.
1485892Sab196087 */
1495892Sab196087 ASSERT(idx <= lp->al_nitems);
1505892Sab196087
1515892Sab196087 addr = (idx * lp->al_size) + (char *)lp->al_data;
1525892Sab196087
1535892Sab196087 /*
1541682Srie * An appended item is added to the next available array element.
1555892Sab196087 * An insert at any other spot requires that the data items that
1565892Sab196087 * exist at the point of insertion be shifted down to open a slot.
1571682Srie */
1585892Sab196087 if (idx < lp->al_nitems)
1595892Sab196087 (void) memmove(addr + lp->al_size, addr,
1605892Sab196087 (lp->al_nitems - idx) * lp->al_size);
1615892Sab196087
1625892Sab196087 lp->al_nitems++;
1635892Sab196087 lp->al_next += lp->al_size;
1645892Sab196087 if (datap != NULL)
1655892Sab196087 (void) memcpy(addr, datap, lp->al_size);
1665892Sab196087 else
1675892Sab196087 (void) memset(addr, 0, lp->al_size);
1685892Sab196087 return (addr);
1695892Sab196087 }
1705892Sab196087
1715892Sab196087 void *
alist_insert_by_offset(Alist ** lpp,const void * datap,size_t size,Aliste init_arritems,Aliste off)1725892Sab196087 alist_insert_by_offset(Alist **lpp, const void *datap, size_t size,
1735892Sab196087 Aliste init_arritems, Aliste off)
1745892Sab196087 {
1755892Sab196087 Aliste idx;
1765892Sab196087
1775892Sab196087 if (*lpp == NULL) {
1785892Sab196087 ASSERT(off == ALIST_OFF_DATA);
1795892Sab196087 idx = 0;
1801682Srie } else {
1815892Sab196087 idx = (off - ALIST_OFF_DATA) / (*lpp)->al_size;
1821682Srie }
1835892Sab196087
1845892Sab196087 return (alist_insert(lpp, datap, size, init_arritems, idx));
1855892Sab196087 }
1865892Sab196087
1875892Sab196087 void *
aplist_insert(APlist ** lpp,const void * ptr,Aliste init_arritems,Aliste idx)1885892Sab196087 aplist_insert(APlist **lpp, const void *ptr, Aliste init_arritems, Aliste idx)
1895892Sab196087 {
1905892Sab196087 APlist *lp = *lpp;
1915892Sab196087
1925892Sab196087 /* The initial array count needs to be non-zero */
1935892Sab196087 ASSERT(init_arritems != 0);
1945892Sab196087
1955892Sab196087 if (lp == NULL) {
1965892Sab196087 Aliste bsize;
1975892Sab196087
1985892Sab196087 /*
1995892Sab196087 * First time here, allocate a new APlist. Note that the
2005892Sab196087 * APlist apl_desc[] entry is defined for 1 element,
2015892Sab196087 * but we actually allocate the number we need.
2025892Sab196087 */
2035892Sab196087 bsize = APLIST_OFF_DATA + (sizeof (void *) * init_arritems);
2045892Sab196087 if ((lp = malloc((size_t)bsize)) == NULL)
2055892Sab196087 return (NULL);
2065892Sab196087 lp->apl_arritems = init_arritems;
2075892Sab196087 lp->apl_nitems = 0;
2085892Sab196087 *lpp = lp;
2095892Sab196087 } else if (lp->apl_nitems >= lp->apl_arritems) {
2105892Sab196087 /*
2115892Sab196087 * The list is full: Increase the memory allocation
2125892Sab196087 * by doubling it.
2135892Sab196087 */
2145892Sab196087 Aliste bsize;
2155892Sab196087
2165892Sab196087 bsize = APLIST_OFF_DATA +
2175892Sab196087 (2 * sizeof (void *) * lp->apl_arritems);
2185892Sab196087 if ((lp = realloc((void *)lp, (size_t)bsize)) == 0)
2195892Sab196087 return (NULL);
2205892Sab196087 lp->apl_arritems *= 2;
2215892Sab196087 *lpp = lp;
2225892Sab196087 }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2255892Sab196087 * The caller is not supposed to use an index that
2265892Sab196087 * would introduce a "hole" in the array.
2275892Sab196087 */
2285892Sab196087 ASSERT(idx <= lp->apl_nitems);
2295892Sab196087
2305892Sab196087 /*
2315892Sab196087 * An appended item is added to the next available array element.
2325892Sab196087 * An insert at any other spot requires that the data items that
2335892Sab196087 * exist at the point of insertion be shifted down to open a slot.
2340Sstevel@tonic-gate */
2355892Sab196087 if (idx < lp->apl_nitems)
2365892Sab196087 (void) memmove((char *)&lp->apl_data[idx + 1],
2375892Sab196087 (char *)&lp->apl_data[idx],
2385892Sab196087 (lp->apl_nitems - idx) * sizeof (void *));
2390Sstevel@tonic-gate
2405892Sab196087 lp->apl_nitems++;
2415892Sab196087 lp->apl_data[idx] = (void *)ptr;
2425892Sab196087 return (&lp->apl_data[idx]);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2465892Sab196087 * Append a value to a list. These are convenience wrappers on top
247*8598SRod.Evans@Sun.COM * of the insert operation. See the description of those routine above
2485892Sab196087 * for details.
2491682Srie */
2501682Srie void *
alist_append(Alist ** lpp,const void * datap,size_t size,Aliste init_arritems)2515892Sab196087 alist_append(Alist **lpp, const void *datap, size_t size,
2525892Sab196087 Aliste init_arritems)
2531682Srie {
2545892Sab196087 Aliste ndx = ((*lpp) == NULL) ? 0 : (*lpp)->al_nitems;
2555892Sab196087
2565892Sab196087 return (alist_insert(lpp, datap, size, init_arritems, ndx));
2571682Srie }
2581682Srie
2595892Sab196087 void *
aplist_append(APlist ** lpp,const void * ptr,Aliste init_arritems)2605892Sab196087 aplist_append(APlist **lpp, const void *ptr, Aliste init_arritems)
2615892Sab196087 {
2625892Sab196087 Aliste ndx = ((*lpp) == NULL) ? 0 : (*lpp)->apl_nitems;
2635892Sab196087
2645892Sab196087 return (aplist_insert(lpp, ptr, init_arritems, ndx));
2655892Sab196087 }
2665892Sab196087
2671682Srie /*
2685892Sab196087 * Delete the item at a specified index/offset, and decrement the variable
2695892Sab196087 * containing the index:
2705892Sab196087 *
2715892Sab196087 * alist_delete - Delete an item from an Alist at the specified
2725892Sab196087 * index.
2735892Sab196087 * alist_delete_by_offset - Delete an item from an Alist at the
2745892Sab196087 * specified offset from the list pointer.
2755892Sab196087 * aplist_delete - Delete a pointer from an APlist at the specified
2765892Sab196087 * index.
2775892Sab196087 *
2785892Sab196087 * entry:
2795892Sab196087 * alp - List to delete item from
2805892Sab196087 * idxp - Address of variable containing the index of the
2815892Sab196087 * item to delete.
2825892Sab196087 * offp - Address of variable containing the offset of the
2835892Sab196087 * item to delete.
2845892Sab196087 *
2855892Sab196087 * exit:
2865892Sab196087 * The item at the position given by (*idxp) or (*offp), depending
2875892Sab196087 * on the routine, is removed from the list. Then, the position
2885892Sab196087 * variable (*idxp or *offp) is decremented by one item. This is done
2895892Sab196087 * to facilitate use of this routine within a TRAVERSE loop.
2905892Sab196087 *
2915892Sab196087 * note:
2925892Sab196087 * Deleting the last element in an array list is cheap, but
2935892Sab196087 * deleting any other item causes a memory copy to occur to
2945892Sab196087 * move the following items up. If you intend to traverse the
2955892Sab196087 * entire list, deleting every item as you go, it will be cheaper
2965892Sab196087 * to omit the delete within the traverse, and then call
2975892Sab196087 * the reset function reset() afterwards.
2980Sstevel@tonic-gate */
2995892Sab196087 void
alist_delete(Alist * lp,Aliste * idxp)3005892Sab196087 alist_delete(Alist *lp, Aliste *idxp)
3010Sstevel@tonic-gate {
3025892Sab196087 Aliste idx = *idxp;
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate
3055892Sab196087 /* The list must be allocated and the index in range */
3065892Sab196087 ASSERT(lp != NULL);
3075892Sab196087 ASSERT(idx < lp->al_nitems);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * If the element to be removed is not the last entry of the array,
3110Sstevel@tonic-gate * slide the following elements over the present element.
3120Sstevel@tonic-gate */
3135892Sab196087 if (idx < --lp->al_nitems) {
3145892Sab196087 char *addr = (idx * lp->al_size) + (char *)lp->al_data;
3155892Sab196087
3165892Sab196087 (void) memmove(addr, addr + lp->al_size,
3175892Sab196087 (lp->al_nitems - idx) * lp->al_size);
3180Sstevel@tonic-gate }
3195892Sab196087 lp->al_next -= lp->al_size;
3205892Sab196087
3215892Sab196087 /* Decrement the callers index variable */
3225892Sab196087 (*idxp)--;
3235892Sab196087 }
3245892Sab196087
3255892Sab196087 void
alist_delete_by_offset(Alist * lp,Aliste * offp)3265892Sab196087 alist_delete_by_offset(Alist *lp, Aliste *offp)
3275892Sab196087 {
3285892Sab196087 Aliste idx;
3295892Sab196087
3305892Sab196087 ASSERT(lp != NULL);
3315892Sab196087 idx = (*offp - ALIST_OFF_DATA) / lp->al_size;
3325892Sab196087
3335892Sab196087 alist_delete(lp, &idx);
3345892Sab196087 *offp -= lp->al_size;
3355892Sab196087 }
3365892Sab196087
3375892Sab196087 void
aplist_delete(APlist * lp,Aliste * idxp)3385892Sab196087 aplist_delete(APlist *lp, Aliste *idxp)
3395892Sab196087 {
3405892Sab196087 Aliste idx = *idxp;
3415892Sab196087
3425892Sab196087
3435892Sab196087 /* The list must be allocated and the index in range */
3445892Sab196087 ASSERT(lp != NULL);
3455892Sab196087 ASSERT(idx < lp->apl_nitems);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /*
3485892Sab196087 * If the element to be removed is not the last entry of the array,
3495892Sab196087 * slide the following elements over the present element.
3500Sstevel@tonic-gate */
3515892Sab196087 if (idx < --lp->apl_nitems)
3525892Sab196087 (void) memmove(&lp->apl_data[idx], &lp->apl_data[idx + 1],
3535892Sab196087 (lp->apl_nitems - idx) * sizeof (void *));
3540Sstevel@tonic-gate
3555892Sab196087 /* Decrement the callers index variable */
3565892Sab196087 (*idxp)--;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /*
3605892Sab196087 * Delete the pointer with a specified value from the APlist.
3615892Sab196087 *
3625892Sab196087 * entry:
3635892Sab196087 * lp - Initialized APlist to delete item from
3645892Sab196087 * ptr - Pointer to be deleted.
3655892Sab196087 *
3665892Sab196087 * exit:
3675892Sab196087 * The list is searched for an item containing the given pointer,
3685892Sab196087 * and if a match is found, that item is delted and True (1) returned.
3695892Sab196087 * If no match is found, then False (0) is returned.
3705892Sab196087 *
3715892Sab196087 * note:
3725892Sab196087 * See note for delete operation, above.
3730Sstevel@tonic-gate */
3740Sstevel@tonic-gate int
aplist_delete_value(APlist * lp,const void * ptr)3755892Sab196087 aplist_delete_value(APlist *lp, const void *ptr)
3760Sstevel@tonic-gate {
3775892Sab196087 size_t idx;
3785892Sab196087
3795892Sab196087 /*
3805892Sab196087 * If the pointer is found in the list, use aplist_delete to
3815892Sab196087 * remove it, and we're done.
3825892Sab196087 */
3835892Sab196087 for (idx = 0; idx < lp->apl_nitems; idx++)
3845892Sab196087 if (ptr == lp->apl_data[idx]) {
3855892Sab196087 aplist_delete(lp, &idx);
3865892Sab196087 return (1);
3875892Sab196087 }
3885892Sab196087
3895892Sab196087 /* If we get here, the item was not in the list */
3905892Sab196087 return (0);
3915892Sab196087 }
3925892Sab196087
3935892Sab196087 /*
3945892Sab196087 * Search the APlist for an element with a given value, and
3955892Sab196087 * if not found, optionally append the element to the end of the list.
3965892Sab196087 *
3975892Sab196087 * entry:
3985892Sab196087 * lpp, ptr - As per aplist_insert().
3995892Sab196087 * init_arritems - As per aplist_insert() if a non-zero value.
4005892Sab196087 * A value of zero is special, and is taken to indicate
4015892Sab196087 * that no insert operation should be performed if
4025892Sab196087 * the item is not found in the list.
4035892Sab196087 *
4045892Sab196087 * exit
4055892Sab196087 * The given item is compared to every item in the given APlist.
4065892Sab196087 * If it is found, ALE_EXISTS is returned.
4075892Sab196087 *
4085892Sab196087 * If it is not found: If init_arr_items is False (0), then
4095892Sab196087 * ALE_NOTFOUND is returned. If init_arr_items is True, then
4105892Sab196087 * the item is appended to the list, and ALE_CREATE returned on success.
4115892Sab196087 *
4125892Sab196087 * On failure, which can only occur due to memory allocation failure,
4135892Sab196087 * ALE_ALLOCFAIL is returned.
4145892Sab196087 *
4155892Sab196087 * note:
4165892Sab196087 * The test operation used by this routine is a linear
4175892Sab196087 * O(N) operation, and is not efficient for more than a
4185892Sab196087 * few items.
4195892Sab196087 */
4205892Sab196087 aplist_test_t
aplist_test(APlist ** lpp,const void * ptr,Aliste init_arritems)4215892Sab196087 aplist_test(APlist **lpp, const void *ptr, Aliste init_arritems)
4225892Sab196087 {
4235892Sab196087 APlist *lp = *lpp;
4245892Sab196087 size_t idx;
4255892Sab196087
4265892Sab196087 /* Is the pointer already in the list? */
4275892Sab196087 if (lp != NULL)
4285892Sab196087 for (idx = 0; idx < lp->apl_nitems; idx++)
4295892Sab196087 if (ptr == lp->apl_data[idx])
4300Sstevel@tonic-gate return (ALE_EXISTS);
4310Sstevel@tonic-gate
4325892Sab196087 /* Is this a no-insert case? If so, report that the item is not found */
4335892Sab196087 if (init_arritems == 0)
4345892Sab196087 return (ALE_NOTFND);
4355892Sab196087
4365892Sab196087 /* Add it to the end of the list */
4375892Sab196087 if (aplist_append(lpp, ptr, init_arritems) == NULL)
4385892Sab196087 return (ALE_ALLOCFAIL);
4390Sstevel@tonic-gate return (ALE_CREATE);
4400Sstevel@tonic-gate }
4415892Sab196087
4425892Sab196087 /*
4435892Sab196087 * Reset the given list to its empty state. Any memory allocated by the
4445892Sab196087 * list is preserved, ready for reuse, but the list is set to its
4455892Sab196087 * empty state, equivalent to having called the delete operation for
4465892Sab196087 * every item.
4475892Sab196087 *
4485892Sab196087 * Note that no cleanup of the discarded items is done. The caller must
4495892Sab196087 * take care of any necessary cleanup before calling aplist_reset().
4505892Sab196087 */
4515892Sab196087 void
alist_reset(Alist * lp)4525892Sab196087 alist_reset(Alist *lp)
4535892Sab196087 {
4545892Sab196087 if (lp != NULL) {
4555892Sab196087 lp->al_nitems = 0;
4565892Sab196087 lp->al_next = ALIST_OFF_DATA;
4575892Sab196087 }
4585892Sab196087 }
4595892Sab196087
4605892Sab196087 void
aplist_reset(APlist * lp)4615892Sab196087 aplist_reset(APlist *lp)
4625892Sab196087 {
4635892Sab196087 if (lp != NULL)
4645892Sab196087 lp->apl_nitems = 0;
4655892Sab196087 }
466