[foreign-memaccess+abi] RFR: 8270376: Finalize API for memory copy

Uwe Schindler uschindler at openjdk.java.net
Tue Jul 13 20:41:28 UTC 2021


On Tue, 13 Jul 2021 16:49:06 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This patch ties up some loose ends with the MemoryCopy API, and it also prepares the ground for some other related refactorings in this area.
> 
> The meat of this patch is represented by the various changes in MemoryCopy, where all methods were renamed to simply `copy`, and the length parameter (now called `elementCount`) is always moved to the end.
> 
> I've also simplified naming of parameters, as I think distinguishing between `index` and `offset` is enough (e.g. an array has an index, a segment has an offset).
> 
> You will see that, at the very end of the class, three more copy methods have been added, which deal fully in terms of segments. I have also moved the complex layout-based memory segment copy operation (which does the swap) as a static method in this class, as I believe the static form makes the method more regular and usable.
> 
> I've made some changes to our uses of `copyFrom` in the linker, to use the new methods in `MemoryCopy` when the copy operation was performing slicing in the source/target segment, which I think makes the code more readable. Of course these changes are completely optional and could be omitted as well.

Looks good. I have a minor change, because the string converter wraps the byte array instead of using the better fitting method signature taking byte array directly.

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java line 293:

> 291:         int len = strlen(segment, start);
> 292:         byte[] bytes = new byte[len];
> 293:         MemoryCopy.copy(segment, start, MemorySegment.ofArray(bytes), 0, len);

No need to wrap the byte array. Just use the other copy method taking byte[]. This spares another object allocation.

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

Changes requested by uschindler (no project role).

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


More information about the panama-dev mailing list