Skip to content

Commit 53d2620

Browse files
authored
Merge pull request verilog-to-routing#660 from j-b-1-7/odin_leak_instantiate_hard_multiplier
odin_leak: fix memory leak in instantiate_hard_multiplier function
2 parents f74cdee + 00825b3 commit 53d2620

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

ODIN_II/SRC/multipliers.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ OTHER DEALINGS IN THE SOFTWARE.
2626
#include <string.h>
2727
#include <algorithm>
2828
#include <cmath>
29+
#include <string>
2930
#include "odin_types.h"
3031
#include "node_creation_library.h"
3132
#include "multipliers.h"
@@ -38,6 +39,7 @@ OTHER DEALINGS IN THE SOFTWARE.
3839

3940
#include "vtr_memory.h"
4041
#include "vtr_list.h"
42+
#include "vtr_util.h"
4143

4244

4345
using vtr::t_linked_vptr;
@@ -395,39 +397,55 @@ void declare_hard_multiplier(nnode_t *node)
395397
*-------------------------------------------------------------------------*/
396398
void instantiate_hard_multiplier(nnode_t *node, short mark, netlist_t * /*netlist*/)
397399
{
398-
char *new_name;
399-
int len, sanity, i;
400+
oassert(node
401+
&& "node is NULL to instanciate hard multiplier");
400402

401403
declare_hard_multiplier(node);
402404

403-
/* Need to give node proper name */
404-
len = strlen(node->name);
405-
len = len + 20; /* 20 chars should hold mul specs */
406-
new_name = (char*)vtr::malloc(len);
405+
std::string node_name = "";
406+
if( node->name )
407+
{
408+
node_name = node->name;
409+
vtr::free(node->name);
410+
node->name = NULL;
411+
}
407412

408-
/* wide input first :) */
409-
if (node->input_port_sizes[0] > node->input_port_sizes[1])
410-
sanity = odin_sprintf(new_name, "%s_%d_%d_%d", node->name, node->input_port_sizes[0], node->input_port_sizes[1], node->output_port_sizes[0]);
413+
if(node->num_output_pins <= 0)
414+
{
415+
/* wide input first :) */
416+
int portA = 0;
417+
int portB = 1;
418+
if (node->input_port_sizes[1] > node->input_port_sizes[0])
419+
{
420+
portA = 1;
421+
portB = 0;
422+
}
423+
std::string tmp(
424+
node_name +
425+
"_" + std::to_string(node->input_port_sizes[portA]) +
426+
"_" + std::to_string(node->input_port_sizes[portB]) +
427+
"_" + std::to_string(node->output_port_sizes[0])
428+
);
429+
node->name = vtr::strdup(tmp.c_str());
430+
}
411431
else
412-
sanity = odin_sprintf(new_name, "%s_%d_%d_%d", node->name, node->input_port_sizes[1], node->input_port_sizes[0], node->output_port_sizes[0]);
413-
414-
if (len <= sanity) /* buffer not large enough */
415-
oassert(FALSE);
416-
417-
/* Give names to the output pins */
418-
for (i = 0; i < node->num_output_pins; i++)
419432
{
420-
if (node->output_pins[i]->name)
433+
/* Give names to the output pins */
434+
for (int i = 0; i < node->num_output_pins; i++)
421435
{
422-
vtr::free(node->output_pins[i]->name);
436+
if (node->output_pins[i]->name)
437+
{
438+
vtr::free(node->output_pins[i]->name);
439+
}
440+
//build the output string
441+
std::string tmp(
442+
node_name +
443+
"[" + std::to_string(node->output_pins[i]->pin_node_idx) + "]"
444+
);
445+
node->output_pins[i]->name = vtr::strdup(tmp.c_str());
423446
}
424-
len = strlen(node->name) + 6; /* 6 chars for pin idx */
425-
new_name = (char*)vtr::malloc(len);
426-
odin_sprintf(new_name, "%s[%d]", node->name, node->output_pins[i]->pin_node_idx);
427-
node->output_pins[i]->name = new_name;
447+
node->name = vtr::strdup(node->output_pins[node->num_output_pins-1]->name);
428448
}
429-
vtr::free(node->name);
430-
node->name = new_name;
431449
node->traverse_visited = mark;
432450
return;
433451
}

0 commit comments

Comments
 (0)