161. Introducing the layout spreader
Let’s suppose that we have the following nested model (the exact same model as in Problem 158):
SequenceLayout innerSeq
= MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_DOUBLE);
SequenceLayout outerSeq
= MemoryLayout.sequenceLayout(10, innerSeq);
Next, we define a VarHandle
via PathElement
and we populate this model accordingly with some random data in a memory segment named segment
(you can see the code listed in Problem 158).
Next, let’s assume that we want to extract the third double
value from the seventh sequence (count starts from 0). Among the approaches, we can rely on sliceHandle()
introduced in Problem 158, as follows:
MethodHandle mHandle = outerSeq.sliceHandle(
PathElement.sequenceElement(),
PathElement.sequenceElement());
MemorySegment ms = (MemorySegment)
mHandle.invokeExact(segment, 7L, 3L);
System.out.println(ms.get(ValueLayout.JAVA_DOUBLE, 0));
Another approach consists of using...