Use other memory allocation systems
Description
The current memory allocation relies on the C99 standard malloc
et al., which is not suitable for embed systems, such as FreeRTOS (see #599 (closed)), where the malloc
provided by the C library is not thread safe.
Analysis
This selection is made at compile time, not a run time.
Upon second analysis, the implementation of realloc
is problematic: under FreeRTOS, it is not implemented.
It may be implemented with a memory copy, but the current size of the buffer must be known.
The following options are under study:
use the private structure of thevPortMalloc
to find the current size of the buffer,- have a
SOPC_Realloc(void *p, size_t oldsize, size_t newsize)
, butoldsize
would not be used under Linux/Windows, hence uses ofSOPC_Realloc
would have to be thoroughly tested under FreeRTOS to avoid bugs, - avoid the use of
realloc
in S2OPC:-
realloc
is used by the expat parser which is not ported to FreeRTOS, - (
realloc
is not used by dictionaries, as elements are re-inserted after dictionary growth), -
realloc
is used inSOPC_Array
growth. In this case, it might be pre-allocated with a maximum size. Maximum size shall be passed at creation times. This impacts:- parsing references and value arrays in the address space loader with expat (not ported to FreeRTOS),
- number of dimensions when parsing a numeric range (used in read and write services): can be maxed with a constant,
- storing events for the internal queues of event: the maximum may reveal sensitive to choose,
- runtime variables: the number of these can be precomputed,
- subscription library: stores the configurations, can be changed to a fixed size array,
- tests, including SOPC_Array tests.
-
Fixes
Use SOPC_Malloc
, ..., to wrap or redefine memory allocation primitives.
Under Linux and Windows, these SOPC_Malloc
can be #define
d to the corresponding C library functions.
For realloc
, we chose the oldsize
variant, as it is simpler to implement.
It should be documented as "deprecated" to prevent its use in new parts of the code.
In the long term, a discussion should be opened to decide whether SOPC_Array
are removed or not.