From: Sven Verdoolaege Date: Tue, 3 Mar 2009 14:54:03 +0000 (+0100) Subject: isl_hash_table_init: take minimal size instead of number of bits needed X-Git-Tag: isl-0.01~260 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e7812f74adb57bd319c7454da44b9e515f370ba3;p=platform%2Fupstream%2Fisl.git isl_hash_table_init: take minimal size instead of number of bits needed --- diff --git a/include/isl_hash.h b/include/isl_hash.h index b850751..a4d85ba 100644 --- a/include/isl_hash.h +++ b/include/isl_hash.h @@ -42,7 +42,7 @@ struct isl_hash_table { struct isl_ctx; int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table, - int init_bits); + int min_size); void isl_hash_table_clear(struct isl_hash_table *table); struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx, struct isl_hash_table *table, diff --git a/isl_hash.c b/isl_hash.c index 3a39bbc..6c4ed42 100644 --- a/isl_hash.c +++ b/isl_hash.c @@ -9,17 +9,28 @@ uint32_t isl_hash_string(uint32_t hash, const char *s) return hash; } +static unsigned int round_up(unsigned int v) +{ + int old_v = v; + + while (v) { + old_v = v; + v ^= v & -v; + } + return old_v << 1; +} + int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table, - int init_bits) + int min_size) { size_t size; if (!table) return -1; - if (init_bits < 2) - init_bits = 2; - table->bits = init_bits; + if (min_size < 2) + min_size = 2; + table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1; table->n = 0; size = 2 << table->bits;