Commit 2449a436 authored by Sébastien Villemot's avatar Sébastien Villemot

K-order DLL, DynareNameList class: removed memory leaks

parent d95b0cf6
......@@ -33,8 +33,8 @@
/* Dynare DynamicModel class */
/**************************************************************************************/
KordpDynare::KordpDynare(const char **endo, int num_endo,
const char **exo, int nexog, int npar,
KordpDynare::KordpDynare(const vector<string> &endo, int num_endo,
const vector<string> &exo, int nexog, int npar,
Vector &ysteady, TwoDMatrix &vcov, Vector &inParams, int nstat,
int npred, int nforw, int nboth, const int jcols, const Vector &nnzd,
const int nsteps, int norder,
......@@ -312,52 +312,16 @@ KordpDynare::ReorderDynareJacobianIndices() throw (TLException)
/**************************************************************************************/
/* DynareNameList class */
/**************************************************************************************/
vector<int>
DynareNameList::selectIndices(const vector<const char *> &ns) const throw (DynareException)
{
vector<int> res;
for (unsigned int i = 0; i < ns.size(); i++)
{
int j = 0;
while (j < getNum() && strcmp(getName(j), ns[i]) != 0)
j++;
if (j == getNum())
throw DynareException(__FILE__, __LINE__,
string("Couldn't find name for ") + ns[i]
+" in DynareNameList::selectIndices");
res.push_back(j);
}
return res;
}
DynareNameList::DynareNameList(const KordpDynare &dynare)
{
for (int i = 0; i < dynare.ny(); i++)
names.push_back(dynare.dnl.getName(i));
}
DynareNameList::DynareNameList(const KordpDynare &dynare, const char **namesp)
{
for (int i = 0; i < dynare.ny(); i++)
names.push_back(namesp[i]);
}
DynareExogNameList::DynareExogNameList(const KordpDynare &dynare)
DynareNameList::DynareNameList(const KordpDynare &dynare, const vector<string> &names_arg) : names(names_arg)
{
for (int i = 0; i < dynare.nexog(); i++)
names.push_back(dynare.denl.getName(i));
}
DynareExogNameList::DynareExogNameList(const KordpDynare &dynare, const char **namesp)
{
for (int i = 0; i < dynare.nexog(); i++)
names.push_back(namesp[i]);
}
DynareStateNameList::DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
const DynareExogNameList &denl)
const DynareNameList &denl)
{
for (int i = 0; i < dynare.nys(); i++)
names.push_back(dnl.getName(i+dynare.nstat()));
names.push_back(string(dnl.getName(i+dynare.nstat())));
for (int i = 0; i < dynare.nexog(); i++)
names.push_back(denl.getName(i));
names.push_back(string(denl.getName(i)));
}
......@@ -45,10 +45,9 @@ class KordpDynare;
/*////////////////////////////////////////////*/
class DynareNameList : public NameList
{
vector<const char *> names;
vector<string> names;
public:
DynareNameList(const KordpDynare &dynare);
DynareNameList(const KordpDynare &dynare, const char **names);
DynareNameList(const KordpDynare &dynare, const vector<string> &names_arg);
int
getNum() const
{
......@@ -57,38 +56,16 @@ public:
const char *
getName(int i) const
{
return names[i];
}
/** This for each string of the input vector calculates its index
* in the names. And returns the resulting vector of indices. If
* the name cannot be found, then an exception is raised. */
vector<int> selectIndices(const vector<const char *> &ns) const throw (DynareException);
};
class DynareExogNameList : public NameList
{
vector<const char *> names;
public:
DynareExogNameList(const KordpDynare &dynare);
DynareExogNameList(const KordpDynare &dynare, const char **names);
int
getNum() const
{
return (int) names.size();
}
const char *
getName(int i) const
{
return names[i];
return names[i].c_str();
}
};
class DynareStateNameList : public NameList
{
vector<const char *> names;
vector<string> names;
public:
DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
const DynareExogNameList &denl);
const DynareNameList &denl);
int
getNum() const
{
......@@ -97,7 +74,7 @@ public:
const char *
getName(int i) const
{
return names[i];
return names[i].c_str();
}
};
/*********************************************/
......@@ -108,7 +85,6 @@ class DynamicModelDLL;
class KordpDynare : public DynamicModel
{
friend class DynareNameList;
friend class DynareExogNameList;
friend class DynareStateNameList;
friend class DynamicModelDLL;
......@@ -131,8 +107,7 @@ class KordpDynare : public DynamicModel
Vector &params;
TwoDMatrix &vCov;
TensorContainer<FSSparseTensor> md; // ModelDerivatives
DynareNameList dnl;
DynareExogNameList denl;
DynareNameList dnl, denl;
DynareStateNameList dsnl;
const double ss_tol;
const vector<int> &varOrder;
......@@ -140,8 +115,8 @@ class KordpDynare : public DynamicModel
double qz_criterium;
vector<int> JacobianIndices;
public:
KordpDynare(const char **endo, int num_endo,
const char **exo, int num_exo, int num_par,
KordpDynare(const vector<string> &endo, int num_endo,
const vector<string> &exo, int num_exo, int num_par,
Vector &ySteady, TwoDMatrix &vCov, Vector &params, int nstat, int nPred,
int nforw, int nboth, const int nJcols, const Vector &NNZD,
const int nSteps, const int ord,
......
......@@ -48,41 +48,23 @@
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) // exclude mexFunction for other applications
const char **
DynareMxArrayToString(const char *cNamesCharStr, const int len, const int width)
{
char **cNamesMX;
cNamesMX = (char **) calloc(len, sizeof(char *));
for (int i = 0; i < len; i++)
cNamesMX[i] = (char *) calloc(width+1, sizeof(char));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < len; j++)
{
// Allow alphanumeric and underscores "_" only:
if (isalnum(cNamesCharStr[j+i*len]) || ('_' == cNamesCharStr[j+i*len]))
{
cNamesMX[j][i] = cNamesCharStr[j+i*len];
}
else cNamesMX[j][i] = '\0';
}
}
return (const char **) cNamesMX;
}
//////////////////////////////////////////////////////
// Convert Matlab Dynare endo and exo names array to C type array of string pointers
// Convert MATLAB Dynare endo and exo names array to a vector<string> array of string pointers
// Poblem is that Matlab mx function returns a long string concatenated by columns rather than rows
// hence a rather low level approach is needed
///////////////////////////////////////////////////////
const char **
DynareMxArrayToString(const mxArray *mxFldp, const int len, const int width)
void
DynareMxArrayToString(const mxArray *mxFldp, const int len, const int width, vector<string> &out)
{
char *cNamesCharStr = mxArrayToString(mxFldp);
const char **ret = DynareMxArrayToString(cNamesCharStr, len, width);
return ret;
out.resize(len);
for (int i = 0; i < width; i++)
for (int j = 0; j < len; j++)
// Allow alphanumeric and underscores "_" only:
if (isalnum(cNamesCharStr[j+i*len]) || (cNamesCharStr[j+i*len] == '_'))
out[j] += cNamesCharStr[j+i*len];
}
extern "C" {
......@@ -196,12 +178,14 @@ extern "C" {
mxFldp = mxGetField(M_, 0, "var_order_endo_names");
const int nendo = (int) mxGetM(mxFldp);
const int widthEndo = (int) mxGetN(mxFldp);
const char **endoNamesMX = DynareMxArrayToString(mxFldp, nendo, widthEndo);
vector<string> endoNames;
DynareMxArrayToString(mxFldp, nendo, widthEndo, endoNames);
mxFldp = mxGetField(M_, 0, "exo_names");
const int nexo = (int) mxGetM(mxFldp);
const int widthExog = (int) mxGetN(mxFldp);
const char **exoNamesMX = DynareMxArrayToString(mxFldp, nexo, widthExog);
vector<string> exoNames;
DynareMxArrayToString(mxFldp, nexo, widthExog, exoNames);
if ((nEndo != nendo) || (nExog != nexo))
mexErrMsgTxt("Incorrect number of input parameters.");
......@@ -225,7 +209,7 @@ extern "C" {
tls.init(kOrder, nStat+2*nPred+3*nBoth+2*nForw+nExog);
// make KordpDynare object
KordpDynare dynare(endoNamesMX, nEndo, exoNamesMX, nExog, nPar, // paramNames,
KordpDynare dynare(endoNames, nEndo, exoNames, nExog, nPar,
ySteady, vCov, modParams, nStat, nPred, nForw, nBoth,
jcols, NNZD, nSteps, kOrder, journal, dynamicDLL,
sstol, var_order_vp, llincidence, qz_criterium);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment