From 75985b8fff7f9134a8459d7b6325bfe131de7a12 Mon Sep 17 00:00:00 2001 From: Theo Date: Thu, 29 Jan 2015 15:18:09 +0000 Subject: [PATCH] Corrected a memory leak problem --- library/tries/base_dbtries.c | 30 ++++++++++++++++++++++++------ library/tries/core_dbtries.c | 15 +++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/library/tries/base_dbtries.c b/library/tries/base_dbtries.c index 72a38603e..28cecde94 100644 --- a/library/tries/base_dbtries.c +++ b/library/tries/base_dbtries.c @@ -231,6 +231,7 @@ static TrData CURRENT_DEPTH_BREADTH_DATA; YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YAP_Int start_counter, YAP_Int *end_counter) { TrNode depth_node, breadth_node, nested_trie; + TrEntry otrie = CURRENT_TRIE; core_set_label_counter(start_counter); CURRENT_TRIE = db_trie; core_set_trie_db_return_term(YAP_MkAtomTerm(YAP_LookupAtom("false"))); @@ -248,6 +249,7 @@ YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YA set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie)); core_finalize_depth_breadth_trie(depth_node, breadth_node); *end_counter = core_get_label_counter(); + CURRENT_TRIE = otrie; return YAP_MkApplTerm((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(nested_trie))), 1, &TrNode_entry(nested_trie)); } // printf("breadth\n"); trie_print(trie); @@ -256,11 +258,13 @@ YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YA set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie)); core_finalize_depth_breadth_trie(depth_node, breadth_node); *end_counter = core_get_label_counter(); + CURRENT_TRIE = otrie; return YAP_MkApplTerm((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(nested_trie))), 1, &TrNode_entry(nested_trie)); } } core_finalize_depth_breadth_trie(depth_node, breadth_node); *end_counter = core_get_label_counter(); + CURRENT_TRIE = otrie; return core_get_trie_db_return_term(); } @@ -279,8 +283,10 @@ TrData trie_get_depth_breadth_reduction_current_data(void) { void trie_replace_nested_trie(TrEntry trie, YAP_Int nested_trie_id, YAP_Term new_term) { + TrEntry otrie = CURRENT_TRIE; CURRENT_TRIE = trie; core_depth_breadth_trie_replace_nested_trie(TrNode_child(TrEntry_trie(trie)), nested_trie_id, new_term, &trie_data_construct, &trie_data_destruct); + CURRENT_TRIE = otrie; return; } @@ -316,13 +322,17 @@ void simplification_reduction(TrEntry trie) { TrData stop_data, new_data, data = NULL; stop_data = TrData_previous(TrEntry_first_data(trie)); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); - while ((data != stop_data) && (data != NULL)) { + while (data && (data != stop_data) && (TrData_trie(data))) { node = core_simplification_reduction(TRIE_ENGINE, TrData_leaf(data), &trie_data_destruct); if (node) { new_trie_data(new_data, trie, node); PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data); + data = NULL; } - if (data == TrEntry_traverse_data(trie)) { + if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) { + data = TrData_previous(data); + TrEntry_traverse_data(trie) = data; + } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) { data = TrData_previous(data); TrEntry_traverse_data(trie) = data; } else @@ -338,7 +348,7 @@ TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) { stop_data = TrData_previous(TrEntry_first_data(trie)); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); - while (data != stop_data) { + while (data && (data != stop_data) && (TrData_trie(data))) { node = core_depth_reduction(TRIE_ENGINE, TrData_leaf(data), depth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction); if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) { //nested trie stop procedure return nested trie node @@ -347,8 +357,12 @@ TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) { if (node) { new_trie_data(new_data, trie, node); PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data); + data = NULL; } - if (data == TrEntry_traverse_data(trie)) { + if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) { + data = TrData_previous(data); + TrEntry_traverse_data(trie) = data; + } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) { data = TrData_previous(data); TrEntry_traverse_data(trie) = data; } else @@ -365,7 +379,7 @@ TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level) { stop_data = TrData_previous(TrEntry_first_data(trie)); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); - while ((data != stop_data) && (data != NULL)) { + while (data && (data != stop_data) && (TrData_trie(data))) { node = core_breadth_reduction(TRIE_ENGINE, TrData_leaf(data), breadth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction); if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) { //nested trie stop procedure return nested trie node @@ -374,8 +388,12 @@ TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level) { if (node) { new_trie_data(new_data, trie, node); PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data); + data = NULL; } - if (data == TrEntry_traverse_data(trie)) { + if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) { + data = TrData_previous(data); + TrEntry_traverse_data(trie) = data; + } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) { data = TrData_previous(data); TrEntry_traverse_data(trie) = data; } else diff --git a/library/tries/core_dbtries.c b/library/tries/core_dbtries.c index d34c2bf82..07ae1d6b0 100644 --- a/library/tries/core_dbtries.c +++ b/library/tries/core_dbtries.c @@ -215,7 +215,7 @@ TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term, void void check_attach_childs(TrNode parent, TrNode search_child, TrNode existing_child, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)); TrNode get_simplification_sibling(TrNode node); TrNode check_parent_first(TrNode node); -//TrNode TrNode_myparent(TrNode node); +TrNode TrNode_myparent(TrNode node); /* -------------------------- */ /* Debug Procedures */ @@ -411,6 +411,7 @@ TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term, void (* temp = TrNode_next(temp); } } + DATA_DESTRUCT_FUNCTION = destruct_function; TrNode_child(child) = NULL; remove_entry(child); return newnode; @@ -538,24 +539,24 @@ TrNode get_simplification_sibling(TrNode node) { TrNode check_parent_first(TrNode node) { TrNode simplification; - if (TrNode_entry(TrNode_parent(node)) != PairInitTag) { - simplification = check_parent_first(TrNode_parent(node)); + if (TrNode_entry(TrNode_myparent(node)) != PairInitTag) { + simplification = check_parent_first(TrNode_myparent(node)); if (simplification != NULL && TrNode_entry(simplification) == PairEndTag) return simplification; } simplification = get_simplification_sibling(node); return simplification; } -/*TrNode TrNode_myparent(TrNode node) { +TrNode TrNode_myparent(TrNode node) { TrNode parent = TrNode_parent(node); while (parent != NULL && IS_FUNCTOR_NODE(parent)) parent = TrNode_parent(parent); return parent; -}*/ +} TrNode core_simplification_reduction(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)) { /* Try to find the greatest parent that has a sibling that is a PairEndTag: this indicates a deep simplification */ - node = check_parent_first(TrNode_parent(node)); + node = check_parent_first(TrNode_myparent(node)); if (node != NULL) { /* do breadth reduction simplification */ node = TrNode_parent(node); @@ -631,6 +632,7 @@ TrNode core_depth_reduction(TrEngine engine, TrNode node, TrNode depth_node, YAP INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); temp = TrNode_parent(leaf); remove_child_nodes(TrNode_child(temp)); + TrNode_child(temp) = NULL; remove_entry(temp); return node; } @@ -774,6 +776,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, /* termination condition */ core_set_trie_db_return_term(get_return_node_term(TrNode_child(node))); node = TrNode_parent(node); + DATA_DESTRUCT_FUNCTION = destruct_function; remove_child_nodes(TrNode_child(node)); TrNode_child(node) = NULL; return NULL;