1*c54f35caSApple OSS Distributions /* 2*c54f35caSApple OSS Distributions * Copyright (c) 2008 Apple Inc. All rights reserved. 3*c54f35caSApple OSS Distributions * 4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*c54f35caSApple OSS Distributions * 6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*c54f35caSApple OSS Distributions * 15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at 16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*c54f35caSApple OSS Distributions * 18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are 19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and 24*c54f35caSApple OSS Distributions * limitations under the License. 25*c54f35caSApple OSS Distributions * 26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*c54f35caSApple OSS Distributions */ 28*c54f35caSApple OSS Distributions #ifndef _KXLD_ARRAY_H_ 29*c54f35caSApple OSS Distributions #define _KXLD_ARRAY_H_ 30*c54f35caSApple OSS Distributions 31*c54f35caSApple OSS Distributions #include <sys/queue.h> 32*c54f35caSApple OSS Distributions #include <sys/types.h> 33*c54f35caSApple OSS Distributions #if KERNEL 34*c54f35caSApple OSS Distributions #include <libkern/kxld_types.h> 35*c54f35caSApple OSS Distributions #else 36*c54f35caSApple OSS Distributions #include "kxld_types.h" 37*c54f35caSApple OSS Distributions #endif 38*c54f35caSApple OSS Distributions 39*c54f35caSApple OSS Distributions /******************************************************************************* 40*c54f35caSApple OSS Distributions * This is a resizeable array implementation designed primarily to maximize 41*c54f35caSApple OSS Distributions * memory reuse. The array should only be allocated once, but it can be 42*c54f35caSApple OSS Distributions * initialized many times. It persists its memory across initializations, and 43*c54f35caSApple OSS Distributions * reallocates only if it needs to grow the internal array, such that memory 44*c54f35caSApple OSS Distributions * allocation churn is eliminated. Growth is accomodated by building a linked 45*c54f35caSApple OSS Distributions * list of identically sized arrays. These arrays can be consolidated into 46*c54f35caSApple OSS Distributions * one large array in the init function. 47*c54f35caSApple OSS Distributions * 48*c54f35caSApple OSS Distributions * A technique commonly used in kxld is to make an array of objects that 49*c54f35caSApple OSS Distributions * themselves contain kxld_arrays. To minimize memory churn across links, only 50*c54f35caSApple OSS Distributions * the individual objects contained in an array should be cleared at the end of 51*c54f35caSApple OSS Distributions * each link, such that they are in a state ready for reinitialization with the 52*c54f35caSApple OSS Distributions * memory they have already allocated. The array that contains them should not 53*c54f35caSApple OSS Distributions * be cleared. After all links are complete, to ensure that all memory is 54*c54f35caSApple OSS Distributions * properly freed, one should call kxld_array_get_slot to walk the entire 55*c54f35caSApple OSS Distributions * allocated space of the array and clean up all potential instances contained 56*c54f35caSApple OSS Distributions * therein. Since this technique is somewhat fragile, there are certain 57*c54f35caSApple OSS Distributions * requirements that must be met, and guarantees that the array implementation 58*c54f35caSApple OSS Distributions * provides. 59*c54f35caSApple OSS Distributions * 60*c54f35caSApple OSS Distributions * Requirements: 61*c54f35caSApple OSS Distributions * - A newly allocated, uninitialized array object must be zeroed out before 62*c54f35caSApple OSS Distributions * it is initialized 63*c54f35caSApple OSS Distributions * - The objects stored in the array that will be reused must consider 64*c54f35caSApple OSS Distributions * being bzeroed a valid initial state. Specifially, they must check that 65*c54f35caSApple OSS Distributions * pointers they contain are nonnull before they are freed or followed 66*c54f35caSApple OSS Distributions * at both construction and destruction time. 67*c54f35caSApple OSS Distributions * 68*c54f35caSApple OSS Distributions * Guarantees: 69*c54f35caSApple OSS Distributions * - The init function will always bzero newly allocated memory. If memory 70*c54f35caSApple OSS Distributions * is added by resizing, it will bzero only the newly allocated portion. 71*c54f35caSApple OSS Distributions * - clear, deinit, and copy are the only functions that will change the 72*c54f35caSApple OSS Distributions * contents of initialized memory. 73*c54f35caSApple OSS Distributions * - The reset, clear, deinit functions will accept a NULL pointer to an array. 74*c54f35caSApple OSS Distributions *******************************************************************************/ 75*c54f35caSApple OSS Distributions 76*c54f35caSApple OSS Distributions STAILQ_HEAD(kxld_array_head, kxld_array_pool); 77*c54f35caSApple OSS Distributions 78*c54f35caSApple OSS Distributions struct kxld_array { 79*c54f35caSApple OSS Distributions struct kxld_array_head pools; 80*c54f35caSApple OSS Distributions size_t itemsize; /* The size of the items that the array contains */ 81*c54f35caSApple OSS Distributions size_t pool_capacity; /* The size of each pool's internal buffer */ 82*c54f35caSApple OSS Distributions u_int pool_maxitems; /* The maximum number of items each pool can hold 83*c54f35caSApple OSS Distributions * given the current size of each pool's buffer. 84*c54f35caSApple OSS Distributions */ 85*c54f35caSApple OSS Distributions u_int nitems; /* The current number of items this array contains */ 86*c54f35caSApple OSS Distributions u_int maxitems; /* The maximum number of items this array can contain */ 87*c54f35caSApple OSS Distributions u_int npools; /* The number of pools in the pool list */ 88*c54f35caSApple OSS Distributions }; 89*c54f35caSApple OSS Distributions 90*c54f35caSApple OSS Distributions struct kxld_array_pool { 91*c54f35caSApple OSS Distributions STAILQ_ENTRY(kxld_array_pool) entries; 92*c54f35caSApple OSS Distributions u_char *buffer; /* The internal memory buffer */ 93*c54f35caSApple OSS Distributions u_int nitems; /* The number of items the array contains */ 94*c54f35caSApple OSS Distributions }; 95*c54f35caSApple OSS Distributions 96*c54f35caSApple OSS Distributions typedef struct kxld_array KXLDArray; 97*c54f35caSApple OSS Distributions typedef struct kxld_array_head KXLDArrayHead; 98*c54f35caSApple OSS Distributions typedef struct kxld_array_pool KXLDArrayPool; 99*c54f35caSApple OSS Distributions 100*c54f35caSApple OSS Distributions /******************************************************************************* 101*c54f35caSApple OSS Distributions * Constructors and Destructors 102*c54f35caSApple OSS Distributions *******************************************************************************/ 103*c54f35caSApple OSS Distributions 104*c54f35caSApple OSS Distributions /* Initializes the array's capacity to a minimum of nitems * itemsize */ 105*c54f35caSApple OSS Distributions kern_return_t kxld_array_init(KXLDArray *array, size_t itemsize, u_int nitems) 106*c54f35caSApple OSS Distributions __attribute__((nonnull, visibility("hidden"))); 107*c54f35caSApple OSS Distributions 108*c54f35caSApple OSS Distributions /* Performs a deep copy of the array */ 109*c54f35caSApple OSS Distributions kern_return_t kxld_array_copy(KXLDArray *array, const KXLDArray *src) 110*c54f35caSApple OSS Distributions __attribute__((nonnull, visibility("hidden"))); 111*c54f35caSApple OSS Distributions 112*c54f35caSApple OSS Distributions /* Sets the number of items in the array to 0 */ 113*c54f35caSApple OSS Distributions void kxld_array_reset(KXLDArray *array) 114*c54f35caSApple OSS Distributions __attribute__((visibility("hidden"))); 115*c54f35caSApple OSS Distributions 116*c54f35caSApple OSS Distributions /* Zeroes out the array and sets nitems to 0 */ 117*c54f35caSApple OSS Distributions void kxld_array_clear(KXLDArray *array) 118*c54f35caSApple OSS Distributions __attribute__((visibility("hidden"))); 119*c54f35caSApple OSS Distributions 120*c54f35caSApple OSS Distributions /* Frees the array's internal buffer */ 121*c54f35caSApple OSS Distributions void kxld_array_deinit(KXLDArray *array) 122*c54f35caSApple OSS Distributions __attribute__((visibility("hidden"))); 123*c54f35caSApple OSS Distributions 124*c54f35caSApple OSS Distributions /******************************************************************************* 125*c54f35caSApple OSS Distributions * Accessors 126*c54f35caSApple OSS Distributions *******************************************************************************/ 127*c54f35caSApple OSS Distributions 128*c54f35caSApple OSS Distributions /* Returns the item at the specified index, or NULL if idx > nitems */ 129*c54f35caSApple OSS Distributions void *kxld_array_get_item(const KXLDArray *array, u_int idx) 130*c54f35caSApple OSS Distributions __attribute__((pure, nonnull, visibility("hidden"))); 131*c54f35caSApple OSS Distributions 132*c54f35caSApple OSS Distributions /* Returns the item at the specified index, or NULL if idx > maxitems */ 133*c54f35caSApple OSS Distributions void *kxld_array_get_slot(const KXLDArray *array, u_int idx) 134*c54f35caSApple OSS Distributions __attribute__((pure, nonnull, visibility("hidden"))); 135*c54f35caSApple OSS Distributions 136*c54f35caSApple OSS Distributions /* Returns the index of a specified item in the array */ 137*c54f35caSApple OSS Distributions kern_return_t kxld_array_get_index(const KXLDArray *array, const void *item, 138*c54f35caSApple OSS Distributions u_int *idx) 139*c54f35caSApple OSS Distributions __attribute__((nonnull, visibility("hidden"))); 140*c54f35caSApple OSS Distributions 141*c54f35caSApple OSS Distributions /******************************************************************************* 142*c54f35caSApple OSS Distributions * Modifiers 143*c54f35caSApple OSS Distributions *******************************************************************************/ 144*c54f35caSApple OSS Distributions 145*c54f35caSApple OSS Distributions /* Grows the array to contain a minimum of nitems. If extra memory is needed, 146*c54f35caSApple OSS Distributions * it will allocate a pool and add it to the list of pools maintained by this 147*c54f35caSApple OSS Distributions * array. 148*c54f35caSApple OSS Distributions */ 149*c54f35caSApple OSS Distributions kern_return_t kxld_array_resize(KXLDArray *array, u_int nitems) 150*c54f35caSApple OSS Distributions __attribute__((nonnull, visibility("hidden"))); 151*c54f35caSApple OSS Distributions 152*c54f35caSApple OSS Distributions /* Removes an element from the array. This is only supported for arrays with 153*c54f35caSApple OSS Distributions * a single pool. 154*c54f35caSApple OSS Distributions */ 155*c54f35caSApple OSS Distributions kern_return_t kxld_array_remove(KXLDArray *array, u_int idx) 156*c54f35caSApple OSS Distributions __attribute__((nonnull, visibility("hidden"))); 157*c54f35caSApple OSS Distributions 158*c54f35caSApple OSS Distributions #endif /* _KXLD_ARRAY_H_ */ 159