From 0aed7731210d0eb41c0ea767edb8067130cf6252 Mon Sep 17 00:00:00 2001 From: Frank Busse Date: Wed, 11 Sep 2019 21:27:11 +0100 Subject: refactor PTree: use unique_ptr --- lib/Core/PTree.cpp | 22 ++++++++++------------ lib/Core/PTree.h | 6 +++--- lib/Core/Searcher.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/Core/PTree.cpp b/lib/Core/PTree.cpp index 9747e814..91a6d883 100644 --- a/lib/Core/PTree.cpp +++ b/lib/Core/PTree.cpp @@ -18,16 +18,15 @@ using namespace klee; PTree::PTree(ExecutionState *initialState) { - root = new PTreeNode(nullptr, initialState); - initialState->ptreeNode = root; + root = std::make_unique(nullptr, initialState); } void PTree::attach(PTreeNode *node, ExecutionState *leftState, ExecutionState *rightState) { assert(node && !node->left && !node->right); node->state = nullptr; - node->left = new PTreeNode(node, leftState); - node->right = new PTreeNode(node, rightState); + node->left = std::make_unique(node, leftState); + node->right = std::make_unique(node, rightState); } void PTree::remove(PTreeNode *n) { @@ -35,14 +34,13 @@ void PTree::remove(PTreeNode *n) { do { PTreeNode *p = n->parent; if (p) { - if (n == p->left) { + if (n == p->left.get()) { p->left = nullptr; } else { - assert(n == p->right); + assert(n == p->right.get()); p->right = nullptr; } } - delete n; n = p; } while (n && !n->left && !n->right); } @@ -58,7 +56,7 @@ void PTree::dump(llvm::raw_ostream &os) { os << "\tnode [style=\"filled\",width=.1,height=.1,fontname=\"Terminus\"]\n"; os << "\tedge [arrowsize=.3]\n"; std::vector stack; - stack.push_back(root); + stack.push_back(root.get()); while (!stack.empty()) { const PTreeNode *n = stack.back(); stack.pop_back(); @@ -67,12 +65,12 @@ void PTree::dump(llvm::raw_ostream &os) { os << ",fillcolor=green"; os << "];\n"; if (n->left) { - os << "\tn" << n << " -> n" << n->left << ";\n"; - stack.push_back(n->left); + os << "\tn" << n << " -> n" << n->left.get() << ";\n"; + stack.push_back(n->left.get()); } if (n->right) { - os << "\tn" << n << " -> n" << n->right << ";\n"; - stack.push_back(n->right); + os << "\tn" << n << " -> n" << n->right.get() << ";\n"; + stack.push_back(n->right.get()); } } os << "}\n"; diff --git a/lib/Core/PTree.h b/lib/Core/PTree.h index 6456b57f..01632ebf 100644 --- a/lib/Core/PTree.h +++ b/lib/Core/PTree.h @@ -18,8 +18,8 @@ namespace klee { class PTreeNode { public: PTreeNode *parent = nullptr; - PTreeNode *left = nullptr; - PTreeNode *right = nullptr; + std::unique_ptr left; + std::unique_ptr right; ExecutionState *state = nullptr; PTreeNode(const PTreeNode&) = delete; @@ -29,7 +29,7 @@ namespace klee { class PTree { public: - PTreeNode * root = nullptr; + std::unique_ptr root; explicit PTree(ExecutionState *initialState); ~PTree() = default; diff --git a/lib/Core/Searcher.cpp b/lib/Core/Searcher.cpp index 8d35b7ef..0d5d61e2 100644 --- a/lib/Core/Searcher.cpp +++ b/lib/Core/Searcher.cpp @@ -264,19 +264,19 @@ RandomPathSearcher::~RandomPathSearcher() { ExecutionState &RandomPathSearcher::selectState() { unsigned flips=0, bits=0; - PTreeNode *n = executor.processTree->root; + PTreeNode *n = executor.processTree->root.get(); while (!n->state) { if (!n->left) { - n = n->right; + n = n->right.get(); } else if (!n->right) { - n = n->left; + n = n->left.get(); } else { if (bits==0) { flips = theRNG.getInt32(); bits = 32; } --bits; - n = (flips&(1<left : n->right; + n = (flips&(1<left.get() : n->right.get(); } } -- cgit 1.4.1