fsl_sbrk.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #if defined(__GNUC__)
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #endif
  12. #if defined(__GNUC__)
  13. /*!
  14. * @brief Function to override ARMGCC default function _sbrk
  15. *
  16. * _sbrk is called by malloc. ARMGCC default _sbrk compares "SP" register and
  17. * heap end, if heap end is larger than "SP", then _sbrk returns error and
  18. * memory allocation failed. This function changes to compare __HeapLimit with
  19. * heap end.
  20. */
  21. caddr_t _sbrk(int incr);
  22. caddr_t _sbrk(int incr)
  23. {
  24. extern char end __asm("end");
  25. extern char heap_limit __asm("__HeapLimit");
  26. static char *heap_end;
  27. char *prev_heap_end;
  28. caddr_t ret;
  29. if (heap_end == NULL)
  30. {
  31. heap_end = &end;
  32. }
  33. prev_heap_end = heap_end;
  34. if ((unsigned int)heap_end + (unsigned int)incr > (unsigned int)(&heap_limit))
  35. {
  36. errno = ENOMEM;
  37. ret = (caddr_t)-1;
  38. }
  39. else
  40. {
  41. heap_end = (char *)((unsigned int)heap_end + (unsigned int)incr);
  42. ret = (caddr_t)prev_heap_end;
  43. }
  44. return ret;
  45. }
  46. #endif