brk.c 758 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* $Source$
  2. * $State$
  3. * $Revision$
  4. */
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
  9. #define STACK_BUFFER 128 /* number of bytes to leave for stack */
  10. extern char _end[1];
  11. static char* current = _end;
  12. int brk(void* newend)
  13. {
  14. /* We determine the amount of free memory by looking at the address of the
  15. * BDOS vector at 0x0006. */
  16. char* memtop = (char*) ((*(unsigned char*)0x0007)<<8);
  17. char* p = newend;
  18. if ((p >= memtop) ||
  19. (p < _end))
  20. return -1;
  21. current = p;
  22. return 0;
  23. }
  24. void* sbrk(intptr_t increment)
  25. {
  26. char* old;
  27. if (increment == 0)
  28. return current;
  29. old = current;
  30. if (brk(old + increment) < 0)
  31. return OUT_OF_MEMORY;
  32. return old;
  33. }