Skip to content

Commit

Permalink
re-org ems mem allocator source codes, update prot_wamr.md (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
xujuntwt95329 authored Mar 30, 2020
1 parent 31feaa0 commit d9890d2
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 653 deletions.
5 changes: 4 additions & 1 deletion core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ enum {
#endif

/* Heap and stack profiling */
#define BEIHAI_ENABLE_MEMORY_PROFILING 0
#define BH_ENABLE_MEMORY_PROFILING 0

/* Heap verification */
#define BH_ENABLE_GC_VERIFY 0

/* Max app number of all modules */
#define MAX_APP_INSTALLATIONS 3
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ load_from_sections(AOTModule *module, AOTSection *sections,
return true;
}

#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
static void aot_free(void *ptr)
{
wasm_runtime_free(ptr);
Expand Down
37 changes: 19 additions & 18 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "bh_platform.h"
#include "mem_alloc.h"

#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0

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

/* Lock of the memory profile list */
static korp_mutex profile_lock;
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of BH_ENABLE_MEMORY_PROFILING */

#ifndef MALLOC_MEMORY_FROM_SYSTEM

Expand Down Expand Up @@ -58,7 +58,7 @@ wasm_memory_init_with_pool(void *mem, unsigned int bytes)
if (_allocator) {
memory_mode = MEMORY_MODE_POOL;
pool_allocator = _allocator;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
os_mutex_init(&profile_lock);
#endif
global_pool_size = bytes;
Expand All @@ -78,7 +78,7 @@ wasm_memory_init_with_allocator(void *_malloc_func,
malloc_func = _malloc_func;
realloc_func = _realloc_func;
free_func = _free_func;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
os_mutex_init(&profile_lock);
#endif
return true;
Expand Down Expand Up @@ -108,7 +108,7 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
void
wasm_runtime_memory_destroy()
{
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
os_mutex_destroy(&profile_lock);
#endif
if (memory_mode == MEMORY_MODE_POOL)
Expand Down Expand Up @@ -166,12 +166,21 @@ wasm_runtime_free(void *ptr)
}
}

#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0

void
memory_profile_print(const char *file, int line,
const char *func, int alloc)
{
os_printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}

void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
{
void *p = wasm_rutime_malloc(size + 8);
void *p = wasm_runtime_malloc(size + 8);

if (p) {
memory_profile_t *profile;
Expand Down Expand Up @@ -292,15 +301,7 @@ void memory_usage_summarize()
os_mutex_unlock(&profile_lock);
}

void
memory_profile_print(const char *file, int line,
const char *func, int alloc)
{
os_printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}

#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of BH_ENABLE_MEMORY_PROFILING */

#else /* else of MALLOC_MEMORY_FROM_SYSTEM */

Expand All @@ -324,7 +325,7 @@ wasm_runtime_free(void *ptr)
free(ptr);
}

#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
Expand Down Expand Up @@ -366,6 +367,6 @@ wasm_runtime_free_profile(const char *file, int line,
if (ptr)
free(ptr);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of BH_ENABLE_MEMORY_PROFILING */
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/

10 changes: 9 additions & 1 deletion core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
#endif

read_leb_uint32(p, p_end, memory->flags);
read_leb_uint32(p, p_end, memory->init_page_count);
Expand Down Expand Up @@ -539,8 +543,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
#endif

read_leb_uint32(p, p_end, memory->flags);
read_leb_uint32(p, p_end, memory->init_page_count);
Expand Down Expand Up @@ -1694,7 +1702,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
return true;
}

#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#if BH_ENABLE_MEMORY_PROFILING != 0
static void wasm_loader_free(void *ptr)
{
wasm_runtime_free(ptr);
Expand Down
Loading

0 comments on commit d9890d2

Please sign in to comment.