Skip to content

Commit bcc9ab8

Browse files
committed
refactor shared heap api and test case
1 parent b937769 commit bcc9ab8

File tree

4 files changed

+60
-86
lines changed

4 files changed

+60
-86
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -358,24 +358,27 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
358358
return cur;
359359
}
360360

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)
361+
/* Destroy and recycle a shared heap(or head of shared heap chain), return
362+
* next node in the shared heap chain through new_head */
363+
static bool
364+
wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head,
365+
WASMSharedHeap **new_head)
365366
{
366-
WASMSharedHeap *cur = NULL, *prev = NULL, *new_head = NULL;
367+
WASMSharedHeap *cur = NULL, *prev = NULL, *next_head = NULL;
367368
bool head_found = false, is_chain_head = true;
368369

369370
if (!head) {
370371
LOG_WARNING("Invalid shared heap to destroy.");
371-
return NULL;
372+
return false;
372373
}
373374

375+
*new_head = NULL;
376+
374377
os_mutex_lock(&shared_heap_list_lock);
375378
if (head->attached_count != 0) {
376379
LOG_WARNING("To destroy shared heap, it needs to be detached first.");
377380
os_mutex_unlock(&shared_heap_list_lock);
378-
return NULL;
381+
return false;
379382
}
380383

381384
for (cur = shared_heap_list; cur; cur = cur->next) {
@@ -387,16 +390,16 @@ wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head)
387390
if (!head_found) {
388391
LOG_WARNING("Shared heap %p isn't tracked by runtime.", head);
389392
os_mutex_unlock(&shared_heap_list_lock);
390-
return NULL;
393+
return false;
391394
}
392395
if (!is_chain_head) {
393396
LOG_WARNING("Shared heap %p isn't the head of a shared heap chain.",
394397
head);
395398
os_mutex_unlock(&shared_heap_list_lock);
396-
return NULL;
399+
return false;
397400
}
398401

399-
new_head = head->chain_next;
402+
next_head = head->chain_next;
400403
if (head == shared_heap_list) {
401404
shared_heap_list = head->next;
402405
destroy_shared_heap_node(head);
@@ -419,18 +422,29 @@ wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head)
419422

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

422-
return new_head;
425+
*new_head = next_head;
426+
427+
return true;
423428
}
424429

425-
WASMSharedHeap *
426-
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain)
430+
bool
431+
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain,
432+
WASMSharedHeap **new_head)
427433
{
428-
WASMSharedHeap *new_head = NULL;
434+
WASMSharedHeap *next_head = NULL;
435+
429436
do {
430-
new_head = wasm_runtime_destroy_shared_heap_head(head);
431-
} while (entire_chain && new_head);
437+
if (!wasm_runtime_destroy_shared_heap_head(head, &next_head)) {
438+
return false;
439+
}
440+
head = next_head;
441+
} while (entire_chain && head);
432442

433-
return new_head;
443+
if (new_head) {
444+
*new_head = next_head;
445+
}
446+
447+
return true;
434448
}
435449

436450
static uint8 *

core/iwasm/common/wasm_memory.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +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);
98+
bool
99+
wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain,
100+
WASMSharedHeap **new_head);
100101

101102
bool
102103
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,

core/iwasm/include/wasm_export.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,17 +2440,19 @@ wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
24402440
/**
24412441
* Destroy a shared heap or shared heap chain starting from the given head.
24422442
* If `entire_chain` is true, destroy the whole chain; otherwise, destroy only
2443-
* the head and return the new head of the chain. The shared heap chain must be
2444-
* detached before destruction, and only the chain head registered with the
2445-
* runtime can be destroyed.
2443+
* the head and return the new head of the chain through `new_head`. The shared
2444+
* heap chain must be detached before destruction, and only the chain head
2445+
* registered with the runtime can be destroyed.
24462446
*
24472447
* @param head The head of the shared heap chain to be destroyed.
24482448
* @param entire_chain Whether to destroy the entire chain.
2449-
* @return The new head of the shared heap chain when `entire_chain` is false;
2450-
* NULL otherwise.
2449+
* @param new_head The new head of the shared heap chain when `entire_chain` is
2450+
* false; NULL otherwise.
2451+
* @return True if destruction succeeds; false otherwise.
24512452
*/
2452-
WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
2453-
wasm_runtime_destroy_shared_heap(wasm_shared_heap_t head, bool entire_chain);
2453+
WASM_RUNTIME_API_EXTERN bool
2454+
wasm_runtime_destroy_shared_heap(wasm_shared_heap_t head, bool entire_chain,
2455+
wasm_shared_heap_t *new_head);
24542456

