@@ -148,6 +148,25 @@ static void
148148wasm_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+
151170static void *
152171runtime_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+
342436static uint8 *
343437get_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}
0 commit comments