[foreign] RFR: 8226555: Provide java.foreign.memory.Array a copy function similar to System.arraycopy

Henry Jen henry.jen at oracle.com
Fri Jun 21 06:59:25 UTC 2019


Hi,

Please review a webrev[1] to provide more flexible way to copy between native and native/java arrays.

This webrev changes Array.assign behavior to make it more consistent:

1. It add a slow-path copy for assign between native arrays. Given the carrier type is the same, in case the native layout doesn’t match, it can fallback to sequential copy, which have been available for assign between native and java arrays.

2. It allows copy source array in full to a larger destination array. Before the patch, only slow path between native and java array can do that. Otherwise, it will have IllegalArgumentException caused by different layout.

This webrev make assign work consistently between all three modes so that, it will copy full source array into destination that is large enough, or throw IndexOutOfBoundException if the destination is too small.

Do check out the test case, that would help to clarify the specs if it’s not clear enough.

There is a minor irritation for the API in that, it can be ambiguous between Array and Object, as you can see following example.

/Users/hjen/ws/panama.dev/test/jdk/java/foreign/memory/ArrayTest.java:360: error: reference to copy is ambiguous
            Array.copy(EMPTY_INT_ARRAY, 0, naInt, 0, 0);
                 ^
  both method <Z#1>copy(Array<Z#1>,long,Object,int,int) in Array and method <Z#2>copy(Object,int,Array<Z#2>,long,int) in Array match
  where Z#1,Z#2 are type-variables:
    Z#1 extends Object declared in method <Z#1>copy(Array<Z#1>,long,Object,int,int)
    Z#2 extends Object declared in method <Z#2>copy(Object,int,Array<Z#2>,long,int)

I am not sure why javac pick those two, while I actually want the third one,

copy(Array<Z>, long, Array<Z>, long, long);

A simple fix is simply to cast the last argument.
            Array.copy(EMPTY_INT_ARRAY, 0, naInt, 0, 0L);

Perhaps we can simply rename cop between Array to copyNative or something. Suggestions?

Cheers,
Henry

[1] http://cr.openjdk.java.net/~henryjen/panama/8226555/0/webrev/


More information about the panama-dev mailing list