cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic memory allocation using cosmic compiler

lowpowermcu
Associate II
Posted on July 30, 2010 at 11:29

Dynamic memory allocation using cosmic compiler

8 REPLIES 8
luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:09

Hi,

there's a chapter Heap Management Control with the C Compiler in the manual, pag 68.

Regards,

Luca

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi ubiali,

Ah ok !

Thanks ubiali for this ultra fast reply !

With regards,

MCU Lüfter

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi ubiali,

In my application I need to allocate memory space with malloc and I have tried to set the heap zone to 1KB but I couldn't.

I have updated the lkf file as following:

# Defines - section reserved for STVD

#<BEGIN DEFINED_VARIABLES>

+def __startmem=@.bss

+spc .bss=0x400 # reserve 1K space

+def __endmem=0x7ff

+def __stack=0xfff

#<END DEFINED_VARIABLES>

But I got the following error message:

#error clnk Debug\project.lkf:1 segment .bss size overflow (807)

How can I enjoy using my heap space and get rid of this error message.

Perhaps that I am using a huge global variables but how can I find it ?

BTW, what does mean ''+def __endmem=0x7ff and +def __stack=0xfff'' ?

Does it mean that stack zone is : 0x800 ---> 0xfff and the user zone is 0x000 --> 0x7ff ?

Thanks and regards,

MCU Lüfter

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:09

Hi,

the error message means that your .bss segment, that is now 0x400 bytes bigger because you want to put the heap in there, exceeds the limits that has been set for it somewhere else in the linker file.

Please post the whole linker file so that I can take a look and tell you where to modify it (also specify the max RAM address of the derivative you are using).

Regards,

Luca

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi ubiali,

Thereafter a copy of my lkf file:

-----------------------------------------------------------------------------------------------------------

#<BEGIN SEGMENT_CONF>

# Segment Code,Constants:

+seg .const -b 0x8080 -m 0xff80 -n .const -it

+seg .text -a .const -n .text

# Segment Eeprom:

+seg .eeprom -b 0x1000 -m 0x800 -n .eeprom

# Segment Zero Page:

+seg .bsct -b 0x0 -m 0x100 -n .bsct

+seg .ubsct -a .bsct -n .ubsct

+seg .bit -a .ubsct -n .bit -id

+seg .share -a .bit -n .share -is

# Segment Ram:

+seg .data -b 0x100 -m 0x700 -n .data

+seg .bss -a .data -n .bss

#<END SEGMENT_CONF>

-----------------------------------------------------------------------------------------------------------

Thanks for each explanation !

Best regards,

MCU Lüfter

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:09

''+seg .data -b 0x100 -m 0xD00 -n .data''

means that the data+bss segments (because they are attached to each other with the -a option) start in 0x100 for a maximum size of 0xD00 (-> the max address is 0xE00). Note that your bss segment now also contains the heap, that will thefore be within this address range as well.

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:09

so, considering that you seem to be using a 4k ram derivative, I think the following should work (but I haven't tried):

# Segment Code,Constants:

+seg .const -b 0x8080 -m 0xff80 -n .const -it

+seg .text -a .const -n .text

# Segment Eeprom:

+seg .eeprom -b 0x1000 -m 0x800 -n .eeprom

# Segment Zero Page:

+seg .bsct -b 0x0 -m 0x100 -n .bsct

+seg .ubsct -a .bsct -n .ubsct

+seg .bit -a .ubsct -n .bit -id

+seg .share -a .bit -n .share -is

# Segment Ram:

+seg .data -b 0x100 -m 0xD00 -n .data

+seg .bss -a .data -n .bss

+def __startmem=@.bss

+spc .bss=0x400      # reserve 1K space

+def __endmem=@.bss

+def __endzp=@.ubsct            # end of uninitialized zpage

+def __memory=@.bss            # end of bss segment

+def __stack=0xfff

This linker file will give you the following RAM configuration:

0x000 to 0x0FF     : zero page containing @tiny variables (.bsct+.ubsct)

0x100 to 0xF00     : contains @near data (.data+.bss) + 1k of heap, starting at the end of data

0xF00 to 0xFFF     : stack

Note that the stack grows backwards from the top address (0xFFF): if it grows bigger than 256 bytes it will ''possibly'' overlap with the heap (if you have enough variables so that the heap actually reaches its maximum allowed address): in this case your application will behave randomly with no linker warnings (as it is always the case with stack overflow).

Hope it is clear (linker files are always complex),

Luca

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi ubiali,

Thanks ubiali !

I get it working but I still need more clarification so thanks in advance.

You have changed

''+seg .data -b 0x100 -m 0x700 -n .data''

by

''+seg .data -b 0x100 -m 0xD00 -n .data''

does it mean that global variables are located in zone 0x100 to 0xD00 ?

Anther question:

In your conclusion:

''This linker file will give you the following RAM configuration:

0x000 to 0x0FF: zero page containing @tiny variables (.bsct+.ubsct)

0x100 to 0xF00: contains @near data (.data+.bss) + 1k of heap,

0xF00 to 0xFFF     : stack''

The zone from 0xD00  to 0xF00 is reserved to which type of data ?

How can I enlarge the @near data + heap zone beyond 0xF00 ? Is it hardware ?

In fact, I need to enlarge more the heap zone and I worry that I will overlap

with stack ? How can I avoid this ?

With regards,

MCU Lüfter