Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 109 additions & 12 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ static void
wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size,
uint64 map_size);

static void
destroy_shared_heap_node(WASMSharedHeap *heap)
{
uint64 map_size;

if (heap->heap_handle) {
mem_allocator_destroy(heap->heap_handle);
wasm_runtime_free(heap->heap_handle);
#ifndef OS_ENABLE_HW_BOUND_CHECK
map_size = heap->size;
#else
map_size = 8 * (uint64)BH_GB;
#endif
wasm_munmap_linear_memory(heap->base_addr, heap->size, map_size);
}

wasm_runtime_free(heap);
}

static void *
runtime_malloc(uint64 size)
{
Expand Down Expand Up @@ -339,6 +358,95 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
return cur;
}

/* Destroy and recycle a shared heap(or head of shared heap chain), return
* next node in the shared heap chain through new_head */
static bool
wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head,
WASMSharedHeap **new_head)
{
WASMSharedHeap *cur = NULL, *prev = NULL, *next_head = NULL;
bool head_found = false, is_chain_head = true;

if (!head) {
LOG_WARNING("Invalid shared heap to destroy.");
return false;
}

*new_head = NULL;

os_mutex_lock(&shared_heap_list_lock);
if (head->attached_count != 0) {
LOG_WARNING("To destroy shared heap, it needs to be detached first.");
os_mutex_unlock(&shared_heap_list_lock);
return false;
}

for (cur = shared_heap_list; cur; cur = cur->next) {
if (cur == head)
head_found = true;
if (cur->chain_next == head)
is_chain_head = false;
}
if (!head_found) {
LOG_WARNING("Shared heap %p isn't tracked by runtime.", head);
os_mutex_unlock(&shared_heap_list_lock);
return false;
}
if (!is_chain_head) {
LOG_WARNING("Shared heap %p isn't the head of a shared heap chain.",
head);
os_mutex_unlock(&shared_heap_list_lock);
return false;
}

next_head = head->chain_next;
if (head == shared_heap_list) {
shared_heap_list = head->next;
destroy_shared_heap_node(head);
}
else {
prev = shared_heap_list;
cur = shared_heap_list->next;
while (prev) {
if (head == cur) {
prev->next = head->next;
destroy_shared_heap_node(cur);
break;
}
prev = prev->next;
cur = cur->next;
}
}

os_mutex_unlock(&shared_heap_list_lock);

LOG_VERBOSE("Destroyed shared heap %p", head);

*new_head = next_head;

return true;
}

bool
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain,
WASMSharedHeap **new_head)
{
WASMSharedHeap *next_head = NULL;

do {
if (!wasm_runtime_destroy_shared_heap_head(head, &next_head)) {
return false;
}
head = next_head;
} while (entire_chain && head);

if (new_head) {
*new_head = next_head;
}

return true;
}

static uint8 *
get_last_used_shared_heap_base_addr_adj(WASMModuleInstanceCommon *module_inst)
{
Expand Down Expand Up @@ -819,7 +927,6 @@ destroy_shared_heaps()
{
WASMSharedHeap *heap;
WASMSharedHeap *cur;
uint64 map_size;

os_mutex_lock(&shared_heap_list_lock);
heap = shared_heap_list;
Expand All @@ -829,17 +936,7 @@ destroy_shared_heaps()
while (heap) {
cur = heap;
heap = heap->next;
if (cur->heap_handle) {
mem_allocator_destroy(cur->heap_handle);
wasm_runtime_free(cur->heap_handle);
#ifndef OS_ENABLE_HW_BOUND_CHECK
map_size = cur->size;
#else
map_size = 8 * (uint64)BH_GB;
#endif
wasm_munmap_linear_memory(cur->base_addr, cur->size, map_size);
}
wasm_runtime_free(cur);
destroy_shared_heap_node(cur);
}
os_mutex_destroy(&shared_heap_list_lock);
}
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/common/wasm_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body);
WASMSharedHeap *
wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain);

bool
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain,
WASMSharedHeap **new_head);

bool
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
WASMSharedHeap *shared_heap);
Expand Down
17 changes: 17 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,23 @@ wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
wasm_shared_heap_t
wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);

/**
* Destroy a shared heap or shared heap chain starting from the given head.
* If `entire_chain` is true, destroy the whole chain; otherwise, destroy only
* the head and return the new head of the chain through `new_head`. The shared
* heap chain must be detached before destruction, and only the chain head
* registered with the runtime can be destroyed.
*
* @param head The head of the shared heap chain to be destroyed.
* @param entire_chain Whether to destroy the entire chain.
* @param new_head The new head of the shared heap chain when `entire_chain` is
* false; NULL otherwise.
* @return True if destruction succeeds; false otherwise.
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_destroy_shared_heap(wasm_shared_heap_t head, bool entire_chain,
wasm_shared_heap_t *new_head);

/**
* 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
Expand Down
Loading
Loading