use WASM_MEM_ALLOC_WITH_USAGE option define custom allocator

which could know the memory's usage is for linear memory or not
This commit is contained in:
Dongsheng.Yan 2024-04-08 14:49:44 +08:00
parent b11dbcba0a
commit e274d83c1c
6 changed files with 63 additions and 7 deletions

View File

@ -554,3 +554,6 @@ else ()
# Disable aot intrinsics for interp, fast-jit and llvm-jit # Disable aot intrinsics for interp, fast-jit and llvm-jit
add_definitions (-DWASM_ENABLE_AOT_INTRINSICS=0) add_definitions (-DWASM_ENABLE_AOT_INTRINSICS=0)
endif () endif ()
if (WAMR_MEM_ALLOC_WITH_USAGE EQUAL 1)
add_definitions(-DWASM_MEM_ALLOC_WITH_USAGE=1)
endif()

View File

@ -587,4 +587,8 @@
#define WASM_TABLE_MAX_SIZE 1024 #define WASM_TABLE_MAX_SIZE 1024
#endif #endif
#ifndef WASM_MEM_ALLOC_WITH_USAGE
#define WASM_MEM_ALLOC_WITH_USAGE 0
#endif
#endif /* end of _CONFIG_H_ */ #endif /* end of _CONFIG_H_ */

View File

@ -27,7 +27,12 @@ static mem_allocator_t pool_allocator = NULL;
static enlarge_memory_error_callback_t enlarge_memory_error_cb; static enlarge_memory_error_callback_t enlarge_memory_error_cb;
static void *enlarge_memory_error_user_data; static void *enlarge_memory_error_user_data;
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
static void *(*malloc_func)(mem_alloc_usage_t usage, unsigned int size) = NULL;
static void *(*realloc_func)(mem_alloc_usage_t usage, void *ptr,
unsigned int size) = NULL;
static void (*free_func)(mem_alloc_usage_t usage, void *ptr) = NULL;
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
static void *allocator_user_data = NULL; static void *allocator_user_data = NULL;
static void *(*malloc_func)(void *user_data, unsigned int size) = NULL; static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
static void *(*realloc_func)(void *user_data, void *ptr, static void *(*realloc_func)(void *user_data, void *ptr,
@ -177,7 +182,9 @@ wasm_runtime_malloc_internal(unsigned int size)
return mem_allocator_malloc(pool_allocator, size); return mem_allocator_malloc(pool_allocator, size);
} }
else if (memory_mode == MEMORY_MODE_ALLOCATOR) { else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
return malloc_func(Alloc_For_Runtime, size);
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
return malloc_func(allocator_user_data, size); return malloc_func(allocator_user_data, size);
#else #else
return malloc_func(size); return malloc_func(size);
@ -201,7 +208,9 @@ wasm_runtime_realloc_internal(void *ptr, unsigned int size)
} }
else if (memory_mode == MEMORY_MODE_ALLOCATOR) { else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
if (realloc_func) if (realloc_func)
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
return realloc_func(Alloc_For_Runtime, ptr, size);
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
return realloc_func(allocator_user_data, ptr, size); return realloc_func(allocator_user_data, ptr, size);
#else #else
return realloc_func(ptr, size); return realloc_func(ptr, size);
@ -233,7 +242,9 @@ wasm_runtime_free_internal(void *ptr)
mem_allocator_free(pool_allocator, ptr); mem_allocator_free(pool_allocator, ptr);
} }
else if (memory_mode == MEMORY_MODE_ALLOCATOR) { else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
free_func(Alloc_For_Runtime, ptr);
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
free_func(allocator_user_data, ptr); free_func(allocator_user_data, ptr);
#else #else
free_func(ptr); free_func(ptr);
@ -796,6 +807,12 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
} }
} }
#if WASM_MEM_ALLOC_WITH_USAGE != 0
if (!(memory_data_new = realloc_func(Alloc_For_LinearMemory, memory_data_old, total_size_new))) {
ret = false;
goto return_func;
}
#else
if (!(memory_data_new = if (!(memory_data_new =
wasm_mremap_linear_memory(memory_data_old, total_size_old, wasm_mremap_linear_memory(memory_data_old, total_size_old,
total_size_new, total_size_new))) { total_size_new, total_size_new))) {
@ -803,6 +820,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
goto return_func; goto return_func;
} }
#endif
if (heap_size > 0) { if (heap_size > 0) {
if (mem_allocator_migrate(memory->heap_handle, if (mem_allocator_migrate(memory->heap_handle,
(char *)heap_data_old (char *)heap_data_old
@ -903,8 +921,14 @@ wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
#else #else
map_size = 8 * (uint64)BH_GB; map_size = 8 * (uint64)BH_GB;
#endif #endif
#if WASM_MEM_ALLOC_WITH_USAGE != 0
free_func(Alloc_For_LinearMemory, memory_inst->memory_data);
#else
wasm_munmap_linear_memory(memory_inst->memory_data, wasm_munmap_linear_memory(memory_inst->memory_data,
memory_inst->memory_data_size, map_size); memory_inst->memory_data_size, map_size);
#endif
memory_inst->memory_data = NULL; memory_inst->memory_data = NULL;
} }
@ -954,9 +978,15 @@ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
*memory_data_size = align_as_and_cast(*memory_data_size, page_size); *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
if (map_size > 0) { if (map_size > 0) {
#if WASM_MEM_ALLOC_WITH_USAGE != 0
if (!(*data = malloc_func(Alloc_For_LinearMemory, *memory_data_size))) {
return BHT_ERROR;
}
#else
if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) { if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
return BHT_ERROR; return BHT_ERROR;
} }
#endif
} }
return BHT_OK; return BHT_OK;

View File

@ -107,6 +107,11 @@ typedef enum {
Alloc_With_System_Allocator, Alloc_With_System_Allocator,
} mem_alloc_type_t; } mem_alloc_type_t;
typedef enum {
Alloc_For_Runtime,
Alloc_For_LinearMemory
} mem_alloc_usage_t;
/* Memory allocator option */ /* Memory allocator option */
typedef union MemAllocOption { typedef union MemAllocOption {
struct { struct {

View File

@ -123,6 +123,11 @@ if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
set (WAMR_BUILD_SIMD 0) set (WAMR_BUILD_SIMD 0)
endif () endif ()
if (NOT DEFINED WAMR_MEM_ALLOC_WITH_USAGE)
# Disable Debug feature by default
set (WAMR_MEM_ALLOC_WITH_USAGE 1)
endif ()
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..) set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

View File

@ -445,31 +445,40 @@ static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#else #else
static void * static void *
malloc_func( malloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage,
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
unsigned int size) unsigned int size)
{ {
//printf("current allocated for %d\n", usage);
return malloc(size); return malloc(size);
} }
static void * static void *
realloc_func( realloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage,
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
void *ptr, unsigned int size) void *ptr, unsigned int size)
{ {
//printf("current realloc for %d\n", usage);
return realloc(ptr, size); return realloc(ptr, size);
} }
static void static void
free_func( free_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage,
#elif WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
void *ptr) void *ptr)
{ {
//printf("current free for %d\n", usage);
free(ptr); free(ptr);
} }
#endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */ #endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */