problems with malloc ()

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

problems with malloc ()

Jump to solution
4,370 Views
Danielito
Contributor I
I have a MC9S09QG8 and the problem is that I get a message when working with malloc (): L1102: Out of allocation space in segment RAM at address 0x10C. There are solutions on how to modify file libdefs.h as LIBDEF_HEAPSIZE but does not work. 
I know it's an MCU ¨ small but deserie create a small buffer with dynamic memory and not let me. Any suggestion is welcome 
thanks 
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
2,127 Views
kef
Specialist I

You didn't rebuilt the library. Changing LBDEF_xx is not enough. To rebuild the library, you should open library project file and make it. This should rebuild all libraries. To rebuild just ansiis.lib, change target combo box from all libraries to "C no float HCS08", then make.

Default library project file path is this:

 

"c:\Program Files\Freescale\CodeWarrior for Microcontrollers V6.2\lib\hc08c\hc08_lib.mcp"

View solution in original post

0 Kudos
Reply
13 Replies
2,127 Views
Lundin
Senior Contributor IV
Since dynamic allocation is unsuitable in embedded systems in general, and in 8-bit apps in particular, trying to introduce a bombastic heap in a tiny 8-bit, 8k micro ultimately sounds like poor design... what exactly are you trying to achieve here?

You will still have to reserve heap size for the worst scenario, so you save no memory whatsoever with a heap. The heap will however invite the possibility of memory leaks.

Why not simply allocate a static array and be done with it? In this plain static array you can implement a ring buffer, or a fixed max size linked list, or whatever such ADT your app needs. Much more efficient and safe.
0 Kudos
Reply
2,127 Views
stanish
NXP Employee
NXP Employee

Hello Danielito,

 

The default heap size (2000 Bytes) is probably too big for this MCU. Reduce the heap size (LIBDEF_HEAPSIZE) in libdefs.hand rebuild the library.

Then you shold be able to allocate heap in RAM.

 

See also this thread.

 

Stanish 

0 Kudos
Reply
2,127 Views
Danielito
Contributor I

Hi Stanish: I tried without success. I send the file PWM4 where the function insert () in MCUinit.c. The problem occurs when you enable the allocation nuevo=(pNodo)malloc(sizeof (NODETYPE));

any suggestion is welcome 

Thanks

 

PWM4.zip

Message Edited by t.dowe on 2009-10-13 10:21 AM
0 Kudos
Reply
2,127 Views
stanish
NXP Employee
NXP Employee

Danielito, 

 

Seems in your case the problem is caused by the fact you've splitted the RAM into two seqments.

 

SEGMENTS      Z_RAM =  READ_WRITE   0x0060 TO 0x00FF;   
  RAM =  READ_WRITE   0x0100 TO 0x015F;
           RAM2 =  READ_WRITE   0x0160 TO 0x025F;  
...
PLACEMENT
  DEFAULT_RAM               INTO  RAM;
  DINAMICA,                 INTO  RAM2;       

 

RAM  size  -> 96 Bytes  

RAM2 size -> 256 Bytes

 

Since Stack and Heap are both placed into DEFAULT_RAM by default there is not enough space.

According to the .prm file stack size is set to 0x50 = 80 Bytes -> there are remaining 16 bytes for variables + Heap.

 

I'd suggest you to set bigger "RAM" memory segment plus you can allocate heap into RAM2:

PLACEMENT
  DEFAULT_RAM               INTO  RAM;
  DINAMICA,HEAP_SEGMENT     INTO  RAM2;

 

Stanish

Message Edited by stanish on 2009-10-13 02:49 PM
0 Kudos
Reply
2,127 Views
Danielito
Contributor I

Hi Stanish: i do

 

RAM                      =  READ_WRITE   0x0100 TO 0x024F;RAM2                     =  READ_WRITE   0x0250 TO 0x025F;

 and the problems continue.

whit variable “nuevo”  assigned whit malloc() in MCUinit.c

in projec.map appears:

 

.bss                              12   R/W      0x100      0x10B   RAM

 and the error:

 

Link Error   : L1102: Out of allocation space in segment RAM at address 0x10C

 no variable “nuevo”assigned in MCUinit.c:

 

.bss                               8   R/W      0x100      0x107   RAM.common                            6   R/W      0x108      0x10D   RAM.stack                            80   R/W      0x10E      0x15D   RAM

 and not problems in RAM.

What can i do? 

 

 

 thank you

 

 

 

0 Kudos
Reply
2,127 Views
stanish
NXP Employee
NXP Employee

Danielito,

 

I've used your project (see .prm below) :

 

SEGMENTS ...    RAM  =  READ_WRITE   0x0100 TO 0x024F;        RAM2 =  READ_WRITE   0x0250 TO 0x025F;...PLACEMENT   DEFAULT_RAM  INTO  RAM;   DINAMICA     INTO  RAM2;        ...
STACKSIZE 0x50

