/* Support files for GNU libc.  Files in the system namespace go here.
   Files in the C namespace (ie those that do not start with an
   underscore) go in .c.  */

#include <unistd.h>

caddr_t _sbrk		_PARAMS ((int));

register char * stack_ptr asm ("sp");
caddr_t _sbrk (int incr)
{
 extern char end;		/* Defined by the linker */
  static char *heap_end;
  char *prev_heap_end;
 
  if (heap_end == 0) {
    heap_end = &end;
  }
  prev_heap_end = heap_end;
  if (heap_end + incr > stack_ptr)
    {
      //stack and heap collision detected!!!
	  //4 KBytes of RAM space needed for first heap page.
	  //Workaround: Set the upper limit of the heap below the stack pointer
      heap_end = stack_ptr - 0x100;
    }

  heap_end += incr;
  return (caddr_t) prev_heap_end;
}
 	
