145. Introducing sequence layout
In Problem X, we already covered the ValueLayout for basic data types. Next, let’s talk about the sequence layout (java.lang.foreign.SequenceLayout).But, before introducing the sequence layout, let’s take a moment and analyze the following snippet of code:
try (Arena arena = Arena.openConfined()) {
MemorySegment segment = MemorySegment.allocateNative(
ValueLayout.JAVA_DOUBLE.byteSize() * 10,
ValueLayout.JAVA_DOUBLE.byteAlignment(), arena.scope());
for (int i = 0; i < 10; i++) {
segment.setAtIndex(ValueLayout.JAVA_DOUBLE,
i, Math.random());
}
for (int i = 0; i < 10; i++) {
System.out.printf(“\nx = %.2f”,
segment.getAtIndex(ValueLayout.JAVA_DOUBLE, i));
}
}
We start by creating a native memory segment for storing 10 double values. Next, we rely on setAtIndex() to set these double values. Finally, we print them.So, basically, we repeat the ValueLayout.JAVA_DOUBLE 10 times. When an element layout is repeated n times (a finite number of times), we can express the code via a sequence layout (java.lang.foreign.SequenceLayout). In other words, a sequence layout represents a repetition/sequence of a given element layout for a finite number of times.The following code uses SequenceLayout to shape the previous snippet:
SequenceLayout seq = MemoryLayout.sequenceLayout(
10, ValueLayout.JAVA_DOUBLE);
The number of repetitions (element count) is 10, and the repeated element layout is ValueLayout.JAVA_DOUBLE.But, how do we set the values of a sequence layout? There are at least two approaches, and one of them relies on a combination of the java.lang.invoke.VarHandle API and the java.lang.foreign.MemoryLayout.PathElement API.
Introducing PathElement
In a nutshell, the PathElement API exposes a friendly approach for navigating a hierarchal memory layout via the so-called layout path. By chaining path elements in a layout path, we can locate an element layout which can be a sequence layout (located via sequence path elements) or, as you’ll see in other problems, a group layout (which can be a struct layout or a union layout located via group path elements). Sequence layouts are traversed via PathElement.sequenceElement(), while group layouts via PathElement.groupElement(). Each element layout has a number of elements referred to as the element count (obtained via a method named elementCount()).
Introducing VarHandle
VarHandle is not new in town. It was introduced in JDK 9. A VarHandle is a dynamically, immutable, non-visible-state, strongly typed reference to a variable that cannot be subclassed. Its goal is to provide read/write access to the handled variables under certain circumstances.A VarHandle is characterized by two aspects:
the type of variables represented by this VarHandle as a generic type (T)
a list of coordinate types (denoted, CT) used to locate variables referenced by this VarHandle
The CT list may be empty.Typically, a VarHandle method gets a variable number of Object arguments. Argument(s) checking is accomplished at runtime (static argument(s) checking is disabled). Different methods of VarHandle expect to have a variable number of arguments of different types.