Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "k_mem.h"
#define MEM_BASE
Private variables
memory_pool
k_MemInit()
k_malloc(size_t)
k_free(void *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesMB1063Core/Src/k_mem.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file k_mem.c * @author MCD Application Team * @brief Utilities for file handling ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "k_mem.h" /** @addtogroup CORE * @{ *//* ... */ /** @defgroup KERNEL_MEMORY * @brief Kernel memory routines * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define MEM_BASE 0x10000000 /* Private macro -------------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ #if (defined ( __CC_ARM )) mem_TypeDef memory_pool __attribute__((at(MEM_BASE))); #elif (defined (__ICCARM__)) #pragma location = MEM_BASE __no_init mem_TypeDef memory_pool;/* ... */ #elif defined ( __GNUC__ ) mem_TypeDef memory_pool __attribute__((section(".ExtRAMData"))); #endif Private variables /* Private functions ---------------------------------------------------------*/ /** * @brief This function Initialize a memory pool for private allocator. * @param None * @retval None *//* ... */ void k_MemInit(void) { memset(&memory_pool, 0, sizeof(mem_TypeDef)); memory_pool.mallocBase = MEM_BASE + sizeof(mem_TypeDef); }{ ... } /** * @brief This function implement a simple memory management algorithm (First fit). * @param size : Requested memory size. * @retval Pointer to the allocated region. *//* ... */ void * k_malloc(size_t size) { __IO uint8_t index = 0, counter = 0, start = 0; uint8_t PageCount = 0, NewStart = 0; if (size > 0) { /* look for the first contiguous memory that meet requested size. */ while ( index < (MAX_PAGE_NUMBER)) { if (memory_pool.PageTable[index++] == 0) { counter++; if (size <= (counter * SIZE_OF_PAGE)) { PageCount = counter - 1; NewStart = start; /* Set memory region state to allocated */ for (index = 0; index <= PageCount;index++) { memory_pool.PageTable[NewStart + index] = 1; }for (index = 0; index <= PageCount;index++) { ... } /* Save size */ memory_pool.size[NewStart] = counter; /* return pointer to allocated region. */ return (void *)((memory_pool.mallocBase + (start << 10)) + 1 * sizeof(uint32_t)); }if (size <= (counter * SIZE_OF_PAGE)) { ... } }if (memory_pool.PageTable[index++] == 0) { ... } else { start = index; counter = 0; }else { ... } }while (index < (MAX_PAGE_NUMBER)) { ... } }if (size > 0) { ... } return NULL; }{ ... } /** * @brief Free previously allocated memory * @param Pointer to the allocated region. * @retval None *//* ... */ void k_free(void * p) { __IO uint8_t index = 0; uint8_t counter = 0, start = 0; /* Handle NULL pointers */ if (p == NULL) return; /* Find start Index */ start = ((((uint32_t)p - sizeof(uint32_t)) - memory_pool.mallocBase) >> 10); /* Retrieve segment size */ counter = memory_pool.size[start]; /* Set size to zero */ memory_pool.size[start] = 0; /* Deallocate memory region */ for (index = 0; index < counter; index++) { memory_pool.PageTable[start + index] = 0; }for (index = 0; index < counter; index++) { ... } return; }{ ... } /** * @} *//* ... */ /** * @} *//* ... */