sysmem.c
Go to the documentation of this file.
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
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
14
* All rights reserved.</center></h2>
15
*
16
* This software component is licensed by ST under BSD 3-Clause license,
17
* the "License"; You may not use this file except in compliance with the
18
* License. You may obtain a copy of the License at:
19
* opensource.org/licenses/BSD-3-Clause
20
*
21
******************************************************************************
22
*/
23
24
/* Includes */
25
#include <errno.h>
26
#include <stddef.h>
27
#include <stdint.h>
28
29
/**
30
* Pointer to the current high watermark of the heap usage
31
*/
32
static
uint8_t *
__sbrk_heap_end
= NULL;
33
34
/**
35
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
36
* and others from the C library
37
*
38
* @verbatim
39
* ############################################################################
40
* # .data # .bss # newlib heap # MSP stack #
41
* # # # # Reserved by _Min_Stack_Size #
42
* ############################################################################
43
* ^-- RAM start ^-- _end _estack, RAM end --^
44
* @endverbatim
45
*
46
* This implementation starts allocating at the '_end' linker symbol
47
* The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
48
* The implementation considers '_estack' linker symbol to be RAM end
49
* NOTE: If the MSP stack, at any point during execution, grows larger than the
50
* reserved size, please increase the '_Min_Stack_Size'.
51
*
52
* @param incr Memory size
53
* @return Pointer to allocated memory
54
*/
55
void
*
_sbrk
(ptrdiff_t incr)
56
{
57
extern
uint8_t _end;
/* Symbol defined in the linker script */
58
extern
uint8_t _estack;
/* Symbol defined in the linker script */
59
extern
uint32_t _Min_Stack_Size;
/* Symbol defined in the linker script */
60
const
uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
61
const
uint8_t *max_heap = (uint8_t *)stack_limit;
62
uint8_t *prev_heap_end;
63
64
/* Initalize heap end at first call */
65
if
(NULL ==
__sbrk_heap_end
)
66
{
67
__sbrk_heap_end
= &_end;
68
}
69
70
/* Protect heap from growing into the reserved MSP stack */
71
if
(
__sbrk_heap_end
+ incr > max_heap)
72
{
73
errno = ENOMEM;
74
return
(
void
*)-1;
75
}
76
77
prev_heap_end =
__sbrk_heap_end
;
78
__sbrk_heap_end
+= incr;
79
80
return
(
void
*)prev_heap_end;
81
}
__sbrk_heap_end
static uint8_t * __sbrk_heap_end
Definition:
sysmem.c:32
_sbrk
void * _sbrk(ptrdiff_t incr)
_sbrk() allocates memory to the newlib heap and is used by malloc and others from the C library
Definition:
sysmem.c:55