int dt_reallocp_array(void *ptr, size_t nmemb, size_t size) { void *val; memcpy(&val, ptr, sizeof(val)); val = dt_realloc_f(val, nmemb, size); memcpy(ptr, &val, sizeof(val)); if (!val && nmemb && size) { return DTERROR(ENOMEM); } return 0; }
/* make/resize hash table */ static int htable(Dt_t* dt) { Dtlink_t **htbl, **t, **endt, *l, *next; ssize_t n, k; Dtdisc_t *disc = dt->disc; Dthash_t *hash = (Dthash_t*)dt->data; if((n = hash->tblz) > 0 && (hash->type&H_FIXED) ) return 0; /* fixed size table */ if(disc && disc->eventf) /* let user have input */ { if((*disc->eventf)(dt, DT_HASHSIZE, &n, disc) > 0 ) { if(n < 0) /* fix table size */ { hash->type |= H_FIXED; n = -n; /* desired table size */ if(hash->tblz >= n ) /* table size is fixed now */ return 0; } } } /* table size should be a power of 2 */ n = n < HLOAD(hash->data.size) ? HLOAD(hash->data.size) : n; for(k = (1<<DT_HTABLE); k < n; ) k *= 2; if((n = k) <= hash->tblz) return 0; /* allocate new table */ if(!(htbl = (Dtlink_t**)(*dt->memoryf)(dt, 0, n*sizeof(Dtlink_t*), disc)) ) { DTERROR(dt, "Error in allocating an extended hash table"); return -1; } memset(htbl, 0, n*sizeof(Dtlink_t*)); /* move objects into new table */ for(endt = (t = hash->htbl) + hash->tblz; t < endt; ++t) { for(l = *t; l; l = next) { next = l->_rght; l->_rght = htbl[k = l->_hash&(n-1)]; htbl[k] = l; } } if(hash->htbl) /* free old table and set new table */ (void)(*dt->memoryf)(dt, hash->htbl, 0, disc); hash->htbl = htbl; hash->tblz = n; return 0; }
int dt_reallocp(void *ptr, size_t size) { void *val; if (!size) { dt_freep(ptr); return 0; } memcpy(&val, ptr, sizeof(val)); val = dt_realloc(val, size); if (!val) { dt_freep(ptr); return DTERROR(ENOMEM); } memcpy(ptr, &val, sizeof(val)); return 0; }