From c2605a5c9be49bca8929cffb0303370886dafc42 Mon Sep 17 00:00:00 2001 From: Cary R Date: Tue, 13 Jan 2009 09:57:12 -0800 Subject: [PATCH] Fix memory leaks in vvp and change vvp T_STRING token to be new based. This patch fixes a bunch of memory leaks in vvp and converts the T_STRING lexical token to be new based. There are still two known leaks that I need to find a way to fix and likely some unknown leaks that still need to be found and fixed. --- vpi/sys_display.c | 6 ++++++ vpi/sys_random.c | 4 ++-- vpi/sys_readmem.c | 8 ++++++-- vpi/sys_vcd.c | 13 ++++++++++--- vvp/array.cc | 8 ++++---- vvp/compile.cc | 6 +++--- vvp/compile.h | 5 ++--- vvp/concat.cc | 23 ++--------------------- vvp/event.cc | 6 ++++-- vvp/lexor.lex | 9 +++++++-- vvp/main.cc | 10 ++++++++-- vvp/sfunc.cc | 5 +++-- vvp/vpi_scope.cc | 7 ++++--- vvp/words.cc | 14 +++++++------- 14 files changed, 68 insertions(+), 56 deletions(-) diff --git a/vpi/sys_display.c b/vpi/sys_display.c index 4faa2a350..4f0b7bf69 100644 --- a/vpi/sys_display.c +++ b/vpi/sys_display.c @@ -2213,6 +2213,7 @@ static PLI_INT32 sys_fdisplay_calltf(PLI_BYTE8*name) vpi_printf("invalid file descriptor/MCD (0x%x) given to %s.\n", fd_mcd, name); vpi_control(vpiFinish, 1); + vpi_free_object(argv); return 0; } @@ -2309,6 +2310,7 @@ static PLI_INT32 sys_swrite_calltf(PLI_BYTE8 *name) "(see %%u/%%z).\n", info.filename, info.lineno, name); } + free(val.value.str); free(info.filename); free(info.items); return 0; @@ -2406,6 +2408,7 @@ static PLI_INT32 sys_sformat_calltf(PLI_BYTE8 *name) "(see %%u/%%z).\n", info.filename, info.lineno, name); } + free(val.value.str); free(info.filename); free(info.items); return 0; @@ -2415,6 +2418,7 @@ static PLI_INT32 sys_end_of_compile(p_cb_data cb_data) { /* The default timeformat prints times in unit of simulation precision. */ + free(timeformat_info.suff); timeformat_info.suff = strdup(""); timeformat_info.units = vpi_get(vpiTimePrecision, 0); timeformat_info.prec = 0; @@ -2481,6 +2485,7 @@ static PLI_INT32 sys_timeformat_calltf(PLI_BYTE8*xx) value.format = vpiStringVal; vpi_get_value(suff, &value); + free(timeformat_info.suff); timeformat_info.suff = strdup(value.value.str); value.format = vpiIntVal; @@ -2553,6 +2558,7 @@ static PLI_INT32 sys_no_aa_compiletf(PLI_BYTE8 *name) vpi_printf("%s arguments may not be automatically " "allocated variables.\n", name); vpi_control(vpiFinish, 1); + vpi_free_object(argv); return 0; } } diff --git a/vpi/sys_random.c b/vpi/sys_random.c index 3e2e8dec7..866ab53c1 100644 --- a/vpi/sys_random.c +++ b/vpi/sys_random.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2003 Stephen Williams (steve@icarus.com) + * Copyright (c) 2000-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -645,6 +645,7 @@ static PLI_INT32 sys_urandom_range_calltf(PLI_BYTE8 *name) /* Calculate and return the result. */ val.value.integer = urandom(0, i_maxval, i_minval); vpi_put_value(callh, &val, 0, vpiNoDelay); + vpi_free_object(argv); return 0; } @@ -978,4 +979,3 @@ void sys_random_register() tf_data.user_data = "$dist_erlang"; vpi_register_systf(&tf_data); } - diff --git a/vpi/sys_readmem.c b/vpi/sys_readmem.c index a7381f53f..91f2a51b0 100644 --- a/vpi/sys_readmem.c +++ b/vpi/sys_readmem.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999 Stephen Williams (steve@icarus.com) + * Copyright (c) 1999-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -412,6 +412,7 @@ static PLI_INT32 sys_writemem_calltf(PLI_BYTE8*name) start_item = vpi_scan(argv); if (start_item!=0){ if (check_integer_constant(name, start_item) == 0){ + free(path); vpi_free_object(argv); return 0; } @@ -420,6 +421,7 @@ static PLI_INT32 sys_writemem_calltf(PLI_BYTE8*name) stop_item = vpi_scan(argv); if (stop_item!=0){ if (check_integer_constant(name, stop_item) == 0){ + free(path); vpi_free_object(argv); return 0; } @@ -427,6 +429,7 @@ static PLI_INT32 sys_writemem_calltf(PLI_BYTE8*name) /* Check that there is no 5th parameter */ if (vpi_scan(argv) != 0){ vpi_printf("ERROR: %s accepts maximum 4 parameters!\n", name ); + free(path); vpi_free_object(argv); return 0; } @@ -519,6 +522,7 @@ static PLI_INT32 sys_writemem_calltf(PLI_BYTE8*name) item = vpi_scan(words); wwid = vpi_get(vpiSize, item); + vpi_free_object(words); if (strcmp(name,"$writememb")==0){ value.format = vpiBinStrVal; @@ -541,6 +545,7 @@ static PLI_INT32 sys_writemem_calltf(PLI_BYTE8*name) } fclose(file); + free(path); return 0; } @@ -580,4 +585,3 @@ void sys_readmem_register() tf_data.user_data = "$writememb"; vpi_register_systf(&tf_data); } - diff --git a/vpi/sys_vcd.c b/vpi/sys_vcd.c index ba175e58d..4685a2137 100644 --- a/vpi/sys_vcd.c +++ b/vpi/sys_vcd.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 1999-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -428,6 +428,7 @@ static PLI_INT32 sys_dumpfile_calltf(PLI_BYTE8*name) vpi_printf("VCD warning: %s called after $dumpvars started,\n" " using existing file (%s).\n", name, dump_path); + vpi_free_object(argv); return 0; } @@ -698,10 +699,16 @@ static PLI_INT32 sys_dumpvars_calltf(PLI_BYTE8*name) if (dump_file == 0) { open_dumpfile(callh); - if (dump_file == 0) return 0; + if (dump_file == 0) { + vpi_free_object(argv); + return 0; + } } - if (install_dumpvars_callback()) return 0; + if (install_dumpvars_callback()) { + vpi_free_object(argv); + return 0; + } /* Get the depth if it exists. */ if (argv) { diff --git a/vvp/array.cc b/vvp/array.cc index 1da9117f7..875a0ed1c 100644 --- a/vvp/array.cc +++ b/vvp/array.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2007-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -938,7 +938,7 @@ void compile_var_array(char*label, char*name, int last, int first, count_var_array_words += arr->array_count; free(label); - free(name); + delete[] name; } void compile_real_array(char*label, char*name, int last, int first, @@ -962,7 +962,7 @@ void compile_real_array(char*label, char*name, int last, int first, count_real_array_words += arr->array_count; free(label); - free(name); + delete[] name; } void compile_net_array(char*label, char*name, int last, int first) @@ -976,7 +976,7 @@ void compile_net_array(char*label, char*name, int last, int first) count_net_array_words += arr->array_count; free(label); - free(name); + delete[] name; } class vvp_fun_arrayport : public vvp_net_fun_t { diff --git a/vvp/compile.cc b/vvp/compile.cc index 6a8e4443e..f43c76f65 100644 --- a/vvp/compile.cc +++ b/vvp/compile.cc @@ -704,7 +704,7 @@ void compile_init(void) void compile_load_vpi_module(char*name) { vpip_load_module(name); - free(name); + delete[] name; } void compile_vpi_time_precision(long pre) @@ -1710,7 +1710,7 @@ void compile_vpi_call(char*label, char*name, compile_errors += 1; /* Done with the lexor-allocated name string. */ - free(name); + delete[] name; } void compile_vpi_func_call(char*label, char*name, @@ -1733,7 +1733,7 @@ void compile_vpi_func_call(char*label, char*name, compile_errors += 1; /* Done with the lexor-allocated name string. */ - free(name); + delete[] name; } /* diff --git a/vvp/compile.h b/vvp/compile.h index fa56d1e88..d8c5c7e89 100644 --- a/vvp/compile.h +++ b/vvp/compile.h @@ -1,7 +1,7 @@ #ifndef __compile_H #define __compile_H /* - * Copyright (c) 2001-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -412,7 +412,7 @@ extern void compile_codelabel(char*label); * The parser uses these functions to compile .scope statements. * The implementations of these live in the vpi_scope.cc file. */ -extern void compile_scope_decl(char*typ, char*lab, char*nam,const char*tnam, +extern void compile_scope_decl(char*typ, char*lab, char*nam, char*tnam, char*par, long file_idx, long lineno, long def_file_idx, long def_lineno); extern void compile_scope_recall(char*sym); @@ -472,5 +472,4 @@ extern void compile_island_tranif(int sense, char*island, extern void compile_island_tranvp(char*island, char*ba, char*bb, unsigned width, unsigned part, unsigned off); - #endif diff --git a/vvp/concat.cc b/vvp/concat.cc index bab8f2ab2..9b0ee161d 100644 --- a/vvp/concat.cc +++ b/vvp/concat.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2005 Stephen Williams (steve@icarus.com) + * Copyright (c) 2004-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -80,6 +80,7 @@ void compile_concat(char*label, unsigned w0, unsigned w1, free(label); inputs_connect(net, argc, argv); + free(argv); } vvp_fun_repeat::vvp_fun_repeat(unsigned width, unsigned repeat) @@ -121,23 +122,3 @@ void compile_repeat(char*label, long width, long repeat, struct symb_s arg) input_connect(net, 0, arg.text); } - - -/* - * $Log: concat.cc,v $ - * Revision 1.5 2005/06/22 00:04:48 steve - * Reduce vvp_vector4 copies by using const references. - * - * Revision 1.4 2005/06/17 03:46:52 steve - * Make functors know their own width. - * - * Revision 1.3 2005/04/09 05:30:38 steve - * Default behavior for recv_vec8 methods. - * - * Revision 1.2 2005/02/07 22:42:42 steve - * Add .repeat functor and BIFIF functors. - * - * Revision 1.1 2005/01/22 00:01:09 steve - * Add missing concat.cc to cvs - * - */ diff --git a/vvp/event.cc b/vvp/event.cc index bff6fb18f..a51a711aa 100644 --- a/vvp/event.cc +++ b/vvp/event.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2004-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -695,6 +695,7 @@ void compile_event(char*label, char*type, unsigned argc, struct symb_s*argv) free(label); inputs_connect(ptr, argc, argv); + free(argv); } static void compile_event_or(char*label, unsigned argc, struct symb_s*argv) @@ -714,6 +715,7 @@ static void compile_event_or(char*label, unsigned argc, struct symb_s*argv) for (unsigned idx = 0 ; idx < argc ; idx += 1) { input_connect(ptr, 0, argv[idx].text); } + free(argv); } /* @@ -737,5 +739,5 @@ void compile_named_event(char*label, char*name) vpip_attach_to_current_scope(obj); free(label); - free(name); + delete[] name; } diff --git a/vvp/lexor.lex b/vvp/lexor.lex index 9c73062df..d70c00754 100644 --- a/vvp/lexor.lex +++ b/vvp/lexor.lex @@ -4,7 +4,7 @@ %{ /* - * Copyright (c) 2001-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -27,6 +27,11 @@ # include "parse.h" # include # include + +static char* strdupnew(char const *str) +{ + return str ? strcpy(new char [strlen(str)+1], str) : 0; +} %} %% @@ -48,7 +53,7 @@ contents of the string without the enclosing quotes. */ \"([^\"\\]|\\.)*\" { yytext[strlen(yytext)-1] = 0; - yylval.text = strdup(yytext+1); + yylval.text = strdupnew(yytext+1); assert(yylval.text); return T_STRING; } diff --git a/vvp/main.cc b/vvp/main.cc index b78e699bd..a729d7bc9 100644 --- a/vvp/main.cc +++ b/vvp/main.cc @@ -146,7 +146,7 @@ void verify_version(char*ivl_ver, char*commit) if (commit) vpi_mcd_printf(1, " %s", commit); vpi_mcd_printf(1, "\n"); } - free(commit); + delete[] commit; char*vvp_ver = strdup(VERSION); char *vp, *ip; @@ -186,7 +186,7 @@ void verify_version(char*ivl_ver, char*commit) ip, vp); } - free(ivl_ver); + delete[] ivl_ver; free(vvp_ver); } @@ -418,5 +418,11 @@ int main(int argc, char*argv[]) count_gen_events, count_gen_pool()); } + /* Clean up the memory. */ + for (vector::iterator cur = file_names.begin(); + cur != file_names.end() ; cur++) { + delete[] *cur; + } + return vvp_return_value; } diff --git a/vvp/sfunc.cc b/vvp/sfunc.cc index 65129fcbd..d242dffeb 100644 --- a/vvp/sfunc.cc +++ b/vvp/sfunc.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2006-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -145,7 +145,7 @@ void compile_sfunc(char*label, char*name, char*format_string, { vpiHandle*vpi_argv = new vpiHandle[argc]; int width_code = make_vpi_argv(argc, vpi_argv, format_string); - free(format_string); + delete[] format_string; vvp_net_t*ptr = new vvp_net_t; @@ -167,4 +167,5 @@ void compile_sfunc(char*label, char*name, char*format_string, that event. */ if (trigger_label) input_connect(ptr, 0, trigger_label); + delete[] name; } diff --git a/vvp/vpi_scope.cc b/vvp/vpi_scope.cc index a03245108..87eec4c6f 100644 --- a/vvp/vpi_scope.cc +++ b/vvp/vpi_scope.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -326,7 +326,7 @@ static void attach_to_scope_(struct __vpiScope*scope, vpiHandle obj) * symbol table and the name is used to construct the actual object. */ void -compile_scope_decl(char*label, char*type, char*name, const char*tname, +compile_scope_decl(char*label, char*type, char*name, char*tname, char*parent, long file_idx, long lineno, long def_file_idx, long def_lineno) { @@ -381,7 +381,8 @@ compile_scope_decl(char*label, char*type, char*name, const char*tname, free(label); free(type); - free(name); + delete[] name; + delete[] tname; if (parent) { static vpiHandle obj; diff --git a/vvp/words.cc b/vvp/words.cc index 4537e2368..cd1a06d51 100644 --- a/vvp/words.cc +++ b/vvp/words.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2008 Stephen Williams (steve@icarus.com) + * Copyright (c) 2003-2009 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -59,7 +59,7 @@ static void __compile_var_real(char*label, char*name, array_attach_word(array, array_addr, obj); } free(label); - if (name) free(name); + if (name) delete[] name; } void compile_var_real(char*label, char*name, int msb, int lsb) @@ -118,7 +118,7 @@ static void __compile_var(char*label, char*name, if (obj) array_attach_word(array, array_addr, obj); } free(label); - if (name) free(name); + if (name) delete[] name; } void compile_variable(char*label, char*name, @@ -194,7 +194,7 @@ static void __compile_net(char*label, char*name, vpip_attach_to_current_scope(obj); free(label); - if (name) free(name); + if (name) delete[] name; if (array_label) free(array_label); free(argv); } @@ -254,7 +254,7 @@ static void __compile_real(char*label, char*name, else if (obj) vpip_attach_to_current_scope(obj); free(label); - if (name) free(name); + if (name) delete[] name; if (array_label) free(array_label); free(argv); } @@ -313,7 +313,7 @@ void compile_alias(char*label, char*name, int msb, int lsb, bool signed_flag, vpip_attach_to_current_scope(obj); free(label); - free(name); + delete[] name; free(argv[0].text); free(argv); } @@ -335,7 +335,7 @@ void compile_alias_real(char*label, char*name, int msb, int lsb, vpip_attach_to_current_scope(obj); free(label); - free(name); + delete[] name; free(argv[0].text); free(argv); }