Skip to content

Commit e0d028b

Browse files
authored
Merge pull request #9246 from The-OpenROAD-Project-staging/odb-tmg-clean
Odb tmg clean
2 parents 1ff1206 + 8bb407e commit e0d028b

File tree

2 files changed

+59
-112
lines changed

2 files changed

+59
-112
lines changed

src/odb/src/db/tmg_conn_g.cpp

Lines changed: 49 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,33 @@
33

44
#include "tmg_conn_g.h"
55

6-
#include <algorithm>
76
#include <cstdio>
87
#include <cstdlib>
98
#include <memory>
9+
#include <vector>
1010

11-
#include "dbCommon.h"
12-
#include "odb/db.h"
13-
#include "odb/dbShape.h"
14-
#include "odb/dbWireCodec.h"
1511
#include "tmg_conn.h"
16-
#include "utl/Logger.h"
1712

1813
namespace odb {
1914

20-
tmg_conn_graph::tmg_conn_graph()
15+
void tmg_conn_graph::init(const int ptN, const int shortN)
2116
{
22-
ptNmax_ = 1024;
23-
shortNmax_ = 1024;
24-
eNmax_ = 1024;
25-
ptV_ = (tcg_pt*) safe_malloc(ptNmax_ * sizeof(tcg_pt));
26-
path_vis_ = (int*) safe_malloc(ptNmax_ * sizeof(int));
27-
eV_ = (tcg_edge*) safe_malloc(2UL * ptNmax_ * sizeof(tcg_edge));
28-
stackV_ = (tcg_edge**) safe_malloc(shortNmax_ * sizeof(tcg_edge*));
29-
}
17+
ptV_.resize(ptN);
18+
for (tcg_pt& pt : ptV_) {
19+
pt.edges = nullptr;
20+
}
3021

31-
tmg_conn_graph::~tmg_conn_graph()
32-
{
33-
free(ptV_);
34-
free(path_vis_);
35-
free(eV_);
36-
free(static_cast<void*>(stackV_));
37-
}
22+
stackV_.clear();
23+
stackV_.reserve(2ul * shortN);
3824

39-
void tmg_conn_graph::init(const int ptN, const int shortN)
40-
{
41-
if (ptN > ptNmax_) {
42-
ptNmax_ = 2 * ptN;
43-
free(ptV_);
44-
ptV_ = (tcg_pt*) safe_malloc(ptNmax_ * sizeof(tcg_pt));
45-
free(path_vis_);
46-
path_vis_ = (int*) safe_malloc(ptNmax_ * sizeof(int));
47-
}
48-
if (shortN > shortNmax_) {
49-
shortNmax_ = 2 * shortN;
50-
free(static_cast<void*>(stackV_));
51-
stackV_ = (tcg_edge**) safe_malloc(shortNmax_ * sizeof(tcg_edge*));
52-
}
53-
if (4 * ptN + 2 * shortN > eNmax_) {
54-
eNmax_ *= 2;
55-
eNmax_ = std::max((4 * ptN) + (2 * shortN), eNmax_);
56-
free(eV_);
57-
eV_ = (tcg_edge*) safe_malloc(eNmax_ * sizeof(tcg_edge));
58-
}
59-
eN_ = 0;
60-
for (int j = 0; j < ptN; j++) {
61-
ptV_[j].edges = nullptr;
62-
}
63-
ptN_ = ptN;
25+
eV_.clear();
6426
}
6527

6628
tcg_edge* tmg_conn_graph::newEdge(const tmg_conn* conn,
6729
const int fr,
6830
const int to)
6931
{
70-
tcg_edge* e = eV_ + eN_++;
32+
tcg_edge* e = &eV_.emplace_back();
7133
e->k = -1;
7234
e->skip = false;
7335
const int ndx = conn->pt(to).x;
@@ -96,7 +58,7 @@ tcg_edge* tmg_conn_graph::newShortEdge(const tmg_conn* conn,
9658
const int fr,
9759
const int to)
9860
{
99-
tcg_edge* e = eV_ + eN_++;
61+
tcg_edge* e = &eV_.emplace_back();
10062
e->k = -1;
10163
e->skip = false;
10264
const int ned = conn->ptDist(fr, to);
@@ -129,11 +91,11 @@ tcg_edge* tmg_conn_graph::newShortEdge(const tmg_conn* conn,
12991

13092
void tmg_conn_graph::clearVisited()
13193
{
132-
for (int j = 0; j < eN_; j++) {
133-
eV_[j].visited = false;
94+
for (tcg_edge& edge : eV_) {
95+
edge.visited = false;
13496
}
135-
for (int j = 0; j < ptN_; j++) {
136-
ptV_[j].visited = 0;
97+
for (tcg_pt& pt : ptV_) {
98+
pt.visited = 0;
13799
}
138100
}
139101

@@ -170,8 +132,8 @@ bool tmg_conn_graph::isBadShort(tcg_edge* pe, const tmg_conn* conn)
170132

171133
void tmg_conn_graph::relocateShorts(tmg_conn* conn)
172134
{
173-
for (int jp = 0; jp < ptN_; jp++) {
174-
tcg_edge* pe = pt(jp).edges;
135+
for (tcg_pt& pt : ptV_) {
136+
tcg_edge* pe = pt.edges;
175137
if (pe == nullptr || pe->next == nullptr) {
176138
continue;
177139
}
@@ -182,7 +144,7 @@ void tmg_conn_graph::relocateShorts(tmg_conn* conn)
182144
bool firstCheck = true;
183145
tcg_edge* pppe = nullptr;
184146
tcg_edge* ppe = nullptr;
185-
for (pe = pt(jp).edges; pe != nullptr; pe = pe->next) {
147+
for (pe = pt.edges; pe != nullptr; pe = pe->next) {
186148
if (ppe == nullptr) {
187149
ppe = pe;
188150
continue;
@@ -208,7 +170,7 @@ void tmg_conn_graph::relocateShorts(tmg_conn* conn)
208170
if (pppe) {
209171
pppe->next = pe;
210172
} else {
211-
pt(jp).edges = pe;
173+
pt.edges = pe;
212174
}
213175
pppe = pe;
214176
pe->next = ppe;
@@ -224,13 +186,13 @@ void tmg_conn_graph::relocateShorts(tmg_conn* conn)
224186
}
225187
}
226188
// re-assign "skip".
227-
for (int jp = 0; jp < ptN_; jp++) {
189+
for (tcg_pt& pt : ptV_) {
228190
tcg_edge* skipe = nullptr;
229191
int noshortn = 0;
230192
int shortn = 0;
231193
tcg_edge* plast = nullptr;
232194
tcg_edge* last = nullptr;
233-
for (tcg_edge* pe = pt(jp).edges; pe != nullptr; pe = pe->next) {
195+
for (tcg_edge* pe = pt.edges; pe != nullptr; pe = pe->next) {
234196
if (!pe->s) {
235197
noshortn++;
236198
continue;
@@ -261,7 +223,7 @@ void tmg_conn_graph::relocateShorts(tmg_conn* conn)
261223
}
262224
// plast->to and last->to is the short pair to skip
263225
// do skip new pair;
264-
tcg_edge* nse = pt(plast->to).edges;
226+
tcg_edge* nse = ptV_[plast->to].edges;
265227
while (nse != nullptr && nse->to != last->to) {
266228
nse = nse->next;
267229
}
@@ -300,13 +262,11 @@ tcg_edge* tmg_conn_graph::getFirstNonShortEdge(int& jstart)
300262
if (loops == 0) {
301263
e = nullptr;
302264
}
265+
stackV_.clear();
303266
if (!e) {
304-
stackV_[0] = nullptr;
305-
stackN_ = 0;
306267
return nullptr;
307268
}
308-
stackV_[0] = e;
309-
stackN_ = 1;
269+
stackV_.push_back(e);
310270
return e;
311271
}
312272

@@ -322,27 +282,21 @@ tcg_edge* tmg_conn_graph::getFirstEdge(const int jstart)
322282
if (!e) {
323283
return nullptr;
324284
}
325-
stackV_[0] = e;
326-
stackN_ = 1;
285+
stackV_.emplace_back(e);
327286
return e;
328287
}
329288

330289
tcg_edge* tmg_conn_graph::getNextEdge(const bool ok_to_descend)
331290
{
332-
tcg_edge* e = stackV_[stackN_ - 1];
291+
tcg_edge* e = stackV_.back();
333292

334293
if (ok_to_descend) {
335294
tcg_edge* e2 = pt(e->to).edges;
336295
while (e2 && (e2->visited || e2->skip)) {
337296
e2 = e2->next;
338297
}
339298
if (e2) {
340-
if (stackN_ >= shortNmax_) {
341-
shortNmax_ = shortNmax_ * 2;
342-
stackV_ = (tcg_edge**) realloc(static_cast<void*>(stackV_),
343-
shortNmax_ * sizeof(tcg_edge*));
344-
}
345-
stackV_[stackN_++] = e2;
299+
stackV_.emplace_back(e2);
346300
return e2;
347301
}
348302
}
@@ -356,21 +310,23 @@ tcg_edge* tmg_conn_graph::getNextEdge(const bool ok_to_descend)
356310
e = e->next;
357311
}
358312
if (e) {
359-
stackV_[stackN_ - 1] = e;
313+
stackV_.back() = e;
360314
return e;
361315
}
362316
// ascend
363-
while (--stackN_ > 0) {
364-
e = stackV_[stackN_ - 1];
317+
stackV_.pop_back();
318+
while (!stackV_.empty()) {
319+
e = stackV_.back();
365320
pt(e->to).visited = 1;
366321
e = e->next;
367322
while (e && (e->visited || e->skip)) {
368323
e = e->next;
369324
}
370325
if (e) {
371-
stackV_[stackN_ - 1] = e;
326+
stackV_.back() = e;
372327
return e;
373328
}
329+
stackV_.pop_back();
374330
}
375331
return nullptr;
376332
}
@@ -386,7 +342,7 @@ void tmg_conn::removeShortLoops()
386342
graph_ = std::make_unique<tmg_conn_graph>();
387343
}
388344
graph_->init(ptV_.size(), shortV_.size());
389-
tcg_pt* pgV = graph_->ptV_;
345+
std::vector<tcg_pt>& pgV = graph_->ptV_;
390346

391347
// setup paths
392348
int npath = -1;
@@ -442,11 +398,7 @@ void tmg_conn::removeShortLoops()
442398
}
443399

444400
// remove all short loops
445-
int* path_vis = graph_->path_vis_;
446401
graph_->clearVisited();
447-
for (int j = 0; j < npath; j++) {
448-
path_vis[j] = 0;
449-
}
450402

451403
for (int jstart = 0; jstart < ptV_.size(); jstart++) {
452404
tcg_edge* e = graph_->getFirstEdge(jstart);
@@ -457,14 +409,14 @@ void tmg_conn::removeShortLoops()
457409
while (e) {
458410
e->visited = true;
459411
e->reverse->visited = true;
460-
tcg_pt* pg = pgV + e->to;
412+
tcg_pt* pg = &pgV[e->to];
461413
if (pg->visited) {
462414
e->skip = true;
463415
e->reverse->skip = true;
464416
e->s->skip = true;
465417
e = graph_->getNextEdge(false);
466418
} else {
467-
pg->visited = 2 + graph_->stackN_;
419+
pg->visited = 2 + graph_->stackV_.size();
468420
e = graph_->getNextEdge(true);
469421
}
470422
}
@@ -481,11 +433,11 @@ void tmg_conn::removeShortLoops()
481433
while (e) {
482434
e->visited = true;
483435
e->reverse->visited = true;
484-
tcg_pt* pg = pgV + e->to;
436+
tcg_pt* pg = &pgV[e->to];
485437
if (pg->visited) {
486438
e = graph_->getNextEdge(false);
487439
} else {
488-
pg->visited = 2 + graph_->stackN_;
440+
pg->visited = 2 + graph_->stackV_.size();
489441
e = graph_->getNextEdge(true);
490442
}
491443
}
@@ -535,7 +487,7 @@ void tmg_conn::removeWireLoops()
535487
// if no shorts, allow the loop to stay;
536488
// we do not expect any router to have a pure path loop
537489

538-
tcg_pt* pgV = graph_->ptV_;
490+
std::vector<tcg_pt>& pgV = graph_->ptV_;
539491

540492
bool done = false;
541493
while (!done) {
@@ -551,15 +503,15 @@ void tmg_conn::removeWireLoops()
551503
while (e) {
552504
e->visited = true;
553505
e->reverse->visited = true;
554-
tcg_pt* pg = pgV + e->to;
506+
tcg_pt* pg = &pgV[e->to];
555507
if (pg->visited == 1) {
556508
done = false;
557509
} else if (pg->visited) {
558510
int k = pg->visited - 2;
559511
int max_dist = 0;
560512
int max_k = 0;
561513
tcg_edge* emax = nullptr;
562-
for (; k < graph_->stackN_; k++) {
514+
for (; k < graph_->stackV_.size(); k++) {
563515
tcg_edge* eloop = graph_->stackV_[k];
564516
if (!eloop->s) {
565517
continue;
@@ -580,19 +532,19 @@ void tmg_conn::removeWireLoops()
580532
emax->s->skip = true;
581533
loop_removed++;
582534
done = false;
583-
if (max_k + 1 < graph_->stackN_) {
535+
if (max_k + 1 < graph_->stackV_.size()) {
584536
int k2;
585-
for (k2 = max_k + 1; k2 < graph_->stackN_ - 1; k2++) {
537+
for (k2 = max_k + 1; k2 < graph_->stackV_.size() - 1; k2++) {
586538
pgV[graph_->stackV_[k2]->to].visited = 1;
587539
}
588-
graph_->stackN_ = max_k + 1;
540+
graph_->stackV_.resize(max_k + 1);
589541
}
590542
}
591543
}
592544
if (pg->visited) {
593545
e = graph_->getNextEdge(false);
594546
} else {
595-
pg->visited = 2 + graph_->stackN_;
547+
pg->visited = 2 + graph_->stackV_.size();
596548
e = graph_->getNextEdge(true);
597549
}
598550
}
@@ -613,11 +565,11 @@ void tmg_conn::removeWireLoops()
613565
while (e) {
614566
e->visited = true;
615567
e->reverse->visited = true;
616-
tcg_pt* pg = pgV + e->to;
568+
tcg_pt* pg = &pgV[e->to];
617569
if (pg->visited) {
618570
e = graph_->getNextEdge(false);
619571
} else {
620-
pg->visited = 2 + graph_->stackN_;
572+
pg->visited = 2 + graph_->stackV_.size();
621573
e = graph_->getNextEdge(true);
622574
}
623575
}
@@ -650,7 +602,7 @@ bool tmg_conn_graph::dfsNext(int* from,
650602
bool* is_loop)
651603
{
652604
tcg_edge* e = e_;
653-
tcg_pt* pgV = ptV_;
605+
std::vector<tcg_pt>& pgV = ptV_;
654606
if (!e) {
655607
return false;
656608
}
@@ -688,7 +640,7 @@ int tmg_conn::isVisited(int j) const
688640

689641
void tmg_conn::checkVisited()
690642
{
691-
tcg_pt* pgV = graph_->ptV_;
643+
std::vector<tcg_pt>& pgV = graph_->ptV_;
692644
for (int j = 0; j < ptV_.size(); j++) {
693645
if (!pgV[j].visited) {
694646
connected_ = false;

0 commit comments

Comments
 (0)