Skip to content

Commit bdb5371

Browse files
authored
Migrate to deepmem (#3)
1 parent e8902da commit bdb5371

File tree

8 files changed

+971
-49
lines changed

8 files changed

+971
-49
lines changed

include/deep_loader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#define SECTION_TYPE_CODE 10
2626
#define SECTION_TYPE_DATA 11
2727

28+
//内存模块实现
29+
#define PAGESIZE 65536
30+
2831
//
2932
//type item
3033
typedef struct DEEPType

include/deep_mem.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef _DEEP_MEM_ALLOC_H
2+
#define _DEEP_MEM_ALLOC_H
3+
4+
#include <stdbool.h>
5+
6+
#define FAST_BIN_LENGTH (8) /* eight size options for fast bins */
7+
#define FAST_BIN_MAX_SIZE (64) /* 8 * 8 bytes */
8+
#define SORTED_BIN_MIN_SIZE (FAST_BIN_MAX_SIZE + 8) /* 64 + 8 bytes */
9+
10+
#define A_FLAG_OFFSET (0) /* is allocated */
11+
#define A_FLAG_MASK (1 << A_FLAG_OFFSET)
12+
#define P_FLAG_OFFSET (1) /* is previous block allocated */
13+
#define P_FLAG_MASK (1 << P_FLAG_OFFSET)
14+
#define BLOCK_SIZE_MASK (0xFFFFFFFF - A_FLAG_MASK - P_FLAG_MASK)
15+
#define REMAINDER_SIZE_MASK ((0xffffffff << 32) & BLOCK_SIZE_MASK)
16+
17+
#define SORTED_BLOCK_INDICES_LEVEL (13)
18+
19+
/* Align the size up to a multiple of eight*/
20+
#define ALIGN_MEM_SIZE(size) (((size + 0x7) >> 3) << 3)
21+
/* Align the size down to a multiple of eight */
22+
#define ALIGN_MEM_SIZE_TRUNC(size) ((size >> 3) << 3)
23+
24+
typedef void *mem_t;
25+
typedef uint64_t mem_size_t;
26+
typedef uint32_t block_head_t;
27+
typedef uint32_t block_size_t;
28+
29+
/* For storing small blocks of memory */
30+
typedef struct fast_block
31+
{
32+
block_head_t head;
33+
union
34+
{
35+
struct fast_block *next;
36+
void *payload;
37+
} payload;
38+
} fast_block_t;
39+
40+
typedef struct sorted_block
41+
{
42+
block_head_t head;
43+
union
44+
{
45+
struct
46+
{
47+
int32_t pred_offset;
48+
int32_t succ_offset;
49+
uint32_t level_of_indices;
50+
// bigger array index corresponds to lower index in skip list,
51+
// i.e., skipping less nodes in the skip list
52+
// 0 means this node is the last one in this level of index.
53+
// offsets[SORTED_BLOCK_INDICES_LEVEL - 1] is the level where each node
54+
// is connected one by one consecutively.
55+
// The skip list is in ascending order by block's size.
56+
int32_t offsets[SORTED_BLOCK_INDICES_LEVEL];
57+
// padding
58+
// uint32_t footer;
59+
} info;
60+
void *payload;
61+
} payload;
62+
} sorted_block_t;
63+
64+
typedef struct mem_pool
65+
{
66+
uint64_t free_memory;
67+
union
68+
{
69+
uint64_t _padding;
70+
sorted_block_t *addr;
71+
} sorted_block;
72+
union
73+
{
74+
uint64_t _remainder_block_head_padding;
75+
block_head_t *remainder_block_head; /* The head of remainder */
76+
};
77+
union
78+
{
79+
uint64_t _remainder_block_end_padding;
80+
void *remainder_block_end; /* The address of the last byte in remainder */
81+
}; /* should not be dereferenced */
82+
union
83+
{
84+
uint64_t _padding;
85+
fast_block_t *addr;
86+
} fast_bins[FAST_BIN_LENGTH];
87+
} mem_pool_t;
88+
89+
bool deep_mem_init (void *mem, uint32_t size);
90+
void deep_mem_destroy (void);
91+
void *deep_malloc (uint32_t size);
92+
void *deep_realloc (void *ptr, uint32_t size);
93+
void deep_free (void *ptr);
94+
bool deep_mem_migrate (void *new_mem, uint32_t size);
95+
96+
#endif /* _DEEP_MEM_ALLOC_H */

include/random.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef _VM_INCLUDE_RANDOM_H
2+
#define _VM_INCLUDE_RANDOM_H
3+
4+
#include <stdint.h>
5+
6+
uint64_t next (void);
7+
8+
/* This is the jump function for the generator. It is equivalent
9+
to 2^64 calls to next(); it can be used to generate 2^64
10+
non-overlapping subsequences for parallel computations. */
11+
void jump (void);
12+
13+
/* This is the long-jump function for the generator. It is equivalent to
14+
2^96 calls to next(); it can be used to generate 2^32 starting points,
15+
from each of which jump() will generate 2^32 non-overlapping
16+
subsequences for parallel distributed computations. */
17+
void long_jump (void);
18+
19+
#endif /* _VM_INCLUDE_RANDOM_H */

src/deep_interp.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include "deep_interp.h"
1010
#include "deep_loader.h"
11+
#include "deep_mem.h"
1112
#include "deep_opcode.h"
1213

1314
#define popS32() (int32_t)*(--sp)
@@ -26,13 +27,13 @@
2627

2728
//创建操作数栈
2829
DEEPStack *stack_cons(void) {
29-
DEEPStack *stack = (DEEPStack *) malloc(sizeof(DEEPStack));
30+
DEEPStack *stack = (DEEPStack *) deep_malloc(sizeof(DEEPStack));
3031
if (stack == NULL) {
3132
printf("Operand stack creation failed!\r\n");
3233
return NULL;
3334
}
3435
stack->capacity = STACK_CAPACITY;
35-
stack->sp = (uint32_t *) malloc(sizeof(uint32_t) * STACK_CAPACITY);
36+
stack->sp = (uint32_t *) deep_malloc(sizeof(uint32_t) * STACK_CAPACITY);
3637
if (stack->sp == NULL) {
3738
printf("Malloc area for stack error!\r\n");
3839
}
@@ -42,8 +43,8 @@ DEEPStack *stack_cons(void) {
4243

4344
//销毁操作数栈
4445
void stack_free(DEEPStack *stack) {
45-
free(stack->sp);
46-
free(stack);
46+
deep_free(stack->sp);
47+
deep_free(stack);
4748
}
4849

4950
//执行代码块指令
@@ -289,7 +290,7 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
289290
uint32_t ret_num = deepType->ret_count;
290291

291292
// current_env->sp-=param_num;//操作数栈指针下移
292-
current_env->local_vars = (uint32_t *) malloc(sizeof(uint32_t) * param_num);
293+
current_env->local_vars = (uint32_t *) deep_malloc(sizeof(uint32_t) * param_num);
293294
uint32_t vars_temp = param_num;
294295
while (vars_temp > 0) {
295296
uint32_t temp = *(--current_env->sp);
@@ -300,7 +301,7 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
300301
LocalVars **locals = func->localvars;
301302

302303
//为func函数创建帧
303-
DEEPInterpFrame *frame = (DEEPInterpFrame *) malloc(sizeof(DEEPInterpFrame));
304+
DEEPInterpFrame *frame = (DEEPInterpFrame *) deep_malloc(sizeof(DEEPInterpFrame));
304305
if (frame == NULL) {
305306
printf("Malloc area for normal_frame error!\r\n");
306307
}
@@ -319,8 +320,8 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
319320
//执行完毕退栈
320321
current_env->cur_frame = frame->prev_frame;
321322
//释放掉局部变量
322-
free(current_env->local_vars);
323-
free(frame);
323+
deep_free(current_env->local_vars);
324+
deep_free(frame);
324325
return;
325326
}
326327

@@ -345,7 +346,7 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
345346
DEEPFunction *main_func = module->func_section[main_index];//module.start_index记录了main函数索引
346347

347348
//为main函数创建帧
348-
DEEPInterpFrame *main_frame = (DEEPInterpFrame *) malloc(sizeof(struct DEEPInterpFrame));
349+
DEEPInterpFrame *main_frame = (DEEPInterpFrame *) deep_malloc(sizeof(struct DEEPInterpFrame));
349350
if (main_frame == NULL) {
350351
printf("Malloc area for main_frame error!\r\n");
351352
}
@@ -359,8 +360,8 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
359360
//执行frame中函数
360361
//sp要下移,栈顶元素即为函数参数
361362
exec_instructions(current_env, module);
362-
free(main_frame);
363+
deep_free(main_frame);
363364

364365
//返回栈顶元素
365366
return *(current_env->sp - 1);
366-
}
367+
}

src/deep_loader.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdint.h>
1212
#include "deep_loader.h"
1313
#include "deep_log.h"
14+
#include "deep_mem.h"
1415

1516
//read a value of specified type
1617
#define READ_VALUE(Type, p) \
@@ -59,7 +60,7 @@ static bool check_magic_number_and_version(uint8_t** p) {
5960
}
6061

6162
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));
6364
memcpy(str, p, len);
6465
str[len] = '\0';
6566
return str;
@@ -71,8 +72,7 @@ static section_listnode* create_section_list(const uint8_t** p, int32_t size) {
7172
section_listnode* section_list = NULL;
7273
section_listnode* current_section = NULL;
7374
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));
7676
if (section_list == NULL) {
7777
section_list = section;
7878
current_section = section;
@@ -96,7 +96,7 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
9696
uint32_t total_size = 0;
9797
uint32_t type_count = 0, param_count = 0, ret_count = 0;
9898
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*));
100100
for (int32_t i = 0; i < type_count; i++) {
101101
if (READ_BYTE(p) == 0x60) {
102102
param_count = read_leb_u32((uint8_t**)&p);
@@ -106,7 +106,7 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
106106
p = p_tmp;
107107
total_size = 8 + param_count + ret_count;
108108

109-
module->type_section[i] = (DEEPType*)malloc(total_size);
109+
module->type_section[i] = (DEEPType*)deep_malloc(total_size);
110110
module->type_section[i]->param_count = param_count;
111111
module->type_section[i]->ret_count = ret_count;
112112

@@ -131,11 +131,10 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
131131
const uint8_t* p_code_temp;
132132
if (func_count == code_func_count) {
133133
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*));
135135

136136
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));
139138
type_index = read_leb_u32((uint8_t**)&p);
140139
code_size = read_leb_u32((uint8_t**)&p_code);
141140
p_code_temp = p_code;
@@ -145,9 +144,9 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
145144
if (local_set_count == 0) {
146145
func->localvars = NULL;
147146
} else {
148-
func->localvars = (LocalVars**)malloc(local_set_count * sizeof(LocalVars*));
147+
func->localvars = (LocalVars**)deep_malloc(local_set_count * sizeof(LocalVars*));
149148
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));
151150
local_set->count = read_leb_u32((uint8_t**)&p_code);
152151
local_set->local_type = READ_BYTE(p_code);
153152
}
@@ -164,10 +163,10 @@ static void decode_export_section(const uint8_t* p, DEEPModule* module) {
164163
uint32_t name_len;
165164
DEEPExport* Export;
166165
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*));
168167

