Skip to content

Commit 926f662

Browse files
authored
Add memory instance support apis (#3786)
Now that WAMR supports multiple memory instances, this PR adds some APIs to access them in a standard way. This involves moving some existing utility functions out from the `WASM_ENABLE_MULTI_MODULE` blocks they were nested in, but multi-memory and multi-module seem independent as far as I can tell so I assume that's okay. APIs added: ```C wasm_runtime_lookup_memory wasm_runtime_get_default_memory wasm_runtime_get_memory wasm_memory_get_cur_page_count wasm_memory_get_max_page_count wasm_memory_get_bytes_per_page wasm_memory_get_shared wasm_memory_get_base_address wasm_memory_enlarge ```
1 parent 9aadbfe commit 926f662

File tree

6 files changed

+307
-76
lines changed

6 files changed

+307
-76
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,24 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
10581058
return NULL;
10591059
}
10601060

1061-
static AOTMemoryInstance *
1061+
AOTMemoryInstance *
1062+
aot_lookup_memory(AOTModuleInstance *module_inst, char const *name)
1063+
{
1064+
#if WASM_ENABLE_MULTI_MEMORY != 0
1065+
uint32 i;
1066+
for (i = 0; i < module_inst->export_memory_count; i++)
1067+
if (!strcmp(module_inst->export_memories[i].name, name))
1068+
return module_inst->export_memories[i].memory;
1069+
return NULL;
1070+
#else
1071+
(void)module_inst->export_memories;
1072+
if (!module_inst->memories)
1073+
return NULL;
1074+
return module_inst->memories[0];
1075+
#endif
1076+
}
1077+
1078+
AOTMemoryInstance *
10621079
aot_get_default_memory(AOTModuleInstance *module_inst)
10631080
{
10641081
if (module_inst->memories)
@@ -1067,6 +1084,14 @@ aot_get_default_memory(AOTModuleInstance *module_inst)
10671084
return NULL;
10681085
}
10691086

1087+
AOTMemoryInstance *
1088+
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
1089+
{
1090+
if ((index >= module_inst->memory_count) || !module_inst->memories)
1091+
return NULL;
1092+
return module_inst->memories[index];
1093+
}
1094+
10701095
static bool
10711096
memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
10721097
AOTModule *module, uint32 heap_size,

core/iwasm/aot/aot_runtime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
532532
AOTFunctionInstance *
533533
aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);
534534

