Where does things get allocated with pvPortMalloc()?

Where does things get allocated with pvPortMalloc()?

Looking at this picture from the FreeRTOS documentation:

what does it mean that “Stack” is allocated on the Heap?

It simply means that in xTaskCreate the memory required for the task stack is allocated taken from FreeRTOS heap as documented and if configured i.e. configSUPPORT_DYNAMIC_ALLOCATION is enabled.
Besides looking up the well documented FreeRTOS sources there is a lot of good documentation e.g. FreeRTOS Kernel Developer Docs recommended to read.
See the chapters Static Vs Dynamic Memory Allocation and Memory Management there.

Ok, but stack and heap are two different things, why is it called ‘task stack’ when the task is allocated on the heap?

Ah, now I understand your confusion! The stack space of all tasks can be allocated on the heap. xTaskCreate() puts the TCB on the heap, and it also reserves a private tack space for that task on the heap. That space will be freed when the task has terminated.
But note that heap_1 does not implement a vPortFree() method, so once allocated is for always.


You can also use xTaskCreateStatic() to start a task. You must provide static memory ( in .data ) for both the task’s TCB and its stack.

So yes, heap space can become stack, but a stack will rarely serve as a heap space :slight_smile:

Thanks for clarifying.
So, it’s not really the actual ‘stack’, it’s all managed on the heap, but kind of ‘emulates’ the stack? Is that correct?

And, what’s saved on the stack part of the heap for a task?
Is it just like for a normal function, i.e. local variables, function return addresses etc?

Each task has it’s own stack. Memory is needed for this stack. This memory block can be automatically allocated from a heap when using xTaskCreate (see the previous post) or can be provided by the caller when using xTaskCreateStatic if configSUPPORT_STATIC_ALLOCATION is enabled.
A task doesn’t get its own heap. A FreeRTOS task is not a process maybe known from other bigger OSs like Linux or Windows. It’s like a thread with a normal stack.

Yes, that is correct. And this “emulated” stack exists for every task.

Yes, that is correct too.


Thank you for the clarification :slight_smile: