@@ -93,8 +93,8 @@ static section_listnode* create_section_list(const uint8_t** p, int32_t size) {
93
93
static void decode_type_section (const uint8_t * p , DEEPModule * module ) {
94
94
95
95
const uint8_t * p_tmp ;
96
- int32_t total_size = 0 ;
97
- int32_t type_count = 0 , param_count = 0 , ret_count = 0 ;
96
+ uint32_t total_size = 0 ;
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
99
module -> type_section = (DEEPType * * )malloc (type_count * sizeof (DEEPType * ));
100
100
for (int32_t i = 0 ; i < type_count ; i ++ ) {
@@ -110,11 +110,11 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
110
110
module -> type_section [i ]-> param_count = param_count ;
111
111
module -> type_section [i ]-> ret_count = ret_count ;
112
112
113
- for (int32_t j = 0 ; j < param_count ; j ++ ) {
113
+ for (uint32_t j = 0 ; j < param_count ; j ++ ) {
114
114
module -> type_section [i ]-> type [j ] = READ_BYTE (p );
115
115
}
116
116
read_leb_u32 ((uint8_t * * )& p );
117
- for (int32_t j = 0 ; j < ret_count ; j ++ ) {
117
+ for (uint32_t j = 0 ; j < ret_count ; j ++ ) {
118
118
module -> type_section [i ]-> type [param_count + j ] = READ_BYTE (p );
119
119
}
120
120
}
@@ -123,17 +123,17 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
123
123
124
124
//读取函数段
125
125
static void decode_func_section (const uint8_t * p , DEEPModule * module ,const uint8_t * p_code ) {
126
- int32_t func_count = read_leb_u32 ((uint8_t * * )& p );
127
- int32_t code_func_count = read_leb_u32 ((uint8_t * * )& p_code );
128
- int32_t type_index , code_size , local_set_count ;
126
+ uint32_t func_count = read_leb_u32 ((uint8_t * * )& p );
127
+ uint32_t code_func_count = read_leb_u32 ((uint8_t * * )& p_code );
128
+ uint32_t type_index , code_size , local_set_count ;
129
129
DEEPFunction * func ;
130
130
LocalVars * local_set ;
131
131
const uint8_t * p_code_temp ;
132
132
if (func_count == code_func_count ) {
133
133
module -> function_count = func_count ;
134
134
module -> func_section = (DEEPFunction * * )malloc (func_count * sizeof (DEEPFunction * ));
135
135
136
- for (int32_t i = 0 ; i < func_count ; i ++ ) {
136
+ for (uint32_t i = 0 ; i < func_count ; i ++ ) {
137
137
func = module -> func_section [i ] = (DEEPFunction * )malloc (sizeof (DEEPFunction ));
138
138
memset (func , 0 , sizeof (DEEPFunction ));
139
139
type_index = read_leb_u32 ((uint8_t * * )& p );
@@ -146,7 +146,7 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
146
146
func -> localvars = NULL ;
147
147
} else {
148
148
func -> localvars = (LocalVars * * )malloc (local_set_count * sizeof (LocalVars * ));
149
- for (int32_t j = 0 ; j < local_set_count ; j ++ ) {
149
+ for (uint32_t j = 0 ; j < local_set_count ; j ++ ) {
150
150
local_set = func -> localvars [j ] = (LocalVars * )malloc (sizeof (LocalVars ));
151
151
local_set -> count = read_leb_u32 ((uint8_t * * )& p_code );
152
152
local_set -> local_type = READ_BYTE (p_code );
@@ -160,13 +160,13 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
160
160
161
161
//读取导出段
162
162
static void decode_export_section (const uint8_t * p , DEEPModule * module ) {
163
- int32_t export_count = read_leb_u32 ((uint8_t * * )& p );
164
- int32_t name_len ;
163
+ uint32_t export_count = read_leb_u32 ((uint8_t * * )& p );
164
+ uint32_t name_len ;
165
165
DEEPExport * Export ;
166
166
module -> export_count = export_count ;
167
167
module -> export_section = (DEEPExport * * )malloc (export_count * sizeof (DEEPExport * ));
168
168
169
- for (int32_t i = 0 ; i < export_count ; i ++ ) {
169
+ for (uint32_t i = 0 ; i < export_count ; i ++ ) {
170
170
Export = module -> export_section [i ] = (DEEPExport * )malloc (sizeof (DEEPExport ));
171
171
name_len = read_leb_u32 ((uint8_t * * )& p );
172
172
Export -> name = str_gen (p , name_len );
@@ -178,12 +178,12 @@ static void decode_export_section(const uint8_t* p, DEEPModule* module) {
178
178
179
179
//读取数据段
180
180
static void decode_data_section (const uint8_t * p , DEEPModule * module ) {
181
- int32_t data_count = read_leb_u32 ((uint8_t * * )& p );
181
+ uint32_t data_count = read_leb_u32 ((uint8_t * * )& p );
182
182
DEEPData * Data ;
183
183
module -> data_count = data_count ;
184
184
module -> data_section = (DEEPData * * )malloc (data_count * sizeof (DEEPData * ));
185
185
186
- for (int32_t i = 0 ; i < data_count ; i ++ ) {
186
+ for (uint32_t i = 0 ; i < data_count ; i ++ ) {
187
187
Data = module -> data_section [i ] = (DEEPData * )malloc (sizeof (DEEPData ));
188
188
READ_BYTE (p );
189
189
p ++ ;
@@ -282,6 +282,28 @@ DEEPModule* deep_load(uint8_t** p, int size) {
282
282
return module ;
283
283
}
284
284
285
+ void module_free (DEEPModule * module ) {
286
+ uint32_t i ;
287
+ for (i = 0 ; i < module -> data_count ; i ++ ) {
288
+ free (module -> data_section [i ]);
289
+ }
290
+ free (module -> data_section );
291
+ for (i = 0 ; i < module -> type_count ; i ++ ) {
292
+ free (module -> type_section [i ]);
293
+ }
294
+ free (module -> type_section );
295
+ for (i = 0 ; i < module -> function_count ; i ++ ) {
296
+ free (module -> func_section [i ]);
297
+ }
298
+ free (module -> func_section );
299
+ for (i = 0 ; i < module -> export_count ; i ++ ) {
300
+ free (module -> export_section [i ]-> name );
301
+ free (module -> export_section [i ]);
302
+ }
303
+ free (module -> export_section );
304
+ free (module );
305
+ }
306
+
285
307
//内存模块实现
286
308
#define PAGESIZE 65536
287
309
@@ -382,4 +404,3 @@ void type_section_dump(DEEPModule* module) {
382
404
}
383
405
printf ("%s\n" , "========================================================" );
384
406
}
385
-
0 commit comments