1 | /** |
---|
2 | ****************************************************************************** |
---|
3 | * @file sysmem.c |
---|
4 | * @author Generated by STM32CubeMX |
---|
5 | * @brief 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 or Picolibc libc manual |
---|
10 | ****************************************************************************** |
---|
11 | * @attention |
---|
12 | * |
---|
13 | * Copyright (c) 2025 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 | |
---|
23 | /* Includes */ |
---|
24 | #include <errno.h> |
---|
25 | #include <stdint.h> |
---|
26 | #include <stddef.h> |
---|
27 | |
---|
28 | /** |
---|
29 | * Pointer to the current high watermark of the heap usage |
---|
30 | */ |
---|
31 | static uint8_t *__sbrk_heap_end = NULL; |
---|
32 | |
---|
33 | /** |
---|
34 | * @brief _sbrk() allocates memory to the newlib heap and is used by malloc |
---|
35 | * and others from the C library |
---|
36 | * |
---|
37 | * @verbatim |
---|
38 | * ############################################################################ |
---|
39 | * # .data # .bss # newlib heap # MSP stack # |
---|
40 | * # # # # Reserved by _Min_Stack_Size # |
---|
41 | * ############################################################################ |
---|
42 | * ^-- RAM start ^-- _end _estack, RAM end --^ |
---|
43 | * @endverbatim |
---|
44 | * |
---|
45 | * This implementation starts allocating at the '_end' linker symbol |
---|
46 | * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack |
---|
47 | * The implementation considers '_estack' linker symbol to be RAM end |
---|
48 | * NOTE: If the MSP stack, at any point during execution, grows larger than the |
---|
49 | * reserved size, please increase the '_Min_Stack_Size'. |
---|
50 | * |
---|
51 | * @param incr Memory size |
---|
52 | * @return Pointer to allocated memory |
---|
53 | */ |
---|
54 | void *_sbrk(ptrdiff_t incr) |
---|
55 | { |
---|
56 | extern uint8_t _end; /* Symbol defined in the linker script */ |
---|
57 | extern uint8_t _estack; /* Symbol defined in the linker script */ |
---|
58 | extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ |
---|
59 | const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; |
---|
60 | const uint8_t *max_heap = (uint8_t *)stack_limit; |
---|
61 | uint8_t *prev_heap_end; |
---|
62 | |
---|
63 | /* Initialize heap end at first call */ |
---|
64 | if (NULL == __sbrk_heap_end) |
---|
65 | { |
---|
66 | __sbrk_heap_end = &_end; |
---|
67 | } |
---|
68 | |
---|
69 | /* Protect heap from growing into the reserved MSP stack */ |
---|
70 | if (__sbrk_heap_end + incr > max_heap) |
---|
71 | { |
---|
72 | errno = ENOMEM; |
---|
73 | return (void *)-1; |
---|
74 | } |
---|
75 | |
---|
76 | prev_heap_end = __sbrk_heap_end; |
---|
77 | __sbrk_heap_end += incr; |
---|
78 | |
---|
79 | return (void *)prev_heap_end; |
---|
80 | } |
---|
81 | |
---|
82 | #if defined(__PICOLIBC__) |
---|
83 | // Picolibc expects syscalls without the leading underscore. |
---|
84 | // This creates a strong alias so that |
---|
85 | // calls to `sbrk()` are resolved to our `_sbrk()` implementation. |
---|
86 | __strong_reference(_sbrk, sbrk); |
---|
87 | #endif |
---|