# Has anyone modified heap\_4.c to add an optimized realloc?

**URL:** <https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578>\
**Category:** Kernel\
**Created:** [May 10, 2017, 10:02am UTC](https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578 "2017-05-10T10:02:20Z")\
**Posts on this page:** 4\
**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 10, 2017, 10:02am UTC](https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578/1 "2017-05-10T10:02:20Z")

</div>

**[cdebrebisson](https://sourceforge.net/u/cdebrebisson/)** wrote on [Wednesday, May 10, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a25516c1/#5a1b):

Hello,

Well, I guess the whole question lies in the title.  
I have been batteling between using the FreeRTOS provided heap management and the compiler provided memory management.  
The FreeRTOS scheme is winning at the moment because I can easely make isso that the memory size is defined at run time and not comile time (since the board can be loaded in the factory with any size RAM)…  
However, I do need a realloc and I would like it to be more optimized than a basic malloc/copy/free scheme (ie: free extra ram if shrink and grow in place (if possible)).

Has anyone ever implemented such function in heap4.c?

Cyrille

---

<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 10, 2017, 11:14am UTC](https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578/2 "2017-05-10T11:14:10Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Wednesday, May 10, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a25516c1/#2122):

If you want realloc, I would go to heap\_3.c and use that method. The other option which is available with some implementations, is to see if your library has hooks in it to make malloc/free/realloc thread safe (I know some versiion of newlib have this).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 10, 2017, 2:23pm UTC](https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578/3 "2017-05-10T14:23:47Z")

</div>

**[cdebrebisson](https://sourceforge.net/u/cdebrebisson/)** wrote on [Wednesday, May 10, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a25516c1/#9a28):

Hello,

Except that this assumes that the linker does have a free/malloc/realloc, which is not the case for me (well, more specially, I do not want to use their versions because I can not modify them and make them able to deal with my ram which is of unknown size at build time. Size is detected at run time).

Cyrille

---

<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 10, 2017, 2:34pm UTC](https://forums.freertos.org/t/has-anyone-modified-heap-4-c-to-add-an-optimized-realloc/6578/4 "2017-05-10T14:34:02Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, May 10, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a25516c1/#9a28/774a):

Most people use a crude free and re-allocation. I’m not aware of  
anything more sophisticated.
