From 1066bf0a7b0d542f7fa02e9c9b5cb52d254d970d Mon Sep 17 00:00:00 2001 From: h_vogt Date: Sun, 12 Jun 2016 00:03:26 +0200 Subject: [PATCH] Store the numparam dicoS structure for each circuit in a list. Functions to add, remove, retrive dicoS from the list and to update dicoS, if the circuit has been changed. Keeping dicoS is necessary because it may be used by measure. This patch prevents (huge) memory leaks by overwriting dicoS if multiple calls to the 'source' command are executed. --- src/frontend/inp.c | 3 ++- src/frontend/numparam/numpaif.h | 3 +++ src/frontend/numparam/spicenum.c | 34 ++++++++++++++++++++++++++++++++ src/frontend/runcoms.c | 4 ++++ src/frontend/runcoms2.c | 3 +++ src/include/ngspice/ftedefs.h | 2 ++ 6 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 9dae7c7e6..4e20b903a 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -1057,8 +1057,9 @@ inp_dodeck( ct->ci_inprogress = FALSE; ct->ci_runonce = FALSE; ct->ci_commands = end; + ct->ci_dicos = nupa_add_dicoslist(); /* prevent false reads in multi-threaded ngshared */ -#ifndef SHARED_MODULE +#ifndef SHARED_MODULE if (reuse) tfree(ct->ci_filename); #endif diff --git a/src/frontend/numparam/numpaif.h b/src/frontend/numparam/numpaif.h index 816ba9e6d..6a1d84f26 100644 --- a/src/frontend/numparam/numpaif.h +++ b/src/frontend/numparam/numpaif.h @@ -23,6 +23,9 @@ extern void nupa_add_param(char *param_name, double value); extern void nupa_add_inst_param(char *param_name, double value); extern void nupa_copy_inst_dico(void); extern void nupa_del_dicoS(void); +extern int nupa_add_dicoslist(void); +extern void nupa_rem_dicoslist(int); +extern void nupa_set_dicoslist(int); extern int dynMaxckt; /* number of lines in deck after expansion */ diff --git a/src/frontend/numparam/spicenum.c b/src/frontend/numparam/spicenum.c index 35079be3e..1a7cb7160 100644 --- a/src/frontend/numparam/spicenum.c +++ b/src/frontend/numparam/spicenum.c @@ -297,6 +297,7 @@ static bool inexpansionS = 0; /* flag subckt expansion phase */ static bool incontrolS = 0; /* flag control code sections */ static bool firstsignalS = 1; static dico_t *dicoS = NULL; +static dico_t *dicos_list[100]; static void @@ -685,3 +686,36 @@ nupa_signal(int sig) firstsignalS = 1; } } + + +/* Store dicoS for each circuit loaded. + The return value will be stored in ft_curckt->ci_dicos. + We need to keep dicoS because it may be used by measure. */ +int +nupa_add_dicoslist(void) +{ + int i; + for (i = 0; i < 100; i++) + if (dicos_list[i] == NULL) { + dicos_list[i] = dicoS; + break; + } + + return (i); +} + + +/* remove dicoS from list if circuit is removed */ +void +nupa_rem_dicoslist(int ir) +{ + dicos_list[ir] = NULL; +} + + +/* change dicoS to the active circuit */ +void +nupa_set_dicoslist(int ir) +{ + dicoS = dicos_list[ir]; +} diff --git a/src/frontend/runcoms.c b/src/frontend/runcoms.c index 8065f1d5f..d8cede126 100644 --- a/src/frontend/runcoms.c +++ b/src/frontend/runcoms.c @@ -15,6 +15,8 @@ Modified: 2000 AlansFixes #include "ngspice/ftedebug.h" #include "ngspice/dvec.h" +#include "numparam/numpaif.h" + #include "circuits.h" #include "completion.h" #include "runcoms.h" @@ -108,6 +110,8 @@ com_scirc(wordlist *wl) modtab = ft_curckt->ci_modtab; /* get the database for save, iplot, stop */ dbs = ft_curckt->ci_dbs; + /* set the numparam dicos structure for use with measure */ + nupa_set_dicoslist(ft_curckt->ci_dicos); } diff --git a/src/frontend/runcoms2.c b/src/frontend/runcoms2.c index 90e11cad3..fe223ca3d 100644 --- a/src/frontend/runcoms2.c +++ b/src/frontend/runcoms2.c @@ -222,6 +222,8 @@ com_remcirc(wordlist *wl) /* delete numparam data structure dicoS */ nupa_del_dicoS(); + /* delete entry in dicoslist */ + nupa_rem_dicoslist(ft_curckt->ci_dicos); dbfree(ft_curckt->ci_dbs); ft_curckt->ci_dbs = NULL; @@ -283,5 +285,6 @@ com_remcirc(wordlist *wl) if (ft_curckt) { modtab = ft_curckt->ci_modtab; dbs = ft_curckt->ci_dbs; + nupa_set_dicoslist(ft_curckt->ci_dicos); } } diff --git a/src/include/ngspice/ftedefs.h b/src/include/ngspice/ftedefs.h index 18d045347..758268859 100644 --- a/src/include/ngspice/ftedefs.h +++ b/src/include/ngspice/ftedefs.h @@ -56,6 +56,8 @@ struct circ { JOB *ci_curOpt; /* most recent options anal. for the circuit */ char *ci_last_an; /* name of last analysis run */ + int ci_dicos; /* index to the numparam dicoS structure + for this circuit */ struct pt_temper *modtlist; /* all expressions with 'temper' in .model lines */ struct pt_temper *devtlist; /* all expressions with 'temper'