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 #include <string.h>
29*c54f35caSApple OSS Distributions
30*c54f35caSApple OSS Distributions #if KERNEL
31*c54f35caSApple OSS Distributions #include <mach/vm_param.h>
32*c54f35caSApple OSS Distributions #else
33*c54f35caSApple OSS Distributions #include <mach/mach_init.h>
34*c54f35caSApple OSS Distributions #endif
35*c54f35caSApple OSS Distributions
36*c54f35caSApple OSS Distributions #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
37*c54f35caSApple OSS Distributions #include <AssertMacros.h>
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions #include "kxld_array.h"
40*c54f35caSApple OSS Distributions #include "kxld_util.h"
41*c54f35caSApple OSS Distributions
42*c54f35caSApple OSS Distributions static kern_return_t array_init(KXLDArray *array, size_t itemsize, u_int nitems);
43*c54f35caSApple OSS Distributions static KXLDArrayPool * pool_create(size_t capacity);
44*c54f35caSApple OSS Distributions static void pool_destroy(KXLDArrayPool *pool, size_t capacity);
45*c54f35caSApple OSS Distributions static u_int reinit_pools(KXLDArray *array, u_int nitems);
46*c54f35caSApple OSS Distributions
47*c54f35caSApple OSS Distributions /*******************************************************************************
48*c54f35caSApple OSS Distributions *******************************************************************************/
49*c54f35caSApple OSS Distributions kern_return_t
kxld_array_init(KXLDArray * array,size_t itemsize,u_int nitems)50*c54f35caSApple OSS Distributions kxld_array_init(KXLDArray *array, size_t itemsize, u_int nitems)
51*c54f35caSApple OSS Distributions {
52*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
53*c54f35caSApple OSS Distributions KXLDArrayPool *dstpool = NULL, *srcpool = NULL, *tmp = NULL;
54*c54f35caSApple OSS Distributions KXLDArrayHead srcpools = STAILQ_HEAD_INITIALIZER(srcpools);
55*c54f35caSApple OSS Distributions size_t srcpool_capacity = 0;
56*c54f35caSApple OSS Distributions u_long offset = 0;
57*c54f35caSApple OSS Distributions
58*c54f35caSApple OSS Distributions check(array);
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions if (!nitems) {
61*c54f35caSApple OSS Distributions kxld_array_reset(array);
62*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
63*c54f35caSApple OSS Distributions goto finish;
64*c54f35caSApple OSS Distributions }
65*c54f35caSApple OSS Distributions
66*c54f35caSApple OSS Distributions require_action(itemsize, finish, rval = KERN_INVALID_ARGUMENT);
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions /* If the array has some pools, we need to see if there is enough space in
69*c54f35caSApple OSS Distributions * those pools to accomodate the requested size array. If there isn't
70*c54f35caSApple OSS Distributions * enough space, we save the existing pools to a temporary STAILQ and zero
71*c54f35caSApple OSS Distributions * out the array structure. This will cause a new pool of sufficient size
72*c54f35caSApple OSS Distributions * to be created, and we then copy the data from the old pools into the new
73*c54f35caSApple OSS Distributions * pool.
74*c54f35caSApple OSS Distributions */
75*c54f35caSApple OSS Distributions if (array->npools) {
76*c54f35caSApple OSS Distributions /* Update the array's maxitems based on the new itemsize */
77*c54f35caSApple OSS Distributions array->pool_maxitems = (u_int) (array->pool_capacity / itemsize);
78*c54f35caSApple OSS Distributions array->maxitems = 0;
79*c54f35caSApple OSS Distributions STAILQ_FOREACH(srcpool, &array->pools, entries) {
80*c54f35caSApple OSS Distributions array->maxitems += array->pool_maxitems;
81*c54f35caSApple OSS Distributions }
82*c54f35caSApple OSS Distributions
83*c54f35caSApple OSS Distributions /* If there's not enough space, save the pools to a temporary STAILQ
84*c54f35caSApple OSS Distributions * and zero out the array structure. Otherwise, rescan the pools to
85*c54f35caSApple OSS Distributions * update their internal nitems counts.
86*c54f35caSApple OSS Distributions */
87*c54f35caSApple OSS Distributions if (array->maxitems < nitems) {
88*c54f35caSApple OSS Distributions STAILQ_FOREACH_SAFE(srcpool, &array->pools, entries, tmp) {
89*c54f35caSApple OSS Distributions STAILQ_REMOVE(&array->pools, srcpool, kxld_array_pool, entries);
90*c54f35caSApple OSS Distributions STAILQ_INSERT_TAIL(&srcpools, srcpool, entries);
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions srcpool_capacity = array->pool_capacity;
93*c54f35caSApple OSS Distributions bzero(array, sizeof(*array));
94*c54f35caSApple OSS Distributions } else {
95*c54f35caSApple OSS Distributions nitems = reinit_pools(array, nitems);
96*c54f35caSApple OSS Distributions require_action(nitems == 0, finish, rval = KERN_FAILURE);
97*c54f35caSApple OSS Distributions }
98*c54f35caSApple OSS Distributions }
99*c54f35caSApple OSS Distributions
100*c54f35caSApple OSS Distributions array->itemsize = itemsize;
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributions /* If array->maxitems is zero, it means we are either rebuilding an array
103*c54f35caSApple OSS Distributions * that was too small, or we're initializing an array for the first time.
104*c54f35caSApple OSS Distributions * In either case, we need to set up a pool of the requested size, and
105*c54f35caSApple OSS Distributions * if we're rebuilding an old array, we'll also copy the data from the old
106*c54f35caSApple OSS Distributions * pools into the new pool.
107*c54f35caSApple OSS Distributions */
108*c54f35caSApple OSS Distributions if (array->maxitems == 0) {
109*c54f35caSApple OSS Distributions rval = array_init(array, itemsize, nitems);
110*c54f35caSApple OSS Distributions require_noerr(rval, finish);
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions dstpool = STAILQ_FIRST(&array->pools);
113*c54f35caSApple OSS Distributions require_action(dstpool, finish, rval = KERN_FAILURE);
114*c54f35caSApple OSS Distributions
115*c54f35caSApple OSS Distributions STAILQ_FOREACH_SAFE(srcpool, &srcpools, entries, tmp) {
116*c54f35caSApple OSS Distributions memcpy(dstpool->buffer + offset, srcpool->buffer, srcpool_capacity);
117*c54f35caSApple OSS Distributions offset += srcpool_capacity;
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributions STAILQ_REMOVE(&srcpools, srcpool, kxld_array_pool, entries);
120*c54f35caSApple OSS Distributions pool_destroy(srcpool, srcpool_capacity);
121*c54f35caSApple OSS Distributions }
122*c54f35caSApple OSS Distributions }
123*c54f35caSApple OSS Distributions
124*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
125*c54f35caSApple OSS Distributions finish:
126*c54f35caSApple OSS Distributions if (rval) {
127*c54f35caSApple OSS Distributions kxld_array_deinit(array);
128*c54f35caSApple OSS Distributions }
129*c54f35caSApple OSS Distributions return rval;
130*c54f35caSApple OSS Distributions }
131*c54f35caSApple OSS Distributions
132*c54f35caSApple OSS Distributions /*******************************************************************************
133*c54f35caSApple OSS Distributions * This may only be called to initialize (or reinitialize) an array with exactly
134*c54f35caSApple OSS Distributions * zero or one pool. Calling this on an array with more than one pool is an
135*c54f35caSApple OSS Distributions * error.
136*c54f35caSApple OSS Distributions *******************************************************************************/
137*c54f35caSApple OSS Distributions static kern_return_t
array_init(KXLDArray * array,size_t itemsize,u_int nitems)138*c54f35caSApple OSS Distributions array_init(KXLDArray *array, size_t itemsize, u_int nitems)
139*c54f35caSApple OSS Distributions {
140*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
141*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
142*c54f35caSApple OSS Distributions
143*c54f35caSApple OSS Distributions require_action(itemsize, finish, rval = KERN_INVALID_ARGUMENT);
144*c54f35caSApple OSS Distributions require_action(array->npools < 2, finish, rval = KERN_INVALID_ARGUMENT);
145*c54f35caSApple OSS Distributions
146*c54f35caSApple OSS Distributions array->itemsize = itemsize;
147*c54f35caSApple OSS Distributions
148*c54f35caSApple OSS Distributions pool = STAILQ_FIRST(&array->pools);
149*c54f35caSApple OSS Distributions if (pool) {
150*c54f35caSApple OSS Distributions require_action(itemsize * nitems < array->pool_capacity,
151*c54f35caSApple OSS Distributions finish, rval = KERN_FAILURE);
152*c54f35caSApple OSS Distributions require_action(array->npools == 1, finish, rval = KERN_FAILURE);
153*c54f35caSApple OSS Distributions bzero(pool->buffer, array->pool_capacity);
154*c54f35caSApple OSS Distributions } else {
155*c54f35caSApple OSS Distributions array->pool_capacity = round_page(array->itemsize * nitems);
156*c54f35caSApple OSS Distributions
157*c54f35caSApple OSS Distributions pool = pool_create(array->pool_capacity);
158*c54f35caSApple OSS Distributions require_action(pool, finish, rval = KERN_RESOURCE_SHORTAGE);
159*c54f35caSApple OSS Distributions STAILQ_INSERT_HEAD(&array->pools, pool, entries);
160*c54f35caSApple OSS Distributions }
161*c54f35caSApple OSS Distributions pool->nitems = nitems;
162*c54f35caSApple OSS Distributions
163*c54f35caSApple OSS Distributions array->pool_maxitems = (u_int) (array->pool_capacity / array->itemsize);
164*c54f35caSApple OSS Distributions array->maxitems = array->pool_maxitems;
165*c54f35caSApple OSS Distributions array->nitems = nitems;
166*c54f35caSApple OSS Distributions array->npools = 1;
167*c54f35caSApple OSS Distributions
168*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
169*c54f35caSApple OSS Distributions finish:
170*c54f35caSApple OSS Distributions return rval;
171*c54f35caSApple OSS Distributions }
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions /*******************************************************************************
174*c54f35caSApple OSS Distributions *******************************************************************************/
175*c54f35caSApple OSS Distributions static KXLDArrayPool *
pool_create(size_t capacity)176*c54f35caSApple OSS Distributions pool_create(size_t capacity)
177*c54f35caSApple OSS Distributions {
178*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL, *rval = NULL;
179*c54f35caSApple OSS Distributions
180*c54f35caSApple OSS Distributions pool = kxld_calloc(sizeof(*pool));
181*c54f35caSApple OSS Distributions require(pool, finish);
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions pool->buffer = kxld_page_alloc(capacity);
184*c54f35caSApple OSS Distributions require(pool->buffer, finish);
185*c54f35caSApple OSS Distributions
186*c54f35caSApple OSS Distributions rval = pool;
187*c54f35caSApple OSS Distributions pool = NULL;
188*c54f35caSApple OSS Distributions
189*c54f35caSApple OSS Distributions finish:
190*c54f35caSApple OSS Distributions if (pool) {
191*c54f35caSApple OSS Distributions pool_destroy(pool, capacity);
192*c54f35caSApple OSS Distributions }
193*c54f35caSApple OSS Distributions return rval;
194*c54f35caSApple OSS Distributions }
195*c54f35caSApple OSS Distributions
196*c54f35caSApple OSS Distributions /*******************************************************************************
197*c54f35caSApple OSS Distributions *******************************************************************************/
198*c54f35caSApple OSS Distributions static void
pool_destroy(KXLDArrayPool * pool,size_t capacity)199*c54f35caSApple OSS Distributions pool_destroy(KXLDArrayPool *pool, size_t capacity)
200*c54f35caSApple OSS Distributions {
201*c54f35caSApple OSS Distributions if (pool) {
202*c54f35caSApple OSS Distributions if (pool->buffer) {
203*c54f35caSApple OSS Distributions kxld_page_free(pool->buffer, capacity);
204*c54f35caSApple OSS Distributions }
205*c54f35caSApple OSS Distributions kxld_free(pool, sizeof(*pool));
206*c54f35caSApple OSS Distributions }
207*c54f35caSApple OSS Distributions }
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions /*******************************************************************************
210*c54f35caSApple OSS Distributions *******************************************************************************/
211*c54f35caSApple OSS Distributions kern_return_t
kxld_array_copy(KXLDArray * dstarray,const KXLDArray * srcarray)212*c54f35caSApple OSS Distributions kxld_array_copy(KXLDArray *dstarray, const KXLDArray *srcarray)
213*c54f35caSApple OSS Distributions {
214*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
215*c54f35caSApple OSS Distributions KXLDArrayPool *dstpool = NULL, *srcpool = NULL;
216*c54f35caSApple OSS Distributions u_long needed_capacity = 0;
217*c54f35caSApple OSS Distributions u_long current_capacity = 0;
218*c54f35caSApple OSS Distributions u_long copysize = 0;
219*c54f35caSApple OSS Distributions u_long offset = 0;
220*c54f35caSApple OSS Distributions
221*c54f35caSApple OSS Distributions check(dstarray);
222*c54f35caSApple OSS Distributions check(srcarray);
223*c54f35caSApple OSS Distributions
224*c54f35caSApple OSS Distributions /* When copying array, we only want to copy to an array with a single
225*c54f35caSApple OSS Distributions * pool. If the array has more than one pool or the array is too small,
226*c54f35caSApple OSS Distributions * we destroy the array and build it from scratch for the copy.
227*c54f35caSApple OSS Distributions */
228*c54f35caSApple OSS Distributions needed_capacity = round_page(srcarray->nitems * srcarray->itemsize);
229*c54f35caSApple OSS Distributions current_capacity = dstarray->npools * dstarray->pool_capacity;
230*c54f35caSApple OSS Distributions if (dstarray->npools > 1 || needed_capacity > current_capacity) {
231*c54f35caSApple OSS Distributions kxld_array_deinit(dstarray);
232*c54f35caSApple OSS Distributions }
233*c54f35caSApple OSS Distributions
234*c54f35caSApple OSS Distributions rval = array_init(dstarray, srcarray->itemsize, srcarray->nitems);
235*c54f35caSApple OSS Distributions require_noerr(rval, finish);
236*c54f35caSApple OSS Distributions
237*c54f35caSApple OSS Distributions dstpool = STAILQ_FIRST(&dstarray->pools);
238*c54f35caSApple OSS Distributions require_action(dstpool, finish, rval = KERN_FAILURE);
239*c54f35caSApple OSS Distributions
240*c54f35caSApple OSS Distributions /* Copy the data from the source pools to the single destination pool. */
241*c54f35caSApple OSS Distributions STAILQ_FOREACH(srcpool, &srcarray->pools, entries) {
242*c54f35caSApple OSS Distributions copysize = srcpool->nitems * srcarray->itemsize;
243*c54f35caSApple OSS Distributions memcpy(dstpool->buffer + offset, srcpool->buffer, copysize);
244*c54f35caSApple OSS Distributions offset += copysize;
245*c54f35caSApple OSS Distributions }
246*c54f35caSApple OSS Distributions
247*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
248*c54f35caSApple OSS Distributions finish:
249*c54f35caSApple OSS Distributions return rval;
250*c54f35caSApple OSS Distributions }
251*c54f35caSApple OSS Distributions
252*c54f35caSApple OSS Distributions /*******************************************************************************
253*c54f35caSApple OSS Distributions *******************************************************************************/
254*c54f35caSApple OSS Distributions void
kxld_array_reset(KXLDArray * array)255*c54f35caSApple OSS Distributions kxld_array_reset(KXLDArray *array)
256*c54f35caSApple OSS Distributions {
257*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
258*c54f35caSApple OSS Distributions
259*c54f35caSApple OSS Distributions if (array) {
260*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
261*c54f35caSApple OSS Distributions pool->nitems = 0;
262*c54f35caSApple OSS Distributions }
263*c54f35caSApple OSS Distributions array->nitems = 0;
264*c54f35caSApple OSS Distributions }
265*c54f35caSApple OSS Distributions }
266*c54f35caSApple OSS Distributions
267*c54f35caSApple OSS Distributions /*******************************************************************************
268*c54f35caSApple OSS Distributions *******************************************************************************/
269*c54f35caSApple OSS Distributions void
kxld_array_clear(KXLDArray * array)270*c54f35caSApple OSS Distributions kxld_array_clear(KXLDArray *array)
271*c54f35caSApple OSS Distributions {
272*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
273*c54f35caSApple OSS Distributions
274*c54f35caSApple OSS Distributions if (array) {
275*c54f35caSApple OSS Distributions kxld_array_reset(array);
276*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
277*c54f35caSApple OSS Distributions bzero(pool->buffer, array->pool_capacity);
278*c54f35caSApple OSS Distributions }
279*c54f35caSApple OSS Distributions }
280*c54f35caSApple OSS Distributions }
281*c54f35caSApple OSS Distributions
282*c54f35caSApple OSS Distributions /*******************************************************************************
283*c54f35caSApple OSS Distributions *******************************************************************************/
284*c54f35caSApple OSS Distributions void
kxld_array_deinit(KXLDArray * array)285*c54f35caSApple OSS Distributions kxld_array_deinit(KXLDArray *array)
286*c54f35caSApple OSS Distributions {
287*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL, *tmp = NULL;
288*c54f35caSApple OSS Distributions
289*c54f35caSApple OSS Distributions if (array) {
290*c54f35caSApple OSS Distributions STAILQ_FOREACH_SAFE(pool, &array->pools, entries, tmp) {
291*c54f35caSApple OSS Distributions STAILQ_REMOVE(&array->pools, pool, kxld_array_pool, entries);
292*c54f35caSApple OSS Distributions pool_destroy(pool, array->pool_capacity);
293*c54f35caSApple OSS Distributions }
294*c54f35caSApple OSS Distributions bzero(array, sizeof(*array));
295*c54f35caSApple OSS Distributions }
296*c54f35caSApple OSS Distributions }
297*c54f35caSApple OSS Distributions
298*c54f35caSApple OSS Distributions /*******************************************************************************
299*c54f35caSApple OSS Distributions *******************************************************************************/
300*c54f35caSApple OSS Distributions void *
kxld_array_get_item(const KXLDArray * array,u_int idx)301*c54f35caSApple OSS Distributions kxld_array_get_item(const KXLDArray *array, u_int idx)
302*c54f35caSApple OSS Distributions {
303*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
304*c54f35caSApple OSS Distributions void *item = NULL;
305*c54f35caSApple OSS Distributions
306*c54f35caSApple OSS Distributions check(array);
307*c54f35caSApple OSS Distributions
308*c54f35caSApple OSS Distributions if (idx >= array->nitems) {
309*c54f35caSApple OSS Distributions goto finish;
310*c54f35caSApple OSS Distributions }
311*c54f35caSApple OSS Distributions
312*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
313*c54f35caSApple OSS Distributions if (idx < pool->nitems) {
314*c54f35caSApple OSS Distributions item = (void *) (pool->buffer + (array->itemsize * idx));
315*c54f35caSApple OSS Distributions break;
316*c54f35caSApple OSS Distributions }
317*c54f35caSApple OSS Distributions
318*c54f35caSApple OSS Distributions idx -= array->pool_maxitems;
319*c54f35caSApple OSS Distributions }
320*c54f35caSApple OSS Distributions
321*c54f35caSApple OSS Distributions finish:
322*c54f35caSApple OSS Distributions return item;
323*c54f35caSApple OSS Distributions }
324*c54f35caSApple OSS Distributions
325*c54f35caSApple OSS Distributions /*******************************************************************************
326*c54f35caSApple OSS Distributions *******************************************************************************/
327*c54f35caSApple OSS Distributions void *
kxld_array_get_slot(const KXLDArray * array,u_int idx)328*c54f35caSApple OSS Distributions kxld_array_get_slot(const KXLDArray *array, u_int idx)
329*c54f35caSApple OSS Distributions {
330*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
331*c54f35caSApple OSS Distributions void *item = NULL;
332*c54f35caSApple OSS Distributions
333*c54f35caSApple OSS Distributions check(array);
334*c54f35caSApple OSS Distributions
335*c54f35caSApple OSS Distributions if (idx >= array->maxitems) {
336*c54f35caSApple OSS Distributions goto finish;
337*c54f35caSApple OSS Distributions }
338*c54f35caSApple OSS Distributions
339*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
340*c54f35caSApple OSS Distributions if (idx < array->pool_maxitems) {
341*c54f35caSApple OSS Distributions item = (void *) (pool->buffer + (array->itemsize * idx));
342*c54f35caSApple OSS Distributions break;
343*c54f35caSApple OSS Distributions }
344*c54f35caSApple OSS Distributions
345*c54f35caSApple OSS Distributions idx -= array->pool_maxitems;
346*c54f35caSApple OSS Distributions }
347*c54f35caSApple OSS Distributions
348*c54f35caSApple OSS Distributions finish:
349*c54f35caSApple OSS Distributions return item;
350*c54f35caSApple OSS Distributions }
351*c54f35caSApple OSS Distributions
352*c54f35caSApple OSS Distributions /*******************************************************************************
353*c54f35caSApple OSS Distributions *******************************************************************************/
354*c54f35caSApple OSS Distributions kern_return_t
kxld_array_get_index(const KXLDArray * array,const void * item,u_int * _idx)355*c54f35caSApple OSS Distributions kxld_array_get_index(const KXLDArray *array, const void *item, u_int *_idx)
356*c54f35caSApple OSS Distributions {
357*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
358*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
359*c54f35caSApple OSS Distributions u_long diff = 0;
360*c54f35caSApple OSS Distributions u_int idx = 0;
361*c54f35caSApple OSS Distributions u_int base_idx = 0;
362*c54f35caSApple OSS Distributions const u_char *it;
363*c54f35caSApple OSS Distributions
364*c54f35caSApple OSS Distributions check(array);
365*c54f35caSApple OSS Distributions check(item);
366*c54f35caSApple OSS Distributions check(_idx);
367*c54f35caSApple OSS Distributions
368*c54f35caSApple OSS Distributions it = item;
369*c54f35caSApple OSS Distributions
370*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
371*c54f35caSApple OSS Distributions if (pool->buffer <= it && it < pool->buffer + array->pool_capacity) {
372*c54f35caSApple OSS Distributions diff = it - pool->buffer;
373*c54f35caSApple OSS Distributions idx = (u_int) (diff / array->itemsize);
374*c54f35caSApple OSS Distributions
375*c54f35caSApple OSS Distributions idx += base_idx;
376*c54f35caSApple OSS Distributions *_idx = idx;
377*c54f35caSApple OSS Distributions
378*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
379*c54f35caSApple OSS Distributions goto finish;
380*c54f35caSApple OSS Distributions }
381*c54f35caSApple OSS Distributions
382*c54f35caSApple OSS Distributions base_idx += array->pool_maxitems;
383*c54f35caSApple OSS Distributions }
384*c54f35caSApple OSS Distributions
385*c54f35caSApple OSS Distributions rval = KERN_FAILURE;
386*c54f35caSApple OSS Distributions finish:
387*c54f35caSApple OSS Distributions return rval;
388*c54f35caSApple OSS Distributions }
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions /*******************************************************************************
391*c54f35caSApple OSS Distributions *******************************************************************************/
392*c54f35caSApple OSS Distributions kern_return_t
kxld_array_resize(KXLDArray * array,u_int nitems)393*c54f35caSApple OSS Distributions kxld_array_resize(KXLDArray *array, u_int nitems)
394*c54f35caSApple OSS Distributions {
395*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
396*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions /* Grow the list of pools until we have enough to fit all of the entries */
399*c54f35caSApple OSS Distributions
400*c54f35caSApple OSS Distributions while (nitems > array->maxitems) {
401*c54f35caSApple OSS Distributions pool = pool_create(array->pool_capacity);
402*c54f35caSApple OSS Distributions require_action(pool, finish, rval = KERN_FAILURE);
403*c54f35caSApple OSS Distributions
404*c54f35caSApple OSS Distributions STAILQ_INSERT_TAIL(&array->pools, pool, entries);
405*c54f35caSApple OSS Distributions
406*c54f35caSApple OSS Distributions array->maxitems += array->pool_maxitems;
407*c54f35caSApple OSS Distributions array->npools += 1;
408*c54f35caSApple OSS Distributions }
409*c54f35caSApple OSS Distributions
410*c54f35caSApple OSS Distributions nitems = reinit_pools(array, nitems);
411*c54f35caSApple OSS Distributions require_action(nitems == 0, finish, rval = KERN_FAILURE);
412*c54f35caSApple OSS Distributions
413*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
414*c54f35caSApple OSS Distributions finish:
415*c54f35caSApple OSS Distributions return rval;
416*c54f35caSApple OSS Distributions }
417*c54f35caSApple OSS Distributions
418*c54f35caSApple OSS Distributions /*******************************************************************************
419*c54f35caSApple OSS Distributions * Sets the number of items for the array and each pool. Returns zero if there
420*c54f35caSApple OSS Distributions * is enough space for all items, and the number of additional items needed
421*c54f35caSApple OSS Distributions * if there is not enough space.
422*c54f35caSApple OSS Distributions *******************************************************************************/
423*c54f35caSApple OSS Distributions static u_int
reinit_pools(KXLDArray * array,u_int nitems)424*c54f35caSApple OSS Distributions reinit_pools(KXLDArray *array, u_int nitems)
425*c54f35caSApple OSS Distributions {
426*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
427*c54f35caSApple OSS Distributions u_int pool_nitems = 0;
428*c54f35caSApple OSS Distributions
429*c54f35caSApple OSS Distributions /* Set the number of items for each pool */
430*c54f35caSApple OSS Distributions
431*c54f35caSApple OSS Distributions pool_nitems = nitems;
432*c54f35caSApple OSS Distributions STAILQ_FOREACH(pool, &array->pools, entries) {
433*c54f35caSApple OSS Distributions if (pool_nitems > array->pool_maxitems) {
434*c54f35caSApple OSS Distributions pool->nitems = array->pool_maxitems;
435*c54f35caSApple OSS Distributions pool_nitems -= array->pool_maxitems;
436*c54f35caSApple OSS Distributions } else {
437*c54f35caSApple OSS Distributions pool->nitems = pool_nitems;
438*c54f35caSApple OSS Distributions pool_nitems = 0;
439*c54f35caSApple OSS Distributions }
440*c54f35caSApple OSS Distributions }
441*c54f35caSApple OSS Distributions array->nitems = nitems;
442*c54f35caSApple OSS Distributions
443*c54f35caSApple OSS Distributions return pool_nitems;
444*c54f35caSApple OSS Distributions }
445*c54f35caSApple OSS Distributions
446*c54f35caSApple OSS Distributions /*******************************************************************************
447*c54f35caSApple OSS Distributions *******************************************************************************/
448*c54f35caSApple OSS Distributions kern_return_t
kxld_array_remove(KXLDArray * array,u_int idx)449*c54f35caSApple OSS Distributions kxld_array_remove(KXLDArray *array, u_int idx)
450*c54f35caSApple OSS Distributions {
451*c54f35caSApple OSS Distributions kern_return_t rval = KERN_FAILURE;
452*c54f35caSApple OSS Distributions KXLDArrayPool *pool = NULL;
453*c54f35caSApple OSS Distributions u_char *dst = NULL;
454*c54f35caSApple OSS Distributions u_char *src = NULL;
455*c54f35caSApple OSS Distributions u_int nitems = 0;
456*c54f35caSApple OSS Distributions
457*c54f35caSApple OSS Distributions check(array);
458*c54f35caSApple OSS Distributions
459*c54f35caSApple OSS Distributions if (idx >= array->nitems) {
460*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
461*c54f35caSApple OSS Distributions goto finish;
462*c54f35caSApple OSS Distributions }
463*c54f35caSApple OSS Distributions
464*c54f35caSApple OSS Distributions /* We only support removing an item if all the items are contained in a
465*c54f35caSApple OSS Distributions * single pool (for now).
466*c54f35caSApple OSS Distributions */
467*c54f35caSApple OSS Distributions require_action(array->npools < 2 || array->nitems < array->pool_maxitems,
468*c54f35caSApple OSS Distributions finish, rval = KERN_NOT_SUPPORTED);
469*c54f35caSApple OSS Distributions
470*c54f35caSApple OSS Distributions pool = STAILQ_FIRST(&array->pools);
471*c54f35caSApple OSS Distributions require_action(pool, finish, rval = KERN_FAILURE);
472*c54f35caSApple OSS Distributions
473*c54f35caSApple OSS Distributions dst = pool->buffer;
474*c54f35caSApple OSS Distributions dst += idx * array->itemsize;
475*c54f35caSApple OSS Distributions
476*c54f35caSApple OSS Distributions src = pool->buffer;
477*c54f35caSApple OSS Distributions src += ((idx + 1) * array->itemsize);
478*c54f35caSApple OSS Distributions
479*c54f35caSApple OSS Distributions nitems = pool->nitems - idx - 1;
480*c54f35caSApple OSS Distributions memmove(dst, src, array->itemsize * nitems);
481*c54f35caSApple OSS Distributions
482*c54f35caSApple OSS Distributions --pool->nitems;
483*c54f35caSApple OSS Distributions --array->nitems;
484*c54f35caSApple OSS Distributions
485*c54f35caSApple OSS Distributions dst = pool->buffer;
486*c54f35caSApple OSS Distributions dst += pool->nitems * array->itemsize;
487*c54f35caSApple OSS Distributions bzero(dst, array->itemsize);
488*c54f35caSApple OSS Distributions
489*c54f35caSApple OSS Distributions rval = KERN_SUCCESS;
490*c54f35caSApple OSS Distributions finish:
491*c54f35caSApple OSS Distributions return rval;
492*c54f35caSApple OSS Distributions }
493