Skip to content

Commit 0cdaba9

Browse files
committed
pdn: use unique_ptr during copies and replacements
Signed-off-by: Peter Gadfort <[email protected]>
1 parent 09a2160 commit 0cdaba9

File tree

6 files changed

+51
-36
lines changed

6 files changed

+51
-36
lines changed

src/pdn/src/PdnGen.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void PdnGen::trimShapes()
195195
const bool is_pin_layer
196196
= pin_layers.find(shape->getLayer()) != pin_layers.end();
197197

198-
Shape* new_shape = nullptr;
198+
std::unique_ptr<Shape> new_shape = nullptr;
199199
const odb::Rect new_rect = shape->getMinimumRect();
200200
if (new_rect == shape->getRect()) { // no change to shape
201201
continue;
@@ -221,7 +221,7 @@ void PdnGen::trimShapes()
221221
}
222222
} else {
223223
if (!is_pin_layer) {
224-
component->replaceShape(shape.get(), {new_shape});
224+
component->replaceShape(shape.get(), std::move(new_shape));
225225
}
226226
}
227227
}

src/pdn/src/grid.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ bool Grid::repairVias(const Shape::ShapeTreeMap& global_shapes,
292292
return !shape->belongsTo(this);
293293
};
294294

295-
std::map<Shape*, Shape*> replace_shapes;
295+
std::map<Shape*, std::unique_ptr<Shape>> replace_shapes;
296296
for (const auto& via : vias_) {
297297
// ensure shapes belong to something
298298
const auto& lower_shape = via->getLowerShape();
@@ -313,28 +313,28 @@ bool Grid::repairVias(const Shape::ShapeTreeMap& global_shapes,
313313
}
314314

315315
if (lower_belongs_to_grid && lower_shape->isModifiable()) {
316-
auto* new_lower
316+
auto new_lower
317317
= lower_shape->extendTo(upper_shape->getRect(),
318318
obstructions[lower_shape->getLayer()],
319319
obs_filter);
320320
if (new_lower != nullptr) {
321-
replace_shapes[lower_shape.get()] = new_lower;
321+
replace_shapes[lower_shape.get()] = std::move(new_lower);
322322
}
323323
}
324324
if (upper_belongs_to_grid && upper_shape->isModifiable()) {
325-
auto* new_upper
325+
auto new_upper
326326
= upper_shape->extendTo(lower_shape->getRect(),
327327
obstructions[upper_shape->getLayer()],
328328
obs_filter);
329329
if (new_upper != nullptr) {
330-
replace_shapes[upper_shape.get()] = new_upper;
330+
replace_shapes[upper_shape.get()] = std::move(new_upper);
331331
}
332332
}
333333
}
334334

