I tried to migrate a working project to mbed RTOS as it's complexity has reached a level that requires threads and synchronization. Unfortunately my application stopped working after importing mbed-rtos library. After investiging the problem I found out that malloc hangs at places where it wouldn't earlier.
Any hints on how to debug the problem? I searched for rtos specific memory handling issues, but could not find any.
I also made simple test cases to simplify the problem and I found that when there is not enough free memory then malloc freezes (causes hard fault) instead of returning null as it does without rtos. But that's not the only case when it hangs. Sometimes it even hangs when I call free to deallocate memory previously allocated with malloc.
This is a known issue we need to fix.
The standard library heap/stack collision algorithm is using "_current_sp()
" to verify that the stack pointer is always above the heap pointer.
This is working fine with the normal mbed Memory Model where there is a single stack positioned above the heap.
This stopped working with the RTOS Memory Model where many stacks are allocated in the zero initialized memory region below the heap.
The solution is "simple": we need to write a new heap/stack collision detection algorithm that will check that the heap pointer is below the main stack ponter
, not the _current_sp()
.
For the moment, lacking time (I am currently completing the work on the new networking libraries), I disabled this check. Therefore an application, instead of failing immediately at startup, will fail only when all the memory has been exhausted.
Is it possible to write my own heap management functions for mbed-rtos similar to FreeRTOS?
Yes, it is possible and in fact this is what we are going to do in the following weeks.
If you need a solution sooner, you can take a look at the standard library documentation: Tailoring the runtime memory model.
HTH,
Emilio
I tried to migrate a working project to mbed RTOS as it's complexity has reached a level that requires threads and synchronization. Unfortunately my application stopped working after importing mbed-rtos library. After investiging the problem I found out that malloc hangs at places where it wouldn't earlier.
Any hints on how to debug the problem? I searched for rtos specific memory handling issues, but could not find any.
I also made simple test cases to simplify the problem and I found that when there is not enough free memory then malloc freezes (causes hard fault) instead of returning null as it does without rtos. But that's not the only case when it hangs. Sometimes it even hangs when I call free to deallocate memory previously allocated with malloc.
Is it possible to write my own heap management functions for mbed-rtos similar to FreeRTOS?