Reworked hashtable create/destroy functions.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 05 Apr 2009 03:31:52 -0400
changeset 733 1b6d68fabe46
parent 732 e070fea1f8c7
child 734 1f69fc50c79c
Reworked hashtable create/destroy functions.
mojoshader_common.c
mojoshader_internal.h
--- a/mojoshader_common.c	Sun Apr 05 03:20:53 2009 -0400
+++ b/mojoshader_common.c	Sun Apr 05 03:31:52 2009 -0400
@@ -58,7 +58,7 @@
     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 @@
               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));
 
-    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 @@
     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 @@
     } // 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)
 {
--- a/mojoshader_internal.h	Sun Apr 05 03:20:53 2009 -0400
+++ b/mojoshader_internal.h	Sun Apr 05 03:31:52 2009 -0400
@@ -132,13 +132,13 @@
 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);