I am in the process of porting my first 3rd party IP to AWE. I am wondering the preferred method for allocating memory for structures defined in a separate h file.
My understanding is:
awe_addcodemarker(M, 'hFileInclude', '#include <my_3rdPartyModule.h>'); % To add the header file for the third party IP which includes some structure definitions. e.g.
typedef struct StructureInstance
{
int32 var1; // some variable
float var2; // other variable.
otherStructureType var3; // some vars might not be basic data types.
} S_StructureInstance_t;
Then there are several options for including variables in the AWE module.
add_variable (M, ...) %limited to 'float', 'fract32', 'int', and 'uint', therefore does not work with the structure type (I assume because of offsets).
add_pointer (M, ...) % Can assign S_StructureInstance_t as the type but does not allocate any memory. From the documentation "Use pointers, for example, to point a data structure used by a third party algorithm"?
or...
add_array(M, 'algorithm', ...) %This allocates the correct amount of memory but of type 'float', 'fract32', 'int', and 'uint' (although it is possible to cast to (S_StructureInstance_t *) when needed).
M.algorithm.arrayHeap = 'AWE_HEAP_FAST2SLOW';
M.algorithm.arraySizeConstructor = 'sizeof(S_StructureInstance_t)';
What is the preferred way to allocate memory in this use case?
If using the add_pointer(), where and when should the memory for the structure be allocated? The documentation implies that this should be the correct approach but obviously the memory still needs to be assigned at some point.
If using add_array() is it ok to just cast it to (S_StructureInstance_t *) when needed? If it was a void pointer, I would assume this would be perfectly fine (as is done in the process function for the awe_modMyModuleInstance), but because the variable type is limited to the 4 data types, is there a special reason this shouldn't be used?
Is there a completely different approach that should be used that I am missing completely?
Many thanks,
2:26pm
Hi MCBizzle,
You are on the right track with add_pointer(). As mentioned in the documentation's section on add_pointer.m (https://documentation.dspconcepts.com/space/DOCHUB/2748822177/MATLAB+Fun...), "you must separately allocate memory for the pointer using custom code in the module’s constructor function."
Thanks,
Michael
5:42am
Thank you Michael.
That helped me in the right direction.
For anyone else with a similar issue,
I got it working by overwriting the constructor function with:
awe_addcodemarker(M, 'constructorFunction', 'Insert:\InnerMyModule_Constructor.c');
Adding a pointer to the structure I wanted to create.
add_pointer(M, 'pMyStructure', 'S_StructureInstance_t*', 'state', 'a custom structure defined in my_3rdPartyModule.h, 0)
And then within the new constructor, assign memory where required in the same style as with the auto-generated functions, e.g.
if ((S->pMyStructure = (S_StructureInstance_t*) awe_fwMalloc(sizeof(S_StructureInstance_t), AWE_HEAP_FAST2SLOW, retVal)) == 0)
{
// Error code is in *retVal
return 0;
}