Skip to content

Commit b25095b

Browse files
committed
8338728: Misc issues in memory layout javadoc
Reviewed-by: pminborg, psandoz
1 parent 414d23c commit b25095b

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/java.base/share/classes/java/lang/foreign/MemoryLayout.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,15 @@ public sealed interface MemoryLayout
664664
* <p>
665665
* If the provided layout path has size {@code m} and contains a dereference path
666666
* element in position {@code k} (where {@code k <= m}) then two layout paths
667-
* {@code P} and {@code P'} are derived, where P contains all the path elements from
668-
* 0 to {@code k - 1} and {@code P'} contains all the path elements from {@code k + 1}
669-
* to {@code m} (if any). Then, the returned var handle is computed as follows:
667+
* {@code P} and {@code Q} are derived, where P contains all the path elements from
668+
* 0 to {@code k - 1} and {@code Q} contains all the path elements from {@code k + 1}
669+
* to {@code m} ({@code Q} could be an empty layout path if {@code k == m}).
670+
* Then, the returned var handle is computed as follows:
670671
*
671672
* {@snippet lang = "java":
672673
* VarHandle baseHandle = this.varHandle(P);
673674
* MemoryLayout target = ((AddressLayout)this.select(P)).targetLayout().get();
674-
* VarHandle targetHandle = target.varHandle(P);
675+
* VarHandle targetHandle = target.varHandle(Q);
675676
* targetHandle = MethodHandles.insertCoordinates(targetHandle, 1, 0L); // always access nested targets at offset 0
676677
* targetHandle = MethodHandles.collectCoordinates(targetHandle, 0,
677678
* baseHandle.toMethodHandle(VarHandle.AccessMode.GET));
@@ -944,7 +945,7 @@ static PathElement sequenceElement(long index) {
944945
* is computed as follows:
945946
* <ul>
946947
* <li>if {@code F > 0}, then {@code B = ceilDiv(C - S, F)}</li>
947-
* <li>if {@code F < 0}, then {@code B = ceilDiv(-(S + 1), -F)}</li>
948+
* <li>if {@code F < 0}, then {@code B = ceilDiv(S + 1, -F)}</li>
948949
* </ul>
949950
* That is, the size of the returned open path element is {@code B}.
950951
*
@@ -972,8 +973,8 @@ static PathElement sequenceElement() {
972973
}
973974

974975
/**
975-
* {@return a path element that dereferences an address layout as its
976-
* {@linkplain AddressLayout#targetLayout() target layout} (where set)}
976+
* {@return a path element that selects the {@linkplain AddressLayout#targetLayout() target layout} of
977+
* an address layout (where set)}
977978
*/
978979
static PathElement dereferenceElement() {
979980
return LayoutPath.DereferenceElement.instance();

test/jdk/java/foreign/TestDereferencePath.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ public void testMulti() {
119119
}
120120
}
121121

122+
static final MemoryLayout A_VALUE = MemoryLayout.structLayout(
123+
ValueLayout.ADDRESS.withName("b")
124+
.withTargetLayout(ValueLayout.JAVA_INT)
125+
);
126+
127+
static final VarHandle a_value = A_VALUE.varHandle(
128+
PathElement.groupElement("b"), PathElement.dereferenceElement());
129+
130+
@Test
131+
public void testDerefValue() {
132+
try (Arena arena = Arena.ofConfined()) {
133+
// init structs
134+
MemorySegment a = arena.allocate(A);
135+
MemorySegment b = arena.allocate(ValueLayout.JAVA_INT);
136+
// init struct fields
137+
a.set(ValueLayout.ADDRESS, 0, b);
138+
b.set(ValueLayout.JAVA_INT, 0, 42);
139+
// dereference
140+
int val = (int) a_value.get(a, 0L);
141+
assertEquals(val, 42);
142+
}
143+
}
144+
145+
122146
@Test(expectedExceptions = IllegalArgumentException.class)
123147
void testBadDerefInSelect() {
124148
A.select(PathElement.groupElement("b"), PathElement.dereferenceElement());

0 commit comments

Comments
 (0)