4.1 k_order_perturbation: simplifying dynamic_dll code

git-svn-id: https://www.dynare.org/svn/dynare/trunk@3199 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
michel 2009-12-06 14:10:43 +00:00
parent f22fa8979c
commit 5031122649
2 changed files with 9 additions and 15 deletions

View File

@ -43,8 +43,9 @@ DynamicModelDLL::DynamicModelDLL(const string &modName, const int y_length, cons
dynamicHinstance = ::LoadLibrary(fName.c_str()); dynamicHinstance = ::LoadLibrary(fName.c_str());
if (dynamicHinstance == NULL) if (dynamicHinstance == NULL)
throw 1; throw 1;
Dynamic = (DynamicFn *) ::GetProcAddress(dynamicHinstance, "Dynamic"); Dynamic = (DynamicFn) ::GetProcAddress(dynamicHinstance, "Dynamic");
if (Dynamic == NULL)
throw 2;
#else // Linux or Mac #else // Linux or Mac
dynamicHinstance = dlopen(fName.c_str(), RTLD_NOW); dynamicHinstance = dlopen(fName.c_str(), RTLD_NOW);
if ((dynamicHinstance == NULL) || dlerror()) if ((dynamicHinstance == NULL) || dlerror())
@ -69,14 +70,16 @@ DynamicModelDLL::DynamicModelDLL(const string &modName, const int y_length, cons
} }
catch (...) catch (...)
{ {
throw DynareException(__FILE__, __LINE__, string("Can't load ") + fName); throw DynareException(__FILE__, __LINE__, string("Can't find Dynamic function in ") + fName);
} }
} }
DynamicModelDLL::~DynamicModelDLL() DynamicModelDLL::~DynamicModelDLL()
{ {
#if defined(__CYGWIN32__) || defined(_WIN32) #if defined(__CYGWIN32__) || defined(_WIN32)
FreeLibrary(dynamicHinstance); bool result = FreeLibrary(dynamicHinstance);
if (result == 0)
throw DynareException(__FILE__, __LINE__, string("Can't free the *_dynamic DLL"));
#else #else
dlclose(dynamicHinstance); dlclose(dynamicHinstance);
#endif #endif

View File

@ -39,16 +39,10 @@
#include "dynare_exception.h" #include "dynare_exception.h"
// <model>_Dynamic DLL pointer // <model>_Dynamic DLL pointer
#if defined(_WIN32) || defined(__CYGWIN32__)
typedef void *(DynamicFn)
#else // Linux or Mac
typedef void (*DynamicFn) typedef void (*DynamicFn)
#endif
(double *y, double *x, int nb_row_x, double *params, (double *y, double *x, int nb_row_x, double *params,
int it_, double *residual, double *g1, double *g2, double *g3); int it_, double *residual, double *g1, double *g2, double *g3);
typedef void *(mexFunctionPtr)(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
/** /**
* creates pointer to Dynamic function inside <model>_dynamic.dll * creates pointer to Dynamic function inside <model>_dynamic.dll
* and handles calls to it. * and handles calls to it.
@ -56,16 +50,13 @@ typedef void *(mexFunctionPtr)(int nlhs, mxArray *plhs[], int nrhs, const mxArra
class DynamicModelDLL class DynamicModelDLL
{ {
private: private:
#if defined(_WIN32) || defined(__CYGWIN32__)
DynamicFn *Dynamic; // pointer to the Dynamic function in DLL
#else
DynamicFn Dynamic; // pointer to the Dynamic function in DLL DynamicFn Dynamic; // pointer to the Dynamic function in DLL
#endif
const int length; // tot num vars = Num of Jacobian rows const int length; // tot num vars = Num of Jacobian rows
const int jcols; // tot num var t-1, t and t+1 instances + exogs = Num of Jacobian columns const int jcols; // tot num var t-1, t and t+1 instances + exogs = Num of Jacobian columns
const int nMax_lag; // no of lags const int nMax_lag; // no of lags
const int nExog; // no of exogenous const int nExog; // no of exogenous
#if (defined _WIN32) || (defined __CYGWIN32__) #if defined(_WIN32) || defined(__CYGWIN32__)
HINSTANCE dynamicHinstance; // DLL instance pointer in Windows HINSTANCE dynamicHinstance; // DLL instance pointer in Windows
#else #else
void *dynamicHinstance; // and in Linux or Mac void *dynamicHinstance; // and in Linux or Mac