Memory Manager For Embedded Systems

Contents

Introduction

One of the first things you realize when programming small embedded systems is that you need to be able to trust your run time library. Over the years, I’ve collected lots of routines that have stood the test of time.

I’ve also had to write my own routines when the ones I could find were not compact enough, or somehow did not quite fit the application. Sometimes I even write code when I think it might be fun.

Yes, I thought writing a memory manager might be fun. But I soon found out that the saying “I know fun, and this is not it” applies to this kind of code. In spades.

After researching a number of other memory managers, I ended up using Joerg Wunsch’s allocator. But the performance of the free operation bugged me, and I felt I could do better. A large number of hours later, I have code that I think is pretty good.

I have some confidence in the code because I wrote a test suite that’s included at the end of the file. It’s not compiled when you build this code for use in a library. By using the test harness I was able to find and track down many bugs.

I’ve used this memory manager for my pbLua project. It’s a port of the Lua programming language to the LEGO MINDSTORMS NXT – an AT91SAM7 based device with 256K of FLASH and 64K of RAM.

The code is reproduced at the end of the webpage. Feel free to cut it out and use it in your own application.

Resources

Joerg Wunsch and the avr-libc provided the first malloc() implementation that I examined in detail.

Doug Lea’s paper on malloc() was another excellent reference and provides a lot of detail on advanced memory management techniques such as binning.

LICENSE.TXT

umm_malloc.h

dbglog.h

umm_malloc.c