# C18 linker restrictions loosened

**URL:** <https://forums.freertos.org/t/c18-linker-restrictions-loosened/3185>\
**Category:** Kernel\
**Created:** [July 20, 2006, 9:14am UTC](https://forums.freertos.org/t/c18-linker-restrictions-loosened/3185 "2006-07-20T09:14:05Z")\
**Posts on this page:** 2\
**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:** [July 20, 2006, 9:14am UTC](https://forums.freertos.org/t/c18-linker-restrictions-loosened/3185/1 "2006-07-20T09:14:05Z")

</div>

**nobody** wrote on [Thursday, July 20, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/905dcf57/#0d9e):

Hi,

I studied the remarks by John Franklin about how to set up the linker script and modify heap\_1.c to prevent variables leaking across page boundaries.

I think his solution is way too complex.

The goal is to get the heap in one memory section (which can cross multiple banks), and keep the rest of the variables from crossing a bank boundary.

This can simply be realised by combining no more banks than required for the heap.  
Let’s say the desired heap is 2200 bytes

This is done by  
#define configTOTAL\_HEAP\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( size\_t ) 2200 )  
in FreeRTOSConfig.h

As a result the variable ‘xHeap’ defined in heap\_1.c will be 2204 bytes long because of the additional long variable in the struct.  
In hexadecimal 2204 is 0x89C, which is the minimum required size of the memory region. (There are no other variables defined in heap\_1.c, so this is all the memory required for de .udata\_heap\_1.o memory section)

It is now sufficient to define a region in the linker script which is between 0x89C and 0x900 long.  
The rest of the banks will be left alone and have a length of 0x100, starting at a boundary.

The linker will automatically fit the .udata\_heap\_1.o memory section (containing the xHeap variable) into the first region with enough space. The remaining space in the combined region can be used by other memory sections.  
(It is in this example \_not\_ allowed to define the region \_larger\_ than 0x900 bytes since other variables stored in the region can then cross a boundary.)

Conclusions:  
- There is no need to modify the FreeRTOS library file heap\_1.c with  
#pragma udata heap\_section  
#pragma udata  
- There is no need to define the combined region "PROTECTED"  
- There is no need for a "SECTION&nbsp;&nbsp;&nbsp; NAME=heap\_section RAM=heapregion" mapping in the linker script

All that is required is to combine no more banks than strictly required to accomodate configTOTAL\_HEAP\_SIZE+4 bytes  
It is allowed to oversize the region to the next bank boundary, and let the linker fill up the rest of the region. You don’t have to define your own (non-PROTECTED) region to fill up the bank if you don’t want to waste the remaining space.

Paul

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2006, 7:40pm UTC](https://forums.freertos.org/t/c18-linker-restrictions-loosened/3185/2 "2006-07-20T19:40:05Z")

</div>

**nobody** wrote on [Thursday, July 20, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/905dcf57/#0d9e/07c7):

Makes sense.&nbsp; Comments John?
