Skip to content

Commit

Permalink
Reworked hashtable create/destroy functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Apr 5, 2009
1 parent 493d81c commit 0161f62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions mojoshader_common.c
Expand Up @@ -58,19 +58,25 @@ 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,
const int stackable,
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;
Expand All @@ -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++)
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions mojoshader_internal.h
Expand Up @@ -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);
Expand Down

0 comments on commit 0161f62

Please sign in to comment.