use realloc_func to enlarge memory

use realloc_func to enlarge memory
This commit is contained in:
Dongsheng.Yan 2024-04-15 09:07:45 +08:00
parent e274d83c1c
commit bcb5cde2af
2 changed files with 17 additions and 10 deletions

View File

@ -776,6 +776,18 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
bh_assert(total_size_new bh_assert(total_size_new
<= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64)); <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
#if WASM_MEM_ALLOC_WITH_USAGE != 0
(void)full_size_mmaped;
if (!(memory_data_new = realloc_func(Alloc_For_LinearMemory,
memory_data_old,
total_size_new))) {
ret = false;
goto return_func;
}
memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
memory->heap_data_end = memory->heap_data + heap_size;
memory->memory_data = memory_data_new;
#else
if (full_size_mmaped) { if (full_size_mmaped) {
#ifdef BH_PLATFORM_WINDOWS #ifdef BH_PLATFORM_WINDOWS
if (!os_mem_commit(memory->memory_data_end, if (!os_mem_commit(memory->memory_data_end,
@ -807,12 +819,6 @@ 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))) {
@ -820,7 +826,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
@ -841,6 +847,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
os_writegsbase(memory_data_new); os_writegsbase(memory_data_new);
#endif #endif
} }
#endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
memory->num_bytes_per_page = num_bytes_per_page; memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count; memory->cur_page_count = total_page_count;
@ -923,6 +930,7 @@ wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
#endif #endif
#if WASM_MEM_ALLOC_WITH_USAGE != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
(void)map_size;
free_func(Alloc_For_LinearMemory, memory_inst->memory_data); free_func(Alloc_For_LinearMemory, memory_inst->memory_data);
#else #else
wasm_munmap_linear_memory(memory_inst->memory_data, wasm_munmap_linear_memory(memory_inst->memory_data,
@ -961,6 +969,7 @@ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
* so the range of ea is 0 to 8G * so the range of ea is 0 to 8G
*/ */
map_size = 8 * (uint64)BH_GB; map_size = 8 * (uint64)BH_GB;
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
page_size = os_getpagesize(); page_size = os_getpagesize();
@ -979,6 +988,7 @@ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
if (map_size > 0) { if (map_size > 0) {
#if WASM_MEM_ALLOC_WITH_USAGE != 0 #if WASM_MEM_ALLOC_WITH_USAGE != 0
(void)wasm_mmap_linear_memory;
if (!(*data = malloc_func(Alloc_For_LinearMemory, *memory_data_size))) { if (!(*data = malloc_func(Alloc_For_LinearMemory, *memory_data_size))) {
return BHT_ERROR; return BHT_ERROR;
} }

View File

@ -452,7 +452,6 @@ malloc_func(
#endif #endif
unsigned int size) unsigned int size)
{ {
//printf("current allocated for %d\n", usage);
return malloc(size); return malloc(size);
} }
@ -465,7 +464,6 @@ realloc_func(
#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);
} }
@ -478,7 +476,6 @@ free_func(
#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 */