Skip to content

Commit 93d9299

Browse files
committed
add new runtime api for reset shared heap(chain)
1 parent 02c2550 commit 93d9299

File tree

3 files changed

+114
-37
lines changed

3 files changed

+114
-37
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 100 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
165228
WASMSharedHeap *
166229
wasm_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);
249285
fail2:
250286
wasm_runtime_free(heap);
251287
fail1:
@@ -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+
342412
static uint8 *
343413
get_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
}

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+
bool
99+
wasm_runtime_reset_shared_heap_chain(WASMSharedHeap *shared_heap);
100+
98101
bool
99102
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
100103
WASMSharedHeap *shared_heap);

core/iwasm/include/wasm_export.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,17 @@ wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
24372437
wasm_shared_heap_t
24382438
wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
24392439

2440+
/**
2441+
* Reset shared heap chain. For each shared heap in the chain, if it is a
2442+
* pre-allocated shared heap, its memory region will be zeroed. For a
2443+
* WAMR-managed shared heap, it will be destroyed and reinitialized.
2444+
*
2445+
* @param shared_heap The head of the shared heap chain.
2446+
* @return true if success, false otherwise.
2447+
*/
2448+
WASM_RUNTIME_API_EXTERN bool
2449+
wasm_runtime_reset_shared_heap_chain(wasm_shared_heap_t shared_heap);
2450+
24402451
/**
24412452
* Attach a shared heap, it can be the head of shared heap chain, in that case,
24422453
* attach the shared heap chain, to a module instance

0 commit comments

Comments
 (0)