-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.c
More file actions
194 lines (171 loc) · 4.64 KB
/
memory.c
File metadata and controls
194 lines (171 loc) · 4.64 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (C) 2020-2021
* "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
* Rafael Lee <rafaellee.img@gmail.com>
* Animula is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* Animula is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "memory.h"
/* NOTE:
* There're two types of stack in animula:
* 1. These are the same thing:
* a. dynamic stack
* b. runtime stack
* c. vm->stack
* 2. These are another same thing:
* a. static stack (or static storage)
* b. compile time stack
* c. __static_stack
* Its contents are confirmed in the compile time, and stored in
* the lightweight executable format (LEF). It'll be mapped to
* __static_stack when LEF is loaded by VM.
*/
u8_t *__static_stack = NULL;
u8_t *__global_array = NULL;
// malloc and free should work after init_ram_heap
void init_ram_heap (void)
{
#if defined ANIMULA_ZEPHYR
os_printk ("MM is managed by zephyr.\n");
#else
# ifndef ANIMULA_LINUX
os_printk ("MM is in raw mode.\n");
# endif
#endif
}
void *raw_malloc (size_t size)
{
#if defined ANIMULA_BOOTSTRAP
# error "raw_malloc hasn't been implemented yet!"
#endif
return NULL;
}
void raw_free (void *ptr)
{
#if defined ANIMULA_BOOTSTRAP
# error "raw_free hasn't been implemented yet!"
#endif
}
#ifdef FORCE_MEMORY_SIZE_TEST
struct memory_block
{
void *ptr;
size_t size;
};
static struct memory_block g_used_memory[MEMORY_TRACKER_ARRAY_LENGTH] = {0};
#endif
void *os_malloc (size_t size)
{
#ifdef FORCE_MEMORY_SIZE_TEST
uint32_t used_memory_size = 0;
for (size_t i = 0; i < MEMORY_TRACKER_ARRAY_LENGTH; i++)
{
used_memory_size += g_used_memory[i].size;
}
if ((used_memory_size + size) > MEMORY_HARD_LIMIT)
{
VM_DEBUG ("os_malloc: Failed to allocate memory!\n");
return NULL;
}
#endif
void *ptr = (void *)__malloc (size);
if (NULL == ptr)
{
VM_DEBUG ("os_malloc: Failed to allocate memory!\n");
}
#ifdef FORCE_MEMORY_SIZE_TEST
else
{
size_t i = 0;
for (; i < MEMORY_TRACKER_ARRAY_LENGTH; i++)
{
if (NULL == g_used_memory[i].ptr)
{
g_used_memory[i].ptr = ptr;
g_used_memory[i].size = size;
break;
}
}
if (MEMORY_TRACKER_ARRAY_LENGTH == i)
{
PANIC ("os_malloc: memory tracker used up! %p\n", ptr);
}
}
#endif
return ptr;
}
void *os_calloc (size_t n, size_t size)
{
#ifdef FORCE_MEMORY_SIZE_TEST
uint32_t used_memory_size = 0;
for (size_t i = 0; i < MEMORY_TRACKER_ARRAY_LENGTH; i++)
{
used_memory_size += g_used_memory[i].size;
}
if ((used_memory_size + n * size) > MEMORY_HARD_LIMIT)
{
VM_DEBUG ("os_calloc: Failed to allocate memory!\n");
return NULL;
}
#endif
void *ptr = (void *)__calloc (n, size);
if (NULL == ptr)
{
VM_DEBUG ("os_calloc: Failed to allocate memory!\n");
}
#ifdef FORCE_MEMORY_SIZE_TEST
else
{
size_t i = 0;
for (; i < MEMORY_TRACKER_ARRAY_LENGTH; i++)
{
if (NULL == g_used_memory[i].ptr)
{
g_used_memory[i].ptr = ptr;
g_used_memory[i].size = size;
break;
}
}
if (MEMORY_TRACKER_ARRAY_LENGTH == i)
{
PANIC ("os_calloc: memory tracker used up! %p\n", ptr);
}
}
#endif
return ptr;
}
void os_free (void *ptr)
{
// According to Linux library call, if ptr is NULL, no operation is performed.
// NULL ptr checking shall not be in this level
// Make it more strict to prevent future bug.
// FIXME: do not panic when free null pointer
if (NULL != ptr)
__free (ptr);
else
PANIC ("Free a NULL ptr\n");
#ifdef FORCE_MEMORY_SIZE_TEST
size_t i = 0;
for (; i < MEMORY_TRACKER_ARRAY_LENGTH; i++)
{
if (ptr == g_used_memory[i].ptr)
{
g_used_memory[i].ptr = NULL;
g_used_memory[i].size = 0;
break;
}
}
if (MEMORY_TRACKER_ARRAY_LENGTH == i)
{
PANIC ("os_free: freed a memory area not managed by us, ptr=%p\n", ptr);
}
#endif
}