< prev index next >

test/jdk/java/foreign/TestUpcallStructScope.java

Print this page
*** 35,12 ***
--- 35,14 ---
   *   --enable-native-access=ALL-UNNAMED
   *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
   *   TestUpcallStructScope
   */
  
+ import jdk.incubator.foreign.Addressable;
  import jdk.incubator.foreign.CLinker;
  import jdk.incubator.foreign.FunctionDescriptor;
+ import jdk.incubator.foreign.NativeSymbol;
  import jdk.incubator.foreign.SymbolLookup;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;

*** 50,18 ***
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.function.Consumer;
  
- import static jdk.incubator.foreign.CLinker.C_DOUBLE;
- import static jdk.incubator.foreign.CLinker.C_INT;
- import static jdk.incubator.foreign.CLinker.C_POINTER;
  import static org.testng.Assert.assertFalse;
  
! public class TestUpcallStructScope {
      static final MethodHandle MH_do_upcall;
!     static final CLinker LINKER = CLinker.getInstance();
      static final MethodHandle MH_Consumer_accept;
  
      // struct S_PDI { void* p0; double p1; int p2; };
      static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
          C_POINTER.withName("p0"),
--- 52,15 ---
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.function.Consumer;
  
  import static org.testng.Assert.assertFalse;
  
! public class TestUpcallStructScope extends NativeTestHelper {
      static final MethodHandle MH_do_upcall;
!     static final CLinker LINKER = CLinker.systemCLinker();
      static final MethodHandle MH_Consumer_accept;
  
      // struct S_PDI { void* p0; double p1; int p2; };
      static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
          C_POINTER.withName("p0"),

*** 72,12 ***
      static {
          System.loadLibrary("TestUpcallStructScope");
          SymbolLookup lookup = SymbolLookup.loaderLookup();
          MH_do_upcall = LINKER.downcallHandle(
              lookup.lookup("do_upcall").get(),
!             MethodType.methodType(void.class, MemoryAddress.class, MemorySegment.class),
-             FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
          );
  
          try {
              MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
                      MethodType.methodType(void.class, Object.class));
--- 71,11 ---
      static {
          System.loadLibrary("TestUpcallStructScope");
          SymbolLookup lookup = SymbolLookup.loaderLookup();
          MH_do_upcall = LINKER.downcallHandle(
              lookup.lookup("do_upcall").get(),
!                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
          );
  
          try {
              MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
                      MethodType.methodType(void.class, Object.class));

*** 94,13 ***
      public void testUpcall() throws Throwable {
          AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
          MethodHandle target = methodHandle(capturedSegment::set);
          FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             MemoryAddress upcallStub = LINKER.upcallStub(target, upcallDesc, scope);
              MemorySegment argSegment = MemorySegment.allocateNative(S_PDI_LAYOUT, scope);
!             MH_do_upcall.invokeExact(upcallStub.address(), argSegment);
          }
  
          MemorySegment captured = capturedSegment.get();
          assertFalse(captured.scope().isAlive());
      }
--- 92,13 ---
      public void testUpcall() throws Throwable {
          AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
          MethodHandle target = methodHandle(capturedSegment::set);
          FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             NativeSymbol upcallStub = LINKER.upcallStub(target, upcallDesc, scope);
              MemorySegment argSegment = MemorySegment.allocateNative(S_PDI_LAYOUT, scope);
!             MH_do_upcall.invoke(upcallStub, argSegment);
          }
  
          MemorySegment captured = capturedSegment.get();
          assertFalse(captured.scope().isAlive());
      }
< prev index next >