malloc fails to allocate from desired heap
I am using GNU ARM Embbedded (newlib) 4.9 20014q4 to create a Cortex-M4 application.
The application tests memory allocations as follows:
extern unsigned int start_of_heap;
extern unsigned int end_of_heap;
extern caddr_t heap;
void foo(uint8_t i)
{
unsigned long blah = 0;
unsigned long * halb;
halb = malloc(
printf("blah(%08x) halb(%08x) heap(%08x)\n", &blah, halb, heap);
if(i)
foo(i - 1);
free(halb);
}
int main(int argc, char ** argv)
{
init_uart(
printf("Heap test (%08x - %08x)\n", &start_of_heap, &end_of_heap);
foo(10);
return 0;
}
The following output is generated:
Heap test (20000230 - 20000a30)
blah(20006fb0) halb(00000410) heap(a767ddfd)
blah(20006f98) halb(00000420) heap(a767ddfd)
blah(20006f80) halb(00000430) heap(a767ddfd)
blah(20006f68) halb(00000440) heap(a767ddfd)
blah(20006f50) halb(00000450) heap(a767ddfd)
blah(20006f38) halb(00000000) heap(a767ddfd)
blah(20006f20) halb(00000000) heap(a767ddfd)
blah(20006f08) halb(00000000) heap(a767ddfd)
blah(20006ef0) halb(00000000) heap(a767ddfd)
blah(20006ed8) halb(00000000) heap(a767ddfd)
blah(20006ec0) halb(00000000) heap(a767ddfd)
There are a few items that concern me but right now I'm focused on the address returned by malloc (00000410+). This appears out of range for the linker defined memory.
From the end of linker SECTIONS:
end = .;
start_of_heap = .;
. += 2048;
end_of_heap = .;
_sbrk is implemented as follows:
extern unsigned int start_of_heap;
extern unsigned int end_of_heap;
caddr_t heap = NULL;
// low level bulk memory allocator - used by malloc
caddr_t _sbrk ( int increment ) {
caddr_t prevHeap;
caddr_t nextHeap;
if (heap == NULL) {
// first allocation
heap = (caddr_
}
prevHeap = heap;
// Always return data aligned on a 8 byte boundary
nextHeap = (caddr_
// get current stack pointer
register caddr_t stackPtr asm ("sp");
// Check enough space and there is no collision with stack coming the other way
// if stack is above start of heap
if ( ((nextHeap > stackPtr)) ||
(nextHeap >= (caddr_
return NULL; // error - no more memory
} else {
heap = nextHeap;
return (caddr_t) prevHeap;
}
}
Where did I misstep?
Thanks,
Devan
UPDATE:
On further inspection the heap variable never appears to equal NULL so it is never initialized. If I change the heap initialization as follows it starts with a correct value:
caddr_t heap = (caddr_
Despite this the malloc still appears to be returning bogus values (00000410+) while _sbrk is returning addresses within the region defined by the linker variables.
Question information
- Language:
- English Edit question
- Status:
- Expired
- Assignee:
- No assignee Edit question
- Last query:
- Last reply: