diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index a24b3c32d..49df9acfb 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -251,7 +251,7 @@ WASMSharedHeap * wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body) { WASMSharedHeap *cur; - bool heap_handle_exist = false; + bool heap_handle_exist = head->heap_handle != NULL; if (!head || !body) { LOG_WARNING("Invalid shared heap to chain."); @@ -265,8 +265,15 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body) os_mutex_unlock(&shared_heap_list_lock); return NULL; } - - for (cur = head; cur; cur = cur->chain_next) { + for (cur = shared_heap_list; cur; cur = cur->next) { + if (cur->chain_next == body) { + LOG_WARNING("To create shared heap chain, the `body` shared heap " + "can't already be in a chain"); + os_mutex_unlock(&shared_heap_list_lock); + return NULL; + } + } + for (cur = body; cur; cur = cur->chain_next) { if (cur->heap_handle && heap_handle_exist) { LOG_WARNING( "To create shared heap chain, only one of shared heap can " @@ -310,7 +317,6 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain) if (!entire_chain) break; } - os_mutex_unlock(&shared_heap_list_lock); return cur; } diff --git a/core/iwasm/common/wasm_memory.h b/core/iwasm/common/wasm_memory.h index bceea0ee4..88d0f3c5d 100644 --- a/core/iwasm/common/wasm_memory.h +++ b/core/iwasm/common/wasm_memory.h @@ -45,6 +45,12 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size) WASMSharedHeap * wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args); +WASMSharedHeap * +wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body); + +WASMSharedHeap * +wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain); + bool wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst, WASMSharedHeap *shared_heap); diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 273657246..15759f777 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -336,6 +336,7 @@ typedef enum { typedef struct SharedHeapInitArgs { uint32_t size; + void *pre_allocated_addr; } SharedHeapInitArgs; /** @@ -2258,7 +2259,37 @@ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args); /** - * Attach a shared heap to a module instance + * This function links two shared heaps, `head` and `body`, where `head` is a + * shared heap and `body` can be shared heap chain head, into a single chain. + * The `head` heap will be the head of the chain, and the `body` heap will be + * appended to it. At most one shared heap in shared heap chain can be + * dynamically allocated, the rest have to be the pre-allocated shared heap * + * + * @param head The head of the shared heap chain. + * @param body The body of the shared heap chain to be appended. + * @return The new head of the shared heap chain. NULL if failed. + */ +WASM_RUNTIME_API_EXTERN wasm_shared_heap_t +wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head, + wasm_shared_heap_t body); + +/** + * This function unchains the shared heaps from the given head. If + * `entire_chain` is true, it will unchain the entire chain of shared heaps. + * Otherwise, it will unchain only the first shared heap in the chain. + * + * @param head The head of the shared heap chain. + * @param entire_chain A boolean flag indicating whether to unchain the entire + * chain. + * @return The new head of the shared heap chain. Or the last shared heap in the + * chain if `entire_chain` is true. + */ +wasm_shared_heap_t +wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain); + +/** + * Attach a shared heap, it can be the head of shared heap chain, in that case, + * attach the shared heap chain, to a module instance * * @param module_inst the module instance * @param shared_heap the shared heap @@ -2277,7 +2308,8 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst); /** - * Allocate memory from a shared heap + * Allocate memory from a shared heap, or the non-preallocated shared heap from + * the shared heap chain * * @param module_inst the module instance * @param size required memory size @@ -2294,7 +2326,8 @@ wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size, void **p_native_addr); /** - * Free the memory allocated from shared heap + * Free the memory allocated from shared heap, or the non-preallocated shared + * heap from the shared heap chain * * @param module_inst the module instance * @param ptr the offset in wasm app