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
```
This commit is contained in:
Benbuck Nason
2024-09-13 19:31:13 -07:00
committed by GitHub
parent 9aadbfee29
commit 926f662231
6 changed files with 307 additions and 76 deletions

View File

@ -120,6 +120,10 @@ typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
typedef void WASMFunctionInstanceCommon;
typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
/* Memory instance */
struct WASMMemoryInstance;
typedef struct WASMMemoryInstance *wasm_memory_inst_t;
/* WASM section */
typedef struct wasm_section_t {
struct wasm_section_t *next;
@ -939,6 +943,100 @@ WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
const wasm_module_inst_t module_inst);
/**
* @brief Lookup a memory instance by name
*
* @param module_inst The module instance
* @param name The name of the memory instance
*
* @return The memory instance if found, NULL otherwise
*/
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
wasm_runtime_lookup_memory(const wasm_module_inst_t module_inst,
const char *name);
/**
* @brief Get the default memory instance
*
* @param module_inst The module instance
*
* @return The memory instance if found, NULL otherwise
*/
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
wasm_runtime_get_default_memory(const wasm_module_inst_t module_inst);
/**
* @brief Get a memory instance by index
*
* @param module_inst The module instance
* @param index The index of the memory instance
*
* @return The memory instance if found, NULL otherwise
*/
WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
wasm_runtime_get_memory(const wasm_module_inst_t module_inst, uint32_t index);
/**
* @brief Get the current number of pages for a memory instance
*
* @param memory_inst The memory instance
*
* @return The current number of pages
*/
WASM_RUNTIME_API_EXTERN uint64_t
wasm_memory_get_cur_page_count(const wasm_memory_inst_t memory_inst);
/**
* @brief Get the maximum number of pages for a memory instance
*
* @param memory_inst The memory instance
*
* @return The maximum number of pages
*/
WASM_RUNTIME_API_EXTERN uint64_t
wasm_memory_get_max_page_count(const wasm_memory_inst_t memory_inst);
/**
* @brief Get the number of bytes per page for a memory instance
*
* @param memory_inst The memory instance
*
* @return The number of bytes per page
*/
WASM_RUNTIME_API_EXTERN uint64_t
wasm_memory_get_bytes_per_page(const wasm_memory_inst_t memory_inst);
/**
* @brief Get the shared status for a memory instance
*
* @param memory_inst The memory instance
*
* @return True if shared, false otherwise
*/
WASM_RUNTIME_API_EXTERN bool
wasm_memory_get_shared(const wasm_memory_inst_t memory_inst);
/**
* @brief Get the base address for a memory instance
*
* @param memory_inst The memory instance
*
* @return The base address on success, false otherwise
*/
WASM_RUNTIME_API_EXTERN void *
wasm_memory_get_base_address(const wasm_memory_inst_t memory_inst);
/**
* @brief Enlarge a memory instance by a number of pages
*
* @param memory_inst The memory instance
* @param inc_page_count The number of pages to add
*
* @return True if successful, false otherwise
*/
WASM_RUNTIME_API_EXTERN bool
wasm_memory_enlarge(wasm_memory_inst_t memory_inst, uint64_t inc_page_count);
/**
* Call the given WASM function of a WASM module instance with
* arguments (bytecode and AoT).