Use user defined malloc/free functions for user defined memory allocator (#2717)

Reported in #2617.
This commit is contained in:
Wenyong Huang 2023-11-03 15:23:31 +08:00 committed by GitHub
parent e161205a27
commit cc23c7ee7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 6 deletions

View File

@ -464,7 +464,37 @@ moudle_destroyer(uint8 *buffer, uint32 size)
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#else
static void *
malloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
unsigned int size)
{
return malloc(size);
}
static void *
realloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
void *ptr, unsigned int size)
{
return realloc(ptr, size);
}
static void
free_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
void *ptr)
{
free(ptr);
}
#endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
#if WASM_ENABLE_STATIC_PGO != 0
static void
@ -861,9 +891,13 @@ main(int argc, char *argv[])
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
/* Set user data for the allocator is needed */
/* init_args.mem_alloc_option.allocator.user_data = user_data; */
#endif
init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
init_args.mem_alloc_option.allocator.free_func = free_func;
#endif
#if WASM_ENABLE_FAST_JIT != 0

View File

@ -191,7 +191,37 @@ validate_env_str(char *env)
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#else
static void *
malloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
unsigned int size)
{
return malloc(size);
}
static void *
realloc_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
void *ptr, unsigned int size)
{
return realloc(ptr, size);
}
static void
free_func(
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data,
#endif
void *ptr)
{
free(ptr);
}
#endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
#if WASM_ENABLE_MULTI_MODULE != 0
static char *
@ -450,9 +480,13 @@ main(int argc, char *argv[])
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
/* Set user data for the allocator is needed */
/* init_args.mem_alloc_option.allocator.user_data = user_data; */
#endif
init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
init_args.mem_alloc_option.allocator.free_func = free_func;
#endif
#if WASM_ENABLE_JIT != 0