From ae63e45c652d44a92ba8c6d0dd653b5a90c28bda Mon Sep 17 00:00:00 2001 From: Payton Turnage Date: Tue, 14 Jan 2014 19:09:35 -0500 Subject: [PATCH 1/3] Rename htab functions; rewrite find to be generic. The htab functions find_str and add_str have been renamed to include _ref in the places they previously noted _str. These are intended to manage an htab containing references to any data; string or not. The htab_find function has been divided up. htab_find_core now handles actually finding the correct node. htab_find and htab_find_ref are just outward facing functions for retrieving a specific kind of data from the node, depending on what the htab is used for. --- include/tvm/tvm_htab.h | 4 ++-- libtvm/tvm_htab.c | 48 ++++++++++++++++++++++----------------- libtvm/tvm_lexer.c | 2 +- libtvm/tvm_preprocessor.c | 2 +- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/include/tvm/tvm_htab.h b/include/tvm/tvm_htab.h index 7838565..eb9c4be 100644 --- a/include/tvm/tvm_htab.h +++ b/include/tvm/tvm_htab.h @@ -23,8 +23,8 @@ tvm_htab_t* htab_create(); void htab_destroy(tvm_htab_t *htab); int htab_add(tvm_htab_t *htab, const char *key, int value); -int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len); +int htab_add_ref(tvm_htab_t *htab, const char *key, const void *valptr, int len); int htab_find(tvm_htab_t *htab, const char *key); -char *htab_find_str(tvm_htab_t *htab, const char *key); +char *htab_find_ref(tvm_htab_t *htab, const char *key); #endif diff --git a/libtvm/tvm_htab.c b/libtvm/tvm_htab.c index 635d3c3..3d5fa44 100644 --- a/libtvm/tvm_htab.c +++ b/libtvm/tvm_htab.c @@ -69,7 +69,7 @@ static void htab_rehash(tvm_htab_t *orig, unsigned int size) next = node->next; if (node->valptr) { - htab_add_str(new, node->key, node->valptr, strlen(node->valptr) + 1); + htab_add_ref(new, node->key, node->valptr, strlen(node->valptr) + 1); free(node->valptr); } else @@ -124,7 +124,7 @@ int htab_add(tvm_htab_t *htab, const char *k, int v) return hash; } -int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len) +int htab_add_ref(tvm_htab_t *htab, const char *key, const void *valptr, int len) { int hash = htab_add(htab, key, 0); int found = 0; @@ -143,32 +143,38 @@ int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len) return hash; } -int htab_find(tvm_htab_t *htab, const char *key) +static tvm_htab_node_t *htab_find_core(tvm_htab_t *htab, const char *key) { int hash = htab_hash(key, htab->size); tvm_htab_node_t *node = htab->nodes[hash]; - while(node) + while (node) { if(!strcmp(node->key, key)) - return node->value; - node = node->next; - } - - return -1; -} - -char *htab_find_str(tvm_htab_t *htab, const char *key) -{ - int hash = htab_hash(key, htab->size); - tvm_htab_node_t *node = htab->nodes[hash]; - - while(node) - { - if(!strcmp(node->key, key)) - return node->valptr; - node = node->next; + return node; + else + node = node->next; } return NULL; } + +int htab_find(tvm_htab_t *htab, const char *key) +{ + tvm_htab_node_t *node = htab_find_core(htab, key); + + if(!node) + return -1; + + return node->value; +} + +char *htab_find_ref(tvm_htab_t *htab, const char *key) +{ + tvm_htab_node_t *node = htab_find_core(htab, key); + + if (!node) + return NULL; + + return node->valptr; +} diff --git a/libtvm/tvm_lexer.c b/libtvm/tvm_lexer.c index b03d49e..a856fb3 100644 --- a/libtvm/tvm_lexer.c +++ b/libtvm/tvm_lexer.c @@ -60,7 +60,7 @@ void lex(tvm_lexer_t *lexer, char *source, tvm_htab_t *defines) for(j = 0; (pToken && j < MAX_TOKENS); j++) { - char *token = htab_find_str(defines, pToken); + char *token = htab_find_ref(defines, pToken); token = token ? token : pToken; lexer->tokens[i][j] = (char *)calloc(1, (strlen(token) + 1)); diff --git a/libtvm/tvm_preprocessor.c b/libtvm/tvm_preprocessor.c index 09d58be..a4b41eb 100644 --- a/libtvm/tvm_preprocessor.c +++ b/libtvm/tvm_preprocessor.c @@ -86,7 +86,7 @@ int tvm_preprocess(char *src, int *src_len, tvm_htab_t *defines) } if(htab_find(defines, keystr) < 0) - htab_add_str(defines, keystr, valstr, strlen(valstr) + 1); + htab_add_ref(defines, keystr, valstr, strlen(valstr) + 1); else { printf("Multiple definitions for %s.\n", keystr); From 584217a142d7151b1fa4fddedf75fbd51611489b Mon Sep 17 00:00:00 2001 From: Payton Turnage Date: Tue, 14 Jan 2014 20:03:41 -0500 Subject: [PATCH 2/3] Modify htab_add to have a generic core. --- libtvm/tvm_htab.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/libtvm/tvm_htab.c b/libtvm/tvm_htab.c index 3d5fa44..2d4af11 100644 --- a/libtvm/tvm_htab.c +++ b/libtvm/tvm_htab.c @@ -87,7 +87,7 @@ static void htab_rehash(tvm_htab_t *orig, unsigned int size) free(new); } -int htab_add(tvm_htab_t *htab, const char *k, int v) +static tvm_htab_node_t *htab_add_core(tvm_htab_t *htab, const char *k) { int hash = htab_hash(k, htab->size); tvm_htab_node_t *node = htab->nodes[hash]; @@ -109,8 +109,6 @@ int htab_add(tvm_htab_t *htab, const char *k, int v) node->key = (char *)calloc((strlen(k) + 1), sizeof(char)); strcpy(node->key, k); - node->value = v; - if(prev) prev->next = node; else htab->nodes[hash] = node; /* root node */ @@ -121,26 +119,32 @@ int htab_add(tvm_htab_t *htab, const char *k, int v) if((float)++htab->num_nodes / htab->size > HTAB_LOAD_FACTOR) htab_rehash(htab, htab->num_nodes * 2); - return hash; + return node; +} + +int htab_add(tvm_htab_t *htab, const char *key, int value) +{ + tvm_htab_node_t *node = htab_add_core(htab, key); + + if (!node) + return -1; + + node->value = value; + + return 0; } int htab_add_ref(tvm_htab_t *htab, const char *key, const void *valptr, int len) { - int hash = htab_add(htab, key, 0); - int found = 0; - tvm_htab_node_t *node = htab->nodes[hash]; + tvm_htab_node_t *node = htab_add_core(htab, key); - while (node && !found) - { - if (!strcmp(node->key, key)) - found = 1; - else - node = node->next; - } + if (!node) + return -1; node->valptr = calloc(len, sizeof(char)); memcpy(node->valptr, valptr, len); - return hash; + + return 0; } static tvm_htab_node_t *htab_find_core(tvm_htab_t *htab, const char *key) From daa02f50823e961663f7d28905ffc1161d133f08 Mon Sep 17 00:00:00 2001 From: Payton Turnage Date: Tue, 14 Jan 2014 20:54:31 -0500 Subject: [PATCH 3/3] Fix bug where an outdate node is returned. If htab_add_core added a node that happened to push the htab past its load factor, it would return a pointer to where the added node was *before* the rehash. Now it does not do this. --- libtvm/tvm_htab.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libtvm/tvm_htab.c b/libtvm/tvm_htab.c index 2d4af11..3ad5db3 100644 --- a/libtvm/tvm_htab.c +++ b/libtvm/tvm_htab.c @@ -89,6 +89,11 @@ static void htab_rehash(tvm_htab_t *orig, unsigned int size) static tvm_htab_node_t *htab_add_core(tvm_htab_t *htab, const char *k) { + /* Increase bucket count and rehash if the + load factor is too high */ + if((float)++htab->num_nodes / htab->size > HTAB_LOAD_FACTOR) + htab_rehash(htab, htab->num_nodes * 2); + int hash = htab_hash(k, htab->size); tvm_htab_node_t *node = htab->nodes[hash]; tvm_htab_node_t *prev = NULL; @@ -114,11 +119,6 @@ static tvm_htab_node_t *htab_add_core(tvm_htab_t *htab, const char *k) node->next = NULL; - /* Increase bucket count and rehash if the - load factor is too high */ - if((float)++htab->num_nodes / htab->size > HTAB_LOAD_FACTOR) - htab_rehash(htab, htab->num_nodes * 2); - return node; }