# Create heap

**URL:** <https://forums.freertos.org/t/create-heap/16828>\
**Category:** Kernel\
**Created:** [February 24, 2023, 3:31pm UTC](https://forums.freertos.org/t/create-heap/16828 "2023-02-24T15:31:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [February 24, 2023, 3:31pm UTC](https://forums.freertos.org/t/create-heap/16828/1 "2023-02-24T15:31:19Z")

</div>

Hi guys,

I would like to create a heap structure independent of the one used by FreeRTOS. I need it for dynamic memory allocation and I also need to be able to specify the memory location and size of the heap. I would like to take advantage of the heap management code in FreeRTOS.

In other words, I like to create it like I would a queue, task, or any other structure in FreeRTOS.

Please advise,

Johnas

---

<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:** [February 24, 2023, 5:48pm UTC](https://forums.freertos.org/t/create-heap/16828/2 "2023-02-24T17:48:35Z")

</div>

FreeRTOS only calls pvPortMalloc() and vPortFree(). Assuming their implementation doesn’t just map to malloc() and free() (which would be the case if you use [heap\_3.c](https://www.freertos.org/a00111.html#heap_3)) then you are free to implement malloc() and free() to behave in anyway that suites your needs.

---

<div class="post-metadata">

**Author:** ![andrew-elder](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/andrew-elder/32/3182_2.png) [@andrew-elder](https://forums.freertos.org/u/andrew-elder)\
**Post date:** [February 24, 2023, 7:04pm UTC](https://forums.freertos.org/t/create-heap/16828/3 "2023-02-24T19:04:30Z")

</div>

If it were me I would check out [https://gee.cs.oswego.edu/dl/html/malloc.html](https://gee.cs.oswego.edu/dl/html/malloc.html)

---

<div class="post-metadata">

**Author:** ![skptak](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/skptak/32/3981_2.png) [@skptak](https://forums.freertos.org/u/skptak)\
**Post date:** [March 1, 2023, 8:24pm UTC](https://forums.freertos.org/t/create-heap/16828/4 "2023-03-01T20:24:56Z")

</div>

Hey @beethoven62, hope you’re doing well. I wanted to reach out and see if you had any other questions related to this topic, or if you’ve figured out how to achieve your end goal?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [March 1, 2023, 10:29pm UTC](https://forums.freertos.org/t/create-heap/16828/5 "2023-03-01T22:29:17Z")

</div>

Hi skptak,

I did resolve my issue. Unfortunately, I had to go to a solution outside of FreeRTOS. This seems like a duplication of effort. FreeRTOS already has multiple heap managers. However, it is somewhat restrictive in its use. It seems targeted for OS use only and not for general purposes. I think a simple tweak to allow a memory block of the user’s choosing, would make the FreeRTOS heap managers much more useful. In other words, make it available to the user just like the task, queue, semaphore, etc. mechanisms are.

Johnas

---

<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:** [March 2, 2023, 2:22am UTC](https://forums.freertos.org/t/create-heap/16828/6 "2023-03-02T02:22:35Z")

</div>

There is no problem with user code calling pvPortMalloc() or vPortFree(), so you can share the heap.

If you want a separate heap, just take the files for the type of heap you want, make a copy and rename the functions to something different.

That is one reason they are provided as source.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 2, 2023, 7:35am UTC](https://forums.freertos.org/t/create-heap/16828/7 "2023-03-02T07:35:57Z")

</div>

> [@beethoven62](#):
>
> I think a simple tweak to allow a memory block of the user’s choosing, would make the FreeRTOS heap managers much more useful.

Do you want a separate memory area to be used as heap for the application? Or do you want to use more than one memory blocks for heap which can be shared by kernel and application?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [March 3, 2023, 10:15pm UTC](https://forums.freertos.org/t/create-heap/16828/8 "2023-03-03T22:15:19Z")

</div>

I need a completely separate memory block for application use purposes only. I’ve found a number of dynamic memory managers, but I’d rather use the FreeRTOS implementation. Your colleague’s answer in which I would need to copy and paste the source code of the FreeRTOS heap manager and tweak it for my needs is unacceptable. I want a single point of reference in terms of code. In other words, there should be one copy of the heap manager code that can be instantiated for multiple purposes including use by the kernel. If a bug was fixed or a new feature added to your heap manager, I would need to manually copy that to my tweaked copy. That is a headache and is asking for trouble in terms of bugs in the code.

Just so you understand what I need this for, I’m using the nRF9160 which is a dual core ARM Cortex M33. The first core is the application. The second core is an LTE modem. The two cores communicate via an IPC interface and shared memory. One of the memory blocks needs to have a dynamic memory manager that allocates/deallocates memory as data is being transmitted to the modem core. This memory block may not be accessed by the kernel. It is for LTE modem transactions only.

There is a secondary restriction in that this block of memory must be located in the lower part of RAM. So, I would need to specify the location of the block and its length.

I’ve looked at the heap managers provided by FreeRTOS and you use a variable ucHeap to specify the memory block that the heap manager works with. I think it would be very advantageous if this can be provided through an API to the heap manager as well as to accommodate multiple memory blocks to be managed by the same heap management code.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [March 3, 2023, 10:54pm UTC](https://forums.freertos.org/t/create-heap/16828/9 "2023-03-03T22:54:17Z")

</div>

Is the separate [NRF fixed block allocator](https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v11.0.0%2Fgroup __mem__ manager.html) an appropriate alternative ?  
Or make use of the preprocessor and `#define` (override or tag) all relevant symbols (ucHeap, public functions) with appropriate names and e.g.  
`#include “heap_4.c”` in a separate my\_heap.c file to create your dedicated heap with dedicated API using the provided memory manager code.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 4, 2023, 12:13pm UTC](https://forums.freertos.org/t/create-heap/16828/10 "2023-03-04T12:13:00Z")

</div>

Thank you for the detailed explanation.

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [March 4, 2023, 1:33pm UTC](https://forums.freertos.org/t/create-heap/16828/11 "2023-03-04T13:33:19Z")

</div>

Hi Hartmut,

Thank you for the link. The Nordic SDK you’ve pointed me to has a lot of goodies including a dynamic memory manager. If I don’t obtain a good solution to use the FreeRTOS heap manager, I will use the Nordic SDK.

As I’ve said, I’ve seen many other memory managers available out there. I would prefer to use the FreeRTOS heap manager as it would be one code base instead of multiple code bases. By the way, Zephyr make this available in their OS (just a point of information). At least the Nordic SDK is a source I have to pull from for other code used in the nRF9160. So, this would be a good second alternative. I can also pull in updates and bug fixes that Nordic might make to the SDK.

Again thanks,

Johnas

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [March 4, 2023, 3:06pm UTC](https://forums.freertos.org/t/create-heap/16828/12 "2023-03-04T15:06:05Z")

</div>

I clearly see your point. And the FreeRTOS memory managers like heap\_4/5 are pretty good. but as you’ve recognized the current implementations don’t support having multiple heap instances. But I think (didn’t try) you could make use of them (single source) with the preprocessor trick mentioned.  
in e.g. my\_heap.c add something like this:

```auto
#define ucHeap ucMyHeap 
#define pvPortMalloc pvMyMalloc
#define pvPortFree pvMyMallocFree
#include “(…/)heap_4.c”

```

you should get your own MyHeap to use separately.  
Good luck 👍

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [March 4, 2023, 3:31pm UTC](https://forums.freertos.org/t/create-heap/16828/13 "2023-03-04T15:31:15Z")

</div>

I’ll investigate your method. It looks promising. In my opinion, an update to FreeRTOS to support what I’m asking for would be a nice new feature.

Thanks
