Skip to content

Commit 95157c2

Browse files
lum1n0usclaude
andcommitted
refactor: improve code quality in aligned allocation
- Simplify wasm_runtime_aligned_alloc_internal control flow using guard clause - Remove redundant wasm_runtime_init() calls in tests (wasm_runtime_full_init handles it) No functional changes, improves code readability and follows existing patterns. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3aedbba commit 95157c2

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,14 +1038,14 @@ wasm_runtime_aligned_alloc_internal(unsigned int size, unsigned int alignment)
10381038
"initialized.\n");
10391039
return NULL;
10401040
}
1041-
else if (memory_mode == MEMORY_MODE_POOL) {
1042-
return mem_allocator_malloc_aligned(pool_allocator, size, alignment);
1043-
}
1044-
else {
1041+
1042+
if (memory_mode != MEMORY_MODE_POOL) {
10451043
LOG_ERROR("wasm_runtime_aligned_alloc failed: only supported in POOL "
10461044
"memory mode.\n");
10471045
return NULL;
10481046
}
1047+
1048+
return mem_allocator_malloc_aligned(pool_allocator, size, alignment);
10491049
}
10501050

10511051
void *

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ test_wasm_runtime_aligned_alloc_valid(void **state)
378378
init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024);
379379
init_args.mem_alloc_option.pool.heap_size = 256 * 1024;
380380

381-
assert_true(wasm_runtime_init());
382381
assert_true(wasm_runtime_full_init(&init_args));
383382

384383
/* Test valid aligned allocation */
@@ -405,7 +404,6 @@ test_wasm_runtime_aligned_alloc_zero_size(void **state)
405404
init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024);
406405
init_args.mem_alloc_option.pool.heap_size = 256 * 1024;
407406

408-
assert_true(wasm_runtime_init());
409407
assert_true(wasm_runtime_full_init(&init_args));
410408

411409
/* Zero size should return NULL */
@@ -428,7 +426,6 @@ test_wasm_runtime_aligned_alloc_zero_alignment(void **state)
428426
init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024);
429427
init_args.mem_alloc_option.pool.heap_size = 256 * 1024;
430428

431-
assert_true(wasm_runtime_init());
432429
assert_true(wasm_runtime_full_init(&init_args));
433430

434431
/* Zero alignment should return NULL */
@@ -449,7 +446,6 @@ test_wasm_runtime_aligned_alloc_system_mode(void **state)
449446
memset(&init_args, 0, sizeof(RuntimeInitArgs));
450447
init_args.mem_alloc_type = Alloc_With_System_Allocator;
451448

452-
assert_true(wasm_runtime_init());
453449
assert_true(wasm_runtime_full_init(&init_args));
454450

455451
/* Should return NULL in non-POOL mode */
@@ -471,7 +467,6 @@ test_wasm_runtime_realloc_rejects_aligned(void **state)
471467
init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024);
472468
init_args.mem_alloc_option.pool.heap_size = 256 * 1024;
473469

474-
assert_true(wasm_runtime_init());
475470
assert_true(wasm_runtime_full_init(&init_args));
476471

477472
/* Allocate with alignment */
@@ -502,7 +497,6 @@ test_wasm_runtime_aligned_alloc_multiple_alignments(void **state)
502497
init_args.mem_alloc_option.pool.heap_buf = malloc(512 * 1024);
503498
init_args.mem_alloc_option.pool.heap_size = 512 * 1024;
504499

505-
assert_true(wasm_runtime_init());
506500
assert_true(wasm_runtime_full_init(&init_args));
507501

508502
for (int i = 0; i < num_alignments; i++) {

0 commit comments

Comments
 (0)