169168
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));
171170
name_len = read_leb_u32((uint8_t**)&p);
172171
Export->name = str_gen(p, name_len);
173172
p += name_len;
@@ -181,10 +180,10 @@ static void decode_data_section(const uint8_t* p, DEEPModule* module) {
181180
uint32_t data_count = read_leb_u32((uint8_t**)&p);
182181
DEEPData* Data;
183182
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*));
185184

186185
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));
188187
READ_BYTE(p);
189188
p ++;
190189
Data->offset = read_leb_u32((uint8_t**)&p);
@@ -265,17 +264,16 @@ DEEPModule* deep_load(uint8_t** p, int size) {
265264
return NULL;
266265
}
267266
size -= 8;
268-
DEEPModule* module = (DEEPModule*)malloc(sizeof(DEEPModule));
267+
DEEPModule* module = (DEEPModule*)deep_malloc(sizeof(DEEPModule));
269268
if(module == NULL) {
270269
error("module malloc fail");
271270
return NULL;
272271
}
273-
memset(module, 0, sizeof(module));
274272
decode_each_sections(module, section_list);
275273
section_listnode* dummy = section_list, *q;
276274
while(dummy != NULL) {
277275
q = dummy->next;
278-
free(dummy);
276+
deep_free(dummy);
279277
dummy = q;
280278
}
281279
section_list = NULL;
@@ -285,31 +283,28 @@ DEEPModule* deep_load(uint8_t** p, int size) {
285283
void module_free(DEEPModule *module) {
286284
uint32_t i;
287285
for (i = 0; i < module->data_count; i++) {
288-
free(module->data_section[i]);
286+
deep_free(module->data_section[i]);
289287
}
290-
free(module->data_section);
288+
deep_free(module->data_section);
291289
for (i = 0; i < module->type_count; i++) {
292-
free(module->type_section[i]);
290+
deep_free(module->type_section[i]);
293291
}
294-
free(module->type_section);
292+
deep_free(module->type_section);
295293
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]);
297296
}
298-
free(module->func_section);
297+
deep_free(module->func_section);
299298
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]);
302301
}
303-
free(module->export_section);
304-
free(module);
302+
deep_free(module->export_section);
303+
deep_free(module);
305304
}
306305

307-
//内存模块实现
308-
#define PAGESIZE 65536
309-
310306
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);
313308
return mem;
314309
}
315310

0 commit comments

Comments
 (0)