Determine heap free space on STM32
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 1:17 PM
I'm using an STM32L4 with STM32CubeIDE, bare metal, and using C++ along with Cube's C libraries. The compiler is the GCC/G++ supplied with STM32CubeIDE.
I have some dynamic memory usage in STL containers etc. I'm aware of the recommendation not to use dynamic memory on bare-metal embedded systems, but it is generally used only during initialization, minimizing the complications.
I would like to know how much of the heap I'm using, and found no way to do that. I saw discussions of __heapstats but it doesn't seem to be defined in this toolchain (?).
It doesn't have to be a programmatic way, if there is some internal data structure of newlib or whatever that I can watch while debugging that will suffice.
p.s.
I know the term "free heap space" is not very well defined, because of block sizes and fragmentation, but I suspect that in my case it's going to be one big free block.
any ideas?
Solved! Go to Solution.
- Labels:
-
STM32CubeIDE
-
STM32L4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-17 5:34 AM
Hello @tinkerer42​
The most generic way to detect stack/heap collisions during run-time would be to by inserting a "known pattern" inside the RAM-memory.
If this memory is completely overwritten then you will know that the heap and stack has collided...
I think there should be some previous ticket where we advised how customer how he can re-write the Reset_Handler to fill the unused RAM with a known pattern.
Have a look at this extension to startup code:
LoopFillZerobss:
ldr r3, = _ebss
cmp r2, r3
bcc FillZerobss
/* ============================ */
/* Fill space between Heap and stack with magic pattern */
ldr r2, =_ebss
mov r4, sp
movs r3, #0xa5a5a5a5 /* The magic pattern */
b LoopFillPattern
FillPattern:
str r3, [r2]
adds r2, r2, #4
LoopFillPattern:
cmp r2, r4
bcc FillPattern
/* Call static constructors */
bl __libc_init_array
Kind regards,
Semer.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 1:21 PM
Walk the linked list the allocator uses..
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 1:26 PM
thanks, not pretty but I'll do it if there's no built-in lib to do it for me. I'd like to ask then -
- what's the name of the data structure?
- Is it the same structure both for C's malloc() and C++'s new() (or equivalent operations)?
the compiler is the GCC / G++ supplied with STM32CubeIDE, by the way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 1:36 PM
The question looks more expansive now.
I'm using Keil.
__microlib_freelist_initialised
__microlib_freelist
The structure is ahead of the pointer returned by malloc()
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 2:04 PM
Thanks, can anyone provide a similar answer for STM32CubeIDE's toolchain?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 2:42 PM
I'd have thought you could objcopy or fromelf the symbol and disassembly/listing of the code.
https://community.st.com/s/question/0D53W00000e0zTdSAI/malloc1
https://code.woboq.org/userspace/glibc/malloc/malloc.c.html
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 3:49 PM
Why not union your variables with main global ones if they are exclusied in sequence, saving mallocs logic? (Manual clearing.maybe needed)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 5:03 PM
Dynamic allocation has its place, especially allocating buffers and objects at start-up, and where the usage might be configurable, or transitory
Now obviously using it like you would on a desktop, with no regard to leaks, or balance, that's a problem. Using it continuously is also not particularly advisable.
Then again using globals in an excessive and sloppy manner is also something to be avoided.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 7:04 PM
the short answer is that it leads me to cleaner code: using STL containers and other C++ features. The longer answer is that it has advantages and disadvantages, it is a common heated arguing point in the embedded community. I would really prefer NOT to open this discussion here, as it would completely distract from my original question.
So: does anyone know how to check heap free space when using the STM32CubeIDE toolchain?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-15 7:06 PM
What would I do with looking at the ELF etc? as they are the results of compilation and therefore know nothing about the runtime state of the heap?
