sysmem.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. ******************************************************************************
  3. * @file sysmem.c
  4. * @author Generated by STM32CubeIDE
  5. * @brief STM32CubeIDE System Memory calls file
  6. *
  7. * For more information about which C functions
  8. * need which of these lowlevel functions
  9. * please consult the newlib libc manual
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2022 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. */
  22. /* Includes */
  23. #include <errno.h>
  24. #include <stdint.h>
  25. /**
  26. * Pointer to the current high watermark of the heap usage
  27. */
  28. static uint8_t *__sbrk_heap_end = NULL;
  29. /**
  30. * @brief _sbrk() allocates memory to the newlib heap and is used by malloc
  31. * and others from the C library
  32. *
  33. * @verbatim
  34. * ############################################################################
  35. * # .data # .bss # newlib heap # MSP stack #
  36. * # # # # Reserved by _Min_Stack_Size #
  37. * ############################################################################
  38. * ^-- RAM start ^-- _end _estack, RAM end --^
  39. * @endverbatim
  40. *
  41. * This implementation starts allocating at the '_end' linker symbol
  42. * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
  43. * The implementation considers '_estack' linker symbol to be RAM end
  44. * NOTE: If the MSP stack, at any point during execution, grows larger than the
  45. * reserved size, please increase the '_Min_Stack_Size'.
  46. *
  47. * @param incr Memory size
  48. * @return Pointer to allocated memory
  49. */
  50. void *_sbrk(ptrdiff_t incr)
  51. {
  52. extern uint8_t _end; /* Symbol defined in the linker script */
  53. extern uint8_t _estack; /* Symbol defined in the linker script */
  54. extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
  55. const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
  56. const uint8_t *max_heap = (uint8_t *)stack_limit;
  57. uint8_t *prev_heap_end;
  58. /* Initialize heap end at first call */
  59. if (NULL == __sbrk_heap_end)
  60. {
  61. __sbrk_heap_end = &_end;
  62. }
  63. /* Protect heap from growing into the reserved MSP stack */
  64. if (__sbrk_heap_end + incr > max_heap)
  65. {
  66. errno = ENOMEM;
  67. return (void *)-1;
  68. }
  69. prev_heap_end = __sbrk_heap_end;
  70. __sbrk_heap_end += incr;
  71. return (void *)prev_heap_end;
  72. }