* ht foo = util_htnew(1024);
* int data = 100;
* char *test = "hello world\n";
- * util_htset(foo, "foo", (void*)&data, sizeof(int));
- * util_gtset(foo, "bar", (void*)test, strlen(test));
+ * util_htset(foo, "foo", (void*)&data);
+ * util_gtset(foo, "bar", (void*)test);
*
* printf("foo: %d, bar %s",
* *((int *)util_htget(foo, "foo")),
* util_htdel(foo);
*/
hash_table_t *util_htnew(size_t size);
-void util_htset(hash_table_t *ht, const char *key, void *value, size_t size);
+void util_htset(hash_table_t *ht, const char *key, void *value);
void *util_htget(hash_table_t *ht, const char *key);
void util_htdel(hash_table_t *ht);
/*===================================================================*/
* EXPOSED INTERFACE comment below
*/
typedef struct hash_node_t {
- char *key; /* the key for this node in table */
- void *value; /* allocated memory storing data */
- size_t size; /* size of data */
- struct hash_node_t *next; /* next node (linked list) */
+ char *key; /* the key for this node in table */
+ void *value; /* pointer to the data as void* */
+ struct hash_node_t *next; /* next node (linked list) */
} hash_node_t;
return val % ht->size;
}
-hash_node_t *_util_htnewpair(const char *key, void *value, size_t size) {
+hash_node_t *_util_htnewpair(const char *key, void *value) {
hash_node_t *node;
if (!(node = mem_a(sizeof(hash_node_t))))
return NULL;
return NULL;
}
- if (!(node->value = mem_a(size))) {
- mem_d(node->key);
- mem_d(node);
- return NULL;
- }
-
- memcpy(node->value, value, size);
- node->size = size;
- node->next = NULL;
+ node->value = value;
+ node->next = NULL;
return node;
}
return hashtable;
}
-void util_htset(hash_table_t *ht, const char *key, void *value, size_t size) {
+void util_htset(hash_table_t *ht, const char *key, void *value) {
size_t bin = 0;
hash_node_t *newnode = NULL;
hash_node_t *next = NULL;
/* already in table, do a replace */
if (next && next->key && !strcmp(key, next->key)) {
- mem_d(next->value);
- next->value = mem_a(size);
- next->size = size;
- memcpy(next->value, value, size);
+ next->value = value;
} else {
/* not found, grow a pair man :P */
- newnode = _util_htnewpair(key, value, size);
+ newnode = _util_htnewpair(key, value);
if (next == ht->table[bin]) {
newnode->next = next;
ht->table[bin] = newnode;
/* free in list */
while (n) {
- if (n->key) mem_d(n->key);
- if (n->value) mem_d(n->value);
+ if (n->key)
+ mem_d(n->key);
n = n->next;
}