Fix memory cleanup for hash tables.

This commit is contained in:
John McNamara 2014-06-27 02:50:47 +01:00
parent 063d3e81cf
commit 94890c06c0
2 changed files with 24 additions and 10 deletions

View File

@ -71,22 +71,22 @@ _insert_hash_element(lxw_hash_table *lxw_hash, void *key, void *value,
size_t key_len)
{
size_t hash_key = _generate_hash_key(key, key_len, lxw_hash->num_buckets);
struct lxw_hash_bucket_list *list;
lxw_hash_element *element;
struct lxw_hash_bucket_list *list = NULL;
lxw_hash_element *element = NULL;
if (!lxw_hash->buckets[hash_key]) {
/* The key isn't in the LXW_HASH hash table. */
/* Create a linked list in the bucket to hold the lxw_hash keys. */
list = calloc(1, sizeof(struct lxw_hash_bucket_list));
RETURN_ON_MEM_ERROR(list, NULL);
GOTO_LABEL_ON_MEM_ERROR(list, mem_error1);
/* Initialise the bucket linked list. */
SLIST_INIT(list);
/* Create an lxw_hash element to add to the linked list. */
element = calloc(1, sizeof(lxw_hash_element));
RETURN_ON_MEM_ERROR(element, NULL);
GOTO_LABEL_ON_MEM_ERROR(element, mem_error1);
/* Store the key and value. */
element->key = key;
@ -126,7 +126,7 @@ _insert_hash_element(lxw_hash_table *lxw_hash, void *key, void *value,
/* Key doesn't exist in the list so this is a hash collision.
* Create an lxw_hash element to add to the linked list. */
element = calloc(1, sizeof(lxw_hash_element));
RETURN_ON_MEM_ERROR(element, NULL);
GOTO_LABEL_ON_MEM_ERROR(element, mem_error2);
/* Store the key and value. */
element->key = key;
@ -143,6 +143,13 @@ _insert_hash_element(lxw_hash_table *lxw_hash, void *key, void *value,
return element;
}
mem_error1:
free(list);
mem_error2:
free(element);
return NULL;
}
/*

View File

@ -231,22 +231,22 @@ int32_t
_get_sst_index(lxw_sst *sst, const char *string)
{
size_t hash_key = _generate_sst_hash_key(string);
struct sst_bucket_list *list;
struct sst_element *element;
struct sst_bucket_list *list = NULL;
struct sst_element *element = NULL;
if (!sst->buckets[hash_key]) {
/* The string isn't in the SST SharedString hash table. */
/* Create a linked list in the bucket to hold the sst strings. */
list = calloc(1, sizeof(struct sst_bucket_list));
RETURN_ON_MEM_ERROR(list, -1);
GOTO_LABEL_ON_MEM_ERROR(list, mem_error1);
/* Initialise the bucket linked list. */
SLIST_INIT(list);
/* Create an sst element to add to the linked list. */
element = calloc(1, sizeof(struct sst_element));
RETURN_ON_MEM_ERROR(element, -1);
GOTO_LABEL_ON_MEM_ERROR(element, mem_error1);
/* Store the string and its index. */
element->index = sst->unique_count;
@ -285,7 +285,7 @@ _get_sst_index(lxw_sst *sst, const char *string)
/* String doesn't exist in the list so this is a hash collision.
* Create an sst element to add to the linked list. */
element = calloc(1, sizeof(struct sst_element));
RETURN_ON_MEM_ERROR(element, -1);
GOTO_LABEL_ON_MEM_ERROR(element, mem_error2);
/* Store the string and its index. */
element->index = sst->unique_count;
@ -303,4 +303,11 @@ _get_sst_index(lxw_sst *sst, const char *string)
return element->index;
}
mem_error1:
free(list);
mem_error2:
free(element);
return -1;
}