Sub topic 4
Value layouts: describing ints, longs and pointers
This chapter assumes you know what a MemorySegment is and how an Arena allocates one. It also assumes you know C's basic types. A segment is only bytes. A value layout tells Java how to read a group of those bytes as a number or a pointer.
What a value layout is
A ValueLayout describes one primitive value in memory. It records three things:
- The Java carrier type, such as
intorlong. - The size in bytes.
- The alignment and byte order.
The ValueLayout interface has constants for the common cases. You pass one to get and set to choose how many bytes to read and how to interpret them.
| Layout | Java carrier | Size | Typical C type |
|---|---|---|---|
| `JAVA_BYTE` | `byte` | 1 | `char`, `int8_t` |
| `JAVA_SHORT` | `short` | 2 | `short` |
| `JAVA_INT` | `int` | 4 | `int` |
| `JAVA_LONG` | `long` | 8 | `long long`, `int64_t`, `size_t` (64-bit) |
| `JAVA_FLOAT` | `float` | 4 | `float` |
| `JAVA_DOUBLE` | `double` | 8 | `double` |
| `ADDRESS` | `MemorySegment` | 8 on 64-bit | any pointer |
Reading and writing values
The offset you give to get and set is in bytes, not elements. The layout decides the width of each access.
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
public class ValueDemo {
public static void main(String[] args) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment seg = arena.allocate(16, 8);
seg.set(ValueLayout.JAVA_LONG, 0, 1234567890123L);
seg.set(ValueLayout.JAVA_INT, 8, 7);
seg.set(ValueLayout.JAVA_FLOAT, 12, 1.5f);
System.out.println(seg.get(ValueLayout.JAVA_LONG, 0));
System.out.println(seg.get(ValueLayout.JAVA_INT, 8));
System.out.println(seg.get(ValueLayout.JAVA_FLOAT, 12));
}
}
}The second argument to allocate asks for 8-byte alignment. That matters because the constants such as JAVA_LONG require natural alignment. An 8-byte value must sit at an address divisible by 8, or the access throws IllegalArgumentException. For data that may be unaligned, use the _UNALIGNED variants, for example JAVA_INT_UNALIGNED.
Choosing sizes for C types
C does not fix the size of every type. int is 4 bytes on all mainstream platforms, so JAVA_INT is safe. long is 8 bytes on 64-bit Linux and macOS but 4 bytes on 64-bit Windows. Use the layout that matches the platform you target. size_t is pointer-sized, so JAVA_LONG is correct only on 64-bit systems.
Pointers with ADDRESS
ValueLayout.ADDRESS describes a pointer. Reading one gives you a MemorySegment. By default that segment has size 0, because Java cannot know how much memory the pointer refers to. Any access through it fails.
To get a usable segment, attach a target layout with withTargetLayout. The result is an AddressLayout.
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
public class PointerDemo {
public static void main(String[] args) {
AddressLayout intPtr = ValueLayout.ADDRESS.withTargetLayout(ValueLayout.JAVA_INT);
try (Arena arena = Arena.ofConfined()) {
MemorySegment value = arena.allocate(ValueLayout.JAVA_INT);
value.set(ValueLayout.JAVA_INT, 0, 99);
MemorySegment holder = arena.allocate(ValueLayout.ADDRESS);
holder.set(ValueLayout.ADDRESS, 0, value);
MemorySegment plain = holder.get(ValueLayout.ADDRESS, 0);
MemorySegment typed = holder.get(intPtr, 0);
System.out.println(plain.byteSize()); // 0
System.out.println(typed.byteSize()); // 4
System.out.println(typed.get(ValueLayout.JAVA_INT, 0)); // 99
}
}
}The target layout is a promise from you, and Java does not check it. If the pointer really refers to less memory than you declared, you get undefined behavior.
Naming layouts
Call withName to label a layout, as in ValueLayout.JAVA_INT.withName("x"). Names do nothing by themselves. The next chapter uses them to find struct fields by name.
Exercise
On 64-bit Windows, which layout should you use for a C long parameter, and why is JAVA_LONG wrong there?
Show answer
Use JAVA_INT. A C long is 4 bytes on 64-bit Windows, while JAVA_LONG is 8 bytes. Using JAVA_LONG would read or write four extra bytes and corrupt neighboring data.
Exercise
A pointer read with plain ValueLayout.ADDRESS has a segment of size 0. How do you get a segment that lets you read a 4-byte int through it?
Show answer
Read it with an address layout that has a target layout, such as ValueLayout.ADDRESS.withTargetLayout(ValueLayout.JAVA_INT). The returned segment then has size 4.