stacksize:  80 Bytes

heap size: 80 Bytes

 

And I can link the app. without any linker error. See the snippet from the map file below:

 

 

*********************************************************************************************SECTION-ALLOCATION SECTIONSection Name                    Size  Type     From       To       Segment---------------------------------------------------------------------------------------------DINAMICA                           1   R/W      0x250      0x250   RAM2....bss                              12   R/W      0x100      0x10B   RAMHEAP_SEGMENT                      80   R/W      0x10C      0x15B   RAM.common                            8   R/W      0x15C      0x163   RAM.stack                            80   R/W      0x164      0x1B3   RAM.... nuevo                                      162       2       2       1   .common    

Stanish

 

 

0 Kudos
Reply
2,127 Views
Danielito
Contributor I

Hi Stanish: because I still have problems sending you 3 file: 1) Project(whitout nuevo).map: this file does not assign the variable nuevo within MCUinit.c when I compile with my CW (whitout problems in the segmentation) 2) Project(with nuevo).map: : this file assign the variable nuevo within MCUinit.c when I compile with my CW (with problems in the segmentation) 3) PWM4.mcp whitout the variable nuevo assigned within MCUinit.c and the changes in the file .prm we observe. If you can compiler PWM4 with your compiler and have no problems, them problems may be in the configuration of the heap in the my CW compiler or the problems is in the compiler of CW..., because if you see the file Project(whitout nuevo), errors appears in several sections relating to HEAP.

 

consulta15-10-09.zip

Message Edited by t.dowe on 2009-10-16 09:52 AM
0 Kudos
Reply
2,127 Views
stanish
NXP Employee
NXP Employee

Danielito,

 

I don't think it's a compiler issue.

I suspect you haven't rebuilt ansi libraries or didn't modify heap size properly. Your "libbdef.h" and "ansiis.lib" are not included in .zip file so i cannot check it. 

But according to "project(with nuevo).map" heap size is still 2000 Bytes.

 

MODULE: -- HEAP.C.o (ansiis.lib) ---
PROCEDURES:
_heapcrash_  0'Error      16      22       0   .text      
VARIABLES:    
_heap_   0'Error     7D0    2000       0   HEAP_SEGMENT

I'd suggest you to double check if LIBDEF_HEAPSIZE (libdefs.h) is reduced to fit  S08QG8 RAM (e.g 80 Bytes)

and Rebuild the Libraries project which update all the ansi*.lib (files). In your case you are using "ansiis.lib" so the cration date of this file should change after rebuid.

Follow these instructions to rebuild your library correctly.

 

Stanish 

 

 

Message Edited by stanish on 2009-10-16 09:59 AM
0 Kudos
Reply
2,127 Views
Danielito
Contributor I

Hi Stanish: the problem was that I not pressed F7 to rebuild the *. lib hc08_lib.mcp project. Now, I wonder if there is a FAQ of the forum can guide me in the reconstruction or construction of library files from source files. 
Thanks for your guidance

Danielito 

0 Kudos
Reply
2,127 Views
stanish
NXP Employee
NXP Employee

Danielito,

 

Check the FAQ below: 

 

FAQ-27441  Can I use malloc in CodeWarrior for HC(S)08?

 

Stanish 

0 Kudos
Reply
2,127 Views
Danielito
Contributor I

Hi, Stanish: still not resolved the problems with HEAP. I did several times what you suggested to not avail.

In file libdefh.h:

#define LIBDEF_HEAPSIZE  100

In file Project.map appears:

  
MODULE:                 -- HEAP.C.o (ansiis.lib) --
- PROCEDURES:
     _heapcrash_                                  0'Error      16      22       0   .text      
- VARIABLES:
     _heap_                                       0'Error     7D0    2000       0   HEAP_SEGMENT

 

I send attached:1)project.map (this update at 18-10-09) 2)ansiis.lib (this file not update. I can not open. CW´s message is: Codewarrior cannot open files of this type.) 3)CW screen image.

Where I continue?

 Thank you

consulta_18-10-09.zip

Message Edited by t.dowe on 2009-10-19 10:24 AM
0 Kudos
Reply
2,128 Views
kef
Specialist I

You didn't rebuilt the library. Changing LBDEF_xx is not enough. To rebuild the library, you should open library project file and make it. This should rebuild all libraries. To rebuild just ansiis.lib, change target combo box from all libraries to "C no float HCS08", then make.

Default library project file path is this:

 

"c:\Program Files\Freescale\CodeWarrior for Microcontrollers V6.2\lib\hc08c\hc08_lib.mcp"

0 Kudos
Reply
2,127 Views
Danielito
Contributor I

what I needed was to know how a library is rebuilt. Thanks to your messages and reference ppoi (FAQ 3404) succeeded. I wonder now how llibrary was built from a source file.

Thanks Kef and Stanish

0 Kudos
Reply