hpm_sbrk.c 715 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifdef __GNUC__
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include "hpm_common.h"
  11. void *_sbrk(int incr)
  12. {
  13. extern char __heap_start__, __heap_end__;
  14. static char *heap_end;
  15. char *prev_heap_end;
  16. void *ret;
  17. if (heap_end == NULL)
  18. {
  19. heap_end = &__heap_start__;
  20. }
  21. prev_heap_end = heap_end;
  22. if ((unsigned int)heap_end + (unsigned int)incr > (unsigned int)(&__heap_end__))
  23. {
  24. errno = ENOMEM;
  25. ret = (void *)-1;
  26. }
  27. else
  28. {
  29. heap_end = (char *)((unsigned int)heap_end + (unsigned int)incr);
  30. ret = (void *)prev_heap_end;
  31. }
  32. return ret;
  33. }
  34. #endif