Skip to content

Commit f7cc950

Browse files
HackerFookmurray
authored andcommitted
clean up some more alloc/free's
Signed-off-by: Dustin DeWeese <[email protected]>
1 parent a328ab3 commit f7cc950

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

vpr/src/route/check_route.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ void check_route(enum e_route_type route_type) {
4242
int max_pins, inode, prev_node;
4343
unsigned int ipin;
4444
bool valid, connects;
45-
bool* connected_to_route; /* [0 .. device_ctx.rr_nodes.size()-1] */
4645
t_trace* tptr;
47-
bool* pin_done;
4846

4947
auto& device_ctx = g_vpr_ctx.device();
5048
auto& cluster_ctx = g_vpr_ctx.clustering();
@@ -70,21 +68,21 @@ void check_route(enum e_route_type route_type) {
7068

7169
auto non_configurable_rr_sets = identify_non_configurable_rr_sets();
7270

73-
connected_to_route = (bool*)vtr::calloc(device_ctx.rr_nodes.size(), sizeof(bool));
71+
auto connected_to_route = std::make_unique<bool[]>(device_ctx.rr_nodes.size());
72+
std::fill_n(connected_to_route.get(), device_ctx.rr_nodes.size(), false);
7473

7574
max_pins = 0;
7675
for (auto net_id : cluster_ctx.clb_nlist.nets())
7776
max_pins = std::max(max_pins, (int)cluster_ctx.clb_nlist.net_pins(net_id).size());
7877

79-
pin_done = (bool*)vtr::malloc(max_pins * sizeof(bool));
78+
auto pin_done = std::make_unique<bool[]>(max_pins);
8079

8180
/* Now check that all nets are indeed connected. */
8281
for (auto net_id : cluster_ctx.clb_nlist.nets()) {
8382
if (cluster_ctx.clb_nlist.net_is_ignored(net_id) || cluster_ctx.clb_nlist.net_sinks(net_id).size() == 0) /* Skip ignored nets. */
8483
continue;
8584

86-
for (ipin = 0; ipin < cluster_ctx.clb_nlist.net_pins(net_id).size(); ipin++)
87-
pin_done[ipin] = false;
85+
std::fill_n(pin_done.get(), cluster_ctx.clb_nlist.net_pins(net_id).size(), false);
8886

8987
/* Check the SOURCE of the net. */
9088
tptr = route_ctx.trace[net_id].head;
@@ -132,7 +130,7 @@ void check_route(enum e_route_type route_type) {
132130
connected_to_route[inode] = true; /* Mark as in path. */
133131

134132
if (device_ctx.rr_nodes[inode].type() == SINK) {
135-
check_sink(inode, net_id, pin_done);
133+
check_sink(inode, net_id, pin_done.get());
136134
num_sinks += 1;
137135
}
138136

@@ -160,12 +158,10 @@ void check_route(enum e_route_type route_type) {
160158

161159
check_net_for_stubs(net_id);
162160

163-
reset_flags(net_id, connected_to_route);
161+
reset_flags(net_id, connected_to_route.get());
164162

165163
} /* End for each net */
166164

167-
free(pin_done);
168-
free(connected_to_route);
169165
VTR_LOG("Completed routing consistency check successfully.\n");
170166
VTR_LOG("\n");
171167
}

vpr/src/route/route_common.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ struct t_trace_branch {
4444

4545
/**************** Static variables local to route_common.c ******************/
4646

47-
static t_heap** heap; /* Indexed from [1..heap_size] */
48-
static int heap_size; /* Number of slots in the heap array */
49-
static int heap_tail; /* Index of first unused slot in the heap array */
47+
static t_heap** heap = nullptr; /* Indexed from [1..heap_size] */
48+
static int heap_size; /* Number of slots in the heap array */
49+
static int heap_tail; /* Index of first unused slot in the heap array */
5050

5151
/* For managing my own list of currently free heap data structures. */
5252
static t_heap* heap_free_head = nullptr;
@@ -467,14 +467,18 @@ void pathfinder_update_cost(float pres_fac, float acc_fac) {
467467
}
468468
}
469469

470+
// Note: malloc()/free() must be used for the heap,
471+
// or realloc() must be eliminated from add_to_heap()
472+
// because there is no C++ equivalent.
470473
void init_heap(const DeviceGrid& grid) {
471474
if (heap != nullptr) {
472475
vtr::free(heap + 1);
473-
heap = nullptr;
474476
}
477+
475478
heap_size = (grid.width() - 1) * (grid.height() - 1);
476-
heap = (t_heap**)vtr::malloc(heap_size * sizeof(t_heap*));
477-
heap--; /* heap stores from [1..heap_size] */
479+
480+
// heap stores from [1..heap_size]
481+
heap = (t_heap**)vtr::malloc(heap_size * sizeof(t_heap*)) - 1;
478482
heap_tail = 1;
479483
}
480484

vpr/src/route/route_timing.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -846,17 +846,11 @@ void alloc_timing_driven_route_structs(float** pin_criticality_ptr,
846846
t_rt_node*** rt_node_of_sink_ptr) {
847847
/* Allocates all the structures needed only by the timing-driven router. */
848848

849-
int max_pins_per_net = get_max_pins_per_net();
850-
int max_sinks = std::max(max_pins_per_net - 1, 0);
849+
int max_sinks = std::max(get_max_pins_per_net() - 1, 0);
851850

852-
float* pin_criticality = (float*)vtr::malloc(max_sinks * sizeof(float));
853-
*pin_criticality_ptr = pin_criticality - 1; /* First sink is pin #1. */
854-
855-
int* sink_order = (int*)vtr::malloc(max_sinks * sizeof(int));
856-
*sink_order_ptr = sink_order - 1;
857-
858-
t_rt_node** rt_node_of_sink = (t_rt_node**)vtr::malloc(max_sinks * sizeof(t_rt_node*));
859-
*rt_node_of_sink_ptr = rt_node_of_sink - 1;
851+
*pin_criticality_ptr = new float[max_sinks] - 1; /* First sink is pin #1. */
852+
*sink_order_ptr = new int[max_sinks] - 1;
853+
*rt_node_of_sink_ptr = new t_rt_node*[max_sinks] - 1;
860854

861855
alloc_route_tree_timing_structs();
862856
}
@@ -869,11 +863,11 @@ void free_timing_driven_route_structs(float* pin_criticality, int* sink_order, t
869863
/* Frees all the structures needed only by the timing-driven router. */
870864

871865
// coverity[offset_free : Intentional]
872-
free(pin_criticality + 1); /* Starts at index 1. */
866+
delete[](pin_criticality + 1); /* Starts at index 1. */
873867
// coverity[offset_free : Intentional]
874-
free(sink_order + 1);
868+
delete[](sink_order + 1);
875869
// coverity[offset_free : Intentional]
876-
free(rt_node_of_sink + 1);
870+
delete[](rt_node_of_sink + 1);
877871

878872
free_route_tree_timing_structs();
879873
}

vpr/src/route/route_tree_timing.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool alloc_route_tree_timing_structs(bool exists_ok) {
9191

9292
void free_route_tree_timing_structs() {
9393
/* Frees the structures needed to build routing trees, and really frees
94-
* (i.e. calls free) all the data on the free lists. */
94+
* (i.e. deletes) all the data on the free lists. */
9595

9696
t_rt_node *rt_node, *next_node;
9797
t_linked_rt_edge *rt_edge, *next_edge;
@@ -102,7 +102,7 @@ void free_route_tree_timing_structs() {
102102

103103
while (rt_node != nullptr) {
104104
next_node = rt_node->u.next;
105-
free(rt_node);
105+
delete rt_node;
106106
rt_node = next_node;
107107
}
108108

@@ -112,7 +112,7 @@ void free_route_tree_timing_structs() {
112112

113113
while (rt_edge != nullptr) {
114114
next_edge = rt_edge->next;
115-
free(rt_edge);
115+
delete rt_edge;
116116
rt_edge = next_edge;
117117
}
118118

@@ -131,7 +131,7 @@ alloc_rt_node() {
131131
if (rt_node != nullptr) {
132132
rt_node_free_list = rt_node->u.next;
133133
} else {
134-
rt_node = (t_rt_node*)vtr::malloc(sizeof(t_rt_node));
134+
rt_node = new t_rt_node;
135135
}
136136

137137
return (rt_node);
@@ -156,7 +156,7 @@ alloc_linked_rt_edge() {
156156
if (linked_rt_edge != nullptr) {
157157
rt_edge_free_list = linked_rt_edge->next;
158158
} else {
159-
linked_rt_edge = (t_linked_rt_edge*)vtr::malloc(sizeof(t_linked_rt_edge));
159+
linked_rt_edge = new t_linked_rt_edge;
160160
}
161161

162162
VTR_ASSERT(linked_rt_edge != nullptr);

0 commit comments

Comments
 (0)