From 1624a9a06f0e520c0c23113e4f6257ec39402f8b Mon Sep 17 00:00:00 2001 From: hladu357 Date: Thu, 6 Jun 2024 13:51:53 +0200 Subject: [PATCH] cleanup added --- trie/trie.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/trie/trie.c b/trie/trie.c index e162be0..bd47ced 100644 --- a/trie/trie.c +++ b/trie/trie.c @@ -2,10 +2,10 @@ #define RULES_FILE "../data/routing-data" #define TEST_FILE "../data/test-data" -#define TEST_ROWS 70 /*17217*/ +#define TEST_ROWS 11 /*17217*/ #define READ_MODE "r" #define RULE_NOT_FOUND -1 -#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1ul << (15 - bit % 16)) ) != 0 ) +#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1u << (15 - bit % 16)) ) != 0 ) trie_node_t create_node() { @@ -20,6 +20,20 @@ trie_holder_t create_holder() { return ret; } +int destory_holder(struct trie_holder* t) { + destory_node(t->_root); + free(t); + return 1; +} + +int destory_node(struct trie_node* n) { + int ret = 0; + if (!n) return ret; + ret = destory_node(n->_l) + destory_node(n->_r); + free(n); + return ret; +} + int trie_insert(trie_holder_t t, ipv6_t key, int val, int mask) { trie_node_t iter = t->_root; trie_node_t* next = &t->_root; @@ -103,6 +117,7 @@ int load_input(trie_holder_t t) { trie_insert(t, key, val, mask); ++cnt; } + fclose(f); return cnt; } @@ -129,7 +144,8 @@ int load_tests(ipv6_t** rips, int** rref) { while (fgetc(f) != ' '); fscanf(f, "%d", &(ref[row])); } - + fclose(f); + *rips = ips; *rref = ref; return TEST_ROWS; @@ -146,6 +162,8 @@ int fire_tests(trie_holder_t t, ipv6_t* ips, int* ref, int num_tests ) { } } + free(ips); + free(ref); return wrong; } @@ -158,6 +176,6 @@ int main() { printf("Loaded %d tests\n", load_tests(&ips, &ref) ); printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) ); - + destory_holder(t); return 0; } \ No newline at end of file