Skip to content

Commit 10808b1

Browse files
committed
add runtime API for shared memory destroy and recycle, and some test cases
1 parent f6ce52f commit 10808b1

File tree

4 files changed

+295
-65
lines changed

4 files changed

+295
-65
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ static void
148148
wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size,
149149
uint64 map_size);
150150

151+
static void
152+
destroy_shared_heap_node(WASMSharedHeap *heap)
153+
{
154+
uint64 map_size;
155+
156+
if (heap->heap_handle) {
157+
mem_allocator_destroy(heap->heap_handle);
158+
wasm_runtime_free(heap->heap_handle);
159+
#ifndef OS_ENABLE_HW_BOUND_CHECK
160+
map_size = heap->size;
161+
#else
162+
map_size = 8 * (uint64)BH_GB;
163+
#endif
164+
wasm_munmap_linear_memory(heap->base_addr, heap->size, map_size);
165+
}
166+
167+
wasm_runtime_free(heap);
168+
}
169+
151170
static void *
152171
runtime_malloc(uint64 size)
153172
{
@@ -339,6 +358,81 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
339358
return cur;
340359
}
341360

361+
/* Destroy and recycle a shared heap(or head of shared heap chain), return next
362+
* node in the shared heap chain */
363+
static WASMSharedHeap *
364+
wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head)
365+
{
366+
WASMSharedHeap *cur = NULL, *prev = NULL, *new_head = NULL;
367+
bool head_found = false, is_chain_head = true;
368+
369+
if (!head) {
370+
LOG_WARNING("Invalid shared heap to destroy.");
371+
return NULL;
372+
}
373+
374+
os_mutex_lock(&shared_heap_list_lock);
375+
if (head->attached_count != 0) {
376+
LOG_WARNING("To destroy shared heap, it needs to be detached first.");
377+
os_mutex_unlock(&shared_heap_list_lock);
378+
return NULL;
379+
}
380+
381+
for (cur = shared_heap_list; cur; cur = cur->next) {
382+
if (cur == head)
383+
head_found = true;
384+
if (cur->chain_next == head)
385+
is_chain_head = false;
386+
}
387+
if (!head_found) {
388+
LOG_WARNING("Shared heap %p isn't tracked by runtime.", head);
389+
os_mutex_unlock(&shared_heap_list_lock);
390+
return NULL;
391+
}
392+
if (!is_chain_head) {
393+
LOG_WARNING("Shared heap %p isn't the head of a shared heap chain.",
394+
head);
395+
os_mutex_unlock(&shared_heap_list_lock);
396+
return NULL;
397+
}
398+
399+
new_head = head->chain_next;
400+
if (head == shared_heap_list) {
401+
shared_heap_list = head->next;
402+
destroy_shared_heap_node(head);
403+
}
404+
else {
405+
prev = shared_heap_list;
406+
cur = shared_heap_list->next;
407+
while (prev) {
408+
if (head == cur) {
409+
prev->next = head->next;
410+
destroy_shared_heap_node(cur);
411+
break;
412+
}
413+
prev = prev->next;
414+
cur = cur->next;
415+
}
416+
}
417+
418+
os_mutex_unlock(&shared_heap_list_lock);
419+
420+
LOG_VERBOSE("Destroyed shared heap %p", head);
421+
422+
return new_head;
423+
}
424+
425+
WASMSharedHeap *
426+
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain)
427+
{
428+
WASMSharedHeap *new_head = NULL;
429+
do {
430+
new_head = wasm_runtime_destroy_shared_heap_head(head);
431+
} while (entire_chain && new_head);
432+
433+
return new_head;
434+
}
435+
342436
static uint8 *
343437
get_last_used_shared_heap_base_addr_adj(WASMModuleInstanceCommon *module_inst)
344438
{
@@ -819,7 +913,6 @@ destroy_shared_heaps()
819913
{
820914
WASMSharedHeap *heap;
821915
WASMSharedHeap *cur;
822-
uint64 map_size;
823916

824917
os_mutex_lock(&shared_heap_list_lock);
825918
heap = shared_heap_list;
@@ -829,17 +922,7 @@ destroy_shared_heaps()
829922
while (heap) {
830923
cur = heap;
831924
heap = heap->next;
832-
if (cur->heap_handle) {
833-
mem_allocator_destroy(cur->heap_handle);
834-
wasm_runtime_free(cur->heap_handle);
835-
#ifndef OS_ENABLE_HW_BOUND_CHECK
836-
map_size = cur->size;
837-
#else
838-
map_size = 8 * (uint64)BH_GB;
839-
#endif
840-
wasm_munmap_linear_memory(cur->base_addr, cur->size, map_size);
841-
}
842-
wasm_runtime_free(cur);
925+
destroy_shared_heap_node(cur);
843926
}
844927
os_mutex_destroy(&shared_heap_list_lock);
845928
}

core/iwasm/common/wasm_memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body);
9595
WASMSharedHeap *
9696
wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain);
9797

98+
WASMSharedHeap *
99+
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain);
100+
98101
bool
99102
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
100103
WASMSharedHeap *shared_heap);

core/iwasm/include/wasm_export.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,21 @@ wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
24052405
wasm_shared_heap_t
24062406
wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
24072407

2408+
/**
2409+
* Destroy a shared heap or shared heap chain starting from the given head.
2410+
* If `entire_chain` is true, destroy the whole chain; otherwise, destroy only
2411+
* the head and return the new head of the chain. The shared heap chain must be
2412+
* detached before destruction, and only the chain head registered with the
2413+
* runtime can be destroyed.
2414+
*
2415+
* @param head The head of the shared heap chain to be destroyed.
2416+
* @param entire_chain Whether to destroy the entire chain.
2417+
* @return The new head of the shared heap chain when `entire_chain` is false;
2418+
* NULL otherwise.
2419+
*/
2420+
WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
2421+
wasm_runtime_destroy_shared_heap(wasm_shared_heap_t head, bool entire_chain);
2422+
24082423
/**
24092424
* Attach a shared heap, it can be the head of shared heap chain, in that case,
24102425
* attach the shared heap chain, to a module instance

0 commit comments

Comments
 (0)