535+
AOTMemoryInstance *
536+
aot_lookup_memory(AOTModuleInstance *module_inst, char const *name);
537+
538+
AOTMemoryInstance *
539+
aot_get_default_memory(AOTModuleInstance *module_inst);
540+
541+
AOTMemoryInstance *
542+
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
543+
535544
/**
536545
* Get a function in the AOT module instance.
537546
*

core/iwasm/common/wasm_memory.c

Lines changed: 116 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -673,11 +673,9 @@ wasm_get_default_memory(WASMModuleInstance *module_inst)
673673
WASMMemoryInstance *
674674
wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
675675
{
676-
bh_assert(index < module_inst->memory_count);
677-
if (module_inst->memories)
678-
return module_inst->memories[index];
679-
else
676+
if ((index >= module_inst->memory_count) || !module_inst->memories)
680677
return NULL;
678+
return module_inst->memories[index];
681679
}
682680

683681
void
@@ -756,15 +754,10 @@ wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
756754
return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
757755
}
758756

759-
bool
760-
wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
761-
uint32 memidx)
757+
static bool
758+
wasm_enlarge_memory_internal(WASMModuleInstanceCommon *module,
759+
WASMMemoryInstance *memory, uint32 inc_page_count)
762760
{
763-
#if WASM_ENABLE_MULTI_MEMORY != 0
764-
WASMMemoryInstance *memory = wasm_get_memory_with_idx(module, memidx);
765-
#else
766-
WASMMemoryInstance *memory = wasm_get_default_memory(module);
767-
#endif
768761
uint8 *memory_data_old, *memory_data_new, *heap_data_old;
769762
uint32 num_bytes_per_page, heap_size;
770763
uint32 cur_page_count, max_page_count, total_page_count;
@@ -913,7 +906,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
913906
wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
914907

915908
return_func:
916-
if (!ret && enlarge_memory_error_cb) {
909+
if (!ret && module && enlarge_memory_error_cb) {
917910
WASMExecEnv *exec_env = NULL;
918911

919912
#if WASM_ENABLE_INTERP != 0
@@ -926,8 +919,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
926919
#endif
927920

928921
enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
929-
failure_reason,
930-
(WASMModuleInstanceCommon *)module, exec_env,
922+
failure_reason, module, exec_env,
931923
enlarge_memory_error_user_data);
932924
}
933925

@@ -971,15 +963,16 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
971963
{
972964
bool ret = false;
973965

966+
if (module->memory_count > 0) {
974967
#if WASM_ENABLE_SHARED_MEMORY != 0
975-
if (module->memory_count > 0)
976968
shared_memory_lock(module->memories[0]);
977969
#endif
978-
ret = wasm_enlarge_memory_internal(module, inc_page_count, 0);
970+
ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
971+
module->memories[0], inc_page_count);
979972
#if WASM_ENABLE_SHARED_MEMORY != 0
980-
if (module->memory_count > 0)
981973
shared_memory_unlock(module->memories[0]);
982974
#endif
975+
}
983976

984977
return ret;
985978
}
@@ -990,15 +983,117 @@ wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
990983
{
991984
bool ret = false;
992985

986+
if (memidx < module->memory_count) {
993987
#if WASM_ENABLE_SHARED_MEMORY != 0
994-
if (memidx < module->memory_count)
995988
shared_memory_lock(module->memories[memidx]);
996989
#endif
997-
ret = wasm_enlarge_memory_internal(module, inc_page_count, memidx);
990+
ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
991+
module->memories[memidx],
992+
inc_page_count);
998993
#if WASM_ENABLE_SHARED_MEMORY != 0
999-
if (memidx < module->memory_count)
1000994
shared_memory_unlock(module->memories[memidx]);
1001995
#endif
996+
}
997+
998+
return ret;
999+
}
1000+
1001+
WASMMemoryInstance *
1002+
wasm_runtime_lookup_memory(WASMModuleInstanceCommon *module_inst,
1003+
const char *name)
1004+
{
1005+
#if WASM_ENABLE_INTERP != 0
1006+
if (module_inst->module_type == Wasm_Module_Bytecode)
1007+
return wasm_lookup_memory((WASMModuleInstance *)module_inst, name);
1008+
#endif
1009+
1010+
#if WASM_ENABLE_AOT != 0
1011+
if (module_inst->module_type == Wasm_Module_AoT)
1012+
return aot_lookup_memory((WASMModuleInstance *)module_inst, name);
1013+
#endif
1014+
1015+
return NULL;
1016+
}
1017+
1018+
WASMMemoryInstance *
1019+
wasm_runtime_get_default_memory(WASMModuleInstanceCommon *module_inst)
1020+
{
1021+
#if WASM_ENABLE_INTERP != 0
1022+
if (module_inst->module_type == Wasm_Module_Bytecode)
1023+
return wasm_get_default_memory((WASMModuleInstance *)module_inst);
1024+
#endif
1025+
1026+
#if WASM_ENABLE_AOT != 0
1027+
if (module_inst->module_type == Wasm_Module_AoT)
1028+
return aot_get_default_memory((AOTModuleInstance *)module_inst);
1029+
#endif
1030+
1031+
return NULL;
1032+
}
1033+
1034+
WASMMemoryInstance *
1035+
wasm_runtime_get_memory(WASMModuleInstanceCommon *module_inst, uint32 index)
1036+
{
1037+
#if WASM_ENABLE_INTERP != 0
1038+
if (module_inst->module_type == Wasm_Module_Bytecode)
1039+
return wasm_get_memory_with_idx((WASMModuleInstance *)module_inst,
1040+
index);
1041+
#endif
1042+
1043+
#if WASM_ENABLE_AOT != 0
1044+
if (module_inst->module_type == Wasm_Module_AoT)
1045+
return aot_get_memory_with_index((AOTModuleInstance *)module_inst,
1046+
index);
1047+
#endif
1048+
1049+
return NULL;
1050+
}
1051+
1052+
uint64
1053+
wasm_memory_get_cur_page_count(WASMMemoryInstance *memory)
1054+
{
1055+
return memory->cur_page_count;
1056+
}
1057+
1058+
uint64
1059+
wasm_memory_get_max_page_count(WASMMemoryInstance *memory)
1060+
{
1061+
return memory->max_page_count;
1062+
}
1063+
1064+
uint64
1065+
wasm_memory_get_bytes_per_page(WASMMemoryInstance *memory)
1066+
{
1067+
return memory->num_bytes_per_page;
1068+
}
1069+
1070+
bool
1071+
wasm_memory_get_shared(WASMMemoryInstance *memory)
1072+
{
1073+
return memory->is_shared_memory;
1074+
}
1075+
1076+
void *
1077+
wasm_memory_get_base_address(WASMMemoryInstance *memory)
1078+
{
1079+
return memory->memory_data;
1080+
}
1081+
1082+
bool
1083+
wasm_memory_enlarge(WASMMemoryInstance *memory, uint64 inc_page_count)
1084+
{
1085+
bool ret = false;
1086+
1087+
if (memory) {
1088+
#if WASM_ENABLE_SHARED_MEMORY != 0
1089+
shared_memory_lock(memory);
1090+
#endif
1091+
ret =
1092+
wasm_enlarge_memory_internal(NULL, memory, (uint32)inc_page_count);
1093+
#if WASM_ENABLE_SHARED_MEMORY != 0
1094+
shared_memory_unlock(memory);
1095+
#endif
1096+
}
10021097

10031098
return ret;
10041099
}

core/iwasm/include/wasm_export.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
120120
typedef void WASMFunctionInstanceCommon;
121121
typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
122122

123+
/* Memory instance */
124+
struct WASMMemoryInstance;
125+
typedef struct WASMMemoryInstance *wasm_memory_inst_t;
126+
123127
/* WASM section */
124128
typedef struct wasm_section_t {
125129
struct wasm_section_t *next;
@@ -939,6 +943,100 @@ WASM_RUNTIME_API_EXTERN void
939943
wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
940944
const wasm_module_inst_t module_inst);
941945

