void tommy_arrayblk_grow(tommy_arrayblk* array, unsigned size) { unsigned block_max = (size + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE; unsigned block_mac = tommy_array_size(&array->block); if (block_mac < block_max) { /* grow the block array */ tommy_array_grow(&array->block, block_max); /* allocate new blocks */ while (block_mac < block_max) { void* ptr = tommy_malloc(TOMMY_ARRAYBLK_SIZE * sizeof(void*)); /* initializes it with zeros */ memset(ptr, 0, TOMMY_ARRAYBLK_SIZE * sizeof(void*)); /* set the new block */ tommy_array_set(&array->block, block_mac, ptr); ++block_mac; } } if (array->size < size) array->size = size; }
void test_array(void) { tommy_array array; unsigned i; tommy_array_init(&array); START("array init"); for(i=0;i<MAX*100;++i) { tommy_array_grow(&array, i + 1); if (tommy_array_get(&array, i) != 0) abort(); } STOP(); START("array set"); for(i=0;i<MAX*100;++i) { tommy_array_set(&array, i, (void*)i); } STOP(); START("array get"); for(i=0;i<MAX*100;++i) { if (tommy_array_get(&array, i) != (void*)i) abort(); } STOP(); tommy_array_done(&array); }
static void crcache_write (cr_cache_t *cache, kr_crate_t *crate, uint32_t idx) { kr_crate_t *cached_crate; if (idx >= pow (2,cache->alloc_exp)) { crcache_grow (cache); } cached_crate = malloc (sizeof (kr_crate_t)); memcpy (cached_crate,crate,sizeof (kr_crate_t)); tommy_array_set (cache->crates,idx,cached_crate); return; }