Skip to content

Commit e049907

Browse files
musibsSomnath Musib
andauthored
BAEL-3852 - A Guide to Foreign Memory Access API in Java 14 (eugenp#9040)
* Code sample for Java Hexagonal architecture * BAEL-3838 Capturing a Java Thread Dump * BAEL-3852 Foreign memory api in Java * BAEL-3852 - Review changes of A Guide to Foreign Memory Access API in Java 14 * BAEL-3852 - Additional review changes for A Guide to Foreign Memory Access API in Java 14 * Review changes for alignment and class removal * Removed incorrectly added old files from the PR * Indentation changes Co-authored-by: Somnath Musib <[email protected]>
1 parent d5f975d commit e049907

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.java14.foreign.api;
2+
3+
import jdk.incubator.foreign.*;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
import java.lang.invoke.VarHandle;
10+
import java.nio.ByteOrder;
11+
12+
public class ForeignMemoryUnitTest {
13+
14+
@Test
15+
public void whenAValueIsSet_thenAccessTheValue() {
16+
long value = 10;
17+
MemoryAddress memoryAddress =
18+
MemorySegment.allocateNative(8).baseAddress();
19+
VarHandle varHandle = MemoryHandles.varHandle(long.class,
20+
ByteOrder.nativeOrder());
21+
varHandle.set(memoryAddress, value);
22+
assertThat(varHandle.get(memoryAddress), is(value));
23+
}
24+
25+
@Test
26+
public void whenMultipleValuesAreSet_thenAccessAll() {
27+
VarHandle varHandle = MemoryHandles.varHandle(int.class,
28+
ByteOrder.nativeOrder());
29+
30+
try(MemorySegment memorySegment =
31+
MemorySegment.allocateNative(100)) {
32+
MemoryAddress base = memorySegment.baseAddress();
33+
for(int i=0; i<25; i++) {
34+
varHandle.set(base.addOffset((i*4)), i);
35+
}
36+
for(int i=0; i<25; i++) {
37+
assertThat(varHandle.get(base.addOffset((i*4))), is(i));
38+
}
39+
}
40+
}
41+
42+
@Test
43+
public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() {
44+
SequenceLayout sequenceLayout =
45+
MemoryLayout.ofSequence(25,
46+
MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder()));
47+
VarHandle varHandle =
48+
sequenceLayout.varHandle(long.class,
49+
MemoryLayout.PathElement.sequenceElement());
50+
51+
try(MemorySegment memorySegment =
52+
MemorySegment.allocateNative(sequenceLayout)) {
53+
MemoryAddress base = memorySegment.baseAddress();
54+
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
55+
varHandle.set(base, i, i);
56+
}
57+
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
58+
assertThat(varHandle.get(base, i), is(i));
59+
}
60+
}
61+
}
62+
63+
@Test
64+
public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() {
65+
MemoryAddress memoryAddress =
66+
MemorySegment.allocateNative(12).baseAddress();
67+
MemoryAddress memoryAddress1 =
68+
memoryAddress.segment().asSlice(0,4).baseAddress();
69+
MemoryAddress memoryAddress2 =
70+
memoryAddress.segment().asSlice(4,4).baseAddress();
71+
MemoryAddress memoryAddress3 =
72+
memoryAddress.segment().asSlice(8,4).baseAddress();
73+
74+
VarHandle intHandle =
75+
MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
76+
intHandle.set(memoryAddress1, Integer.MIN_VALUE);
77+
intHandle.set(memoryAddress2, 0);
78+
intHandle.set(memoryAddress3, Integer.MAX_VALUE);
79+
80+
assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE));
81+
assertThat(intHandle.get(memoryAddress2), is(0));
82+
assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE));
83+
}
84+
}

0 commit comments

Comments
 (0)