diff --git a/ChangeLog b/ChangeLog
index b70514742..58837608d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+2005-08-04 Paolo Nenzi
+
+ * Fixed a bug in memory management of xspice/ngspice that caused
+ segmentation fault for codemodels that adds their own states to
+ the state vector. Changes supplied by Hitoshi Tanaka
+ (HDA01055@nifty.com):
+
+ * src/math/ni/niiter.c: removed the first malloc for OldCKTstate0
+ since the number of states can be alteres by xspice code models.
+ This was already addressed in the past but this line remained.
+
+ * src/xspice/mif/mifsetup.c: added MIFunsetup() to conform to spice3f
+ devices model interface (xspice was based on spice3c1).
+
+ * src/xspice/xspice.c, src/xspice/cm/cmexport.c,
+ src/xspice/cmpp/writ_ifs.c: changed the entry corresponding to the
+ unsetup function from NULL to MIFunsetup.
+
+ * src/include/mifproto.h: added prototype for MIFunsetup.
+
+ * src/spicelib/analysis/cktsetup.c: cleared node if initial condition
+ o nodeset are given to fix a segfaults on second run of xspice
+ netlists.
+
+
2005-06-30 Paolo Nenzi
* src/frontend/resource.c: added IPC stuff
diff --git a/src/include/mifproto.h b/src/include/mifproto.h
index 924cbed5f..7cad1b760 100755
--- a/src/include/mifproto.h
+++ b/src/include/mifproto.h
@@ -81,6 +81,11 @@ extern int MIFsetup(
int *state
);
+extern int MIFunsetup(
+ GENmodel *inModel,
+ CKTcircuit *ckt
+);
+
extern int MIFload(
GENmodel *inModel,
CKTcircuit *ckt
diff --git a/src/maths/ni/niiter.c b/src/maths/ni/niiter.c
index 61583eaee..415ae8f92 100644
--- a/src/maths/ni/niiter.c
+++ b/src/maths/ni/niiter.c
@@ -68,7 +68,9 @@ NIiter(CKTcircuit *ckt, int maxIter)
return(error);
}
}
- OldCKTstate0=(double *)MALLOC((ckt->CKTnumStates+1)*sizeof(double));
+
+// OldCKTstate0=(double *)MALLOC((ckt->CKTnumStates+1)*sizeof(double));
+
for(;;){
ckt->CKTnoncon=0;
#ifdef NEWPRED
@@ -162,7 +164,7 @@ NIiter(CKTcircuit *ckt, int maxIter)
OldCKTstate0=(double *)MALLOC((ckt->CKTnumStates+1)*sizeof(double));
for(i=0;iCKTnumStates;i++) {
*(OldCKTstate0+i) = *(ckt->CKTstate0+i);
- };
+ }
startTime = (*(SPfrontEnd->IFseconds))();
SMPsolve(ckt->CKTmatrix,ckt->CKTrhs,ckt->CKTrhsSpare);
diff --git a/src/spicelib/analysis/cktsetup.c b/src/spicelib/analysis/cktsetup.c
index 0bb8478a8..83f9db13c 100644
--- a/src/spicelib/analysis/cktsetup.c
+++ b/src/spicelib/analysis/cktsetup.c
@@ -118,6 +118,7 @@ int
CKTunsetup(CKTcircuit *ckt)
{
int i, error, e2;
+ CKTnode *node;
error = OK;
if (!ckt->CKTisSetup)
@@ -127,6 +128,13 @@ CKTunsetup(CKTcircuit *ckt)
tfree(ckt->CKTstates[i]);
}
+ /* added by HT 050802*/
+ for(node=ckt->CKTnodes;node;node=node->next){
+ if(node->icGiven || node->nsGiven) {
+ node->ptr=0;
+ }
+ }
+
for (i=0;iCKThead[i] != NULL) ){
e2 = (*((*DEVices[i]).DEVunsetup))(ckt->CKThead[i],ckt);
diff --git a/src/xspice/cm/cmexport.c b/src/xspice/cm/cmexport.c
index 397b979d0..88a7e5ae6 100755
--- a/src/xspice/cm/cmexport.c
+++ b/src/xspice/cm/cmexport.c
@@ -19,7 +19,7 @@ struct coreInfo_t coreInfo =
MIFgetMod,
MIFgetValue,
MIFsetup,
- (int (*)(GENmodel *, CKTcircuit *))empty,
+ MIFunsetup,
MIFload,
MIFmParam,
MIFask,
diff --git a/src/xspice/cmpp/writ_ifs.c b/src/xspice/cmpp/writ_ifs.c
index 356986ff0..e160e4ea6 100755
--- a/src/xspice/cmpp/writ_ifs.c
+++ b/src/xspice/cmpp/writ_ifs.c
@@ -920,7 +920,7 @@ static void write_SPICEdev(
fprintf(fp, "MIFmParam, \n"); /* DEVmodParam */
fprintf(fp, "MIFload, \n"); /* DEVload */
fprintf(fp, "MIFsetup, \n"); /* DEVsetup */
- fprintf(fp, "NULL, \n"); /* DEVunsetup */
+ fprintf(fp, "MIFunsetup, \n"); /* DEVunsetup */
fprintf(fp, "NULL, \n"); /* DEVpzSetup */
fprintf(fp, "NULL, \n"); /* DEVtemperature */
fprintf(fp, "MIFtrunc, \n"); /* DEVtrunc */
diff --git a/src/xspice/mif/mifsetup.c b/src/xspice/mif/mifsetup.c
index 11708aa44..407b68023 100755
--- a/src/xspice/mif/mifsetup.c
+++ b/src/xspice/mif/mifsetup.c
@@ -44,8 +44,6 @@ NON-STANDARD FEATURES
/* #include "prefix.h" */
#include "ngspice.h"
-#include
-//#include "util.h"
#include "smpdefs.h"
#include "devdefs.h"
#include "sperror.h"
@@ -61,7 +59,7 @@ NON-STANDARD FEATURES
extern SPICEdev **DEVices; /* info about all device types */
-
+Mif_Boolean_t mif_UNSET=MIF_TRUE;
/* define macro for easy creation of matrix entries/pointers for outputs */
#define TSTALLOC(ptr,first,second) \
@@ -454,3 +452,77 @@ MIFsetup(
return(OK);
}
+
+
+int
+MIFunsetup(GENmodel *inModel,CKTcircuit *ckt)
+{
+ MIFmodel *model;
+ MIFinstance *here;
+ Mif_Smp_Ptr_t *smp_data_out;
+ Mif_Smp_Ptr_t *smp_data_cntl;
+ Mif_Port_Type_t type,in_type,out_type;
+ Mif_Cntl_Src_Type_t cntl_src_type;
+ int num_conn,num_port,i,j;
+
+
+ for (model = (MIFmodel *)inModel; model != NULL;
+ model = model->MIFnextModel)
+ {
+ for(here = model->MIFinstances; here != NULL;
+ here = here->MIFnextInstance)
+ {
+ num_conn=here->num_conn;
+ for(i = 0; i < num_conn; i++) {
+
+ // if the connection is null, skip to next connection
+ if(here->conn[i]->is_null) continue;
+
+ // prepare things for convenient access later
+ num_port = here->conn[i]->size;
+
+ // loop through all ports on this connection
+ for(j = 0; j < num_port; j++) {
+
+ // Skip if port is digital or user-defined type
+ type = here->conn[i]->port[j]->type;
+
+ // determine the type of this output port
+ out_type = here->conn[i]->port[j]->type;
+
+ // create a pointer to the smp data for quick access
+ smp_data_out = &(here->conn[i]->port[j]->smp_data);
+
+ // determine the type of this input port
+ in_type = here->conn[i]->port[j]->type;
+ cntl_src_type = MIFget_cntl_src_type(in_type, out_type);
+
+ switch(cntl_src_type)
+ {
+ case MIF_VCVS:
+ case MIF_ICVS:
+ case -1:
+ if(smp_data_out->branch)
+ {
+ CKTdltNNum(ckt, smp_data_out->branch);
+ smp_data_out->branch = 0;
+ }
+
+ if(smp_data_out->ibranch)
+ {
+ CKTdltNNum(ckt, smp_data_out->ibranch);
+ smp_data_out->ibranch = 0;
+ }
+ here->initialized=MIF_FALSE;
+ break;
+ }
+
+ }
+ }
+
+ }
+ }
+ //printf("MIFunsetup completed.\n");
+ return OK;
+}
+
diff --git a/src/xspice/xspice.c b/src/xspice/xspice.c
index 3c89f0097..1762f5223 100755
--- a/src/xspice/xspice.c
+++ b/src/xspice/xspice.c
@@ -19,7 +19,7 @@ struct coreInfo_t coreInfo =
MIFgetMod,
MIFgetValue,
MIFsetup,
- (int (*)(GENmodel *, CKTcircuit *))empty,
+ MIFunsetup,
MIFload,
MIFmParam,
MIFask,