On Thu, 20 Nov 2025 14:59:34 GMT, Jorn Vernee <[email protected]> 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