On Mon, 17 Nov 2025 15:03:15 GMT, Jorn Vernee <[email protected]> wrote:

>> Harald Eilertsen has updated the pull request incrementally with one 
>> additional commit since the last revision:
>> 
>>   Fix calculation of allocationSize when byteAlignment < MAX_MALLOC_ALIGN
>>   
>>   This work was sponsored by: The FreeBSD Foundation
>
> src/java.base/share/classes/jdk/internal/foreign/SegmentFactories.java line 
> 207:
> 
>> 205:         long result;
>> 206:         if (byteAlignment > MAX_MALLOC_ALIGN || alignedSize % 
>> byteAlignment != 0) {
>> 207:             allocationSize = alignedSize + byteAlignment - 
>> Math.min(MAX_MALLOC_ALIGN, alignedSize);
> 
> This doesn't look correct to me. Let's say we want to do a 7 byte allocation 
> (with `init == false`), aligned to 4. We get `7 % 4 -> 3` so we enter this 
> branch, and then `allocationSize = 7 + 4 - 7`, and we end up allocating only 
> 4 bytes. The previous computation only works if `MAX_MALLOC_ALIGN` is greater 
> than `byteAlignment`.
> 
> I think it's simpler to just replace `MAX_MALLOC_ALIGN` in this `if` 
> statement with an 'expected alignment' derived from the size.

Maybe something like `expectedAlignment = Math.min(MAX_MALLOC_ALIGN, 
alignedSize % byteAlignment == 0 ?alignedSize : 0)` (i.e. don't assume any 
alignment for e.g. 7 byte allocations.

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

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

Reply via email to