alist.h revision 57ef7aa924e4bfdf3118d9b5b4285dfc94b632f3
/*
* 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
* 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.
*
* Define an Alist, a list maintained as a reallocable array, and a for() loop
* macro to generalize its traversal. Note that the array can be reallocated
* as it is being traversed, thus the offset of each element is recomputed from
* the start of the structure.
*/
#ifndef _ALIST_H
#define _ALIST_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* An Alist implements array lists. The functionality is similar to
* that of a linked list. However, an Alist is represented by a single
* contigious allocation of memory. The head of the memory is a header
* that contains control information for the list. Following the header
* is an array used to hold the user data. In the type definitions that
* follow, we define these as an array with a single element, but when
* we allocate the memory, we actually allocate the amount of memory needed.
*
* There are two "flavors" of array list:
*
* Alist - Contain arbitrary data, usually structs.
* APlist - Contain pointers to data allocated elsewhere.
*
* This differentiation is useful, because pointer lists are heavily
* used, and support a slightly different set of operations that are
* unique to their purpose.
*
* Array lists are initially represented by a NULL pointer. The memory
* for the list is only allocated if an item is inserted. This is very
* efficient for data structures that may or may not be needed for a
* given linker operation --- you only pay for what you use. In addition:
*
* - Array lists grow as needed (memory is reallocated as necessary)
* - Data is kept contiguously (no unused holes in between elements)
* at the beginning of the data area. This locality has
* good cache behavior, as access to adjacent items are
* highly likely to be in the same page of memory.
* will cause a relatively expensive overlapped memory
* - As with any generic memory alloctor (i.e. malloc()/free()),
* array lists are not type safe for the data they contain.
* Data is managed as (void *) pointers to data of a given
* length, so the Alist module cannot prevent the caller from
* inserting/extracting the wrong type of data. The caller
* must guard against this.
* - To free an array list, simply call the standard free() function
* on the list pointer.
*/
/*
* Aliste is used to represent list indexes, offsets, and sizes.
*/
/*
* Alist is used to hold non-pointer items --- usually structs:
* - There must be an even number of Aliste fields before the
* al_data field. This ensures that al_data will have
* an alignment of 8, no matter whether sizeof(Aliste)
* is 4 or 8. That means that al_data will have sufficient
* alignment for any use, just like memory allocated via
* malloc().
* - al_nitems and al_next are redundant, in that they are
* directly related:
* al_next = al_nitems * al_size
* We do this to make ALIST_TRAVERSE_BYOFFSET maximally
* efficient. This doesn't waste space, because of the
* requirement to have an even # of Alist fields (above).
*
* Note that Alists allow the data to be referenced by 0 based array
* index, or by their byte offset from the start of the Alist memory
* allocation. The index form is preferred for most use, as it is simpler.
* However, by-offset access is used by rtld link maps, and this ability
* is convenient in that case.
*/
typedef struct {
} Alist;
/*
* APlist is a variant of Alist that contains pointers. There are several
* benefits to this special type:
* - API is simpler
* - Pointers are used directly, instead of requiring a
* pointer-to-pointer double indirection.
* - The implementation is slightly more efficient.
* - Operations that make particular sense for pointers
* can be supported without confusing the API for the
* regular Alists.
*/
typedef struct {
} APlist;
#ifdef _SYSCALL32 /* required by librtld_db */
typedef struct {
} APlist32;
#endif /* _SYSCALL32 */
/*
* The ALIST_OFF_DATA and APLIST_OFF_DATA macros give the byte offset
* from the start of an array list to the first byte of the data area
* used to hold user data. The same trick used by the standard offsetof()
* macro is used.
*/
/*
* The TRAVERSE macros are intended to be used within a for(), and
* cause the resulting loop to iterate over each item in the loop,
* in order.
* ALIST_TRAVERSE: Traverse over the items in an Alist,
* using the zero based item array index to refer to
* each item.
* ALIST_TRAVERSE_BY_OFFSET: Traverse over the items in an
* Alist using the byte offset from the head of the
* Alist pointer to refer to each item. It should be noted
* that the first such offset is given by ALIST_OFF_DATA,
* and as such, there will never be a 0 offset. Some code
* uses this fact to treat 0 as a reserved value with
* special meaning.
*
* By-offset access is convenient for some parts of
* rtld, where a value of 0 is used to indicate an
* uninitialized link map control.
*
* APLIST_TRAVERSE: Traverse over the pointers in an APlist, using
* the zero based item array index to refer to each pointer.
*/
/*
* Within the loop:
*
* LIST - Pointer to Alist structure for list
* IDX - The current item index
* OFF - The current item offset
* DATA - Pointer to item
*/
(IDX) = 0, \
\
\
(IDX)++, \
\
\
/*
* Within the loop:
*
* LIST - Pointer to APlist structure for list
* IDX - The current item index
* PTR - item value
*
* Note that this macro is designed to ensure that PTR retains the
* value of the final pointer in the list after exiting the for loop,
* and to avoid dereferencing an out of range address. This is done by
* doing the dereference in the middle expression, using the comma
* operator to ensure that a NULL pointer won't stop the loop.
*/
(IDX) = 0; \
\
\
(IDX)++
/*
* Possible values returned by aplist_test()
*/
typedef enum {
ALE_ALLOCFAIL = 0, /* Memory allocation error */
/*
* Access to an Alist item by index or offset. This is needed because the
* size of an item in an Alist is not known by the C compiler, and we
* have to do the indexing arithmetic explicitly.
*
* For an APlist, index the apl_data field directly --- No macro is needed.
*/
/*
* The number of items currently found in a list (nitems), and the total number
* of slots in the current data allocation (arritems). These macros handle the
* case where the list has not been allocated yet.
*/
extern void alist_reset(Alist *);
extern int aplist_delete_value(APlist *, const void *);
extern void *aplist_insert(APlist **, const void *,
extern void aplist_reset(APlist *);
#ifdef __cplusplus
}
#endif
#endif /* _ALIST_H */