Skip to content

Commit 639d6ba

Browse files
lum1n0usclaude
andcommitted
feat(api): expose aligned allocation through wasm_runtime_aligned_alloc
Add public API for aligned memory allocation, exposing the existing mem_allocator_malloc_aligned infrastructure through wasm_export.h. - Add wasm_runtime_aligned_alloc() API declaration with documentation - Implement internal helper wasm_runtime_aligned_alloc_internal() - Add public function with size/alignment validation - POOL mode only, returns NULL for other memory modes - Follows wasm_runtime_malloc() patterns for consistency Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 1ad2fad commit 639d6ba

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,24 @@ wasm_runtime_free_internal(void *ptr)
10301030
}
10311031
}
10321032

1033+
static inline void *
1034+
wasm_runtime_aligned_alloc_internal(unsigned int size, unsigned int alignment)
1035+
{
1036+
if (memory_mode == MEMORY_MODE_UNKNOWN) {
1037+
LOG_ERROR("wasm_runtime_aligned_alloc failed: memory hasn't been "
1038+
"initialized.\n");
1039+
return NULL;
1040+
}
1041+
else if (memory_mode == MEMORY_MODE_POOL) {
1042+
return mem_allocator_malloc_aligned(pool_allocator, size, alignment);
1043+
}
1044+
else {
1045+
LOG_ERROR("wasm_runtime_aligned_alloc failed: only supported in POOL "
1046+
"memory mode.\n");
1047+
return NULL;
1048+
}
1049+
}
1050+
10331051
void *
10341052
wasm_runtime_malloc(unsigned int size)
10351053
{
@@ -1052,6 +1070,29 @@ wasm_runtime_malloc(unsigned int size)
10521070
return wasm_runtime_malloc_internal(size);
10531071
}
10541072

1073+
void *
1074+
wasm_runtime_aligned_alloc(unsigned int size, unsigned int alignment)
1075+
{
1076+
if (size == 0) {
1077+
LOG_WARNING("warning: wasm_runtime_aligned_alloc with size zero\n");
1078+
return NULL;
1079+
}
1080+
1081+
if (alignment == 0) {
1082+
LOG_WARNING("warning: wasm_runtime_aligned_alloc with zero alignment\n");
1083+
return NULL;
1084+
}
1085+
1086+
#if WASM_ENABLE_FUZZ_TEST != 0
1087+
if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
1088+
LOG_WARNING("warning: wasm_runtime_aligned_alloc with too large size\n");
1089+
return NULL;
1090+
}
1091+
#endif
1092+
1093+
return wasm_runtime_aligned_alloc_internal(size, alignment);
1094+
}
1095+
10551096
void *
10561097
wasm_runtime_realloc(void *ptr, unsigned int size)
10571098
{

core/iwasm/include/wasm_export.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ wasm_runtime_destroy(void);
422422
WASM_RUNTIME_API_EXTERN void *
423423
wasm_runtime_malloc(unsigned int size);
424424

425+
/**
426+
* Allocate memory with specified alignment from runtime memory environment.
427+
* This function mimics aligned_alloc() behavior in WebAssembly context.
428+
*
429+
* Note: Only supported in POOL memory mode. Other modes will return NULL.
430+
* Note: Allocated memory cannot be reallocated with wasm_runtime_realloc().
431+
*
432+
* @param size bytes need to allocate (must be multiple of alignment)
433+
* @param alignment alignment requirement (must be power of 2, >= 8, <= page size)
434+
*
435+
* @return the pointer to aligned memory allocated, or NULL on failure
436+
*/
437+
WASM_RUNTIME_API_EXTERN void *
438+
wasm_runtime_aligned_alloc(unsigned int size, unsigned int alignment);
439+
425440
/**
426441
* Reallocate memory from runtime memory environment
427442
*

0 commit comments

Comments
 (0)