On Wed, 18 Oct 2023 23:56:17 GMT, Maurizio Cimadamore <mcimadam...@openjdk.org> wrote:
>> 8318364: Add an FFM-based implementation of harfbuzz OpenType layout > > src/java.desktop/share/classes/sun/font/HBShaper.java line 110: > >> 108: >> 109: private static VarHandle getVarHandle(StructLayout struct, String >> name) { >> 110: VarHandle h = struct.varHandle(PathElement.groupElement(name)); > > Note that, strictly speaking, these combiners are _not_ required. If you just > call `MemoryLayout::varHandle`, you get back a var handle that takes a > `MemorySegment` and a `long` offset. So, you can, if you want, adapt the var > handle so that the offset parameter becomes something else. But you could > also just leave the var handle as is. Then, in the loop that is doing the > access, you can do this: > > > for (int i = 0 ; i < limit ; i++) { > x_offsetHandle.get(segment, PositionLayout.scale(0, i)); > y_offsetHandle.get(segment, PositionLayout.scale(0, i)); > ... > } > > That is, use the offset hole to your advantage, to pass the struct base > offset (obtained by scaling the enclosing struct layout by the value of the > loop induction variable). > > (That said, I understand that working with logical indices is a common > operation, and that this is made a bit harder by the recent API changes. We > should consider, as @JornVernee mentioned, adding back a more general > `MemoryLayout::arrayElementVarHandle` which will give you the var handle you > need - with coordinates `MemorySegment` and `long` - a logical index, not an > offset). A PR which adds `MemoryLayout::arrayElementVarHandle` can be found here: https://github.com/openjdk/jdk/pull/16272 With this, you can call the new method in order to create the var handle. The returned var handle will accept *two* long coordinate - the first is a base offset (as usual), the second in a logical index (what you need). The PR also adds plenty of narrative text describing how access to variable-length arrays should be performed using layouts (and also shows cases where the offset parameter is used in a non-trivial fashion). ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/15476#discussion_r1365898029