]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
No copy, just store pointer for hash table
authorDale Weiler <killfieldengine@gmail.com>
Fri, 23 Nov 2012 20:01:58 +0000 (20:01 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 23 Nov 2012 20:01:58 +0000 (20:01 +0000)
gmqcc.h
util.c

diff --git a/gmqcc.h b/gmqcc.h
index dc3de394495d5396a8ecd94eb411939f59e882be..5cbf7fe121628c7d8eef80cd400cff2bd22fdce1 100644 (file)
--- 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 91447785fbf6a06eea3801be35942f00dd0b5b81..ef20764e78896932a181e8b88a0c372af35f322f 100644 (file)
--- 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;
         }