24552457
/**
24562458
* Attach a shared heap, it can be the head of shared heap chain, in that case,

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,9 @@ TEST_F(shared_heap_test, test_destroy_shared_heap_head_only)
334334

335335
ASSERT_NE(nullptr, second);
336336

337-
WASMSharedHeap *new_head =
338-
wasm_runtime_destroy_shared_heap(shared_heap_chain, false);
337+
WASMSharedHeap *new_head = nullptr;
338+
ASSERT_TRUE(
339+
wasm_runtime_destroy_shared_heap(shared_heap_chain, false, &new_head));
339340

340341
ASSERT_EQ(second, new_head);
341342
ASSERT_EQ(nullptr, new_head->chain_next);
@@ -355,8 +356,10 @@ TEST_F(shared_heap_test, test_destroy_shared_heap_entire_chain)
355356
FAIL() << "Failed to create shared heap";
356357
}
357358

358-
EXPECT_EQ(nullptr,
359-
wasm_runtime_destroy_shared_heap(shared_heap_chain, true));
359+
WASMSharedHeap *new_head = nullptr;
360+
EXPECT_TRUE(
361+
wasm_runtime_destroy_shared_heap(shared_heap_chain, true, &new_head));
362+
EXPECT_EQ(nullptr, new_head);
360363
}
361364

362365
TEST_F(shared_heap_test, test_destroy_shared_heap_not_chain_head)
@@ -372,14 +375,16 @@ TEST_F(shared_heap_test, test_destroy_shared_heap_not_chain_head)
372375

373376
ASSERT_NE(nullptr, body);
374377

375-
EXPECT_EQ(nullptr, wasm_runtime_destroy_shared_heap(body, true));
378+
WASMSharedHeap *new_head = nullptr;
379+
EXPECT_FALSE(wasm_runtime_destroy_shared_heap(body, true, &new_head));
376380
EXPECT_EQ(body, shared_heap_chain->chain_next);
377381

378382
test_shared_heap(shared_heap_chain, "test.wasm", "test", 0, argv);
379383
EXPECT_EQ(10, argv[0]);
380384

381-
EXPECT_EQ(nullptr,
382-
wasm_runtime_destroy_shared_heap(shared_heap_chain, true));
385+
EXPECT_TRUE(
386+
wasm_runtime_destroy_shared_heap(shared_heap_chain, true, &new_head));
387+
EXPECT_EQ(nullptr, new_head);
383388
}
384389

385390
TEST_F(shared_heap_test, test_destroy_shared_heap_when_attached)
@@ -395,63 +400,15 @@ TEST_F(shared_heap_test, test_destroy_shared_heap_when_attached)
395400
ASSERT_TRUE(wasm_runtime_attach_shared_heap(module_env.wasm_module_inst,
396401
shared_heap));
397402

398-
EXPECT_EQ(nullptr, wasm_runtime_destroy_shared_heap(shared_heap, true));
403+
WASMSharedHeap *new_head = nullptr;
404+
EXPECT_FALSE(
405+
wasm_runtime_destroy_shared_heap(shared_heap, true, &new_head));
399406

400407
wasm_runtime_detach_shared_heap(module_env.wasm_module_inst);
401408
destroy_module_env(module_env);
402409

403-
EXPECT_EQ(nullptr, wasm_runtime_destroy_shared_heap(shared_heap, true));
404-
}
405-
406-
TEST_F(shared_heap_test, test_destroy_invalid_shared_heap)
407-
{
408-
WASMSharedHeap fake_shared_heap = {};
409-
410-
EXPECT_EQ(nullptr,
411-
wasm_runtime_destroy_shared_heap(&fake_shared_heap, true));
412-
}
413-
414-
TEST_F(shared_heap_test, test_destroy_shared_heap_untracked_next_head_only)
415-
{
416-
WASMSharedHeap *shared_heap_chain = nullptr;
417-
WASMSharedHeap *body = nullptr;
418-
WASMSharedHeap fake_shared_heap = {};
419-
uint32 BUF_SIZE = os_getpagesize();
420-
421-
create_test_shared_heap_chain(preallocated_buf, BUF_SIZE, preallocated_buf2,
422-
BUF_SIZE, &shared_heap_chain);
423-
body = shared_heap_chain->chain_next;
424-
425-
ASSERT_NE(nullptr, body);
426-
427-
shared_heap_chain->chain_next = &fake_shared_heap;
428-
EXPECT_EQ(NULL, wasm_runtime_destroy_shared_heap(shared_heap_chain, false));
429-
shared_heap_chain->chain_next = body;
430-
431-
EXPECT_EQ(body, wasm_runtime_destroy_shared_heap(shared_heap_chain, false));
432-
EXPECT_EQ(nullptr, wasm_runtime_destroy_shared_heap(body, true));
433-
}
434-
435-
TEST_F(shared_heap_test, test_destroy_shared_heap_untracked_next_entire_chain)
436-
{
437-
WASMSharedHeap *shared_heap_chain = nullptr;
438-
WASMSharedHeap *body = nullptr;
439-
WASMSharedHeap fake_shared_heap = {};
440-
uint32 BUF_SIZE = os_getpagesize();
441-
442-
create_test_shared_heap_chain(NULL, BUF_SIZE, NULL, BUF_SIZE,
443-
&shared_heap_chain);
444-
body = shared_heap_chain->chain_next;
445-
446-
ASSERT_NE(nullptr, body);
447-
448-
shared_heap_chain->chain_next = &fake_shared_heap;
449-
EXPECT_EQ(nullptr,
450-
wasm_runtime_destroy_shared_heap(shared_heap_chain, true));
451-
452-
shared_heap_chain->chain_next = body;
453-
EXPECT_EQ(nullptr,
454-
wasm_runtime_destroy_shared_heap(shared_heap_chain, true));
410+
EXPECT_TRUE(wasm_runtime_destroy_shared_heap(shared_heap, true, &new_head));
411+
EXPECT_EQ(nullptr, new_head);
455412
}
456413

457414
TEST_F(shared_heap_test, test_shared_heap_rmw)

0 commit comments

Comments
 (0)