Part 1 Proposal for JDK-8264594

leerho leerho at gmail.com
Thu May 20 16:13:48 UTC 2021


Maurizio,
Ah, but I forgot to include a source index offset and a copy length instead
of array length.  Extending your example to a full template:

```
//Given source info:
double[] srcArr = ...
int srcCopyLen = ... //units of double
int srcIndex = ... //units of double

//Given destination info:
MemorySegment dstSeg = ... //big enough
long dstOffsetBytes = ...

//In MemoryCopy:
public static void copyFromArray(

double[] srcArr, int srcIndex, int srcCopyLen,

MemorySegment dstSeg, long dstOffsetBytes, ByteOrder order) {

MemorySegment srcSegSlice =
MemorySegment.ofArray(srcArr).asSlice(srcIndex*8, srcCopyLen*8);
Memory Segment dstSegSlice = dstSeg.asSlice(dstOffsetBytes, srcCopyLen*8);
dstSegSlice.copyFrom(srcSegSlice, MemoryLayouts.JAVA_DOUBLE, order);

}
```
I am not sure if the *dstSegSlice* requires the *srcCopyLen*. I would hope
that it is smart enough to realize that the input length is smaller than
the given offset minus the segment size.

Lee.

On Thu, May 20, 2021 at 2:11 AM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

>
> 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