Extend os_mmap to support map file from fd (#2763)

Add an extra argument `os_file_handle file` for `os_mmap` to support
mapping file from a file fd, and remove `os_get_invalid_handle` from
`posix_file.c` and `win_file.c`, instead, add it in the `platform_internal.h`
files to remove the dependency on libc-wasi.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi
2023-11-16 08:28:54 +08:00
committed by GitHub
parent 0b8a904193
commit 24aa1cb408
35 changed files with 127 additions and 45 deletions

View File

@ -978,12 +978,6 @@ os_closedir(os_dir_stream dir_stream)
return __WASI_ESUCCESS;
}
os_file_handle
os_get_invalid_handle()
{
return -1;
}
os_dir_stream
os_get_invalid_dir_stream()
{

View File

@ -37,7 +37,7 @@ round_down(uintptr_t v, uintptr_t b)
#endif
void *
os_mmap(void *hint, size_t size, int prot, int flags)
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
{
int map_prot = PROT_NONE;
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
@ -114,7 +114,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
/* try 10 times, step with 1MB each time */
for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i++) {
addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0);
addr = mmap(hint_addr, request_size, map_prot, map_flags, file, 0);
if (addr != MAP_FAILED) {
if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) {
/* unmap and try again if the mapped address doesn't
@ -136,7 +136,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
if (addr == MAP_FAILED) {
/* try 5 times */
for (i = 0; i < 5; i++) {
addr = mmap(hint, request_size, map_prot, map_flags, -1, 0);
addr = mmap(hint, request_size, map_prot, map_flags, file, 0);
if (addr != MAP_FAILED)
break;
}
@ -266,4 +266,4 @@ os_icache_flush(void *start, size_t len)
#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__)
sys_icache_invalidate(start, len);
#endif
}
}

View File

@ -664,7 +664,7 @@ os_thread_signal_init(os_signal_handler handler)
/* Initialize memory for signal alternate stack of current thread */
if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE,
MMAP_MAP_NONE))) {
MMAP_MAP_NONE, os_get_invalid_handle()))) {
os_printf("Failed to mmap memory for alternate stack\n");
goto fail1;
}