Open side-bar Menu
 Embedded Software
Colin Walls
Colin Walls
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor … More »

Dynamic memory in real time systems – a solution?

 
October 16th, 2019 by Colin Walls

In C code, variables can be declared in a couple of ways. They might be static, so they are allocated a specific address in memory; this is the case for variables defined outside of functions or inside a function and qualified with the keyword static. Alternatively, a variable might be automatic, which means that it is allocated space on the stack or in a register for the duration of its scope [or, more precisely, its lifetime]. Another possibility is to dynamically allocate memory using the standard library functions …

In broad terms, I would say that dynamic allocation of memory in a real time system is a very bad idea. This is for two key reasons, both associated with the standard allocation function malloc(). First, an allocation call is non-deterministic – never good news when predictability of performance is a key requirement. Second, an allocation request may fail in a somewhat unpredictable manner. This is because of heap fragmentation – there is plenty of free memory available, but no contiguous block is large enough for a given request. In this example, there is 6K of free memory, but a request for a block larger than 3K will fail.

Of course, there is a solution. Just about every real time operating system [RTOS] around provides facilities for the deterministic allocation of fixed size memory blocks. You simply set up a “pool” of blocks during initialization and request and release blocks as required. The allocation time is predictable and the failure mode well defined and controlled. Of course, fixed size blocks sound restricting, but that is not the case for many types of application. If you really want to have something that looks like malloc(), you can easily write one. All that is needed is a set of memory pools with exponentially increasing block size. The new allocation function simply requests a large enough block from the appropriate pool.

I have written about this, in more detail, in a white paper.

Logged in as . Log out »




© 2024 Internet Business Systems, Inc.
670 Aberdeen Way, Milpitas, CA 95035
+1 (408) 882-6554 — Contact Us, or visit our other sites:
TechJobsCafe - Technical Jobs and Resumes EDACafe - Electronic Design Automation GISCafe - Geographical Information Services  MCADCafe - Mechanical Design and Engineering ShareCG - Share Computer Graphic (CG) Animation, 3D Art and 3D Models
  Privacy PolicyAdvertise