From: Matthew Fernandez Date: Sat, 12 Jun 2021 20:34:29 +0000 (-0700) Subject: avoid manual memory management of PairingHeap pointers in Block X-Git-Tag: 2.48.0~44^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a72ec6e91f497a1df0f0d3133a91a617221aaa4b;p=graphviz 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. --- 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 +#include #include #include #include @@ -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* &h,bool in) { - delete h; - h = new PairingHeap(&compareConstraints); +void Block::setUpConstraintHeap(std::unique_ptr> &h, + bool in) { + h = std::unique_ptr>( + new PairingHeap(&compareConstraints)); for (Variable *v : vars) { vector *cs=in?&(v->in):&(v->out); for (vector::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 #include #include #include @@ -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 *in; - PairingHeap *out; + std::unique_ptr> in; + std::unique_ptr> out; private: typedef enum {NONE, LEFT, RIGHT} Direction; typedef std::pair 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* &h,bool in); + void setUpConstraintHeap(std::unique_ptr> &h,bool in); };