BAEL-3852 - A Guide to Foreign Memory Access API in Java 14 (#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 <somnath.musib@voltbank.com.au>
This commit is contained in:
Somnath Musib 2020-05-11 00:14:53 +10:00 committed by GitHub
parent 8e5456b24b
commit 5c90dbc963
1 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,84 @@
package com.baeldung.java14.foreign.api;
import jdk.incubator.foreign.*;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
public class ForeignMemoryUnitTest {
@Test
public void whenAValueIsSet_thenAccessTheValue() {
long value = 10;
MemoryAddress memoryAddress =
MemorySegment.allocateNative(8).baseAddress();
VarHandle varHandle = MemoryHandles.varHandle(long.class,
ByteOrder.nativeOrder());
varHandle.set(memoryAddress, value);
assertThat(varHandle.get(memoryAddress), is(value));
}
@Test
public void whenMultipleValuesAreSet_thenAccessAll() {
VarHandle varHandle = MemoryHandles.varHandle(int.class,
ByteOrder.nativeOrder());
try(MemorySegment memorySegment =
MemorySegment.allocateNative(100)) {
MemoryAddress base = memorySegment.baseAddress();
for(int i=0; i<25; i++) {
varHandle.set(base.addOffset((i*4)), i);
}
for(int i=0; i<25; i++) {
assertThat(varHandle.get(base.addOffset((i*4))), is(i));
}
}
}
@Test
public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() {
SequenceLayout sequenceLayout =
MemoryLayout.ofSequence(25,
MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder()));
VarHandle varHandle =
sequenceLayout.varHandle(long.class,
MemoryLayout.PathElement.sequenceElement());
try(MemorySegment memorySegment =
MemorySegment.allocateNative(sequenceLayout)) {
MemoryAddress base = memorySegment.baseAddress();
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
varHandle.set(base, i, i);
}
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
assertThat(varHandle.get(base, i), is(i));
}
}
}
@Test
public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() {
MemoryAddress memoryAddress =
MemorySegment.allocateNative(12).baseAddress();
MemoryAddress memoryAddress1 =
memoryAddress.segment().asSlice(0,4).baseAddress();
MemoryAddress memoryAddress2 =
memoryAddress.segment().asSlice(4,4).baseAddress();
MemoryAddress memoryAddress3 =
memoryAddress.segment().asSlice(8,4).baseAddress();
VarHandle intHandle =
MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
intHandle.set(memoryAddress1, Integer.MIN_VALUE);
intHandle.set(memoryAddress2, 0);
intHandle.set(memoryAddress3, Integer.MAX_VALUE);
assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE));
assertThat(intHandle.get(memoryAddress2), is(0));
assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE));
}
}