335-
for (const auto& [old_shape, new_shape] : replace_shapes) {
335+
for (auto& [old_shape, new_shape] : replace_shapes) {
336336
auto* component = old_shape->getGridComponent();
337-
component->replaceShape(old_shape, {new_shape});
337+
component->replaceShape(old_shape, std::move(new_shape));
338338
}
339339

340340
debugPrint(getLogger(),

src/pdn/src/grid_component.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,23 @@ void GridComponent::removeShape(Shape* shape)
193193
}
194194

195195
void GridComponent::replaceShape(Shape* shape,
196-
const std::vector<Shape*>& replacements)
196+
std::unique_ptr<Shape> replacement)
197+
{
198+
std::vector<std::unique_ptr<Shape>> replacements;
199+
replacements.push_back(std::move(replacement));
200+
replaceShape(shape, replacements);
201+
}
202+
203+
void GridComponent::replaceShape(
204+
Shape* shape,
205+
std::vector<std::unique_ptr<Shape>>& replacements)
197206
{
198207
auto vias = shape->getVias();
199208

200209
removeShape(shape);
201210

202-
for (auto* new_shape : replacements) {
203-
const auto& new_shape_ptr = addShape(new_shape);
211+
for (auto& new_shape : replacements) {
212+
const auto& new_shape_ptr = addShape(new_shape.release());
204213

205214
if (new_shape_ptr == nullptr) {
206215
continue;
@@ -217,6 +226,8 @@ void GridComponent::replaceShape(Shape* shape,
217226
}
218227
}
219228
}
229+
230+
replacements.clear();
220231
}
221232

222233
void GridComponent::getObstructions(
@@ -283,17 +294,17 @@ void GridComponent::cutShapes(const Shape::ObstructionTreeMap& obstructions)
283294
continue;
284295
}
285296
const auto& obs = obstructions.at(layer);
286-
std::map<Shape*, std::vector<Shape*>> replacement_shapes;
297+
std::map<Shape*, std::vector<std::unique_ptr<Shape>>> replacement_shapes;
287298
for (const auto& shape : shapes) {
288-
std::vector<Shape*> replacements;
299+
std::vector<std::unique_ptr<Shape>> replacements;
289300
if (!shape->cut(obs, getGrid(), replacements)) {
290301
continue;
291302
}
292303

293304
replacement_shapes[shape.get()] = std::move(replacements);
294305
}
295306

296-
for (const auto& [shape, replacement] : replacement_shapes) {
307+
for (auto& [shape, replacement] : replacement_shapes) {
297308
replaceShape(shape, replacement);
298309
}
299310
}

src/pdn/src/grid_component.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class GridComponent
5656
void getShapes(Shape::ShapeTreeMap& shapes) const;
5757
void removeShapes(Shape::ShapeTreeMap& shapes) const;
5858
void removeShape(Shape* shape);
59-
void replaceShape(Shape* shape, const std::vector<Shape*>& replacements);
59+
void replaceShape(Shape* shape, std::unique_ptr<Shape> replacement);
60+
void replaceShape(Shape* shape,
61+
std::vector<std::unique_ptr<Shape>>& replacements);
6062
void clearShapes() { shapes_.clear(); }
6163
int getShapeCount() const;
6264

src/pdn/src/shape.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ utl::Logger* Shape::getLogger() const
6262
return grid_component_->getLogger();
6363
}
6464

