diff --git a/dhash/dhash.c b/dhash/dhash.c index 5f9f631..98439e8 100644 --- a/dhash/dhash.c +++ b/dhash/dhash.c @@ -44,7 +44,6 @@ #include #include #include -#include #include #include "dhash.h" @@ -71,9 +70,6 @@ } \ } while(0) -#define discard_const(ptr) ((void *)((intptr_t)(ptr))) -#define discard_const_p(type, ptr) ((type *)discard_const(ptr)) - /*****************************************************************************/ /************************** Internal Type Definitions ************************/ /*****************************************************************************/ @@ -682,10 +678,11 @@ int hash_destroy(hash_table_t *table) while (p != NULL) { q = p->next; hdelete_callback(table, HASH_TABLE_DESTROY, &p->entry); - if (p->entry.key.type == HASH_KEY_STRING) { + if (p->entry.key.type == HASH_KEY_STRING + || p->entry.key.type == HASH_KEY_CONST_STRING) { + /* Internally we do not use constant memory for keys + * in hash table elements. */ hfree(table, p->entry.key.str); - } else if (p->entry.key.type == HASH_KEY_CONST_STRING) { - hfree(table, discard_const(p->entry.key.c_str)); } hfree(table, (char *)p); p = q; @@ -986,7 +983,8 @@ int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value) element->entry.key.ul = key->ul; break; case HASH_KEY_STRING: - len = strlen(key->str)+1; + case HASH_KEY_CONST_STRING: + len = strlen(key->c_str) + 1; element->entry.key.str = halloc(table, len); if (element->entry.key.str == NULL) { hfree(table, element); @@ -994,15 +992,6 @@ int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value) } memcpy(element->entry.key.str, key->str, len); break; - case HASH_KEY_CONST_STRING: - len = strlen(key->c_str)+1; - element->entry.key.c_str = halloc(table, len); - if (element->entry.key.c_str == NULL) { - hfree(table, element); - return HASH_ERROR_NO_MEMORY; - } - memcpy(discard_const(element->entry.key.c_str), key->c_str, len); - break; } *chain = element; /* link into chain */ @@ -1093,10 +1082,9 @@ int hash_delete(hash_table_t *table, hash_key_t *key) return error; } } - if (element->entry.key.type == HASH_KEY_STRING) { + if (element->entry.key.type == HASH_KEY_STRING + || element->entry.key.type == HASH_KEY_CONST_STRING) { hfree(table, element->entry.key.str); - } else if (element->entry.key.type == HASH_KEY_CONST_STRING) { - hfree(table, discard_const(element->entry.key.c_str)); } hfree(table, element); return HASH_SUCCESS;