Skip to content

Commit b6adec3

Browse files
authored
shared-platform: Remove dependency on shared-utils' bh_memory_remap_slow (#3153)
As an original design rule, the code in `core/shared/platform` should not rely on the code in `core/share/utils`. In the current implementation, platform layer calls function `bh_memory_remap_slow` in utils layer. This PR adds inline function `os_mremap_slow` in platform_api_vmcore.h, and lets os_remap call it if mremap fails. And remove bh_memutils.h/c as as they are unused. And resolve the compilation warning in wamrc: ```bash core/shared/platform/common/posix/posix_memmap.c:255:16: warning: implicit declaration of function ‘bh_memory_remap_slow’ 255 | return bh_memory_remap_slow(old_addr, old_size, new_size); ```
1 parent 3a0e864 commit b6adec3

File tree

7 files changed

+22
-69
lines changed

7 files changed

+22
-69
lines changed

core/shared/platform/common/memory/mremap.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#include "bh_memutils.h"
6+
#include "platform_api_vmcore.h"
77

88
void *
99
os_mremap(void *old_addr, size_t old_size, size_t new_size)
1010
{
11-
return bh_memory_remap_slow(old_addr, old_size, new_size);
11+
return os_mremap_slow(old_addr, old_size, new_size);
1212
}

core/shared/platform/common/posix/platform_api_posix.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ list (REMOVE_AT CMAKE_REQUIRED_DEFINITIONS 0)
2525

2626
if(MREMAP_EXISTS)
2727
add_definitions (-DWASM_HAVE_MREMAP=1)
28+
add_definitions (-D_GNU_SOURCE)
2829
else()
2930
add_definitions (-DWASM_HAVE_MREMAP=0)
3031
include (${CMAKE_CURRENT_LIST_DIR}/../memory/platform_api_memory.cmake)

core/shared/platform/common/posix/posix_memmap.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#if !defined(_GNU_SOURCE) && WASM_HAVE_MREMAP != 0
7-
/* Enable mremap */
8-
#define _GNU_SOURCE
9-
#include "bh_memutils.h"
10-
#endif
11-
126
#include "platform_api_vmcore.h"
137

148
#if defined(__APPLE__) || defined(__MACH__)
@@ -252,7 +246,7 @@ os_mremap(void *old_addr, size_t old_size, size_t new_size)
252246
#if BH_ENABLE_TRACE_MMAP != 0
253247
os_printf("mremap failed: %d\n", errno);
254248
#endif
255-
return bh_memory_remap_slow(old_addr, old_size, new_size);
249+
return os_mremap_slow(old_addr, old_size, new_size);
256250
}
257251

258252
return ptr;

core/shared/platform/include/platform_api_vmcore.h

+18
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ os_munmap(void *addr, size_t size);
142142
int
143143
os_mprotect(void *addr, size_t size, int prot);
144144

145+
static inline void *
146+
os_mremap_slow(void *old_addr, size_t old_size, size_t new_size)
147+
{
148+
void *new_memory = os_mmap(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ,
149+
0, os_get_invalid_handle());
150+
if (!new_memory) {
151+
return NULL;
152+
}
153+
/*
154+
* bh_memcpy_s can't be used as it doesn't support values bigger than
155+
* UINT32_MAX
156+
*/
157+
memcpy(new_memory, old_addr, new_size < old_size ? new_size : old_size);
158+
os_munmap(old_addr, old_size);
159+
160+
return new_memory;
161+
}
162+
145163
/* Doesn't guarantee that protection flags will be preserved.
146164
os_mprotect() must be called after remapping. */
147165
void *

core/shared/utils/bh_memutils.c

-24
This file was deleted.

core/shared/utils/bh_memutils.h

-35
This file was deleted.

product-mini/platforms/nuttx/wamr.mk

-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ CSRCS += nuttx_platform.c \
437437
bh_hashmap.c \
438438
bh_list.c \
439439
bh_log.c \
440-
bh_memutils.c \
441440
bh_queue.c \
442441
bh_vector.c \
443442
bh_read_file.c \

0 commit comments

Comments
 (0)