RFR: 8371637: allocateNativeInternal sometimes return incorrectly aligned memory [v6]

Jorn Vernee jvernee at openjdk.org
Thu Nov 20 15:05:23 UTC 2025


On Thu, 20 Nov 2025 14:59:34 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> src/java.base/share/classes/jdk/internal/foreign/SegmentFactories.java line 215:
>> 
>>> 213:             result = Utils.alignUp(allocationBase, byteAlignment);
>>> 214:         } else {
>>> 215:             allocationSize = Math.max(alignedSize, byteAlignment);
>> 
>> This looks correct but it really needs a comment.
>
> Suggestion:
> 
>             // always allocate at least 'byteAlignment' bytes, so that malloc is guaranteed to 
>             // return a pointer aligned to that alignment, for cases where byteAlignment > alignedSize
>             allocationSize = Math.max(alignedSize, byteAlignment);

Also, just a thought: theoretically we would be allocating 'too much', e.g. in a case where we need 1 byte with 16 byte alignment, in cases where malloc would already return a 16 byte aligned address. But I don't think it matters in practice since a 16 byte alignment in the implementation means that malloc can't use the 'other' 15 bytes after a 1 byte allocation any ways, since those addresses are not 16 byte aligned.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28235#discussion_r2546430391


More information about the core-libs-dev mailing list