@@ -162,10 +162,73 @@ runtime_malloc(uint64 size)
162162 return mem ;
163163}
164164
165+ static void
166+ destroy_runtime_managed_shared_heap (WASMSharedHeap * heap )
167+ {
168+ uint64 map_size ;
169+
170+ mem_allocator_destroy (heap -> heap_handle );
171+ wasm_runtime_free (heap -> heap_handle );
172+ heap -> heap_handle = NULL ;
173+
174+ #ifndef OS_ENABLE_HW_BOUND_CHECK
175+ map_size = heap -> size ;
176+ #else
177+ map_size = 8 * (uint64 )BH_GB ;
178+ #endif
179+ wasm_munmap_linear_memory (heap -> base_addr , heap -> size , map_size );
180+ heap -> base_addr = NULL ;
181+ }
182+
183+ static bool
184+ create_runtime_managed_shared_heap (WASMSharedHeap * heap ,
185+ uint64 heap_struct_size )
186+ {
187+ uint64 map_size ;
188+
189+ heap -> heap_handle = runtime_malloc (mem_allocator_get_heap_struct_size ());
190+ if (!heap -> heap_handle ) {
191+ heap -> base_addr = NULL ;
192+ return false;
193+ }
194+
195+ #ifndef OS_ENABLE_HW_BOUND_CHECK
196+ map_size = heap -> size ;
197+ #else
198+ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
199+ * ea = i + memarg.offset
200+ * both i and memarg.offset are u32 in range 0 to 4G
201+ * so the range of ea is 0 to 8G
202+ */
203+ map_size = 8 * (uint64 )BH_GB ;
204+ #endif
205+
206+ if (!(heap -> base_addr = wasm_mmap_linear_memory (map_size , heap -> size ))) {
207+ goto fail1 ;
208+ }
209+ if (!mem_allocator_create_with_struct_and_pool (
210+ heap -> heap_handle , heap_struct_size , heap -> base_addr , heap -> size )) {
211+ LOG_WARNING ("init share heap failed" );
212+ goto fail2 ;
213+ }
214+
215+ LOG_VERBOSE ("Create runtime managed shared heap %p with size %u" ,
216+ heap -> base_addr , (uint32 )heap -> size );
217+ return true;
218+
219+ fail2 :
220+ wasm_munmap_linear_memory (heap -> base_addr , heap -> size , map_size );
221+ fail1 :
222+ wasm_runtime_free (heap -> heap_handle );
223+ heap -> heap_handle = NULL ;
224+ heap -> base_addr = NULL ;
225+ return false;
226+ }
227+
165228WASMSharedHeap *
166229wasm_runtime_create_shared_heap (SharedHeapInitArgs * init_args )
167230{
168- uint64 heap_struct_size = sizeof (WASMSharedHeap ), map_size ;
231+ uint64 heap_struct_size = sizeof (WASMSharedHeap );
169232 uint32 size = init_args -> size ;
170233 WASMSharedHeap * heap ;
171234
@@ -203,32 +266,9 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
203266 heap -> base_addr , size );
204267 }
205268 else {
206- if (!(heap -> heap_handle =
207- runtime_malloc (mem_allocator_get_heap_struct_size ()))) {
269+ if (!create_runtime_managed_shared_heap (heap , heap_struct_size )) {
208270 goto fail2 ;
209271 }
210-
211- #ifndef OS_ENABLE_HW_BOUND_CHECK
212- map_size = size ;
213- #else
214- /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
215- * ea = i + memarg.offset
216- * both i and memarg.offset are u32 in range 0 to 4G
217- * so the range of ea is 0 to 8G
218- */
219- map_size = 8 * (uint64 )BH_GB ;
220- #endif
221-
222- if (!(heap -> base_addr = wasm_mmap_linear_memory (map_size , size ))) {
223- goto fail3 ;
224- }
225- if (!mem_allocator_create_with_struct_and_pool (
226- heap -> heap_handle , heap_struct_size , heap -> base_addr , size )) {
227- LOG_WARNING ("init share heap failed" );
228- goto fail4 ;
229- }
230- LOG_VERBOSE ("Create pool shared heap %p with size %u" , heap -> base_addr ,
231- size );
232272 }
233273
234274 os_mutex_lock (& shared_heap_list_lock );
@@ -242,10 +282,6 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
242282 os_mutex_unlock (& shared_heap_list_lock );
243283 return heap ;
244284
245- fail4 :
246- wasm_munmap_linear_memory (heap -> base_addr , size , map_size );
247- fail3 :
248- wasm_runtime_free (heap -> heap_handle );
249285fail2 :
250286 wasm_runtime_free (heap );
251287fail1 :
@@ -339,6 +375,40 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
339375 return cur ;
340376}
341377
378+ bool
379+ wasm_runtime_reset_shared_heap_chain (WASMSharedHeap * shared_heap )
380+ {
381+ uint64 heap_struct_size = sizeof (WASMSharedHeap );
382+ WASMSharedHeap * cur ;
383+
384+ if (!shared_heap ) {
385+ return false;
386+ }
387+
388+ os_mutex_lock (& shared_heap_list_lock );
389+ if (shared_heap -> attached_count != 0 ) {
390+ os_mutex_unlock (& shared_heap_list_lock );
391+ return false;
392+ }
393+
394+ for (cur = shared_heap ; cur ; cur = cur -> chain_next ) {
395+ if (cur -> heap_handle ) {
396+ destroy_runtime_managed_shared_heap (cur );
397+
398+ if (!create_runtime_managed_shared_heap (cur , heap_struct_size )) {
399+ os_mutex_unlock (& shared_heap_list_lock );
400+ return false;
401+ }
402+ }
403+ else {
404+ memset (cur -> base_addr , 0 , (size_t )cur -> size );
405+ }
406+ }
407+
408+ os_mutex_unlock (& shared_heap_list_lock );
409+ return true;
410+ }
411+
342412static uint8 *
343413get_last_used_shared_heap_base_addr_adj (WASMModuleInstanceCommon * module_inst )
344414{
@@ -830,14 +900,7 @@ destroy_shared_heaps()
830900 cur = heap ;
831901 heap = heap -> next ;
832902 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 );
903+ destroy_runtime_managed_shared_heap (cur );
841904 }
842905 wasm_runtime_free (cur );
843906 }
0 commit comments