Skip to content

Commit b992351

Browse files
authored
Merge pull request #2100 from verilog-to-routing/rem_malloc_to_new
Remaining malloc to new
2 parents 119560c + 4397396 commit b992351

File tree

20 files changed

+147
-161
lines changed

20 files changed

+147
-161
lines changed

ODIN_II/SRC/adders.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ void remove_list_node(t_linked_vptr* pre, t_linked_vptr* next) {
10641064
pre->next = next->next;
10651065
else
10661066
pre->next = NULL;
1067-
vtr::free(next);
1067+
delete next;
10681068
}
10691069

10701070
/*---------------------------------------------------------------------------
@@ -1434,4 +1434,4 @@ nnode_t* check_missing_ports(nnode_t* node, uintptr_t traverse_mark_number, netl
14341434
}
14351435

14361436
return new_node;
1437-
}
1437+
}

libs/libvtrutil/src/vtr_list.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,16 @@ t_linked_vptr* insert_in_vptr_list(t_linked_vptr* head, void* vptr_to_add) {
99
/* Inserts a new element at the head of a linked list of void pointers. *
1010
* Returns the new head of the list. */
1111

12-
t_linked_vptr* linked_vptr;
13-
14-
linked_vptr = (t_linked_vptr*)vtr::malloc(sizeof(t_linked_vptr));
15-
16-
linked_vptr->data_vptr = vptr_to_add;
17-
linked_vptr->next = head;
18-
return (linked_vptr); /* New head of the list */
12+
return new t_linked_vptr{vptr_to_add, head}; /* New head of the list */
1913
}
2014

2115
/* Deletes the element at the head of a linked list of void pointers. *
2216
* Returns the new head of the list. */
2317
t_linked_vptr* delete_in_vptr_list(t_linked_vptr* head) {
24-
t_linked_vptr* linked_vptr;
25-
2618
if (head == nullptr)
2719
return nullptr;
28-
linked_vptr = head->next;
29-
free(head);
20+
t_linked_vptr* const linked_vptr = head->next;
21+
delete head;
3022
return linked_vptr; /* New head of the list */
3123
}
3224

libs/libvtrutil/src/vtr_memory.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cstddef>
22
#include <cstdlib>
3+
#include <math.h>
34

45
#include "vtr_assert.h"
56
#include "vtr_list.h"
@@ -43,7 +44,7 @@ void* calloc(size_t nelem, size_t size) {
4344
if ((ret = std::calloc(nelem, size)) == nullptr) {
4445
throw VtrError("Unable to calloc memory.", __FILE__, __LINE__);
4546
}
46-
return (ret);
47+
return ret;
4748
}
4849

