How to properly define a complex externally defined input argument to entry point function in code generation?

3 visualizaciones (últimos 30 días)
I have a C project where some of the code is generated using Matlab Coder. One of the inputs to the entry point function for the generated code is a struct defined in a header file outside of the code generation. I have managed to tell Matlab Coder that it is an externally defined input, and can generate code.
My problem is that one of the fields in the struct is an array of another struct with undefined size:
typedef struct
{
...
uint16 sizeOfSample
AnotherDefinedStruct sample[]
} ExternallyDefinedStruct;
I have not found a way to make Matlab Coder understand this. Within the Matlab code, the struct is sent as an input argument to another .m-function, and since sample is an array of unknown size, it needs to be sent as a reference in the generated code:
functionCall(ExternallyDefinedStruct * measurement)
However, in the generated code it is passed by value, which makes for some weird results when the function then tries to read the values in the array.
I have tried to find a way to set the argument to Matlab Coder so that it understands that this is an array and cannot be passed by value, but so far I only end up with another type in the generated .._types.h file (which shouldn't be there at all since the struct is fully defined in my header file), and/or a function call that still passes by value.
My approach has been along these lines, with a lot of testing different ways to get it to be an array:
arg = struct;
...
arg.sizeOfSample = coder.typeof(uint16);
sample = struct;
arg.sample = coder.typeof(sample,[1,inf]);
arg.sample = coder.cstructname(arg.sample,'AnotherDefinedStruct','extern','Headerfile','path.h')
arg = coder.cstructname(arg,'ExterallyDefinedStruct','extern','Headerfile','path.h')
Is there a way to make Matlab Coder understand that this is not a fixed size array so that the struct gets passed by reference in internal function calls? Ideally I would like to simply provide the header file where the struct is defined to Matlab Coder and it figures it out from there, but I have not been able to find such an option.
(At first, I thought that I could use coder.ref() instead to indicate to Matlab Coder to pass argument by reference, but I found that this can only be used inside a coder.ceval() call, which this is not since it the call is to another .m-function.)

Respuesta aceptada

Denis Gurchenkov
Denis Gurchenkov el 30 de Sept. de 2022
Hi AB,
I don't see any way to accomplish this directly. MATLAB Coder just can't comprehend a notion of an open-ended struct, nor it has a notion of "this has to be passed by reference". I'll pass this to the development team as a request - can they come up with a way to support this feature.
One thing - generated code should be passing this struct by reference in two cases:
  1. Either the struct is not modified inside the called function, or
  2. The struct is always passed to the function in the form of "x = foo(x)", and the function signature also is "function p = foo(p)", that is, the struct is both an input and an output.
So I wonder if you could, as a work around, define this struct as if it has a fixed size field, and then use the #2 above to avoid copy by value? I know this is a hack but it could work?
  3 comentarios
AB
AB el 3 de Oct. de 2022
I have solved this now by forcing the code generation to inline the function, which also feels like a hack, but at least it works for now, and fortunately it is not too many calls to the function.
A future support to have more control over the code generation for externally defined inputs would be really appreciated, right now this has me questioning if code generation is the way to go for this project (though I have been very happy with it as it has been working great up until now).
Denis Gurchenkov
Denis Gurchenkov el 3 de Oct. de 2022
Hi AB, I'd like to learn more about your use case. Can you please send a PM so we can chat?
> This case as you describe is fulfilled, the code within the function never alters any value in the struct, so why is it not already passed by reference?
It should be passed by reference, indeed. I'd be happy to take a look at your code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by