< prev index next >

test/jdk/java/foreign/TestNative.java

Print this page
*** 27,19 ***
   * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
   * @modules jdk.incubator.foreign/jdk.internal.foreign
   * @run testng/othervm --enable-native-access=ALL-UNNAMED TestNative
   */
  
- import jdk.incubator.foreign.CLinker;
- import jdk.incubator.foreign.MemoryAccess;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemoryLayout.PathElement;
- import jdk.incubator.foreign.MemoryLayouts;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.SequenceLayout;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  
  import java.lang.invoke.VarHandle;
  import java.nio.Buffer;
--- 27,17 ---
   * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
   * @modules jdk.incubator.foreign/jdk.internal.foreign
   * @run testng/othervm --enable-native-access=ALL-UNNAMED TestNative
   */
  
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemoryLayout.PathElement;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.SequenceLayout;
+ import jdk.incubator.foreign.ValueLayout;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  
  import java.lang.invoke.VarHandle;
  import java.nio.Buffer;

*** 54,49 ***
  import java.util.function.BiConsumer;
  import java.util.function.BiFunction;
  import java.util.function.Consumer;
  import java.util.function.Function;
  
  import static org.testng.Assert.*;
  
! public class TestNative {
  
      static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_INT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_FLOAT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_LONG.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
!             MemoryLayouts.JAVA_DOUBLE.withOrder(ByteOrder.nativeOrder())
      );
  
!     static VarHandle byteHandle = bytes.varHandle(byte.class, PathElement.sequenceElement());
!     static VarHandle charHandle = chars.varHandle(char.class, PathElement.sequenceElement());
!     static VarHandle shortHandle = shorts.varHandle(short.class, PathElement.sequenceElement());
!     static VarHandle intHandle = ints.varHandle(int.class, PathElement.sequenceElement());
!     static VarHandle floatHandle = floats.varHandle(float.class, PathElement.sequenceElement());
!     static VarHandle longHandle = doubles.varHandle(long.class, PathElement.sequenceElement());
!     static VarHandle doubleHandle = longs.varHandle(double.class, PathElement.sequenceElement());
  
      static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
          for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
              handleSetter.accept(base, i);
          }
--- 52,50 ---
  import java.util.function.BiConsumer;
  import java.util.function.BiFunction;
  import java.util.function.Consumer;
  import java.util.function.Function;
  
+ import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
  import static org.testng.Assert.*;
  
! public class TestNative extends NativeTestHelper {
  
      static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
!             JAVA_INT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_LONG.withOrder(ByteOrder.nativeOrder())
      );
  
      static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
!             ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.nativeOrder())
      );
  
!     static VarHandle byteHandle = bytes.varHandle(PathElement.sequenceElement());
!     static VarHandle charHandle = chars.varHandle(PathElement.sequenceElement());
!     static VarHandle shortHandle = shorts.varHandle(PathElement.sequenceElement());
!     static VarHandle intHandle = ints.varHandle(PathElement.sequenceElement());
!     static VarHandle floatHandle = floats.varHandle(PathElement.sequenceElement());
!     static VarHandle longHandle = longs.varHandle(PathElement.sequenceElement());
!     static VarHandle doubleHandle = doubles.varHandle(PathElement.sequenceElement());
  
      static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
          for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
              handleSetter.accept(base, i);
          }

*** 142,18 ***
      public static native long getLongRaw(long addr, int index);
      public static native double getDoubleRaw(long addr, int index);
  
      public static native long getCapacity(Buffer buffer);
  
-     public static MemoryAddress allocate(int size) {
-         return CLinker.allocateMemory(size);
-     }
- 
-     public static void free(MemoryAddress addr) {
-         CLinker.freeMemory(addr);
-     }
- 
      @Test(dataProvider="nativeAccessOps")
      public void testNativeAccess(Consumer<MemorySegment> checker, Consumer<MemorySegment> initializer, SequenceLayout seq) {
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(seq, scope);
              initializer.accept(segment);
--- 141,10 ---

*** 174,49 ***
          }
      }
  
      @Test
      public void testDefaultAccessModes() {
!         MemoryAddress addr = allocate(12);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             MemorySegment mallocSegment = addr.asSegment(12, () -> free(addr), scope);
              assertFalse(mallocSegment.isReadOnly());
          }
      }
  
-     @Test
-     public void testDefaultAccessModesEverthing() {
-         MemorySegment everything = MemorySegment.globalNativeSegment();
-         assertFalse(everything.isReadOnly());
-     }
- 
      @Test
      public void testMallocSegment() {
!         MemoryAddress addr = allocate(12);
          MemorySegment mallocSegment = null;
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             mallocSegment = addr.asSegment(12, () -> free(addr), scope);
              assertEquals(mallocSegment.byteSize(), 12);
              //free here
          }
          assertTrue(!mallocSegment.scope().isAlive());
      }
  
      @Test
!     public void testEverythingSegment() {
!         MemoryAddress addr = allocate(4);
!         MemorySegment everything = MemorySegment.globalNativeSegment();
!         MemoryAccess.setIntAtOffset(everything, addr.toRawLongValue(), 42);
!         assertEquals(MemoryAccess.getIntAtOffset(everything, addr.toRawLongValue()), 42);
-         free(addr);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadResize() {
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(4, 1, scope);
!             segment.address().asSegment(0, ResourceScope.globalScope());
          }
      }
  
      static {
          System.loadLibrary("NativeAccess");
--- 165,44 ---
          }
      }
  
      @Test
      public void testDefaultAccessModes() {
!         MemoryAddress addr = allocateMemory(12);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             scope.addCloseAction(() -> freeMemory(addr));
+             MemorySegment mallocSegment = MemorySegment.ofAddressNative(addr, 12, scope);
              assertFalse(mallocSegment.isReadOnly());
          }
      }
  
      @Test
      public void testMallocSegment() {
!         MemoryAddress addr = allocateMemory(12);
          MemorySegment mallocSegment = null;
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             scope.addCloseAction(() -> freeMemory(addr));
+             mallocSegment = MemorySegment.ofAddressNative(addr, 12, scope);
              assertEquals(mallocSegment.byteSize(), 12);
              //free here
          }
          assertTrue(!mallocSegment.scope().isAlive());
      }
  
      @Test
!     public void testAddressAccess() {
!         MemoryAddress addr = allocateMemory(4);
!         addr.set(JAVA_INT, 0, 42);
!         assertEquals(addr.get(JAVA_INT, 0), 42);
!         freeMemory(addr);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadResize() {
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(4, 1, scope);
!             MemorySegment.ofAddressNative(segment.address(), 0, ResourceScope.globalScope());
          }
      }
  
      static {
          System.loadLibrary("NativeAccess");
< prev index next >