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