sbrk question?

Jeff Johnston jjohnstn@redhat.com
Fri Mar 17 08:38:00 GMT 2006


Roman.Pollak@Sun.COM wrote:
> Hi,
> 
> I try to port newlib to a self made OS and run in to problems.
> So far I didn't find the real problem, since it overwrites structures
> and crashes when uses stdio functions.
> Could it be that the malloc expects to have a continues sbrk return values?

Malloc is expecting sbrk to behave like sbrk is supposed to.  It asks 
for an increment and expects the return address to increase.  If it does 
not increase as expected, it assumes that something else is issuing sbrk 
calls.  Take a look at malloc_extend_top in libc/stdlib/mallocr.c.

If that is not your problem, you may want to look at where the stack and 
heap are defined.  If they are colliding during your application and you 
aren't catching this, random behavior will occur.  I can't comment on 
your platform as you haven't given any details.  Make sure your crt0 is 
properly clearing the .bss section and initializing the stack pointer 
(usually top of data area and above _end linker symbol which is used as 
start of heap).

> Or may I alloc memory from a different place (such as kernel and
> newlib), without running in to trouble?
> 

You can create your own malloc family set of routines if you want. 
Routines that use malloc/calloc/realloc don't have any expectation of 
the memory address return value (other than non-null).  Remember that 
newlib internally uses _r versions of a number of malloc-family 
routines.  These versions take a reentrant structure pointer which is 
needed to properly set errno.

-- Jeff J.



More information about the Newlib mailing list