# FreeRTOS: Memory Management

**URL:** <https://forums.freertos.org/t/freertos-memory-management/5937>\
**Category:** Kernel\
**Created:** [May 22, 2016, 9:50pm UTC](https://forums.freertos.org/t/freertos-memory-management/5937 "2016-05-22T21:50:00Z")\
**Posts on this page:** 8\
**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:** [May 22, 2016, 9:50pm UTC](https://forums.freertos.org/t/freertos-memory-management/5937/1 "2016-05-22T21:50:00Z")

</div>

**[paulromero](https://sourceforge.net/u/paulromero/)** wrote on [Sunday, May 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#3155):

Dear Forum:

Is is possible to configure FreeRTOS such that the heap. implementation  
of prvPortMalloc() etc. can be used on separate heap and ram segments  
the concurrently ?

More clearly, I would like to use the same memory  
management algrorithm for two completey separate areas of memory.  
One is a large ram segment and the other is the default heap segment.

I am almost certan the names of the allocation and free routines  
must have distinct names and something needs to be done to point  
the at the right segment during initialization.

Best Regards,

Paul R.

---

<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 22, 2016, 11:45pm UTC](https://forums.freertos.org/t/freertos-memory-management/5937/2 "2016-05-22T23:45:36Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, May 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#d040):

You probably want to make a copy of the heap#.c file and give the functions a new name to make the version to handle the separate area. You can then add whatever is needed to force the heap array into the right memory, this is very system dependent.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 23, 2016, 6:08am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/3 "2016-05-23T06:08:24Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Monday, May 23, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#c52c):

Do you want two different malloc routines, e.g.:

```auto
    /* Malloc internal RAM. */
    void *pvPortMalloc();
    void vPortFree();

    /* Malloc external RAM. */
    void *pvPortMalloc_2();
    void vPortFree_2();

```

Or do you want a single malloc/free couple that manages both areas of memory?  
In the last case you can use heap\_5.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:** [May 23, 2016, 7:40am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/4 "2016-05-23T07:40:37Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 23, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#c52c/bf65):

Further to Hein’s post, here is a link with a little more information on  
heap\_5: [http://www.freertos.org/a00111.html#heap\_5](http://www.freertos.org/a00111.html#heap_5)

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 25, 2016, 12:56am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/5 "2016-05-25T00:56:14Z")

</div>

**[paulromero](https://sourceforge.net/u/paulromero/)** wrote on [Wednesday, May 25, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#c52c/1e32):

Hi Richard:

I would like to use two different malloc() routines  
with independent memory management.

I designed a best fit memory management algorithm  
which has been extensively tested and and moderately  
analyzed. It is pretty much system independent. All  
you need to do is point at the chunk of memory you  
want to use. However, it is not reentrant and the  
code could use some refinement.

The code that implements the algorithm and the proof of  
it are attached to this note for your perusal. Can  
you think of a way to make it safe for use in a  
multitasking environment without using mutexes ?  
I assume no one is dumb enough to try to use in  
an interrupt handler or critical section.

Best Regards,

Paul R.

---

<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 25, 2016, 2:17am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/6 "2016-05-25T02:17:01Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Wednesday, May 25, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#d251):

The simplest way to get mutual exclusion is a mutex, that is the basic purpose of them (which is what gives them their name). The alternative would be to do something to disable task switching for the duration, either with a critical section (which may cause unexceptable delays to interrupts) or to disable the scheduler, which will perhaps hold off a high priority task needlessly.

What is your problem with using a mutex?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 25, 2016, 7:32am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/7 "2016-05-25T07:32:43Z")

</div>

**[paulromero](https://sourceforge.net/u/paulromero/)** wrote on [Wednesday, May 25, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#2e97):

Hi Richard:

I have no problem, and actually like, using mutexes. However, some folks  
complain that they degrade performance.

Best Regards,

Paul R.

---

<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 25, 2016, 8:14am UTC](https://forums.freertos.org/t/freertos-memory-management/5937/8 "2016-05-25T08:14:49Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, May 25, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/810d3ad7/#2e97/135a):

On the assumption the allocation is going to be fast, and taking your  
point that nobody should be doing this in an interrupt (although you  
would be surprised what people do ;o) I would second Richard D’s  
suggestion of suspending the scheduler.

```
vTaskSuspendAll();
// Do you thing here
xTaskResumeAll();
```

It might be that you do not need to do this around the entire allocation  
function, but just at select points within the algorithm where  
simultaneous access would be an issue.
