11
11
#include <stdint.h>
12
12
#include "deep_loader.h"
13
13
#include "deep_log.h"
14
+ #include "deep_mem.h"
14
15
15
16
//read a value of specified type
16
17
#define READ_VALUE (Type , p ) \
@@ -59,7 +60,7 @@ static bool check_magic_number_and_version(uint8_t** p) {
59
60
}
60
61
61
62
static char * str_gen (const char * p , int32_t len ) {
62
- char * str = (char * )malloc ((len + 1 ) * sizeof (char ));
63
+ char * str = (char * )deep_malloc ((len + 1 ) * sizeof (char ));
63
64
memcpy (str , p , len );
64
65
str [len ] = '\0' ;
65
66
return str ;
@@ -71,8 +72,7 @@ static section_listnode* create_section_list(const uint8_t** p, int32_t size) {
71
72
section_listnode * section_list = NULL ;
72
73
section_listnode * current_section = NULL ;
73
74
while (buf < buf_end ) {
74
- section_listnode * section = (section_listnode * )malloc (sizeof (section_listnode ));
75
- memset (section , 0 , sizeof (section_listnode ));
75
+ section_listnode * section = (section_listnode * )deep_malloc (sizeof (section_listnode ));
76
76
if (section_list == NULL ) {
77
77
section_list = section ;
78
78
current_section = section ;
@@ -96,7 +96,7 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
96
96
uint32_t total_size = 0 ;
97
97
uint32_t type_count = 0 , param_count = 0 , ret_count = 0 ;
98
98
module -> type_count = type_count = read_leb_u32 ((uint8_t * * )& p );
99
- module -> type_section = (DEEPType * * )malloc (type_count * sizeof (DEEPType * ));
99
+ module -> type_section = (DEEPType * * )deep_malloc (type_count * sizeof (DEEPType * ));
100
100
for (int32_t i = 0 ; i < type_count ; i ++ ) {
101
101
if (READ_BYTE (p ) == 0x60 ) {
102
102
param_count = read_leb_u32 ((uint8_t * * )& p );
@@ -106,7 +106,7 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
106
106
p = p_tmp ;
107
107
total_size = 8 + param_count + ret_count ;
108
108
109
- module -> type_section [i ] = (DEEPType * )malloc (total_size );
109
+ module -> type_section [i ] = (DEEPType * )deep_malloc (total_size );
110
110
module -> type_section [i ]-> param_count = param_count ;
111
111
module -> type_section [i ]-> ret_count = ret_count ;
112
112
@@ -131,11 +131,10 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
131
131
const uint8_t * p_code_temp ;
132
132
if (func_count == code_func_count ) {
133
133
module -> function_count = func_count ;
134
- module -> func_section = (DEEPFunction * * )malloc (func_count * sizeof (DEEPFunction * ));
134
+ module -> func_section = (DEEPFunction * * )deep_malloc (func_count * sizeof (DEEPFunction * ));
135
135
136
136
for (uint32_t i = 0 ; i < func_count ; i ++ ) {
137
- func = module -> func_section [i ] = (DEEPFunction * )malloc (sizeof (DEEPFunction ));
138
- memset (func , 0 , sizeof (DEEPFunction ));
137
+ func = module -> func_section [i ] = (DEEPFunction * )deep_malloc (sizeof (DEEPFunction ));
139
138
type_index = read_leb_u32 ((uint8_t * * )& p );
140
139
code_size = read_leb_u32 ((uint8_t * * )& p_code );
141
140
p_code_temp = p_code ;
@@ -145,9 +144,9 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
145
144
if (local_set_count == 0 ) {
146
145
func -> localvars = NULL ;
147
146
} else {
148
- func -> localvars = (LocalVars * * )malloc (local_set_count * sizeof (LocalVars * ));
147
+ func -> localvars = (LocalVars * * )deep_malloc (local_set_count * sizeof (LocalVars * ));
149
148
for (uint32_t j = 0 ; j < local_set_count ; j ++ ) {
150
- local_set = func -> localvars [j ] = (LocalVars * )malloc (sizeof (LocalVars ));
149
+ local_set = func -> localvars [j ] = (LocalVars * )deep_malloc (sizeof (LocalVars ));
151
150
local_set -> count = read_leb_u32 ((uint8_t * * )& p_code );
152
151
local_set -> local_type = READ_BYTE (p_code );
153
152
}
@@ -164,10 +163,10 @@ static void decode_export_section(const uint8_t* p, DEEPModule* module) {
164
163
uint32_t name_len ;
165
164
DEEPExport * Export ;
166
165
module -> export_count = export_count ;
167
- module -> export_section = (DEEPExport * * )malloc (export_count * sizeof (DEEPExport * ));
166
+ module -> export_section = (DEEPExport * * )deep_malloc (export_count * sizeof (DEEPExport * ));
168
167
169
168
for (uint32_t i = 0 ; i < export_count ; i ++ ) {
170
- Export = module -> export_section [i ] = (DEEPExport * )malloc (sizeof (DEEPExport ));
169
+ Export = module -> export_section [i ] = (DEEPExport * )deep_malloc (sizeof (DEEPExport ));
171
170
name_len = read_leb_u32 ((uint8_t * * )& p );
172
171
Export -> name = str_gen (p , name_len );
173
172
p += name_len ;
@@ -181,10 +180,10 @@ static void decode_data_section(const uint8_t* p, DEEPModule* module) {
181
180
uint32_t data_count = read_leb_u32 ((uint8_t * * )& p );
182
181
DEEPData * Data ;
183
182
module -> data_count = data_count ;
184
- module -> data_section = (DEEPData * * )malloc (data_count * sizeof (DEEPData * ));
183
+ module -> data_section = (DEEPData * * )deep_malloc (data_count * sizeof (DEEPData * ));
185
184
186
185
for (uint32_t i = 0 ; i < data_count ; i ++ ) {
187
- Data = module -> data_section [i ] = (DEEPData * )malloc (sizeof (DEEPData ));
186
+ Data = module -> data_section [i ] = (DEEPData * )deep_malloc (sizeof (DEEPData ));
188
187
READ_BYTE (p );
189
188
p ++ ;
190
189
Data -> offset = read_leb_u32 ((uint8_t * * )& p );
@@ -265,17 +264,16 @@ DEEPModule* deep_load(uint8_t** p, int size) {
265
264
return NULL ;
266
265
}
267
266
size -= 8 ;
268
- DEEPModule * module = (DEEPModule * )malloc (sizeof (DEEPModule ));
267
+ DEEPModule * module = (DEEPModule * )deep_malloc (sizeof (DEEPModule ));
269
268
if (module == NULL ) {
270
269
error ("module malloc fail" );
271
270
return NULL ;
272
271
}
273
- memset (module , 0 , sizeof (module ));
274
272
decode_each_sections (module , section_list );
275
273
section_listnode * dummy = section_list , * q ;
276
274
while (dummy != NULL ) {
277
275
q = dummy -> next ;
278
- free (dummy );
276
+ deep_free (dummy );
279
277
dummy = q ;
280
278
}
281
279
section_list = NULL ;
@@ -285,31 +283,28 @@ DEEPModule* deep_load(uint8_t** p, int size) {
285
283
void module_free (DEEPModule * module ) {
286
284
uint32_t i ;
287
285
for (i = 0 ; i < module -> data_count ; i ++ ) {
288
- free (module -> data_section [i ]);
286
+ deep_free (module -> data_section [i ]);
289
287
}
290
- free (module -> data_section );
288
+ deep_free (module -> data_section );
291
289
for (i = 0 ; i < module -> type_count ; i ++ ) {
292
- free (module -> type_section [i ]);
290
+ deep_free (module -> type_section [i ]);
293
291
}
294
- free (module -> type_section );
292
+ deep_free (module -> type_section );
295
293
for (i = 0 ; i < module -> function_count ; i ++ ) {
296
- free (module -> func_section [i ]);
294
+ deep_free (module -> func_section [i ]-> localvars );
295
+ deep_free (module -> func_section [i ]);
297
296
}
298
- free (module -> func_section );
297
+ deep_free (module -> func_section );
299
298
for (i = 0 ; i < module -> export_count ; i ++ ) {
300
- free (module -> export_section [i ]-> name );
301
- free (module -> export_section [i ]);
299
+ deep_free (module -> export_section [i ]-> name );
300
+ deep_free (module -> export_section [i ]);
302
301
}
303
- free (module -> export_section );
304
- free (module );
302
+ deep_free (module -> export_section );
303
+ deep_free (module );
305
304
}
306
305
307
- //内存模块实现
308
- #define PAGESIZE 65536
309
-
310
306
uint8_t * init_memory (uint32_t min_page ) {
311
- uint8_t * mem = (uint8_t * )malloc (min_page * PAGESIZE );
312
- memset (mem , 0 , min_page * PAGESIZE );
307
+ uint8_t * mem = (uint8_t * )deep_malloc (min_page * PAGESIZE );
313
308
return mem ;
314
309
}
315
310
0 commit comments