From: Dale Weiler Date: Fri, 23 Nov 2012 20:01:58 +0000 (+0000) Subject: No copy, just store pointer for hash table X-Git-Tag: 0.1.9~304 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=6e047ec65b05c000d509e470e03db4fe37492127;p=xonotic%2Fgmqcc.git No copy, just store pointer for hash table --- diff --git a/gmqcc.h b/gmqcc.h index dc3de39..5cbf7fe 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -280,8 +280,8 @@ typedef struct hash_table_t { * 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")), @@ -291,7 +291,7 @@ typedef struct hash_table_t { * 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); /*===================================================================*/ diff --git a/util.c b/util.c index 9144778..ef20764 100644 --- a/util.c +++ b/util.c @@ -519,10 +519,9 @@ void _util_vec_grow(void **a, size_t i, size_t s) { * 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; @@ -542,7 +541,7 @@ size_t _util_hthash(hash_table_t *ht, const char *key) { 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; @@ -552,15 +551,8 @@ hash_node_t *_util_htnewpair(const char *key, void *value, size_t size) { 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; } @@ -591,7 +583,7 @@ hash_table_t *util_htnew(size_t size) { 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; @@ -605,13 +597,10 @@ void util_htset(hash_table_t *ht, const char *key, void *value, size_t size) { /* 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; @@ -648,8 +637,8 @@ void util_htdel(hash_table_t *ht) { /* 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; }