From 0161f626a9efc9cb3430751ff1df8b288f1eeca5 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 5 Apr 2009 03:31:52 -0400 Subject: [PATCH] Reworked hashtable create/destroy functions. --- mojoshader_common.c | 18 ++++++++++++------ mojoshader_internal.h | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mojoshader_common.c b/mojoshader_common.c index ef58b9fb..07b580e6 100644 --- a/mojoshader_common.c +++ b/mojoshader_common.c @@ -58,7 +58,7 @@ int hash_insert(HashTable *table, const void *key, const void *value) return 1; } // hash_insert -int hash_init(HashTable *table, const uint32 initial_table_size, +HashTable *hash_create(const uint32 initial_table_size, const HashTable_HashFn hashfn, const HashTable_KeyMatchFn keymatchfn, const HashTable_NukeFn nukefn, @@ -66,11 +66,17 @@ int hash_init(HashTable *table, const uint32 initial_table_size, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) { const uint32 alloc_len = sizeof (HashItem *) * initial_table_size; - + HashTable *table = (HashTable *) m(sizeof (HashTable)); + if (table == NULL) + return 0; memset(table, '\0', sizeof (HashTable)); + table->table = (HashItem **) m(alloc_len, d); if (table->table == NULL) + { + f(table, d); return 0; + } // if memset(table->table, '\0', alloc_len); table->table_len = initial_table_size; @@ -82,9 +88,9 @@ int hash_init(HashTable *table, const uint32 initial_table_size, table->free = f; table->malloc_data = d; return 1; -} // hash_init +} // hash_create -void hash_deinit(HashTable *table) +void hash_destroy(HashTable *table) { uint32 i; for (i = 0; i < table->table_len; i++) @@ -100,8 +106,8 @@ void hash_deinit(HashTable *table) } // for table->free(table->table, table->malloc_data); - memset(table, '\0', sizeof (HashTable)); -} // hash_deinit + table->free(table, table->malloc_data); +} // hash_destroy int hash_remove(HashTable *table, const void *key) { diff --git a/mojoshader_internal.h b/mojoshader_internal.h index a15b6b83..1c9491e8 100644 --- a/mojoshader_internal.h +++ b/mojoshader_internal.h @@ -132,13 +132,13 @@ typedef uint32 (*HashTable_HashFn)(const void *key); typedef int (*HashTable_KeyMatchFn)(const void *a, const void *b); typedef void (*HashTable_NukeFn)(const void *key, const void *value); -int hash_init(HashTable *table, const uint32 initial_table_size, +HashTable *hash_create(const uint32 initial_table_size, const HashTable_HashFn hashfn, const HashTable_KeyMatchFn keymatchfn, const HashTable_NukeFn nukefn, const int stackable, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d); -void hash_deinit(HashTable *table); +void hash_destroy(HashTable *table); int hash_insert(HashTable *table, const void *key, const void *value); int hash_remove(HashTable *table, const void *key); int hash_find(const HashTable *table, const void *key, const void **_value);