1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| // MMFile.cpp : Defines the initialization routines for the DLL.
//
#include "MMFile.h"
#include "mex.h"
static MMFile * hObj = NULL;
static bool initFlag = true;
static mxArray * persistent_array_ptr = NULL;
static double * memory_holder = NULL;
// *******************************************************************
// MEX Interface.
// *******************************************************************
// MEX exit routine when MEX file is unloaded from process space.
extern "C"
void mexExitFunction(void) // This name needs to be exported?
{
// We need to put back pointer to memory that MATLAB allocated so that
// when it cleans up it does the correct thing.
mxSetPr(persistent_array_ptr,memory_holder);
mxSetN(persistent_array_ptr,1);
mxDestroyArray(persistent_array_ptr);
}
// *******************************************************************
// MEX Interface.
// *******************************************************************
// MATLAB MAIN entry point.
extern "C"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
mexPrintf("\tProblem 1.\n");
// *******************************************************************
// Register the MEX exit function first time thru mex file
// Setup a persistent mxArray that we will us for passing data
// *******************************************************************
if (initFlag)
{
mexPrintf("\tProblem 2.\n");
mexAtExit(mexExitFunction);
persistent_array_ptr = mxCreateDoubleMatrix(1,1,mxREAL);
mexMakeArrayPersistent(persistent_array_ptr);
memory_holder = mxGetPr(persistent_array_ptr);
initFlag = false;
}
// *******************************************************************
// Error checking
// *******************************************************************
// If the hObj does not exist then a problem caused us not to be
// able to create mem-map file and we need to tell the user.
if (hObj==NULL)
{
mexPrintf("\tProblem createing Memory mapped file.\n");
mexPrintf("\tClear memeory and try again.\n");
nlhs=0;
plhs[0]=NULL;
return;
}
// Error checking on user past variables
if(nrhs!=1)
{
if ((nlhs !=1) | (nrhs > 1))
{
mexPrintf("Usage:\n");
mexPrintf("\tmmfile(data);\n");
mexPrintf("\tdata=mmfile();\n");
nlhs=0;
plhs[0]=NULL;
return;
mexPrintf("\tProblem 3 nrhs>1.\n");
}
}
// *******************************************************************
// Actual work of the MEX file.
// *******************************************************************
if (nrhs==1)
{ // User wants to put data into memory map file
hObj->putData(mxGetPr(prhs[0]),mxGetNumberOfElements(prhs[0]));
mexPrintf("\tProblem 4 nrhs==1 putData.\n");
}
else // Or user wants to get data from memory mapped file.
{
mexPrintf("\tProblem 5 nrhs!=1 persistent.\n");
plhs[0]=persistent_array_ptr;
mexPrintf("\tProblem 6 nrhs!=1 persistent.\n");
// Notice that we pass back a pointer to the mem-map
// and we do not copy the data out of the point
mxSetPr(plhs[0],hObj->getPointer());
mexPrintf("\tProblem 7 GetPointer.\n");
// Tell the mxArray the size of the data in the mem-map
mxSetN(plhs[0],hObj->getLength());
mexPrintf("\tProblem 8 getLength.\n");
}
}
/* Main entry point for DLL loading and unloading */
BOOL WINAPI DllMain (
HANDLE hModule,
DWORD dwFunction,
LPVOID lpNot)
{
switch (dwFunction)
{
case DLL_PROCESS_ATTACH:
hObj = new MMFile();
break;
case DLL_PROCESS_DETACH:
delete hObj;
break;
default:
break;
}
return true;
} |
Partager