From 06adfa3753d7856eb8600764e3e61551dab426fa 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 | 1 + 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 | 1 + 6 files changed, 46 insertions(+) diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 526bb8c0a..6c5c50445 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -1044,6 +1044,7 @@ inp_dodeck( ct->ci_inprogress = FALSE; ct->ci_runonce = FALSE; ct->ci_commands = end; + ct->ci_dicos = nupa_add_dicoslist(); if (reuse) tfree(ct->ci_filename); ct->ci_filename = copy(filename); diff --git a/src/frontend/numparam/numpaif.h b/src/frontend/numparam/numpaif.h index 706034368..240a1ae77 100644 --- a/src/frontend/numparam/numpaif.h +++ b/src/frontend/numparam/numpaif.h @@ -21,6 +21,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 876f423e1..4685b8874 100644 --- a/src/frontend/numparam/spicenum.c +++ b/src/frontend/numparam/spicenum.c @@ -409,6 +409,7 @@ static unsigned char dologfileS = 0; /* for debugging */ static unsigned char firstsignalS = 1; static FILE *logfileS = NULL; static dico_t *dicoS = NULL; +static dico_t *dicos_list[100]; /* already part of dico : */ @@ -909,3 +910,36 @@ dump_symbols(dico_t *dico) fprintf(stderr, "Symbol table\n"); nupa_list_params(stderr); } + + +/* 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 c9b18ad04..1b58e0215 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 50f92392d..1bce6bfce 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; @@ -281,5 +283,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 376d85815..e4206cea2 100644 --- a/src/include/ngspice/ftedefs.h +++ b/src/include/ngspice/ftedefs.h @@ -53,6 +53,7 @@ struct circ { JOB *ci_specOpt; /* the special options anal. for command line jobs */ JOB *ci_curOpt; /* the 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*/ FTESTATistics *FTEstats; /* Statistics for the front end */ } ;