Living dangerously with - or without - malloc()
Most software developers stick to the straight and narrow in their use of C’s malloc() library function - and its faithful companion free ()- in the constrained firmware environment of most embedded designs. But in this week’s Embedded.com Tech Focus Newsletter, Colin Walls in “Dynamic memory and heap contiguity“ provides some tips on how to live dangerously using dynamic allocation and do so with reasonable safety.
In his article, Colin does an excellent job in describing some new techniques that he’s convinced me are worth trying, especially if you are looking for a way out of the performance and deterministic straight jacket that dependence on malloc imposes.
Many current and future designs using 32 bit and multicore MCU implementations will require either a safer way to use dynamic allocation or some way to extend static allocation. Included in the newsletter are a number of design articles and blogs on various static and dynamic techniques to achieve this. In addition, a number of other articles worth checking out include:
Memory allocation in C
Mastering stack and heap for memory allocation reliability
Building a user space library for dynamic memory analysis
Tackling memory allocation in multicore/multithreaded apps
Colin and the other contributors to Embedded.com haven't been the only ones looking at ways to live dangerously. Here are a few papers I found on how to use dynamic allocation in a number of next generation designs:
A scalable lock-free dynamic memory allocator
Dynamic memory allocators for real-time embedded systems
Using Palloc for parallel dynamic memory allocation
Dynamic memory management for embedded real-time systems
Efficient dynamic heap allocation of scratchpad memory
As you read these various perspectives of the issue of dynamic versus static memory allocation, keep in mind the warning that Colin includes in his article:
“The word “dynamic” should – and normally does – ring alarm bells with embedded software developers,” he writes. “The dynamic allocation/creation of anything is fraught with problems in real time and deeply embedded systems, so, wherever possible, static alternatives should be considered.”
But, he adds, while dynamic memory allocation is a good example of where this caution is wise, a clear understanding of the issues involved can lead to implementations that are appropriate for your embedded design, without living too dangerously.
Embedded.com Site Editor Bernard Cole is also editor of the twice-a-week Embedded.com newsletters as well as a partner in the TechRite Associates editorial services consultancy. He welcomes your feedback. Send an email to firstname.lastname@example.org, or call 928-525-9087.