Lines Matching refs:array

42 static kern_return_t array_init(KXLDArray *array, size_t itemsize, u_int nitems);
45 static u_int reinit_pools(KXLDArray *array, u_int nitems);
50 kxld_array_init(KXLDArray *array, size_t itemsize, u_int nitems)
58 check(array);
61 kxld_array_reset(array);
68 /* If the array has some pools, we need to see if there is enough space in
69 * those pools to accomodate the requested size array. If there isn't
71 * out the array structure. This will cause a new pool of sufficient size
75 if (array->npools) {
76 /* Update the array's maxitems based on the new itemsize */
77 array->pool_maxitems = (u_int) (array->pool_capacity / itemsize);
78 array->maxitems = 0;
79 STAILQ_FOREACH(srcpool, &array->pools, entries) {
80 array->maxitems += array->pool_maxitems;
84 * and zero out the array structure. Otherwise, rescan the pools to
87 if (array->maxitems < nitems) {
88 STAILQ_FOREACH_SAFE(srcpool, &array->pools, entries, tmp) {
89 STAILQ_REMOVE(&array->pools, srcpool, kxld_array_pool, entries);
92 srcpool_capacity = array->pool_capacity;
93 bzero(array, sizeof(*array));
95 nitems = reinit_pools(array, nitems);
100 array->itemsize = itemsize;
102 /* If array->maxitems is zero, it means we are either rebuilding an array
103 * that was too small, or we're initializing an array for the first time.
105 * if we're rebuilding an old array, we'll also copy the data from the old
108 if (array->maxitems == 0) {
110 rval = array_init(array, itemsize, nitems);
113 dstpool = STAILQ_FIRST(&array->pools);
128 if (rval) kxld_array_deinit(array);
133 * This may only be called to initialize (or reinitialize) an array with exactly
134 * zero or one pool. Calling this on an array with more than one pool is an
138 array_init(KXLDArray *array, size_t itemsize, u_int nitems)
144 require_action(array->npools < 2, finish, rval=KERN_INVALID_ARGUMENT);
146 array->itemsize = itemsize;
148 pool = STAILQ_FIRST(&array->pools);
150 require_action(itemsize * nitems < array->pool_capacity,
152 require_action(array->npools == 1, finish, rval=KERN_FAILURE);
153 bzero(pool->buffer, array->pool_capacity);
155 array->pool_capacity = round_page(array->itemsize * nitems);
157 pool = pool_create(array->pool_capacity);
159 STAILQ_INSERT_HEAD(&array->pools, pool, entries);
163 array->pool_maxitems = (u_int) (array->pool_capacity / array->itemsize);
164 array->maxitems = array->pool_maxitems;
165 array->nitems = nitems;
166 array->npools = 1;
221 /* When copying array, we only want to copy to an array with a single
222 * pool. If the array has more than one pool or the array is too small,
223 * we destroy the array and build it from scratch for the copy.
252 kxld_array_reset(KXLDArray *array)
256 if (array) {
257 STAILQ_FOREACH(pool, &array->pools, entries) {
260 array->nitems = 0;
267 kxld_array_clear(KXLDArray *array)
271 if (array) {
272 kxld_array_reset(array);
273 STAILQ_FOREACH(pool, &array->pools, entries) {
274 bzero(pool->buffer, array->pool_capacity);
282 kxld_array_deinit(KXLDArray *array)
286 if (array) {
287 STAILQ_FOREACH_SAFE(pool, &array->pools, entries, tmp) {
288 STAILQ_REMOVE(&array->pools, pool, kxld_array_pool, entries);
289 pool_destroy(pool, array->pool_capacity);
291 bzero(array, sizeof(*array));
298 kxld_array_get_item(const KXLDArray *array, u_int idx)
303 check(array);
305 if (idx >= array->nitems) goto finish;
307 STAILQ_FOREACH(pool, &array->pools, entries) {
309 item = (void *) (pool->buffer + (array->itemsize * idx));
313 idx -= array->pool_maxitems;
323 kxld_array_get_slot(const KXLDArray *array, u_int idx)
328 check(array);
330 if (idx >= array->maxitems) goto finish;
332 STAILQ_FOREACH(pool, &array->pools, entries) {
333 if (idx < array->pool_maxitems) {
334 item = (void *) (pool->buffer + (array->itemsize * idx));
338 idx -= array->pool_maxitems;
348 kxld_array_get_index(const KXLDArray *array, const void *item, u_int *_idx)
357 check(array);
363 STAILQ_FOREACH(pool, &array->pools, entries) {
364 if (pool->buffer <= it && it < pool->buffer + array->pool_capacity) {
366 idx = (u_int) (diff / array->itemsize);
375 base_idx += array->pool_maxitems;
386 kxld_array_resize(KXLDArray *array, u_int nitems)
393 while (nitems > array->maxitems) {
394 pool = pool_create(array->pool_capacity);
397 STAILQ_INSERT_TAIL(&array->pools, pool, entries);
399 array->maxitems += array->pool_maxitems;
400 array->npools += 1;
403 nitems = reinit_pools(array, nitems);
412 * Sets the number of items for the array and each pool. Returns zero if there
417 reinit_pools(KXLDArray *array, u_int nitems)
425 STAILQ_FOREACH(pool, &array->pools, entries) {
426 if (pool_nitems > array->pool_maxitems) {
427 pool->nitems = array->pool_maxitems;
428 pool_nitems -= array->pool_maxitems;
434 array->nitems = nitems;
442 kxld_array_remove(KXLDArray *array, u_int idx)
450 check(array);
452 if (idx >= array->nitems) {
460 require_action(array->npools < 2 || array->nitems < array->pool_maxitems,
463 pool = STAILQ_FIRST(&array->pools);
467 dst += idx * array->itemsize;
470 src += ((idx + 1) * array->itemsize);
473 memmove(dst, src, array->itemsize * nitems);
476 --array->nitems;
479 dst += pool->nitems * array->itemsize;
480 bzero(dst, array->itemsize);