Skip to content

Feat: aligned allocation#4886

Open
lum1n0us wants to merge 4 commits intobytecodealliance:mainfrom
lum1n0us:feat/aligned-allocation
Open

Feat: aligned allocation#4886
lum1n0us wants to merge 4 commits intobytecodealliance:mainfrom
lum1n0us:feat/aligned-allocation

Conversation

@lum1n0us
Copy link
Copy Markdown
Contributor

@lum1n0us lum1n0us commented Mar 20, 2026

Please refer to core/shared/mem-alloc/ems/ems_gc_internal.h in PR to get more details about arch.

@lum1n0us lum1n0us force-pushed the feat/aligned-allocation branch from a02ad8b to 6d08a77 Compare March 26, 2026 05:00
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>
@lum1n0us lum1n0us force-pushed the feat/aligned-allocation branch from 6d08a77 to 21857a9 Compare March 27, 2026 02:44
@lum1n0us lum1n0us marked this pull request as ready for review March 30, 2026 07:11
@lum1n0us lum1n0us linked an issue Mar 31, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@TianlongLiang TianlongLiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first round review is on the source code. I will continue to review test cases

return NULL;
}

if (size == 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we may want to consider alternative implementations returning a NULL pointer when malloc(0) is called. Current impl is perfectly fine though, just some thoughts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a separate PR for be_queue?

}

if (size > SIZE_MAX - alignment - HMU_SIZE - OBJ_PREFIX_SIZE
- OBJ_SUFFIX_SIZE - 8) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these 8 bytes? If they are used to store those metadata

    /* Store offset 8 bytes before returned pointer */
    *((uint32_t *)((char *)ret - 8)) = offset;

    /* Store magic with encoded alignment */
    *((uint32_t *)((char *)ret - 4)) =
        ALIGNED_ALLOC_MAGIC_VALUE | alignment_log2;

Would you think it's better to use a Macro for those 8 bytes(like ALIGNED_HEADER or something) and modify a corresponding comment? Currently, the offset is not in it

 * Memory Layout Diagram:
 * ----------------------
 *
 *  Low Address                                               High Address
 *  ┌─────────────┬──────────┬────────────────┬──────────────┬─────────────┐
 *HMU HeaderPaddingMagic + OffsetAligned DataPadding*  │   (meta)    │ (0-align)│    (4 bytes)   │   (size)     │  (overhead) │
 *  └─────────────┴──────────┴────────────────┴──────────────┴─────────────┘
 *                             ▲                ▲
 *                             │                │
 *                             magic_ptr        user_ptr (returned, aligned)


/* Calculate total size needed */
tot_size_unaligned =
HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE + alignment - 1 + 8;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about 8 bytes here, and it seems that duplicate overflow check with L671-L673?

if (!obj)
return false;

uint32_t *magic_ptr = (uint32_t *)((char *)obj - 4);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe it's worth considering using a macro instead of hard code offset?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't see WASM_RUNTIME_API_INTERN be used in another place. Is it simply an indicator for testing purposes, like in the mem-alloc test? Or we would actually add those prefixes in the future to internal APIs?


#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this header?

assert_int_equal(hmu_get_ut(hmu_aligned), HMU_VO);

/* Sizes should be reasonable */
assert_true(hmu_get_size(hmu_normal) >= 128);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to test the exact size? Or upper limit maybe

}
}

assert_true(count > 10); /* At least some should succeed */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the current size of heap_buf, I think maybe all should succeed

}
}

assert_true(count > 20);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, maybe we should be more aggressive?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add aligned memory allocation API (aligned_alloc)

2 participants