From 8d374157fe2a5013a3b59fe9dba93d1319291f31 Mon Sep 17 00:00:00 2001 From: h_vogt Date: Mon, 10 Sep 2012 22:52:10 +0200 Subject: [PATCH] XSPICE square: remove memory leak, initialize phase1 --- .../examples/analog_models4_transient.deck | 7 ++- src/xspice/icm/analog/square/cfunc.mod | 52 ++++++++++++++----- src/xspice/icm/analog/square/ifspec.ifs | 8 ++- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/xspice/examples/analog_models4_transient.deck b/src/xspice/examples/analog_models4_transient.deck index 0fc5654b1..a5bc2687c 100644 --- a/src/xspice/examples/analog_models4_transient.deck +++ b/src/xspice/examples/analog_models4_transient.deck @@ -53,7 +53,12 @@ a5 3 1 0 50 oneshot1 + retrig=FALSE) * * -* +*** square block *** +a6 2 60 square1 +.model square1 square (cntl_array=[-1.0 0.0 10.0 20.0] ++ freq_array=[500 500 7000 7000] ++ out_low=-0.8 out_high=1.2 duty_cycle=0.5 ++ rise_time=1e-6 fall_time=2e-6) * *** resistors to ground *** r1 1 0 10k diff --git a/src/xspice/icm/analog/square/cfunc.mod b/src/xspice/icm/analog/square/cfunc.mod index 94337e553..2ef1d9399 100644 --- a/src/xspice/icm/analog/square/cfunc.mod +++ b/src/xspice/icm/analog/square/cfunc.mod @@ -69,6 +69,18 @@ NON-STANDARD FEATURES +typedef struct { + + double *control; /* the storage array for the + control vector (cntl_array) */ + + double *freq; /* the storage array for the + pulse width array (pw_array) */ + + Boolean_t tran_init; /* for initialization of phase1) */ + +} Local_Data_t; + /*=== FUNCTION PROTOTYPE DEFINITIONS ===*/ @@ -179,6 +191,9 @@ void cm_square(ARGS) /* structure holding parms, Mif_Complex_t ac_gain; + Local_Data_t *loc; /* Pointer to local static data, not to be included + in the state vector */ + /**** Retrieve frequently used parameters... ****/ cntl_size = PARAM_SIZE(cntl_array); @@ -205,6 +220,24 @@ void cm_square(ARGS) /* structure holding parms, cm_analog_alloc(T3,sizeof(double)); cm_analog_alloc(T4,sizeof(double)); + /*** allocate static storage for *loc ***/ + STATIC_VAR (locdata) = calloc (1 , sizeof ( Local_Data_t )); + loc = STATIC_VAR (locdata); + + /* Allocate storage for breakpoint domain & pulse width values */ + x = loc->control = (double *) calloc((size_t) cntl_size, sizeof(double)); + if (!x) { + cm_message_send(square_allocation_error); + return; + } + y = loc->freq = (double *) calloc((size_t) freq_size, sizeof(double)); + if (!y) { + cm_message_send(square_allocation_error); + return; + } + + loc->tran_init = FALSE; + } if(ANALYSIS == MIF_DC) { @@ -239,18 +272,13 @@ void cm_square(ARGS) /* structure holding parms, time3 = *t3; time4 = *t4; - /* Allocate storage for breakpoint domain & freq. range values */ + loc = STATIC_VAR (locdata); + x = loc->control; + y = loc->freq; - x = (double *) calloc((size_t) cntl_size, sizeof(double)); - if (!x) { - cm_message_send(square_allocation_error); - return; - } - - y = (double *) calloc((size_t) freq_size, sizeof(double)); - if (!y) { - cm_message_send(square_allocation_error); - return; + if (!loc->tran_init) { + *phase1 = 0.0; + loc->tran_init = TRUE; } /* Retrieve x and y values. */ @@ -301,7 +329,7 @@ void cm_square(ARGS) /* structure holding parms, *phase = *phase1 + freq*(TIME - T(1)); /* convert the phase to an integer */ - int_cycle = *phase1; + int_cycle = (int)*phase1; /* dphase is the percent into the cycle for the period */ diff --git a/src/xspice/icm/analog/square/ifspec.ifs b/src/xspice/icm/analog/square/ifspec.ifs index e5cdd6b46..5547e702c 100644 --- a/src/xspice/icm/analog/square/ifspec.ifs +++ b/src/xspice/icm/analog/square/ifspec.ifs @@ -20,7 +20,7 @@ NAME_TABLE: C_Function_Name: cm_square -Spice_Model_Name: square +Spice_Model_Name: square Description: "controlled square wave oscillator" @@ -80,3 +80,9 @@ Vector: no Vector_Bounds: - Null_Allowed: yes + +STATIC_VAR_TABLE: + +Static_Var_Name: locdata +Description: "local static data" +Data_Type: pointer