[foreign-memaccess+abi] RFR: 8267240: Bounded arena allocator doesn't work if bounded size > BLOCK_SIZE
Jorn Vernee
jvernee at openjdk.java.net
Mon May 17 14:44:23 UTC 2021
On Mon, 17 May 2021 11:43:05 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> This patch follows the discussion in [1], and splits the unbounded allocator from the bounded one, as attempts to reusing logic were causing issues when bounded arena allocators were created with bounded size > BLOCK_SIZE.
>
> I have looked for simplifications of the `trySlice` logic (also mentioned in the emails) - but in my opinion it cannot be simplified much. When an allocator is unbounded, if we receive a request that is more than half the size of the allocator block, we cannot guarantee that we can align it correctly in a segment whose size is BLOCK_SIZE. So, I think the limit of MAX_ALLOC_SIZE = BLOCK_SIZE / 2 is correct, in the sense that anything above that limit needs a standalone allocation (or we won't be able to align it in certain edge cases).
>
> Of course, if we didn't care about alignment, the logic could be simplified, but since this is a native allocator, I think respecting alignment of the allocation request is important.
>
> [1] - https://mail.openjdk.java.net/pipermail/panama-dev/2021-May/013796.html
src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/ArenaAllocator.java line 61:
> 59: return slice;
> 60: } else {
> 61: if (Utils.alignUp(bytesSize, bytesAlignment) > DEFAULT_MAX_ALLOC_SIZE) {
This seems to be the subject of some of the emails. I think maybe it might be worth it to break this out into an intermediate variable with a good name?
I also think the DEFAULT_MAX_ALLOC_SIZE could just be DEFAULT_BLOCK_SIZE, if we change this check to check for the maximum allocation size computed by `byteSize + bytesAlignment - 1` instead of using `alignUp`:
Suggestion:
long maxPossibleAllocationSize = bytesSize + bytesAlignment - 1;
if (maxPossibleAllocationSize > DEFAULT_BLOCK_SIZE) {
e.g. if we have an allocation aligned to 8 bytes, we can have a maximum of 7 bytes of alignment padding (in case the address is off by just 1 byte).
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/537
More information about the panama-dev
mailing list