Skip to content

Commit a328ab3

Browse files
HackerFookmurray
authored andcommitted
replace some uses of malloc/free with std::unique_ptr
Signed-off-by: Dustin DeWeese <[email protected]>
1 parent 0fac3e9 commit a328ab3

File tree

3 files changed

+68
-114
lines changed

3 files changed

+68
-114
lines changed

vpr/src/route/rr_graph.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ static void build_rr_graph(const t_graph_type graph_type,
517517
VTR_ASSERT(max_chan_width % 2 == 0);
518518
total_sets /= 2;
519519
}
520-
int* sets_per_seg_type = get_seg_track_counts(total_sets, segment_inf, use_full_seg_groups);
520+
auto sets_per_seg_type = get_seg_track_counts(total_sets, segment_inf, use_full_seg_groups);
521521

522522
if (is_global_graph) {
523523
//All pins can connect during global routing
@@ -526,13 +526,13 @@ static void build_rr_graph(const t_graph_type graph_type,
526526
Fc_out = std::vector<vtr::Matrix<int>>(types.size(), ones);
527527
} else {
528528
bool Fc_clipped = false;
529-
Fc_in = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type, max_chan_width,
529+
Fc_in = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type.get(), max_chan_width,
530530
e_fc_type::IN, directionality, &Fc_clipped);
531531
if (Fc_clipped) {
532532
*Warnings |= RR_GRAPH_WARN_FC_CLIPPED;
533533
}
534534
Fc_clipped = false;
535-
Fc_out = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type, max_chan_width,
535+
Fc_out = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type.get(), max_chan_width,
536536
e_fc_type::OUT, directionality, &Fc_clipped);
537537
if (Fc_clipped) {
538538
*Warnings |= RR_GRAPH_WARN_FC_CLIPPED;
@@ -568,7 +568,7 @@ static void build_rr_graph(const t_graph_type graph_type,
568568
}
569569

570570
auto perturb_ipins = alloc_and_load_perturb_ipins(types.size(), segment_inf.size(),
571-
sets_per_seg_type, Fc_in, Fc_out, directionality);
571+
sets_per_seg_type.get(), Fc_in, Fc_out, directionality);
572572
/* END FC */
573573

574574
/* Alloc node lookups, count nodes, alloc rr nodes */
@@ -657,7 +657,7 @@ static void build_rr_graph(const t_graph_type graph_type,
657657
for (unsigned int itype = 0; itype < types.size(); ++itype) {
658658
ipin_to_track_map[itype] = alloc_and_load_pin_to_track_map(RECEIVER,
659659
Fc_in[itype], &types[itype], perturb_ipins[itype], directionality,
660-
segment_inf.size(), sets_per_seg_type);
660+
segment_inf.size(), sets_per_seg_type.get());
661661

662662
track_to_pin_lookup[itype] = alloc_and_load_track_to_pin_lookup(ipin_to_track_map[itype], Fc_in[itype], types[itype].width, types[itype].height,
663663
types[itype].num_pins, max_chan_width, segment_inf.size());
@@ -674,7 +674,7 @@ static void build_rr_graph(const t_graph_type graph_type,
674674
max_chan_width, segment_inf);
675675
opin_to_track_map[itype] = alloc_and_load_pin_to_track_map(DRIVER,
676676
Fc_out[itype], &types[itype], perturb_opins, directionality,
677-
segment_inf.size(), sets_per_seg_type);
677+
segment_inf.size(), sets_per_seg_type.get());
678678
}
679679
}
680680
/* END OPIN MAP */
@@ -749,10 +749,6 @@ static void build_rr_graph(const t_graph_type graph_type,
749749
free_switchblock_permutations(sb_conn_map);
750750
sb_conn_map = nullptr;
751751
}
752-
if (sets_per_seg_type) {
753-
free(sets_per_seg_type);
754-
sets_per_seg_type = nullptr;
755-
}
756752

