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:
Marcin Kolny
2024-02-02 14:17:44 +00:00
committed by GitHub
parent 2eb60060d8
commit a27ddece7f
25 changed files with 384 additions and 425 deletions

View 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);
}

View File

@ -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)

View File

@ -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} )

View File

@ -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)
{

View File

@ -12,6 +12,9 @@ include (${CMAKE_CURRENT_LIST_DIR}/../common/posix/platform_api_posix.cmake)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_POSIX_SOURCE})
file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h)

View File

@ -132,7 +132,7 @@ enum {
MMAP_MAP_32BIT = 1,
/* Don't interpret addr as a hint: place the mapping at exactly
that address. */
MMAP_MAP_FIXED = 2
MMAP_MAP_FIXED = 2,
};
void *
@ -142,6 +142,11 @@ os_munmap(void *addr, size_t size);
int
os_mprotect(void *addr, size_t size, int prot);
/* Doesn't guarantee that protection flags will be preserved.
os_mprotect() must be called after remapping. */
void *
os_mremap(void *old_addr, size_t old_size, size_t new_size);
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
void *
os_get_dbus_mirror(void *ibus);

View File

@ -79,6 +79,8 @@ os_get_invalid_handle()
return -1;
}
#define os_getpagesize getpagesize
#ifdef __cplusplus
}
#endif

View File

@ -37,6 +37,9 @@ else()
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif()
include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
file (GLOB source_all_untrusted ${PLATFORM_SHARED_DIR}/untrusted/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all})

View File

@ -44,6 +44,8 @@ typedef pthread_t korp_thread;
typedef pthread_rwlock_t korp_rwlock;
typedef sem_t korp_sem;
#define os_getpagesize getpagesize
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)

View File

@ -16,5 +16,6 @@ if (WAMR_BUILD_LIBC_WASI EQUAL 1)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif ()
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE})
include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE} ${PLATFORM_COMMON_MEMORY_SOURCE})

View File

@ -20,6 +20,9 @@ else()
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif()
include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake)
set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE})
set (PLATFORM_SHARED_SOURCE ${source_all})
file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h)