Stack Memory Defragmentation – heap_2.c

Are there any workarounds concering the problem of the Memory Defragmentation of the heap  ? My idea is, during vPortFree() look at the following block and the previous block in the list of free blocks. If any or both blocks are free, it should be possible to combine the free blocks to one common free block. With this solution, the defragmentation should be avoided ? Any comments ? Klaus

Stack Memory Defragmentation – heap_2.c

It is common for heap managers to combine adjacent free blocks. This reduces but does not remove fragmentation problems. It generally also makes the functions slower and less deterministic.

Stack Memory Defragmentation – heap_2.c

Are there any add-ons for FreeRTOS for such a heap manager ? Klaus

Stack Memory Defragmentation – heap_2.c

It mainly depend on your development environment. For example, if you use GCC and newlib, you could use its malloc() and free() implementation, that already take care of that.

Stack Memory Defragmentation – heap_2.c

If the defragmenting would be done by something optional in the idle task, the speed of the operation shouldn’t be a concern, unless a significant amount of time is spent inside critical sections. To the person who suggested malloc/free: how useful is that really for embedded systems? malloc just seems a bit too general purpose for me with no way of configuring it, i.e. total heap size, minimum/maximum block size, where is any housekeeping done, what are worst and best case conditions, etc. I actually wound up writing my own heap manager because I wanted something handle based that would allow blocks to be relocated and reference counted.

Stack Memory Defragmentation – heap_2.c

Thanks for the replies.  I think, the solution with the defragmentation in the idle task is the best solution. Is a source code for this solution available ?