# Tasks: Management of custom dynamic memory

**URL:** <https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664>\
**Category:** Kernel\
**Created:** [June 16, 2023, 11:10am UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664 "2023-06-16T11:10:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESP\_0xjakob](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ESP\_0xjakob](https://forums.freertos.org/u/ESP_0xjakob)\
**Post date:** [June 16, 2023, 11:10am UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/1 "2023-06-16T11:10:29Z")

</div>

Hi,

I’m looking for a way to pass custom allocated dynamic stack memory to a FreeRTOS task, like in `xTaskCreateStatic()`, but it should be deleted automatically in `prvDeleteTCB()`.

The reason we need this is that we keep getting requests for pthreads with stack memory on external RAM in the ESP-IDF pthread implementation. ESP-IDF’s pthread library, which is basically implemented on top of FreeRTOS, similar to FreeRTOS-Plus-POSIX. `xTaskCreate()` uses `pvPortMallocStack()`, which is hard to customize for this purpose since it only takes the size as argument.

I already considered following approaches:

- using `xTaskCreateStatic()` and `portCLEAN_UP_TCB()`: This does not work due to the implementation of `prvDeleteTCB()`, which still accesses the TCB after calling `portCLEAN_UP_TCB()`
- using `xTaskCreateRestricted()` and `portCLEAN_UP_TCB()`: This might work, but according to the documentation, `xTaskCreateRestricted()` should only be used “in systems that include an MPU implementation”
- Using `xTaskCreateStatic()` and `portCLEAN_UP_TCB()` and Changing `prvDeleteTCB()` to copy relevant members of the TCB to local variables so both stack and TCB can be deleted by `portCLEAN_UP_TCB()`: This would involve changes in FreeRTOS itself and we would not like to divert if we are not sure if the change can be upstreamed.
- Possibly making `ucStaticallyAllocated` of the TCB more general and add another task create function, but that would be a substantial change, I guess. Similar problem as above.

Is there any other possibility to achieve the goal of having custom dynamic memory or did I miss something? If not, what would be the best of the solutions provided above?

Any links have been omitted because I’m a new user

Thanks!

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 16, 2023, 7:03pm UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/2 "2023-06-16T19:03:00Z")

</div>

If the allocator is only for the stack, can you provide your own implementation of xPortMallocStack() and vPortFreeStack()? These default to xPortMalloc() and vPortFree() if configSTACK\_ALLOCATION\_FROM\_SEPARATE\_HEAP is undefined or defined as 0, and are only used to allocate and free the stack if configSTACK\_ALLOCATION\_FROM\_SEPARATE\_HEAP is defined as 1.

Relevant references:  
[https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/tasks.c#L763](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/tasks.c#L763)  
[https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/tasks.c#L3976](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/tasks.c#L3976)  
[https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/include/portable.h#L183](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/include/portable.h#L183)

---

<div class="post-metadata">

**Author:** ![ESP\_0xjakob](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ESP\_0xjakob](https://forums.freertos.org/u/ESP_0xjakob)\
**Post date:** [June 26, 2023, 9:53am UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/3 "2023-06-26T09:53:51Z")

</div>

Hello, thanks for the answer! We haven’t upgraded our FreeRTOS to a version including `xPortMallocStack()`, so I missed these. They are probably closest to what we need so far. But the problem we face is that we want to be able to selectively place some of the created FreeRTOS task stacks in a different memory region, the rest needs to stay in the “default” region. Hence, we need a way to signal this to `xPortMallocStack()`, which is impossible at the moment because it has only one argument: the requested size of the stack. There are probably ways around this using any kind of global context information, but this will become really tricky if a higher priority task is preemting the original task and also tries to create a task.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 26, 2023, 11:31am UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/4 "2023-06-26T11:31:44Z")

</div>

Perhaps the best option for custom stack allocation is to create the task with xTaskCreateStatic (and enable static memory allocation). Then you can just create the stacks in the memory region you want.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 26, 2023, 4:52pm UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/5 "2023-06-26T16:52:52Z")

</div>

Would adding a second macro, something along the lines of portCLEAN\_UP\_MEMORY(), somewhere around [here help](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.5.1/tasks.c#L3991)? If so feel free to create a PR to do that - although we would have to agree on the new macro’s name. The “port” on portCLEAN\_UP\_TCB means it is in the port layer (I think this was added for TriCore targets that aren’t stack based), which in turn means the port in use always calls that macro when the containing function gets called. You’re use case sounds more selective, so it might be a “config” macro.

---

<div class="post-metadata">

**Author:** ![ESP\_0xjakob](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ESP\_0xjakob](https://forums.freertos.org/u/ESP_0xjakob)\
**Post date:** [June 29, 2023, 1:45am UTC](https://forums.freertos.org/t/tasks-management-of-custom-dynamic-memory/17664/6 "2023-06-29T01:45:06Z")

</div>

@rtel

> Would adding a second macro, something along the lines of portCLEAN\_UP\_MEMORY()

I’m try the addition of a macro right now. I would like to test it first to be 100% sure that this works for all our cases before creating a PR.

@richard-damon

> Perhaps the best option for custom stack allocation is to create the task with xTaskCreateStatic (and enable static memory allocation). Then you can just create the stacks in the memory region you want.

This works in some cases. However, the problem is that we would like to have a mechanism to automatically free memory once tasks are deleted. This is important for higher-level APIs such as pthread, which in turn might be used by other libraries not written with this special kind of memory management in mind.

Without any modification to the kernel, I currently don’t see any possibility for notification that the corresponding stack memory of a task can be free-ed. Unless I missed something, in which case I’d be happy to hear alternatives.
