Skip to content

Replace some uses of malloc/free with std::unique_ptr #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions vpr/src/route/check_route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ void check_route(enum e_route_type route_type) {
int max_pins, inode, prev_node;
unsigned int ipin;
bool valid, connects;
bool* connected_to_route; /* [0 .. device_ctx.rr_nodes.size()-1] */
t_trace* tptr;
bool* pin_done;

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

auto non_configurable_rr_sets = identify_non_configurable_rr_sets();

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

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

pin_done = (bool*)vtr::malloc(max_pins * sizeof(bool));
auto pin_done = std::make_unique<bool[]>(max_pins);

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

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

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

if (device_ctx.rr_nodes[inode].type() == SINK) {
check_sink(inode, net_id, pin_done);
check_sink(inode, net_id, pin_done.get());
num_sinks += 1;
}

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

check_net_for_stubs(net_id);

reset_flags(net_id, connected_to_route);
reset_flags(net_id, connected_to_route.get());

} /* End for each net */

free(pin_done);
free(connected_to_route);
VTR_LOG("Completed routing consistency check successfully.\n");
VTR_LOG("\n");
}
Expand Down
16 changes: 10 additions & 6 deletions vpr/src/route/route_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ struct t_trace_branch {

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

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

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

// Note: malloc()/free() must be used for the heap,
// or realloc() must be eliminated from add_to_heap()
// because there is no C++ equivalent.
void init_heap(const DeviceGrid& grid) {
if (heap != nullptr) {
vtr::free(heap + 1);
heap = nullptr;
}

heap_size = (grid.width() - 1) * (grid.height() - 1);
heap = (t_heap**)vtr::malloc(heap_size * sizeof(t_heap*));
heap--; /* heap stores from [1..heap_size] */

// heap stores from [1..heap_size]
heap = (t_heap**)vtr::malloc(heap_size * sizeof(t_heap*)) - 1;
heap_tail = 1;
}

Expand Down
22 changes: 8 additions & 14 deletions vpr/src/route/route_timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,17 +838,11 @@ void alloc_timing_driven_route_structs(float** pin_criticality_ptr,
t_rt_node*** rt_node_of_sink_ptr) {
/* Allocates all the structures needed only by the timing-driven router. */

int max_pins_per_net = get_max_pins_per_net();
int max_sinks = std::max(max_pins_per_net - 1, 0);
int max_sinks = std::max(get_max_pins_per_net() - 1, 0);

float* pin_criticality = (float*)vtr::malloc(max_sinks * sizeof(float));
*pin_criticality_ptr = pin_criticality - 1; /* First sink is pin #1. */

int* sink_order = (int*)vtr::malloc(max_sinks * sizeof(int));
*sink_order_ptr = sink_order - 1;

t_rt_node** rt_node_of_sink = (t_rt_node**)vtr::malloc(max_sinks * sizeof(t_rt_node*));
*rt_node_of_sink_ptr = rt_node_of_sink - 1;
*pin_criticality_ptr = new float[max_sinks] - 1; /* First sink is pin #1. */
*sink_order_ptr = new int[max_sinks] - 1;
*rt_node_of_sink_ptr = new t_rt_node*[max_sinks] - 1;

alloc_route_tree_timing_structs();
}
Expand All @@ -861,11 +855,11 @@ void free_timing_driven_route_structs(float* pin_criticality, int* sink_order, t
/* Frees all the stuctures needed only by the timing-driven router. */

// coverity[offset_free : Intentional]
free(pin_criticality + 1); /* Starts at index 1. */
delete[](pin_criticality + 1); /* Starts at index 1. */
// coverity[offset_free : Intentional]
free(sink_order + 1);
delete[](sink_order + 1);
// coverity[offset_free : Intentional]
free(rt_node_of_sink + 1);
delete[](rt_node_of_sink + 1);

free_route_tree_timing_structs();
}
Expand Down Expand Up @@ -1628,7 +1622,7 @@ static void timing_driven_expand_cheapest(t_heap* cheapest,
target_node,
router_stats);
} else {
//Post-heap prune, do not re-explore from the current/new partial path as it
//Post-heap prune, do not re-explore from the current/new partial path as it
//has worse cost than the best partial path to this node found so far
VTR_LOGV_DEBUG(f_router_debug, " Worse cost to %d\n", inode);
VTR_LOGV_DEBUG(f_router_debug, " Old total cost: %g\n", best_total_cost);
Expand Down
10 changes: 5 additions & 5 deletions vpr/src/route/route_tree_timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool alloc_route_tree_timing_structs(bool exists_ok) {

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

t_rt_node *rt_node, *next_node;
t_linked_rt_edge *rt_edge, *next_edge;
Expand All @@ -102,7 +102,7 @@ void free_route_tree_timing_structs() {

while (rt_node != nullptr) {
next_node = rt_node->u.next;
free(rt_node);
delete rt_node;
rt_node = next_node;
}

Expand All @@ -112,7 +112,7 @@ void free_route_tree_timing_structs() {

while (rt_edge != nullptr) {
next_edge = rt_edge->next;
free(rt_edge);
delete rt_edge;
rt_edge = next_edge;
}

Expand All @@ -131,7 +131,7 @@ alloc_rt_node() {
if (rt_node != nullptr) {
rt_node_free_list = rt_node->u.next;
} else {
rt_node = (t_rt_node*)vtr::malloc(sizeof(t_rt_node));
rt_node = new t_rt_node;
}

return (rt_node);
Expand All @@ -156,7 +156,7 @@ alloc_linked_rt_edge() {
if (linked_rt_edge != nullptr) {
rt_edge_free_list = linked_rt_edge->next;
} else {
linked_rt_edge = (t_linked_rt_edge*)vtr::malloc(sizeof(t_linked_rt_edge));
linked_rt_edge = new t_linked_rt_edge;
}

VTR_ASSERT(linked_rt_edge != nullptr);
Expand Down
16 changes: 6 additions & 10 deletions vpr/src/route/rr_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static void build_rr_graph(const t_graph_type graph_type,
VTR_ASSERT(max_chan_width % 2 == 0);
total_sets /= 2;
}
int* sets_per_seg_type = get_seg_track_counts(total_sets, segment_inf, use_full_seg_groups);
auto sets_per_seg_type = get_seg_track_counts(total_sets, segment_inf, use_full_seg_groups);

if (is_global_graph) {
//All pins can connect during global routing
Expand All @@ -529,13 +529,13 @@ static void build_rr_graph(const t_graph_type graph_type,
Fc_out = std::vector<vtr::Matrix<int>>(types.size(), ones);
} else {
bool Fc_clipped = false;
Fc_in = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type, max_chan_width,
Fc_in = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type.get(), max_chan_width,
e_fc_type::IN, directionality, &Fc_clipped);
if (Fc_clipped) {
*Warnings |= RR_GRAPH_WARN_FC_CLIPPED;
}
Fc_clipped = false;
Fc_out = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type, max_chan_width,
Fc_out = alloc_and_load_actual_fc(types, max_pins, segment_inf, sets_per_seg_type.get(), max_chan_width,
e_fc_type::OUT, directionality, &Fc_clipped);
if (Fc_clipped) {
*Warnings |= RR_GRAPH_WARN_FC_CLIPPED;
Expand Down Expand Up @@ -571,7 +571,7 @@ static void build_rr_graph(const t_graph_type graph_type,
}

auto perturb_ipins = alloc_and_load_perturb_ipins(types.size(), segment_inf.size(),
sets_per_seg_type, Fc_in, Fc_out, directionality);
sets_per_seg_type.get(), Fc_in, Fc_out, directionality);
/* END FC */

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

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,
types[itype].num_pins, max_chan_width, segment_inf.size());
Expand All @@ -672,7 +672,7 @@ static void build_rr_graph(const t_graph_type graph_type,
max_chan_width, segment_inf);
opin_to_track_map[itype] = alloc_and_load_pin_to_track_map(DRIVER,
Fc_out[itype], &types[itype], perturb_opins, directionality,
segment_inf.size(), sets_per_seg_type);
segment_inf.size(), sets_per_seg_type.get());
}
}
/* END OPIN MAP */
Expand Down Expand Up @@ -734,10 +734,6 @@ static void build_rr_graph(const t_graph_type graph_type,
free_switchblock_permutations(sb_conn_map);
sb_conn_map = nullptr;
}
if (sets_per_seg_type) {
free(sets_per_seg_type);
sets_per_seg_type = nullptr;
}

free_type_track_to_pin_map(track_to_pin_lookup, types, max_chan_width);
if (clb_to_clb_directs != nullptr) {
Expand Down
Loading