realloc heap
I use realloc in a parser for converting a JSON file into a binary format. At the begginning of the parse process I dont know how much json object are in the file, so I use realloc.
It works fine till one moment.
My Trace show the problem
SYSCALL : HEAP ptr 0x200057f0, start 0x200056d8, end 0x2001f8d0, size 107000, free 106720
SYSCALL : HEAP ptr 0x20006000, start 0x200056d8, end 0x2001f8d0, size 107000, free 104656
SYSCALL : HEAP ptr 0x20007000, start 0x200056d8, end 0x2001f8d0, size 107000, free 100560
SYSCALL : HEAP ptr 0x20009000, start 0x200056d8, end 0x2001f8d0, size 107000, free 92368
SYSCALL : HEAP ptr 0x2000d000, start 0x200056d8, end 0x2001f8d0, size 107000, free 75984
SYSCALL : HEAP ptr 0x20015000, start 0x200056d8, end 0x2001f8d0, size 107000, free 43216
SYSCALL : NOMEM heap_ptr 0x20025000, __HeapLimit 0x2001f8d0, Heapstart 0x200056d8, Heapsize 107000
The first call of _sbrk function malloc or realloc will have 255Byte
the secound call 2048, the third 4096, next 8192, next 16384 , then 32768
at least malloc will have 65536k but I have only 43k.
What can I do ? I only want alloc 24Byte and realloc or malloc will have a block of 64kByte.
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Related bugs
Related FAQ:
None Link to a FAQ
Revision history for this message
![]() |
#1 |
I did try the case again with a loop
void API_Init( void )
{
_PTR ptr = malloc(200);
_PTR Ptr;
int i;
for(i=0; i<10000; i++)
{
Ptr = realloc( ptr, i*32);
if (Ptr)
{
ptr = Ptr;
}
else
{
ptr=0;
break;
}
}
free(ptr);
_ptr = malloc(200);
for(i=0; i<10000; i++)
{
Ptr = realloc( ptr, i*32);
if (Ptr)
{
ptr = Ptr;
}
else
{
ptr=0;
break;
}
}
free(ptr);
.......
Here the SBRK function
caddr_t _sbrk( int incr )
{
unsigned long heap_end;
unsigned long heap_size;
unsigned long heap_start;
unsigned char *prev_heap;
unsigned long free;
heap_start = (unsigned long)&heap;
heap_end = (unsigned long)&heap[sizeof( heap ) - 1];
heap_size = sizeof( heap );
//Falls die Initalisierung nicht richtig funktioniert.
if( ( (unsigned long)heap_CPU < heap_start ) || ( (unsigned long)heap_CPU > heap_end ) )
{
}
// Bei erster Benutzung muss die Start-Adresse übergeben werden
if( heap_CPU == NULL )
{
}
if (incr > heap_end)
{
//Fehler zurückgeben
return( (caddr_t) -1 );
}
// der alte Wert muss zurückgegeben werden
prev_heap = heap_CPU;
// Prüfen ob die Speicherreservi
if( (unsigned long) ( heap_CPU + incr ) > (unsigned long)( heap_start + heap_size ) )
{
free = (unsigned long)heap_end - (unsigned long)(heap_CPU);
return( (caddr_t) -1 );
}
// Reservierung freigeben
heap_CPU += incr;
free = (unsigned long)heap_end - (unsigned long)heap_CPU;
DEBUGString( "HEAP", "NEW END ADDRESS <0x%08X>. Requested Size %6u, Free %6u", heap_CPU , incr, free);
return( (caddr_t) prev_heap );
}
Here the Trace from my program
14-07-08 15:14:20:853, HEAP | NEW END ADDRESS <0x20019240>. Requested Size 224, Free 87839
14-07-08 15:14:21:763, HEAP | NEW END ADDRESS <0x2001A000>. Requested Size 3520, Free 84319
14-07-08 15:14:22:393, HEAP | NEW END ADDRESS <0x2001B000>. Requested Size 4096, Free 80223
14-07-08 15:14:23:034, HEAP | NEW END ADDRESS <0x2001D000>. Requested Size 8192, Free 72031
14-07-08 15:14:23:713, HEAP | NEW END ADDRESS <0x20021000>. Requested Size 16384, Free 55647
14-07-08 15:14:24:738, HEAP | NEW END ADDRESS <0x20029000>. Requested Size 32768, Free 22879
14-07-08 15:14:25:895, HEAP | ERROR: OUT OF MEMORY! Requested Size 65536, Free 22879
and now call I the free function
14-07-08 15:14:30:381, HEAP | NEW END ADDRESS <0x2002A000>. Requested Size 4096, Free 18783
14-07-08 15:14:34:182, HEAP | NEW END ADDRESS <0x2002C000>. Requested Size 8192, Free 10591
14-07-08 15:14:40:791, HEAP | ERROR: OUT OF MEMORY! Requested Size 16384, Free 10591
The Trace show me that the free function not works correctly. The beginning of the new loop does not use the start address of heap, but the used heap is 0.
What can I do????
Revision history for this message
![]() |
#2 |
Dear Maik,
Can you please share the environment that this problem shows up, such as OS, architecture, GCC version, libc version.
From the information you shared I do not see any relavance to this project.
- Joey
Revision history for this message
![]() |
#3 |
Hello Joey,
I did use µC-OS-II and FreeRTOS. My microcontroller is a stm32f427 and I use eclipse kepler for development.
I did try to make a malloc over the complete heap section, the I can work with realloc on the end of heap.
But if I use realloc to make the memory allocation then I have the problem that I can not use all size of my heap memory.
Revision history for this message
![]() |
#4 |
Would do please try newlib-nano, (-specs=nano.specs in linker option)? Its
malloc is much simpler for you to control the behavior.
On Tue, Aug 5, 2014 at 11:07 PM, Maik Siebrand <
<email address hidden>> wrote:
> Question #251292 on GCC ARM Embedded changed:
> https:/
>
> Maik Siebrand posted a new comment:
> Hello Joey,
>
> I did use µC-OS-II and FreeRTOS. My microcontroller is a stm32f427 and I
> use eclipse kepler for development.
> I did try to make a malloc over the complete heap section, the I can work
> with realloc on the end of heap.
> But if I use realloc to make the memory allocation then I have the problem
> that I can not use all size of my heap memory.
>
> --
> You received this question notification because you are an answer
> contact for GCC ARM Embedded.
>
Revision history for this message
![]() |
#5 |
It does not work
Look
uilding target: evo4.3.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -Wno-char-
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
d:/programme/
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [evo4.3.elf] Error 1
Revision history for this message
![]() |
#6 |
Please remove "-Xlinker" before -specs
On Wed, Aug 6, 2014 at 10:42 PM, Maik Siebrand <
<email address hidden>> wrote:
> Question #251292 on GCC ARM Embedded changed:
> https:/
>
> Status: Answered => Open
>
> Maik Siebrand is still having a problem:
> It does not work
>
> Look
>
>
> uilding target: evo4.3.elf
> Invoking: Cross ARM C Linker
> arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O0 -fmessage-length=0
> -fsigned-char -ffunction-sections -fdata-sections -Wall
> -Wno-char-
> "D:\Design\
> -Xlinker --gc-sections
> -L"D:\Design\
> -L"D:\Design\
> -Xlinker -specs=nano.specs -Wl,-Map,
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> ./codebase/
> -lstm32stdlibF4
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> BFD (GNU Tools for ARM Embedded Processors) 2.23.2.20140228 assertion fail
> /home/build/
> d:/programme/
> warning: cannot find entry symbol cs=nano.specs; defaulting to 080001d0
> collect2.exe: error: ld returned 1 exit status
> mingw32-make: *** [evo4.3.elf] Error 1
>
> --
> You received this question notification because you are an answer
> contact for GCC ARM Embedded.
>
Can you help with this problem?
Provide an answer of your own, or ask Maik Siebrand for more information if necessary.