Part 1 Proposal for JDK-8264594

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu May 20 09:11:12 UTC 2021


On 20/05/2021 10:00, Maurizio Cimadamore wrote:
> //Given:
> int arrLen = N;
> double[] srcArr = new double[arrLen];
> ResourceScope scope = ...
> MemorySegment dstSeg = ... big enough, but we need to load at a 
> dstOffset.
>
> //prepare the source segment:
> MemoryLayout doubleLayout = valueLayout(64, ByteOrder.nativeOrder);
> MemoryLayout seqLayout = sequenceLayout(arrLen, doubleLayout);
> MemorySegment srcSeg = MemorySegment.ofArray(srcArr);
>
> //prepare the destination segment:
> dstSegSlice = dstSeg.asSlice(dstOffset, arrLen*8);
> dstSeg.copyFrom(srcSeg, seqLayout, ByteOrder.nativeOrder);
>
> Whew!   This can't be right. I must be doing something wrong.  How did 
> you intend for this new method to be used? 

The usage here is wrong. I think something like this:

```
MemorySegment srcSeg = MemorySegment.ofArray(srcArr);
dstSegSlice = dstSeg.asSlice(dstOffset, arrLen*8);
dstSeg.copyFrom(srcSeg, MemoryLayouts.JAVA_DOUBLE, ByteOrder.nativeOrder);
```

That is, the layout parameter is an "element layout" which tells the 
copy routine what is the granularity of the "swappiness" behavior 
associated with the bulk copy (in this case, this being a double, 8 bytes).

These three lines of code will form the backbone of all the various 
static methods we can add to conveniently copy data from segments to 
arrays and back (with given byte order).

Maurizio





More information about the panama-dev mailing list