[foreign-abi] RFR: 8248499: Add methods to allocate off heap arrays from Java arrays

Jorn Vernee jvernee at openjdk.java.net
Tue Jun 30 12:43:34 UTC 2020


On Mon, 29 Jun 2020 19:29:08 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This patch adds methods to NativeScope which allow clients to allocate off-heap arrays from Java on-heap arrays.
> 
> While in most cases the code for copying is straightforward, we have to take into account endianness mismatches (in
> case the user wants to store the array elements off-heap in an endianness that doesn't match that of the platform).
> To allow for that, I've added a new implementation method in AbstractMemorySegmentImpl, which is the same as
> `copyFrom`, but adds the byte swap (a wrapper around `Unsafe::copySwapMemory`).

Marked as reviewed by jvernee (Committer).

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/NativeScope.java line 197:

> 196:             throw new IllegalArgumentException("Bad layout size");
> 197:         }
> 198:         MemoryAddress addr = allocate(MemoryLayout.ofSequence(array.length, elementLayout));

Can maybe be changed to
Suggestion:

        Utils.checkPrimitiveCarrierCompat(byte.class, elementLayout);

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/NativeScope.java line 247:

> 246:             ((AbstractMemorySegmentImpl)addr.segment()).copyFromSwap(MemorySegment.ofArray(array),
> elementLayout.byteSize()); 247:         }
> 248:         return addr;

This code seems to be the same in the other implementations (except for the call to MS::ofArray). Maybe you want to
factor it out into a helper method?

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java line 135:

> 134:
> 135:     public void copyFromSwap(MemorySegment src, long elemSize) {
> 136:         AbstractMemorySegmentImpl that = (AbstractMemorySegmentImpl)src;

Should this maybe be a public API in MemorySegment as well?

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

PR: https://git.openjdk.java.net/panama-foreign/pull/222


More information about the panama-dev mailing list