Always allocate linear memory using mmap (#3052)
With this approach we can omit using memset() for the newly allocated memory therefore the physical pages are not being used unless touched by the program. This also simplifies the implementation.
This commit is contained in:
12
core/shared/platform/common/memory/mremap.c
Normal file
12
core/shared/platform/common/memory/mremap.c
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Amazon Inc. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_memutils.h"
|
||||
|
||||
void *
|
||||
os_mremap(void *old_addr, size_t old_size, size_t new_size)
|
||||
{
|
||||
return bh_memory_remap_slow(old_addr, old_size, new_size);
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
# Copyright (C) 2024 Amazon Inc. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
file (GLOB_RECURSE PLATFORM_COMMON_MEMORY_SOURCE ${CMAKE_CURRENT_LIST_DIR}/*.c)
|
||||
@ -16,4 +16,19 @@ else()
|
||||
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
|
||||
endif()
|
||||
|
||||
# This is to support old CMake version. Newer version of CMake could use
|
||||
# list APPEND/POP_BACK methods.
|
||||
include(CheckSymbolExists)
|
||||
set (CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE ${CMAKE_REQUIRED_DEFINITIONS})
|
||||
check_symbol_exists (mremap "sys/mman.h" MREMAP_EXISTS)
|
||||
list (REMOVE_AT CMAKE_REQUIRED_DEFINITIONS 0)
|
||||
|
||||
if(MREMAP_EXISTS)
|
||||
add_definitions (-DWASM_HAVE_MREMAP=1)
|
||||
else()
|
||||
add_definitions (-DWASM_HAVE_MREMAP=0)
|
||||
include (${CMAKE_CURRENT_LIST_DIR}/../memory/platform_api_memory.cmake)
|
||||
set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
|
||||
endif()
|
||||
|
||||
set (PLATFORM_COMMON_POSIX_SOURCE ${source_all} )
|
||||
|
||||
@ -3,6 +3,12 @@
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE) && WASM_HAVE_MREMAP != 0
|
||||
/* Enable mremap */
|
||||
#define _GNU_SOURCE
|
||||
#include "bh_memutils.h"
|
||||
#endif
|
||||
|
||||
#include "platform_api_vmcore.h"
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
@ -236,6 +242,23 @@ os_munmap(void *addr, size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
#if WASM_HAVE_MREMAP != 0
|
||||
void *
|
||||
os_mremap(void *old_addr, size_t old_size, size_t new_size)
|
||||
{
|
||||
void *ptr = mremap(old_addr, old_size, new_size, MREMAP_MAYMOVE);
|
||||
|
||||
if (ptr == MAP_FAILED) {
|
||||
#if BH_ENABLE_TRACE_MMAP != 0
|
||||
os_printf("mremap failed: %d\n", errno);
|
||||
#endif
|
||||
return bh_memory_remap_slow(old_addr, old_size, new_size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
os_mprotect(void *addr, size_t size, int prot)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user