Add destructor for hash_table

This commit is contained in:
Ian Romanick 2009-07-27 12:17:06 -07:00
parent 258f640eda
commit 0044d3ba94
2 changed files with 21 additions and 3 deletions

View file

@ -68,7 +68,8 @@ hash_table_ctor(unsigned num_buckets, hash_func_t hash,
num_buckets = 16;
}
ht = malloc(sizeof(*ht) + ((num_buckets - 1) * sizeof(ht->buckets[0])));
ht = _mesa_malloc(sizeof(*ht) + ((num_buckets - 1)
* sizeof(ht->buckets[0])));
if (ht != NULL) {
ht->hash = hash;
ht->compare = compare;
@ -83,6 +84,14 @@ hash_table_ctor(unsigned num_buckets, hash_func_t hash,
}
void
hash_table_dtor(struct hash_table *ht)
{
hash_table_clear(ht);
_mesa_free(ht);
}
void
hash_table_clear(struct hash_table *ht)
{
@ -94,7 +103,7 @@ hash_table_clear(struct hash_table *ht)
for (i = 0; i < ht->num_buckets; i++) {
foreach_s(node, temp, & ht->buckets[i]) {
remove_from_list(node);
free(node);
_mesa_free(node);
}
assert(is_empty_list(& ht->buckets[i]));
@ -128,7 +137,7 @@ hash_table_insert(struct hash_table *ht, void *data, const void *key)
const unsigned bucket = hash_value % ht->num_buckets;
struct hash_node *node;
node = calloc(1, sizeof(*node));
node = _mesa_calloc(1, sizeof(*node));
node->data = data;
node->key = key;

View file

@ -53,6 +53,15 @@ extern struct hash_table *hash_table_ctor(unsigned num_buckets,
hash_func_t hash, hash_compare_func_t compare);
/**
* Release all memory associated with a hash table
*
* \warning
* This function cannot release memory occupied either by keys or data.
*/
extern void hash_table_dtor(struct hash_table *ht);
/**
* Flush all entries from a hash table
*