This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

sbrk implementation in newlib


Hi,

I am trying to implement sbrk for a private target in newlib.

For my target stack grows from lower address to higher address and
ideally heap should grow in the opposite direction. Hence i assigned
the start of heap at the end of memory. So it is like stack grows from
1000 to 5000 and stack grows downwards from 5000 towards 0. For this
configuration the following the sbrk implementation that i have
written in the newlib

register char *stack_ptr asm ("sp");

caddr_t
sbrk (incr)
     int incr;
{
  extern char _heap; /* Defined by the linker */
  static char *h_end;

  if (h_end == 0)
    h_end = &_heap;

  if ((h_end - incr) <= stack_ptr)
    {
      write (1, "Heap and stack collision\n", 25);
      abort ();
    }

  h_end = h_end - incr;
  return (caddr_t)h_end;
}

But malloc always returns null for this implementation. Is it not
possible to have a configuration that i am trying to implement?
Or should i limit the stack region say to 3000 and start heap from
3000 and have the traditional sbrk which is implemented by most
targets?

Regards,
Shafi

P.S i am not subscribed to this list. Please keep in CC when you reply.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]