Skip to content

Commit 5c22e61

Browse files
committed
first draft of shared heap enhancement in interpreter and runtime: update function signature
1 parent 013306d commit 5c22e61

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

core/iwasm/common/wasm_memory.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ WASMSharedHeap *
251251
wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
252252
{
253253
WASMSharedHeap *cur;
254-
bool heap_handle_exist = false;
254+
bool heap_handle_exist = head->heap_handle != NULL;
255255

256256
if (!head || !body) {
257257
LOG_WARNING("Invalid shared heap to chain.");
@@ -265,8 +265,15 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
265265
os_mutex_unlock(&shared_heap_list_lock);
266266
return NULL;
267267
}
268-
269-
for (cur = head; cur; cur = cur->chain_next) {
268+
for (cur = shared_heap_list; cur; cur = cur->next) {
269+
if (cur->chain_next == body) {
270+
LOG_WARNING("To create shared heap chain, the `body` shared heap "
271+
"can't already be in a chain");
272+
os_mutex_unlock(&shared_heap_list_lock);
273+
return NULL;
274+
}
275+
}
276+
for (cur = body; cur; cur = cur->chain_next) {
270277
if (cur->heap_handle && heap_handle_exist) {
271278
LOG_WARNING(
272279
"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)
310317
if (!entire_chain)
311318
break;
312319
}
313-
314320
os_mutex_unlock(&shared_heap_list_lock);
315321
return cur;
316322
}

core/iwasm/common/wasm_memory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
4545
WASMSharedHeap *
4646
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
4747

48+
WASMSharedHeap *
49+
wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body);
50+
51+
WASMSharedHeap *
52+
wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain);
53+
4854
bool
4955
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
5056
WASMSharedHeap *shared_heap);

core/iwasm/include/wasm_export.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ typedef enum {
336336

337337
typedef struct SharedHeapInitArgs {
338338
uint32_t size;
339+
void *pre_allocated_addr;
339340
} SharedHeapInitArgs;
340341

341342
/**
@@ -2258,7 +2259,37 @@ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
22582259
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
22592260

22602261
/**
2261-
* Attach a shared heap to a module instance
2262+
* This function links two shared heaps, `head` and `body`, where `head` is a
2263+
* shared heap and `body` can be shared heap chain head, into a single chain.
2264+
* The `head` heap will be the head of the chain, and the `body` heap will be
2265+
* appended to it. At most one shared heap in shared heap chain can be
2266+
* dynamically allocated, the rest have to be the pre-allocated shared heap *
2267+
*
2268+
* @param head The head of the shared heap chain.
2269+
* @param body The body of the shared heap chain to be appended.
2270+
* @return The new head of the shared heap chain. NULL if failed.
2271+
*/
2272+
WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
2273+
wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
2274+
wasm_shared_heap_t body);
2275+
2276+
/**
2277+
* This function unchains the shared heaps from the given head. If
2278+
* `entire_chain` is true, it will unchain the entire chain of shared heaps.
2279+
* Otherwise, it will unchain only the first shared heap in the chain.
2280+
*
2281+
* @param head The head of the shared heap chain.
2282+
* @param entire_chain A boolean flag indicating whether to unchain the entire
2283+
* chain.
2284+
* @return The new head of the shared heap chain. Or the last shared heap in the
2285+
* chain if `entire_chain` is true.
2286+
*/
2287+
wasm_shared_heap_t
2288+
wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
2289+
2290+
/**
2291+
* Attach a shared heap, it can be the head of shared heap chain, in that case,
2292+
* attach the shared heap chain, to a module instance
22622293
*
22632294
* @param module_inst the module instance
22642295
* @param shared_heap the shared heap
@@ -2277,7 +2308,8 @@ WASM_RUNTIME_API_EXTERN void
22772308
wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst);
22782309

22792310
/**
2280-
* Allocate memory from a shared heap
2311+
* Allocate memory from a shared heap, or the non-preallocated shared heap from
2312+
* the shared heap chain
22812313
*
22822314
* @param module_inst the module instance
22832315
* @param size required memory size
@@ -2294,7 +2326,8 @@ wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
22942326
void **p_native_addr);
22952327

22962328
/**
2297-
* Free the memory allocated from shared heap
2329+
* Free the memory allocated from shared heap, or the non-preallocated shared
2330+
* heap from the shared heap chain
22982331
*
22992332
* @param module_inst the module instance
23002333
* @param ptr the offset in wasm app

0 commit comments

Comments
 (0)