Tool/software: TI C/C++ Compiler
I'm looking through the malloc() code path in the support library for TI's ARM Optimizing C/C++ Compiler.
One path I was researching is how malloc() behaves when the system has been compiled with no heap allocated (i.e. specify --heap_size=0 to the linker).
As documented in both the TI assembler and TI compiler documentation, the TI linker:
The linker also creates a global symbol, __SYSMEM_SIZE, and assigns it a value equal to the size of the heap in bytes.
Looking through the code, specifically the minit() code within memory.c, it is clear that the code is not capable of dealing with a __SYSMEM_SIZE of 0. If __SYSMEM_SIZE were 0, then calling malloc() would corrupt memory.
As it happens, the compilation chain _is_ attempting to handle this case, however. It appears that the linker enforces a minimum size of 8. If you specify --heap_size=0 (or, presumably, --heap_size=7), the TI linker will actually generate a .sysmem section of 8 bytes, and set __SYSMEM_SIZE to 8.
I would, and am, argue that this approach of implementing part of the C library functionality within the linker is not a clean separation of responsibility.
I separately argue that putting part of this functionality within the linker hides functionality; it is very valuable to us to be able to look at the C library source, but the linker is more opaque.
Finally, I argue that the behavior is confusing. I would have expected to be able to write my own code that could check __SYSMEM_SIZE against 0 to determine whether or not there is any heap compiled into the firmware. Having to write this check instead against a value of 8 (or potentially some other architecture-dependent value) would be a surprising result.
I would like to request that the memory.c code be updated to explicitly handle a __SYSMEM_SIZE of 0, and the linker updated accordingly.
In the event that this request is not approved, I would like to request that this linker behavior be documented accordingly, potentially in the minit() source code itself as well as in the TI manuals.
--thx