[foreign-memaccess] RFR: 8252504: Add a method to MemoryLayout which returns a offset-computing method handle

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Nov 19 17:30:12 UTC 2020


On Thu, 19 Nov 2020 15:53:44 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> This PR adds two new methods: `bitOffsetHandle` and `byteOffsetHandle` to `MemoryLayout`, that can be used to create method handles that can compute the effective offset of a layout element given a set of additional `long` indexes.
> 
> I also cleaned up a leftover in the javadoc of bitOffset and byteOffset where an API note was stating that `sequenceElement()` layout path elements would be interpreted as `sequenceElement(0)`, but they were actually being rejected outright (as they should).
> 
> I also added some tests to cover the corner case where an UnsupportedOperationException was being thrown. (some of the churn in the tests is from introducing static imports. I hope it's not too noisy).

Looks good - added some javadoc/test suggestions

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemoryLayout.java line 359:

> 357:      * by a given layout path, where the path is considered rooted in this layout.
> 358:      *
> 359:      * <p>This is like {@link #bitOffset(PathElement...)}, except that free dimensions

I'd replace this text, or maybe move it after we explain what the signature of the returned method handle is. E.g.

The returned method handle has a return type of `long`, and features as many `long` parameter types as there are free dimensions in the provided layout path. In other words, this method can be used as a replacements in cases where {@link #bitOffset(PathElement...)} does not work.
Or something similar.

test/jdk/java/foreign/TestLayoutPaths.java line 436:

> 434:         return new Object[][]{
> 435:             {
> 436:                 MemoryLayout.ofSequence(10, JAVA_INT),

The test is good, but there's no case which features more than one long index. I think it is worth adding at least a few, since that's where all the combination logic (and ordering) can go wrong?

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

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


More information about the panama-dev mailing list