Skip to content

Commit ad45e49

Browse files
committed
Always use mmap to allocate linear memory
With this approach we can ommit 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.
1 parent b98f150 commit ad45e49

25 files changed

+376
-420
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ jobs:
145145
"-DWAMR_BUILD_SIMD=1",
146146
"-DWAMR_BUILD_TAIL_CALL=1",
147147
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
148-
"-DWAMR_ENABLE_SHARED_MEMORY_MMAP=1",
149148
]
150149
os: [ubuntu-22.04]
151150
platform: [android, linux]

build-scripts/config_common.cmake

-6
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ if (WAMR_BUILD_SHARED_MEMORY EQUAL 1)
248248
else ()
249249
add_definitions (-DWASM_ENABLE_SHARED_MEMORY=0)
250250
endif ()
251-
if (WAMR_ENABLE_SHARED_MEMORY_MMAP EQUAL 1)
252-
add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=1)
253-
message (" Shared memory allocated using mmap enabled")
254-
else ()
255-
add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=0)
256-
endif ()
257251
if (WAMR_BUILD_THREAD_MGR EQUAL 1)
258252
message (" Thread manager enabled")
259253
endif ()

core/config.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@
188188
#define WASM_ENABLE_APP_FRAMEWORK 0
189189
#endif
190190

191+
#ifndef WASM_HAVE_MREMAP
192+
#define WASM_HAVE_MREMAP 0
193+
#endif
194+
191195
/* Bulk memory operation */
192196
#ifndef WASM_ENABLE_BULK_MEMORY
193197
#define WASM_ENABLE_BULK_MEMORY 0
@@ -513,9 +517,4 @@
513517
#define WASM_ENABLE_QUICK_AOT_ENTRY 1
514518
#endif
515519

516-
/* Disable mmap based shared memory by default */
517-
#ifndef WASM_ENABLE_SHARED_MEMORY_MMAP
518-
#define WASM_ENABLE_SHARED_MEMORY_MMAP 0
519-
#endif
520-
521520
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_runtime.c

+11-79
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,6 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
340340
static void
341341
memories_deinstantiate(AOTModuleInstance *module_inst)
342342
{
343-
#ifdef WASM_LINEAR_MEMORY_MMAP
344-
uint64 map_size;
345-
#endif
346343
uint32 i;
347344
AOTMemoryInstance *memory_inst;
348345

@@ -364,23 +361,7 @@ memories_deinstantiate(AOTModuleInstance *module_inst)
364361
}
365362

366363
if (memory_inst->memory_data) {
367-
#ifndef OS_ENABLE_HW_BOUND_CHECK
368-
#ifdef WASM_LINEAR_MEMORY_MMAP
369-
if (shared_memory_is_shared(memory_inst)) {
370-
map_size = (uint64)memory_inst->num_bytes_per_page
371-
* memory_inst->max_page_count;
372-
wasm_munmap_linear_memory(memory_inst->memory_data,
373-
map_size, map_size);
374-
}
375-
else
376-
#endif
377-
wasm_runtime_free(memory_inst->memory_data);
378-
#else
379-
map_size = (uint64)memory_inst->num_bytes_per_page
380-
* memory_inst->cur_page_count;
381-
wasm_munmap_linear_memory(memory_inst->memory_data, map_size,
382-
8 * (uint64)BH_GB);
383-
#endif
364+
wasm_deallocate_linear_memory(memory_inst);
384365
}
385366
}
386367
}
@@ -402,14 +383,10 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
402383
uint32 heap_offset = num_bytes_per_page * init_page_count;
403384
uint64 memory_data_size, max_memory_data_size;
404385
uint8 *p = NULL, *global_addr;
405-
#ifdef WASM_LINEAR_MEMORY_MMAP
406-
uint8 *mapped_mem = NULL;
407-
uint64 map_size;
408-
#endif
409386

387+
bool is_shared_memory = false;
410388
#if WASM_ENABLE_SHARED_MEMORY != 0
411-
bool is_shared_memory = memory->memory_flags & 0x02 ? true : false;
412-
389+
is_shared_memory = memory->memory_flags & 0x02 ? true : false;
413390
/* Shared memory */
414391
if (is_shared_memory && parent != NULL) {
415392
AOTMemoryInstance *shared_memory_instance;
@@ -519,55 +496,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
519496
module->aux_stack_size);
520497
LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
521498

522-
memory_data_size = (uint64)num_bytes_per_page * init_page_count;
523499
max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
524-
bh_assert(memory_data_size <= UINT32_MAX);
525500
bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
526501
(void)max_memory_data_size;
527502

528-
#ifndef OS_ENABLE_HW_BOUND_CHECK
529-
#if WASM_ENABLE_SHARED_MEMORY != 0
530-
if (is_shared_memory) {
531-
#if WASM_ENABLE_SHARED_MEMORY_MMAP != 0
532-
map_size = max_memory_data_size;
533-
if (max_memory_data_size > 0
534-
&& !(p = mapped_mem =
535-
wasm_mmap_linear_memory(map_size, &max_memory_data_size,
536-
error_buf, error_buf_size))) {
537-
return NULL;
538-
}
539-
#else
540-
/* Allocate maximum memory size when memory is shared */
541-
if (max_memory_data_size > 0
542-
&& !(p = runtime_malloc(max_memory_data_size, error_buf,
543-
error_buf_size))) {
544-
return NULL;
545-
}
546-
#endif
547-
}
548-
else
549-
#endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */
550-
{
551-
/* Allocate initial memory size when memory is not shared */
552-
if (memory_data_size > 0
553-
&& !(p = runtime_malloc(memory_data_size, error_buf,
554-
error_buf_size))) {
555-
return NULL;
556-
}
557-
}
558-
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
559-
/* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
560-
* ea = i + memarg.offset
561-
* both i and memarg.offset are u32 in range 0 to 4G
562-
* so the range of ea is 0 to 8G
563-
*/
564-
map_size = 8 * (uint64)BH_GB;
565-
if (!(p = mapped_mem = wasm_mmap_linear_memory(
566-
map_size, &memory_data_size, error_buf, error_buf_size))) {
567-
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
503+
if (wasm_allocate_linear_memory(&p, is_shared_memory, num_bytes_per_page,
504+
init_page_count, max_page_count,
505+
&memory_data_size)
506+
!= BHT_OK) {
507+
set_error_buf(error_buf, error_buf_size,
508+
"allocate linear memory failed");
568509
return NULL;
569510
}
570-
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
571511

572512
memory_inst->module_type = Wasm_Module_AoT;
573513
memory_inst->num_bytes_per_page = num_bytes_per_page;
@@ -617,16 +557,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
617557
if (heap_size > 0)
618558
wasm_runtime_free(memory_inst->heap_handle);
619559
fail1:
620-
#ifdef WASM_LINEAR_MEMORY_MMAP
621-
if (mapped_mem)
622-
wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size);
623-
else
624-
#endif
625-
{
626-
if (memory_inst->memory_data)
627-
wasm_runtime_free(memory_inst->memory_data);
628-
}
629-
memory_inst->memory_data = NULL;
560+
wasm_deallocate_linear_memory(memory_inst);
561+
630562
return NULL;
631563
}
632564

0 commit comments

Comments
 (0)