When using MATLAB Coder to generate C code and there is a dynamic array, the foo_emxutil.c that is created is allocating memory as a power of 2?
Mostrar comentarios más antiguos
Using MATLAB Coder, the C code was generated for the following function.
function y = mainFunc(X,q)
x = single(X(1:2:q,1)) + 1i*single(X(2:2:q,1));
y = (x.')*x;
end
This function has a dynamic array x an in the process of generating the C code, the following main_emxutil.c is generated.
/*
* File: mainFunc_emxutil.c
*
* MATLAB Coder version : 5.3
* C/C++ source code generated on : 19-Dec-2023 16:20:39
*/
/* Include Files */
#include "mainFunc_emxutil.h"
#include "mainFunc_types.h"
#include <stdlib.h>
#include <string.h>
/* Function Definitions */
/*
* Arguments : emxArray_creal32_T_main_main *emxArray
* int oldNumel
* Return Type : void
*/
void emxEnsureCapacity_creal32_T_main(emxArray_creal32_T_main_main *emxArray,
int oldNumel)
{
int i;
int newNumel;
void *newData;
if (oldNumel < 0) {
oldNumel = 0;
}
newNumel = 1;
for (i = 0; i < emxArray->numDimensions; i++) {
newNumel *= emxArray->size[i];
}
if (newNumel > emxArray->allocatedSize) {
i = emxArray->allocatedSize;
if (i < 16) {
i = 16;
}
while (i < newNumel) {
if (i > 1073741823) {
i = MAX_int32_T;
} else {
i *= 2;
}
}
newData = calloc((unsigned int)i, sizeof(creal32_T));
if (emxArray->data != NULL) {
memcpy(newData, emxArray->data, sizeof(creal32_T) * oldNumel);
if (emxArray->canFreeData) {
free(emxArray->data);
}
}
emxArray->data = (creal32_T *)newData;
emxArray->allocatedSize = i;
emxArray->canFreeData = true;
}
}
/*
* Arguments : emxArray_creal32_T_main_main **pEmxArray
* Return Type : void
*/
void emxFree_creal32_T_main(emxArray_creal32_T_main_main **pEmxArray)
{
if (*pEmxArray != (emxArray_creal32_T_main_main *)NULL) {
if (((*pEmxArray)->data != (creal32_T *)NULL) &&
(*pEmxArray)->canFreeData) {
free((*pEmxArray)->data);
}
free((*pEmxArray)->size);
free(*pEmxArray);
*pEmxArray = (emxArray_creal32_T_main_main *)NULL;
}
}
/*
* Arguments : emxArray_creal32_T_main_main **pEmxArray
* Return Type : void
*/
void emxInit_creal32_T_main(emxArray_creal32_T_main_main **pEmxArray)
{
emxArray_creal32_T_main_main *emxArray;
*pEmxArray = (emxArray_creal32_T_main_main *)malloc(
sizeof(emxArray_creal32_T_main_main));
emxArray = *pEmxArray;
emxArray->data = (creal32_T *)NULL;
emxArray->numDimensions = 1;
emxArray->size = (int *)malloc(sizeof(int));
emxArray->allocatedSize = 0;
emxArray->canFreeData = true;
emxArray->size[0] = 0;
}
/*
* File trailer for mainFunc_emxutil.c
*
* [EOF]
*/
In the while loop, the allocated array size is increasing by a power of 2 (i *= 2). Is there way to avoid this such that the allocated size only increased by 1 (i++). I would like to avoid editing the generate C files. So if there is a MATLAB Coder setting or some other setting that I am missing that would help.
The following script was used to generate the C code.
%% Create configuration object of class 'coder.EmbeddedCodeConfig'.
cfg = coder.config('lib','ecoder',true);
cfg.CustomSymbolStrGlobalVar = '$M$N_main';
cfg.CustomSymbolStrType = '$M$N_main';
cfg.CustomSymbolStrEMXArray = 'emxArray_$M$N_main';
cfg.CustomSymbolStrEMXArrayFcn = 'emx$M$N_main';
cfg.FilePartitionMethod = 'SingleFile';
cfg.GenCodeOnly = true;
cfg.GenerateReport = true;
cfg.MATLABSourceComments = true;
cfg.MaxIdLength = 256;
cfg.PreserveVariableNames = 'UserNames';
cfg.ReportPotentialDifferences = false;
%% Define argument types for entry-point 'mainFunc'.
ARGS = cell(1,1);
ARGS{1} = cell(2,1);
ARGS{1}{1} = coder.typeof(int16(0),[40000000 1]);
ARGS{1}{2} = coder.typeof(int32(0));
%% Invoke MATLAB Coder.
codegen -config cfg mainFunc -args ARGS{1}
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!