static knh_map_t *hmap_init(CTX ctx, size_t init, const char *path, void *option) { knh_hmap_t *hmap = (knh_hmap_t*)KNH_MALLOC(ctx, sizeof(knh_hmap_t)); knh_bzero(hmap, sizeof(knh_hmap_t)); if(init < K_HASH_INITSIZE) init = K_HASH_INITSIZE; hmap->hentry = (knh_hentry_t**)KNH_REALLOC(ctx, hmap->DBGNAME, NULL, 0, init, sizeof(knh_hentry_t*)); hmap->hmax = init; hmap->size = 0; hmap->factor = KNH_HASH_FACTOR(hmap->hmax); return (knh_map_t*)hmap; }
void knh_Array_grow(CTX ctx, kArray *a, size_t newsize, size_t reqsize) { size_t capacity = a->dim->capacity; if(newsize < reqsize) newsize = reqsize; if(newsize == 0) return; if(capacity == 0) { size_t wsize = (Array_isNDATA(a)) ? sizeof(kunbox_t) : sizeof(Object*); a->dim = new_dim(ctx, newsize, wsize); DBG_ASSERT(a->list == NULL); } else { ((kdim_t*)a->dim)->capacity = newsize; } a->list = (kObject**)KNH_REALLOC(ctx, "Array", a->list, capacity, newsize, a->dim->wsize); }
void knh_Bytes_expands(CTX ctx, knh_Bytes_t *ba, size_t newsize) { if(ba->dim->capacity == 0) { newsize = k_goodsize(newsize); ba->bu.ubuf = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); ba->dim = new_dim(ctx, newsize, 1); } else { knh_uchar_t *ubuf = ba->bu.ubuf; ba->bu.ubuf = (knh_uchar_t*)KNH_REALLOC(ctx, ba->DBG_name, ba->bu.ubuf, ba->dim->capacity, newsize, 1); ((knh_dim_t*)ba->dim)->capacity = newsize; if(unlikely(ctx->bufa == ba)) { KNH_INFO(ctx, "newsize=%ld, pointer=(%p => %p)", newsize, ubuf, ba->bu.ubuf); Bytes_checkstack(ctx, ubuf, ubuf + ba->bu.len, ba->bu.ubuf); } } }
static knh_hmap_t *hmap_rehash(CTX ctx, knh_hmap_t *hmap) { size_t i, newhmax = hmap->hmax * 2 + 1; knh_hentry_t **hentry = knh_map_hentry(hmap); knh_hentry_t **newhentry = (knh_hentry_t**)KNH_REALLOC(ctx, hmap->DBGNAME, NULL, 0, newhmax, sizeof(knh_hentry_t*)); for(i = 0; i < hmap->hmax; i++) { knh_hentry_t *e = hentry[i]; while(e != NULL) { knh_hentry_t *p = e; knh_hashcode_t ni = p->hcode % newhmax; e = e->next; p->next = newhentry[ni]; newhentry[ni] = p; } } KNH_FREE(ctx, hentry, hmap->hmax * sizeof(knh_hentry_t*)); hmap->hentry = newhentry; hmap->hmax = newhmax; hmap->factor = KNH_HASH_FACTOR(newhmax); return hmap; }