65-
Shape* Shape::copy() const
65+
std::unique_ptr<Shape> Shape::copy() const
6666
{
67-
auto* shape = new Shape(layer_, net_, rect_, type_);
67+
auto shape = std::make_unique<Shape>(layer_, net_, rect_, type_);
6868
shape->shape_type_ = shape_type_;
6969
shape->obs_ = obs_;
7070
shape->iterm_connections_ = iterm_connections_;
@@ -204,7 +204,7 @@ odb::Rect Shape::getMinimumRect() const
204204

205205
bool Shape::cut(const ObstructionTree& obstructions,
206206
const Grid* ignore_grid,
207-
std::vector<Shape*>& replacements) const
207+
std::vector<std::unique_ptr<Shape>>& replacements) const
208208
{
209209
return cut(
210210
obstructions, replacements, [ignore_grid](const ShapePtr& other) -> bool {
@@ -217,7 +217,7 @@ bool Shape::cut(const ObstructionTree& obstructions,
217217
}
218218

219219
bool Shape::cut(const ObstructionTree& obstructions,
220-
std::vector<Shape*>& replacements,
220+
std::vector<std::unique_ptr<Shape>>& replacements,
221221
const std::function<bool(const ShapePtr&)>& obs_filter) const
222222
{
223223
using namespace boost::polygon::operators;
@@ -306,11 +306,11 @@ bool Shape::cut(const ObstructionTree& obstructions,
306306
}
307307

308308
if (accept) {
309-
auto* new_shape = copy();
309+
auto new_shape = copy();
310310
new_shape->setRect(new_rect);
311311
new_shape->updateTermConnections();
312312

313-
replacements.push_back(new_shape);
313+
replacements.push_back(std::move(new_shape));
314314
}
315315
}
316316
return true;
@@ -593,12 +593,12 @@ std::string Shape::getRectText(const odb::Rect& rect, double dbu_to_micron)
593593
rect.yMax() / dbu_to_micron);
594594
}
595595

596-
Shape* Shape::extendTo(
596+
std::unique_ptr<Shape> Shape::extendTo(
597597
const odb::Rect& rect,
598598
const ObstructionTree& obstructions,
599599
const std::function<bool(const ShapePtr&)>& obs_filter) const
600600
{
601-
std::unique_ptr<Shape> new_shape(copy());
601+
std::unique_ptr<Shape> new_shape = copy();
602602

603603
if (isHorizontal()) {
604604
new_shape->rect_.set_xlo(std::min(rect_.xMin(), rect.xMin()));
@@ -626,7 +626,7 @@ Shape* Shape::extendTo(
626626
return nullptr;
627627
}
628628

629-
return new_shape.release();
629+
return new_shape;
630630
}
631631

632632
Shape::ShapeTreeMap Shape::convertVectorToTree(ShapeVectorMap& vec)
@@ -672,9 +672,10 @@ FollowPinShape::FollowPinShape(odb::dbTechLayer* layer,
672672
{
673673
}
674674

675-
Shape* FollowPinShape::copy() const
675+
std::unique_ptr<Shape> FollowPinShape::copy() const
676676
{
677-
auto* shape = new FollowPinShape(getLayer(), getNet(), getRect());
677+
auto shape
678+
= std::make_unique<FollowPinShape>(getLayer(), getNet(), getRect());
678679
shape->generateObstruction();
679680
shape->rows_ = rows_;
680681
return shape;
@@ -741,9 +742,10 @@ odb::Rect FollowPinShape::getMinimumRect() const
741742
return min_shape;
742743
}
743744

744-
bool FollowPinShape::cut(const ObstructionTree& obstructions,
745-
const Grid* ignore_grid,
746-
std::vector<Shape*>& replacements) const
745+
bool FollowPinShape::cut(
746+
const ObstructionTree& obstructions,
747+
const Grid* ignore_grid,
748+
std::vector<std::unique_ptr<Shape>>& replacements) const
747749
{
748750
return Shape::cut(
749751
obstructions, replacements, [](const ShapePtr& other) -> bool {

src/pdn/src/shape.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,18 @@ class Shape
175175
int getNumberOfConnectionsBelow() const;
176176
int getNumberOfConnectionsAbove() const;
177177

178-
Shape* extendTo(
178+
std::unique_ptr<Shape> extendTo(
179179
const odb::Rect& rect,
180180
const ObstructionTree& obstructions,
181181
const std::function<bool(const ShapePtr&)>& obs_filter
182182
= [](const ShapePtr&) { return true; }) const;
183183

184184
virtual bool cut(const ObstructionTree& obstructions,
185185
const Grid* ignore_grid,
186-
std::vector<Shape*>& replacements) const;
186+
std::vector<std::unique_ptr<Shape>>& replacements) const;
187187

188188
// return a copy of the shape
189-
virtual Shape* copy() const;
189+
virtual std::unique_ptr<Shape> copy() const;
190190
// merge this shape with another
191191
virtual void merge(Shape* shape);
192192

@@ -223,7 +223,7 @@ class Shape
223223

224224
protected:
225225
bool cut(const ObstructionTree& obstructions,
226-
std::vector<Shape*>& replacements,
226+
std::vector<std::unique_ptr<Shape>>& replacements,
227227
const std::function<bool(const ShapePtr&)>& obs_filter) const;
228228

229229
private:
@@ -261,7 +261,7 @@ class FollowPinShape : public Shape
261261
void addRow(odb::dbRow* row) { rows_.insert(row); }
262262

263263
odb::Rect getMinimumRect() const override;
264-
Shape* copy() const override;
264+
std::unique_ptr<Shape> copy() const override;
265265
void merge(Shape* shape) override;
266266
void updateTermConnections() override;
267267

@@ -274,7 +274,7 @@ class FollowPinShape : public Shape
274274

275275
bool cut(const ObstructionTree& obstructions,
276276
const Grid* ignore_grid,
277-
std::vector<Shape*>& replacements) const override;
277+
std::vector<std::unique_ptr<Shape>>& replacements) const override;
278278

279279
private:
280280
std::set<odb::dbRow*> rows_;

0 commit comments

Comments
 (0)