4950
void* malloc(size_t size) {
@@ -55,7 +56,7 @@ void* malloc(size_t size) {
5556
if ((ret = std::malloc(size)) == nullptr && size != 0) {
5657
throw VtrError("Unable to malloc memory.", __FILE__, __LINE__);
5758
}
58-
return (ret);
59+
return ret;
5960
}
6061

6162
void* realloc(void* ptr, size_t size) {
@@ -66,7 +67,7 @@ void* realloc(void* ptr, size_t size) {
6667
throw VtrError(string_fmt("Unable to realloc memory (ptr=%p, size=%d).", ptr, size),
6768
__FILE__, __LINE__);
6869
}
69-
return (ret);
70+
return ret;
7071
}
7172

7273
void* chunk_malloc(size_t size, t_chunk* chunk_info) {
@@ -102,7 +103,9 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) {
102103

103104
if ((size_t)(chunk_info->mem_avail) < size) { /* Need to malloc more memory. */
104105
if (size > CHUNK_SIZE) { /* Too big, use standard routine. */
105-
tmp_ptr = (char*)vtr::malloc(size);
106+
/* Want to allocate a block of memory the size of size.
107+
* i.e. malloc(size) */
108+
tmp_ptr = new char[size];
106109

107110
/* When debugging, uncomment the code below to see if memory allocation size */
108111
/* makes sense */
@@ -113,11 +116,11 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) {
113116

114117
VTR_ASSERT(chunk_info != nullptr);
115118
chunk_info->chunk_ptr_head = insert_in_vptr_list(chunk_info->chunk_ptr_head, tmp_ptr);
116-
return (tmp_ptr);
119+
return tmp_ptr;
117120
}
118121

119122
if (chunk_info->mem_avail < FRAGMENT_THRESHOLD) { /* Only a small scrap left. */
120-
chunk_info->next_mem_loc_ptr = (char*)vtr::malloc(CHUNK_SIZE);
123+
chunk_info->next_mem_loc_ptr = new char[CHUNK_SIZE];
121124
chunk_info->mem_avail = CHUNK_SIZE;
122125
VTR_ASSERT(chunk_info != nullptr);
123126
chunk_info->chunk_ptr_head = insert_in_vptr_list(chunk_info->chunk_ptr_head, chunk_info->next_mem_loc_ptr);
@@ -128,10 +131,11 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) {
128131
* to allocate normally. */
129132

130133
else {
131-
tmp_ptr = (char*)vtr::malloc(size);
134+
tmp_ptr = new char[size];
132135
VTR_ASSERT(chunk_info != nullptr);
133136
chunk_info->chunk_ptr_head = insert_in_vptr_list(chunk_info->chunk_ptr_head, tmp_ptr);
134-
return (tmp_ptr);
137+
138+
return tmp_ptr;
135139
}
136140
}
137141

@@ -147,7 +151,7 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) {
147151
tmp_ptr = chunk_info->next_mem_loc_ptr;
148152
chunk_info->next_mem_loc_ptr += aligned_size;
149153
chunk_info->mem_avail -= aligned_size;
150-
return (tmp_ptr);
154+
return tmp_ptr;
151155
}
152156

153157
void free_chunk_memory(t_chunk* chunk_info) {
@@ -158,11 +162,14 @@ void free_chunk_memory(t_chunk* chunk_info) {
158162
curr_ptr = chunk_info->chunk_ptr_head;
159163

160164
while (curr_ptr != nullptr) {
161-
free(curr_ptr->data_vptr); /* Free memory "chunk". */
165+
/* Must cast pointers to type char*, since the're of type void*, which delete can't
166+
* be called on.*/
167+
delete[]((char*)curr_ptr->data_vptr); /* Free memory "chunk". */
162168
prev_ptr = curr_ptr;
163169
curr_ptr = curr_ptr->next;
164-
free(prev_ptr); /* Free memory used to track "chunk". */
170+
delete (t_linked_vptr*)prev_ptr; /* Free memory used to track "chunk". */
165171
}
172+
166173
chunk_info->chunk_ptr_head = nullptr;
167174
chunk_info->mem_avail = 0;
168175
chunk_info->next_mem_loc_ptr = nullptr;

vpr/src/base/SetupVPR.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,11 @@ static void SetupPowerOpts(const t_options& Options, t_power_opts* power_opts, t
648648

649649
if (power_opts->do_power) {
650650
if (!Arch->power)
651-
Arch->power = (t_power_arch*)vtr::malloc(sizeof(t_power_arch));
651+
Arch->power = new t_power_arch();
652+
652653
if (!Arch->clocks)
653-
Arch->clocks = (t_clock_arch*)vtr::malloc(sizeof(t_clock_arch));
654+
Arch->clocks = new t_clock_arch();
655+
654656
device_ctx.clock_arch = Arch->clocks;
655657
} else {
656658
Arch->power = nullptr;

vpr/src/draw/draw.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ void alloc_draw_structs(const t_arch* arch) {
516516

517517
/* Space is allocated for draw_rr_node but not initialized because we do *
518518
* not yet know information about the routing resources. */
519-
draw_state->draw_rr_node = (t_draw_rr_node*)vtr::malloc(
520-
device_ctx.rr_graph.num_nodes() * sizeof(t_draw_rr_node));
519+
draw_state->draw_rr_node.resize(device_ctx.rr_graph.num_nodes());
521520

522521
draw_state->arch_info = arch;
523522

@@ -534,7 +533,6 @@ void free_draw_structs() {
534533
*
535534
* For safety, set all the array pointers to NULL in case any data
536535
* structure gets freed twice. */
537-
t_draw_state* draw_state = get_draw_state_vars();
538536
t_draw_coords* draw_coords = get_draw_coords_vars();
539537

540538
if (draw_coords != nullptr) {
@@ -544,10 +542,6 @@ void free_draw_structs() {
544542
draw_coords->tile_y = nullptr;
545543
}
546544

547-
if (draw_state != nullptr) {
548-
free(draw_state->draw_rr_node);
549-
draw_state->draw_rr_node = nullptr;
550-
}
551545
#else
552546
;
553547
#endif /* NO_GRAPHICS */
@@ -569,9 +563,7 @@ void init_draw_coords(float width_val) {
569563
/* Each time routing is on screen, need to reallocate the color of each *
570564
* rr_node, as the number of rr_nodes may change. */
571565
if (rr_graph.num_nodes() != 0) {
572-
draw_state->draw_rr_node = (t_draw_rr_node*)vtr::realloc(
573-
draw_state->draw_rr_node,
574-
(rr_graph.num_nodes()) * sizeof(t_draw_rr_node));
566+
draw_state->draw_rr_node.resize(rr_graph.num_nodes());
575567
/*FIXME: the type cast should be eliminated by making draw_rr_node adapt RRNodeId */
576568
for (const RRNodeId& rr_id : rr_graph.nodes()) {
577569
draw_state->draw_rr_node[(size_t)rr_id].color = DEFAULT_RR_NODE_COLOR;

vpr/src/draw/draw_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct t_draw_state {
203203
e_route_type draw_route_type = GLOBAL;
204204
char default_message[vtr::bufsize];
205205
vtr::vector<ClusterNetId, ezgl::color> net_color;
206-
t_draw_rr_node* draw_rr_node = nullptr;
206+
std::vector<t_draw_rr_node> draw_rr_node;
207207
std::shared_ptr<const SetupTimingInfo> setup_timing_info;
208208
const t_arch* arch_info = nullptr;
209209
std::shared_ptr<const vtr::ColorMap> color_map = nullptr;

vpr/src/pack/cluster_util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ void alloc_and_load_pb_stats(t_pb* pb, const int feasible_block_array_size) {
815815
pb->pb_stats->lookahead_output_pins_used = std::vector<std::vector<AtomNetId>>(pb->pb_graph_node->num_output_pin_class);
816816
pb->pb_stats->num_feasible_blocks = NOT_VALID;
817817
pb->pb_stats->feasible_blocks = new t_pack_molecule*[feasible_block_array_size];
818+
818819
for (int i = 0; i < feasible_block_array_size; i++)
819820
pb->pb_stats->feasible_blocks[i] = nullptr;
820821

vpr/src/place/place_macro.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,17 @@ void free_placement_macros_structs() {
424424
unsigned int itype;
425425
if (f_idirect_from_blk_pin != nullptr) {
426426
for (itype = 1; itype < device_ctx.physical_tile_types.size(); itype++) {
427-
free(f_idirect_from_blk_pin[itype]);
427+
delete[](f_idirect_from_blk_pin[itype]);
428428
}
429-
free(f_idirect_from_blk_pin);
429+
delete[](f_idirect_from_blk_pin);
430430
f_idirect_from_blk_pin = nullptr;
431431
}
432432

433433
if (f_direct_type_from_blk_pin != nullptr) {
434434
for (itype = 1; itype < device_ctx.physical_tile_types.size(); itype++) {
435-
free(f_direct_type_from_blk_pin[itype]);
435+
delete[](f_direct_type_from_blk_pin[itype]);
436436
}
437-
free(f_direct_type_from_blk_pin);
437+
delete[](f_direct_type_from_blk_pin);
438438
f_direct_type_from_blk_pin = nullptr;
439439
}
440440
}

0 commit comments

Comments
 (0)