# malloc to separate memory regions

**URL:** <https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164>\
**Category:** Kernel\
**Created:** [September 16, 2016, 5:39pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164 "2016-09-16T17:39:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 16, 2016, 5:39pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/1 "2016-09-16T17:39:04Z")

</div>

**[logicanalyzer](https://sourceforge.net/u/logicanalyzer/)** wrote on [Friday, September 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#f9a9):

I have different classes of memory, (faster, slower, nearby, farther away) and I would like to control where malloc allocates memory. The heap\_5 scheme allows the heap to span multiple non-contiguous memory regions but treats them as a single heap.

I need separate heaps and a way to control which one malloc allocates from. Is there a straightforward way to do this, or do I need to write heap\_6.c?

---

<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:** [September 16, 2016, 6:20pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/2 "2016-09-16T18:20:34Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, September 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#c823):

I think there might be a change request on this topic already. Basically it is not a straight forward change because it would need a new parameter to the pvPortMalloc() function, and that would have lots of backward compatibility issues. However, you can have more than one memory allocator in your application - so the FreeRTOS code would call the ‘standard’ pvPortMalloc(), and your application can call whichever memory allocation function is most appropriate for the memory being allocated.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 16, 2016, 6:51pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/3 "2016-09-16T18:51:45Z")

</div>

**[dumarjo](https://sourceforge.net/u/dumarjo/)** wrote on [Friday, September 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#c823/9442):

Hi,

This is how we do it.

Jonathan

Le 16/09/2016 à 14:20, Real Time Engineers ltd. a écrit :

> I think there might be a change request on this topic already.  
> Basically it is not a straight forward change because it would need a  
> new parameter to the pvPortMalloc() function, and that would have lots  
> of backward compatibility issues. However, you can have more than one  
> memory allocator in your application - so the FreeRTOS code would call  
> the ‘standard’ pvPortMalloc(), and your application can call whichever  
> memory allocation function is most appropriate for the memory being  
> allocated.
> 
> * * *
> 
> malloc to separate memory regions  
> [https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/?limit=25#c823](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/?limit=25#c823)
> 
> * * *
> 
> Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/p/freertos/discussion/382005/)
> 
> To unsubscribe from further messages, please visit  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/auth/subscriptions/)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 19, 2018, 6:36pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/4 "2018-05-19T18:36:36Z")

</div>

**[elmood](https://sourceforge.net/u/elmood/)** wrote on [Saturday, May 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#6382):

Has there been any change in the way memory management works since this thread was started? I’m new to FreeRTOS and in reading the docs it states: _"Using two heap implementations simultaneously permits task stacks and other RTOS objects to be placed in fast internal RAM, and application data to be placed in slower external RAM. "_ but then later (regarding the five heap implementations) it states: _“Exactly one of these source files should be included in a project at a time”_

That seems a bit contradictory. I guess this raises two different questions:

- Can you define which heap region will be used when making a call to pvPortMalloc()? I have SRAM and SDRAM (slower) which I’d like to use for different purposes as the example suggests.

- Can you actually use two implementations as the first quote suggests? This seems like less of a concern but I’m still confused by the wording.

An actual example, or more clarification on the documentation page would be helpful. Overall the docs are very good, but I want to make sure I understand this before jumping into my project.

---

<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:** [May 19, 2018, 7:53pm UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/5 "2018-05-19T19:53:43Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Saturday, May 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#1669):

You can edit the name of the functions in one of the version of the heap files, and then you can include both. That is one of the big advantages of it being provided in source format.

---

<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:** [May 20, 2018, 1:03am UTC](https://forums.freertos.org/t/malloc-to-separate-memory-regions/6164/6 "2018-05-20T01:03:05Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, May 20, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65d41896/#1669/1b5d):

Yes - and I suspect the quoted text is basically saying this in that  
RTOS code (internally) can call pvPortMalloc() and application code can  
call malloc() directly (with all the normal thread safety, etc., caveats  
around that) so the RTOS objects come from a different heap to  
everything else.
