sbrk.c 480 B

123456789101112131415161718192021222324252627
  1. /*
  2. * sbrk allocator
  3. */
  4. #include <stddef.h>
  5. #include <errno.h>
  6. #include "sys.h"
  7. #include "fw.h"
  8. #define HEAP_SIZE (4 << 20) /* 4 MB */
  9. static char __attribute__((section(".heap"))) __heap[HEAP_SIZE];
  10. static char *cur_brk = __heap;
  11. void *_sbrk(size_t increment)
  12. {
  13. char *old_brk = cur_brk;
  14. char *new_brk = old_brk + increment;
  15. if (unlikely(new_brk > &__heap[HEAP_SIZE])) {
  16. errno = ENOMEM;
  17. return (void *)(-1);
  18. }
  19. cur_brk = new_brk;
  20. return old_brk;
  21. }