946+
/**
947+
* @brief Lookup a memory instance by name
948+
*
949+
* @param module_inst The module instance
950+
* @param name The name of the memory instance
951+
*
952+
* @return The memory instance if found, NULL otherwise
953+
*/
954+
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
955+
wasm_runtime_lookup_memory(const wasm_module_inst_t module_inst,
956+
const char *name);
957+
958+
/**
959+
* @brief Get the default memory instance
960+
*
961+
* @param module_inst The module instance
962+
*
963+
* @return The memory instance if found, NULL otherwise
964+
*/
965+
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
966+
wasm_runtime_get_default_memory(const wasm_module_inst_t module_inst);
967+
968+
/**
969+
* @brief Get a memory instance by index
970+
*
971+
* @param module_inst The module instance
972+
* @param index The index of the memory instance
973+
*
974+
* @return The memory instance if found, NULL otherwise
975+
*/
976+
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
977+
wasm_runtime_get_memory(const wasm_module_inst_t module_inst, uint32_t index);
978+
979+
/**
980+
* @brief Get the current number of pages for a memory instance
981+
*
982+
* @param memory_inst The memory instance
983+
*
984+
* @return The current number of pages
985+
*/
986+
WASM_RUNTIME_API_EXTERN uint64_t
987+
wasm_memory_get_cur_page_count(const wasm_memory_inst_t memory_inst);
988+
989+
/**
990+
* @brief Get the maximum number of pages for a memory instance
991+
*
992+
* @param memory_inst The memory instance
993+
*
994+
* @return The maximum number of pages
995+
*/
996+
WASM_RUNTIME_API_EXTERN uint64_t
997+
wasm_memory_get_max_page_count(const wasm_memory_inst_t memory_inst);
998+
999+
/**
1000+
* @brief Get the number of bytes per page for a memory instance
1001+
*
1002+
* @param memory_inst The memory instance
1003+
*
1004+
* @return The number of bytes per page
1005+
*/
1006+
WASM_RUNTIME_API_EXTERN uint64_t
1007+
wasm_memory_get_bytes_per_page(const wasm_memory_inst_t memory_inst);
1008+
1009+
/**
1010+
* @brief Get the shared status for a memory instance
1011+
*
1012+
* @param memory_inst The memory instance
1013+
*
1014+
* @return True if shared, false otherwise
1015+
*/
1016+
WASM_RUNTIME_API_EXTERN bool
1017+
wasm_memory_get_shared(const wasm_memory_inst_t memory_inst);
1018+
1019+
/**
1020+
* @brief Get the base address for a memory instance
1021+
*
1022+
* @param memory_inst The memory instance
1023+
*
1024+
* @return The base address on success, false otherwise
1025+
*/
1026+
WASM_RUNTIME_API_EXTERN void *
1027+
wasm_memory_get_base_address(const wasm_memory_inst_t memory_inst);
1028+
1029+
/**
1030+
* @brief Enlarge a memory instance by a number of pages
1031+
*
1032+
* @param memory_inst The memory instance
1033+
* @param inc_page_count The number of pages to add
1034+
*
1035+
* @return True if successful, false otherwise
1036+
*/
1037+
WASM_RUNTIME_API_EXTERN bool
1038+
wasm_memory_enlarge(wasm_memory_inst_t memory_inst, uint64_t inc_page_count);
1039+
9421040
/**
9431041
* Call the given WASM function of a WASM module instance with
9441042
* arguments (bytecode and AoT).

0 commit comments

Comments
 (0)