/* * EXPOSED INTERFACE for the hashtable implementation * util_htnew(size) -- to make a new hashtable * util_htset(table, key, value, sizeof(value)) -- to set something in the table * util_htget(table, key) -- to get something from the table * util_htdel(table) -- to delete the table */ hash_table_t *util_htnew(size_t size) { hash_table_t *hashtable = NULL; stat_size_entry_t *find = NULL; if (size < 1) return NULL; if (!stat_size_hashtables) stat_size_hashtables = stat_size_new(); if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t)))) return NULL; if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) { mem_d(hashtable); return NULL; } if ((find = stat_size_get(stat_size_hashtables, size))) find->value++; else { stat_type_hashtables++; stat_size_put(stat_size_hashtables, size, 1); } hashtable->size = size; memset(hashtable->table, 0, sizeof(hash_node_t*) * size); stat_used_hashtables++; return hashtable; }
/* * The reallocate function for resizing vectors. */ void _util_vec_grow(void **a, size_t i, size_t s) { vector_t *d = vec_meta(*a); size_t m = 0; stat_size_entry_t *e = NULL; void *p = NULL; if (*a) { m = 2 * d->allocated + i; p = mem_r(d, s * m + sizeof(vector_t)); } else { m = i + 1; p = mem_a(s * m + sizeof(vector_t)); ((vector_t*)p)->used = 0; stat_used_vectors++; } if (!stat_size_vectors) stat_size_vectors = stat_size_new(); if ((e = stat_size_get(stat_size_vectors, s))) { e->value ++; } else { stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */ stat_type_vectors++; } *a = (vector_t*)p + 1; vec_meta(*a)->allocated = m; }
/* * The reallocate function for resizing vectors. */ void _util_vec_grow(void **a, size_t i, size_t s) { vector_t *d = (vector_t*)((char *)*a - IDENT_VEC_TOP); size_t m = 0; stat_size_entry_t *e = NULL; void *p = NULL; if (*a) { m = 2 * d->allocated + i; p = mem_r(d, s * m + IDENT_VEC_TOP); } else { m = i + 1; p = mem_a(s * m + IDENT_VEC_TOP); ((vector_t*)p)->used = 0; stat_used_vectors++; } if (!stat_size_vectors) stat_size_vectors = stat_size_new(); if ((e = stat_size_get(stat_size_vectors, s))) { e->value ++; } else { stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */ stat_type_vectors++; } d = (vector_t*)p; d->allocated = m; memcpy(d + 1, IDENT_VEC, IDENT_SIZE); *a = (void *)((char *)d + IDENT_VEC_TOP); }