1

currently I am implementing legacy C Code into C MEX S-functions at MATLAB/Simulink (R2017b). I have already put a duplicate entry on Entry at MathWorks-MATLAB Answers. The legacy functions take as a general rule pointer to structures involving pointer to further structures, example legacy code:

func.h:

typedef struct {
  double*       param;
}T_DATA_1;

typedef struct {
  double        in;
  double        out;
  T_DATA_1*     pParam;
}T_DATA_2;

func.c:

void func(T_DATA_2 *pData){
  pData->out = pData->in + *(pData->pParam->param);
}

I already know that I can wrap manually, e.g., the input bus signal to the legacy structure in the C S-function just like in the following code segment:

void wrap_BussignalToLegacy(T_DATA_2 *pLegacy, T_DATA_2__BUSOBJ *pInput){
    pLegacy->in                  = pInput->in;
    pLegacy->out                 = pInput->out;
    *(pLegacy->pParam->param)    = pInput->pParam.param;
}

with an appropriate structure for the bus signal removing all pointer declarations.

typedef struct {
      double       param;
}T_DATA_1__BUSOBJ;

typedef struct {
      double        in;
      double        out;
      T_DATA_1      pParam;
}T_DATA_2__BUSOBJ;

Of course I have to write a similiar function to wrap the legacy structure back to the output bus signal.

Using this method manually just works fine but for the case that structures are becoming larger with increasing amount of pointers to further structures, it will take a tremendous time to write appropriate signal wrappers.

One idea involves writing a MATLAB parser which analyzes the legacy structures and generates C Code which is called in the C S-functions. Does anyone already know that such a parser and code generator exists?

The workaround to iterate over all structure fields via pointer arithmetic will fail because memory size is not known beforehand. As an alternative to iterations, X-Macros can be used but would still depend on a parser and code generator.

The stackoverflow entry Share data between multiple c mex s-functions gives a good overview of two approaches but lacks on code generation which will be needed in future. Does anyone know further workarounds to handle legacy pointer structures including pointer to further structures?

Thanks for any reply and help!

0 Answers0