From a72ec6e91f497a1df0f0d3133a91a617221aaa4b Mon Sep 17 00:00:00 2001 From: Matthew Fernandez <matthew.fernandez@gmail.com> Date: Sat, 12 Jun 2021 13:34:29 -0700 Subject: [PATCH] avoid manual memory management of PairingHeap pointers in Block Not only does this free us from having to think about allocation tracking and memory leaks, because Block does not participate in any inheritance hierarchy we can also remove its explicit destructor. --- lib/vpsc/block.cpp | 19 +++++++------------ lib/vpsc/block.h | 8 ++++---- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/vpsc/block.cpp b/lib/vpsc/block.cpp index aac8c3fb9..8bb58a77b 100644 --- a/lib/vpsc/block.cpp +++ b/lib/vpsc/block.cpp @@ -17,6 +17,7 @@ * Adaptagrams repository. */ #include <cassert> +#include <memory> #include <vpsc/pairingheap/PairingHeap.h> #include <vpsc/constraint.h> #include <vpsc/block.h> @@ -42,8 +43,6 @@ void Block::addVariable(Variable *v) { Block::Block(Variable *v) { timeStamp=0; posn=weight=wposn=0; - in=nullptr; - out=nullptr; deleted=false; if(v!=nullptr) { v->offset=0; @@ -58,20 +57,16 @@ double Block::desiredWeightedPosition() { } return wp; } -Block::~Block() -{ - delete in; - delete out; -} void Block::setUpInConstraints() { setUpConstraintHeap(in,true); } void Block::setUpOutConstraints() { setUpConstraintHeap(out,false); } -void Block::setUpConstraintHeap(PairingHeap<Constraint*>* &h,bool in) { - delete h; - h = new PairingHeap<Constraint*>(&compareConstraints); +void Block::setUpConstraintHeap(std::unique_ptr<PairingHeap<Constraint*>> &h, + bool in) { + h = std::unique_ptr<PairingHeap<Constraint*>>( + new PairingHeap<Constraint*>(&compareConstraints)); for (Variable *v : vars) { vector<Constraint*> *cs=in?&(v->in):&(v->out); for (vector<Constraint*>::iterator j=cs->begin();j!=cs->end();j++) { @@ -133,7 +128,7 @@ void Block::mergeIn(Block *b) { // We check the top of the heaps to remove possible internal constraints findMinInConstraint(); b->findMinInConstraint(); - in->merge(b->in); + in->merge(b->in.get()); if (RECTANGLE_OVERLAP_LOGGING) { ofstream f(LOGFILE,ios::app); f<<" merged heap: "<<*in<<"\n"; @@ -142,7 +137,7 @@ void Block::mergeIn(Block *b) { void Block::mergeOut(Block *b) { findMinOutConstraint(); b->findMinOutConstraint(); - out->merge(b->out); + out->merge(b->out.get()); } Constraint *Block::findMinInConstraint() { Constraint *v = nullptr; diff --git a/lib/vpsc/block.h b/lib/vpsc/block.h index 3a5129b7b..e15a87758 100644 --- a/lib/vpsc/block.h +++ b/lib/vpsc/block.h @@ -19,6 +19,7 @@ #pragma once +#include <memory> #include <vector> #include <iostream> #include <vpsc/pairingheap/PairingHeap.h> @@ -35,7 +36,6 @@ public: double wposn; Block(Variable *v=nullptr); Block(const Block &) = delete; - ~Block(); Constraint* findMinLM(); Constraint* findMinLMBetween(Variable* lv, Variable* rv); Constraint* findMinInConstraint(); @@ -54,8 +54,8 @@ public: double cost(); bool deleted; long timeStamp; - PairingHeap<Constraint*> *in; - PairingHeap<Constraint*> *out; + std::unique_ptr<PairingHeap<Constraint*>> in; + std::unique_ptr<PairingHeap<Constraint*>> out; private: typedef enum {NONE, LEFT, RIGHT} Direction; typedef std::pair<double, Constraint*> Pair; @@ -67,5 +67,5 @@ private: bool canFollowRight(Constraint *c, Variable *last); void populateSplitBlock(Block *b, Variable *v, Variable *u); void addVariable(Variable *v); - void setUpConstraintHeap(PairingHeap<Constraint*>* &h,bool in); + void setUpConstraintHeap(std::unique_ptr<PairingHeap<Constraint*>> &h,bool in); }; -- 2.40.0