I am new to freeRTOS but have been writing embedded code for years, mostly with STM32 in recent times.
We want to use this RTOS for our next project due to the need for concurrent tasks and I was wondering if someone could help with my question.
Basically I want to create a repeating task that consumes API functions from a 3rd party library used to perform sensor measurements (its very memory hungry when running API functions).
This Library makes extensive use of dynamic memory allocation (Malloc) whilst it is operational.
Question - Is it possible to call 3rd party library API calls from within a task, where the functions dynamically allocate memory globally (outside of the task)?
If not, then I would need to reserve something like 20-30k RAM to a specific task, even if it will only be used when the library function is being utilised.
To add to what Gaurav said, if you are using MPU then even malloc-ed memory should only be used by one thread. If other thread tries to use the memory allocated by the first one, then it would lead to a fault.
But if you are not using MPU, it should be possible to call the APIs from within the thread. As long as the memory is allocated from the heap using malloc/free the task would not require a bigger stack. But if the APIs allocate memory on the stack (e.g. using things such as int arr[30000];) then the task stack needs to be bigger.
The developer mentioned that the library requires pointers to the appropriate Malloc() and free() functions during the init, and suggested we should use pvPortMalloc() and vPortFree().
If pvPortMalloc() is used, would other tasks have visibility of the memory allocation within that first task (is there cross-visibility of memory allocation between tasks)?
(In reality, most other tasks would use statically defined memory such as global arrays for logged data/settings structures/UART comms etc but some small locally declared arrays may also be used at times).
Which FreeRTOS port are you using? If you are using a FreeRTOS port without Memory Protection Unit (MPU) support, ever task runs as privileged and therefore, has access to everything. So a malicious task can both read and write any memory. What problem are you trying to solve by achieving isolation?
Hi,
Thanks for the response. I am not specifically trying to achieve isolation, I just need to understand how the PortMalloc() function will work in the context of the heap and adjacent task memory usage.
For example, in RTOS each task is allocated its heap memory during development. I was not sure whether this PortMalloc() function only reserved its memory from its reserved area, or from global memory. This is because the library which I will use from within one of the tasks may allocated 20-30K but then subsequently release it again when not used. I dont know if that released memory would then become available to other tasks, or only stay within the one task.
This is not correct. Each task has its own stack but heap is common to all tasks. FreeRTOS provides several heap implementations to choose from - FreeRTOS heap memory management - FreeRTOS™.
As I mentioned above, heap memory is shared among all the task and therefore, once the memory is freed, it is available to all the tasks.