Skip to content

Commit d9890d2

Browse files
re-org ems mem allocator source codes, update prot_wamr.md (#217)
1 parent 31feaa0 commit d9890d2

File tree

11 files changed

+364
-653
lines changed

11 files changed

+364
-653
lines changed

core/config.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ enum {
118118
#endif
119119

120120
/* Heap and stack profiling */
121-
#define BEIHAI_ENABLE_MEMORY_PROFILING 0
121+
#define BH_ENABLE_MEMORY_PROFILING 0
122+
123+
/* Heap verification */
124+
#define BH_ENABLE_GC_VERIFY 0
122125

123126
/* Max app number of all modules */
124127
#define MAX_APP_INSTALLATIONS 3

core/iwasm/aot/aot_loader.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ load_from_sections(AOTModule *module, AOTSection *sections,
16021602
return true;
16031603
}
16041604

1605-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
1605+
#if BH_ENABLE_MEMORY_PROFILING != 0
16061606
static void aot_free(void *ptr)
16071607
{
16081608
wasm_runtime_free(ptr);

core/iwasm/common/wasm_memory.c

+19-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "bh_platform.h"
88
#include "mem_alloc.h"
99

10-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
10+
#if BH_ENABLE_MEMORY_PROFILING != 0
1111

1212
/* Memory profile data of a function */
1313
typedef struct memory_profile {
@@ -30,7 +30,7 @@ static memory_profile_t *memory_profiles_list = NULL;
3030

3131
/* Lock of the memory profile list */
3232
static korp_mutex profile_lock;
33-
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
33+
#endif /* end of BH_ENABLE_MEMORY_PROFILING */
3434

3535
#ifndef MALLOC_MEMORY_FROM_SYSTEM
3636

@@ -58,7 +58,7 @@ wasm_memory_init_with_pool(void *mem, unsigned int bytes)
5858
if (_allocator) {
5959
memory_mode = MEMORY_MODE_POOL;
6060
pool_allocator = _allocator;
61-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
61+
#if BH_ENABLE_MEMORY_PROFILING != 0
6262
os_mutex_init(&profile_lock);
6363
#endif
6464
global_pool_size = bytes;
@@ -78,7 +78,7 @@ wasm_memory_init_with_allocator(void *_malloc_func,
7878
malloc_func = _malloc_func;
7979
realloc_func = _realloc_func;
8080
free_func = _free_func;
81-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
81+
#if BH_ENABLE_MEMORY_PROFILING != 0
8282
os_mutex_init(&profile_lock);
8383
#endif
8484
return true;
@@ -108,7 +108,7 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
108108
void
109109
wasm_runtime_memory_destroy()
110110
{
111-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
111+
#if BH_ENABLE_MEMORY_PROFILING != 0
112112
os_mutex_destroy(&profile_lock);
113113
#endif
114114
if (memory_mode == MEMORY_MODE_POOL)
@@ -166,12 +166,21 @@ wasm_runtime_free(void *ptr)
166166
}
167167
}
168168

169-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
169+
#if BH_ENABLE_MEMORY_PROFILING != 0
170+
171+
void
172+
memory_profile_print(const char *file, int line,
173+
const char *func, int alloc)
174+
{
175+
os_printf("location:%s@%d:used:%d:contribution:%d\n",
176+
func, line, memory_in_use, alloc);
177+
}
178+
170179
void *
171180
wasm_runtime_malloc_profile(const char *file, int line,
172181
const char *func, unsigned int size)
173182
{
174-
void *p = wasm_rutime_malloc(size + 8);
183+
void *p = wasm_runtime_malloc(size + 8);
175184

176185
if (p) {
177186
memory_profile_t *profile;
@@ -292,15 +301,7 @@ void memory_usage_summarize()
292301
os_mutex_unlock(&profile_lock);
293302
}
294303

295-
void
296-
memory_profile_print(const char *file, int line,
297-
const char *func, int alloc)
298-
{
299-
os_printf("location:%s@%d:used:%d:contribution:%d\n",
300-
func, line, memory_in_use, alloc);
301-
}
302-
303-
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
304+
#endif /* end of BH_ENABLE_MEMORY_PROFILING */
304305

305306
#else /* else of MALLOC_MEMORY_FROM_SYSTEM */
306307

@@ -324,7 +325,7 @@ wasm_runtime_free(void *ptr)
324325
free(ptr);
325326
}
326327

327-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
328+
#if BH_ENABLE_MEMORY_PROFILING != 0
328329
void *
329330
wasm_runtime_malloc_profile(const char *file, int line,
330331
const char *func, unsigned int size)
@@ -366,6 +367,6 @@ wasm_runtime_free_profile(const char *file, int line,
366367
if (ptr)
367368
free(ptr);
368369
}
369-
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
370+
#endif /* end of BH_ENABLE_MEMORY_PROFILING */
370371
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/
371372

core/iwasm/interpreter/wasm_loader.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
492492
{
493493
const uint8 *p = *p_buf, *p_end = buf_end;
494494
uint32 pool_size = wasm_runtime_memory_pool_size();
495+
#if WASM_ENABLE_APP_FRAMEWORK != 0
495496
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
496497
/ DEFAULT_NUM_BYTES_PER_PAGE;
498+
#else
499+
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
500+
#endif
497501

498502
read_leb_uint32(p, p_end, memory->flags);
499503
read_leb_uint32(p, p_end, memory->init_page_count);
@@ -539,8 +543,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
539543
{
540544
const uint8 *p = *p_buf, *p_end = buf_end;
541545
uint32 pool_size = wasm_runtime_memory_pool_size();
546+
#if WASM_ENABLE_APP_FRAMEWORK != 0
542547
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
543548
/ DEFAULT_NUM_BYTES_PER_PAGE;
549+
#else
550+
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
551+
#endif
544552

545553
read_leb_uint32(p, p_end, memory->flags);
546554
read_leb_uint32(p, p_end, memory->init_page_count);
@@ -1694,7 +1702,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
16941702
return true;
16951703
}
16961704

1697-
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
1705+
#if BH_ENABLE_MEMORY_PROFILING != 0
16981706
static void wasm_loader_free(void *ptr)
16991707
{
17001708
wasm_runtime_free(ptr);

0 commit comments

Comments
 (0)