757753
free_type_track_to_pin_map(track_to_pin_lookup, types, max_chan_width);
758754
if (clb_to_clb_directs != nullptr) {

vpr/src/route/rr_graph2.cpp

Lines changed: 59 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,26 @@ static int vpr_to_phy_track(const int itrack,
8888
const t_chan_seg_details* seg_details,
8989
const enum e_directionality directionality);
9090

91-
static int* label_wire_muxes(const int chan_num,
92-
const int seg_num,
93-
const t_chan_seg_details* seg_details,
94-
const int seg_type_index,
95-
const int max_len,
96-
const enum e_direction dir,
97-
const int max_chan_width,
98-
const bool check_cb,
99-
int* num_wire_muxes,
100-
int* num_wire_muxes_cb_restricted);
101-
102-
static int* label_incoming_wires(const int chan_num,
103-
const int seg_num,
104-
const int sb_seg,
105-
const t_chan_seg_details* seg_details,
106-
const int max_len,
107-
const enum e_direction dir,
108-
const int max_chan_width,
109-
int* num_incoming_wires,
110-
int* num_ending_wires);
91+
static std::unique_ptr<int[]> label_wire_muxes(const int chan_num,
92+
const int seg_num,
93+
const t_chan_seg_details* seg_details,
94+
const int seg_type_index,
95+
const int max_len,
96+
const enum e_direction dir,
97+
const int max_chan_width,
98+
const bool check_cb,
99+
int* num_wire_muxes,
100+
int* num_wire_muxes_cb_restricted);
101+
102+
static std::unique_ptr<int[]> label_incoming_wires(const int chan_num,
103+
const int seg_num,
104+
const int sb_seg,
105+
const t_chan_seg_details* seg_details,
106+
const int max_len,
107+
const enum e_direction dir,
108+
const int max_chan_width,
109+
int* num_incoming_wires,
110+
int* num_ending_wires);
111111

112112
static int find_label_of_track(int* wire_mux_on_track,
113113
int num_wire_muxes,
@@ -143,16 +143,14 @@ static bool should_apply_switch_override(int switch_override);
143143
* no longer less than the requested number of tracks. As a final
144144
* step, if we were closer to target before last more, undo it
145145
* and end up with a result that uses fewer tracks than given. */
146-
int* get_seg_track_counts(const int num_sets,
147-
const std::vector<t_segment_inf>& segment_inf,
148-
const bool use_full_seg_groups) {
149-
int* result;
150-
double* demand;
146+
std::unique_ptr<int[]> get_seg_track_counts(const int num_sets,
147+
const std::vector<t_segment_inf>& segment_inf,
148+
const bool use_full_seg_groups) {
151149
int imax, freq_sum, assigned, size;
152150
double scale, max, reduce;
153151

154-
result = (int*)vtr::malloc(sizeof(int) * segment_inf.size());
155-
demand = (double*)vtr::malloc(sizeof(double) * segment_inf.size());
152+
auto result = std::make_unique<int[]>(segment_inf.size());
153+
auto demand = std::make_unique<double[]>(segment_inf.size());
156154

157155
/* Scale factor so we can divide by any length
158156
* and still use integers */
@@ -199,13 +197,6 @@ int* get_seg_track_counts(const int num_sets,
199197
result[imax] -= size;
200198
}
201199

202-
/* Free temps */
203-
if (demand) {
204-
vtr::free(demand);
205-
demand = nullptr;
206-
}
207-
208-
/* This must be freed by caller */
209200
return result;
210201
}
211202

@@ -228,7 +219,6 @@ t_seg_details* alloc_and_load_seg_details(int* max_chan_width,
228219
int cur_track, ntracks, itrack, length, j, index;
229220
int arch_wire_switch, arch_opin_switch, fac, num_sets, tmp;
230221
int group_start, first_track;
231-
int* sets_per_seg_type = nullptr;
232222
t_seg_details* seg_details = nullptr;
233223
bool longline;
234224

@@ -245,8 +235,8 @@ t_seg_details* alloc_and_load_seg_details(int* max_chan_width,
245235
}
246236

247237
/* Map segment type fractions and groupings to counts of tracks */
248-
sets_per_seg_type = get_seg_track_counts((*max_chan_width / fac),
249-
segment_inf, use_full_seg_groups);
238+
auto sets_per_seg_type = get_seg_track_counts((*max_chan_width / fac),
239+
segment_inf, use_full_seg_groups);
250240

251241
/* Count the number tracks actually assigned. */
252242
tmp = 0;
@@ -356,9 +346,6 @@ t_seg_details* alloc_and_load_seg_details(int* max_chan_width,
356346
}
357347
} /* End for each segment type. */
358348

359-
/* free variables */
360-
vtr::free(sets_per_seg_type);
361-
362349
if (num_seg_details) {
363350
*num_seg_details = cur_track;
364351
}
@@ -761,8 +748,6 @@ int get_unidir_opin_connections(const int chan,
761748
/* Gets a linked list of Fc nodes of specified seg_type_index to connect
762749
* to in given chan seg. Fc_ofs is used for the opin staggering pattern. */
763750

764-
int* inc_muxes = nullptr;
765-
int* dec_muxes = nullptr;
766751
int num_inc_muxes, num_dec_muxes, iconn;
767752
int inc_inode_index, dec_inode_index;
768753
int inc_mux, dec_mux;
@@ -781,10 +766,10 @@ int get_unidir_opin_connections(const int chan,
781766

782767
/* Get the lists of possible muxes. */
783768
int dummy;
784-
inc_muxes = label_wire_muxes(chan, seg, seg_details, seg_type_index, max_len,
785-
INC_DIRECTION, max_chan_width, true, &num_inc_muxes, &dummy);
786-
dec_muxes = label_wire_muxes(chan, seg, seg_details, seg_type_index, max_len,
787-
DEC_DIRECTION, max_chan_width, true, &num_dec_muxes, &dummy);
769+
auto inc_muxes = label_wire_muxes(chan, seg, seg_details, seg_type_index, max_len,
770+
INC_DIRECTION, max_chan_width, true, &num_inc_muxes, &dummy);
771+
auto dec_muxes = label_wire_muxes(chan, seg, seg_details, seg_type_index, max_len,
772+
DEC_DIRECTION, max_chan_width, true, &num_dec_muxes, &dummy);
788773

789774
/* Clip Fc to the number of muxes. */
790775
if (((Fc / 2) > num_inc_muxes) || ((Fc / 2) > num_dec_muxes)) {
@@ -823,15 +808,6 @@ int get_unidir_opin_connections(const int chan,
823808
++num_edges;
824809
}
825810

826-
if (inc_muxes) {
827-
vtr::free(inc_muxes);
828-
inc_muxes = nullptr;
829-
}
830-
if (dec_muxes) {
831-
vtr::free(dec_muxes);
832-
dec_muxes = nullptr;
833-
}
834-
835811
return num_edges;
836812
}
837813

@@ -1850,7 +1826,6 @@ static int get_unidir_track_to_chan_seg(const int from_track,
18501826
const int from_rr_node,
18511827
t_rr_edge_info_set& rr_edges_to_create) {
18521828
int num_labels = 0;
1853-
int* mux_labels = nullptr;
18541829

18551830
/* x, y coords for get_rr_node lookups */
18561831
int to_x = (CHANX == to_type ? to_seg : to_chan);
@@ -1868,15 +1843,11 @@ static int get_unidir_track_to_chan_seg(const int from_track,
18681843

18691844
/* get list of muxes to which we can connect */
18701845
int dummy;
1871-
mux_labels = label_wire_muxes(to_chan, to_seg, seg_details, UNDEFINED, max_len,
1872-
to_dir, max_chan_width, false, &num_labels, &dummy);
1846+
auto mux_labels = label_wire_muxes(to_chan, to_seg, seg_details, UNDEFINED, max_len,
1847+
to_dir, max_chan_width, false, &num_labels, &dummy);
18731848

18741849
/* Can't connect if no muxes. */
18751850
if (num_labels < 1) {
1876-
if (mux_labels) {
1877-
vtr::free(mux_labels);
1878-
mux_labels = nullptr;
1879-
}
18801851
return 0;
18811852
}
18821853

@@ -1929,10 +1900,6 @@ static int get_unidir_track_to_chan_seg(const int from_track,
19291900
}
19301901
}
19311902

1932-
if (mux_labels) {
1933-
vtr::free(mux_labels);
1934-
mux_labels = nullptr;
1935-
}
19361903
return count;
19371904
}
19381905

@@ -2159,8 +2126,8 @@ void load_sblock_pattern_lookup(const int i,
21592126

21602127
/* SB's range from (0, 0) to (grid.width() - 2, grid.height() - 2) */
21612128
/* First find all four sides' incoming wires */
2162-
int* wire_mux_on_track[4];
2163-
int* incoming_wire_label[4];
2129+
std::unique_ptr<int[]> wire_mux_on_track[4];
2130+
std::unique_ptr<int[]> incoming_wire_label[4];
21642131
int num_incoming_wires[4];
21652132
int num_ending_wires[4];
21662133
int num_wire_muxes[4];
@@ -2314,7 +2281,7 @@ void load_sblock_pattern_lookup(const int i,
23142281
* use any pattern such as Wilton */
23152282
/* In the direct connect case, I know for sure the init mux is at the same track #
23162283
* as this ending wire, but still need to find the init mux label for Fs > 3 */
2317-
int mux = find_label_of_track(wire_mux_on_track[to_side],
2284+
int mux = find_label_of_track(wire_mux_on_track[to_side].get(),
23182285
num_wire_muxes[to_side], itrack);
23192286
sblock_pattern[i][j][side_opp][to_side][itrack][0] = mux;
23202287
} else {
@@ -2331,35 +2298,26 @@ void load_sblock_pattern_lookup(const int i,
23312298
}
23322299
}
23332300
}
2334-
2335-
for (e_side side : {TOP, RIGHT, BOTTOM, LEFT}) {
2336-
if (incoming_wire_label[side]) {
2337-
vtr::free(incoming_wire_label[side]);
2338-
}
2339-
if (wire_mux_on_track[side]) {
2340-
vtr::free(wire_mux_on_track[side]);
2341-
}
2342-
}
23432301
}
23442302

2345-
static int* label_wire_muxes(const int chan_num,
2346-
const int seg_num,
2347-
const t_chan_seg_details* seg_details,
2348-
const int seg_type_index,
2349-
const int max_len,
2350-
const enum e_direction dir,
2351-
const int max_chan_width,
2352-
const bool check_cb,
2353-
int* num_wire_muxes,
2354-
int* num_wire_muxes_cb_restricted) {
2303+
static std::unique_ptr<int[]> label_wire_muxes(const int chan_num,
2304+
const int seg_num,
2305+
const t_chan_seg_details* seg_details,
2306+
const int seg_type_index,
2307+
const int max_len,
2308+
const enum e_direction dir,
2309+
const int max_chan_width,
2310+
const bool check_cb,
2311+
int* num_wire_muxes,
2312+
int* num_wire_muxes_cb_restricted) {
23552313
/* Labels the muxes on that side (seg_num, chan_num, direction). The returned array
23562314
* maps a label to the actual track #: array[0] = <the track number of the first/lowest mux>
23572315
* This routine orders wire muxes by their natural order, i.e. track #
23582316
* If seg_type_index == UNDEFINED, all segments in the channel are considered. Otherwise this routine
23592317
* only looks at segments that belong to the specified segment type. */
23602318

23612319
int itrack, start, end, num_labels, num_labels_restricted, pass;
2362-
int* labels = nullptr;
2320+
std::unique_ptr<int[]> labels;
23632321
bool is_endpoint;
23642322

23652323
/* COUNT pass then a LOAD pass */
@@ -2368,7 +2326,7 @@ static int* label_wire_muxes(const int chan_num,
23682326
for (pass = 0; pass < 2; ++pass) {
23692327
/* Alloc the list on LOAD pass */
23702328
if (pass > 0) {
2371-
labels = (int*)vtr::malloc(sizeof(int) * num_labels);
2329+
labels = std::make_unique<int[]>(num_labels);
23722330
num_labels = 0;
23732331
}
23742332

@@ -2427,25 +2385,25 @@ static int* label_wire_muxes(const int chan_num,
24272385
return labels;
24282386
}
24292387

2430-
static int* label_incoming_wires(const int chan_num,
2431-
const int seg_num,
2432-
const int sb_seg,
2433-
const t_chan_seg_details* seg_details,
2434-
const int max_len,
2435-
const enum e_direction dir,
2436-
const int max_chan_width,
2437-
int* num_incoming_wires,
2438-
int* num_ending_wires) {
2388+
static std::unique_ptr<int[]> label_incoming_wires(const int chan_num,
2389+
const int seg_num,
2390+
const int sb_seg,
2391+
const t_chan_seg_details* seg_details,
2392+
const int max_len,
2393+
const enum e_direction dir,
2394+
const int max_chan_width,
2395+
int* num_incoming_wires,
2396+
int* num_ending_wires) {
24392397
/* Labels the incoming wires on that side (seg_num, chan_num, direction).
24402398
* The returned array maps a track # to a label: array[0] = <the new hash value/label for track 0>,
24412399
* the labels 0,1,2,.. identify consecutive incoming wires that have sblock (passing wires with sblock and ending wires) */
24422400

24432401
int itrack, start, end, i, num_passing, num_ending, pass;
2444-
int* labels;
24452402
bool sblock_exists, is_endpoint;
24462403

24472404
/* Alloc the list of labels for the tracks */
2448-
labels = (int*)vtr::malloc(max_chan_width * sizeof(int));
2405+
auto labels = std::make_unique<int[]>(max_chan_width);
2406+
24492407
for (i = 0; i < max_chan_width; ++i) {
24502408
labels[i] = UN_SET; /* crash hard if unset */
24512409
}

vpr/src/route/rr_graph2.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ void load_sblock_pattern_lookup(const int i,
214214
const enum e_switch_block_type switch_block_type,
215215
t_sblock_pattern& sblock_pattern);
216216

217-
int* get_seg_track_counts(const int num_sets,
218-
const std::vector<t_segment_inf>& segment_inf,
219-
const bool use_full_seg_groups);
217+
std::unique_ptr<int[]> get_seg_track_counts(const int num_sets,
218+
const std::vector<t_segment_inf>& segment_inf,
219+
const bool use_full_seg_groups);
220220

221221
void dump_seg_details(const t_chan_seg_details* seg_details,
222222
int max_chan_width,

0 commit comments

Comments
 (0)