From ae63e45c652d44a92ba8c6d0dd653b5a90c28bda Mon Sep 17 00:00:00 2001 From: Payton Turnage Date: Tue, 14 Jan 2014 19:09:35 -0500 Subject: [PATCH] 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);