Sub topic 5
Describing structs and arrays with memory layouts
This chapter assumes you know MemorySegment, Arena, and ValueLayout from the earlier chapters, plus how C structs sit in memory. A ValueLayout describes one primitive or pointer. A memory layout combines value layouts into a larger shape, such as a C struct or array.
Why use layouts instead of raw offsets
You can read a struct with hand-written offsets such as 4 and 8. That breaks when a field is added or reordered, and nothing checks that the offsets match the C header. A layout states the shape once. The API then computes sizes and offsets from it.
Struct layouts
MemoryLayout.structLayout describes a C struct. Its members are laid out one after another. Give each field a name with withName so you can find it later.
import java.lang.foreign.Arena;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemoryLayout.PathElement;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.StructLayout;
import java.lang.foreign.ValueLayout;
public class PointDemo {
static final StructLayout POINT = MemoryLayout.structLayout(
ValueLayout.JAVA_INT.withName("x"),
ValueLayout.JAVA_INT.withName("y"));
static final long X = POINT.byteOffset(PathElement.groupElement("x"));
static final long Y = POINT.byteOffset(PathElement.groupElement("y"));
public static void main(String[] args) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment p = arena.allocate(POINT);
p.set(ValueLayout.JAVA_INT, X, 10);
p.set(ValueLayout.JAVA_INT, Y, 20);
System.out.println(p.get(ValueLayout.JAVA_INT, X) + "," + p.get(ValueLayout.JAVA_INT, Y));
System.out.println(POINT.byteSize()); // 8
}
}
}byteOffset takes a path through the layout. PathElement.groupElement("x") means "the member named x". Calling arena.allocate(POINT) reserves POINT.byteSize() bytes with the right alignment.
Padding
C compilers insert padding so each field is aligned. A layout does not add padding for you. If you leave a gap out, structLayout throws an IllegalArgumentException because a field would be misaligned. Add the gap with MemoryLayout.paddingLayout.
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemoryLayout.PathElement;
import java.lang.foreign.StructLayout;
import java.lang.foreign.ValueLayout;
public class PaddedDemo {
public static void main(String[] args) {
// struct { int id; double value; } on a typical 64-bit platform
StructLayout record = MemoryLayout.structLayout(
ValueLayout.JAVA_INT.withName("id"),
MemoryLayout.paddingLayout(4),
ValueLayout.JAVA_DOUBLE.withName("value"));
System.out.println(record.byteSize()); // 16
System.out.println(record.byteOffset(PathElement.groupElement("value"))); // 8
}
}Always check the layout against what your C compiler produces for the target platform.
Arrays with sequence layouts
MemoryLayout.sequenceLayout(count, element) describes count repeated elements. Use PathElement.sequenceElement(i) in a path to select element i.
import java.lang.foreign.Arena;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemoryLayout.PathElement;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.ValueLayout;
public class PointArrayDemo {
static final StructLayout POINT = MemoryLayout.structLayout(
ValueLayout.JAVA_INT.withName("x"),
ValueLayout.JAVA_INT.withName("y"));
static final SequenceLayout POINTS = MemoryLayout.sequenceLayout(3, POINT);
public static void main(String[] args) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment points = arena.allocate(POINTS);
for (int i = 0; i < 3; i++) {
long x = POINTS.byteOffset(PathElement.sequenceElement(i), PathElement.groupElement("x"));
long y = POINTS.byteOffset(PathElement.sequenceElement(i), PathElement.groupElement("y"));
points.set(ValueLayout.JAVA_INT, x, i);
points.set(ValueLayout.JAVA_INT, y, i * 10);
}
System.out.println(POINTS.byteSize()); // 24
long lastY = POINTS.byteOffset(PathElement.sequenceElement(2), PathElement.groupElement("y"));
System.out.println(points.get(ValueLayout.JAVA_INT, lastY)); // 20
}
}
}The path sequenceElement(2) then groupElement("y") gives offset 2 * 8 + 4 = 20.
Layouts and function descriptors
Layouts also feed into native calls. A struct passed by value in a FunctionDescriptor is described by its StructLayout. A pointer to a struct can carry one as a target layout.
Exercise
A C struct is struct { char tag; long value; } on a 64-bit Linux platform where long is 8 bytes and 8-byte aligned. Which layout describes it, and what are its size and the offset of value?
Show answer
Use structLayout(JAVA_BYTE.withName("tag"), paddingLayout(7), JAVA_LONG.withName("value")). The 7 padding bytes keep value 8-byte aligned. value is at offset 8, and the total size is 16 bytes.