diff --git a/test/functional/Java16andUp/build.xml b/test/functional/Java16andUp/build.xml index 8fbe448cab9..d69a7a5b612 100644 --- a/test/functional/Java16andUp/build.xml +++ b/test/functional/Java16andUp/build.xml @@ -39,17 +39,6 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex - - - - - - - - - - - @@ -99,46 +88,21 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex ===debug: on ===destdir: ${DEST} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/test/functional/Java16andUp/playlist.xml b/test/functional/Java16andUp/playlist.xml index ad8eabd13f2..7076ae4abd7 100644 --- a/test/functional/Java16andUp/playlist.xml +++ b/test/functional/Java16andUp/playlist.xml @@ -138,99 +138,4 @@ 16+ - - - CloseScope0Tests - - - https://github.com/eclipse-openj9/openj9/issues/14028#issuecomment-988225623 - hotspot - - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-opens jdk.incubator.foreign/jdk.internal.foreign=ALL-UNNAMED \ - --add-opens java.base/jdk.internal.misc=ALL-UNNAMED \ - --add-opens java.base/java.lang=ALL-UNNAMED \ - --add-modules jdk.incubator.foreign \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_160.xml$(Q) \ - -testnames CloseScope0Tests \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - - sanity - - - functional - - - 16 - - - - - Jep389Tests_testClinkerFfi_DownCall - - - https://github.com/eclipse-openj9/openj9/issues/14028#issuecomment-988225623 - hotspot - - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-modules jdk.incubator.foreign \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_160.xml$(Q) -testnames Jep389Tests_testClinkerFfi_DownCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.riscv,^os.zos,^os.sunos,^arch.arm,^os.osx - - sanity - - - functional - - - openj9 - - - 16 - - - - - Jep389Tests_testClinkerFfi_VaList - - - https://github.com/eclipse-openj9/openj9/issues/14028#issuecomment-988225623 - hotspot - - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-modules jdk.incubator.foreign \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_160.xml$(Q) -testnames Jep389Tests_testClinkerFfi_VaList \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.ppc,^arch.390,^arch.riscv,^os.zos,^os.sunos,^arch.arm,^os.osx - - sanity - - - functional - - - openj9 - - - 16 - - diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java b/test/functional/Java16andUp/src_160/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java deleted file mode 100644 index f4ca0f2a940..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java +++ /dev/null @@ -1,187 +0,0 @@ -package org.openj9.test.foreignMemoryAccess; - -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ - -import org.testng.Assert; -import static org.testng.Assert.*; -import org.testng.annotations.Test; -import org.openj9.test.util.VersionCheck; - -import java.lang.reflect.*; -import java.lang.ref.Cleaner; -import jdk.internal.misc.ScopedMemoryAccess.*; - -import org.objectweb.asm.*; -import static org.objectweb.asm.Opcodes.*; - -@Test(groups = { "level.sanity" }) -public class TestCloseScope0 { - public static byte[] dump() throws Exception { - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); - MethodVisitor mv; - AnnotationVisitor av0; - - cw.visit(V16, ACC_PUBLIC + ACC_SUPER, "jdk/internal/misc/RunInScoped", null, "java/lang/Object", null); - { - mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); - mv.visitCode(); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); - mv.visitInsn(RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - { - /** - * @Scoped // <-- package private annotation that isn't visible here. This is why we use ASM to generate the class - * public static void runInScoped(Runnable r, Scope scope) { - * r.run(); - * Reference.reachabilityFence(scope); - * } - */ - mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "runInScoped", "(Ljava/lang/Runnable;Ljdk/internal/misc/ScopedMemoryAccess$Scope;)V", null, null); - { - av0 = mv.visitAnnotation("Ljdk/internal/misc/ScopedMemoryAccess$Scoped;", true); - av0.visitEnd(); - } - mv.visitCode(); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Runnable", "run", "()V", true); - mv.visitVarInsn(ALOAD, 1); - mv.visitMethodInsn(INVOKESTATIC, "java/lang/ref/Reference", "reachabilityFence", "(Ljava/lang/Object;)V", false); - mv.visitInsn(RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - cw.visitEnd(); - - return cw.toByteArray(); - } - - private static Throwable expected = null; - private static volatile boolean t1Started = false; - private static volatile boolean t2Waiting = false; - - @Test(expectedExceptions=java.lang.IllegalStateException.class) - public static void closeScopeDuringAccess() throws Throwable { - /* Reflect setup */ - Class memoryOrResourceScope; - Method createShared; - Method close; - Object scope; - - int version = VersionCheck.major(); - if (version == 16) { - memoryOrResourceScope = Class.forName("jdk.internal.foreign.MemoryScope"); - createShared = memoryOrResourceScope.getDeclaredMethod("createShared", new Class[] {Object.class, Runnable.class, Cleaner.class}); - createShared.setAccessible(true); - scope = createShared.invoke(null, null, new Thread(), null); - } else { - memoryOrResourceScope = Class.forName("jdk.internal.foreign.ResourceScopeImpl"); - createShared = memoryOrResourceScope.getDeclaredMethod("createShared", new Class[] {Cleaner.class}); - createShared.setAccessible(true); - scope = createShared.invoke(null, Cleaner.create()); - } - close = memoryOrResourceScope.getDeclaredMethod("close"); - close.setAccessible(true); - /* End Reflect setup */ - - /* ASM setup */ - final byte[] classBytes = dump(); - - ClassLoader loader = ClassLoader.getSystemClassLoader(); - Class cls = Class.forName("java.lang.ClassLoader"); - Method defineClass = cls.getDeclaredMethod( - "defineClass", - new Class[] { String.class, byte[].class, int.class, int.class }); - defineClass.setAccessible(true); - - Object[] dcArgs = new Object[] {"jdk.internal.misc.RunInScoped", classBytes, 0, classBytes.length}; - Class RunInScoped = (Class)defineClass.invoke(loader, dcArgs); - Method runInScoped = RunInScoped.getDeclaredMethod("runInScoped", new Class[] {Runnable.class, Scope.class}); - /* End ASM setup */ - - Synch synch1 = new Synch(); - Synch synch2 = new Synch(); - - Thread t1 = new Thread(()->{ - try { - synchronized (synch1) { - t1Started = true; - synch1.wait(); - } - close.invoke(scope); - } catch (InvocationTargetException e) { - // This is the expected behaviour (throws IllegalStateException) - expected = e.getCause(); - } catch (InterruptedException | IllegalAccessException e) { - e.printStackTrace(); - } finally { - while (!t2Waiting) { - Thread.yield(); - } - synchronized (synch2) { - synch2.notify(); - } - } - }, "ScopeCloserThread"); - - class MyRunnable implements Runnable { - public void run() { - try { - while (!t1Started) { - Thread.yield(); - } - synchronized (synch1) { - synch1.notify(); - } - synchronized (synch2) { - t2Waiting = true; - synch2.wait(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - MyRunnable r = new MyRunnable(); - Thread t2 = new Thread(()->{ - try { - runInScoped.invoke(null, r, scope); - } catch (Exception e) { - e.printStackTrace(); - } - }, "RunInScopeThread"); - - t1.start(); - t2.start(); - - t1.join(); - t2.join(); - - if (expected != null) throw expected; - } -} - -class Synch {} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/InvalidDownCallTests.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/InvalidDownCallTests.java deleted file mode 100644 index 198a4bd48f0..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/InvalidDownCallTests.java +++ /dev/null @@ -1,397 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.ValueLayout; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemoryLayouts; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies the majority of illegal cases including the mismatch between the type - * and the corresponding layout, the inconsisitency of the arity, etc. - */ -@Test(groups = { "level.sanity" }) -public class InvalidDownCallTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinOS = osName.contains("win"); - private static ValueLayout longLayout = (isWinOS || isAixOS) ? C_LONG_LONG : C_LONG; - LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - LibraryLookup defaultLib = LibraryLookup.ofDefault(); - CLinker clinker = CLinker.getInstance(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidBooleanTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Boolean return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidBooleanTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, Boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Boolean argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidCharacterTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Character.class, char.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Character return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidCharacterTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(char.class, char.class, Character.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Character argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidByteTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Byte.class, byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Byte return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidByteTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, Byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Byte argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidShortTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Short.class, short.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Short return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidShortTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(short.class, short.class, Short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Short argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidIntegerTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Integer.class,int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Integer return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidIntegerTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(int.class,Integer.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Integer argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidLongTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Long.class, long.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Long return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidLongTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(long.class, long.class, Long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Long argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidFloatTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Float.class, float.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Float return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidFloatTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(float.class, Float.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Float argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*") - public void test_invalidDoubleTypeOnReturn() throws Throwable { - MethodType mt = MethodType.methodType(Double.class, double.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Double return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*") - public void test_invalidDoubleTypeArgument() throws Throwable { - MethodType mt = MethodType.methodType(double.class, double.class, Double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the Double argument"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedVoidReturnLayoutWithIntType() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the void return layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedVoidReturnTypeWithIntReturnLayout() throws Throwable { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the void return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedShortReturnLayoutWithIntType() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the non-void return layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedShortReturnTypeWithIntReturnLayout() throws Throwable { - MethodType mt = MethodType.methodType(short.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the non-void return type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* inconsistent with the arity .*") - public void test_mismatchedArity() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched arity"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedBoolArgWithShortLayout() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_SHORT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedCharArgWithCharLayout() throws Throwable { - MethodType mt = MethodType.methodType(char.class, char.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_CHAR, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedByteArgWithShortLayout() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedIntArgTypeWithShortLayout() throws Throwable { - MethodType mt = MethodType.methodType(short.class, int.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedIntArgLayoutWithShortType() throws Throwable { - MethodType mt = MethodType.methodType(short.class, short.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_INT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedIntArgTypeWithLongLayout() throws Throwable { - MethodType mt = MethodType.methodType(long.class, int.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedIntArgLayoutWithLongType() throws Throwable { - MethodType mt = MethodType.methodType(long.class, long.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedIntArgTypeWithFloatLayout() throws Throwable { - MethodType mt = MethodType.methodType(float.class, int.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedIntArgLayoutWithFloatType() throws Throwable { - MethodType mt = MethodType.methodType(float.class, float.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched int layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedLongArgTypeWithDoubleLayout() throws Throwable { - MethodType mt = MethodType.methodType(double.class, long.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched long type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatch between the layout .*") - public void test_mismatchedLongArgLayoutWithDoubleType() throws Throwable { - MethodType mt = MethodType.methodType(double.class, double.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched long layout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "ValueLayout is expected.*") - public void test_invalidMemoryLayoutForIntType() throws Throwable { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, MemoryLayout.ofPaddingBits(32)); - Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "ValueLayout is expected.*") - public void test_invalidMemoryLayoutForMemoryAddress() throws Throwable { - Symbol functionSymbol = defaultLib.lookup("strlen").get(); - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, MemoryLayout.ofPaddingBits(64)); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedLayoutSizeForIntType() throws Throwable { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, MemoryLayouts.BITS_64_LE); - Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout size"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*") - public void test_mismatchedLayoutSizeForMemoryAddress() throws Throwable { - Symbol functionSymbol = defaultLib.lookup("strlen").get(); - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, MemoryLayouts.BITS_16_LE); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout size"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* neither primitive nor .*") - public void test_unsupportedStringType() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, String.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, MemoryLayouts.BITS_64_LE); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the unsupported String type"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "GroupLayout is expected: .*") - public void test_unsupportedStructLayout() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, MemoryLayouts.BITS_64_LE); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - fail("Failed to throw out IllegalArgumentException in the case of the unsupported layout for struct"); - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiCallTests.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiCallTests.java deleted file mode 100644 index b74028dd984..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiCallTests.java +++ /dev/null @@ -1,152 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies multiple downcalls with the same or different layouts or argument/return types. - */ -@Test(groups = { "level.sanity" }) -public class MultiCallTests { - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - - @Test - public void test_twoCallsWithSameFuncDescriptor() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - mh = clinker.downcallHandle(functionSymbol, mt, fd); - result = (int)mh.invokeExact(235, 439); - Assert.assertEquals(result, 674); - } - - @Test - public void test_twoCallsWithDiffFuncDescriptor() throws Throwable { - MethodType mt1 = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd1 = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol1 = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - MethodType mt2 = MethodType.methodType(int.class, int.class, int.class, int.class); - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol2 = nativeLib.lookup("add3Ints").get(); - mh = clinker.downcallHandle(functionSymbol2, mt2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } - - @Test - public void test_multiCallsWithMixedFuncDescriptors() throws Throwable { - MethodType mt1 = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd1 = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol1 = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - MethodType mt2 = MethodType.methodType(int.class, int.class, int.class, int.class); - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol2 = nativeLib.lookup("add3Ints").get(); - mh = clinker.downcallHandle(functionSymbol2, mt2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - - MethodType mt3 = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol3 = nativeLib.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol3, mt3, fd3); - mh.invokeExact(454, 398); - - mh = clinker.downcallHandle(functionSymbol1, mt1, fd1); - result = (int)mh.invokeExact(234, 567); - Assert.assertEquals(result, 801); - - mh = clinker.downcallHandle(functionSymbol2, mt2, fd2); - result = (int)mh.invokeExact(312, 323, 334); - Assert.assertEquals(result, 969); - - mh = clinker.downcallHandle(functionSymbol3, mt3, fd3); - mh.invokeExact(539, 672); - } - - @Test - public void test_twoCallsWithDiffReturnType() throws Throwable { - MethodType mt1 = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd1 = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol1 = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - MethodType mt2 = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol2 = nativeLib.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol2, mt2, fd2); - mh.invokeExact(454, 398); - } - - @Test - public void test_multiCallsWithSameArgLayouts() throws Throwable { - MethodType mt1 = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol1 = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd); - int intResult = (int)mh.invokeExact(112, 123); - Assert.assertEquals(intResult, 235); - - mh = clinker.downcallHandle(functionSymbol1, mt1, fd); - intResult = (int)mh.invokeExact(234, 567); - Assert.assertEquals(intResult, 801); - - MethodType mt2 = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol2 = nativeLib.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol2, mt2, fd2); - mh.invokeExact(454, 398); - - MethodType mt3 = MethodType.methodType(boolean.class, boolean.class, boolean.class); - Symbol functionSymbol3 = nativeLib.lookup("add2BoolsWithOr").get(); - mh = clinker.downcallHandle(functionSymbol3, mt3, fd); - boolean boolResult = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(boolResult, true); - boolResult = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(boolResult, false); - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests1.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests1.java deleted file mode 100644 index b0b15691620..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests1.java +++ /dev/null @@ -1,94 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies the downcalls with the same layout & argument and return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests1 implements Thread.UncaughtExceptionHandler { - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - private volatile Throwable initException; - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDescriptor() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(235, 439); - Assert.assertEquals(result, 674); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests2.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests2.java deleted file mode 100644 index 7f97afe5b92..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests2.java +++ /dev/null @@ -1,96 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies the downcalls with the diffrent layouts and arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests2 implements Thread.UncaughtExceptionHandler { - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - private volatile Throwable initException; - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests3.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests3.java deleted file mode 100644 index 13c8b366b8c..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests3.java +++ /dev/null @@ -1,95 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies the downcalls with the diffrent return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests3 implements Thread.UncaughtExceptionHandler { - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - private volatile Throwable initException; - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffReturnType() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - mh.invokeExact(454, 398); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests4.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests4.java deleted file mode 100644 index 69ef2c1f7fc..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/MultiThreadingTests4.java +++ /dev/null @@ -1,168 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which verifies multiple downcalls combined with the diffrent layouts/arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests4 implements Thread.UncaughtExceptionHandler { - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - private volatile Throwable initException; - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiThreadsWithMixedFuncDescriptors() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(128, 246); - Assert.assertEquals(result, 374); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 642, 971); - Assert.assertEquals(result, 1725); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr3 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr4 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(416, 728); - Assert.assertEquals(result, 1144); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr5 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(1012, 1023, 2035); - Assert.assertEquals(result, 4070); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr6 = new Thread(){ - public void run() { - try { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - boolean result = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(result, false); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - thr3.setUncaughtExceptionHandler(this); - thr4.setUncaughtExceptionHandler(this); - thr5.setUncaughtExceptionHandler(this); - thr6.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - thr3.start(); - thr4.start(); - thr5.start(); - thr6.start(); - - thr6.join(); - thr5.join(); - thr4.join(); - thr3.join(); - thr2.join(); - thr1.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/PrimitiveTypeTests.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/PrimitiveTypeTests.java deleted file mode 100644 index 3e5573fea0b..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/PrimitiveTypeTests.java +++ /dev/null @@ -1,683 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.ValueLayout; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemoryAccess; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.LibraryLookup; -import jdk.incubator.foreign.NativeScope; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for primitive types, - * which covers generic tests, tests with the void type, the MemoryAddress type, and the vararg list. - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinOS = osName.contains("win"); - /* long long is 64 bits on AIX/ppc64, which is the same as Windows */ - private static ValueLayout longLayout = (isWinOS || isAixOS) ? C_LONG_LONG : C_LONG; - - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static LibraryLookup defaultLib = LibraryLookup.ofDefault(); - private static CLinker clinker = CLinker.getInstance(); - - @Test - public void test_addTwoBoolsWithOr() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(result, false); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 1); - boolean result = (boolean)mh.invokeExact(false, intSegmt.address()); - intSegmt.close(); - Assert.assertEquals(result, true); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 0); - result = (boolean)mh.invokeExact(true, intSegmt.address()); - intSegmt.close(); - Assert.assertEquals(result, true); - } - - @Test - public void test_addTwoBoolsWithOr_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2BoolsWithOr").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (boolean)mh.invokeExact(false, true); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar() throws Throwable { - MethodType mt = MethodType.methodType(char.class, char.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - char result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(char.class, MemoryAddress.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setChar(shortSegmt, 'B'); - char result = (char)mh.invokeExact(shortSegmt.address(), 'D'); - shortSegmt.close(); - Assert.assertEquals(result, 'C'); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_POINTER.withName("pointer"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setChar(shortSegmt, 'B'); - result = (char)mh.invokeExact(shortSegmt.address(), 'D'); - shortSegmt.close(); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewChar_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(char.class, char.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - char result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - byte result = (byte)mh.invokeExact((byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_CHAR.withName("char")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (byte)mh.invokeExact((byte)8, (byte)9); - Assert.assertEquals(result, (byte)17); - } - - @Test - public void test_addTwoNegtiveBytes() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - byte result = (byte)mh.invokeExact((byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_CHAR.withName("char")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (byte)mh.invokeExact((byte)-7, (byte)-8); - Assert.assertEquals(result, (byte)-15); - } - - @Test - public void test_addByteAndByteFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, byte.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addByteAndByteFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment charSegmt = MemorySegment.allocateNative(C_CHAR); - MemoryAccess.setByte(charSegmt, (byte)3); - byte result = (byte)mh.invokeExact((byte)6, charSegmt.address()); - charSegmt.close(); - Assert.assertEquals(result, (byte)9); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - charSegmt = MemorySegment.allocateNative(C_CHAR); - MemoryAccess.setByte(charSegmt, (byte)7); - result = (byte)mh.invokeExact((byte)8, charSegmt.address()); - charSegmt.close(); - Assert.assertEquals(result, (byte)15); - } - - @Test - public void test_addTwoBytes_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR); - Symbol functionSymbol = nativeLib.lookup("add2Bytes").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - byte result = (byte)mh.invokeExact((byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_CHAR.withName("char")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (byte)mh.invokeExact((byte)6, (byte)7); - Assert.assertEquals(result, (byte)13); - } - - @Test - public void test_addTwoShorts() throws Throwable { - MethodType mt = MethodType.methodType(short.class, short.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - short result = (short)mh.invokeExact((short)24, (short)32); - Assert.assertEquals(result, (short)56); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (short)mh.invokeExact((short)11, (short)22); - Assert.assertEquals(result, (short)33); - } - - @Test - public void test_addTwoNegtiveShorts() throws Throwable { - MethodType mt = MethodType.methodType(short.class, short.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - short result = (short)mh.invokeExact((short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (short)mh.invokeExact((short)-11, (short)-22); - Assert.assertEquals(result, (short)-33); - } - - @Test - public void test_addShortAndShortFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(short.class, MemoryAddress.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setShort(shortSegmt, (short)24); - short result = (short)mh.invokeExact(shortSegmt.address(), (short)32); - Assert.assertEquals(result, (short)56); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_POINTER.withName("pointer"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setShort(shortSegmt, (short)22); - result = (short)mh.invokeExact(shortSegmt.address(), (short)33); - Assert.assertEquals(result, (short)55); - } - - @Test - public void test_addTwoShorts_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(short.class, short.class, short.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("add2Shorts").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - short result = (short)mh.invokeExact((short)24, (short)32); - Assert.assertEquals(result, (short)56); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (short)mh.invokeExact((short)12, (short)34); - Assert.assertEquals(result, (short)46); - } - - @Test - public void test_addTwoInts() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("short"), C_INT.withName("short"), C_INT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (int)mh.invokeExact(111, 222); - Assert.assertEquals(result, 333); - } - - @Test - public void test_addTwoNegtiveInts() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(-112, -123); - Assert.assertEquals(result, -235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (int)mh.invokeExact(-222, -444); - Assert.assertEquals(result, -666); - } - - @Test - public void test_addIntAndIntFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 215); - int result = (int)mh.invokeExact(321, intSegmt.address()); - intSegmt.close(); - Assert.assertEquals(result, 536); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 333); - result = (int)mh.invokeExact(444, intSegmt.address()); - intSegmt.close(); - Assert.assertEquals(result, 777); - } - - @Test - public void test_addTwoInts_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2Ints").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (int)mh.invokeExact(234, 567); - Assert.assertEquals(result, 801); - } - - @Test - public void test_addTwoIntsReturnVoid() throws Throwable { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - mh.invokeExact(454, 398); - } - - @Test - public void test_addTwoIntsReturnVoid_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(void.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT); - Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - mh.invokeExact(454, 398); - } - - @Test - public void test_addIntAndChar() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("addIntAndChar").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - int result = (int)mh.invokeExact(58, 'A'); - Assert.assertEquals(result, 123); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (int)mh.invokeExact(60, 'B'); - Assert.assertEquals(result, 126); - } - - @Test - public void test_addIntAndChar_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(int.class, int.class, char.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_SHORT); - Symbol functionSymbol = nativeLib.lookup("addIntAndChar").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - int result = (int)mh.invokeExact(58, 'A'); - Assert.assertEquals(result, 123); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_SHORT.withName("short")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (int)mh.invokeExact(60, 'B'); - Assert.assertEquals(result, 126); - } - - @Test - public void test_addTwoLongs() throws Throwable { - MethodType mt = MethodType.methodType(long.class, long.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - long result = (long)mh.invokeExact(57424L, 698235L); - Assert.assertEquals(result, 755659L); - - FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), longLayout.withName("long"), longLayout.withName("long")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (long)mh.invokeExact(333222L, 111555L); - Assert.assertEquals(result, 444777L); - } - - @Test - public void test_addLongAndLongFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, longLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment longSegmt = MemorySegment.allocateNative(longLayout); - MemoryAccess.setLong(longSegmt, 57424L); - long result = (long)mh.invokeExact(longSegmt.address(), 698235L); - longSegmt.close(); - Assert.assertEquals(result, 755659L); - - FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("int"), longLayout.withName("long")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - longSegmt = MemorySegment.allocateNative(longLayout.withName("long")); - MemoryAccess.setLong(longSegmt, 11111L); - result = (long)mh.invokeExact(longSegmt.address(), 22222L); - longSegmt.close(); - Assert.assertEquals(result, 33333L); - } - - @Test - public void test_addTwoLongs_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(long.class, long.class, long.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout); - Symbol functionSymbol = nativeLib.lookup("add2Longs").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - long result = (long)mh.invokeExact(57424L, 698235L); - Assert.assertEquals(result, 755659L); - - FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), longLayout.withName("long"), longLayout.withName("long")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (long)mh.invokeExact(111222L, 333444L); - Assert.assertEquals(result, 444666L); - } - - @Test - public void test_addTwoFloats() throws Throwable { - MethodType mt = MethodType.methodType(float.class, float.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - float result = (float)mh.invokeExact(5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_FLOAT.withName("float"), C_FLOAT.withName("float"), C_FLOAT.withName("float")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (float)mh.invokeExact(15.74f, 16.79f); - Assert.assertEquals(result, 32.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(float.class, float.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT); - MemoryAccess.setFloat(floatSegmt, 6.79f); - float result = (float)mh.invokeExact(5.74f, floatSegmt.address()); - floatSegmt.close(); - Assert.assertEquals(result, 12.53f, 0.01f); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_FLOAT.withName("float"), C_FLOAT.withName("float"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - floatSegmt = MemorySegment.allocateNative(C_FLOAT.withName("float")); - MemoryAccess.setFloat(floatSegmt, 16.79f); - result = (float)mh.invokeExact(15.74f, floatSegmt.address()); - floatSegmt.close(); - Assert.assertEquals(result, 32.53f, 0.01f); - } - - @Test - public void test_addTwoFloats_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(float.class, float.class, float.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT); - Symbol functionSymbol = nativeLib.lookup("add2Floats").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - float result = (float)mh.invokeExact(5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_FLOAT.withName("float"), C_FLOAT.withName("float"), C_FLOAT.withName("float")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (float)mh.invokeExact(15.74f, 16.79f); - Assert.assertEquals(result, 32.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles() throws Throwable { - MethodType mt = MethodType.methodType(double.class, double.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - double result = (double)mh.invokeExact(159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_DOUBLE.withName("double"), C_DOUBLE.withName("double"), C_DOUBLE.withName("double")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - result = (double)mh.invokeExact(1159.748d, 1262.795d); - Assert.assertEquals(result, 2422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer() throws Throwable { - MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE); - MemoryAccess.setDouble(doubleSegmt, 159.748d); - double result = (double)mh.invokeExact(doubleSegmt.address(), 262.795d); - doubleSegmt.close(); - Assert.assertEquals(result, 422.543d, 0.001d); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_DOUBLE.withName("double"), C_POINTER.withName("pointer"), C_DOUBLE.withName("double")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - doubleSegmt = MemorySegment.allocateNative(C_DOUBLE.withName("double")); - MemoryAccess.setDouble(doubleSegmt, 1159.748d); - result = (double)mh.invokeExact(doubleSegmt.address(), 1262.795d); - doubleSegmt.close(); - Assert.assertEquals(result, 2422.543d, 0.001d); - } - - @Test - public void test_addTwoDoubles_fromMemAddr() throws Throwable { - MethodType mt = MethodType.methodType(double.class, double.class, double.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE); - Symbol functionSymbol = nativeLib.lookup("add2Doubles").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - double result = (double)mh.invokeExact(159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_DOUBLE.withName("double"), C_DOUBLE.withName("double"), C_DOUBLE.withName("double")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - result = (double)mh.invokeExact(1159.748d, 1262.795d); - Assert.assertEquals(result, 2422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr() throws Throwable { - Symbol strlenSymbol = defaultLib.lookup("strlen").get(); - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER); - MethodHandle mh = clinker.downcallHandle(strlenSymbol, mt, fd); - MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invokeExact(funcMemSegment.address()); - Assert.assertEquals(strLength, 27); - - FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(strlenSymbol, mt, fd2); - strLength = (long)mh.invokeExact(funcMemSegment.address()); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable { - Symbol strlenSymbol = defaultLib.lookup("strlen").get(); - MemoryAddress memAddr = strlenSymbol.address(); - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invokeExact(funcMemSegment.address()); - Assert.assertEquals(strLength, 27); - - FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("pointer")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - strLength = (long)mh.invokeExact(funcMemSegment.address()); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib() throws Throwable { - Symbol allocSymbol = defaultLib.lookup("malloc").get(); - MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout); - MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocMethodType, allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(10L); - long allocMemAddrValue = allocMemAddr.toRawLongValue(); - - MemorySegment memSeg = MemorySegment.ofNativeRestricted(); - MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15); - Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15); - - Symbol freeSymbol = defaultLib.lookup("free").get(); - MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER); - MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeMethodType, freeFuncDesc); - freeHandle.invokeExact(allocMemAddr); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_fromMemAddr() throws Throwable { - Symbol allocSymbol = defaultLib.lookup("malloc").get(); - MemoryAddress allocMemAddrFromSymbol = allocSymbol.address(); - MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout); - MethodHandle allocHandle = clinker.downcallHandle(allocMemAddrFromSymbol, allocMethodType, allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(10L); - long allocMemAddrValue = allocMemAddr.toRawLongValue(); - - MemorySegment memSeg = MemorySegment.ofNativeRestricted(); - MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15); - Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15); - - Symbol freeSymbol = defaultLib.lookup("free").get(); - MemoryAddress freeMemAddr = freeSymbol.address(); - MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER); - MethodHandle freeHandle = clinker.downcallHandle(freeMemAddr, freeMethodType, freeFuncDesc); - freeHandle.invokeExact(allocMemAddr); - } - - @Test - public void test_memoryAllocFreeFromCLinkerMethod() throws Throwable { - MemoryAddress allocMemAddr = CLinker.allocateMemoryRestricted(10L); - long allocMemAddrValue = allocMemAddr.toRawLongValue(); - - MemorySegment memSeg = MemorySegment.ofNativeRestricted(); - MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 49); - Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 49); - - CLinker.freeMemoryRestricted(allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr() throws Throwable { - Symbol functionSymbol = defaultLib.lookup("printf").get(); - MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n"); - mh.invoke(formatMemSegment.address(), 15, 27, 42); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_POINTER.withName("pointer"), - C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(functionSymbol, mt, fd2); - mh.invoke(formatMemSegment.address(), 115, 127, 242); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable { - Symbol functionSymbol = defaultLib.lookup("printf").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n"); - mh.invoke(formatMemSegment.address(), 15, 27, 42); - - FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_POINTER.withName("pointer"), - C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int")); - mh = clinker.downcallHandle(memAddr, mt, fd2); - mh.invoke(formatMemSegment.address(), 115, 127, 242); - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/StructTests.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/StructTests.java deleted file mode 100644 index 206f6b6c4e1..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/downcall/StructTests.java +++ /dev/null @@ -1,3081 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.nio.ByteOrder; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; - -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemoryLayout.PathElement; -import jdk.incubator.foreign.ValueLayout; -import jdk.incubator.foreign.GroupLayout; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.MemoryAccess; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.MemoryHandles; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for argument/return struct. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class StructTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinOS = osName.contains("win"); - private static ValueLayout longLayout = (isWinOS || isAixOS) ? C_LONG_LONG : C_LONG; - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static CLinker clinker = CLinker.getInstance(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt, 0); - boolHandle2.set(structSegmt, 1); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - structSegmt.close(); - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(boolean.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment booleanSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(booleanSegmt, 1); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt, 0); - boolHandle2.set(structSegmt, 1); - - boolean result = (boolean)mh.invokeExact(booleanSegmt.address(), structSegmt); - Assert.assertEquals(result, false); - booleanSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT, C_INT); - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment booleanSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(booleanSegmt, 0); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 0); - MemoryAccess.setIntAtOffset(structSegmt, 4, 1); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(booleanSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_INT.byteSize()); - VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder()); - int result = (int)intHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 1); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), booleanSegmt.address().toRawLongValue()); - booleanSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt, 1); - boolHandle2.set(structSegmt, 0); - - boolean result = (boolean)mh.invokeExact(false, structSegmt.address()); - Assert.assertEquals(result, true); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_INT.withName("elem2")); - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1); - MemoryAccess.setIntAtOffset(structSegmt, 4, 0); - MemoryAccess.setIntAtOffset(structSegmt, 8, 1); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1); - MemoryAccess.setIntAtOffset(structSegmt, 4, 0); - MemoryAccess.setIntAtOffset(structSegmt, 8, 1); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_INT); - MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1); - MemoryAccess.setIntAtOffset(structSegmt, 4, 0); - MemoryAccess.setIntAtOffset(structSegmt, 8, 1); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(intArray.withName("array_elem1"), C_INT.withName("elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 0); - MemoryAccess.setIntAtOffset(structSegmt, 4, 1); - MemoryAccess.setIntAtOffset(structSegmt, 8, 0); - - int result = (int)mh.invokeExact(0, structSegmt); - Assert.assertEquals(result, 1); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), intArray.withName("array_elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 0); - MemoryAccess.setIntAtOffset(structSegmt, 4, 1); - MemoryAccess.setIntAtOffset(structSegmt, 8, 0); - - int result = (int)mh.invokeExact(0, structSegmt); - Assert.assertEquals(result, 1); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(intArray, C_INT); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1); - MemoryAccess.setIntAtOffset(structSegmt, 4, 0); - MemoryAccess.setIntAtOffset(structSegmt, 8, 1); - - int result = (int)mh.invokeExact(0, structSegmt); - Assert.assertEquals(result, 0); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_INT.withName("elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 0); - MemoryAccess.setIntAtOffset(structSegmt, 4, 1); - MemoryAccess.setIntAtOffset(structSegmt, 8, 0); - MemoryAccess.setIntAtOffset(structSegmt, 12, 1); - MemoryAccess.setIntAtOffset(structSegmt, 16, 0); - - int result = (int)mh.invokeExact(1, structSegmt); - Assert.assertEquals(result, 1); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 0); - MemoryAccess.setIntAtOffset(structSegmt, 4, 1); - MemoryAccess.setIntAtOffset(structSegmt, 8, 0); - MemoryAccess.setIntAtOffset(structSegmt, 12, 1); - MemoryAccess.setIntAtOffset(structSegmt, 16, 0); - - int result = (int)mh.invokeExact(1, structSegmt); - Assert.assertEquals(result, 1); - structSegmt.close(); - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT, C_INT); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_INT); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1); - MemoryAccess.setIntAtOffset(structSegmt, 4, 0); - MemoryAccess.setIntAtOffset(structSegmt, 8, 1); - MemoryAccess.setIntAtOffset(structSegmt, 12, 0); - MemoryAccess.setIntAtOffset(structSegmt, 16, 1); - - int result = (int)mh.invokeExact(0, structSegmt); - Assert.assertEquals(result, 1); - structSegmt.close(); - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt1, 1); - boolHandle2.set(structSegmt1, 0); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt2, 1); - boolHandle2.set(structSegmt2, 1); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), 0); - Assert.assertEquals(boolHandle2.get(resultSegmt), 1); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt1, 1); - boolHandle2.set(structSegmt1, 0); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt2, 1); - boolHandle2.set(structSegmt2, 1); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals(boolHandle1.get(resultSegmt), 0); - Assert.assertEquals(boolHandle2.get(resultSegmt), 1); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"), C_INT.withName("elem3")); - VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(int.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt1, 1); - boolHandle2.set(structSegmt1, 0); - boolHandle3.set(structSegmt1, 1); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - boolHandle1.set(structSegmt2, 1); - boolHandle2.set(structSegmt2, 1); - boolHandle3.set(structSegmt2, 0); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), 0); - Assert.assertEquals(boolHandle2.get(resultSegmt), 1); - Assert.assertEquals(boolHandle3.get(resultSegmt), 1); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact((byte)6, structSegmt); - Assert.assertEquals(result, 23); - structSegmt.close(); - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(byte.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR); - MemoryAccess.setByte(byteSegmt, (byte)12); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invokeExact(byteSegmt.address(), structSegmt); - Assert.assertEquals(result, 42); - byteSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR); - MemoryAccess.setByte(byteSegmt, (byte)12); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(byteSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_CHAR.byteSize()); - VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder()); - byte result = (byte)byteHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 49); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue()); - byteSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(byte.class, byte.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invokeExact((byte)13, structSegmt.address()); - Assert.assertEquals(result, 36); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_CHAR.withName("elem2")); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)22); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize())); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)24); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)36); - - byte result = (byte)mh.invokeExact((byte)48, structSegmt); - Assert.assertEquals(result, 120); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR, C_CHAR); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_CHAR); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)22); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray() throws Throwable { - SequenceLayout byteArray = MemoryLayout.ofSequence(2, C_CHAR); - GroupLayout structLayout = MemoryLayout.ofStruct(byteArray.withName("array_elem1"), - C_CHAR.withName("elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize())); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder() throws Throwable { - SequenceLayout byteArray = MemoryLayout.ofSequence(2, C_CHAR); - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize())); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)14); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)16); - - byte result = (byte)mh.invokeExact((byte)18, structSegmt); - Assert.assertEquals(result, 60); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName() throws Throwable { - SequenceLayout byteArray = MemoryLayout.ofSequence(2, C_CHAR); - GroupLayout structLayout = MemoryLayout.ofStruct(byteArray, C_CHAR); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray() throws Throwable { - GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), - C_CHAR.withName("elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize() * 3)); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)13); - MemoryAccess.setByteAtOffset(structSegmt, 3, (byte)14); - MemoryAccess.setByteAtOffset(structSegmt, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize() * 3)); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)14); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)16); - MemoryAccess.setByteAtOffset(structSegmt, 3, (byte)18); - MemoryAccess.setByteAtOffset(structSegmt, 4, (byte)20); - - byte result = (byte)mh.invokeExact((byte)22, structSegmt); - Assert.assertEquals(result, 102); - structSegmt.close(); - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR, C_CHAR); - SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_CHAR); - MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout); - Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setByteAtOffset(structSegmt, 0, (byte)11); - MemoryAccess.setByteAtOffset(structSegmt, 1, (byte)12); - MemoryAccess.setByteAtOffset(structSegmt, 2, (byte)13); - MemoryAccess.setByteAtOffset(structSegmt, 3, (byte)14); - MemoryAccess.setByteAtOffset(structSegmt, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - structSegmt.close(); - } - - @Test - public void test_add2ByteStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2ByteStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2ByteStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3ByteStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), - C_CHAR.withName("elem3"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(byte.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3ByteStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact('C', structSegmt); - Assert.assertEquals(result, 'D'); - structSegmt.close(); - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(char.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment charSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setChar(charSegmt, 'D'); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invokeExact(charSegmt.address(), structSegmt); - Assert.assertEquals(result, 'M'); - charSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment charSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setChar(charSegmt, 'D'); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(charSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_SHORT.byteSize()); - VarHandle charHandle = MemoryHandles.varHandle(char.class, ByteOrder.nativeOrder()); - char result = (char)charHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 'M'); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), charSegmt.address().toRawLongValue()); - charSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(char.class, char.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invokeExact('G', structSegmt.address()); - Assert.assertEquals(result, 'V'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_SHORT.withName("elem2")); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'E'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'F'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'E'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'F'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray() throws Throwable { - SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(charArray.withName("array_elem1"), - C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'A'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'B'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder() throws Throwable { - SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'A'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'B'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName() throws Throwable { - SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(charArray, C_SHORT, MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'A'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'B'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray() throws Throwable { - GroupLayout charStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, charStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_SHORT.withName("elem2")); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'E'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'F'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'G'); - MemoryAccess.setCharAtOffset(structSegmt, 6, 'H'); - MemoryAccess.setCharAtOffset(structSegmt, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout charStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, charStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'E'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'F'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'G'); - MemoryAccess.setCharAtOffset(structSegmt, 6, 'H'); - MemoryAccess.setCharAtOffset(structSegmt, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - structSegmt.close(); - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout charStruct = MemoryLayout.ofStruct(C_SHORT, C_SHORT); - SequenceLayout structArray = MemoryLayout.ofSequence(2, charStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_SHORT); - MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setCharAtOffset(structSegmt, 0, 'E'); - MemoryAccess.setCharAtOffset(structSegmt, 2, 'F'); - MemoryAccess.setCharAtOffset(structSegmt, 4, 'G'); - MemoryAccess.setCharAtOffset(structSegmt, 6, 'H'); - MemoryAccess.setCharAtOffset(structSegmt, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - structSegmt.close(); - } - - @Test - public void test_add2CharStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2CharStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3CharStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3")); - VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(char.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - short result = (short)mh.invokeExact((short)6, structSegmt); - Assert.assertEquals(result, 23); - structSegmt.close(); - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(short.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setShort(shortSegmt, (short)12); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invokeExact(shortSegmt.address(), structSegmt); - Assert.assertEquals(result, 49); - shortSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT); - MemoryAccess.setShort(shortSegmt, (short)12); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(shortSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_SHORT.byteSize()); - VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder()); - short result = (short)shortHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 49); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue()); - shortSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(short.class, short.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invokeExact((short)66, structSegmt.address()); - Assert.assertEquals(result, 132); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), - C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)31); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)33); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)31); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)33); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_SHORT); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)31); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)33); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray() throws Throwable { - SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(shortArray.withName("array_elem1"), - C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder() throws Throwable { - SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName() throws Throwable { - SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT); - GroupLayout structLayout = MemoryLayout.ofStruct(shortArray, C_SHORT, MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray() throws Throwable { - GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struc_array_elem1"), C_SHORT.withName("elem2")); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - MemoryAccess.setShortAtOffset(structSegmt, 6, (short)444); - MemoryAccess.setShortAtOffset(structSegmt, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - MemoryAccess.setShortAtOffset(structSegmt, 6, (short)444); - MemoryAccess.setShortAtOffset(structSegmt, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - structSegmt.close(); - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT, C_SHORT); - SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_SHORT); - MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setShortAtOffset(structSegmt, 0, (short)111); - MemoryAccess.setShortAtOffset(structSegmt, 2, (short)222); - MemoryAccess.setShortAtOffset(structSegmt, 4, (short)333); - MemoryAccess.setShortAtOffset(structSegmt, 6, (short)444); - MemoryAccess.setShortAtOffset(structSegmt, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - structSegmt.close(); - } - - @Test - public void test_add2ShortStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2ShortStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2ShortStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3ShortStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), - C_SHORT.withName("elem3"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(short.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3ShortStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(2244668, structSegmt); - Assert.assertEquals(result, 4601569); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntShortFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), - C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntShortFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(22334455, structSegmt); - Assert.assertEquals(result, 33590565); - structSegmt.close(); - } - - @Test - public void test_addIntAndShortIntFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), - MemoryLayout.ofPaddingBits(C_SHORT.bitSize()), C_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndShortIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(11335577, structSegmt); - Assert.assertEquals(result, 33815031); - structSegmt.close(); - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 7654321); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invokeExact(intSegmt.address(), structSegmt); - Assert.assertEquals(result, 11356912); - structSegmt.close(); - intSegmt.close(); - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment intSegmt = MemorySegment.allocateNative(C_INT); - MemoryAccess.setInt(intSegmt, 1122333); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(intSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_INT.byteSize()); - VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder()); - int result = (int)intHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 13366998); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), intSegmt.address().toRawLongValue()); - intSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(int.class, int.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invokeExact(19202122, structSegmt.address()); - Assert.assertEquals(result, 45485154); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_INT.withName("elem2")); - - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 21222324); - MemoryAccess.setIntAtOffset(structSegmt, 4, 25262728); - MemoryAccess.setIntAtOffset(structSegmt, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 21222324); - MemoryAccess.setIntAtOffset(structSegmt, 4, 25262728); - MemoryAccess.setIntAtOffset(structSegmt, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_INT); - - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 21222324); - MemoryAccess.setIntAtOffset(structSegmt, 4, 25262728); - MemoryAccess.setIntAtOffset(structSegmt, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(intArray.withName("array_elem1"), C_INT.withName("elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), intArray.withName("array_elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName() throws Throwable { - SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT); - GroupLayout structLayout = MemoryLayout.ofStruct(intArray, C_INT); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_INT.withName("elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444); - MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444); - MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout intStruct = MemoryLayout.ofStruct(C_INT, C_INT); - SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_INT); - MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111); - MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222); - MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333); - MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444); - MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - structSegmt.close(); - } - - @Test - public void test_add2IntStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2IntStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3IntStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"), C_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(int.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - long result = (long)mh.invokeExact(2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - structSegmt.close(); - } - - @Test - public void test_addIntAndIntLongFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), - MemoryLayout.ofPaddingBits(C_INT.bitSize()), longLayout.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndIntLongFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - structSegmt.close(); - } - - @Test - public void test_addIntAndLongIntFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), - C_INT.withName("elem2"), MemoryLayout.ofPaddingBits(C_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addIntAndLongIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - structSegmt.close(); - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment longSegmt = MemorySegment.allocateNative(longLayout); - MemoryAccess.setLong(longSegmt, 1111111111L); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invokeExact(longSegmt.address(), structSegmt); - Assert.assertEquals(result, 9999999999L); - longSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment longSegmt = MemorySegment.allocateNative(longLayout); - MemoryAccess.setLong(longSegmt, 1122334455L); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(longSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(longLayout.byteSize()); - VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder()); - long result = (long)longHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 9034792245L); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), longSegmt.address().toRawLongValue()); - longSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(long.class, long.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invokeExact(668800224466L, structSegmt.address()); - Assert.assertEquals(result, 1339955106732L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 135791357913L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 246802468024L); - MemoryAccess.setLongAtOffset(structSegmt, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 135791357913L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 246802468024L); - MemoryAccess.setLongAtOffset(structSegmt, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(longLayout, longLayout); - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout, nestedStructLayout); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 135791357913L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 246802468024L); - MemoryAccess.setLongAtOffset(structSegmt, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray() throws Throwable { - SequenceLayout longArray = MemoryLayout.ofSequence(2, longLayout); - GroupLayout structLayout = MemoryLayout.ofStruct(longArray.withName("array_elem1"), longLayout.withName("elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder() throws Throwable { - SequenceLayout longArray = MemoryLayout.ofSequence(2, longLayout); - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longArray.withName("array_elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName() throws Throwable { - SequenceLayout longArray = MemoryLayout.ofSequence(2, longLayout); - GroupLayout structLayout = MemoryLayout.ofStruct(longArray, longLayout); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray() throws Throwable { - GroupLayout longStruct = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, longStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), longLayout.withName("elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L); - MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout longStruct = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, longStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), structArray.withName("struct_array_elem2")); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L); - MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - structSegmt.close(); - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout longStruct = MemoryLayout.ofStruct(longLayout, longLayout); - SequenceLayout structArray = MemoryLayout.ofSequence(2, longStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, longLayout); - MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L); - MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L); - MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L); - MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L); - MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - structSegmt.close(); - } - - @Test - public void test_add2LongStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2LongStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals(longHandle1.get(resultSegmt), 11022446688L); - Assert.assertEquals(longHandle2.get(resultSegmt), 8911335576L); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3LongStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2"), longLayout.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(long.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - float result = (float)mh.invokeExact(6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(float.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT); - MemoryAccess.setFloat(floatSegmt, 12.12F); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invokeExact(floatSegmt.address(), structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - structSegmt.close(); - floatSegmt.close(); - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT); - MemoryAccess.setFloat(floatSegmt, 12.12F); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(floatSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_FLOAT.byteSize()); - VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder()); - float result = (float)floatHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 49.69F, 0.01F); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue()); - floatSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(float.class, float.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invokeExact(79.33F, structSegmt.address()); - Assert.assertEquals(result, 160.66F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_FLOAT.withName("elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 31.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 33.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 31.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 33.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_FLOAT, C_FLOAT); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_FLOAT); - - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 31.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 33.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray() throws Throwable { - SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT); - GroupLayout structLayout = MemoryLayout.ofStruct(floatArray.withName("array_elem1"), C_FLOAT.withName("elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder() throws Throwable { - SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT); - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName() throws Throwable { - SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT); - GroupLayout structLayout = MemoryLayout.ofStruct(floatArray, C_FLOAT); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray() throws Throwable { - GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_FLOAT.withName("elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT, C_FLOAT); - SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_FLOAT); - MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout); - Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F); - MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F); - MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F); - MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F); - MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - structSegmt.close(); - } - - @Test - public void test_add2FloatStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2FloatStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3FloatStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"), C_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(float.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - double result = (double)mh.invokeExact(3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct() throws Throwable { - GroupLayout structLayout = null; - MemorySegment structSegmt = null; - - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - if (isAixOS) { - structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_DOUBLE.withName("elem2")); - structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setFloatAtOffset(structSegmt, 0, 18.444F); - MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D); - } else { - structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), - MemoryLayout.ofPaddingBits(C_FLOAT.bitSize()), C_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - } - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct() throws Throwable { - GroupLayout structLayout = null; - MemorySegment structSegmt = null; - - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - if (isAixOS) { - structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_DOUBLE.withName("elem2")); - structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setIntAtOffset(structSegmt, 0, 18); - MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D); - } else { - structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), - MemoryLayout.ofPaddingBits(C_INT.bitSize()), C_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - } - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE); - MemoryAccess.setDouble(doubleSegmt, 112.123D); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invokeExact(doubleSegmt.address(), structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - doubleSegmt.close(); - structSegmt.close(); - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE); - MemoryAccess.setDouble(doubleSegmt, 212.123D); - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(doubleSegmt.address(), structSegmt); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_DOUBLE.byteSize()); - VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder()); - double result = (double)doubleHandle.get(resultSegmt, 0); - Assert.assertEquals(result, 650.368D, 0.001D); - Assert.assertEquals(resultSegmt.address().toRawLongValue(), doubleSegmt.address().toRawLongValue()); - doubleSegmt.close(); - structSegmt.close(); - resultSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemoryAddress.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_POINTER); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invokeExact(66.333D, structSegmt.address()); - Assert.assertEquals(result, 132.666D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_DOUBLE.withName("elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 31.789D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 33.456D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 31.789D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 33.456D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_DOUBLE, C_DOUBLE); - GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_DOUBLE); - - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 31.789D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 33.456D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE); - GroupLayout structLayout = MemoryLayout.ofStruct(doubleArray.withName("array_elem1"), C_DOUBLE.withName("elem2")); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE); - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE); - GroupLayout structLayout = MemoryLayout.ofStruct(doubleArray, C_DOUBLE); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_DOUBLE.withName("elem2")); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D); - MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.ofSequence(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D); - MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.ofStruct(C_DOUBLE, C_DOUBLE); - SequenceLayout structArray = MemoryLayout.ofSequence(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_DOUBLE); - MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout); - Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt = MemorySegment.allocateNative(structLayout); - MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D); - MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D); - MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D); - MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D); - MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - structSegmt.close(); - } - - @Test - public void test_add2DoubleStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add2DoubleStructs_returnStructPointer() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - - MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout); - Symbol functionSymbol = nativeLib.lookup("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invokeExact(structSegmt1.address(), structSegmt2); - MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize()); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } - - @Test - public void test_add3DoubleStructs_returnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"), C_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(double.class, PathElement.groupElement("elem3")); - - MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Symbol functionSymbol = nativeLib.lookup("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - structSegmt1.close(); - structSegmt2.close(); - resultSegmt.close(); - } -} diff --git a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/valist/VaListTests.java b/test/functional/Java16andUp/src_160/org/openj9/test/jep389/valist/VaListTests.java deleted file mode 100644 index 23f9643920f..00000000000 --- a/test/functional/Java16andUp/src_160/org/openj9/test/jep389/valist/VaListTests.java +++ /dev/null @@ -1,154 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep389.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import jdk.incubator.foreign.CLinker; -import static jdk.incubator.foreign.CLinker.*; -import static jdk.incubator.foreign.CLinker.VaList.Builder; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.ValueLayout; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemoryAccess; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.LibraryLookup; -import static jdk.incubator.foreign.LibraryLookup.Symbol; -import jdk.incubator.foreign.NativeScope; - -/** - * Test cases for JEP 389: Foreign Linker API (Incubator) DownCall for the vararg list. - */ -@Test(groups = { "level.sanity" }) -public class VaListTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinOS = osName.contains("win"); - /* long long is 64 bits on AIX/ppc64, which is the same as Windows */ - private static ValueLayout longLayout = (isWinOS || isAixOS) ? C_LONG_LONG : C_LONG; - - private static LibraryLookup nativeLib = LibraryLookup.ofLibrary("clinkerffitests"); - private static LibraryLookup defaultLib = LibraryLookup.ofDefault(); - private static CLinker clinker = CLinker.getInstance(); - - @Test - public void test_addIntsWithVaList() throws Throwable { - Symbol functionSymbol = nativeLib.lookup("addIntsFromVaList").get(); - MethodType mt = MethodType.methodType(int.class, int.class, VaList.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_VA_LIST); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - try (NativeScope scope = NativeScope.unboundedScope()) { - VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromInt(C_INT, 700) - .vargFromInt(C_INT, 800) - .vargFromInt(C_INT, 900) - .vargFromInt(C_INT, 1000), scope); - int result = (int)mh.invoke(4, vaList); - Assert.assertEquals(result, 3400); - } - } - - @Test - public void test_addLongsWithVaList() throws Throwable { - Symbol functionSymbol = nativeLib.lookup("addLongsFromVaList").get(); - MethodType mt = MethodType.methodType(long.class, int.class, VaList.class); - FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, C_VA_LIST); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - try (NativeScope scope = NativeScope.unboundedScope()) { - VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromLong(longLayout, 700000L) - .vargFromLong(longLayout, 800000L) - .vargFromLong(longLayout, 900000L) - .vargFromLong(longLayout, 1000000L), scope); - long result = (long)mh.invoke(4, vaList); - Assert.assertEquals(result, 3400000L); - } - } - - @Test - public void test_addDoublesWithVaList() throws Throwable { - Symbol functionSymbol = nativeLib.lookup("addDoublesFromVaList").get(); - MethodType mt = MethodType.methodType(double.class, int.class, VaList.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_INT, C_VA_LIST); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - try (NativeScope scope = NativeScope.unboundedScope()) { - VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromDouble(C_DOUBLE, 150.1001D) - .vargFromDouble(C_DOUBLE, 160.2002D) - .vargFromDouble(C_DOUBLE, 170.1001D) - .vargFromDouble(C_DOUBLE, 180.2002D), scope); - double result = (double)mh.invoke(4, vaList); - Assert.assertEquals(result, 660.6006D); - } - } - - @Test - public void test_vprintfFromDefaultLibWithVaList() throws Throwable { - /* 1) Disable the test on Windows given a misaligned access exception coming from - * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString() - * is also captured on OpenJDK/Hotspot. - * 2) Disable the test on AIX as the default library loading is not yet implemented in OpenJDK. - */ - if (!isWinOS && !isAixOS) { - Symbol functionSymbol = defaultLib.lookup("vprintf").get(); - MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, VaList.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST); - MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd); - - try (NativeScope scope = NativeScope.unboundedScope()) { - MemorySegment formatMemSegment = CLinker.toCString("%d * %d = %d\n", scope); - VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromInt(C_INT, 7) - .vargFromInt(C_INT, 8) - .vargFromInt(C_INT, 56), scope); - mh.invoke(formatMemSegment.address(), vaList); - } - } - } - - @Test - public void test_vprintfFromDefaultLibWithVaList_fromMemAddr() throws Throwable { - /* 1) Disable the test on Windows given a misaligned access exception coming from - * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString() - * is also captured on OpenJDK/Hotspot. - * 2) Disable the test on AIX as the default library loading is not yet implemented in OpenJDK. - */ - if (!isWinOS && !isAixOS) { - Symbol functionSymbol = defaultLib.lookup("vprintf").get(); - MemoryAddress memAddr = functionSymbol.address(); - MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, VaList.class); - FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST); - MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd); - - try (NativeScope scope = NativeScope.unboundedScope()) { - MemorySegment formatMemSegment = CLinker.toCString("%d * %d = %d\n", scope); - VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromInt(C_INT, 7) - .vargFromInt(C_INT, 8) - .vargFromInt(C_INT, 56), scope); - mh.invoke(formatMemSegment.address(), vaList); - } - } - } -} diff --git a/test/functional/Java16andUp/testng_160.xml b/test/functional/Java16andUp/testng_160.xml deleted file mode 100644 index 8c86d82d5cf..00000000000 --- a/test/functional/Java16andUp/testng_160.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java18andUp/build.xml b/test/functional/Java18andUp/build.xml deleted file mode 100644 index dedb6e18ff1..00000000000 --- a/test/functional/Java18andUp/build.xml +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - Tests for Java 18 and up - - - - - - - - - - - - - - - - - - - Ant version is ${ant.version} - ============COMPILER SETTINGS============ - ===fork: yes - ===executable: ${compiler.javac} - ===debug: on - ===destdir: ${DEST} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java18andUp/playlist.xml b/test/functional/Java18andUp/playlist.xml deleted file mode 100644 index 5f3251b4b8c..00000000000 --- a/test/functional/Java18andUp/playlist.xml +++ /dev/null @@ -1,144 +0,0 @@ - - - - - Jep419Tests_testClinkerFfi_DownCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-modules jdk.incubator.foreign \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_180.xml$(Q) -testnames Jep419Tests_testClinkerFfi_DownCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.x86,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 18 - - - - - Jep419Tests_testClinkerFfi_UpCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-modules jdk.incubator.foreign \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_180.xml$(Q) -testnames Jep419Tests_testClinkerFfi_UpCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.x86,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 18 - - - - - Jep419Tests_testClinkerFfi_VaList - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-modules jdk.incubator.foreign \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_180.xml$(Q) -testnames Jep419Tests_testClinkerFfi_VaList \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.x86,^arch.aarch64,^arch.ppc,^arch.390,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 18 - - - - - CloseScope0Tests - - - https://github.com/eclipse-openj9/openj9/issues/14028#issuecomment-988225623 - hotspot - - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-opens jdk.incubator.foreign/jdk.internal.foreign=ALL-UNNAMED \ - --add-opens java.base/jdk.internal.misc=ALL-UNNAMED \ - --add-opens java.base/java.lang=ALL-UNNAMED \ - --add-modules jdk.incubator.foreign \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_180.xml$(Q) \ - -testnames CloseScope0Tests \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - - sanity - - - functional - - - 18 - - - diff --git a/test/functional/Java18andUp/src/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java b/test/functional/Java18andUp/src/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java deleted file mode 100644 index f4ca0f2a940..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/foreignMemoryAccess/TestCloseScope0.java +++ /dev/null @@ -1,187 +0,0 @@ -package org.openj9.test.foreignMemoryAccess; - -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ - -import org.testng.Assert; -import static org.testng.Assert.*; -import org.testng.annotations.Test; -import org.openj9.test.util.VersionCheck; - -import java.lang.reflect.*; -import java.lang.ref.Cleaner; -import jdk.internal.misc.ScopedMemoryAccess.*; - -import org.objectweb.asm.*; -import static org.objectweb.asm.Opcodes.*; - -@Test(groups = { "level.sanity" }) -public class TestCloseScope0 { - public static byte[] dump() throws Exception { - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); - MethodVisitor mv; - AnnotationVisitor av0; - - cw.visit(V16, ACC_PUBLIC + ACC_SUPER, "jdk/internal/misc/RunInScoped", null, "java/lang/Object", null); - { - mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); - mv.visitCode(); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); - mv.visitInsn(RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - { - /** - * @Scoped // <-- package private annotation that isn't visible here. This is why we use ASM to generate the class - * public static void runInScoped(Runnable r, Scope scope) { - * r.run(); - * Reference.reachabilityFence(scope); - * } - */ - mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "runInScoped", "(Ljava/lang/Runnable;Ljdk/internal/misc/ScopedMemoryAccess$Scope;)V", null, null); - { - av0 = mv.visitAnnotation("Ljdk/internal/misc/ScopedMemoryAccess$Scoped;", true); - av0.visitEnd(); - } - mv.visitCode(); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Runnable", "run", "()V", true); - mv.visitVarInsn(ALOAD, 1); - mv.visitMethodInsn(INVOKESTATIC, "java/lang/ref/Reference", "reachabilityFence", "(Ljava/lang/Object;)V", false); - mv.visitInsn(RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - cw.visitEnd(); - - return cw.toByteArray(); - } - - private static Throwable expected = null; - private static volatile boolean t1Started = false; - private static volatile boolean t2Waiting = false; - - @Test(expectedExceptions=java.lang.IllegalStateException.class) - public static void closeScopeDuringAccess() throws Throwable { - /* Reflect setup */ - Class memoryOrResourceScope; - Method createShared; - Method close; - Object scope; - - int version = VersionCheck.major(); - if (version == 16) { - memoryOrResourceScope = Class.forName("jdk.internal.foreign.MemoryScope"); - createShared = memoryOrResourceScope.getDeclaredMethod("createShared", new Class[] {Object.class, Runnable.class, Cleaner.class}); - createShared.setAccessible(true); - scope = createShared.invoke(null, null, new Thread(), null); - } else { - memoryOrResourceScope = Class.forName("jdk.internal.foreign.ResourceScopeImpl"); - createShared = memoryOrResourceScope.getDeclaredMethod("createShared", new Class[] {Cleaner.class}); - createShared.setAccessible(true); - scope = createShared.invoke(null, Cleaner.create()); - } - close = memoryOrResourceScope.getDeclaredMethod("close"); - close.setAccessible(true); - /* End Reflect setup */ - - /* ASM setup */ - final byte[] classBytes = dump(); - - ClassLoader loader = ClassLoader.getSystemClassLoader(); - Class cls = Class.forName("java.lang.ClassLoader"); - Method defineClass = cls.getDeclaredMethod( - "defineClass", - new Class[] { String.class, byte[].class, int.class, int.class }); - defineClass.setAccessible(true); - - Object[] dcArgs = new Object[] {"jdk.internal.misc.RunInScoped", classBytes, 0, classBytes.length}; - Class RunInScoped = (Class)defineClass.invoke(loader, dcArgs); - Method runInScoped = RunInScoped.getDeclaredMethod("runInScoped", new Class[] {Runnable.class, Scope.class}); - /* End ASM setup */ - - Synch synch1 = new Synch(); - Synch synch2 = new Synch(); - - Thread t1 = new Thread(()->{ - try { - synchronized (synch1) { - t1Started = true; - synch1.wait(); - } - close.invoke(scope); - } catch (InvocationTargetException e) { - // This is the expected behaviour (throws IllegalStateException) - expected = e.getCause(); - } catch (InterruptedException | IllegalAccessException e) { - e.printStackTrace(); - } finally { - while (!t2Waiting) { - Thread.yield(); - } - synchronized (synch2) { - synch2.notify(); - } - } - }, "ScopeCloserThread"); - - class MyRunnable implements Runnable { - public void run() { - try { - while (!t1Started) { - Thread.yield(); - } - synchronized (synch1) { - synch1.notify(); - } - synchronized (synch2) { - t2Waiting = true; - synch2.wait(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - MyRunnable r = new MyRunnable(); - Thread t2 = new Thread(()->{ - try { - runInScoped.invoke(null, r, scope); - } catch (Exception e) { - e.printStackTrace(); - } - }, "RunInScopeThread"); - - t1.start(); - t2.start(); - - t1.join(); - t2.join(); - - if (expected != null) throw expected; - } -} - -class Synch {} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/InvalidDownCallTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/InvalidDownCallTests.java deleted file mode 100644 index b8e96994f6f..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/InvalidDownCallTests.java +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies the illegal cases including unsupported layouts, etc. - * Note: the majority of illegal cases are removed given the corresponding method type - * is deduced from the function descriptor which is verified in OpenJDK. - */ -@Test(groups = { "level.sanity" }) -public class InvalidDownCallTests { - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForIntType() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, MemoryLayout.paddingLayout(32)); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForMemoryAddress() throws Throwable { - NativeSymbol functionSymbol = clinker.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, MemoryLayout.paddingLayout(64)); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForReturnType() throws Throwable { - NativeSymbol functionSymbol = clinker.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(MemoryLayout.paddingLayout(64), JAVA_LONG); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiCallTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiCallTests.java deleted file mode 100644 index e59d2035bda..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiCallTests.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies multiple downcalls with the same or different layouts or argument/return types. - */ -@Test(groups = { "level.sanity" }) -public class MultiCallTests { - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_twoCallsWithSameFuncDescriptor() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - mh = clinker.downcallHandle(functionSymbol, fd); - result = (int)mh.invokeExact(235, 439); - Assert.assertEquals(result, 674); - } - - @Test - public void test_twoCallsWithDiffFuncDescriptor() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add3Ints").get(); - mh = clinker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } - - @Test - public void test_multiCallsWithMixedFuncDescriptors() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add3Ints").get(); - mh = clinker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - - FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol3 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(454, 398); - - mh = clinker.downcallHandle(functionSymbol1, fd1); - result = (int)mh.invokeExact(234, 567); - Assert.assertEquals(result, 801); - - mh = clinker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(312, 323, 334); - Assert.assertEquals(result, 969); - - mh = clinker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(539, 672); - } - - @Test - public void test_twoCallsWithDiffReturnType() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } - - @Test - public void test_multiCallsWithSameArgLayouts() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd); - int intResult = (int)mh.invokeExact(112, 123); - Assert.assertEquals(intResult, 235); - - mh = clinker.downcallHandle(functionSymbol1, fd); - intResult = (int)mh.invokeExact(234, 567); - Assert.assertEquals(intResult, 801); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = clinker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests1.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests1.java deleted file mode 100644 index fad2777dc89..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests1.java +++ /dev/null @@ -1,112 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies the downcalls with the same downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SameDowncallHandler() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(322, intSegmt); - Assert.assertEquals(result, 537); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests2.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests2.java deleted file mode 100644 index 522a9341b69..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests2.java +++ /dev/null @@ -1,133 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.GroupLayout; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemoryLayout.PathElement; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies the downcalls with the shared downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - private static final FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - private static final NativeSymbol functionSymbol = SymbolLookup.loaderLookup().lookup("add2IntStructs_returnStruct").get(); - private static final MethodHandle mh = CLinker.systemCLinker().downcallHandle(functionSymbol, fd); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SharedDowncallHandler() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001123); - intHandle2.set(structSegmt2, 33445567); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224467); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113355); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests3.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests3.java deleted file mode 100644 index 0559f48ca29..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests3.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies the downcalls with the diffrent layouts and arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests3 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests4.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests4.java deleted file mode 100644 index 2bb7dab1197..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests4.java +++ /dev/null @@ -1,97 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies the downcalls with the diffrent return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests4 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffReturnType() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests5.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests5.java deleted file mode 100644 index af9ee88abc7..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/MultiThreadingTests5.java +++ /dev/null @@ -1,166 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall, - * which verifies multiple downcalls combined with the diffrent layouts/arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests5 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiThreadsWithMixedFuncDescriptors() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(128, 246); - Assert.assertEquals(result, 374); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 642, 971); - Assert.assertEquals(result, 1725); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr4 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(416, 728); - Assert.assertEquals(result, 1144); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr5 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(1012, 1023, 2035); - Assert.assertEquals(result, 4070); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr6 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(result, false); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - thr3.setUncaughtExceptionHandler(this); - thr4.setUncaughtExceptionHandler(this); - thr5.setUncaughtExceptionHandler(this); - thr6.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - thr3.start(); - thr4.start(); - thr5.start(); - thr6.start(); - - thr6.join(); - thr5.join(); - thr4.join(); - thr3.join(); - thr2.join(); - thr1.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests1.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests1.java deleted file mode 100644 index 19d2489b120..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests1.java +++ /dev/null @@ -1,295 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(NativeSymbol symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests1 { - private static CLinker clinker = CLinker.systemCLinker(); - private static ResourceScope resourceScope = ResourceScope.newImplicitScope(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.nativeAllocator(resourceScope); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, resourceScope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - char result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke((byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(-112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } - - @Test - public void test_addIntAndChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndChar").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - long result = (long)mh.invokeExact(57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - float result = (float)mh.invokeExact(5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - double result = (double)mh.invokeExact(159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_1() throws Throwable { - NativeSymbol strlenSymbol = clinker.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = clinker.downcallHandle(strlenSymbol, fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP419 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable { - NativeSymbol allocSymbol = clinker.lookup("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(10L); - allocMemAddr.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15); - - NativeSymbol freeSymbol = clinker.lookup("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeFuncDesc); - freeHandle.invoke(allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_1() throws Throwable { - NativeSymbol functionSymbol = clinker.lookup("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests2.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests2.java deleted file mode 100644 index e13a1a6f876..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/PrimitiveTypeTests2.java +++ /dev/null @@ -1,297 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.VaList; -import static jdk.incubator.foreign.ValueLayout.*; -import static jdk.incubator.foreign.VaList.Builder; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests2 { - private static CLinker clinker = CLinker.systemCLinker(); - private static ResourceScope resourceScope = ResourceScope.newImplicitScope(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.nativeAllocator(resourceScope); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = clinker.downcallHandle(fd); - boolean result = (boolean)mh.invokeExact(functionSymbol, true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, resourceScope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(functionSymbol, false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = clinker.downcallHandle(fd); - char result = (char)mh.invokeExact(functionSymbol, 'B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(functionSymbol, charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = clinker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke(functionSymbol, (byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = clinker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(functionSymbol, shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = clinker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, -112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(functionSymbol, 321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = clinker.downcallHandle(fd); - mh.invokeExact(functionSymbol, 454, 398); - } - - @Test - public void test_addIntAndChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndChar").get(); - MethodHandle mh = clinker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Longs").get(); - MethodHandle mh = clinker.downcallHandle(fd); - long result = (long)mh.invokeExact(functionSymbol, 57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(functionSymbol, longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Floats").get(); - MethodHandle mh = clinker.downcallHandle(fd); - float result = (float)mh.invokeExact(functionSymbol, 5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(functionSymbol, 5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Doubles").get(); - MethodHandle mh = clinker.downcallHandle(fd); - double result = (double)mh.invokeExact(functionSymbol, 159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(functionSymbol, doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_2() throws Throwable { - NativeSymbol strlenSymbol = clinker.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = clinker.downcallHandle(fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP419 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(strlenSymbol, funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_2() throws Throwable { - NativeSymbol allocSymbol = clinker.lookup("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = clinker.downcallHandle(allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(allocSymbol, 10L); - allocMemAddr.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15); - - NativeSymbol freeSymbol = clinker.lookup("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = clinker.downcallHandle(freeFuncDesc); - freeHandle.invoke(freeSymbol, allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_2() throws Throwable { - NativeSymbol functionSymbol = clinker.lookup("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = clinker.downcallHandle(fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(functionSymbol, formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests1.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests1.java deleted file mode 100644 index 467f6fcf5ee..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests1.java +++ /dev/null @@ -1,3103 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.GroupLayout; -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.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(NativeSymbol symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests1 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(boolSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact((byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(byteSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke((byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact((byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact((byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact((byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact('C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - - short result = (short)mh.invokeExact((short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke((short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get(); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(intSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invokeExact(2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invokeExact(6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(floatSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invokeExact(3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests2.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests2.java deleted file mode 100644 index 17ebf98674d..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/downcall/StructTests2.java +++ /dev/null @@ -1,3100 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.GroupLayout; -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.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.SymbolLookup; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests2 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(functionSymbol, boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, boolSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(functionSymbol, byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, byteSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke(functionSymbol, (byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStruct").get(); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact(functionSymbol, 'C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(functionSymbol, charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, charSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke(functionSymbol, 'G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - short result = (short)mh.invokeExact(functionSymbol, (short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(functionSymbol, shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, shortSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke(functionSymbol, (short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get(); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MethodHandle mh = clinker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(functionSymbol, 2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(functionSymbol, 22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(functionSymbol, 11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(functionSymbol, intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, intSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(functionSymbol, 19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - long result = (long)mh.invokeExact(functionSymbol, 2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(functionSymbol, 22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(functionSymbol, 1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(functionSymbol, longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, longSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(functionSymbol, 668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - float result = (float)mh.invokeExact(functionSymbol, 6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(functionSymbol, floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, floatSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(functionSymbol, 79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - double result = (double)mh.invokeExact(functionSymbol, 3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_2() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_2() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(functionSymbol, 66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = clinker.downcallHandle(fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallMHTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallMHTests.java deleted file mode 100644 index 13180ce43fc..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallMHTests.java +++ /dev/null @@ -1,560 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) intended for - * the situations when the multiple primitive specific upcalls happen within - * the same resource scope or from different resource scopes. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallMHTests { - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr1); - Assert.assertEquals(result, true); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - result = (boolean)mh.invoke(true, false, upcallFuncAddr2); - Assert.assertEquals(result, true); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - result = (boolean)mh.invoke(true, false, upcallFuncAddr3); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - MemorySegment charSegmt1 = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt1, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - MemorySegment charSegmt2 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt2, 'D', upcallFuncAddr2); - Assert.assertEquals(result, 'C'); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - MemorySegment charSegmt3 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt3, 'D', upcallFuncAddr3); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr1); - Assert.assertEquals(result, (byte)88); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - result = (byte)mh.invoke((byte)33, upcallFuncAddr2); - Assert.assertEquals(result, (byte)88); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - result = (byte)mh.invoke((byte)33, upcallFuncAddr3); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - MemorySegment shortSegmt1 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr1 = (MemoryAddress)mh.invoke(shortSegmt1, (short)555, upcallFuncAddr1); - Assert.assertEquals(resultAddr1.get(JAVA_SHORT, 0), (short)999); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - MemorySegment shortSegmt2 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr2 = (MemoryAddress)mh.invoke(shortSegmt2, (short)555, upcallFuncAddr2); - Assert.assertEquals(resultAddr2.get(JAVA_SHORT, 0), (short)999); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - MemorySegment shortSegmt3 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr3 = (MemoryAddress)mh.invoke(shortSegmt3, (short)555, upcallFuncAddr3); - Assert.assertEquals(resultAddr3.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr1); - Assert.assertEquals(result, 222235); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr2); - Assert.assertEquals(result, 222235); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr3); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr1); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr2); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr3); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(111454, 111398, upcallFuncAddr); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt1, 6666698235L, upcallFuncAddr1); - Assert.assertEquals(result, 12409155659L); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt2, 6666698235L, upcallFuncAddr2); - Assert.assertEquals(result, 12409155659L); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt3, 6666698235L, upcallFuncAddr3); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - float result = (float)mh.invoke(5.74F, upcallFuncAddr1); - Assert.assertEquals(result, 12.53F, 0.01F); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - result = (float)mh.invoke(5.74F, upcallFuncAddr2); - Assert.assertEquals(result, 12.53F, 0.01F); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - result = (float)mh.invoke(5.74F, upcallFuncAddr3); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - - NativeSymbol upcallFuncAddr1 = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr1 = (MemoryAddress)mh.invoke(doubleSegmt1, 1262.795D, upcallFuncAddr1); - Assert.assertEquals(resultAddr1.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - NativeSymbol upcallFuncAddr2 = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr2 = (MemoryAddress)mh.invoke(doubleSegmt2, 1262.795D, upcallFuncAddr2); - Assert.assertEquals(resultAddr2.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - NativeSymbol upcallFuncAddr3 = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr3 = (MemoryAddress)mh.invoke(doubleSegmt3, 1262.795D, upcallFuncAddr3); - Assert.assertEquals(resultAddr3.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests1.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests1.java deleted file mode 100644 index df2eaf7dd5a..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests1.java +++ /dev/null @@ -1,135 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within different resource scopes, in which case the upcall - * metadata and the generated thunk are allocated separately. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithDiffScopes() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests2.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests2.java deleted file mode 100644 index 28d42c85b2b..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/MultiUpcallThrdsMHTests2.java +++ /dev/null @@ -1,128 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within the same resource scope, in which case the upcall - * metadata and the generated thunk are only allocated once and shared among - * these threads. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static CLinker clinker = CLinker.systemCLinker(); - private static ResourceScope resourceScope = ResourceScope.newImplicitScope(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithSameScope() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), resourceScope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), resourceScope); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), resourceScope); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithMixedSigStruTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithMixedSigStruTests.java deleted file mode 100644 index 87cfdd78987..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithMixedSigStruTests.java +++ /dev/null @@ -1,892 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.GroupLayout; -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.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for the mixed native signatures - * in argument/return struct in upcall, which are not covered in UpcallMHWithStructTests and - * specially designed to validate the native signature types required in the genenerated thunk. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithMixedSigStruTests { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addIntAndIntShortFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntShortFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invoke(22334455, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndShortIntFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invoke(11335577, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntAndIntLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invoke(22446688, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndLongIntFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invoke(1234567, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111844.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 111111111); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111546.221D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - public static void test_addDoubleAndDoubleFloatPlusPaddingFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAnd2FloatsDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAnd2FloatsDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAnd2FloatsDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDouble2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDouble2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDouble2FloatsFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndInt2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt2FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 111111); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addDoubleAndIntFloatDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatIntDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22.33F); - elemHandle2.set(structSegmt, 111111111); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndLongDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndLongDoubleFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndLongDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22222222222222L); - elemHandle2.set(structSegmt, 33333.444D); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 22222222311110.555D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt3FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndInt3FloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt3FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 77777777); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - elemHandle4.set(structSegmt, 44.55F); - - float result = (float)mh.invoke(66.678F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.778F, 0.001F); - } - } - - @Test - public void test_addLongAndLong2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLong2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLong2FloatsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 777777777777L); - elemHandle2.set(structSegmt, 11.25F); - elemHandle3.set(structSegmt, 22.75F); - - long result = (long)mh.invoke(555555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1333333333365L); - } - } - - @Test - public void test_addFloatAnd3FloatsIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_INT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAnd3FloatsIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAnd3FloatsIntFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 44.55F); - elemHandle4.set(structSegmt, 77777777); - - float result = (float)mh.invoke(66.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.556F, 0.001F); - } - } - - @Test - public void test_addLongAndFloatLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndFloatLongFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndFloatLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 55.11F); - elemHandle2.set(structSegmt, 150000000000L); - - long result = (long)mh.invoke(5555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 155555555610L); - } - } - - @Test - public void test_addDoubleAndDoubleFloatIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 111111111); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.341D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33333.444D); - elemHandle2.set(structSegmt, 222222222222L); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 222222311110.555D, 0.001D); - } - } - - @Test - public void test_addLongAnd2FloatsLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAnd2FloatsLongFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAnd2FloatsLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.11F); - elemHandle2.set(structSegmt, 22.11F); - elemHandle3.set(structSegmt, 4444444444L); - - long result = (long)mh.invoke(11111111111L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15555555588L); - } - } - - @Test - public void test_addShortAnd3ShortsCharFromStructByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(3, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAnd3ShortsCharFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAnd3ShortsCharFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1000); - structSegmt.set(JAVA_SHORT, 2, (short)2000); - structSegmt.set(JAVA_SHORT, 4, (short)3000); - structSegmt.set(JAVA_CHAR, 6, 'A'); - - short result = (short)mh.invoke((short)4000, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 10065); - } - } - - @Test - public void test_addFloatAndIntFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndIntFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndIntFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 555555555); - elemHandle2.set(structSegmt, 11.222F); - elemHandle3.set(structSegmt, 666666666); - elemHandle4.set(structSegmt, 33.444F); - - float result = (float)mh.invoke(77.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1222222343.122F, 0.001F); - } - } - - @Test - public void test_addDoubleAndIntDoubleFloatFromStructByUpcallMH() throws Throwable { - /* The size of [int, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 7777); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33.444F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 6666); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15217.122D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFloatFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.222F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 818.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleLongFromStructByUpcallMH() throws Throwable { - /* The size of [int, double, long] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 24 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - elemHandle3.set(structSegmt, 888888888888L); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 889000000732.344D, 0.001D); - } - } - - @Test - public void test_return254BytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("return254BytesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_return254BytesFromStruct, - FunctionDescriptor.of(structLayout), scope); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 254; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } - - @Test - public void test_return4KBytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("return4KBytesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_return4KBytesFromStruct, - FunctionDescriptor.of(structLayout), scope); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 4096; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithPrimTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithPrimTests.java deleted file mode 100644 index a1139cb356e..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithPrimTests.java +++ /dev/null @@ -1,724 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.MemoryAddress; -import jdk.incubator.foreign.MemoryLayout; -import jdk.incubator.foreign.MemorySegment; -import jdk.incubator.foreign.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for primitive types in upcall. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithPrimTests { - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), scope); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPointerWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), scope); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromNativePtrWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromNativePtrWithOrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), scope); - boolean result = (boolean)mh.invoke(false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), scope); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), scope); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addTwoBytesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BytesByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Bytes, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE), scope); - byte result = (byte)mh.invoke((byte)6, (byte)3, upcallFuncAddr); - Assert.assertEquals(result, 9); - } - } - - @Test - public void test_addByteAndByteFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)7); - byte result = (byte)mh.invoke((byte)8, byteSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, 88); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_createNewCharFrom2CharsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFrom2CharsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFrom2Chars, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR), scope); - char result = (char)mh.invoke('B', 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), scope); - char result = (char)mh.invoke('D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_addTwoShortsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Shorts, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT), scope); - short result = (short)mh.invoke((short)1111, (short)2222, upcallFuncAddr); - Assert.assertEquals(result, 3333); - } - } - - @Test - public void test_addShortAndShortFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)2222); - short result = (short)mh.invoke(shortSegmt, (short)3333, upcallFuncAddr); - Assert.assertEquals(result, 5555); - } - } - - @Test - public void test_addShortAndShortFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), scope); - short result = (short)mh.invoke((short)789, upcallFuncAddr); - Assert.assertEquals(result, 1245); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addIntAndIntFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - int result = (int)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(result, 555536); - } - } - - @Test - public void test_addIntAndIntFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), scope); - int result = (int)mh.invoke(222222, upcallFuncAddr); - Assert.assertEquals(result, 666666); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_add3IntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3IntsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, 111124, upcallFuncAddr); - Assert.assertEquals(result, 333359); - } - } - - @Test - public void test_addIntAndCharByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndCharByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndChar, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR), scope); - int result = (int)mh.invoke(555558, 'A', upcallFuncAddr); - Assert.assertEquals(result, 555623); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), scope); - mh.invoke(44454, 333398, upcallFuncAddr); - } - } - - @Test - public void test_addTwoLongsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Longs, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG), scope); - long result = (long)mh.invoke(333333222222L, 111111555555L, upcallFuncAddr); - Assert.assertEquals(result, 444444777777L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), scope); - long result = (long)mh.invoke(5555555555L, upcallFuncAddr); - Assert.assertEquals(result, 8888888888L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addTwoFloatsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatsByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Floats, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT), scope); - float result = (float)mh.invoke(15.74F, 16.79F, upcallFuncAddr); - Assert.assertEquals(result, 32.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - float result = (float)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_add2DoublesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoublesByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2Doubles, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE), scope); - double result = (double)mh.invoke(159.748D, 262.795D, upcallFuncAddr); - Assert.assertEquals(result, 422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - double result = (double)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromNativePtrByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), scope); - double result = (double)mh.invoke(1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_qsortByUpcallMH() throws Throwable { - int expectedArray[] = {11, 12, 13, 14, 15, 16, 17}; - int expectedArrayLength = expectedArray.length; - - FunctionDescriptor fd = FunctionDescriptor.ofVoid(ADDRESS, JAVA_INT, JAVA_INT, ADDRESS); - NativeSymbol functionSymbol = clinker.lookup("qsort").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_compare, - FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment arraySegmt = allocator.allocateArray(JAVA_INT, new int[]{17, 14, 13, 16, 15, 12, 11}); - mh.invoke(arraySegmt, 7, 4, upcallFuncAddr); - int[] sortedArray = arraySegmt.toArray(JAVA_INT); - for (int index = 0; index < expectedArrayLength; index++) { - Assert.assertEquals(sortedArray[index], expectedArray[index]); - } - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithStructTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithStructTests.java deleted file mode 100644 index 4e0a9f8ce4f..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMHWithStructTests.java +++ /dev/null @@ -1,2886 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.CLinker; -import jdk.incubator.foreign.FunctionDescriptor; -import jdk.incubator.foreign.GroupLayout; -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.NativeSymbol; -import jdk.incubator.foreign.ResourceScope; -import jdk.incubator.foreign.SegmentAllocator; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.SymbolLookup; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for argument/return struct in upcall. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithStructTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - /* The padding of struct is not required on Linux/s390x and Windows/x64 */ - private static boolean isStructPaddingNotRequired = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")) - || osName.contains("linux") && arch.equals("s390x"); - private static CLinker clinker = CLinker.systemCLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAnd20BoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAnd20BoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAnd20BoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - structSegmt.set(JAVA_BOOLEAN, 5, true); - structSegmt.set(JAVA_BOOLEAN, 6, false); - structSegmt.set(JAVA_BOOLEAN, 7, true); - structSegmt.set(JAVA_BOOLEAN, 8, false); - structSegmt.set(JAVA_BOOLEAN, 9, true); - structSegmt.set(JAVA_BOOLEAN, 10, false); - structSegmt.set(JAVA_BOOLEAN, 11, true); - structSegmt.set(JAVA_BOOLEAN, 12, false); - structSegmt.set(JAVA_BOOLEAN, 13, true); - structSegmt.set(JAVA_BOOLEAN, 14, false); - structSegmt.set(JAVA_BOOLEAN, 15, true); - structSegmt.set(JAVA_BOOLEAN, 16, false); - structSegmt.set(JAVA_BOOLEAN, 17, true); - structSegmt.set(JAVA_BOOLEAN, 18, false); - structSegmt.set(JAVA_BOOLEAN, 19, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout), scope); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXorByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructPointerWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXorByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXorByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invoke((byte)6, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteAnd20BytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAnd20BytesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAnd20BytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)1); - structSegmt.set(JAVA_BYTE, 1, (byte)2); - structSegmt.set(JAVA_BYTE, 2, (byte)3); - structSegmt.set(JAVA_BYTE, 3, (byte)4); - structSegmt.set(JAVA_BYTE, 4, (byte)5); - structSegmt.set(JAVA_BYTE, 5, (byte)6); - structSegmt.set(JAVA_BYTE, 6, (byte)7); - structSegmt.set(JAVA_BYTE, 7, (byte)8); - structSegmt.set(JAVA_BYTE, 8, (byte)9); - structSegmt.set(JAVA_BYTE, 9, (byte)10); - structSegmt.set(JAVA_BYTE, 10, (byte)1); - structSegmt.set(JAVA_BYTE, 11, (byte)2); - structSegmt.set(JAVA_BYTE, 12, (byte)3); - structSegmt.set(JAVA_BYTE, 13, (byte)4); - structSegmt.set(JAVA_BYTE, 14, (byte)5); - structSegmt.set(JAVA_BYTE, 15, (byte)6); - structSegmt.set(JAVA_BYTE, 16, (byte)7); - structSegmt.set(JAVA_BYTE, 17, (byte)8); - structSegmt.set(JAVA_BYTE, 18, (byte)9); - structSegmt.set(JAVA_BYTE, 19, (byte)10); - - byte result = (byte)mh.invoke((byte)11, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 121); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct_returnBytePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 42); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - byte result = (byte)mh.invoke((byte)13, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)46, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invoke((byte)48, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArrayByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)14, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 80); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invoke((byte)18, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invoke((byte)16, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invoke((byte)22, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_add1ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add1ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add1ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - } - } - - @Test - public void test_add2ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invoke('C', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharAnd10CharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, - JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAnd10CharsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAnd10CharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'A'); - structSegmt.set(JAVA_CHAR, 4, 'B'); - structSegmt.set(JAVA_CHAR, 6, 'B'); - structSegmt.set(JAVA_CHAR, 8, 'C'); - structSegmt.set(JAVA_CHAR, 10, 'C'); - structSegmt.set(JAVA_CHAR, 12, 'D'); - structSegmt.set(JAVA_CHAR, 14, 'D'); - structSegmt.set(JAVA_CHAR, 16, 'E'); - structSegmt.set(JAVA_CHAR, 18, 'E'); - - char result = (char)mh.invoke('A', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'U'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct_returnCharPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructPointer, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArrayByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)888); - shortHandle2.set(structSegmt, (short)999); - - short result = (short)mh.invoke((short)777, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2664); - } - } - - @Test - public void test_addShortAnd10ShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, - JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAnd10ShortsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAnd10ShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)10); - structSegmt.set(JAVA_SHORT, 2, (short)20); - structSegmt.set(JAVA_SHORT, 4, (short)30); - structSegmt.set(JAVA_SHORT, 6, (short)40); - structSegmt.set(JAVA_SHORT, 8, (short)50); - structSegmt.set(JAVA_SHORT, 10, (short)60); - structSegmt.set(JAVA_SHORT, 12, (short)70); - structSegmt.set(JAVA_SHORT, 14, (short)80); - structSegmt.set(JAVA_SHORT, 16, (short)90); - structSegmt.set(JAVA_SHORT, 18, (short)100); - - short result = (short)mh.invoke((short)110, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 660); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - short result = (short)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 3349); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct_returnShortPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 3349); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructPointer, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)2222); - shortHandle2.set(structSegmt, (short)4444); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13332); - } - } - - @Test - public void test_addShortAndShortsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArrayByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_add2ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)734); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)712); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 734); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 712); - } - } - - @Test - public void test_add3ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)325); - shortHandle2.set(structSegmt1, (short)326); - shortHandle3.set(structSegmt1, (short)327); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)334); - shortHandle2.set(structSegmt2, (short)335); - shortHandle3.set(structSegmt2, (short)336); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)659); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)661); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)663); - } - } - - @Test - public void test_addIntAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invoke(2244668, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAnd5IntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), - JAVA_INT.withName("elem3"), JAVA_INT.withName("elem4"), JAVA_INT.withName("elem5")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle intHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle intHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAnd5IntsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAnd5IntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1111111); - intHandle2.set(structSegmt, 2222222); - intHandle3.set(structSegmt, 3333333); - intHandle4.set(structSegmt, 2222222); - intHandle5.set(structSegmt, 1111111); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 14444443); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct_returnIntPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArrayByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invoke(2468024680L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct_returnLongPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructPointer, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArrayByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invoke(6.56F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatAnd5FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), - JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4"), JAVA_FLOAT.withName("elem5")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle floatHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle floatHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAnd5FloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAnd5FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 1.01F); - floatHandle2.set(structSegmt, 1.02F); - floatHandle3.set(structSegmt, 1.03F); - floatHandle4.set(structSegmt, 1.04F); - floatHandle5.set(structSegmt, 1.05F); - - float result = (float)mh.invoke(1.06F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 6.21F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invoke(3336.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructPointer, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - NativeSymbol functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = clinker.downcallHandle(functionSymbol, fd); - - try (ResourceScope scope = ResourceScope.newConfinedScope()) { - NativeSymbol upcallFuncAddr = clinker.upcallStub(UpcallMethodHandles.MH_add3DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMethodHandles.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMethodHandles.java deleted file mode 100644 index 324cd15add3..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/upcall/UpcallMethodHandles.java +++ /dev/null @@ -1,1887 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.upcall; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.Addressable; -import static jdk.incubator.foreign.CLinker.*; -import jdk.incubator.foreign.GroupLayout; -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.SegmentAllocator; -import jdk.incubator.foreign.SequenceLayout; -import jdk.incubator.foreign.ValueLayout; -import static jdk.incubator.foreign.ValueLayout.*; -import jdk.incubator.foreign.VaList; - -/** - * The helper class that contains all upcall method handles with primitive types or struct - * as arguments. - */ -public class UpcallMethodHandles { - private static final Lookup lookup = MethodHandles.lookup(); - private static ResourceScope scope = ResourceScope.newImplicitScope(); - private static SegmentAllocator allocator = SegmentAllocator.newNativeArena(scope); - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - - static final MethodType MT_Bool_Bool_MemSegmt = methodType(boolean.class, boolean.class, MemorySegment.class); - static final MethodType MT_Addr_Bool_MemAddr = methodType(Addressable.class, boolean.class, MemoryAddress.class); - static final MethodType MT_Char_Char_MemSegmt = methodType(char.class, char.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Char = methodType(Addressable.class, MemoryAddress.class, char.class); - static final MethodType MT_Byte_Byte_MemSegmt = methodType(byte.class, byte.class, MemorySegment.class); - static final MethodType MT_Addr_Byte_MemAddr = methodType(Addressable.class, byte.class, MemoryAddress.class); - static final MethodType MT_Short_Short_MemSegmt = methodType(short.class, short.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Short = methodType(Addressable.class, MemoryAddress.class, short.class); - static final MethodType MT_Int_Int_MemSegmt = methodType(int.class, int.class, MemorySegment.class); - static final MethodType MT_Addr_Int_MemAddr = methodType(Addressable.class, int.class, MemoryAddress.class); - static final MethodType MT_Long_Long_MemSegmt = methodType(long.class, long.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Long = methodType(Addressable.class, MemoryAddress.class, long.class); - static final MethodType MT_Long_Int_MemSegmt = methodType(long.class, int.class, MemorySegment.class); - static final MethodType MT_Float_Float_MemSegmt = methodType(float.class, float.class, MemorySegment.class); - static final MethodType MT_Addr_Float_MemAddr = methodType(Addressable.class, float.class, MemoryAddress.class); - static final MethodType MT_Double_Double_MemSegmt = methodType(double.class, double.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Double = methodType(Addressable.class, MemoryAddress.class, double.class); - static final MethodType MT_Addr_MemAddr_MemSegmt = methodType(Addressable.class, MemoryAddress.class, MemorySegment.class); - static final MethodType MT_MemSegmt_MemSegmt_MemSegmt = methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - static final MethodType MT_MemSegmt = methodType(MemorySegment.class); - - public static final MethodHandle MH_add2BoolsWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPointerWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetPtr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetArgPtr; - public static final MethodHandle MH_createNewCharFrom2Chars; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPointer; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetPtr; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Bytes; - public static final MethodHandle MH_addByteAndByteFromPointer; - public static final MethodHandle MH_addByteAndByteFromPtr_RetPtr; - public static final MethodHandle MH_addByteAndByteFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Shorts; - public static final MethodHandle MH_addShortAndShortFromPointer; - public static final MethodHandle MH_addShortAndShortFromPtr_RetPtr; - public static final MethodHandle MH_addShortAndShortFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Ints; - public static final MethodHandle MH_addIntAndIntFromPointer; - public static final MethodHandle MH_addIntAndIntFromPtr_RetPtr; - public static final MethodHandle MH_addIntAndIntFromPtr_RetArgPtr; - public static final MethodHandle MH_add3Ints; - public static final MethodHandle MH_addIntAndChar; - public static final MethodHandle MH_add2IntsReturnVoid; - public static final MethodHandle MH_add2Longs; - public static final MethodHandle MH_addLongAndLongFromPointer; - public static final MethodHandle MH_addLongAndLongFromPtr_RetPtr; - public static final MethodHandle MH_addLongAndLongFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Floats; - public static final MethodHandle MH_addFloatAndFloatFromPointer; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetPtr; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Doubles; - public static final MethodHandle MH_addDoubleAndDoubleFromPointer; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetPtr; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetArgPtr; - public static final MethodHandle MH_compare; - - public static final MethodHandle MH_addBoolAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolAnd20BoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer; - public static final MethodHandle MH_addBoolAndBoolsFromStructPointerWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStruct; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStructPointer; - public static final MethodHandle MH_add3BoolStructsWithXor_returnStruct; - - public static final MethodHandle MH_addByteAndBytesFromStruct; - public static final MethodHandle MH_addByteAnd20BytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct_returnBytePointer; - public static final MethodHandle MH_addByteAndBytesFromStructPointer; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add1ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStructPointer; - public static final MethodHandle MH_add3ByteStructs_returnStruct; - - public static final MethodHandle MH_addCharAndCharsFromStruct; - public static final MethodHandle MH_addCharAnd10CharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct_returnCharPointer; - public static final MethodHandle MH_addCharAndCharsFromStructPointer; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2CharStructs_returnStruct; - public static final MethodHandle MH_add2CharStructs_returnStructPointer; - public static final MethodHandle MH_add3CharStructs_returnStruct; - - public static final MethodHandle MH_addShortAndShortsFromStruct; - public static final MethodHandle MH_addShortAnd10ShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct_returnShortPointer; - public static final MethodHandle MH_addShortAndShortsFromStructPointer; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2ShortStructs_returnStruct; - public static final MethodHandle MH_add2ShortStructs_returnStructPointer; - public static final MethodHandle MH_add3ShortStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntsFromStruct; - public static final MethodHandle MH_addIntAnd5IntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct_returnIntPointer; - public static final MethodHandle MH_addIntAndIntsFromStructPointer; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2IntStructs_returnStruct; - public static final MethodHandle MH_add2IntStructs_returnStructPointer; - public static final MethodHandle MH_add3IntStructs_returnStruct; - - public static final MethodHandle MH_addLongAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct_returnLongPointer; - public static final MethodHandle MH_addLongAndLongsFromStructPointer; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2LongStructs_returnStruct; - public static final MethodHandle MH_add2LongStructs_returnStructPointer; - public static final MethodHandle MH_add3LongStructs_returnStruct; - - public static final MethodHandle MH_addFloatAndFloatsFromStruct; - public static final MethodHandle MH_addFloatAnd5FloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer; - public static final MethodHandle MH_addFloatAndFloatsFromStructPointer; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2FloatStructs_returnStruct; - public static final MethodHandle MH_add2FloatStructs_returnStructPointer; - public static final MethodHandle MH_add3FloatStructs_returnStruct; - - public static final MethodHandle MH_addDoubleAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer; - public static final MethodHandle MH_addDoubleAndDoublesFromStructPointer; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2DoubleStructs_returnStruct; - public static final MethodHandle MH_add2DoubleStructs_returnStructPointer; - public static final MethodHandle MH_add3DoubleStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntShortFromStruct; - public static final MethodHandle MH_addIntAndShortIntFromStruct; - public static final MethodHandle MH_addIntAndIntLongFromStruct; - public static final MethodHandle MH_addIntAndLongIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAnd2FloatsDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDouble2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndInt2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndFloatIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndLongDoubleFromStruct; - public static final MethodHandle MH_addFloatAndInt3FloatsFromStruct; - public static final MethodHandle MH_addLongAndLong2FloatsFromStruct; - public static final MethodHandle MH_addFloatAnd3FloatsIntFromStruct; - public static final MethodHandle MH_addLongAndFloatLongFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatIntFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleLongFromStruct; - public static final MethodHandle MH_addLongAnd2FloatsLongFromStruct; - public static final MethodHandle MH_addShortAnd3ShortsCharFromStruct; - public static final MethodHandle MH_addFloatAndIntFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleLongFromStruct; - public static final MethodHandle MH_return254BytesFromStruct; - public static final MethodHandle MH_return4KBytesFromStruct; - - static { - try { - MH_add2BoolsWithOr = lookup.findStatic(UpcallMethodHandles.class, "add2BoolsWithOr", methodType(boolean.class, boolean.class, boolean.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPointerWithOr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPointerWithOr", methodType(boolean.class, boolean.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetPtr", MT_Addr_Bool_MemAddr); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetArgPtr", MT_Addr_Bool_MemAddr); //$NON-NLS-1$ - - MH_createNewCharFrom2Chars = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFrom2Chars", methodType(char.class, char.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPointer = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPointer", methodType(char.class, MemoryAddress.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetPtr", MT_Addr_MemAddr_Char); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetArgPtr", MT_Addr_MemAddr_Char); //$NON-NLS-1$ - - MH_add2Bytes = lookup.findStatic(UpcallMethodHandles.class, "add2Bytes", methodType(byte.class, byte.class, byte.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPointer", methodType(byte.class, byte.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetPtr", MT_Addr_Byte_MemAddr); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetArgPtr", MT_Addr_Byte_MemAddr); //$NON-NLS-1$ - - MH_add2Shorts = lookup.findStatic(UpcallMethodHandles.class, "add2Shorts", methodType(short.class, short.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPointer", methodType(short.class, MemoryAddress.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetPtr", MT_Addr_MemAddr_Short); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetArgPtr", MT_Addr_MemAddr_Short); //$NON-NLS-1$ - - MH_add2Ints = lookup.findStatic(UpcallMethodHandles.class, "add2Ints", methodType(int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPointer", methodType(int.class, int.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetPtr", MT_Addr_Int_MemAddr); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetArgPtr", MT_Addr_Int_MemAddr); //$NON-NLS-1$ - MH_add3Ints = lookup.findStatic(UpcallMethodHandles.class, "add3Ints", methodType(int.class, int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndChar = lookup.findStatic(UpcallMethodHandles.class, "addIntAndChar", methodType(int.class, int.class, char.class)); //$NON-NLS-1$ - MH_add2IntsReturnVoid = lookup.findStatic(UpcallMethodHandles.class, "add2IntsReturnVoid", methodType(void.class, int.class, int.class)); //$NON-NLS-1$ - - MH_add2Longs = lookup.findStatic(UpcallMethodHandles.class, "add2Longs", methodType(long.class, long.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPointer", methodType(long.class, MemoryAddress.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetPtr", MT_Addr_MemAddr_Long); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetArgPtr", MT_Addr_MemAddr_Long); //$NON-NLS-1$ - - MH_add2Floats = lookup.findStatic(UpcallMethodHandles.class, "add2Floats", methodType(float.class, float.class, float.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPointer", methodType(float.class, float.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetPtr", MT_Addr_Float_MemAddr); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetArgPtr", MT_Addr_Float_MemAddr); //$NON-NLS-1$ - - MH_add2Doubles = lookup.findStatic(UpcallMethodHandles.class, "add2Doubles", methodType(double.class, double.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPointer", methodType(double.class, MemoryAddress.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetPtr", MT_Addr_MemAddr_Double); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetArgPtr", MT_Addr_MemAddr_Double); //$NON-NLS-1$ - - MH_compare = lookup.findStatic(UpcallMethodHandles.class, "compare", methodType(int.class, MemoryAddress.class, MemoryAddress.class)); //$NON-NLS-1$ - - MH_addBoolAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAnd20BoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAnd20BoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor", methodType(boolean.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructPointerWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructPointerWithXor", methodType(boolean.class, boolean.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addByteAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAnd20BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAnd20BytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct", methodType(byte.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct_returnBytePointer = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct_returnBytePointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructPointer", methodType(byte.class, byte.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_add1ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add1ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addCharAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAnd10CharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAnd10CharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct", methodType(char.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct_returnCharPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct_returnCharPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructPointer", methodType(char.class, char.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addShortAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd10ShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd10ShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct", methodType(short.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct_returnShortPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct_returnShortPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructPointer", methodType(short.class, short.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAnd5IntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAnd5IntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct", methodType(int.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct_returnIntPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct_returnIntPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructPointer", methodType(int.class, int.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addLongAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct", methodType(long.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct_returnLongPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct_returnLongPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructPointer", methodType(long.class, long.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addFloatAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd5FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd5FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct", methodType(float.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct_returnFloatPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructPointer", methodType(float.class, float.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addDoubleAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct", methodType(double.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructPointer", methodType(double.class, double.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntShortFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntShortFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndShortIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortIntFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntLongFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndLongIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndLongIntFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAnd2FloatsDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAnd2FloatsDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDouble2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDouble2FloatsFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt2FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndLongDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndLongDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt3FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt3FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLong2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLong2FloatsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd3FloatsIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd3FloatsIntFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndFloatLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndFloatLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addLongAnd2FloatsLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAnd2FloatsLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd3ShortsCharFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd3ShortsCharFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndIntFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndIntFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_return254BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return254BytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - MH_return4KBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return4KBytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - - } catch (IllegalAccessException | NoSuchMethodException e) { - throw new InternalError(e); - } - } - - public static boolean add2BoolsWithOr(boolean boolArg1, boolean boolArg2) { - boolean result = boolArg1 || boolArg2; - return result; - } - - public static boolean addBoolAndBoolFromPointerWithOr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - return result; - } - - public static Addressable addBoolAndBoolFromPtrWithOr_RetPtr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - resultSegmt.set(JAVA_BOOLEAN, 0, result); - return resultSegmt.address(); - } - - public static Addressable addBoolAndBoolFromPtrWithOr_RetArgPtr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - boolArg2Addr.set(JAVA_BOOLEAN, 0, result); - return boolArg2Addr; - } - - public static char createNewCharFrom2Chars(char charArg1, char charArg2) { - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static char createNewCharFromCharAndCharFromPointer(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static Addressable createNewCharFromCharAndCharFromPtr_RetPtr(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_CHAR.byteSize(), scope); - resultSegmt.set(JAVA_CHAR, 0, result); - return resultSegmt.address(); - } - - public static Addressable createNewCharFromCharAndCharFromPtr_RetArgPtr(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - charArg1Addr.set(JAVA_CHAR, 0, result); - return charArg1Addr; - } - - public static byte add2Bytes(byte byteArg1, byte byteArg2) { - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static byte addByteAndByteFromPointer(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static Addressable addByteAndByteFromPtr_RetPtr(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BYTE.byteSize(), scope); - resultSegmt.set(JAVA_BYTE, 0, byteSum); - return resultSegmt.address(); - } - - public static Addressable addByteAndByteFromPtr_RetArgPtr(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - byteArg2Addr.set(JAVA_BYTE, 0, byteSum); - return byteArg2Addr; - } - - public static short add2Shorts(short shortArg1, short shortArg2) { - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static short addShortAndShortFromPointer(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static Addressable addShortAndShortFromPtr_RetPtr(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_SHORT.byteSize(), scope); - resultSegmt.set(JAVA_SHORT, 0, shortSum); - return resultSegmt.address(); - } - - public static Addressable addShortAndShortFromPtr_RetArgPtr(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - shortArg1Addr.set(JAVA_SHORT, 0, shortSum); - return shortArg1Addr; - } - - public static int add2Ints(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - return intSum; - } - - public static int addIntAndIntFromPointer(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - return intSum; - } - - public static Addressable addIntAndIntFromPtr_RetPtr(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_INT.byteSize(), scope); - resultSegmt.set(JAVA_INT, 0, intSum); - return resultSegmt.address(); - } - - public static Addressable addIntAndIntFromPtr_RetArgPtr(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - intArg2Addr.set(JAVA_INT, 0, intSum); - return intArg2Addr; - } - - public static int add3Ints(int intArg1, int intArg2, int intArg3) { - int intSum = intArg1 + intArg2 + intArg3; - return intSum; - } - - public static int addIntAndChar(int intArg, char charArg) { - int sum = intArg + charArg; - return sum; - } - - public static void add2IntsReturnVoid(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - System.out.println("add2IntsReturnVoid: intSum = " + intSum + "\n"); - } - - public static long add2Longs(long longArg1, long longArg2) { - long longSum = longArg1 + longArg2; - return longSum; - } - - public static long addLongAndLongFromPointer(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - return longSum; - } - - public static Addressable addLongAndLongFromPtr_RetPtr(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_LONG.byteSize(), scope); - resultSegmt.set(JAVA_LONG, 0, longSum); - return resultSegmt.address(); - } - - public static Addressable addLongAndLongFromPtr_RetArgPtr(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - longArg1Addr.set(JAVA_LONG, 0, longSum); - return longArg1Addr; - } - - public static float add2Floats(float floatArg1, float floatArg2) { - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static float addFloatAndFloatFromPointer(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static Addressable addFloatAndFloatFromPtr_RetPtr(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_FLOAT.byteSize(), scope); - resultSegmt.set(JAVA_FLOAT, 0, floatSum); - return resultSegmt.address(); - } - - public static Addressable addFloatAndFloatFromPtr_RetArgPtr(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - floatArg2Addr.set(JAVA_FLOAT, 0, floatSum); - return floatArg2Addr; - } - - public static double add2Doubles(double doubleArg1, double doubleArg2) { - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static double addDoubleAndDoubleFromPointer(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static Addressable addDoubleAndDoubleFromPtr_RetPtr(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_DOUBLE.byteSize(), scope); - resultSegmt.set(JAVA_DOUBLE, 0, doubleSum); - return resultSegmt.address(); - } - - public static Addressable addDoubleAndDoubleFromPtr_RetArgPtr(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - doubleArg1Addr.set(JAVA_DOUBLE, 0, doubleSum); - return doubleArg1Addr; - } - - public static int compare(MemoryAddress argAddr1, MemoryAddress argAddr2) { - int intArg1 = argAddr1.get(JAVA_INT, 0); - int intArg2 = argAddr2.get(JAVA_INT, 0); - return (intArg1 - intArg2); - } - - public static boolean addBoolAndBoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAnd20BoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1) - ^ arg2.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 3) ^ arg2.get(JAVA_BOOLEAN, 4) - ^ arg2.get(JAVA_BOOLEAN, 5) ^ arg2.get(JAVA_BOOLEAN, 6) ^ arg2.get(JAVA_BOOLEAN, 7) - ^ arg2.get(JAVA_BOOLEAN, 8) ^ arg2.get(JAVA_BOOLEAN, 9) ^ arg2.get(JAVA_BOOLEAN, 10) - ^ arg2.get(JAVA_BOOLEAN, 11) ^ arg2.get(JAVA_BOOLEAN, 12) ^ arg2.get(JAVA_BOOLEAN, 13) - ^ arg2.get(JAVA_BOOLEAN, 14) ^ arg2.get(JAVA_BOOLEAN, 15) ^ arg2.get(JAVA_BOOLEAN, 16) - ^ arg2.get(JAVA_BOOLEAN, 17) ^ arg2.get(JAVA_BOOLEAN, 18) ^ arg2.get(JAVA_BOOLEAN, 19); - return boolSum; - } - - public static boolean addBoolFromPointerAndBoolsFromStructWithXor(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolSum = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static Addressable addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolSum = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Addr.set(JAVA_BOOLEAN, 0, boolSum); - return arg1Addr; - } - - public static boolean addBoolAndBoolsFromStructPointerWithXor(boolean arg1, MemoryAddress arg2Addr) { - boolean boolSum = arg1 ^ arg2Addr.get(JAVA_BOOLEAN, 0) ^ arg2Addr.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor(boolean arg1, MemorySegment arg2) { - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ nestedStructElem1 ^ nestedStructElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ structElem1 ^ nestedStructElem1 ^ nestedStructElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray(boolean arg1, MemorySegment arg2) { - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ structElem1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray(boolean arg1, MemorySegment arg2) { - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 3); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem2 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 3); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem1 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static MemorySegment add2BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, scope); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return boolStructSegmt; - } - - public static Addressable add2BoolStructsWithXor_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolStruct_Elem1 = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1Addr.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Addr.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - arg1Addr.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, scope); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolean boolStruct_Elem3 = arg1.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 2); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - boolStructSegmt.set(JAVA_BOOLEAN, 2, boolStruct_Elem3); - return boolStructSegmt; - } - - public static byte addByteAndBytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAnd20BytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1) - + arg2.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 3) + arg2.get(JAVA_BYTE, 4) - + arg2.get(JAVA_BYTE, 5) + arg2.get(JAVA_BYTE, 6) + arg2.get(JAVA_BYTE, 7) - + arg2.get(JAVA_BYTE, 8) + arg2.get(JAVA_BYTE, 9) + arg2.get(JAVA_BYTE, 10) - + arg2.get(JAVA_BYTE, 11) + arg2.get(JAVA_BYTE, 12) + arg2.get(JAVA_BYTE, 13) - + arg2.get(JAVA_BYTE, 14) + arg2.get(JAVA_BYTE, 15) + arg2.get(JAVA_BYTE, 16) - + arg2.get(JAVA_BYTE, 17) + arg2.get(JAVA_BYTE, 18) + arg2.get(JAVA_BYTE, 19)); - return byteSum; - } - - public static byte addByteFromPointerAndBytesFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteSum = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static Addressable addByteFromPointerAndBytesFromStruct_returnBytePointer(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteSum = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - arg1Addr.set(JAVA_BYTE, 0, byteSum); - return arg1Addr; - } - - public static byte addByteAndBytesFromStructPointer(byte arg1, MemoryAddress arg2Addr) { - byte byteSum = (byte)(arg1 + arg2Addr.get(JAVA_BYTE, 0) + arg2Addr.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct(byte arg1, MemorySegment arg2) { - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray(byte arg1, MemorySegment arg2) { - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedByteArrayElem1 + nestedByteArrayElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedByteArrayElem1 + nestedByteArrayElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray(byte arg1, MemorySegment arg2) { - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 3); - byte structElem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 3); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static MemorySegment add1ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - return byteStructSegmt; - } - - public static MemorySegment add2ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - return byteStructSegmt; - } - - public static Addressable add2ByteStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteStruct_Elem1 = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1Addr.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - arg1Addr.set(JAVA_BYTE, 0, byteStruct_Elem1); - arg1Addr.set(JAVA_BYTE, 1, byteStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byte byteStruct_Elem3 = (byte)(arg1.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 2)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - byteStructSegmt.set(JAVA_BYTE, 2, byteStruct_Elem3); - return byteStructSegmt; - } - - public static char addCharAndCharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAnd10CharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - + arg2.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 6) + arg2.get(JAVA_CHAR, 8) - + arg2.get(JAVA_CHAR, 10) + arg2.get(JAVA_CHAR, 12) + arg2.get(JAVA_CHAR, 14) - + arg2.get(JAVA_CHAR, 16) + arg2.get(JAVA_CHAR, 18) - 10 * 'A'); - return result; - } - - public static char addCharFromPointerAndCharsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - char result = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static Addressable addCharFromPointerAndCharsFromStruct_returnCharPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - char result = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - arg1Addr.set(JAVA_CHAR, 0, result); - return arg1Addr; - } - - public static char addCharAndCharsFromStructPointer(char arg1, MemoryAddress arg2Addr) { - char result = (char)(arg1 + arg2Addr.get(JAVA_CHAR, 0) + arg2Addr.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct(char arg1, MemorySegment arg2) { - char nestedStructElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray(char arg1, MemorySegment arg2) { - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedCharArrayElem1 + nestedCharArrayElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 2); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedCharArrayElem1 + nestedCharArrayElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray(char arg1, MemorySegment arg2) { - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 6); - char structElem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 6); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static MemorySegment add2CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, scope); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - return charStructSegmt; - } - - public static Addressable add2CharStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - char charStruct_Elem1 = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1Addr.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - arg1Addr.set(JAVA_CHAR, 0, charStruct_Elem1); - arg1Addr.set(JAVA_CHAR, 2, charStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, scope); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - char charStruct_Elem3 = (char)(arg1.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 4) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - charStructSegmt.set(JAVA_CHAR, 4, charStruct_Elem3); - return charStructSegmt; - } - - public static short addShortAndShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAnd10ShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 6) + arg2.get(JAVA_SHORT, 8) - + arg2.get(JAVA_SHORT, 10) + arg2.get(JAVA_SHORT, 12) + arg2.get(JAVA_SHORT, 14) - + arg2.get(JAVA_SHORT, 16) + arg2.get(JAVA_SHORT, 18)); - return shortSum; - } - - public static short addShortFromPointerAndShortsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortSum = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static Addressable addShortFromPointerAndShortsFromStruct_returnShortPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortSum = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - arg1Addr.set(JAVA_SHORT, 0, shortSum); - return arg1Addr; - } - - public static short addShortAndShortsFromStructPointer(short arg1, MemoryAddress arg2Addr) { - short shortSum = (short)(arg1 + arg2Addr.get(JAVA_SHORT, 0) + arg2Addr.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct(short arg1, MemorySegment arg2) { - short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray(short arg1, MemorySegment arg2) { - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedShortArrayElem1 + nestedShortArrayElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 2); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedShortArrayElem1 + nestedShortArrayElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray(short arg1, MemorySegment arg2) { - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 6); - short structElem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 6); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static MemorySegment add2ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, scope); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - return shortStructSegmt; - } - - public static Addressable add2ShortStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortStruct_Elem1 = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1Addr.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - arg1Addr.set(JAVA_SHORT, 0, shortStruct_Elem1); - arg1Addr.set(JAVA_SHORT, 2, shortStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, scope); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - short shortStruct_Elem3 = (short)(arg1.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 4)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - shortStructSegmt.set(JAVA_SHORT, 4, shortStruct_Elem3); - return shortStructSegmt; - } - - public static int addIntAndIntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAnd5IntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4) - + arg2.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 12) + arg2.get(JAVA_INT, 16); - return intSum; - } - - public static int addIntFromPointerAndIntsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static Addressable addIntFromPointerAndIntsFromStruct_returnIntPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - arg1Addr.set(JAVA_INT, 0, intSum); - return arg1Addr; - } - - public static int addIntAndIntsFromStructPointer(int arg1, MemoryAddress arg2Addr) { - int intSum = arg1 + arg2Addr.get(JAVA_INT, 0) + arg2Addr.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAndIntsFromNestedStruct(int arg1, MemorySegment arg2) { - int nestedStructElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromNestedStruct_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem1 = arg2.get(JAVA_INT, 4); - int nestedStructElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray(int arg1, MemorySegment arg2) { - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedIntArrayElem1 + nestedIntArrayElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 4); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedIntArrayElem1 + nestedIntArrayElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray(int arg1, MemorySegment arg2) { - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 12); - int structElem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 12); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static MemorySegment add2IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, scope); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - return intStructSegmt; - } - - public static Addressable add2IntStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum_Elem1 = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intSum_Elem2 = arg1Addr.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - arg1Addr.set(JAVA_INT, 0, intSum_Elem1); - arg1Addr.set(JAVA_INT, 4, intSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, scope); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - int intStruct_Elem3 = arg1.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 8); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - intStructSegmt.set(JAVA_INT, 8, intStruct_Elem3); - return intStructSegmt; - } - - public static long addLongAndLongsFromStruct(long arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongFromPointerAndLongsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static Addressable addLongFromPointerAndLongsFromStruct_returnLongPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - arg1Addr.set(JAVA_LONG, 0, longSum); - return arg1Addr; - } - - public static long addLongAndLongsFromStructPointer(long arg1, MemoryAddress arg2Addr) { - long longSum = arg1 + arg2Addr.get(JAVA_LONG, 0) + arg2Addr.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongAndLongsFromNestedStruct(long arg1, MemorySegment arg2) { - long nestedStructElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromNestedStruct_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem1 = arg2.get(JAVA_LONG, 8); - long nestedStructElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray(long arg1, MemorySegment arg2) { - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedLongrrayElem1 + nestedLongrrayElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 8); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedLongrrayElem1 + nestedLongrrayElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray(long arg1, MemorySegment arg2) { - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 24); - long structElem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 24); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static MemorySegment add2LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, scope); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - return longStructSegmt; - } - - public static Addressable add2LongStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum_Elem1 = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longSum_Elem2 = arg1Addr.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - arg1Addr.set(JAVA_LONG, 0, longSum_Elem1); - arg1Addr.set(JAVA_LONG, 8, longSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, scope); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - long longStruct_Elem3 = arg1.get(JAVA_LONG, 16) + arg2.get(JAVA_LONG, 16); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - longStructSegmt.set(JAVA_LONG, 16, longStruct_Elem3); - return longStructSegmt; - } - - public static float addFloatAndFloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAnd5FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12) + arg2.get(JAVA_FLOAT, 16); - return floatSum; - } - - public static float addFloatFromPointerAndFloatsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static Addressable addFloatFromPointerAndFloatsFromStruct_returnFloatPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - arg1Addr.set(JAVA_FLOAT, 0, floatSum); - return arg1Addr; - } - - public static float addFloatAndFloatsFromStructPointer(float arg1, MemoryAddress arg2Addr) { - float floatSum = arg1 + arg2Addr.get(JAVA_FLOAT, 0) + arg2Addr.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct(float arg1, MemorySegment arg2) { - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray(float arg1, MemorySegment arg2) { - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedFloatArrayElem1 + nestedFloatArrayElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedFloatArrayElem1 + nestedFloatArrayElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray(float arg1, MemorySegment arg2) { - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 12); - float structElem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 12); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static MemorySegment add2FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, scope); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - return floatStructSegmt; - } - - public static Addressable add2FloatStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum_Elem1 = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatSum_Elem2 = arg1Addr.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - arg1Addr.set(JAVA_FLOAT, 0, floatSum_Elem1); - arg1Addr.set(JAVA_FLOAT, 4, floatSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, scope); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - float floatStruct_Elem3 = arg1.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 8); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - floatStructSegmt.set(JAVA_FLOAT, 8, floatStruct_Elem3); - return floatStructSegmt; - } - - public static double addDoubleAndDoublesFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleFromPointerAndDoublesFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static Addressable addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - arg1Addr.set(JAVA_DOUBLE, 0, doubleSum); - return arg1Addr; - } - - public static double addDoubleAndDoublesFromStructPointer(double arg1, MemoryAddress arg2Addr) { - double doubleSum = arg1 + arg2Addr.get(JAVA_DOUBLE, 0) + arg2Addr.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct(double arg1, MemorySegment arg2) { - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray(double arg1, MemorySegment arg2) { - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray(double arg1, MemorySegment arg2) { - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 24); - double structElem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 24); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static MemorySegment add2DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, scope); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - return doubleStructSegmt; - } - - public static Addressable add2DoubleStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum_Elem1 = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleSum_Elem2 = arg1Addr.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - arg1Addr.set(JAVA_DOUBLE, 0, doubleSum_Elem1); - arg1Addr.set(JAVA_DOUBLE, 8, doubleSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, scope); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - double doubleStruct_Elem3 = arg1.get(JAVA_DOUBLE, 16) + arg2.get(JAVA_DOUBLE, 16); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - doubleStructSegmt.set(JAVA_DOUBLE, 16, doubleStruct_Elem3); - return doubleStructSegmt; - } - - public static int addIntAndIntShortFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_SHORT, 4); - return intSum; - } - - public static int addIntAndShortIntFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static long addIntAndIntLongFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addIntAndLongIntFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_INT, 8); - return longSum; - } - - public static double addDoubleAndIntDoubleFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_INT, 8); - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8); - return doubleSum; - } - - public static double addDoubleAnd2FloatsDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDouble2FloatsFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return doubleSum; - } - - public static float addFloatAndInt2FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static float addFloatAndFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float structElem2 = Integer.valueOf(arg2.get(JAVA_INT, 4)).floatValue(); - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + structElem2 + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static double addDoubleAndIntFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndFloatIntDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_INT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndLongDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static float addFloatAndInt3FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static long addLongAndLong2FloatsFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 8)).longValue(); - long structElem3 = Float.valueOf(arg2.get(JAVA_FLOAT, 12)).longValue(); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static float addFloatAnd3FloatsIntFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return floatSum; - } - - public static long addLongAndFloatLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2; - return longSum; - } - - public static double addDoubleAndDoubleFloatIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return doubleSum; - } - - public static double addDoubleAndDoubleLongFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_LONG, 8); - return doubleSum; - } - - public static long addLongAnd2FloatsLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 4)).longValue(); - long structElem3 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static short addShortAnd3ShortsCharFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_CHAR, 6)); - return shortSum; - } - - public static float addFloatAndIntFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4)+ arg2.get(JAVA_INT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static double addDoubleAndIntDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleIntFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleIntFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleLongFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, long] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 24 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double structElem3 = arg2.get(JAVA_LONG, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static MemorySegment return254BytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, scope); - - for (int i = 0; i < 254; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } - - public static MemorySegment return4KBytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, scope); - - for (int i = 0; i < 4096; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } -} diff --git a/test/functional/Java18andUp/src/org/openj9/test/jep419/valist/ApiTests.java b/test/functional/Java18andUp/src/org/openj9/test/jep419/valist/ApiTests.java deleted file mode 100644 index f73acaa9cb9..00000000000 --- a/test/functional/Java18andUp/src/org/openj9/test/jep419/valist/ApiTests.java +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep419.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.VarHandle; - -import jdk.incubator.foreign.GroupLayout; -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.SegmentAllocator; -import jdk.incubator.foreign.VaList; -import static jdk.incubator.foreign.ValueLayout.*; -import static jdk.incubator.foreign.VaList.Builder; - -/** - * Test cases for JEP 419: Foreign Linker API (Second Incubator) for the vararg list in VaList API specific cases. - */ -@Test(groups = { "level.sanity" }) -public class ApiTests { -} diff --git a/test/functional/Java18andUp/testng_180.xml b/test/functional/Java18andUp/testng_180.xml deleted file mode 100644 index 5c4a2a09efb..00000000000 --- a/test/functional/Java18andUp/testng_180.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java19andUp/build.xml b/test/functional/Java19andUp/build.xml deleted file mode 100644 index 6a1b6e52b88..00000000000 --- a/test/functional/Java19andUp/build.xml +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - Tests for Java 19 and up - - - - - - - - - - - - - - - - - - - Ant version is ${ant.version} - ============COMPILER SETTINGS============ - ===fork: yes - ===executable: ${compiler.javac} - ===debug: on - ===destdir: ${DEST} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java19andUp/playlist.xml b/test/functional/Java19andUp/playlist.xml deleted file mode 100644 index e7f082eb051..00000000000 --- a/test/functional/Java19andUp/playlist.xml +++ /dev/null @@ -1,145 +0,0 @@ - - - - - Jep425Tests_testVirtualThread - - --enable-preview -Xgcpolicy:optthruput - --enable-preview -Xgcpolicy:gencon - --enable-preview -Xgcpolicy:balanced - --enable-preview -Xgcpolicy:optavgpause - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-opens java.base/java.lang=ALL-UNNAMED \ - -cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_190.xml$(Q) \ - -testnames Jep425Tests_testVirtualThread \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^os.zos,^os.sunos - - sanity - - - functional - - - native - - - openj9 - - - 19 - - - - - Jep424Tests_testLinkerFfi_DownCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_190.xml$(Q) -testnames Jep424Tests_testLinkerFfi_DownCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 19 - - - - - Jep424Tests_testLinkerFfi_UpCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_190.xml$(Q) -testnames Jep424Tests_testLinkerFfi_UpCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 19 - - - - - Jep424Tests_testLinkerFfi_VaList - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_190.xml$(Q) -testnames Jep424Tests_testLinkerFfi_VaList \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.aarch64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 19 - - - diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/InvalidDownCallTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/InvalidDownCallTests.java deleted file mode 100644 index e1d81274bb2..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/InvalidDownCallTests.java +++ /dev/null @@ -1,188 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; -import java.lang.invoke.WrongMethodTypeException; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, - * which verifies the illegal cases including unsupported layouts, etc. - * Note: the majority of illegal cases are removed given the corresponding method type - * is deduced from the function descriptor which is verified in OpenJDK. - */ -@Test(groups = { "level.sanity" }) -public class InvalidDownCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForIntType() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, MemoryLayout.paddingLayout(32)); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForMemoryAddress() throws Throwable { - Addressable functionSymbol = defaultLibLookup.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, MemoryLayout.paddingLayout(64)); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForReturnType() throws Throwable { - Addressable functionSymbol = defaultLibLookup.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(MemoryLayout.paddingLayout(64), JAVA_LONG); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - int result = (int)mh.invoke(19202122, null); - fail("Failed to throw out NullPointerException in the case of the null value"); - } - - @Test(expectedExceptions = WrongMethodTypeException.class) - public void test_nullValueForStructArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, null); - fail("Failed to throw out WrongMethodTypeException in the case of the null value"); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - int result = (int)mh.invoke(19202122, MemoryAddress.NULL); - fail("Failed to throw out NullPointerException in the case of the null address"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Heap segment not allowed.*") - public void test_heapSegmentForPtrArgument_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt = MemorySegment.ofArray(new int[]{11121314, 15161718}); - int result = (int)mh.invoke(19202122, structSegmt); - fail("Failed to throw out IllegalArgumentException in the case of the heap segment"); - } - - /* An UnsupportedOperationException is thrown out by MemorySegment.address() in the case of - * the on-heap segment in JDK19. - */ - @Test(expectedExceptions = UnsupportedOperationException.class, expectedExceptionsMessageRegExp = "Cannot obtain address of on-heap segment.*") - public void test_heapSegmentForPtrArgument_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt = MemorySegment.ofArray(new int[]{11121314, 15161718}); - int result = (int)mh.invokeExact(19202122, structSegmt.address()); - fail("Failed to throw out UnsupportedOperationException in the case of the heap address"); - } - - public void test_heapSegmentForStructArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = MemorySegment.ofArray(new int[]{99001122, 33445566}); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiCallTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiCallTests.java deleted file mode 100644 index 437c0b89c6d..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiCallTests.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, - * which verifies multiple downcalls with the same or different layouts or argument/return types. - */ -@Test(groups = { "level.sanity" }) -public class MultiCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_twoCallsWithSameFuncDescriptor() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - mh = linker.downcallHandle(functionSymbol, fd); - result = (int)mh.invokeExact(235, 439); - Assert.assertEquals(result, 674); - } - - @Test - public void test_twoCallsWithDiffFuncDescriptor() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol2 = nativeLibLookup.lookup("add3Ints").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } - - @Test - public void test_multiCallsWithMixedFuncDescriptors() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol2 = nativeLibLookup.lookup("add3Ints").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - - FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol3 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(454, 398); - - mh = linker.downcallHandle(functionSymbol1, fd1); - result = (int)mh.invokeExact(234, 567); - Assert.assertEquals(result, 801); - - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(312, 323, 334); - Assert.assertEquals(result, 969); - - mh = linker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(539, 672); - } - - @Test - public void test_twoCallsWithDiffReturnType() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } - - @Test - public void test_multiCallsWithSameArgLayouts() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd); - int intResult = (int)mh.invokeExact(112, 123); - Assert.assertEquals(intResult, 235); - - mh = linker.downcallHandle(functionSymbol1, fd); - intResult = (int)mh.invokeExact(234, 567); - Assert.assertEquals(intResult, 801); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests1.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests1.java deleted file mode 100644 index ba28f0ece31..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests1.java +++ /dev/null @@ -1,112 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.SegmentAllocator; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, which - * verifies the downcalls with the same downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SameDowncallHandler() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(322, intSegmt); - Assert.assertEquals(result, 537); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests2.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests2.java deleted file mode 100644 index c29f44bc6a4..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests2.java +++ /dev/null @@ -1,131 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, which - * verifies the downcalls with the shared downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - - static { - System.loadLibrary("clinkerffitests"); - } - private static final GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - private static final FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - private static final Addressable functionSymbol = SymbolLookup.loaderLookup().lookup("add2IntStructs_returnStruct").get(); - private static final MethodHandle mh = Linker.nativeLinker().downcallHandle(functionSymbol, fd); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SharedDowncallHandler() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001123); - intHandle2.set(structSegmt2, 33445567); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224467); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113355); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests3.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests3.java deleted file mode 100644 index 5d9219a1e10..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests3.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, - * which verifies the downcalls with the diffrent layouts and arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests3 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests4.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests4.java deleted file mode 100644 index f10d77b5777..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests4.java +++ /dev/null @@ -1,97 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, - * which verifies the downcalls with the diffrent return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests4 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffReturnType() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests5.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests5.java deleted file mode 100644 index c2daba8b257..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/MultiThreadingTests5.java +++ /dev/null @@ -1,166 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall, - * which verifies multiple downcalls combined with the diffrent layouts/arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests5 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiThreadsWithMixedFuncDescriptors() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(128, 246); - Assert.assertEquals(result, 374); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 642, 971); - Assert.assertEquals(result, 1725); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr4 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(416, 728); - Assert.assertEquals(result, 1144); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr5 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(1012, 1023, 2035); - Assert.assertEquals(result, 4070); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr6 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(result, false); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - thr3.setUncaughtExceptionHandler(this); - thr4.setUncaughtExceptionHandler(this); - thr5.setUncaughtExceptionHandler(this); - thr6.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - thr3.start(); - thr4.start(); - thr5.start(); - thr6.start(); - - thr6.join(); - thr5.join(); - thr4.join(); - thr3.join(); - thr2.join(); - thr1.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests1.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests1.java deleted file mode 100644 index 9e96aa8fd40..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests1.java +++ /dev/null @@ -1,296 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(Addressable symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests1 { - private static Linker linker = Linker.nativeLinker(); - private static MemorySession session = MemorySession.openImplicit(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.newNativeArena(session); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - char result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke((byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(-112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } - - @Test - public void test_addIntAndChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndChar").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - long result = (long)mh.invokeExact(57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - float result = (float)mh.invokeExact(5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Doubles").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - double result = (double)mh.invokeExact(159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_1() throws Throwable { - Addressable strlenSymbol = defaultLibLookup.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = linker.downcallHandle(strlenSymbol, fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP424 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable { - Addressable allocSymbol = defaultLibLookup.lookup("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = linker.downcallHandle(allocSymbol, allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(10L); - allocMemAddr.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15); - - Addressable freeSymbol = defaultLibLookup.lookup("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = linker.downcallHandle(freeSymbol, freeFuncDesc); - freeHandle.invoke(allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_1() throws Throwable { - Addressable functionSymbol = defaultLibLookup.lookup("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests2.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests2.java deleted file mode 100644 index bfdaded261f..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/PrimitiveTypeTests2.java +++ /dev/null @@ -1,298 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests2 { - private static Linker linker = Linker.nativeLinker(); - private static MemorySession session = MemorySession.openImplicit(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.newNativeArena(session); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(fd); - boolean result = (boolean)mh.invokeExact(functionSymbol, true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(functionSymbol, false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get(); - MethodHandle mh = linker.downcallHandle(fd); - char result = (char)mh.invokeExact(functionSymbol, 'B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(functionSymbol, charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke(functionSymbol, (byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(functionSymbol, shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, -112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(functionSymbol, 321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(fd); - mh.invokeExact(functionSymbol, 454, 398); - } - - @Test - public void test_addIntAndChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndChar").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get(); - MethodHandle mh = linker.downcallHandle(fd); - long result = (long)mh.invokeExact(functionSymbol, 57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(functionSymbol, longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get(); - MethodHandle mh = linker.downcallHandle(fd); - float result = (float)mh.invokeExact(functionSymbol, 5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(functionSymbol, 5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - Addressable functionSymbol = nativeLibLookup.lookup("add2Doubles").get(); - MethodHandle mh = linker.downcallHandle(fd); - double result = (double)mh.invokeExact(functionSymbol, 159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(functionSymbol, doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_2() throws Throwable { - Addressable strlenSymbol = defaultLibLookup.lookup("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = linker.downcallHandle(fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP424 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(strlenSymbol, funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_2() throws Throwable { - Addressable allocSymbol = defaultLibLookup.lookup("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = linker.downcallHandle(allocFuncDesc); - MemoryAddress allocMemAddr = (MemoryAddress)allocHandle.invokeExact(allocSymbol, 10L); - allocMemAddr.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15); - - Addressable freeSymbol = defaultLibLookup.lookup("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = linker.downcallHandle(freeFuncDesc); - freeHandle.invoke(freeSymbol, allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_2() throws Throwable { - Addressable functionSymbol = defaultLibLookup.lookup("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(functionSymbol, formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests1.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests1.java deleted file mode 100644 index 8e71d68bbf9..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests1.java +++ /dev/null @@ -1,3103 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests1 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(boolSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact((byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(byteSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke((byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact((byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact((byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact((byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact('C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - - short result = (short)mh.invokeExact((short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke((short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get(); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(intSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invokeExact(2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invokeExact(6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(floatSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invokeExact(3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests2.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests2.java deleted file mode 100644 index d494cb7ac5b..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/downcall/StructTests2.java +++ /dev/null @@ -1,3100 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests2 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(functionSymbol, boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, boolSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(functionSymbol, byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, byteSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke(functionSymbol, (byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStruct").get(); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact(functionSymbol, 'C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(functionSymbol, charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, charSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke(functionSymbol, 'G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - short result = (short)mh.invokeExact(functionSymbol, (short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(functionSymbol, shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, shortSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke(functionSymbol, (short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get(); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(functionSymbol, 2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(functionSymbol, 22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(functionSymbol, 11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(functionSymbol, intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, intSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(functionSymbol, 19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - long result = (long)mh.invokeExact(functionSymbol, 2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(functionSymbol, 22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(functionSymbol, 1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(functionSymbol, longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, longSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(functionSymbol, 668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - float result = (float)mh.invokeExact(functionSymbol, 6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(functionSymbol, floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, floatSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(functionSymbol, 79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - double result = (double)mh.invokeExact(functionSymbol, 3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_2() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_2() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(functionSymbol, 66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - Addressable functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/InvalidUpCallTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/InvalidUpCallTests.java deleted file mode 100644 index 863097d11c0..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/InvalidUpCallTests.java +++ /dev/null @@ -1,248 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.Linker; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for argument/return struct in upcall, - * which verify the illegal cases specific to the return value. - */ -@Test(groups = { "level.sanity" }) -public class InvalidUpCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "An exception is thrown from the upcall method") - public void test_throwExceptionFromUpcallMethod() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_throwException, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out IllegalArgumentException from the the upcall method"); - } - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "An exception is thrown from the upcall method") - public void test_nestedUpcall_throwExceptionFromUpcallMethod() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_nestedUpcall, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out IllegalArgumentException from the nested upcall"); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_nullValue, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null value upon return"); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForReturnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_nullValue, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null value upon return"); - } - } - - /* UnsupportedOperationException is thrown out from MemorySegment.address() in upcall in JDK19 if - * the memory segment is not native, which is based on different implemenations in OpenJDK - * as compared to JDK17/20. - */ - @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "A NULL memory address is not allowed.*") - public void test_nullAddrForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_nullAddr, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null memory address upon return"); - } - } - - /* In JDK19, an UnsupportedOperationException by MemorySegment.address() upon return - * in upcall in the case of the heap segment. - */ - @Test(expectedExceptions = UnsupportedOperationException.class, expectedExceptionsMessageRegExp = "Cannot obtain address of on-heap segment.*") - public void test_heapSegmentForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_heapSegmt, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out UnsupportedOperationException in the case of the heap segment upon return"); - } - } - - public void test_heapSegmentForReturnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_heapSegmt, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallMHTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallMHTests.java deleted file mode 100644 index 22d343b93e2..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallMHTests.java +++ /dev/null @@ -1,560 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) intended for - * the situations when the multiple primitive specific upcalls happen within - * the same memory session or from different memory sessions. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallMHTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr1); - Assert.assertEquals(result, true); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - result = (boolean)mh.invoke(true, false, upcallFuncAddr2); - Assert.assertEquals(result, true); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - result = (boolean)mh.invoke(true, false, upcallFuncAddr3); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - MemorySegment charSegmt1 = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt1, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - MemorySegment charSegmt2 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt2, 'D', upcallFuncAddr2); - Assert.assertEquals(result, 'C'); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - MemorySegment charSegmt3 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt3, 'D', upcallFuncAddr3); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr1); - Assert.assertEquals(result, (byte)88); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - result = (byte)mh.invoke((byte)33, upcallFuncAddr2); - Assert.assertEquals(result, (byte)88); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - result = (byte)mh.invoke((byte)33, upcallFuncAddr3); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - MemorySegment shortSegmt1 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr1 = (MemoryAddress)mh.invoke(shortSegmt1, (short)555, upcallFuncAddr1); - Assert.assertEquals(resultAddr1.get(JAVA_SHORT, 0), (short)999); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - MemorySegment shortSegmt2 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr2 = (MemoryAddress)mh.invoke(shortSegmt2, (short)555, upcallFuncAddr2); - Assert.assertEquals(resultAddr2.get(JAVA_SHORT, 0), (short)999); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - MemorySegment shortSegmt3 = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr3 = (MemoryAddress)mh.invoke(shortSegmt3, (short)555, upcallFuncAddr3); - Assert.assertEquals(resultAddr3.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr1); - Assert.assertEquals(result, 222235); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr2); - Assert.assertEquals(result, 222235); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr3); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr1); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr2); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr3); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(111454, 111398, upcallFuncAddr); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt1, 6666698235L, upcallFuncAddr1); - Assert.assertEquals(result, 12409155659L); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt2, 6666698235L, upcallFuncAddr2); - Assert.assertEquals(result, 12409155659L); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt3, 6666698235L, upcallFuncAddr3); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - float result = (float)mh.invoke(5.74F, upcallFuncAddr1); - Assert.assertEquals(result, 12.53F, 0.01F); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - result = (float)mh.invoke(5.74F, upcallFuncAddr2); - Assert.assertEquals(result, 12.53F, 0.01F); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - result = (float)mh.invoke(5.74F, upcallFuncAddr3); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_SameSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr1 = (MemoryAddress)mh.invoke(doubleSegmt1, 1262.795D, upcallFuncAddr1); - Assert.assertEquals(resultAddr1.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr2 = (MemoryAddress)mh.invoke(doubleSegmt2, 1262.795D, upcallFuncAddr2); - Assert.assertEquals(resultAddr2.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr3 = (MemoryAddress)mh.invoke(doubleSegmt3, 1262.795D, upcallFuncAddr3); - Assert.assertEquals(resultAddr3.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_DiffSession() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests1.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests1.java deleted file mode 100644 index 50687ae96d2..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests1.java +++ /dev/null @@ -1,135 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within different memory sessions, in which case the upcall - * metadata and the generated thunk are allocated separately. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithDiffSessions() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests2.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests2.java deleted file mode 100644 index 64ec4938984..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/MultiUpcallThrdsMHTests2.java +++ /dev/null @@ -1,128 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within the same memory session, in which case the upcall - * metadata and the generated thunk are only allocated once and shared among - * these threads. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - private static MemorySession session = MemorySession.openImplicit(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithSameSession() throws Throwable { - Thread thr1 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread(){ - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null){ - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithMixedSigStruTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithMixedSigStruTests.java deleted file mode 100644 index ba185e21c08..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithMixedSigStruTests.java +++ /dev/null @@ -1,894 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for the mixed native signatures - * in argument/return struct in upcall, which are not covered in UpcallMHWithStructTests and - * specially designed to validate the native signature types required in the genenerated thunk. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithMixedSigStruTests { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addIntAndIntShortFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntShortFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntShortFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invoke(22334455, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndShortIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndShortIntFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invoke(11335577, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntAndIntLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invoke(22446688, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndLongIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndLongIntFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invoke(1234567, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111844.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 111111111); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111546.221D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - public static void test_addDoubleAndDoubleFloatPlusPaddingFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAnd2FloatsDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAnd2FloatsDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAnd2FloatsDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDouble2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDouble2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDouble2FloatsFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndInt2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt2FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 111111); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addDoubleAndIntFloatDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatIntDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22.33F); - elemHandle2.set(structSegmt, 111111111); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndLongDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndLongDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndLongDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22222222222222L); - elemHandle2.set(structSegmt, 33333.444D); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 22222222311110.555D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt3FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndInt3FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt3FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 77777777); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - elemHandle4.set(structSegmt, 44.55F); - - float result = (float)mh.invoke(66.678F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.778F, 0.001F); - } - } - - @Test - public void test_addLongAndLong2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLong2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLong2FloatsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 777777777777L); - elemHandle2.set(structSegmt, 11.25F); - elemHandle3.set(structSegmt, 22.75F); - - long result = (long)mh.invoke(555555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1333333333365L); - } - } - - @Test - public void test_addFloatAnd3FloatsIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_INT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAnd3FloatsIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAnd3FloatsIntFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 44.55F); - elemHandle4.set(structSegmt, 77777777); - - float result = (float)mh.invoke(66.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.556F, 0.001F); - } - } - - @Test - public void test_addLongAndFloatLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndFloatLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndFloatLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 55.11F); - elemHandle2.set(structSegmt, 150000000000L); - - long result = (long)mh.invoke(5555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 155555555610L); - } - } - - @Test - public void test_addDoubleAndDoubleFloatIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 111111111); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.341D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33333.444D); - elemHandle2.set(structSegmt, 222222222222L); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 222222311110.555D, 0.001D); - } - } - - @Test - public void test_addLongAnd2FloatsLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAnd2FloatsLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAnd2FloatsLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.11F); - elemHandle2.set(structSegmt, 22.11F); - elemHandle3.set(structSegmt, 4444444444L); - - long result = (long)mh.invoke(11111111111L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15555555588L); - } - } - - @Test - public void test_addShortAnd3ShortsCharFromStructByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(3, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAnd3ShortsCharFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAnd3ShortsCharFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1000); - structSegmt.set(JAVA_SHORT, 2, (short)2000); - structSegmt.set(JAVA_SHORT, 4, (short)3000); - structSegmt.set(JAVA_CHAR, 6, 'A'); - - short result = (short)mh.invoke((short)4000, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 10065); - } - } - - @Test - public void test_addFloatAndIntFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndIntFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndIntFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 555555555); - elemHandle2.set(structSegmt, 11.222F); - elemHandle3.set(structSegmt, 666666666); - elemHandle4.set(structSegmt, 33.444F); - - float result = (float)mh.invoke(77.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1222222343.122F, 0.001F); - } - } - - @Test - public void test_addDoubleAndIntDoubleFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 7777); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33.444F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 6666); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15217.122D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFloatFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.222F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 818.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleLongFromStructByUpcallMH() throws Throwable { - /* The padding in the struct [int, double, long] on AIX/PPC 64-bit is different from - * other platforms as follows: - * 1) there is no padding between int and double. - * 2) there is a 4-byte padding between double and long. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32), MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - elemHandle3.set(structSegmt, 888888888888L); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 889000000732.344D, 0.001D); - } - } - - @Test - public void test_return254BytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("return254BytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_return254BytesFromStruct, - FunctionDescriptor.of(structLayout), session); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 254; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } - - @Test - public void test_return4KBytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("return4KBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_return4KBytesFromStruct, - FunctionDescriptor.of(structLayout), session); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 4096; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithPrimTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithPrimTests.java deleted file mode 100644 index 30a566576e7..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithPrimTests.java +++ /dev/null @@ -1,724 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for primitive types in upcall. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithPrimTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), session); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPointerWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), session); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromNativePtrWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromNativePtrWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), session); - boolean result = (boolean)mh.invoke(false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), session); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), session); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addTwoBytesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BytesByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Bytes, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE), session); - byte result = (byte)mh.invoke((byte)6, (byte)3, upcallFuncAddr); - Assert.assertEquals(result, 9); - } - } - - @Test - public void test_addByteAndByteFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)7); - byte result = (byte)mh.invoke((byte)8, byteSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, 88); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_createNewCharFrom2CharsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2CharsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFrom2Chars, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR), session); - char result = (char)mh.invoke('B', 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), session); - char result = (char)mh.invoke('D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_addTwoShortsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Shorts, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT), session); - short result = (short)mh.invoke((short)1111, (short)2222, upcallFuncAddr); - Assert.assertEquals(result, 3333); - } - } - - @Test - public void test_addShortAndShortFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)2222); - short result = (short)mh.invoke(shortSegmt, (short)3333, upcallFuncAddr); - Assert.assertEquals(result, 5555); - } - } - - @Test - public void test_addShortAndShortFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), session); - short result = (short)mh.invoke((short)789, upcallFuncAddr); - Assert.assertEquals(result, 1245); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addIntAndIntFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - int result = (int)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(result, 555536); - } - } - - @Test - public void test_addIntAndIntFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - int result = (int)mh.invoke(222222, upcallFuncAddr); - Assert.assertEquals(result, 666666); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_add3IntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT), session); - int result = (int)mh.invoke(111112, 111123, 111124, upcallFuncAddr); - Assert.assertEquals(result, 333359); - } - } - - @Test - public void test_addIntAndCharByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndCharByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndChar, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR), session); - int result = (int)mh.invoke(555558, 'A', upcallFuncAddr); - Assert.assertEquals(result, 555623); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), session); - mh.invoke(44454, 333398, upcallFuncAddr); - } - } - - @Test - public void test_addTwoLongsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Longs, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG), session); - long result = (long)mh.invoke(333333222222L, 111111555555L, upcallFuncAddr); - Assert.assertEquals(result, 444444777777L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), session); - long result = (long)mh.invoke(5555555555L, upcallFuncAddr); - Assert.assertEquals(result, 8888888888L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addTwoFloatsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Floats, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT), session); - float result = (float)mh.invoke(15.74F, 16.79F, upcallFuncAddr); - Assert.assertEquals(result, 32.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - float result = (float)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_add2DoublesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoublesByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Doubles, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE), session); - double result = (double)mh.invoke(159.748D, 262.795D, upcallFuncAddr); - Assert.assertEquals(result, 422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - double result = (double)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), session); - double result = (double)mh.invoke(1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_qsortByUpcallMH() throws Throwable { - int expectedArray[] = {11, 12, 13, 14, 15, 16, 17}; - int expectedArrayLength = expectedArray.length; - - FunctionDescriptor fd = FunctionDescriptor.ofVoid(ADDRESS, JAVA_INT, JAVA_INT, ADDRESS); - Addressable functionSymbol = defaultLibLookup.lookup("qsort").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_compare, - FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment arraySegmt = allocator.allocateArray(JAVA_INT, new int[]{17, 14, 13, 16, 15, 12, 11}); - mh.invoke(arraySegmt, 7, 4, upcallFuncAddr); - int[] sortedArray = arraySegmt.toArray(JAVA_INT); - for (int index = 0; index < expectedArrayLength; index++) { - Assert.assertEquals(sortedArray[index], expectedArray[index]); - } - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithStructTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithStructTests.java deleted file mode 100644 index b4fd6efdc9a..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMHWithStructTests.java +++ /dev/null @@ -1,2888 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for argument/return struct in upcall. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithStructTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - /* The padding of struct is not required on Linux/s390x and Windows/x64 */ - private static boolean isStructPaddingNotRequired = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")) - || osName.contains("linux") && arch.equals("s390x"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAnd20BoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAnd20BoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAnd20BoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - structSegmt.set(JAVA_BOOLEAN, 5, true); - structSegmt.set(JAVA_BOOLEAN, 6, false); - structSegmt.set(JAVA_BOOLEAN, 7, true); - structSegmt.set(JAVA_BOOLEAN, 8, false); - structSegmt.set(JAVA_BOOLEAN, 9, true); - structSegmt.set(JAVA_BOOLEAN, 10, false); - structSegmt.set(JAVA_BOOLEAN, 11, true); - structSegmt.set(JAVA_BOOLEAN, 12, false); - structSegmt.set(JAVA_BOOLEAN, 13, true); - structSegmt.set(JAVA_BOOLEAN, 14, false); - structSegmt.set(JAVA_BOOLEAN, 15, true); - structSegmt.set(JAVA_BOOLEAN, 16, false); - structSegmt.set(JAVA_BOOLEAN, 17, true); - structSegmt.set(JAVA_BOOLEAN, 18, false); - structSegmt.set(JAVA_BOOLEAN, 19, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout), session); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructPointerWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructPointerWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXorByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invoke((byte)6, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteAnd20BytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAnd20BytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAnd20BytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)1); - structSegmt.set(JAVA_BYTE, 1, (byte)2); - structSegmt.set(JAVA_BYTE, 2, (byte)3); - structSegmt.set(JAVA_BYTE, 3, (byte)4); - structSegmt.set(JAVA_BYTE, 4, (byte)5); - structSegmt.set(JAVA_BYTE, 5, (byte)6); - structSegmt.set(JAVA_BYTE, 6, (byte)7); - structSegmt.set(JAVA_BYTE, 7, (byte)8); - structSegmt.set(JAVA_BYTE, 8, (byte)9); - structSegmt.set(JAVA_BYTE, 9, (byte)10); - structSegmt.set(JAVA_BYTE, 10, (byte)1); - structSegmt.set(JAVA_BYTE, 11, (byte)2); - structSegmt.set(JAVA_BYTE, 12, (byte)3); - structSegmt.set(JAVA_BYTE, 13, (byte)4); - structSegmt.set(JAVA_BYTE, 14, (byte)5); - structSegmt.set(JAVA_BYTE, 15, (byte)6); - structSegmt.set(JAVA_BYTE, 16, (byte)7); - structSegmt.set(JAVA_BYTE, 17, (byte)8); - structSegmt.set(JAVA_BYTE, 18, (byte)9); - structSegmt.set(JAVA_BYTE, 19, (byte)10); - - byte result = (byte)mh.invoke((byte)11, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 121); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct_returnBytePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 42); - Assert.assertEquals(resultAddr.toRawLongValue(), byteSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - byte result = (byte)mh.invoke((byte)13, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)46, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invoke((byte)48, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArrayByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)14, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 80); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invoke((byte)18, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invoke((byte)16, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invoke((byte)22, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_add1ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add1ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add1ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - } - } - - @Test - public void test_add2ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invoke('C', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharAnd10CharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, - JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAnd10CharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAnd10CharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'A'); - structSegmt.set(JAVA_CHAR, 4, 'B'); - structSegmt.set(JAVA_CHAR, 6, 'B'); - structSegmt.set(JAVA_CHAR, 8, 'C'); - structSegmt.set(JAVA_CHAR, 10, 'C'); - structSegmt.set(JAVA_CHAR, 12, 'D'); - structSegmt.set(JAVA_CHAR, 14, 'D'); - structSegmt.set(JAVA_CHAR, 16, 'E'); - structSegmt.set(JAVA_CHAR, 18, 'E'); - - char result = (char)mh.invoke('A', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'U'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct_returnCharPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultAddr.toRawLongValue(), charSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructPointer, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArrayByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2CharStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultAddr.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)888); - shortHandle2.set(structSegmt, (short)999); - - short result = (short)mh.invoke((short)777, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2664); - } - } - - @Test - public void test_addShortAnd10ShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, - JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAnd10ShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAnd10ShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)10); - structSegmt.set(JAVA_SHORT, 2, (short)20); - structSegmt.set(JAVA_SHORT, 4, (short)30); - structSegmt.set(JAVA_SHORT, 6, (short)40); - structSegmt.set(JAVA_SHORT, 8, (short)50); - structSegmt.set(JAVA_SHORT, 10, (short)60); - structSegmt.set(JAVA_SHORT, 12, (short)70); - structSegmt.set(JAVA_SHORT, 14, (short)80); - structSegmt.set(JAVA_SHORT, 16, (short)90); - structSegmt.set(JAVA_SHORT, 18, (short)100); - - short result = (short)mh.invoke((short)110, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 660); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - short result = (short)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 3349); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct_returnShortPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 3349); - Assert.assertEquals(resultAddr.toRawLongValue(), shortSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructPointer, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)2222); - shortHandle2.set(structSegmt, (short)4444); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13332); - } - } - - @Test - public void test_addShortAndShortsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArrayByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_add2ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)734); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)712); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 734); - Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 712); - } - } - - @Test - public void test_add3ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)325); - shortHandle2.set(structSegmt1, (short)326); - shortHandle3.set(structSegmt1, (short)327); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)334); - shortHandle2.set(structSegmt2, (short)335); - shortHandle3.set(structSegmt2, (short)336); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)659); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)661); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)663); - } - } - - @Test - public void test_addIntAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invoke(2244668, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAnd5IntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), - JAVA_INT.withName("elem3"), JAVA_INT.withName("elem4"), JAVA_INT.withName("elem5")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle intHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle intHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAnd5IntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAnd5IntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1111111); - intHandle2.set(structSegmt, 2222222); - intHandle3.set(structSegmt, 3333333); - intHandle4.set(structSegmt, 2222222); - intHandle5.set(structSegmt, 1111111); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 14444443); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct_returnIntPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArrayByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultAddr.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invoke(2468024680L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct_returnLongPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructPointer, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArrayByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invoke(6.56F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatAnd5FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), - JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4"), JAVA_FLOAT.withName("elem5")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle floatHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle floatHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAnd5FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAnd5FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 1.01F); - floatHandle2.set(structSegmt, 1.02F); - floatHandle3.set(structSegmt, 1.03F); - floatHandle4.set(structSegmt, 1.04F); - floatHandle5.set(structSegmt, 1.05F); - - float result = (float)mh.invoke(1.06F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 6.21F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultAddr.toRawLongValue(), floatSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invoke(3336.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultAddr.toRawLongValue(), doubleSegmt.address().toRawLongValue()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructPointer, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemoryAddress resultAddr = (MemoryAddress)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add3DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMethodHandles.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMethodHandles.java deleted file mode 100644 index ba16ce5f9d2..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/upcall/UpcallMethodHandles.java +++ /dev/null @@ -1,1956 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2021 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.upcall; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.Linker; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; - -import static java.lang.foreign.Linker.*; -import static java.lang.foreign.ValueLayout.*; - -/** - * The helper class that contains all upcall method handles with primitive types or struct - * as arguments. - */ -public class UpcallMethodHandles { - private static final Lookup lookup = MethodHandles.lookup(); - private static MemorySession session = MemorySession.openImplicit(); - private static SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - - static final MethodType MT_Bool_Bool_MemSegmt = methodType(boolean.class, boolean.class, MemorySegment.class); - static final MethodType MT_Addr_Bool_MemAddr = methodType(Addressable.class, boolean.class, MemoryAddress.class); - static final MethodType MT_Char_Char_MemSegmt = methodType(char.class, char.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Char = methodType(Addressable.class, MemoryAddress.class, char.class); - static final MethodType MT_Byte_Byte_MemSegmt = methodType(byte.class, byte.class, MemorySegment.class); - static final MethodType MT_Addr_Byte_MemAddr = methodType(Addressable.class, byte.class, MemoryAddress.class); - static final MethodType MT_Short_Short_MemSegmt = methodType(short.class, short.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Short = methodType(Addressable.class, MemoryAddress.class, short.class); - static final MethodType MT_Int_Int_MemSegmt = methodType(int.class, int.class, MemorySegment.class); - static final MethodType MT_Addr_Int_MemAddr = methodType(Addressable.class, int.class, MemoryAddress.class); - static final MethodType MT_Long_Long_MemSegmt = methodType(long.class, long.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Long = methodType(Addressable.class, MemoryAddress.class, long.class); - static final MethodType MT_Long_Int_MemSegmt = methodType(long.class, int.class, MemorySegment.class); - static final MethodType MT_Float_Float_MemSegmt = methodType(float.class, float.class, MemorySegment.class); - static final MethodType MT_Addr_Float_MemAddr = methodType(Addressable.class, float.class, MemoryAddress.class); - static final MethodType MT_Double_Double_MemSegmt = methodType(double.class, double.class, MemorySegment.class); - static final MethodType MT_Addr_MemAddr_Double = methodType(Addressable.class, MemoryAddress.class, double.class); - static final MethodType MT_Addr_MemAddr_MemSegmt = methodType(Addressable.class, MemoryAddress.class, MemorySegment.class); - static final MethodType MT_MemSegmt_MemSegmt_MemSegmt = methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - static final MethodType MT_MemSegmt = methodType(MemorySegment.class); - - public static final MethodHandle MH_add2BoolsWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPointerWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetPtr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetArgPtr; - public static final MethodHandle MH_createNewCharFrom2Chars; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPointer; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetPtr; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Bytes; - public static final MethodHandle MH_addByteAndByteFromPointer; - public static final MethodHandle MH_addByteAndByteFromPtr_RetPtr; - public static final MethodHandle MH_addByteAndByteFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Shorts; - public static final MethodHandle MH_addShortAndShortFromPointer; - public static final MethodHandle MH_addShortAndShortFromPtr_RetPtr; - public static final MethodHandle MH_addShortAndShortFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Ints; - public static final MethodHandle MH_addIntAndIntFromPointer; - public static final MethodHandle MH_addIntAndIntFromPtr_RetPtr; - public static final MethodHandle MH_addIntAndIntFromPtr_RetArgPtr; - public static final MethodHandle MH_add3Ints; - public static final MethodHandle MH_addIntAndChar; - public static final MethodHandle MH_add2IntsReturnVoid; - public static final MethodHandle MH_add2Longs; - public static final MethodHandle MH_addLongAndLongFromPointer; - public static final MethodHandle MH_addLongAndLongFromPtr_RetPtr; - public static final MethodHandle MH_addLongAndLongFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Floats; - public static final MethodHandle MH_addFloatAndFloatFromPointer; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetPtr; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Doubles; - public static final MethodHandle MH_addDoubleAndDoubleFromPointer; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetPtr; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetArgPtr; - public static final MethodHandle MH_compare; - - public static final MethodHandle MH_addBoolAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolAnd20BoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer; - public static final MethodHandle MH_addBoolAndBoolsFromStructPointerWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStruct; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStructPointer; - public static final MethodHandle MH_add3BoolStructsWithXor_returnStruct; - - public static final MethodHandle MH_addByteAndBytesFromStruct; - public static final MethodHandle MH_addByteAnd20BytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct_returnBytePointer; - public static final MethodHandle MH_addByteAndBytesFromStructPointer; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add1ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStructPointer; - public static final MethodHandle MH_add3ByteStructs_returnStruct; - - public static final MethodHandle MH_addCharAndCharsFromStruct; - public static final MethodHandle MH_addCharAnd10CharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct_returnCharPointer; - public static final MethodHandle MH_addCharAndCharsFromStructPointer; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2CharStructs_returnStruct; - public static final MethodHandle MH_add2CharStructs_returnStructPointer; - public static final MethodHandle MH_add3CharStructs_returnStruct; - - public static final MethodHandle MH_addShortAndShortsFromStruct; - public static final MethodHandle MH_addShortAnd10ShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct_returnShortPointer; - public static final MethodHandle MH_addShortAndShortsFromStructPointer; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2ShortStructs_returnStruct; - public static final MethodHandle MH_add2ShortStructs_returnStructPointer; - public static final MethodHandle MH_add3ShortStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntsFromStruct; - public static final MethodHandle MH_addIntAnd5IntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct_returnIntPointer; - public static final MethodHandle MH_addIntAndIntsFromStructPointer; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2IntStructs_returnStruct; - public static final MethodHandle MH_add2IntStructs_returnStruct_throwException; - public static final MethodHandle MH_add2IntStructs_returnStruct_nestedUpcall; - public static final MethodHandle MH_add2IntStructs_returnStruct_nullValue; - public static final MethodHandle MH_add2IntStructs_returnStruct_heapSegmt; - public static final MethodHandle MH_add2IntStructs_returnStructPointer; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_nullValue; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_nullAddr; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_heapSegmt; - public static final MethodHandle MH_add3IntStructs_returnStruct; - - public static final MethodHandle MH_addLongAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct_returnLongPointer; - public static final MethodHandle MH_addLongAndLongsFromStructPointer; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2LongStructs_returnStruct; - public static final MethodHandle MH_add2LongStructs_returnStructPointer; - public static final MethodHandle MH_add3LongStructs_returnStruct; - - public static final MethodHandle MH_addFloatAndFloatsFromStruct; - public static final MethodHandle MH_addFloatAnd5FloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer; - public static final MethodHandle MH_addFloatAndFloatsFromStructPointer; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2FloatStructs_returnStruct; - public static final MethodHandle MH_add2FloatStructs_returnStructPointer; - public static final MethodHandle MH_add3FloatStructs_returnStruct; - - public static final MethodHandle MH_addDoubleAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer; - public static final MethodHandle MH_addDoubleAndDoublesFromStructPointer; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2DoubleStructs_returnStruct; - public static final MethodHandle MH_add2DoubleStructs_returnStructPointer; - public static final MethodHandle MH_add3DoubleStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntShortFromStruct; - public static final MethodHandle MH_addIntAndShortIntFromStruct; - public static final MethodHandle MH_addIntAndIntLongFromStruct; - public static final MethodHandle MH_addIntAndLongIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAnd2FloatsDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDouble2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndInt2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndFloatIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndLongDoubleFromStruct; - public static final MethodHandle MH_addFloatAndInt3FloatsFromStruct; - public static final MethodHandle MH_addLongAndLong2FloatsFromStruct; - public static final MethodHandle MH_addFloatAnd3FloatsIntFromStruct; - public static final MethodHandle MH_addLongAndFloatLongFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatIntFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleLongFromStruct; - public static final MethodHandle MH_addLongAnd2FloatsLongFromStruct; - public static final MethodHandle MH_addShortAnd3ShortsCharFromStruct; - public static final MethodHandle MH_addFloatAndIntFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleLongFromStruct; - public static final MethodHandle MH_return254BytesFromStruct; - public static final MethodHandle MH_return4KBytesFromStruct; - - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - - try { - MH_add2BoolsWithOr = lookup.findStatic(UpcallMethodHandles.class, "add2BoolsWithOr", methodType(boolean.class, boolean.class, boolean.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPointerWithOr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPointerWithOr", methodType(boolean.class, boolean.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetPtr", MT_Addr_Bool_MemAddr); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetArgPtr", MT_Addr_Bool_MemAddr); //$NON-NLS-1$ - - MH_createNewCharFrom2Chars = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFrom2Chars", methodType(char.class, char.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPointer = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPointer", methodType(char.class, MemoryAddress.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetPtr", MT_Addr_MemAddr_Char); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetArgPtr", MT_Addr_MemAddr_Char); //$NON-NLS-1$ - - MH_add2Bytes = lookup.findStatic(UpcallMethodHandles.class, "add2Bytes", methodType(byte.class, byte.class, byte.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPointer", methodType(byte.class, byte.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetPtr", MT_Addr_Byte_MemAddr); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetArgPtr", MT_Addr_Byte_MemAddr); //$NON-NLS-1$ - - MH_add2Shorts = lookup.findStatic(UpcallMethodHandles.class, "add2Shorts", methodType(short.class, short.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPointer", methodType(short.class, MemoryAddress.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetPtr", MT_Addr_MemAddr_Short); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetArgPtr", MT_Addr_MemAddr_Short); //$NON-NLS-1$ - - MH_add2Ints = lookup.findStatic(UpcallMethodHandles.class, "add2Ints", methodType(int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPointer", methodType(int.class, int.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetPtr", MT_Addr_Int_MemAddr); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetArgPtr", MT_Addr_Int_MemAddr); //$NON-NLS-1$ - MH_add3Ints = lookup.findStatic(UpcallMethodHandles.class, "add3Ints", methodType(int.class, int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndChar = lookup.findStatic(UpcallMethodHandles.class, "addIntAndChar", methodType(int.class, int.class, char.class)); //$NON-NLS-1$ - MH_add2IntsReturnVoid = lookup.findStatic(UpcallMethodHandles.class, "add2IntsReturnVoid", methodType(void.class, int.class, int.class)); //$NON-NLS-1$ - - MH_add2Longs = lookup.findStatic(UpcallMethodHandles.class, "add2Longs", methodType(long.class, long.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPointer", methodType(long.class, MemoryAddress.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetPtr", MT_Addr_MemAddr_Long); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetArgPtr", MT_Addr_MemAddr_Long); //$NON-NLS-1$ - - MH_add2Floats = lookup.findStatic(UpcallMethodHandles.class, "add2Floats", methodType(float.class, float.class, float.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPointer", methodType(float.class, float.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetPtr", MT_Addr_Float_MemAddr); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetArgPtr", MT_Addr_Float_MemAddr); //$NON-NLS-1$ - - MH_add2Doubles = lookup.findStatic(UpcallMethodHandles.class, "add2Doubles", methodType(double.class, double.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPointer", methodType(double.class, MemoryAddress.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetPtr", MT_Addr_MemAddr_Double); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetArgPtr", MT_Addr_MemAddr_Double); //$NON-NLS-1$ - - MH_compare = lookup.findStatic(UpcallMethodHandles.class, "compare", methodType(int.class, MemoryAddress.class, MemoryAddress.class)); //$NON-NLS-1$ - - MH_addBoolAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAnd20BoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAnd20BoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor", methodType(boolean.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructPointerWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructPointerWithXor", methodType(boolean.class, boolean.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addByteAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAnd20BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAnd20BytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct", methodType(byte.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct_returnBytePointer = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct_returnBytePointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructPointer", methodType(byte.class, byte.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_add1ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add1ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addCharAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAnd10CharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAnd10CharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct", methodType(char.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct_returnCharPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct_returnCharPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructPointer", methodType(char.class, char.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addShortAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd10ShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd10ShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct", methodType(short.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct_returnShortPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct_returnShortPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructPointer", methodType(short.class, short.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAnd5IntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAnd5IntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct", methodType(int.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct_returnIntPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct_returnIntPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructPointer", methodType(int.class, int.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_throwException = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_throwException", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_nestedUpcall = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_nestedUpcall", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_nullValue = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_nullValue", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_heapSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_heapSegmt", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_nullValue = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_nullValue", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_nullAddr = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_nullAddr", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_heapSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_heapSegmt", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addLongAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct", methodType(long.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct_returnLongPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct_returnLongPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructPointer", methodType(long.class, long.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addFloatAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd5FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd5FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct", methodType(float.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct_returnFloatPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructPointer", methodType(float.class, float.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addDoubleAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct", methodType(double.class, MemoryAddress.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructPointer", methodType(double.class, double.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStructPointer", MT_Addr_MemAddr_MemSegmt); //$NON-NLS-1$ - MH_add3DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntShortFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntShortFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndShortIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortIntFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntLongFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndLongIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndLongIntFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAnd2FloatsDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAnd2FloatsDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDouble2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDouble2FloatsFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt2FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndLongDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndLongDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt3FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt3FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLong2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLong2FloatsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd3FloatsIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd3FloatsIntFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndFloatLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndFloatLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addLongAnd2FloatsLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAnd2FloatsLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd3ShortsCharFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd3ShortsCharFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndIntFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndIntFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_return254BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return254BytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - MH_return4KBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return4KBytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - - } catch (IllegalAccessException | NoSuchMethodException e) { - throw new InternalError(e); - } - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - public static boolean add2BoolsWithOr(boolean boolArg1, boolean boolArg2) { - boolean result = boolArg1 || boolArg2; - return result; - } - - public static boolean addBoolAndBoolFromPointerWithOr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - return result; - } - - public static Addressable addBoolAndBoolFromPtrWithOr_RetPtr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, session); - resultSegmt.set(JAVA_BOOLEAN, 0, result); - return resultSegmt.address(); - } - - public static Addressable addBoolAndBoolFromPtrWithOr_RetArgPtr(boolean boolArg1, MemoryAddress boolArg2Addr) { - boolean result = boolArg1 || boolArg2Addr.get(JAVA_BOOLEAN, 0); - boolArg2Addr.set(JAVA_BOOLEAN, 0, result); - return boolArg2Addr; - } - - public static char createNewCharFrom2Chars(char charArg1, char charArg2) { - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static char createNewCharFromCharAndCharFromPointer(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static Addressable createNewCharFromCharAndCharFromPtr_RetPtr(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_CHAR.byteSize(), session); - resultSegmt.set(JAVA_CHAR, 0, result); - return resultSegmt.address(); - } - - public static Addressable createNewCharFromCharAndCharFromPtr_RetArgPtr(MemoryAddress charArg1Addr, char charArg2) { - char charArg1 = charArg1Addr.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - charArg1Addr.set(JAVA_CHAR, 0, result); - return charArg1Addr; - } - - public static byte add2Bytes(byte byteArg1, byte byteArg2) { - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static byte addByteAndByteFromPointer(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static Addressable addByteAndByteFromPtr_RetPtr(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BYTE.byteSize(), session); - resultSegmt.set(JAVA_BYTE, 0, byteSum); - return resultSegmt.address(); - } - - public static Addressable addByteAndByteFromPtr_RetArgPtr(byte byteArg1, MemoryAddress byteArg2Addr) { - byte byteArg2 = byteArg2Addr.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - byteArg2Addr.set(JAVA_BYTE, 0, byteSum); - return byteArg2Addr; - } - - public static short add2Shorts(short shortArg1, short shortArg2) { - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static short addShortAndShortFromPointer(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static Addressable addShortAndShortFromPtr_RetPtr(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_SHORT.byteSize(), session); - resultSegmt.set(JAVA_SHORT, 0, shortSum); - return resultSegmt.address(); - } - - public static Addressable addShortAndShortFromPtr_RetArgPtr(MemoryAddress shortArg1Addr, short shortArg2) { - short shortArg1 = shortArg1Addr.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - shortArg1Addr.set(JAVA_SHORT, 0, shortSum); - return shortArg1Addr; - } - - public static int add2Ints(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - return intSum; - } - - public static int addIntAndIntFromPointer(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - return intSum; - } - - public static Addressable addIntAndIntFromPtr_RetPtr(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_INT.byteSize(), session); - resultSegmt.set(JAVA_INT, 0, intSum); - return resultSegmt.address(); - } - - public static Addressable addIntAndIntFromPtr_RetArgPtr(int intArg1, MemoryAddress intArg2Addr) { - int intArg2 = intArg2Addr.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - intArg2Addr.set(JAVA_INT, 0, intSum); - return intArg2Addr; - } - - public static int add3Ints(int intArg1, int intArg2, int intArg3) { - int intSum = intArg1 + intArg2 + intArg3; - return intSum; - } - - public static int addIntAndChar(int intArg, char charArg) { - int sum = intArg + charArg; - return sum; - } - - public static void add2IntsReturnVoid(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - System.out.println("add2IntsReturnVoid: intSum = " + intSum + "\n"); - } - - public static long add2Longs(long longArg1, long longArg2) { - long longSum = longArg1 + longArg2; - return longSum; - } - - public static long addLongAndLongFromPointer(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - return longSum; - } - - public static Addressable addLongAndLongFromPtr_RetPtr(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_LONG.byteSize(), session); - resultSegmt.set(JAVA_LONG, 0, longSum); - return resultSegmt.address(); - } - - public static Addressable addLongAndLongFromPtr_RetArgPtr(MemoryAddress longArg1Addr, long longArg2) { - long longArg1 = longArg1Addr.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - longArg1Addr.set(JAVA_LONG, 0, longSum); - return longArg1Addr; - } - - public static float add2Floats(float floatArg1, float floatArg2) { - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static float addFloatAndFloatFromPointer(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static Addressable addFloatAndFloatFromPtr_RetPtr(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_FLOAT.byteSize(), session); - resultSegmt.set(JAVA_FLOAT, 0, floatSum); - return resultSegmt.address(); - } - - public static Addressable addFloatAndFloatFromPtr_RetArgPtr(float floatArg1, MemoryAddress floatArg2Addr) { - float floatArg2 = floatArg2Addr.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - floatArg2Addr.set(JAVA_FLOAT, 0, floatSum); - return floatArg2Addr; - } - - public static double add2Doubles(double doubleArg1, double doubleArg2) { - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static double addDoubleAndDoubleFromPointer(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static Addressable addDoubleAndDoubleFromPtr_RetPtr(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_DOUBLE.byteSize(), session); - resultSegmt.set(JAVA_DOUBLE, 0, doubleSum); - return resultSegmt.address(); - } - - public static Addressable addDoubleAndDoubleFromPtr_RetArgPtr(MemoryAddress doubleArg1Addr, double doubleArg2) { - double doubleArg1 = doubleArg1Addr.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - doubleArg1Addr.set(JAVA_DOUBLE, 0, doubleSum); - return doubleArg1Addr; - } - - public static int compare(MemoryAddress argAddr1, MemoryAddress argAddr2) { - int intArg1 = argAddr1.get(JAVA_INT, 0); - int intArg2 = argAddr2.get(JAVA_INT, 0); - return (intArg1 - intArg2); - } - - public static boolean addBoolAndBoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAnd20BoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1) - ^ arg2.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 3) ^ arg2.get(JAVA_BOOLEAN, 4) - ^ arg2.get(JAVA_BOOLEAN, 5) ^ arg2.get(JAVA_BOOLEAN, 6) ^ arg2.get(JAVA_BOOLEAN, 7) - ^ arg2.get(JAVA_BOOLEAN, 8) ^ arg2.get(JAVA_BOOLEAN, 9) ^ arg2.get(JAVA_BOOLEAN, 10) - ^ arg2.get(JAVA_BOOLEAN, 11) ^ arg2.get(JAVA_BOOLEAN, 12) ^ arg2.get(JAVA_BOOLEAN, 13) - ^ arg2.get(JAVA_BOOLEAN, 14) ^ arg2.get(JAVA_BOOLEAN, 15) ^ arg2.get(JAVA_BOOLEAN, 16) - ^ arg2.get(JAVA_BOOLEAN, 17) ^ arg2.get(JAVA_BOOLEAN, 18) ^ arg2.get(JAVA_BOOLEAN, 19); - return boolSum; - } - - public static boolean addBoolFromPointerAndBoolsFromStructWithXor(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolSum = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static Addressable addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolSum = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Addr.set(JAVA_BOOLEAN, 0, boolSum); - return arg1Addr; - } - - public static boolean addBoolAndBoolsFromStructPointerWithXor(boolean arg1, MemoryAddress arg2Addr) { - boolean boolSum = arg1 ^ arg2Addr.get(JAVA_BOOLEAN, 0) ^ arg2Addr.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor(boolean arg1, MemorySegment arg2) { - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ nestedStructElem1 ^ nestedStructElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ structElem1 ^ nestedStructElem1 ^ nestedStructElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray(boolean arg1, MemorySegment arg2) { - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ structElem1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray(boolean arg1, MemorySegment arg2) { - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 3); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem2 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 3); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem1 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static MemorySegment add2BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, session); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return boolStructSegmt; - } - - public static Addressable add2BoolStructsWithXor_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - boolean boolStruct_Elem1 = arg1Addr.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1Addr.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Addr.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - arg1Addr.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, session); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolean boolStruct_Elem3 = arg1.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 2); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - boolStructSegmt.set(JAVA_BOOLEAN, 2, boolStruct_Elem3); - return boolStructSegmt; - } - - public static byte addByteAndBytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAnd20BytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1) - + arg2.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 3) + arg2.get(JAVA_BYTE, 4) - + arg2.get(JAVA_BYTE, 5) + arg2.get(JAVA_BYTE, 6) + arg2.get(JAVA_BYTE, 7) - + arg2.get(JAVA_BYTE, 8) + arg2.get(JAVA_BYTE, 9) + arg2.get(JAVA_BYTE, 10) - + arg2.get(JAVA_BYTE, 11) + arg2.get(JAVA_BYTE, 12) + arg2.get(JAVA_BYTE, 13) - + arg2.get(JAVA_BYTE, 14) + arg2.get(JAVA_BYTE, 15) + arg2.get(JAVA_BYTE, 16) - + arg2.get(JAVA_BYTE, 17) + arg2.get(JAVA_BYTE, 18) + arg2.get(JAVA_BYTE, 19)); - return byteSum; - } - - public static byte addByteFromPointerAndBytesFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteSum = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static Addressable addByteFromPointerAndBytesFromStruct_returnBytePointer(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteSum = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - arg1Addr.set(JAVA_BYTE, 0, byteSum); - return arg1Addr; - } - - public static byte addByteAndBytesFromStructPointer(byte arg1, MemoryAddress arg2Addr) { - byte byteSum = (byte)(arg1 + arg2Addr.get(JAVA_BYTE, 0) + arg2Addr.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct(byte arg1, MemorySegment arg2) { - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray(byte arg1, MemorySegment arg2) { - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedByteArrayElem1 + nestedByteArrayElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedByteArrayElem1 + nestedByteArrayElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray(byte arg1, MemorySegment arg2) { - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 3); - byte structElem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 3); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static MemorySegment add1ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, session); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - return byteStructSegmt; - } - - public static MemorySegment add2ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, session); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - return byteStructSegmt; - } - - public static Addressable add2ByteStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - byte byteStruct_Elem1 = (byte)(arg1Addr.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1Addr.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - arg1Addr.set(JAVA_BYTE, 0, byteStruct_Elem1); - arg1Addr.set(JAVA_BYTE, 1, byteStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, session); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byte byteStruct_Elem3 = (byte)(arg1.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 2)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - byteStructSegmt.set(JAVA_BYTE, 2, byteStruct_Elem3); - return byteStructSegmt; - } - - public static char addCharAndCharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAnd10CharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - + arg2.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 6) + arg2.get(JAVA_CHAR, 8) - + arg2.get(JAVA_CHAR, 10) + arg2.get(JAVA_CHAR, 12) + arg2.get(JAVA_CHAR, 14) - + arg2.get(JAVA_CHAR, 16) + arg2.get(JAVA_CHAR, 18) - 10 * 'A'); - return result; - } - - public static char addCharFromPointerAndCharsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - char result = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static Addressable addCharFromPointerAndCharsFromStruct_returnCharPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - char result = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - arg1Addr.set(JAVA_CHAR, 0, result); - return arg1Addr; - } - - public static char addCharAndCharsFromStructPointer(char arg1, MemoryAddress arg2Addr) { - char result = (char)(arg1 + arg2Addr.get(JAVA_CHAR, 0) + arg2Addr.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct(char arg1, MemorySegment arg2) { - char nestedStructElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray(char arg1, MemorySegment arg2) { - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedCharArrayElem1 + nestedCharArrayElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 2); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedCharArrayElem1 + nestedCharArrayElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray(char arg1, MemorySegment arg2) { - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 6); - char structElem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 6); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static MemorySegment add2CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, session); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - return charStructSegmt; - } - - public static Addressable add2CharStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - char charStruct_Elem1 = (char)(arg1Addr.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1Addr.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - arg1Addr.set(JAVA_CHAR, 0, charStruct_Elem1); - arg1Addr.set(JAVA_CHAR, 2, charStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, session); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - char charStruct_Elem3 = (char)(arg1.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 4) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - charStructSegmt.set(JAVA_CHAR, 4, charStruct_Elem3); - return charStructSegmt; - } - - public static short addShortAndShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAnd10ShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 6) + arg2.get(JAVA_SHORT, 8) - + arg2.get(JAVA_SHORT, 10) + arg2.get(JAVA_SHORT, 12) + arg2.get(JAVA_SHORT, 14) - + arg2.get(JAVA_SHORT, 16) + arg2.get(JAVA_SHORT, 18)); - return shortSum; - } - - public static short addShortFromPointerAndShortsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortSum = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static Addressable addShortFromPointerAndShortsFromStruct_returnShortPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortSum = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - arg1Addr.set(JAVA_SHORT, 0, shortSum); - return arg1Addr; - } - - public static short addShortAndShortsFromStructPointer(short arg1, MemoryAddress arg2Addr) { - short shortSum = (short)(arg1 + arg2Addr.get(JAVA_SHORT, 0) + arg2Addr.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct(short arg1, MemorySegment arg2) { - short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray(short arg1, MemorySegment arg2) { - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedShortArrayElem1 + nestedShortArrayElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 2); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedShortArrayElem1 + nestedShortArrayElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray(short arg1, MemorySegment arg2) { - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 6); - short structElem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 6); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static MemorySegment add2ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, session); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - return shortStructSegmt; - } - - public static Addressable add2ShortStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - short shortStruct_Elem1 = (short)(arg1Addr.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1Addr.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - arg1Addr.set(JAVA_SHORT, 0, shortStruct_Elem1); - arg1Addr.set(JAVA_SHORT, 2, shortStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, session); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - short shortStruct_Elem3 = (short)(arg1.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 4)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - shortStructSegmt.set(JAVA_SHORT, 4, shortStruct_Elem3); - return shortStructSegmt; - } - - public static int addIntAndIntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAnd5IntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4) - + arg2.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 12) + arg2.get(JAVA_INT, 16); - return intSum; - } - - public static int addIntFromPointerAndIntsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static Addressable addIntFromPointerAndIntsFromStruct_returnIntPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - arg1Addr.set(JAVA_INT, 0, intSum); - return arg1Addr; - } - - public static int addIntAndIntsFromStructPointer(int arg1, MemoryAddress arg2Addr) { - int intSum = arg1 + arg2Addr.get(JAVA_INT, 0) + arg2Addr.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAndIntsFromNestedStruct(int arg1, MemorySegment arg2) { - int nestedStructElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromNestedStruct_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem1 = arg2.get(JAVA_INT, 4); - int nestedStructElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray(int arg1, MemorySegment arg2) { - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedIntArrayElem1 + nestedIntArrayElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 4); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedIntArrayElem1 + nestedIntArrayElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray(int arg1, MemorySegment arg2) { - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 12); - int structElem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 12); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static MemorySegment add2IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, session); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - return intStructSegmt; - } - - public static MemorySegment add2IntStructs_returnStruct_throwException(MemorySegment arg1, MemorySegment arg2) { - throw new IllegalArgumentException("An exception is thrown from the upcall method"); - } - - public static MemorySegment add2IntStructs_returnStruct_nestedUpcall(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_throwException, - FunctionDescriptor.of(structLayout, structLayout, structLayout), session); - MemorySegment resultSegmt = null; - try { - resultSegmt = (MemorySegment)mh.invoke(allocator, arg1, arg2, upcallFuncAddr); - } catch (Throwable e) { - throw (IllegalArgumentException)e; - } - return resultSegmt; - } - - public static MemorySegment add2IntStructs_returnStruct_nullValue(MemorySegment arg1, MemorySegment arg2) { - return null; - } - - public static MemorySegment add2IntStructs_returnStruct_heapSegmt(MemorySegment arg1, MemorySegment arg2) { - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - return MemorySegment.ofArray(new int[]{intStruct_Elem1, intStruct_Elem2}); - } - - public static Addressable add2IntStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum_Elem1 = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intSum_Elem2 = arg1Addr.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - arg1Addr.set(JAVA_INT, 0, intSum_Elem1); - arg1Addr.set(JAVA_INT, 4, intSum_Elem2); - return arg1Addr; - } - - public static Addressable add2IntStructs_returnStructPointer_nullValue(MemoryAddress arg1Addr, MemorySegment arg2) { - return null; - } - - public static Addressable add2IntStructs_returnStructPointer_nullAddr(MemoryAddress arg1Addr, MemorySegment arg2) { - return MemoryAddress.NULL; - } - - public static Addressable add2IntStructs_returnStructPointer_heapSegmt(MemoryAddress arg1Addr, MemorySegment arg2) { - int intSum_Elem1 = arg1Addr.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intSum_Elem2 = arg1Addr.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - MemorySegment resultSegmt = MemorySegment.ofArray(new int[]{intSum_Elem1, intSum_Elem2}); - return resultSegmt.address(); - } - - public static MemorySegment add3IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, session); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - int intStruct_Elem3 = arg1.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 8); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - intStructSegmt.set(JAVA_INT, 8, intStruct_Elem3); - return intStructSegmt; - } - - public static long addLongAndLongsFromStruct(long arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongFromPointerAndLongsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static Addressable addLongFromPointerAndLongsFromStruct_returnLongPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - arg1Addr.set(JAVA_LONG, 0, longSum); - return arg1Addr; - } - - public static long addLongAndLongsFromStructPointer(long arg1, MemoryAddress arg2Addr) { - long longSum = arg1 + arg2Addr.get(JAVA_LONG, 0) + arg2Addr.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongAndLongsFromNestedStruct(long arg1, MemorySegment arg2) { - long nestedStructElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromNestedStruct_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem1 = arg2.get(JAVA_LONG, 8); - long nestedStructElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray(long arg1, MemorySegment arg2) { - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedLongrrayElem1 + nestedLongrrayElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 8); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedLongrrayElem1 + nestedLongrrayElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray(long arg1, MemorySegment arg2) { - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 24); - long structElem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 24); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static MemorySegment add2LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, session); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - return longStructSegmt; - } - - public static Addressable add2LongStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - long longSum_Elem1 = arg1Addr.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longSum_Elem2 = arg1Addr.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - arg1Addr.set(JAVA_LONG, 0, longSum_Elem1); - arg1Addr.set(JAVA_LONG, 8, longSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, session); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - long longStruct_Elem3 = arg1.get(JAVA_LONG, 16) + arg2.get(JAVA_LONG, 16); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - longStructSegmt.set(JAVA_LONG, 16, longStruct_Elem3); - return longStructSegmt; - } - - public static float addFloatAndFloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAnd5FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12) + arg2.get(JAVA_FLOAT, 16); - return floatSum; - } - - public static float addFloatFromPointerAndFloatsFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static Addressable addFloatFromPointerAndFloatsFromStruct_returnFloatPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - arg1Addr.set(JAVA_FLOAT, 0, floatSum); - return arg1Addr; - } - - public static float addFloatAndFloatsFromStructPointer(float arg1, MemoryAddress arg2Addr) { - float floatSum = arg1 + arg2Addr.get(JAVA_FLOAT, 0) + arg2Addr.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct(float arg1, MemorySegment arg2) { - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray(float arg1, MemorySegment arg2) { - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedFloatArrayElem1 + nestedFloatArrayElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedFloatArrayElem1 + nestedFloatArrayElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray(float arg1, MemorySegment arg2) { - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 12); - float structElem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 12); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static MemorySegment add2FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, session); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - return floatStructSegmt; - } - - public static Addressable add2FloatStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - float floatSum_Elem1 = arg1Addr.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatSum_Elem2 = arg1Addr.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - arg1Addr.set(JAVA_FLOAT, 0, floatSum_Elem1); - arg1Addr.set(JAVA_FLOAT, 4, floatSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, session); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - float floatStruct_Elem3 = arg1.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 8); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - floatStructSegmt.set(JAVA_FLOAT, 8, floatStruct_Elem3); - return floatStructSegmt; - } - - public static double addDoubleAndDoublesFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleFromPointerAndDoublesFromStruct(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static Addressable addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - arg1Addr.set(JAVA_DOUBLE, 0, doubleSum); - return arg1Addr; - } - - public static double addDoubleAndDoublesFromStructPointer(double arg1, MemoryAddress arg2Addr) { - double doubleSum = arg1 + arg2Addr.get(JAVA_DOUBLE, 0) + arg2Addr.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct(double arg1, MemorySegment arg2) { - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray(double arg1, MemorySegment arg2) { - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray(double arg1, MemorySegment arg2) { - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 24); - double structElem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 24); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static MemorySegment add2DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, session); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - return doubleStructSegmt; - } - - public static Addressable add2DoubleStructs_returnStructPointer(MemoryAddress arg1Addr, MemorySegment arg2) { - double doubleSum_Elem1 = arg1Addr.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleSum_Elem2 = arg1Addr.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - arg1Addr.set(JAVA_DOUBLE, 0, doubleSum_Elem1); - arg1Addr.set(JAVA_DOUBLE, 8, doubleSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, session); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - double doubleStruct_Elem3 = arg1.get(JAVA_DOUBLE, 16) + arg2.get(JAVA_DOUBLE, 16); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - doubleStructSegmt.set(JAVA_DOUBLE, 16, doubleStruct_Elem3); - return doubleStructSegmt; - } - - public static int addIntAndIntShortFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_SHORT, 4); - return intSum; - } - - public static int addIntAndShortIntFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static long addIntAndIntLongFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addIntAndLongIntFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_INT, 8); - return longSum; - } - - public static double addDoubleAndIntDoubleFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_INT, 8); - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8); - return doubleSum; - } - - public static double addDoubleAnd2FloatsDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDouble2FloatsFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return doubleSum; - } - - public static float addFloatAndInt2FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static float addFloatAndFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float structElem2 = Integer.valueOf(arg2.get(JAVA_INT, 4)).floatValue(); - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + structElem2 + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static double addDoubleAndIntFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndFloatIntDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_INT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndLongDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static float addFloatAndInt3FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static long addLongAndLong2FloatsFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 8)).longValue(); - long structElem3 = Float.valueOf(arg2.get(JAVA_FLOAT, 12)).longValue(); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static float addFloatAnd3FloatsIntFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return floatSum; - } - - public static long addLongAndFloatLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2; - return longSum; - } - - public static double addDoubleAndDoubleFloatIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return doubleSum; - } - - public static double addDoubleAndDoubleLongFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_LONG, 8); - return doubleSum; - } - - public static long addLongAnd2FloatsLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 4)).longValue(); - long structElem3 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static short addShortAnd3ShortsCharFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_CHAR, 6)); - return shortSum; - } - - public static float addFloatAndIntFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4)+ arg2.get(JAVA_INT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static double addDoubleAndIntDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleIntFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleIntFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleLongFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The padding in the struct [int, double, long] on AIX/PPC 64-bit is different from - * other platforms as follows: - * 1) there is no padding between int and double. - * 2) there is a 4-byte padding between double and long. - */ - double structElem2 = (isAixOS) ? arg2.get(JAVA_DOUBLE.withBitAlignment(32), 4) : arg2.get(JAVA_DOUBLE, 8); - double structElem3 = arg2.get(JAVA_LONG, 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static MemorySegment return254BytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, session); - - for (int i = 0; i < 254; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } - - public static MemorySegment return4KBytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, session); - - for (int i = 0; i < 4096; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/ApiTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/ApiTests.java deleted file mode 100644 index 97f995031fa..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/ApiTests.java +++ /dev/null @@ -1,903 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.VarHandle; -import java.util.NoSuchElementException; - -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for the vararg list in VaList API specific cases. - */ -@Test(groups = { "level.sanity" }) -public class ApiTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isMacOsAarch64 = osName.contains("mac") && arch.contains("aarch64"); - private static boolean isSysVPPC64le = osName.contains("linux") && arch.contains("ppc64"); - - @Test - public void test_emptyVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList emptyVaList = VaList.empty(); - /* As specified in the implemention of OpenJDK, a NULL address is set to - * the empty va_list on Windows/x86_64, MacOS/Aarch64, Linux/ppc64le and - * AIX/ppc64 while the va_list without any argument is created on a fixed - * address on other platforms. - */ - if (isWinX64 || isMacOsAarch64 || isSysVPPC64le || isAixOS) { - Assert.assertEquals(emptyVaList.address(), MemoryAddress.NULL); - } else { - Assert.assertNotEquals(emptyVaList.address(), MemoryAddress.NULL); - } - } - } - - @Test - public void test_vaListSession() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 100), session); - MemorySession vaListSession = vaList.session(); - Assert.assertEquals(vaListSession, session); - } - } - - @Test - public void test_checkIntVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), session); - - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_checkLongVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), session); - - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_checkDoubleVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), session); - - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0.0001D); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0.0001D); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test - public void test_checkIntPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()), session); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_checkLongPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()), session); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_checkDoublePtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()), session); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_checkIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_checkLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_checkLongStructVaListFromPrefixAllocator() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1), session); - - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout, session); - MemorySegment argSegmt = vaList.nextVarg(structLayout, SegmentAllocator.prefixAllocator(structSegmt2)); - Assert.assertEquals(longHandle1.get(argSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - Assert.assertEquals(longHandle1.get(structSegmt2), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(structSegmt2), 6677889911L); /* the 2nd element of the 1st struct argument */ - } - } - - @Test - public void test_checkDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 111150.1001D); - doubleHandle2.set(structSegmt1, 111160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 111170.1001D); - doubleHandle2.set(structSegmt2, 111180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 111150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 111160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 111170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 111180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyIntVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_copyLongVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_copyDoubleVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0001D); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0001D); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0001D); /* the 3rd argument */ - } - } - - @Test - public void test_copyIntPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_copyLongPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_copyDoublePtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()), session); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt1.address()); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt2.address()); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_copyIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(resultArgSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(resultArgSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(resultArgSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(resultArgSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(resultArgSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(resultArgSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(resultArgSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(resultArgSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 111150.1001D); - doubleHandle2.set(structSegmt1, 111160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 111170.1001D); - doubleHandle2.set(structSegmt2, 111180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(resultArgSegmt), 111150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(resultArgSegmt), 111160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(resultArgSegmt), 111170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(resultArgSegmt), 111180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreNextArg_IntVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), session); - - vaList.nextVarg(JAVA_INT); /* the 1st argument */ - vaList.nextVarg(JAVA_INT); /* the 2nd argument */ - vaList.nextVarg(JAVA_INT); /* the 3rd argument */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.nextVarg(JAVA_INT); - fail("Failed to throw out NoSuchElementException when nextVarg() exceeds the memory region of VaList"); - } - } - - @Test - public void test_createIntVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_createLongVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_createDoubleVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0.0001D); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0.0001D); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test - public void test_createIntPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemoryAddress resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 700); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 800); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 900); /* the 3rd argument */ - } - } - - @Test - public void test_createLongPtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemoryAddress resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 700000L); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 800000L); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_createDoublePtrVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemoryAddress resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 111150.1001D, 0.0001D); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 111160.2002D, 0.0001D); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - Assert.assertEquals(resultAddr.get(JAVA_DOUBLE, 0), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreNextArg_IntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - - vaList.nextVarg(structLayout, allocator); /* the 1st struct argument */ - vaList.nextVarg(structLayout, allocator); /* the 2nd struct argument */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.nextVarg(structLayout, allocator); - fail("Failed to throw out NoSuchElementException when nextVarg() exceeds the memory region of VaList"); - } - } - - @Test - public void test_createIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(newArgSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(newArgSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(newArgSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(newArgSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_createLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(newArgSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(newArgSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(newArgSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(newArgSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_createDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - VaList newVaList = VaList.ofAddress(vaList.address(), session); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(newArgSegmt), 11150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(newArgSegmt), 11160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(newArgSegmt), 11170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(newArgSegmt), 11180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreSkippedArg_IntArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), session); - vaList.skip(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); /* Skip over all arguments in VaList */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.skip(JAVA_INT); - fail("Failed to throw out NoSuchElementException when skip() exceeds the memory region of VaList"); - } - } - - @Test - public void test_skipIntArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), session); - vaList.skip(JAVA_INT); - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - vaList.skip(JAVA_INT); - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 1000); /* the 4th argument */ - } - } - - @Test - public void test_skipLongArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), session); - vaList.skip(JAVA_LONG, JAVA_LONG); - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_skipDoubleArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D) - .addVarg(JAVA_DOUBLE, 111180.2002D), session); - vaList.skip(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111180.2002D, 0.0001D); /* the 4th argument */ - } - } - - @Test - public void test_skipIntPtrArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - MemorySegment intSegmt4 = allocator.allocate(JAVA_INT, 1000); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()) - .addVarg(ADDRESS, intSegmt4.address()), session); - vaList.skip(ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt2.address()); /* the 2nd argument */ - } - } - - @Test - public void test_skipLongPtrArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - MemorySegment longSegmt4 = allocator.allocate(JAVA_LONG, 1000000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()) - .addVarg(ADDRESS, longSegmt4.address()), session); - vaList.skip(ADDRESS, ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt3.address()); /* the 3rd argument */ - } - } - - @Test - public void test_skipDoublePtrArgOfVaList() throws Throwable { - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - MemorySegment doubleSegmt4 = allocator.allocate(JAVA_DOUBLE, 111180.1002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()) - .addVarg(ADDRESS, doubleSegmt4.address()), session); - vaList.skip(ADDRESS, ADDRESS, ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt4.address()); /* the 4th argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreSkippedArg_IntStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - vaList.skip(structLayout, structLayout); /* Skip over all arguments in VaList */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.skip(structLayout); - fail("Failed to throw out NoSuchElementException when skip() exceeds the memory region of VaList"); - } - } - - @Test - public void test_skipIntStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_skipLongStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_skipDoubleStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 11170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 11180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/DowncallTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/DowncallTests.java deleted file mode 100644 index 551bd8ffaad..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/DowncallTests.java +++ /dev/null @@ -1,1090 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for the vararg list in downcall. - */ -@Test(groups = { "level.sanity" }) -public class DowncallTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxX64 = osName.contains("linux") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxAarch64 = osName.contains("linux") && arch.equals("aarch64"); - /* The padding of struct is not required on Power in terms of VaList */ - private static boolean isStructPaddingNotRequired = arch.startsWith("ppc64"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addIntsFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addIntsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), session); - int result = (int)mh.invoke(4, vaList); - Assert.assertEquals(result, 3400); - } - } - - @Test - public void test_addLongsFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addLongsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), session); - long result = (long)mh.invoke(4, vaList); - Assert.assertEquals(result, 3400000L); - } - } - - @Test - public void test_addDoublesFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addDoublesFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 150.1001D) - .addVarg(JAVA_DOUBLE, 160.2002D) - .addVarg(JAVA_DOUBLE, 170.1001D) - .addVarg(JAVA_DOUBLE, 180.2002D), session); - double result = (double)mh.invoke(4, vaList); - Assert.assertEquals(result, 660.6006D, 0.0001D); - } - } - - @Test - public void test_addMixedArgsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting this struct with - * the mixed elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - Addressable functionSymbol = nativeLibLookup.lookup("addMixedArgsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_DOUBLE, 160.2002D), session); - double result = (double)mh.invoke(vaList); - Assert.assertEquals(result, 800860.2002D, 0.0001D); - } - } - } - - @Test - public void test_addMoreMixedArgsFromVaList() throws Throwable { - /* VaList on Linux/x86_64 and Windows/x86_64 in OpenJDK is unable to handle - * the va_list with over 8 arguments (confirmed by OpenJDK/Hotspot). So the - * test is disabled for now till the issue is fixed by OpenJDK. - */ - if (!isLinuxX64 && !isWinX64) { - Addressable functionSymbol = nativeLibLookup.lookup("addMoreMixedArgsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 100) - .addVarg(JAVA_LONG, 200000L) - .addVarg(JAVA_INT, 300) - .addVarg(JAVA_LONG, 400000L) - .addVarg(JAVA_INT, 500) - .addVarg(JAVA_LONG, 600000L) - .addVarg(JAVA_INT, 700) - .addVarg(JAVA_DOUBLE, 161.2001D) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_DOUBLE, 162.2002D) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_DOUBLE, 163.2003D) - .addVarg(JAVA_INT, 1000) - .addVarg(JAVA_DOUBLE, 164.2004D) - .addVarg(JAVA_INT, 1100) - .addVarg(JAVA_DOUBLE, 165.2005D), session); - double result = (double)mh.invoke(vaList); - Assert.assertEquals(result, 1206216.0015D, 0.0001D); - } - } - } - - @Test - public void test_addIntsByPtrFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addIntsByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()), session); - int result = (int)mh.invoke(3, vaList); - Assert.assertEquals(result, 2400); - } - } - - @Test - public void test_addLongsByPtrFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addLongsByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()), session); - long result = (long)mh.invoke(3, vaList); - Assert.assertEquals(result, 2400000L); - } - } - - @Test - public void test_addDoublesByPtrFromVaList() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addDoublesByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()), session); - double result = (double)mh.invoke(3, vaList); - Assert.assertEquals(result, 333480.4004D, 0.0001D); - } - } - - @Test - public void test_add1ByteOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1ByteOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)2); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt3, (byte)3); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt4, (byte)4); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt5, (byte)5); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt6, (byte)6); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt7, (byte)7); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt8, (byte)8); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt9, (byte)9); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt10, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - byte result = (byte)mh.invoke(10, vaList); - Assert.assertEquals(result, 55); - } - } - } - - @Test - public void test_add2BytesOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two byte elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)3); - byteHandle2.set(structSegmt2, (byte)4); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - byte result = (byte)mh.invoke(2, vaList); - Assert.assertEquals(result, 10); - } - } - } - - @Test - public void test_add3BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)4); - byteHandle2.set(structSegmt2, (byte)5); - byteHandle3.set(structSegmt2, (byte)6); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - byte result = (byte)mh.invoke(2, vaList); - Assert.assertEquals(result, 21); - } - } - - @Test - public void test_add5BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - Addressable functionSymbol = nativeLibLookup.lookup("add5BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)6); - byteHandle2.set(structSegmt2, (byte)7); - byteHandle3.set(structSegmt2, (byte)8); - byteHandle4.set(structSegmt2, (byte)9); - byteHandle5.set(structSegmt2, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - byte result = (byte)mh.invoke(2, vaList); - Assert.assertEquals(result, 55); - } - } - - @Test - public void test_add7BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), - JAVA_BYTE.withName("elem5"), JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle byteHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle byteHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - Addressable functionSymbol = nativeLibLookup.lookup("add7BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - byteHandle6.set(structSegmt1, (byte)6); - byteHandle7.set(structSegmt1, (byte)7); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)8); - byteHandle2.set(structSegmt2, (byte)9); - byteHandle3.set(structSegmt2, (byte)10); - byteHandle4.set(structSegmt2, (byte)11); - byteHandle5.set(structSegmt2, (byte)12); - byteHandle6.set(structSegmt2, (byte)13); - byteHandle7.set(structSegmt2, (byte)14); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - byte result = (byte)mh.invoke(2, vaList); - Assert.assertEquals(result, 105); - } - } - - @Test - public void test_add1ShortOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1ShortOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt3, (short)333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt4, (short)444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt5, (short)555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt6, (short)666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt7, (short)777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt8, (short)888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt9, (short)999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt10, (short)123); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - short result = (short)mh.invoke(10, vaList); - Assert.assertEquals(result, 5118); - } - } - } - - @Test - public void test_add2ShortsOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two short elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)333); - shortHandle2.set(structSegmt2, (short)444); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - short result = (short)mh.invoke(2, vaList); - Assert.assertEquals(result, 1110); - } - } - } - - @Test - public void test_add3ShortsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3ShortsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - shortHandle3.set(structSegmt1, (short)333); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)444); - shortHandle2.set(structSegmt2, (short)555); - shortHandle3.set(structSegmt2, (short)666); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - short result = (short)mh.invoke(2, vaList); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add1IntOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1IntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - intHandle1.set(structSegmt3, 3333333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - intHandle1.set(structSegmt4, 4444444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - intHandle1.set(structSegmt5, 5555555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - intHandle1.set(structSegmt6, 6666666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - intHandle1.set(structSegmt7, 7777777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - intHandle1.set(structSegmt8, 8888888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - intHandle1.set(structSegmt9, 9999999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - intHandle1.set(structSegmt10, 1234567); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - int result = (int)mh.invoke(10, vaList); - Assert.assertEquals(result, 51234562); - } - } - } - - @Test - public void test_add2IntsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - int result = (int)mh.invoke(2, vaList); - Assert.assertEquals(result, 8956224); - } - } - - @Test - public void test_add3IntsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3IntsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - intHandle3.set(structSegmt1, 7788999); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 1133555); - intHandle2.set(structSegmt2, 2244666); - intHandle3.set(structSegmt2, 3322111); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - int result = (int)mh.invoke(2, vaList); - Assert.assertEquals(result, 20067330); - } - } - - @Test - public void test_add2LongsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2LongsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - long result = (long)mh.invoke(2, vaList); - Assert.assertEquals(result, 17822661054L); - } - } - - @Test - public void test_add1FloatOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with only - * one float element (confirmed by OpenJDK/Hotspot). Thus, the test is disabled on - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1FloatOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 3.33F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 4.44F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 5.56F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), session); - float result = (float)mh.invoke(5, vaList); - Assert.assertEquals(result, 16.66F, 0.01F); - } - } - } - - @Test - public void test_add2FloatsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 3.33F); - floatHandle2.set(structSegmt2, 4.44F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 5.55F); - floatHandle2.set(structSegmt3, 6.66F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 7.77F); - floatHandle2.set(structSegmt4, 8.88F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.99F); - floatHandle2.set(structSegmt5, 1.23F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 4.56F); - floatHandle2.set(structSegmt6, 7.89F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), session); - float result = (float)mh.invoke(6, vaList); - Assert.assertEquals(result, 63.63F, 0.01F); - } - } - - @Test - public void test_add3FloatsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), MemoryLayout.paddingLayout(32)); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3FloatsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - floatHandle3.set(structSegmt1, 3.33F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 4.44F); - floatHandle2.set(structSegmt2, 5.55F); - floatHandle3.set(structSegmt2, 6.66F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 7.77F); - floatHandle2.set(structSegmt3, 8.88F); - floatHandle3.set(structSegmt3, 9.99F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 1.23F); - floatHandle2.set(structSegmt4, 4.56F); - floatHandle3.set(structSegmt4, 7.89F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.87F); - floatHandle2.set(structSegmt5, 6.54F); - floatHandle3.set(structSegmt5, 3.21F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 2.46F); - floatHandle2.set(structSegmt6, 8.13F); - floatHandle3.set(structSegmt6, 5.79F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), session); - float result = (float)mh.invoke(6, vaList); - Assert.assertEquals(result, 99.63F, 0.01F); - } - } - - @Test - public void test_add1DoubleOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1DoubleOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 11111.1001D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 11111.1002D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 11111.1003D); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 11111.1004D); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 11111.1005D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), session); - double result = (double)mh.invoke(5, vaList); - Assert.assertEquals(result, 55555.5015D, 0.0001D); - } - } - - @Test - public void test_add2DoublesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2DoublesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - double result = (double)mh.invoke(2, vaList); - Assert.assertEquals(result, 44660.6006D, 0.0001D); - } - } - - @Test - public void test_vprintfFromDefaultLibFromVaList() throws Throwable { - /* 1) Disable the test on Windows given a misaligned access exception coming from - * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString() - * is also captured on OpenJDK/Hotspot. - * 2) Disable the test on AIX as Valist is not yet implemented in OpenJDK. - */ - if (!isWinX64 && !isAixOS) { - Addressable functionSymbol = defaultLibLookup.lookup("vprintf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment formatSegmt = allocator.allocateUtf8String("%d * %d = %d\n"); - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 7) - .addVarg(JAVA_INT, 8) - .addVarg(JAVA_INT, 56), session); - mh.invoke(formatSegmt, vaList); - } - } - } - - @Test - public void test_addIntShortOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addIntShortOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, (short)123); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, (short)456); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, (short)789); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - int result = (int)mh.invoke(3, vaList); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addShortIntOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addShortIntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, (short)123); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, (short)456); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, (short)789); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - int result = (int)mh.invoke(3, vaList); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addIntLongOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addIntLongOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, 101010101010L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, 202020202020L); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, 303030303030L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - long result = (long)mh.invoke(3, vaList); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addLongIntOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addLongIntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 101010101010L); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 202020202020L); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 303030303030L); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - long result = (long)mh.invoke(3, vaList); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addFloatDoubleOfStructsFromVaList() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addFloatDoubleOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1.11F); - elemHandle2.set(structSegmt1, 222.222D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2.22F); - elemHandle2.set(structSegmt2, 333.333D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3.33F); - elemHandle2.set(structSegmt3, 444.444D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - double result = (double)mh.invoke(3, vaList); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } - - @Test - public void test_addDoubleFloatOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFloatOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 222.222D); - elemHandle2.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 333.333D); - elemHandle2.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 444.444D); - elemHandle2.set(structSegmt3, 3.33F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - double result = (double)mh.invoke(3, vaList); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/UpcallTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/UpcallTests.java deleted file mode 100644 index c4cf9819c10..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/UpcallTests.java +++ /dev/null @@ -1,1152 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Addressable; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 424: Foreign Linker API (Preview) for the vararg list in upcall. - */ -@Test(groups = { "level.sanity" }) -public class UpcallTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxX64 = osName.contains("linux") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxAarch64 = osName.contains("linux") && arch.equals("aarch64"); - /* The padding of struct is not required on Power in terms of VaList */ - private static boolean isStructPaddingNotRequired = arch.startsWith("ppc64"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addIntsFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addIntsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(4, vaList, upcallFuncAddr); - Assert.assertEquals(result, 3400); - } - } - - @Test - public void test_addLongsFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addLongsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), session); - - long result = (long)mh.invoke(4, vaList, upcallFuncAddr); - Assert.assertEquals(result, 3400000L); - } - } - - @Test - public void test_addDoublesFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addDoublesFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D) - .addVarg(JAVA_DOUBLE, 111180.2002D), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoublesFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(4, vaList, upcallFuncAddr); - Assert.assertEquals(result, 444660.6006D, 0.0001D); - } - } - - @Test - public void test_addMixedArgsFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addMixedArgsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_DOUBLE, 160.2002D), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addMixedArgsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS), session); - - double result = (double)mh.invoke(vaList, upcallFuncAddr); - Assert.assertEquals(result, 800860.2002D, 0.0001D); - } - } - - @Test - public void test_addMoreMixedArgsFromVaListByUpcallMH() throws Throwable { - /* VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). So the test is disabled - * for now till the issue is fixed by OpenJDK on Linux/x86_64. - */ - if (!isLinuxX64) { - Addressable functionSymbol = nativeLibLookup.lookup("addMoreMixedArgsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 100) - .addVarg(JAVA_LONG, 200000L) - .addVarg(JAVA_INT, 300) - .addVarg(JAVA_LONG, 400000L) - .addVarg(JAVA_INT, 500) - .addVarg(JAVA_LONG, 600000L) - .addVarg(JAVA_INT, 700) - .addVarg(JAVA_DOUBLE, 161.2001D) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_DOUBLE, 162.2002D) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_DOUBLE, 163.2003D) - .addVarg(JAVA_INT, 1000) - .addVarg(JAVA_DOUBLE, 164.2004D) - .addVarg(JAVA_INT, 1100) - .addVarg(JAVA_DOUBLE, 165.2005D), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addMoreMixedArgsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS), session); - - double result = (double)mh.invoke(vaList, upcallFuncAddr); - Assert.assertEquals(result, 1206216.0015D, 0.0001D); - } - } - } - - @Test - public void test_addIntsByPtrFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addIntsByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1.address()) - .addVarg(ADDRESS, intSegmt2.address()) - .addVarg(ADDRESS, intSegmt3.address()), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntsByPtrFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 2400); - } - } - - @Test - public void test_addLongsByPtrFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addLongsByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1.address()) - .addVarg(ADDRESS, longSegmt2.address()) - .addVarg(ADDRESS, longSegmt3.address()), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongsByPtrFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), session); - - long result = (long)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 2400000L); - } - } - - @Test - public void test_addDoublesByPtrFromVaListByUpcallMH() throws Throwable { - Addressable functionSymbol = nativeLibLookup.lookup("addDoublesByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1.address()) - .addVarg(ADDRESS, doubleSegmt2.address()) - .addVarg(ADDRESS, doubleSegmt3.address()), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoublesByPtrFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 480.4004D, 0.0001D); - } - } - - @Test - public void test_add1ByteOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1ByteOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)2); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt3, (byte)3); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt4, (byte)4); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt5, (byte)5); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt6, (byte)6); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt7, (byte)7); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt8, (byte)8); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt9, (byte)9); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt10, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1ByteOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), session); - - byte result = (byte)mh.invoke(10, vaList, upcallFuncAddr); - Assert.assertEquals(result, 55); - } - } - } - - @Test - public void test_add2BytesOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two byte elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)3); - byteHandle2.set(structSegmt2, (byte)4); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), session); - - byte result = (byte)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 10); - } - } - } - - @Test - public void test_add3BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)4); - byteHandle2.set(structSegmt2, (byte)5); - byteHandle3.set(structSegmt2, (byte)6); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), session); - - byte result = (byte)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 21); - } - } - - @Test - public void test_add5BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - Addressable functionSymbol = nativeLibLookup.lookup("add5BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)6); - byteHandle2.set(structSegmt2, (byte)7); - byteHandle3.set(structSegmt2, (byte)8); - byteHandle4.set(structSegmt2, (byte)9); - byteHandle5.set(structSegmt2, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add5BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), session); - - byte result = (byte)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 55); - } - } - - @Test - public void test_add7BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), - JAVA_BYTE.withName("elem5"), JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle byteHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle byteHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - Addressable functionSymbol = nativeLibLookup.lookup("add7BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - byteHandle6.set(structSegmt1, (byte)6); - byteHandle7.set(structSegmt1, (byte)7); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)8); - byteHandle2.set(structSegmt2, (byte)9); - byteHandle3.set(structSegmt2, (byte)10); - byteHandle4.set(structSegmt2, (byte)11); - byteHandle5.set(structSegmt2, (byte)12); - byteHandle6.set(structSegmt2, (byte)13); - byteHandle7.set(structSegmt2, (byte)14); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add7BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), session); - - byte result = (byte)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 105); - } - } - - @Test - public void test_add1ShortOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1ShortOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt3, (short)333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt4, (short)444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt5, (short)555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt6, (short)666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt7, (short)777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt8, (short)888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt9, (short)999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt10, (short)123); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1ShortOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), session); - - short result = (short)mh.invoke(10, vaList, upcallFuncAddr); - Assert.assertEquals(result, 5118); - } - } - } - - @Test - public void test_add2ShortsOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two short elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2ShortsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)333); - shortHandle2.set(structSegmt2, (short)444); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2ShortsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), session); - - short result = (short)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 1110); - } - } - } - - @Test - public void test_add3ShortsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3ShortsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - shortHandle3.set(structSegmt1, (short)333); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)444); - shortHandle2.set(structSegmt2, (short)555); - shortHandle3.set(structSegmt2, (short)666); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3ShortsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), session); - - short result = (short)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add1IntOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1IntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - intHandle1.set(structSegmt3, 3333333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - intHandle1.set(structSegmt4, 4444444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - intHandle1.set(structSegmt5, 5555555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - intHandle1.set(structSegmt6, 6666666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - intHandle1.set(structSegmt7, 7777777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - intHandle1.set(structSegmt8, 8888888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - intHandle1.set(structSegmt9, 9999999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - intHandle1.set(structSegmt10, 1234567); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1IntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(10, vaList, upcallFuncAddr); - Assert.assertEquals(result, 51234562); - } - } - } - - @Test - public void test_add2IntsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2IntsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2IntsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 8956224); - } - } - - @Test - public void test_add3IntsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3IntsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - intHandle3.set(structSegmt1, 7788999); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 1133555); - intHandle2.set(structSegmt2, 2244666); - intHandle3.set(structSegmt2, 3322111); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3IntsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 20067330); - } - } - - @Test - public void test_add2LongsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2LongsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2LongsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), session); - - long result = (long)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 17822661054L); - } - } - - @Test - public void test_add1FloatOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with only - * one float element (confirmed by OpenJDK/Hotspot). Thus, the test is disabled on - * Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1FloatOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 3.33F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 4.44F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 5.56F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1FloatOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), session); - - float result = (float)mh.invoke(5, vaList, upcallFuncAddr); - Assert.assertEquals(result, 16.66F, 0.01F); - } - } - } - - @Test - public void test_add2FloatsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2FloatsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 3.33F); - floatHandle2.set(structSegmt2, 4.44F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 5.55F); - floatHandle2.set(structSegmt3, 6.66F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 7.77F); - floatHandle2.set(structSegmt4, 8.88F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.99F); - floatHandle2.set(structSegmt5, 1.23F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 4.56F); - floatHandle2.set(structSegmt6, 7.89F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2FloatsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), session); - - float result = (float)mh.invoke(6, vaList, upcallFuncAddr); - Assert.assertEquals(result, 63.63F, 0.01F); - } - } - - @Test - public void test_add3FloatsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), MemoryLayout.paddingLayout(32)); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - Addressable functionSymbol = nativeLibLookup.lookup("add3FloatsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - floatHandle3.set(structSegmt1, 3.33F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 4.44F); - floatHandle2.set(structSegmt2, 5.55F); - floatHandle3.set(structSegmt2, 6.66F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 7.77F); - floatHandle2.set(structSegmt3, 8.88F); - floatHandle3.set(structSegmt3, 9.99F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 1.23F); - floatHandle2.set(structSegmt4, 4.56F); - floatHandle3.set(structSegmt4, 7.89F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.87F); - floatHandle2.set(structSegmt5, 6.54F); - floatHandle3.set(structSegmt5, 3.21F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 2.46F); - floatHandle2.set(structSegmt6, 8.13F); - floatHandle3.set(structSegmt6, 5.79F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3FloatsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), session); - - float result = (float)mh.invoke(6, vaList, upcallFuncAddr); - Assert.assertEquals(result, 99.63F, 0.01F); - } - } - - @Test - public void test_add1DoubleOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - Addressable functionSymbol = nativeLibLookup.lookup("add1DoubleOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11111.1001D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11111.1002D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt3, 11111.1003D); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt4, 11111.1004D); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt5, 11111.1005D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1DoubleOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(5, vaList, upcallFuncAddr); - Assert.assertEquals(result, 55555.5015D, 0.0001D); - } - } - - @Test - public void test_add2DoublesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("add2DoublesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2DoublesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(2, vaList, upcallFuncAddr); - Assert.assertEquals(result, 44660.6006D, 0.0001D); - } - } - - @Test - public void test_addIntShortOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addIntShortOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, (short)123); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, (short)456); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, (short)789); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntShortOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addShortIntOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addShortIntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, (short)123); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, (short)456); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, (short)789); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addShortIntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), session); - - int result = (int)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addIntLongOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addIntLongOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, 101010101010L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, 202020202020L); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, 303030303030L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntLongOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), session); - - long result = (long)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addLongIntOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addLongIntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 101010101010L); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 202020202020L); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 303030303030L); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongIntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), session); - - long result = (long)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addFloatDoubleOfStructsFromVaListByUpcallMH() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addFloatDoubleOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1.11F); - elemHandle2.set(structSegmt1, 222.222D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2.22F); - elemHandle2.set(structSegmt2, 333.333D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3.33F); - elemHandle2.set(structSegmt3, 444.444D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addFloatDoubleOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } - - @Test - public void test_addDoubleFloatOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFloatOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (MemorySession session = MemorySession.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 222.222D); - elemHandle2.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 333.333D); - elemHandle2.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 444.444D); - elemHandle2.set(structSegmt3, 3.33F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), session); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoubleFloatOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), session); - - double result = (double)mh.invoke(3, vaList, upcallFuncAddr); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/VaListUpcallMethodHandles.java b/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/VaListUpcallMethodHandles.java deleted file mode 100644 index e3456e1d9d0..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep424/valist/VaListUpcallMethodHandles.java +++ /dev/null @@ -1,589 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep424.valist; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; - -import static java.lang.foreign.Linker.*; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryAddress; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; -import java.lang.foreign.VaList; - -/** - * The helper class that contains all upcall method handles with VaList as arguments - * - * Note: VaList is simply treated as a pointer (specified in OpenJDK) in java - * when va_list is passed as argument in native. - */ -public class VaListUpcallMethodHandles { - private static final Lookup lookup = MethodHandles.lookup(); - private static MemorySession session = MemorySession.openImplicit(); - private static SegmentAllocator allocator = SegmentAllocator.newNativeArena(session); - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - - static final MethodType MT_Byte_Int_MemAddr = methodType(byte.class, int.class, MemoryAddress.class); - static final MethodType MT_Short_Int_MemAddr = methodType(short.class, int.class, MemoryAddress.class); - static final MethodType MT_Int_Int_MemAddr = methodType(int.class, int.class, MemoryAddress.class); - static final MethodType MT_Long_Int_MemAddr = methodType(long.class, int.class, MemoryAddress.class); - static final MethodType MT_Float_Int_MemAddr = methodType(float.class, int.class, MemoryAddress.class); - static final MethodType MT_Double_Int_MemAddr = methodType(double.class, int.class, MemoryAddress.class); - - public static final MethodHandle MH_addIntsFromVaList; - public static final MethodHandle MH_addLongsFromVaList; - public static final MethodHandle MH_addDoublesFromVaList; - public static final MethodHandle MH_addMixedArgsFromVaList; - public static final MethodHandle MH_addMoreMixedArgsFromVaList; - public static final MethodHandle MH_addIntsByPtrFromVaList; - public static final MethodHandle MH_addLongsByPtrFromVaList; - public static final MethodHandle MH_addDoublesByPtrFromVaList; - public static final MethodHandle MH_add1ByteOfStructsFromVaList; - public static final MethodHandle MH_add2BytesOfStructsFromVaList; - public static final MethodHandle MH_add3BytesOfStructsFromVaList; - public static final MethodHandle MH_add5BytesOfStructsFromVaList; - public static final MethodHandle MH_add7BytesOfStructsFromVaList; - public static final MethodHandle MH_add1ShortOfStructsFromVaList; - public static final MethodHandle MH_add2ShortsOfStructsFromVaList; - public static final MethodHandle MH_add3ShortsOfStructsFromVaList; - public static final MethodHandle MH_add1IntOfStructsFromVaList; - public static final MethodHandle MH_add2IntsOfStructsFromVaList; - public static final MethodHandle MH_add3IntsOfStructsFromVaList; - public static final MethodHandle MH_add2LongsOfStructsFromVaList; - public static final MethodHandle MH_add1FloatOfStructsFromVaList; - public static final MethodHandle MH_add2FloatsOfStructsFromVaList; - public static final MethodHandle MH_add3FloatsOfStructsFromVaList; - public static final MethodHandle MH_add1DoubleOfStructsFromVaList; - public static final MethodHandle MH_add2DoublesOfStructsFromVaList; - public static final MethodHandle MH_addIntShortOfStructsFromVaList; - public static final MethodHandle MH_addShortIntOfStructsFromVaList; - public static final MethodHandle MH_addIntLongOfStructsFromVaList; - public static final MethodHandle MH_addLongIntOfStructsFromVaList; - public static final MethodHandle MH_addFloatDoubleOfStructsFromVaList; - public static final MethodHandle MH_addDoubleFloatOfStructsFromVaList; - - static { - try { - MH_addIntsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addLongsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addDoublesFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoublesFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addMixedArgsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addMixedArgsFromVaList", methodType(double.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addMoreMixedArgsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addMoreMixedArgsFromVaList", methodType(double.class, MemoryAddress.class)); //$NON-NLS-1$ - MH_addIntsByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntsByPtrFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addLongsByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongsByPtrFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addDoublesByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoublesByPtrFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_add1ByteOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1ByteOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add2BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add3BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add5BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add5BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add7BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add7BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add1ShortOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1ShortOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add2ShortsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2ShortsOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add3ShortsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3ShortsOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add1IntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1IntOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add2IntsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2IntsOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add3IntsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3IntsOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add2LongsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2LongsOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_add1FloatOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1FloatOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add2FloatsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2FloatsOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add3FloatsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3FloatsOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add1DoubleOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1DoubleOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_add2DoublesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2DoublesOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addIntShortOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntShortOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addShortIntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addShortIntOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addIntLongOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntLongOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addLongIntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongIntOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addFloatDoubleOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addFloatDoubleOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addDoubleFloatOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoubleFloatOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - - } catch (IllegalAccessException | NoSuchMethodException e) { - throw new InternalError(e); - } - } - - public static int addIntsFromVaList(int argCount, MemoryAddress intVaListAddr) { - VaList intVaList = VaList.ofAddress(intVaListAddr, session); - int intSum = 0; - while (argCount > 0) { - intSum += intVaList.nextVarg(JAVA_INT); - argCount--; - } - return intSum; - } - - public static long addLongsFromVaList(int argCount, MemoryAddress longVaListAddr) { - VaList longVaList = VaList.ofAddress(longVaListAddr, session); - long longSum = 0; - while (argCount > 0) { - longSum += longVaList.nextVarg(JAVA_LONG); - argCount--; - } - return longSum; - } - - public static double addDoublesFromVaList(int argCount, MemoryAddress doubleVaListAddr) { - VaList doubleVaList = VaList.ofAddress(doubleVaListAddr, session); - double doubleSum = 0; - while (argCount > 0) { - doubleSum += doubleVaList.nextVarg(JAVA_DOUBLE); - argCount--; - } - return doubleSum; - } - - public static double addMixedArgsFromVaList(MemoryAddress argVaListAddr) { - VaList argVaList = VaList.ofAddress(argVaListAddr, session); - double doubleSum = argVaList.nextVarg(JAVA_INT) - + argVaList.nextVarg(JAVA_LONG) + argVaList.nextVarg(JAVA_DOUBLE); - return doubleSum; - } - - public static double addMoreMixedArgsFromVaList(MemoryAddress argVaListAddr) { - VaList argVaList = VaList.ofAddress(argVaListAddr, session); - double doubleSum = argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE); - return doubleSum; - } - - public static int addIntsByPtrFromVaList(int argCount, MemoryAddress ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr, session); - int intSum = 0; - while (argCount > 0) { - intSum += ptrVaList.nextVarg(ADDRESS).get(JAVA_INT, 0); - argCount--; - } - return intSum; - } - - public static long addLongsByPtrFromVaList(int argCount, MemoryAddress ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr, session); - long longSum = 0; - while (argCount > 0) { - longSum += ptrVaList.nextVarg(ADDRESS).get(JAVA_LONG, 0); - argCount--; - } - return longSum; - } - - public static double addDoublesByPtrFromVaList(int argCount, MemoryAddress ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr, session); - double doubleSum = 0; - while (argCount > 0) { - doubleSum += ptrVaList.nextVarg(ADDRESS).get(JAVA_DOUBLE, 0); - argCount--; - } - return doubleSum; - } - - public static byte add1ByteOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add2BytesOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add3BytesOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) + (byte)elemHandle3.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add5BytesOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), - JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle elemHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) - + (byte)elemHandle3.get(argSegmt) + (byte)elemHandle4.get(argSegmt) - + (byte)elemHandle5.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add7BytesOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), - JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5"), - JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle elemHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle elemHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle elemHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) - + (byte)elemHandle3.get(argSegmt) + (byte)elemHandle4.get(argSegmt) - + (byte)elemHandle5.get(argSegmt) + (byte)elemHandle6.get(argSegmt) - + (byte)elemHandle7.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static short add1ShortOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static short add2ShortsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static short add3ShortsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt) + (short)elemHandle3.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static int add1IntOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int add2IntsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int add3IntsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt) + (int)elemHandle3.get(argSegmt); - argCount--; - } - return intSum; - } - - public static long add2LongsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (long)elemHandle1.get(argSegmt) + (long)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static float add1FloatOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static float add2FloatsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static float add3FloatsOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt) + (float)elemHandle3.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static double add1DoubleOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static double add2DoublesOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt) + (double)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static int addIntShortOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int addShortIntOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (short)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static long addLongIntOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (long)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static long addIntLongOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (int)elemHandle1.get(argSegmt) + (long)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static double addFloatDoubleOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2").withBitAlignment(32)) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (float)elemHandle1.get(argSegmt) + (double)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static double addDoubleFloatOfStructsFromVaList(int argCount, MemoryAddress struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr, session); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } -} diff --git a/test/functional/Java19andUp/src/org/openj9/test/jep425/VirtualThreadTests.java b/test/functional/Java19andUp/src/org/openj9/test/jep425/VirtualThreadTests.java deleted file mode 100644 index 409fdd5b422..00000000000 --- a/test/functional/Java19andUp/src/org/openj9/test/jep425/VirtualThreadTests.java +++ /dev/null @@ -1,333 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2022 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep425; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.Thread; -import java.lang.reflect.*; -import java.time.Duration; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.LockSupport; -import java.util.stream.IntStream; - -/** - * Test cases for JEP 425: Virtual Threads (Preview) Continuation execution - * which verifies the basic cases including Continuation enter, yield, resume. - */ -@Test(groups = { "level.sanity" }) -public class VirtualThreadTests { - static { - try { - System.loadLibrary("j9ben"); - } catch (UnsatisfiedLinkError e) { - System.out.println("No natives for JNI tests"); - } - } - - public static native boolean lockSupportPark(); - - @Test - public void test_basicVirtualthread() { - var wrapper = new Object(){ boolean executed = false; }; - try { - Thread t = Thread.ofVirtual().name("duke").unstarted(() -> { - wrapper.executed = true; - }); - - t.start(); - t.join(); - - AssertJUnit.assertTrue("Virtual Thread operation not executed", wrapper.executed); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - @Test - public void test_VirtualthreadYieldResume() { - try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { - int numThreads = 6; - int expectedThreadResult = 3; - long estimatedThreadCompletionTime = 5L; /* seconds */ - int[] results = new int[numThreads]; - - IntStream.range(0, numThreads).forEach(i -> { - executor.submit(() -> { - results[i] = 1; - Thread.sleep(Duration.ofSeconds(1)); - results[i] += 1; - Thread.sleep(Duration.ofSeconds(1)); - results[i] += 1; - return i; - }); - }); - - /* Wait incrementally for the worst-case scenario where all virtual threads are - * executed sequentially. Exit the wait loop if the virtual threads finish early. - */ - for (int i = 0; i < numThreads; i++) { - executor.awaitTermination(estimatedThreadCompletionTime, TimeUnit.SECONDS); - boolean exit = true; - for (int j = 0; j < numThreads; j++) { - if (results[j] != expectedThreadResult) { - exit = false; - } - } - if (exit) { - break; - } - } - - for (int i = 0; i < numThreads; i++) { - AssertJUnit.assertTrue( - "Virtual Thread " + i + ": incorrect result of " + results[i], - (results[i] == expectedThreadResult)); - } - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testSyncThread_ready = false; - - @Test - public void test_synchronizedBlockFromVirtualthread() { - try { - Thread t = Thread.ofVirtual().name("synchronized").unstarted(() -> { - synchronized(VirtualThreadTests.class) { - testSyncThread_ready = true; - LockSupport.park(); - } - }); - - t.start(); - while (!testSyncThread_ready) { - Thread.sleep(1); - } - /* Let virtual thread park */ - Thread.sleep(500); - AssertJUnit.assertTrue("Virtual Thread state should be WAITING", (t.getState() == Thread.State.WAITING)); - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testJNIThread_ready = false; - - @Test - public void test_jniFromVirtualthread() { - try { - Thread t = Thread.ofVirtual().name("native").unstarted(() -> { - testJNIThread_ready = true; - lockSupportPark(); - }); - - t.start(); - while (!testJNIThread_ready) { - Thread.sleep(1); - } - /* Let virtual thread park */ - Thread.sleep(500); - AssertJUnit.assertTrue("Virtual Thread state should be WAITING", (t.getState() == Thread.State.WAITING)); - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testThread1Ready = false; - - @Test - public void test_YieldedVirtualThreadGetStackTrace() { - /* The expected frame count is based on test's callstack and OpenJ9's implementation of - * Continuation.yield(). - */ - int expectedFrames = 12; - String expectedMethodName = "yieldImpl"; - - try { - Thread t = Thread.ofVirtual().name("yielded-stackwalker").start(() -> { - testThread1Ready = true; - LockSupport.park(); - }); - while (!testThread1Ready) { - Thread.sleep(10); - } - - /* Incrementally wait for 10000 ms. */ - for (int i = 0; i < 20; i++) { - /* Let the virtual thread park. */ - Thread.sleep(500); - if (Thread.State.WAITING == t.getState()) { - break; - } - } - - StackTraceElement[] ste = t.getStackTrace(); - - /* If the stacktrace doesn't match the expected result, then print out the stacktrace - * for debuggging. - */ - if ((expectedFrames != ste.length) || !ste[0].getMethodName().equals(expectedMethodName)) { - for (StackTraceElement st : ste) { - System.out.println(st); - } - } - - AssertJUnit.assertTrue( - "Expected " + expectedFrames + " frames, got " + ste.length, - (expectedFrames == ste.length)); - - AssertJUnit.assertTrue( - "Expected top frame to be yieldImpl, got " + ste[0].getMethodName(), - ste[0].getMethodName().equals(expectedMethodName)); - - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testThread2_state = false; - - @Test - public void test_RunningVirtualThreadGetStackTrace() { - try { - Thread t = Thread.ofVirtual().name("running-stackwalker").start(() -> { - testThread2_state = true; - while (testThread2_state); - }); - while (!testThread2_state) { - Thread.sleep(10); - } - - StackTraceElement[] ste = t.getStackTrace(); - AssertJUnit.assertTrue("Expected 4 frames, got " + ste.length, (4 == ste.length)); - AssertJUnit.assertTrue("Expected top frame to be VirtualThreadTests class, got " + ste[0].toString(), ste[0].getClassName().equals("org.openj9.test.jep425.VirtualThreadTests")); - - testThread2_state = false; - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static int readVirtualThreadStates(Class vthreadCls, String fieldName) throws Exception { - Field vthreadState = vthreadCls.getDeclaredField(fieldName); - vthreadState.setAccessible(true); - return vthreadState.getInt(null); - } - - @Test - public void test_verifyJVMTIMacros() { - final int JVMTI_VTHREAD_STATE_NEW = 0; - final int JVMTI_VTHREAD_STATE_STARTED = 1; - final int JVMTI_VTHREAD_STATE_RUNNABLE= 2; - final int JVMTI_VTHREAD_STATE_RUNNING = 3; - final int JVMTI_VTHREAD_STATE_PARKING = 4; - final int JVMTI_VTHREAD_STATE_PARKED = 5; - final int JVMTI_VTHREAD_STATE_PINNED = 6; - final int JVMTI_VTHREAD_STATE_YIELDING = 7; - final int JVMTI_VTHREAD_STATE_TERMINATED = 99; - final int JVMTI_VTHREAD_STATE_SUSPENDED = (1 << 8); - final int JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED = (JVMTI_VTHREAD_STATE_RUNNABLE | JVMTI_VTHREAD_STATE_SUSPENDED); - final int JVMTI_VTHREAD_STATE_PARKED_SUSPENDED = (JVMTI_VTHREAD_STATE_PARKED | JVMTI_VTHREAD_STATE_SUSPENDED); - - int value = 0; - - try { - Class vthreadCls = Class.forName("java.lang.VirtualThread"); - - value = readVirtualThreadStates(vthreadCls, "NEW"); - if (JVMTI_VTHREAD_STATE_NEW != value) { - Assert.fail("JVMTI_VTHREAD_STATE_NEW (" + JVMTI_VTHREAD_STATE_NEW + ") does not match VirtualThread.NEW (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "STARTED"); - if (JVMTI_VTHREAD_STATE_STARTED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_STARTED (" + JVMTI_VTHREAD_STATE_STARTED + ") does not match VirtualThread.STARTED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNABLE"); - if (JVMTI_VTHREAD_STATE_RUNNABLE != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNABLE (" + JVMTI_VTHREAD_STATE_RUNNABLE + ") does not match VirtualThread.RUNNABLE (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNING"); - if (JVMTI_VTHREAD_STATE_RUNNING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNING (" + JVMTI_VTHREAD_STATE_RUNNING + ") does not match VirtualThread.RUNNING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKING"); - if (JVMTI_VTHREAD_STATE_PARKING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKING (" + JVMTI_VTHREAD_STATE_PARKING + ") does not match VirtualThread.PARKING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKED"); - if (JVMTI_VTHREAD_STATE_PARKED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKED (" + JVMTI_VTHREAD_STATE_PARKED + ") does not match VirtualThread.PARKED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PINNED"); - if (JVMTI_VTHREAD_STATE_PINNED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PINNED (" + JVMTI_VTHREAD_STATE_PINNED + ") does not match VirtualThread.PINNED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "YIELDING"); - if (JVMTI_VTHREAD_STATE_YIELDING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_YIELDING (" + JVMTI_VTHREAD_STATE_YIELDING + ") does not match VirtualThread.YIELDING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "TERMINATED"); - if (JVMTI_VTHREAD_STATE_TERMINATED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_TERMINATED (" + JVMTI_VTHREAD_STATE_TERMINATED + ") does not match VirtualThread.TERMINATED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "SUSPENDED"); - if (JVMTI_VTHREAD_STATE_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_SUSPENDED (" + JVMTI_VTHREAD_STATE_SUSPENDED + ") does not match VirtualThread.SUSPENDED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNABLE_SUSPENDED"); - if (JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED (" + JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED + ") does not match VirtualThread.RUNNABLE_SUSPENDED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKED_SUSPENDED"); - if (JVMTI_VTHREAD_STATE_PARKED_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKED_SUSPENDED (" + JVMTI_VTHREAD_STATE_PARKED_SUSPENDED + ") does not match VirtualThread.PARKED_SUSPENDED (" + value + ")"); - } - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } -} diff --git a/test/functional/Java19andUp/testng_190.xml b/test/functional/Java19andUp/testng_190.xml deleted file mode 100644 index d877c20ebcf..00000000000 --- a/test/functional/Java19andUp/testng_190.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java20andUp/build.xml b/test/functional/Java20andUp/build.xml deleted file mode 100644 index 76129c2befb..00000000000 --- a/test/functional/Java20andUp/build.xml +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - Tests for Java 20 and up - - - - - - - - - - - - - - - - - - - Ant version is ${ant.version} - ============COMPILER SETTINGS============ - ===fork: yes - ===executable: ${compiler.javac} - ===debug: on - ===destdir: ${DEST} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java20andUp/playlist.xml b/test/functional/Java20andUp/playlist.xml deleted file mode 100644 index a2e1f958203..00000000000 --- a/test/functional/Java20andUp/playlist.xml +++ /dev/null @@ -1,145 +0,0 @@ - - - - - Jep425Tests_testVirtualThread - - --enable-preview -Xgcpolicy:optthruput - --enable-preview -Xgcpolicy:gencon - --enable-preview -Xgcpolicy:balanced - --enable-preview -Xgcpolicy:optavgpause - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --add-opens java.base/java.lang=ALL-UNNAMED \ - -cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_200.xml$(Q) \ - -testnames Jep425Tests_testVirtualThread \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^os.zos,^os.sunos - - sanity - - - functional - - - native - - - openj9 - - - 20 - - - - - Jep434Tests_testLinkerFfi_DownCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_200.xml$(Q) -testnames Jep434Tests_testLinkerFfi_DownCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 20 - - - - - Jep434Tests_testLinkerFfi_UpCall - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_200.xml$(Q) -testnames Jep434Tests_testLinkerFfi_UpCall \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 20 - - - - - Jep434Tests_testLinkerFfi_VaList - - --enable-preview - - $(ADD_JVM_LIB_DIR_TO_LIBPATH) $(JAVA_COMMAND) $(JVM_OPTIONS) \ - --enable-native-access=ALL-UNNAMED \ - -Dforeign.restricted=permit \ - -cp $(Q)$(LIB_DIR)$(D)asm.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \ - org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng_200.xml$(Q) -testnames Jep434Tests_testLinkerFfi_VaList \ - -groups $(TEST_GROUP) \ - -excludegroups $(DEFAULT_EXCLUDE); \ - $(TEST_STATUS) - - bits.64,^arch.aarch64,^arch.arm,^arch.riscv,^os.zos,^os.sunos - - sanity - - - functional - - - openj9 - - - 20 - - - diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep425/VirtualThreadTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep425/VirtualThreadTests.java deleted file mode 100644 index 521bb0f70fe..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep425/VirtualThreadTests.java +++ /dev/null @@ -1,333 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep425; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.reflect.*; -import java.lang.Thread; -import java.time.Duration; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.LockSupport; -import java.util.stream.IntStream; - -/** - * Test cases for JEP 425: Virtual Threads (Preview) Continuation execution - * which verifies the basic cases including Continuation enter, yield, resume. - */ -@Test(groups = { "level.sanity" }) -public class VirtualThreadTests { - static { - try { - System.loadLibrary("j9ben"); - } catch (UnsatisfiedLinkError e) { - System.out.println("No natives for JNI tests"); - } - } - - public static native boolean lockSupportPark(); - - @Test - public void test_basicVirtualthread() { - var wrapper = new Object(){ boolean executed = false; }; - try { - Thread t = Thread.ofVirtual().name("duke").unstarted(() -> { - wrapper.executed = true; - }); - - t.start(); - t.join(); - - AssertJUnit.assertTrue("Virtual Thread operation not executed", wrapper.executed); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - @Test - public void test_VirtualthreadYieldResume() { - try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { - int numThreads = 6; - int expectedThreadResult = 3; - long estimatedThreadCompletionTime = 5L; /* seconds */ - int[] results = new int[numThreads]; - - IntStream.range(0, numThreads).forEach(i -> { - executor.submit(() -> { - results[i] = 1; - Thread.sleep(Duration.ofSeconds(1)); - results[i] += 1; - Thread.sleep(Duration.ofSeconds(1)); - results[i] += 1; - return i; - }); - }); - - /* Wait incrementally for the worst-case scenario where all virtual threads are - * executed sequentially. Exit the wait loop if the virtual threads finish early. - */ - for (int i = 0; i < numThreads; i++) { - executor.awaitTermination(estimatedThreadCompletionTime, TimeUnit.SECONDS); - boolean exit = true; - for (int j = 0; j < numThreads; j++) { - if (results[j] != expectedThreadResult) { - exit = false; - } - } - if (exit) { - break; - } - } - - for (int i = 0; i < numThreads; i++) { - AssertJUnit.assertTrue( - "Virtual Thread " + i + ": incorrect result of " + results[i], - (results[i] == expectedThreadResult)); - } - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testSyncThread_ready; - - @Test - public void test_synchronizedBlockFromVirtualthread() { - try { - Thread t = Thread.ofVirtual().name("synchronized").unstarted(() -> { - synchronized (VirtualThreadTests.class) { - testSyncThread_ready = true; - LockSupport.park(); - } - }); - - t.start(); - while (!testSyncThread_ready) { - Thread.sleep(1); - } - /* Let virtual thread park */ - Thread.sleep(500); - Assert.assertEquals(t.getState(), Thread.State.WAITING); - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testJNIThread_ready; - - @Test - public void test_jniFromVirtualthread() { - try { - Thread t = Thread.ofVirtual().name("native").unstarted(() -> { - testJNIThread_ready = true; - lockSupportPark(); - }); - - t.start(); - while (!testJNIThread_ready) { - Thread.sleep(1); - } - /* Let virtual thread park */ - Thread.sleep(500); - Assert.assertEquals(t.getState(), Thread.State.WAITING); - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testThread1Ready = false; - - @Test - public void test_YieldedVirtualThreadGetStackTrace() { - /* The expected frame count is based on test's callstack and OpenJ9's implementation of - * Continuation.yield(). - */ - int expectedFrames = 12; - String expectedMethodName = "yieldImpl"; - - try { - Thread t = Thread.ofVirtual().name("yielded-stackwalker").start(() -> { - testThread1Ready = true; - LockSupport.park(); - }); - while (!testThread1Ready) { - Thread.sleep(10); - } - - /* Incrementally wait for 10000 ms. */ - for (int i = 0; i < 20; i++) { - /* Let the virtual thread park. */ - Thread.sleep(500); - if (Thread.State.WAITING == t.getState()) { - break; - } - } - - StackTraceElement[] ste = t.getStackTrace(); - - /* If the stacktrace doesn't match the expected result, then print out the stacktrace - * for debuggging. - */ - if ((expectedFrames != ste.length) || !ste[0].getMethodName().equals(expectedMethodName)) { - for (StackTraceElement st : ste) { - System.out.println(st); - } - } - - AssertJUnit.assertTrue( - "Expected " + expectedFrames + " frames, got " + ste.length, - (expectedFrames == ste.length)); - - AssertJUnit.assertTrue( - "Expected top frame to be yieldImpl, got " + ste[0].getMethodName(), - ste[0].getMethodName().equals(expectedMethodName)); - - LockSupport.unpark(t); - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static volatile boolean testThread2_state = false; - - @Test - public void test_RunningVirtualThreadGetStackTrace() { - try { - Thread t = Thread.ofVirtual().name("running-stackwalker").start(() -> { - testThread2_state = true; - while (testThread2_state); - }); - while (!testThread2_state) { - Thread.sleep(10); - } - - StackTraceElement[] ste = t.getStackTrace(); - Assert.assertEquals(ste.length, 4); - Assert.assertEquals(ste[0].getClassName(), "org.openj9.test.jep425.VirtualThreadTests"); - - testThread2_state = false; - t.join(); - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } - - private static int readVirtualThreadStates(Class vthreadCls, String fieldName) throws Exception { - Field vthreadState = vthreadCls.getDeclaredField(fieldName); - vthreadState.setAccessible(true); - return vthreadState.getInt(null); - } - - @Test - public void test_verifyJVMTIMacros() { - final int JVMTI_VTHREAD_STATE_NEW = 0; - final int JVMTI_VTHREAD_STATE_STARTED = 1; - final int JVMTI_VTHREAD_STATE_RUNNABLE= 2; - final int JVMTI_VTHREAD_STATE_RUNNING = 3; - final int JVMTI_VTHREAD_STATE_PARKING = 4; - final int JVMTI_VTHREAD_STATE_PARKED = 5; - final int JVMTI_VTHREAD_STATE_PINNED = 6; - final int JVMTI_VTHREAD_STATE_YIELDING = 7; - final int JVMTI_VTHREAD_STATE_TERMINATED = 99; - final int JVMTI_VTHREAD_STATE_SUSPENDED = (1 << 8); - final int JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED = (JVMTI_VTHREAD_STATE_RUNNABLE | JVMTI_VTHREAD_STATE_SUSPENDED); - final int JVMTI_VTHREAD_STATE_PARKED_SUSPENDED = (JVMTI_VTHREAD_STATE_PARKED | JVMTI_VTHREAD_STATE_SUSPENDED); - - int value = 0; - - try { - Class vthreadCls = Class.forName("java.lang.VirtualThread"); - - value = readVirtualThreadStates(vthreadCls, "NEW"); - if (JVMTI_VTHREAD_STATE_NEW != value) { - Assert.fail("JVMTI_VTHREAD_STATE_NEW (" + JVMTI_VTHREAD_STATE_NEW + ") does not match VirtualThread.NEW (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "STARTED"); - if (JVMTI_VTHREAD_STATE_STARTED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_STARTED (" + JVMTI_VTHREAD_STATE_STARTED + ") does not match VirtualThread.STARTED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNABLE"); - if (JVMTI_VTHREAD_STATE_RUNNABLE != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNABLE (" + JVMTI_VTHREAD_STATE_RUNNABLE + ") does not match VirtualThread.RUNNABLE (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNING"); - if (JVMTI_VTHREAD_STATE_RUNNING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNING (" + JVMTI_VTHREAD_STATE_RUNNING + ") does not match VirtualThread.RUNNING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKING"); - if (JVMTI_VTHREAD_STATE_PARKING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKING (" + JVMTI_VTHREAD_STATE_PARKING + ") does not match VirtualThread.PARKING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKED"); - if (JVMTI_VTHREAD_STATE_PARKED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKED (" + JVMTI_VTHREAD_STATE_PARKED + ") does not match VirtualThread.PARKED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PINNED"); - if (JVMTI_VTHREAD_STATE_PINNED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PINNED (" + JVMTI_VTHREAD_STATE_PINNED + ") does not match VirtualThread.PINNED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "YIELDING"); - if (JVMTI_VTHREAD_STATE_YIELDING != value) { - Assert.fail("JVMTI_VTHREAD_STATE_YIELDING (" + JVMTI_VTHREAD_STATE_YIELDING + ") does not match VirtualThread.YIELDING (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "TERMINATED"); - if (JVMTI_VTHREAD_STATE_TERMINATED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_TERMINATED (" + JVMTI_VTHREAD_STATE_TERMINATED + ") does not match VirtualThread.TERMINATED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "SUSPENDED"); - if (JVMTI_VTHREAD_STATE_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_SUSPENDED (" + JVMTI_VTHREAD_STATE_SUSPENDED + ") does not match VirtualThread.SUSPENDED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "RUNNABLE_SUSPENDED"); - if (JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED (" + JVMTI_VTHREAD_STATE_RUNNABLE_SUSPENDED + ") does not match VirtualThread.RUNNABLE_SUSPENDED (" + value + ")"); - } - - value = readVirtualThreadStates(vthreadCls, "PARKED_SUSPENDED"); - if (JVMTI_VTHREAD_STATE_PARKED_SUSPENDED != value) { - Assert.fail("JVMTI_VTHREAD_STATE_PARKED_SUSPENDED (" + JVMTI_VTHREAD_STATE_PARKED_SUSPENDED + ") does not match VirtualThread.PARKED_SUSPENDED (" + value + ")"); - } - } catch (Exception e) { - Assert.fail("Unexpected exception occured : " + e.getMessage() , e); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/InvalidDownCallTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/InvalidDownCallTests.java deleted file mode 100644 index c8b62ad8925..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/InvalidDownCallTests.java +++ /dev/null @@ -1,192 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.MethodHandle; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; -import java.lang.invoke.WrongMethodTypeException; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, - * which verify the illegal cases including unsupported layouts, etc. - * Note: the majority of illegal cases are removed given the corresponding method type - * is deduced from the function descriptor which is verified in OpenJDK. - */ -@Test(groups = { "level.sanity" }) -public class InvalidDownCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForIntType() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, MemoryLayout.paddingLayout(32)); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForMemAddr() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, MemoryLayout.paddingLayout(64)); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*") - public void test_invalidMemoryLayoutForReturnType() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(MemoryLayout.paddingLayout(64), JAVA_LONG); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout"); - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - int result = (int)mh.invoke(19202122, null); - fail("Failed to throw out NullPointerException in the case of the null value"); - } - - @Test(expectedExceptions = WrongMethodTypeException.class) - public void test_nullValueForStructArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, null); - fail("Failed to throw out WrongMethodTypeException in the case of the null value"); - } - } - - public void test_nullSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("validateNullAddrArgument").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - int result = (int)mh.invoke(19202122, MemorySegment.NULL); - Assert.assertEquals(result, 19202122); - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullSegmentForStructArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, MemorySegment.NULL); - fail("Failed to throw out NullPointerException in the case of the null segment"); - } - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Heap segment not allowed.*") - public void test_heapSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt = MemorySegment.ofArray(new int[]{11121314, 15161718}); - int result = (int)mh.invoke(19202122, structSegmt); - fail("Failed to throw out IllegalArgumentException in the case of the heap segment"); - } - - public void test_heapSegmentForStructArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = MemorySegment.ofArray(new int[]{99001122, 33445566}); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiCallTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiCallTests.java deleted file mode 100644 index 7438c3f2c35..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiCallTests.java +++ /dev/null @@ -1,139 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, - * which verifies multiple downcalls with the same or different layouts or argument/return types. - */ -@Test(groups = { "level.sanity" }) -public class MultiCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_twoCallsWithSameFuncDescriptor() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - mh = linker.downcallHandle(functionSymbol, fd); - result = (int)mh.invokeExact(235, 439); - Assert.assertEquals(result, 674); - } - - @Test - public void test_twoCallsWithDiffFuncDescriptor() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol1 = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol2 = nativeLibLookup.find("add3Ints").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } - - @Test - public void test_multiCallsWithMixedFuncDescriptors() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol1 = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol2 = nativeLibLookup.find("add3Ints").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - - FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol3 = nativeLibLookup.find("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(454, 398); - - mh = linker.downcallHandle(functionSymbol1, fd1); - result = (int)mh.invokeExact(234, 567); - Assert.assertEquals(result, 801); - - mh = linker.downcallHandle(functionSymbol2, fd2); - result = (int)mh.invokeExact(312, 323, 334); - Assert.assertEquals(result, 969); - - mh = linker.downcallHandle(functionSymbol3, fd3); - mh.invokeExact(539, 672); - } - - @Test - public void test_twoCallsWithDiffReturnType() throws Throwable { - FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol1 = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd1); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol2 = nativeLibLookup.find("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } - - @Test - public void test_multiCallsWithSameArgLayouts() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol1 = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol1, fd); - int intResult = (int)mh.invokeExact(112, 123); - Assert.assertEquals(intResult, 235); - - mh = linker.downcallHandle(functionSymbol1, fd); - intResult = (int)mh.invokeExact(234, 567); - Assert.assertEquals(intResult, 801); - - FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol2 = nativeLibLookup.find("add2IntsReturnVoid").get(); - mh = linker.downcallHandle(functionSymbol2, fd2); - mh.invokeExact(454, 398); - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests1.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests1.java deleted file mode 100644 index 8cb29b7aada..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests1.java +++ /dev/null @@ -1,114 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.SegmentAllocator; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, which - * verifies the downcalls with the same downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SameDowncallHandler() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(322, intSegmt); - Assert.assertEquals(result, 537); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests2.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests2.java deleted file mode 100644 index e8eb4027f6c..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests2.java +++ /dev/null @@ -1,132 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, which - * verifies the downcalls with the shared downcall handlder (cached as soft reference in OpenJDK) - * in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - - static { - System.loadLibrary("clinkerffitests"); - } - private static final GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - private static final FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - private static final MemorySegment functionSymbol = SymbolLookup.loaderLookup().find("add2IntStructs_returnStruct").get(); - private static final MethodHandle mh = Linker.nativeLinker().downcallHandle(functionSymbol, fd); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithSameFuncDesc_SharedDowncallHandler() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001123); - intHandle2.set(structSegmt2, 33445567); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224467); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113355); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests3.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests3.java deleted file mode 100644 index 85de2045891..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests3.java +++ /dev/null @@ -1,101 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, - * which verifies the downcalls with the diffrent layouts and arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests3 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123, 235); - Assert.assertEquals(result, 470); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests4.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests4.java deleted file mode 100644 index 7aea53a8a85..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests4.java +++ /dev/null @@ -1,100 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, - * which verifies the downcalls with the diffrent return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests4 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_twoThreadsWithDiffReturnType() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - - thr1.join(); - thr2.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests5.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests5.java deleted file mode 100644 index 1276a66167f..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/MultiThreadingTests5.java +++ /dev/null @@ -1,173 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall, - * which verifies multiple downcalls combined with the diffrent layouts/arguments/return types in multithreading. - */ -@Test(groups = { "level.sanity" }) -public class MultiThreadingTests5 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiThreadsWithMixedFuncDescriptors() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(128, 246); - Assert.assertEquals(result, 374); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 642, 971); - Assert.assertEquals(result, 1725); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr3 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr4 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(416, 728); - Assert.assertEquals(result, 1144); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr5 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add3Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(1012, 1023, 2035); - Assert.assertEquals(result, 4070); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - Thread thr6 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(false, false); - Assert.assertEquals(result, false); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - - thr1.setUncaughtExceptionHandler(this); - thr2.setUncaughtExceptionHandler(this); - thr3.setUncaughtExceptionHandler(this); - thr4.setUncaughtExceptionHandler(this); - thr5.setUncaughtExceptionHandler(this); - thr6.setUncaughtExceptionHandler(this); - - thr1.start(); - thr2.start(); - thr3.start(); - thr4.start(); - thr5.start(); - thr6.start(); - - thr6.join(); - thr5.join(); - thr4.join(); - thr3.join(); - thr2.join(); - thr1.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests1.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests1.java deleted file mode 100644 index 0744d3a2a8b..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests1.java +++ /dev/null @@ -1,306 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SegmentScope; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests1 { - private static Linker linker = Linker.nativeLinker(); - private static SegmentScope scope = SegmentScope.auto(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.nativeAllocator(scope); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - boolean result = (boolean)mh.invokeExact(true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFrom2Chars").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - char result = (char)mh.invokeExact('B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - byte result = (byte)mh.invokeExact((byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke((byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - short result = (short)mh.invokeExact((short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(-112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - mh.invokeExact(454, 398); - } - - @Test - public void test_addIntAndChar_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndChar").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - int result = (int)mh.invokeExact(58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - MemorySegment functionSymbol = nativeLibLookup.find("add2Longs").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - long result = (long)mh.invokeExact(57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Floats").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - float result = (float)mh.invokeExact(5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Doubles").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - double result = (double)mh.invokeExact(159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_1() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_1() throws Throwable { - MemorySegment strlenSymbol = defaultLibLookup.find("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = linker.downcallHandle(strlenSymbol, fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP424 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable { - MemorySegment allocSymbol = defaultLibLookup.find("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = linker.downcallHandle(allocSymbol, allocFuncDesc); - MemorySegment allocMemAddr = (MemorySegment)allocHandle.invokeExact(10L); - MemorySegment allocMemSegmt = MemorySegment.ofAddress(allocMemAddr.address(), 10L); - allocMemSegmt.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemSegmt.get(JAVA_INT, 0), 15); - - MemorySegment freeSymbol = defaultLibLookup.find("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = linker.downcallHandle(freeSymbol, freeFuncDesc); - freeHandle.invoke(allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_1() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(formatSegmt, 15, 27, 42); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_LinkerOption_1() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd, Linker.Option.firstVariadicArg(1)); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests2.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests2.java deleted file mode 100644 index 2a076cfb788..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/PrimitiveTypeTests2.java +++ /dev/null @@ -1,308 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SegmentScope; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in downcall. - * - * Note: the test suite is intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class PrimitiveTypeTests2 { - private static Linker linker = Linker.nativeLinker(); - private static SegmentScope scope = SegmentScope.auto(); - private static SegmentAllocator nativeAllocator = SegmentAllocator.nativeAllocator(scope); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOr").get(); - MethodHandle mh = linker.downcallHandle(fd); - boolean result = (boolean)mh.invokeExact(functionSymbol, true, false); - Assert.assertEquals(result, true); - } - - @Test - public void test_addBoolAndBoolFromPointerWithOr_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromPointerWithOr").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(functionSymbol, false, boolSegmt); - Assert.assertEquals(result, true); - } - - @Test - public void test_generateNewChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFrom2Chars").get(); - MethodHandle mh = linker.downcallHandle(fd); - char result = (char)mh.invokeExact(functionSymbol, 'B', 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_generateNewCharFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(functionSymbol, charSegmt, 'D'); - Assert.assertEquals(result, 'C'); - } - - @Test - public void test_addTwoBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, (byte)3); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoNegtiveBytes_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Bytes").get(); - MethodHandle mh = linker.downcallHandle(fd); - byte result = (byte)mh.invokeExact(functionSymbol, (byte)-6, (byte)-3); - Assert.assertEquals(result, (byte)-9); - } - - @Test - public void test_addByteAndByteFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment byteSegmt = nativeAllocator.allocate(JAVA_BYTE, (byte)3); - byte result = (byte)mh.invoke(functionSymbol, (byte)6, byteSegmt); - Assert.assertEquals(result, (byte)9); - } - - @Test - public void test_addTwoShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)24, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoNegtiveShorts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Shorts").get(); - MethodHandle mh = linker.downcallHandle(fd); - short result = (short)mh.invokeExact(functionSymbol, (short)-24, (short)-32); - Assert.assertEquals(result, (short)-56); - } - - @Test - public void test_addShortAndShortFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment shortSegmt = nativeAllocator.allocate(JAVA_SHORT, (short)24); - short result = (short)mh.invoke(functionSymbol, shortSegmt, (short)32); - Assert.assertEquals(result, (short)56); - } - - @Test - public void test_addTwoInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 112, 123); - Assert.assertEquals(result, 235); - } - - @Test - public void test_addTwoNegtiveInts_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Ints").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, -112, -123); - Assert.assertEquals(result, -235); - } - - @Test - public void test_addIntAndIntFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215); - int result = (int)mh.invoke(functionSymbol, 321, intSegmt); - Assert.assertEquals(result, 536); - } - - @Test - public void test_addTwoIntsReturnVoid_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoid").get(); - MethodHandle mh = linker.downcallHandle(fd); - mh.invokeExact(functionSymbol, 454, 398); - } - - @Test - public void test_addIntAndChar_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndChar").get(); - MethodHandle mh = linker.downcallHandle(fd); - int result = (int)mh.invokeExact(functionSymbol, 58, 'A'); - Assert.assertEquals(result, 123); - } - - @Test - public void test_addTwoLongs_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG); - MemorySegment functionSymbol = nativeLibLookup.find("add2Longs").get(); - MethodHandle mh = linker.downcallHandle(fd); - long result = (long)mh.invokeExact(functionSymbol, 57424L, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addLongAndLongFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L); - long result = (long)mh.invoke(functionSymbol, longSegmt, 698235L); - Assert.assertEquals(result, 755659L); - } - - @Test - public void test_addTwoFloats_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT); - MemorySegment functionSymbol = nativeLibLookup.find("add2Floats").get(); - MethodHandle mh = linker.downcallHandle(fd); - float result = (float)mh.invokeExact(functionSymbol, 5.74f, 6.79f); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addFloatAndFloatFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment floatSegmt = nativeAllocator.allocate(JAVA_FLOAT, 6.79f); - float result = (float)mh.invoke(functionSymbol, 5.74f, floatSegmt); - Assert.assertEquals(result, 12.53f, 0.01f); - } - - @Test - public void test_addTwoDoubles_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - MemorySegment functionSymbol = nativeLibLookup.find("add2Doubles").get(); - MethodHandle mh = linker.downcallHandle(fd); - double result = (double)mh.invokeExact(functionSymbol, 159.748d, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_addDoubleAndDoubleFromPointer_2() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment doubleSegmt = nativeAllocator.allocate(JAVA_DOUBLE, 159.748d); - double result = (double)mh.invoke(functionSymbol, doubleSegmt, 262.795d); - Assert.assertEquals(result, 422.543d, 0.001d); - } - - @Test - public void test_strlenFromDefaultLibWithMemAddr_2() throws Throwable { - MemorySegment strlenSymbol = defaultLibLookup.find("strlen").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS); - MethodHandle mh = linker.downcallHandle(fd); - MemorySegment funcSegmt = nativeAllocator.allocateUtf8String("JEP424 DOWNCALL TEST SUITES"); - long strLength = (long)mh.invoke(strlenSymbol, funcSegmt); - Assert.assertEquals(strLength, 27); - } - - @Test - public void test_memoryAllocFreeFromDefaultLib_2() throws Throwable { - MemorySegment allocSymbol = defaultLibLookup.find("malloc").get(); - FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG); - MethodHandle allocHandle = linker.downcallHandle(allocFuncDesc); - MemorySegment allocMemAddr = (MemorySegment)allocHandle.invokeExact(allocSymbol, 10L); - MemorySegment allocMemSegmt = MemorySegment.ofAddress(allocMemAddr.address(), 10L); - allocMemSegmt.set(JAVA_INT, 0, 15); - Assert.assertEquals(allocMemSegmt.get(JAVA_INT, 0), 15); - - MemorySegment freeSymbol = defaultLibLookup.find("free").get(); - FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS); - MethodHandle freeHandle = linker.downcallHandle(freeFuncDesc); - freeHandle.invoke(freeSymbol, allocMemAddr); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_2() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(fd); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(functionSymbol, formatSegmt, 15, 27, 42); - } - - @Test - public void test_printfFromDefaultLibWithMemAddr_LinkerOption_2() throws Throwable { - MemorySegment functionSymbol = defaultLibLookup.find("printf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT); - MethodHandle mh = linker.downcallHandle(fd, Linker.Option.firstVariadicArg(1)); - MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n"); - mh.invoke(functionSymbol, formatSegmt, 15, 27, 42); - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests1.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests1.java deleted file mode 100644 index 2bbe857ae98..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests1.java +++ /dev/null @@ -1,3117 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests1 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(boolSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BOOLEAN.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultSegmt.address(), boolSegmt.address()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_1() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact((byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(byteSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultSegmt.address(), byteSegmt.address()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke((byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact((byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact((byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact((byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_1() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact((byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact((byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact((byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact('C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(charSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_CHAR.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultSegmt.address(), charSegmt.address()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact('H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_1() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact('D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact('J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - - short result = (short)mh.invokeExact((short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultSegmt.address(), shortSegmt.address()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke((short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact((short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact((short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact((short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3ShortStructs_returnStruct").get(); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntShortFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(intSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultSegmt.address(), intSegmt.address()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invokeExact(2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntLongFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(longSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultSegmt.address(), longSegmt.address()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invokeExact(6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(floatSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_FLOAT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultSegmt.address(), floatSegmt.address()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_1() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invokeExact(3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - double result = (double)mh.invokeExact(113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultSegmt.address(), doubleSegmt.address()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_1() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_1() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_1() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests2.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests2.java deleted file mode 100644 index c56afb832dd..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/downcall/StructTests2.java +++ /dev/null @@ -1,3114 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.downcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for argument/return struct in downcall. - * - * Note: - * [1] the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - * - * [2] the test suite is mainly intended for the following Clinker API: - * MethodHandle downcallHandle(FunctionDescriptor function) - */ -@Test(groups = { "level.sanity" }) -public class StructTests2 { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(functionSymbol, boolSegmt, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, boolSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BOOLEAN.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultSegmt.address(), boolSegmt.address()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXor_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructPointerWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_withoutLayoutName_2() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = MemoryLayout.structLayout(boolArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invokeExact(functionSymbol, true, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BOOLEAN, - MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - structSegmt.set(JAVA_BOOLEAN, 3, false); - structSegmt.set(JAVA_BOOLEAN, 4, true); - - boolean result = (boolean)mh.invokeExact(functionSymbol, false, structSegmt); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize())); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3BoolStructsWithXor_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - byte result = (byte)mh.invoke(functionSymbol, byteSegmt, structSegmt); - Assert.assertEquals(result, 42); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStruct_returnBytePointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, byteSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultSegmt.address(), byteSegmt.address()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - - byte result = (byte)mh.invoke(functionSymbol, (byte)13, structSegmt); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)48, structSegmt); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)46, structSegmt); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrder_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)18, structSegmt); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_2() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)14, structSegmt); - Assert.assertEquals(result, 50); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)22, structSegmt); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_BYTE, - MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invokeExact(functionSymbol, (byte)16, structSegmt); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_add2ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStruct").get(); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize())); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3ByteStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invokeExact(functionSymbol, 'C', structSegmt); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(functionSymbol, charSegmt, structSegmt); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStruct_returnCharPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, charSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_CHAR.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultSegmt.address(), charSegmt.address()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke(functionSymbol, 'G', structSegmt); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invokeExact(functionSymbol, 'H', structSegmt); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName_2() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = MemoryLayout.structLayout(charArray, JAVA_CHAR, MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invokeExact(functionSymbol, 'D', structSegmt); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_CHAR); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invokeExact(functionSymbol, 'J', structSegmt); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize())); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3CharStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)8); - shortHandle2.set(structSegmt, (short)9); - short result = (short)mh.invokeExact(functionSymbol, (short)6, structSegmt); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - short result = (short)mh.invoke(functionSymbol, shortSegmt, structSegmt); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStruct_returnShortPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)18); - shortHandle2.set(structSegmt, (short)19); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, shortSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 49); - Assert.assertEquals(resultSegmt.address(), shortSegmt.address()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)22); - shortHandle2.set(structSegmt, (short)44); - - short result = (short)mh.invoke(functionSymbol, (short)66, structSegmt); - Assert.assertEquals(result, 132); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT, - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)31); - structSegmt.set(JAVA_SHORT, 2, (short)33); - structSegmt.set(JAVA_SHORT, 4, (short)35); - - short result = (short)mh.invokeExact(functionSymbol, (short)37, structSegmt); - Assert.assertEquals(result, 136); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_2() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - - short result = (short)mh.invokeExact(functionSymbol, (short)444, structSegmt); - Assert.assertEquals(result, 1110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_SHORT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)111); - structSegmt.set(JAVA_SHORT, 2, (short)222); - structSegmt.set(JAVA_SHORT, 4, (short)333); - structSegmt.set(JAVA_SHORT, 6, (short)444); - structSegmt.set(JAVA_SHORT, 8, (short)555); - - short result = (short)mh.invokeExact(functionSymbol, (short)666, structSegmt); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add2ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)134); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)112); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)56); - shortHandle2.set(structSegmt1, (short)45); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)78); - shortHandle2.set(structSegmt2, (short)67); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 134); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); - } - } - - @Test - public void test_add3ShortStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3ShortStructs_returnStruct").get(); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MethodHandle mh = linker.downcallHandle(fd); - - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)25); - shortHandle2.set(structSegmt1, (short)26); - shortHandle3.set(structSegmt1, (short)27); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)34); - shortHandle2.set(structSegmt2, (short)35); - shortHandle3.set(structSegmt2, (short)36); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)59); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)61); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)63); - } - } - - @Test - public void test_addIntAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invokeExact(functionSymbol, 2244668, structSegmt); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAndIntShortFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntShortFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invokeExact(functionSymbol, 22334455, structSegmt); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invokeExact(functionSymbol, 11335577, structSegmt); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(functionSymbol, intSegmt, structSegmt); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStruct_returnIntPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, intSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultSegmt.address(), intSegmt.address()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(functionSymbol, 19202122, structSegmt); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invokeExact(functionSymbol, 33343536, structSegmt); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_2() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invokeExact(functionSymbol, 4444444, structSegmt); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT, JAVA_INT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_INT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invokeExact(functionSymbol, 6666666, structSegmt); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3IntStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - long result = (long)mh.invokeExact(functionSymbol, 2468024680L, structSegmt); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addIntAndIntLongFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntLongFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invokeExact(functionSymbol, 22446688, structSegmt); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(JAVA_INT.bitSize())); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invokeExact(functionSymbol, 1234567, structSegmt); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(functionSymbol, longSegmt, structSegmt); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStruct_returnLongPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, longSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultSegmt.address(), longSegmt.address()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(functionSymbol, 668800224466L, structSegmt); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG, nestedStructLayout); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invokeExact(functionSymbol, 778899001122L, structSegmt); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrder_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_2() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - - long result = (long)mh.invokeExact(functionSymbol, 444444444L, structSegmt); - Assert.assertEquals(result, 1111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_LONG); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 111111111L); - structSegmt.set(JAVA_LONG, 8, 222222222L); - structSegmt.set(JAVA_LONG, 16, 333333333L); - structSegmt.set(JAVA_LONG, 24, 444444444L); - structSegmt.set(JAVA_LONG, 32, 555555555L); - - long result = (long)mh.invokeExact(functionSymbol, 666666666L, structSegmt); - Assert.assertEquals(result, 2333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3LongStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - float result = (float)mh.invokeExact(functionSymbol, 6.56F, structSegmt); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(functionSymbol, floatSegmt, structSegmt); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, floatSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_FLOAT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultSegmt.address(), floatSegmt.address()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(functionSymbol, 79.33F, structSegmt); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invokeExact(functionSymbol, 37.88F, structSegmt); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_2() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invokeExact(functionSymbol, 444.44F, structSegmt); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invokeExact(functionSymbol, 666.66F, structSegmt); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3FloatStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - double result = (double)mh.invokeExact(functionSymbol, 3336.333D, structSegmt); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStruct_2() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStruct_2() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18); - elemHandle2.set(structSegmt, 619.777D); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - double result = (double)mh.invokeExact(functionSymbol, 113.567D, structSegmt); - Assert.assertEquals(result, 751.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFloatFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleIntFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19); - - double result = (double)mh.invokeExact(functionSymbol, 216.666D, structSegmt); - Assert.assertEquals(result, 454.221D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, doubleSegmt, structSegmt); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultSegmt.address(), doubleSegmt.address()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(functionSymbol, 66.333D, structSegmt); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_withoutLayoutName_2() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_DOUBLE); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invokeExact(functionSymbol, 37.864D, structSegmt); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_withoutLayoutName_2() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invokeExact(functionSymbol, 444.444D, structSegmt); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE, JAVA_DOUBLE); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_DOUBLE); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invokeExact(functionSymbol, 666.666D, structSegmt); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointer_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStructPointer").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(functionSymbol, structSegmt1, structSegmt2); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStruct_2() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); - MemorySegment functionSymbol = nativeLibLookup.find("add3DoubleStructs_returnStruct").get(); - MethodHandle mh = linker.downcallHandle(fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/InvalidUpCallTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/InvalidUpCallTests.java deleted file mode 100644 index ecad1bf39f9..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/InvalidUpCallTests.java +++ /dev/null @@ -1,266 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.Linker; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) in upcall, - * which verify the illegal cases including the returned segment, etc. - */ -@Test(groups = { "level.sanity" }) -public class InvalidUpCallTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "An exception is thrown from the upcall method") - public void test_throwExceptionFromUpcallMethod() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_throwException, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out IllegalArgumentException from the the upcall method"); - } - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "An exception is thrown from the upcall method") - public void test_nestedUpcall_throwExceptionFromUpcallMethod() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_nestedUpcall, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out IllegalArgumentException from the nested upcall"); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_nullValue, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null value upon return"); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullValueForReturnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_nullValue, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null value upon return"); - } - } - - public void test_nullSegmentForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("validateReturnNullAddrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_nullSegmt, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 11223344); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 55667788); - } - } - - @Test(expectedExceptions = NullPointerException.class) - public void test_nullSegmentForReturnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_nullSegmt, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out NullPointerException in the case of the null segment upon return"); - } - } - - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Heap segment not allowed.*") - public void test_heapSegmentForReturnPtr() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer_heapSegmt, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - fail("Failed to throw out IllegalArgumentException in the case of the heap segment upon return"); - } - } - - public void test_heapSegmentForReturnStruct() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_heapSegmt, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallMHTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallMHTests.java deleted file mode 100644 index 02ecc79fad3..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallMHTests.java +++ /dev/null @@ -1,571 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) intended for - * the situations when the multiple primitive specific upcalls happen within - * the same memory arena.scope() or from different memory arena.scope()s. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallMHTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr1); - Assert.assertEquals(result, true); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - result = (boolean)mh.invoke(true, false, upcallFuncAddr2); - Assert.assertEquals(result, true); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - result = (boolean)mh.invoke(true, false, upcallFuncAddr3); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addTwoBoolsWithOrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - MemorySegment charSegmt1 = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt1, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - MemorySegment charSegmt2 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt2, 'D', upcallFuncAddr2); - Assert.assertEquals(result, 'C'); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - MemorySegment charSegmt3 = allocator.allocate(JAVA_CHAR, 'B'); - result = (char)mh.invoke(charSegmt3, 'D', upcallFuncAddr3); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr1); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr1); - Assert.assertEquals(result, (byte)88); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - result = (byte)mh.invoke((byte)33, upcallFuncAddr2); - Assert.assertEquals(result, (byte)88); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - result = (byte)mh.invoke((byte)33, upcallFuncAddr3); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, (byte)88); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - MemorySegment shortSegmt1 = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr1 = (MemorySegment)mh.invoke(shortSegmt1, (short)555, upcallFuncAddr1); - MemorySegment resultSegmt1 = MemorySegment.ofAddress(resultAddr1.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt1.get(JAVA_SHORT, 0), (short)999); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - MemorySegment shortSegmt2 = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr2 = (MemorySegment)mh.invoke(shortSegmt2, (short)555, upcallFuncAddr2); - MemorySegment resultSegmt2 = MemorySegment.ofAddress(resultAddr2.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt2.get(JAVA_SHORT, 0), (short)999); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - MemorySegment shortSegmt3 = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr3 = (MemorySegment)mh.invoke(shortSegmt3, (short)555, upcallFuncAddr3); - MemorySegment resultSegmt3 = MemorySegment.ofAddress(resultAddr3.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt3.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), (short)999); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), (short)999); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), (short)999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr1); - Assert.assertEquals(result, 222235); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr2); - Assert.assertEquals(result, 222235); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - result = (int)mh.invoke(111112, 111123, upcallFuncAddr3); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr1); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr2); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr3); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(111454, 111398, upcallFuncAddr); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt1, 6666698235L, upcallFuncAddr1); - Assert.assertEquals(result, 12409155659L); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt2, 6666698235L, upcallFuncAddr2); - Assert.assertEquals(result, 12409155659L); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 5742457424L); - result = (long)mh.invoke(longSegmt3, 6666698235L, upcallFuncAddr3); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - float result = (float)mh.invoke(5.74F, upcallFuncAddr1); - Assert.assertEquals(result, 12.53F, 0.01F); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - result = (float)mh.invoke(5.74F, upcallFuncAddr2); - Assert.assertEquals(result, 12.53F, 0.01F); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - result = (float)mh.invoke(5.74F, upcallFuncAddr3); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_SameScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - - MemorySegment upcallFuncAddr1 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr1 = (MemorySegment)mh.invoke(doubleSegmt1, 1262.795D, upcallFuncAddr1); - MemorySegment resultSegmt1 = MemorySegment.ofAddress(resultAddr1.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt1.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - MemorySegment upcallFuncAddr2 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr2 = (MemorySegment)mh.invoke(doubleSegmt2, 1262.795D, upcallFuncAddr2); - MemorySegment resultSegmt2 = MemorySegment.ofAddress(resultAddr2.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt2.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - - MemorySegment upcallFuncAddr3 = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr3 = (MemorySegment)mh.invoke(doubleSegmt3, 1262.795D, upcallFuncAddr3); - MemorySegment resultSegmt3 = MemorySegment.ofAddress(resultAddr3.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt3.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH_DiffScope() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests1.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests1.java deleted file mode 100644 index 398f0f59d1e..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests1.java +++ /dev/null @@ -1,137 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within different memory arena.scope()s, in which case the upcall - * metadata and the generated thunk are allocated separately. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests1 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithDiffScopes() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests2.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests2.java deleted file mode 100644 index a13c7ea35f1..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/MultiUpcallThrdsMHTests2.java +++ /dev/null @@ -1,131 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentScope; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) intended for - * the situation when the multi-threading specific upcalls happen to the same - * upcall method handle within the same memory scope, in which case the upcall - * metadata and the generated thunk are only allocated once and shared among - * these threads. - */ -@Test(groups = { "level.sanity" }) -public class MultiUpcallThrdsMHTests2 implements Thread.UncaughtExceptionHandler { - private volatile Throwable initException; - private static Linker linker = Linker.nativeLinker(); - private static SegmentScope scope = SegmentScope.auto(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test(enabled=false) - @Override - public void uncaughtException(Thread thr, Throwable t) { - initException = t; - } - - @Test - public void test_multiUpcallThrdsWithSameScope() throws Throwable { - Thread thr1 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr1.setUncaughtExceptionHandler(this); - thr1.start(); - - Thread thr2 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111113, 111124, upcallFuncAddr); - Assert.assertEquals(result, 222237); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr2.setUncaughtExceptionHandler(this); - thr2.start(); - - Thread thr3 = new Thread() { - @Override - public void run() { - try { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), scope); - int result = (int)mh.invoke(111114, 111125, upcallFuncAddr); - Assert.assertEquals(result, 222239); - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - }; - thr3.setUncaughtExceptionHandler(this); - thr3.start(); - - thr1.join(); - thr2.join(); - thr3.join(); - - if (initException != null) { - throw new RuntimeException(initException); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithMixedSigStruTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithMixedSigStruTests.java deleted file mode 100644 index 6ce7a38c45f..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithMixedSigStruTests.java +++ /dev/null @@ -1,892 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for the mixed native signatures - * in argument/return struct in upcall, which are not covered in UpcallMHWithStructTests and - * specially designed to validate the native signature types required in the genenerated thunk. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithMixedSigStruTests { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addIntAndIntShortFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntShortFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntShortFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, (short)32766); - - int result = (int)mh.invoke(22334455, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33590565); - } - } - - @Test - public void test_addIntAndShortIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndShortIntFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, (short)32766); - elemHandle2.set(structSegmt, 22446688); - - int result = (int)mh.invoke(11335577, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 33815031); - } - } - - @Test - public void test_addIntAndIntLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11223344); - elemHandle2.set(structSegmt, 667788990011L); - - long result = (long)mh.invoke(22446688, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667822660043L); - } - } - - @Test - public void test_addIntAndLongIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndLongIntFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 667788990011L); - elemHandle2.set(structSegmt, 11223344); - - long result = (long)mh.invoke(1234567, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 667801447922L); - } - } - - @Test - public void test_addDoubleAndIntDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111844.344D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 111111111); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111546.221D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFromStructByUpcallMH() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 18.444F); - elemHandle2.set(structSegmt, 619.777D); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 751.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - public static void test_addDoubleAndDoubleFloatPlusPaddingFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 218.555D); - elemHandle2.set(structSegmt, 19.22F); - - double result = (double)mh.invoke(216.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 454.441D, 0.001D); - } - } - - @Test - public void test_addDoubleAnd2FloatsDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAnd2FloatsDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAnd2FloatsDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDouble2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDouble2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDouble2FloatsFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - double result = (double)mh.invoke(111.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 478.105D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndInt2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt2FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 111111); - elemHandle3.set(structSegmt, 22.33F); - - float result = (float)mh.invoke(55.567F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111200.12F, 0.01F); - } - } - - @Test - public void test_addDoubleAndIntFloatDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntFloatDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntFloatDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatIntDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatIntDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatIntDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22.33F); - elemHandle2.set(structSegmt, 111111111); - elemHandle3.set(structSegmt, 333.444D); - - double result = (double)mh.invoke(555.55D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.324D, 0.001D); - } - } - - @Test - public void test_addDoubleAndLongDoubleFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndLongDoubleFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndLongDoubleFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 22222222222222L); - elemHandle2.set(structSegmt, 33333.444D); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 22222222311110.555D, 0.001D); - } - } - - @Test - public void test_addFloatAndInt3FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndInt3FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndInt3FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 77777777); - elemHandle2.set(structSegmt, 11.22F); - elemHandle3.set(structSegmt, 22.33F); - elemHandle4.set(structSegmt, 44.55F); - - float result = (float)mh.invoke(66.678F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.778F, 0.001F); - } - } - - @Test - public void test_addLongAndLong2FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLong2FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLong2FloatsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 777777777777L); - elemHandle2.set(structSegmt, 11.25F); - elemHandle3.set(structSegmt, 22.75F); - - long result = (long)mh.invoke(555555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1333333333365L); - } - } - - @Test - public void test_addFloatAnd3FloatsIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_INT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAnd3FloatsIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAnd3FloatsIntFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.22F); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 44.55F); - elemHandle4.set(structSegmt, 77777777); - - float result = (float)mh.invoke(66.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 77777921.556F, 0.001F); - } - } - - @Test - public void test_addLongAndFloatLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndFloatLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndFloatLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 55.11F); - elemHandle2.set(structSegmt, 150000000000L); - - long result = (long)mh.invoke(5555555555L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 155555555610L); - } - } - - @Test - public void test_addDoubleAndDoubleFloatIntFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFloatIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFloatIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 333.444D); - elemHandle2.set(structSegmt, 22.33F); - elemHandle3.set(structSegmt, 111111111); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111112022.341D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33333.444D); - elemHandle2.set(structSegmt, 222222222222L); - - double result = (double)mh.invoke(55555.111D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 222222311110.555D, 0.001D); - } - } - - @Test - public void test_addLongAnd2FloatsLongFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAnd2FloatsLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAnd2FloatsLongFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.11F); - elemHandle2.set(structSegmt, 22.11F); - elemHandle3.set(structSegmt, 4444444444L); - - long result = (long)mh.invoke(11111111111L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15555555588L); - } - } - - @Test - public void test_addShortAnd3ShortsCharFromStructByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(3, JAVA_SHORT); - GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAnd3ShortsCharFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAnd3ShortsCharFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1000); - structSegmt.set(JAVA_SHORT, 2, (short)2000); - structSegmt.set(JAVA_SHORT, 4, (short)3000); - structSegmt.set(JAVA_CHAR, 6, 'A'); - - short result = (short)mh.invoke((short)4000, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 10065); - } - } - - @Test - public void test_addFloatAndIntFloatIntFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_FLOAT.withName("elem4")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndIntFloatIntFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndIntFloatIntFloatFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 555555555); - elemHandle2.set(structSegmt, 11.222F); - elemHandle3.set(structSegmt, 666666666); - elemHandle4.set(structSegmt, 33.444F); - - float result = (float)mh.invoke(77.456F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1222222343.122F, 0.001F); - } - } - - @Test - public void test_addDoubleAndIntDoubleFloatFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 7777); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 33.444F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 8584.566D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleIntFromStructByUpcallMH() throws Throwable { - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_INT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleIntFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleIntFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 6666); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 7777); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15217.122D, 0.001D); - } - } - - @Test - public void test_addDoubleAndFloatDoubleFloatFromStructByUpcallMH() throws Throwable { - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")) - : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2"), - JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatDoubleFloatFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatDoubleFloatFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 11.222F); - elemHandle2.set(structSegmt, 218.555D); - elemHandle3.set(structSegmt, 33.444F); - - double result = (double)mh.invoke(555.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 818.788D, 0.001D); - } - } - - @Test - public void test_addDoubleAndIntDoubleLongFromStructByUpcallMH() throws Throwable { - /* The padding in the struct [int, double, long] on AIX/PPC 64-bit is different from - * other platforms as follows: - * 1) there is no padding between int and double. - * 2) there is a 4-byte padding between double and long. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_DOUBLE.withName("elem2"), MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem3")) - : MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(32), - JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntDoubleLongFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntDoubleLongFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - elemHandle1.set(structSegmt, 111111111); - elemHandle2.set(structSegmt, 619.777D); - elemHandle3.set(structSegmt, 888888888888L); - - double result = (double)mh.invoke(113.567D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 889000000732.344D, 0.001D); - } - } - - @Test - public void test_return254BytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("return254BytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_return254BytesFromStruct, - FunctionDescriptor.of(structLayout), arena.scope()); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 254; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } - - @Test - public void test_return4KBytesFromStructByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("return4KBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_return4KBytesFromStruct, - FunctionDescriptor.of(structLayout), arena.scope()); - MemorySegment byteArrStruSegment = (MemorySegment)mh.invoke(allocator, upcallFuncAddr); - for (int i = 0; i < 4096; i++) { - Assert.assertEquals(byteArrStruSegment.get(JAVA_BYTE, i), (byte)i); - } - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithPrimTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithPrimTests.java deleted file mode 100644 index c2f56786cd7..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithPrimTests.java +++ /dev/null @@ -1,739 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for primitive types in upcall. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithPrimTests { - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addTwoBoolsWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolsWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolsWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN), arena.scope()); - boolean result = (boolean)mh.invoke(true, false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPointerWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromPointerWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), arena.scope()); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - boolean result = (boolean)mh.invoke(false, boolSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromNativePtrWithOrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromNativePtrWithOrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPointerWithOr, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), arena.scope()); - boolean result = (boolean)mh.invoke(false, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), arena.scope()); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemorySegment resultAddr = (MemorySegment)mh.invoke(false, boolSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BOOLEAN.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addBoolAndBoolFromPtrWithOr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolFromPtrWithOr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolFromPtrWithOr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BOOLEAN, ADDRESS), arena.scope()); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - MemorySegment resultAddr = (MemorySegment)mh.invoke(false, boolSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); - } - } - - @Test - public void test_addTwoBytesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BytesByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Bytes, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE), arena.scope()); - byte result = (byte)mh.invoke((byte)6, (byte)3, upcallFuncAddr); - Assert.assertEquals(result, 9); - } - } - - @Test - public void test_addByteAndByteFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)7); - byte result = (byte)mh.invoke((byte)8, byteSegmt, upcallFuncAddr); - Assert.assertEquals(result, 15); - } - } - - @Test - public void test_addByteAndByteFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - byte result = (byte)mh.invoke((byte)33, upcallFuncAddr); - Assert.assertEquals(result, 88); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemorySegment resultAddr = (MemorySegment)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_addByteAndByteFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndByteFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndByteFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_BYTE, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)35); - MemorySegment resultAddr = (MemorySegment)mh.invoke((byte)47, byteSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 82); - } - } - - @Test - public void test_createNewCharFrom2CharsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFrom2CharsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFrom2Chars, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR), arena.scope()); - char result = (char)mh.invoke('B', 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - char result = (char)mh.invoke(charSegmt, 'D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPointer, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR), arena.scope()); - char result = (char)mh.invoke('D', upcallFuncAddr); - Assert.assertEquals(result, 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemorySegment resultAddr = (MemorySegment)mh.invoke(charSegmt, 'D', upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_CHAR.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_createNewCharFromCharAndCharFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("createNewCharFromCharAndCharFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_createNewCharFromCharAndCharFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_CHAR), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'B'); - MemorySegment resultAddr = (MemorySegment)mh.invoke(charSegmt, 'D', upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_CHAR.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); - } - } - - @Test - public void test_addTwoShortsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Shorts, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT), arena.scope()); - short result = (short)mh.invoke((short)1111, (short)2222, upcallFuncAddr); - Assert.assertEquals(result, 3333); - } - } - - @Test - public void test_addShortAndShortFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)2222); - short result = (short)mh.invoke(shortSegmt, (short)3333, upcallFuncAddr); - Assert.assertEquals(result, 5555); - } - } - - @Test - public void test_addShortAndShortFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPointer, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, JAVA_SHORT), arena.scope()); - short result = (short)mh.invoke((short)789, upcallFuncAddr); - Assert.assertEquals(result, 1245); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addShortAndShortFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)444); - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, (short)555, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 999); - } - } - - @Test - public void test_addTwoIntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, upcallFuncAddr); - Assert.assertEquals(result, 222235); - } - } - - @Test - public void test_addIntAndIntFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - int result = (int)mh.invoke(333321, intSegmt, upcallFuncAddr); - Assert.assertEquals(result, 555536); - } - } - - @Test - public void test_addIntAndIntFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - int result = (int)mh.invoke(222222, upcallFuncAddr); - Assert.assertEquals(result, 666666); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemorySegment resultAddr = (MemorySegment)mh.invoke(333321, intSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_addIntAndIntFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_INT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 222215); - MemorySegment resultAddr = (MemorySegment)mh.invoke(333321, intSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 555536); - } - } - - @Test - public void test_add3IntsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3IntsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3Ints, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT), arena.scope()); - int result = (int)mh.invoke(111112, 111123, 111124, upcallFuncAddr); - Assert.assertEquals(result, 333359); - } - } - - @Test - public void test_addIntAndCharByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndCharByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndChar, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR), arena.scope()); - int result = (int)mh.invoke(555558, 'A', upcallFuncAddr); - Assert.assertEquals(result, 555623); - } - } - - @Test - public void test_addTwoIntsReturnVoidByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsReturnVoidByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntsReturnVoid, - FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT), arena.scope()); - mh.invoke(44454, 333398, upcallFuncAddr); - } - } - - @Test - public void test_addTwoLongsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Longs, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, JAVA_LONG), arena.scope()); - long result = (long)mh.invoke(333333222222L, 111111555555L, upcallFuncAddr); - Assert.assertEquals(result, 444444777777L); - } - } - - @Test - public void test_addLongAndLongFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - long result = (long)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - Assert.assertEquals(result, 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPointer, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG), arena.scope()); - long result = (long)mh.invoke(5555555555L, upcallFuncAddr); - Assert.assertEquals(result, 8888888888L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemorySegment resultAddr = (MemorySegment)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addLongAndLongFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_LONG), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 5742457424L); - MemorySegment resultAddr = (MemorySegment)mh.invoke(longSegmt, 6666698235L, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 12409155659L); - } - } - - @Test - public void test_addTwoFloatsByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatsByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Floats, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT), arena.scope()); - float result = (float)mh.invoke(15.74F, 16.79F, upcallFuncAddr); - Assert.assertEquals(result, 32.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - float result = (float)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - float result = (float)mh.invoke(5.74F, upcallFuncAddr); - Assert.assertEquals(result, 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemorySegment resultAddr = (MemorySegment)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_FLOAT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, JAVA_FLOAT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 6.79F); - MemorySegment resultAddr = (MemorySegment)mh.invoke(5.74F, floatSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_FLOAT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 12.53F, 0.01F); - } - } - - @Test - public void test_add2DoublesByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoublesByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2Doubles, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE), arena.scope()); - double result = (double)mh.invoke(159.748D, 262.795D, upcallFuncAddr); - Assert.assertEquals(result, 422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPointerByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - double result = (double)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromNativePtrByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromNativePtrByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPointer, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, JAVA_DOUBLE), arena.scope()); - double result = (double)mh.invoke(1262.795D, upcallFuncAddr); - Assert.assertEquals(result, 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoubleFromPtr_RetArgPtr_ByUpcallMH() throws Throwable { - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromPtr_RetPtr_ByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromPtr_RetArgPtr, - FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_DOUBLE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 1159.748D); - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, 1262.795D, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 2422.543D, 0.001D); - } - } - - @Test - public void test_qsortByUpcallMH() throws Throwable { - int expectedArray[] = {11, 12, 13, 14, 15, 16, 17}; - int expectedArrayLength = expectedArray.length; - - FunctionDescriptor fd = FunctionDescriptor.ofVoid(ADDRESS, JAVA_INT, JAVA_INT, ADDRESS); - MemorySegment functionSymbol = defaultLibLookup.find("qsort").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_compare, - FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment arraySegmt = allocator.allocateArray(JAVA_INT, new int[]{17, 14, 13, 16, 15, 12, 11}); - mh.invoke(arraySegmt, 7, 4, upcallFuncAddr); - int[] sortedArray = arraySegmt.toArray(JAVA_INT); - for (int index = 0; index < expectedArrayLength; index++) { - Assert.assertEquals(sortedArray[index], expectedArray[index]); - } - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithStructTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithStructTests.java deleted file mode 100644 index 16b67f3da8e..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMHWithStructTests.java +++ /dev/null @@ -1,2948 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for argument/return struct in upcall. - * - * Note: the padding elements in the struct are only required by RI or VarHandle (accessing the - * data address) while they are totally ignored in OpenJ9 given the padding/alignment are - * computed by libffi automatically in native. - */ -@Test(groups = { "level.sanity" }) -public class UpcallMHWithStructTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - /* The padding of struct is not required on Linux/s390x and Windows/x64 */ - private static boolean isStructPaddingNotRequired = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")) - || osName.contains("linux") && arch.equals("s390x"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addBoolAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAnd20BoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, - JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN, JAVA_BOOLEAN - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAnd20BoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAnd20BoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - structSegmt.set(JAVA_BOOLEAN, 5, true); - structSegmt.set(JAVA_BOOLEAN, 6, false); - structSegmt.set(JAVA_BOOLEAN, 7, true); - structSegmt.set(JAVA_BOOLEAN, 8, false); - structSegmt.set(JAVA_BOOLEAN, 9, true); - structSegmt.set(JAVA_BOOLEAN, 10, false); - structSegmt.set(JAVA_BOOLEAN, 11, true); - structSegmt.set(JAVA_BOOLEAN, 12, false); - structSegmt.set(JAVA_BOOLEAN, 13, true); - structSegmt.set(JAVA_BOOLEAN, 14, false); - structSegmt.set(JAVA_BOOLEAN, 15, true); - structSegmt.set(JAVA_BOOLEAN, 16, false); - structSegmt.set(JAVA_BOOLEAN, 17, true); - structSegmt.set(JAVA_BOOLEAN, 18, false); - structSegmt.set(JAVA_BOOLEAN, 19, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, structLayout), arena.scope()); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, true); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, false); - boolHandle2.set(structSegmt, true); - - boolean result = (boolean)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, false); - } - } - - @Test - public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN); - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, arena.scope()); - boolSegmt.set(JAVA_BOOLEAN, 0, false); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(boolSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BOOLEAN.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); - Assert.assertEquals(resultSegmt.address(), boolSegmt.address()); - } - } - - @Test - public void test_addBoolAndBoolsFromStructPointerWithXorByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructPointerWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructPointerWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - boolHandle1.set(structSegmt, true); - boolHandle2.set(structSegmt, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXorByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXorByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedStructWithXor_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, true); - structSegmt.set(JAVA_BOOLEAN, 1, false); - structSegmt.set(JAVA_BOOLEAN, 2, true); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(boolArray.withName("array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - - boolean result = (boolean)mh.invoke(false, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BOOLEAN.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BOOLEAN, 0, false); - structSegmt.set(JAVA_BOOLEAN, 1, true); - structSegmt.set(JAVA_BOOLEAN, 2, false); - structSegmt.set(JAVA_BOOLEAN, 3, true); - structSegmt.set(JAVA_BOOLEAN, 4, false); - - boolean result = (boolean)mh.invoke(true, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - } - } - - @Test - public void test_add2BoolStructsWithXor_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), false); - Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); - } - } - - @Test - public void test_add3BoolStructsWithXor_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3")) : MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"), - JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle boolHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3BoolStructsWithXor_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3BoolStructsWithXor_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt1, true); - boolHandle2.set(structSegmt1, false); - boolHandle3.set(structSegmt1, true); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - boolHandle1.set(structSegmt2, true); - boolHandle2.set(structSegmt2, true); - boolHandle3.set(structSegmt2, false); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(boolHandle1.get(resultSegmt), false); - Assert.assertEquals(boolHandle2.get(resultSegmt), true); - Assert.assertEquals(boolHandle3.get(resultSegmt), true); - } - } - - @Test - public void test_addByteAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)8); - byteHandle2.set(structSegmt, (byte)9); - - byte result = (byte)mh.invoke((byte)6, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23); - } - } - - @Test - public void test_addByteAnd20BytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, - JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE, JAVA_BYTE - ); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAnd20BytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAnd20BytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)1); - structSegmt.set(JAVA_BYTE, 1, (byte)2); - structSegmt.set(JAVA_BYTE, 2, (byte)3); - structSegmt.set(JAVA_BYTE, 3, (byte)4); - structSegmt.set(JAVA_BYTE, 4, (byte)5); - structSegmt.set(JAVA_BYTE, 5, (byte)6); - structSegmt.set(JAVA_BYTE, 6, (byte)7); - structSegmt.set(JAVA_BYTE, 7, (byte)8); - structSegmt.set(JAVA_BYTE, 8, (byte)9); - structSegmt.set(JAVA_BYTE, 9, (byte)10); - structSegmt.set(JAVA_BYTE, 10, (byte)1); - structSegmt.set(JAVA_BYTE, 11, (byte)2); - structSegmt.set(JAVA_BYTE, 12, (byte)3); - structSegmt.set(JAVA_BYTE, 13, (byte)4); - structSegmt.set(JAVA_BYTE, 14, (byte)5); - structSegmt.set(JAVA_BYTE, 15, (byte)6); - structSegmt.set(JAVA_BYTE, 16, (byte)7); - structSegmt.set(JAVA_BYTE, 17, (byte)8); - structSegmt.set(JAVA_BYTE, 18, (byte)9); - structSegmt.set(JAVA_BYTE, 19, (byte)10); - - byte result = (byte)mh.invoke((byte)11, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 121); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)18); - byteHandle2.set(structSegmt, (byte)19); - - byte result = (byte)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49); - } - } - - @Test - public void test_addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteFromPointerAndBytesFromStruct_returnBytePointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteFromPointerAndBytesFromStruct_returnBytePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte)12); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)14); - byteHandle2.set(structSegmt, (byte)16); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(byteSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_BYTE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 42); - Assert.assertEquals(resultSegmt.address(), byteSegmt.address()); - } - } - - @Test - public void test_addByteAndBytesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructPointer, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)11); - byteHandle2.set(structSegmt, (byte)12); - byte result = (byte)mh.invoke((byte)13, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 36); - } - } - - @Test - public void test_addByteAndBytesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)46, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 112); - } - } - - @Test - public void test_addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)24); - structSegmt.set(JAVA_BYTE, 2, (byte)36); - - byte result = (byte)mh.invoke((byte)48, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 120); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArrayByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(byteArray.withName("array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)22); - structSegmt.set(JAVA_BYTE, 2, (byte)33); - - byte result = (byte)mh.invoke((byte)14, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 80); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - byteArray.withName("array_elem2"), MemoryLayout.paddingLayout(8)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedByteArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - - byte result = (byte)mh.invoke((byte)18, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 60); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2")) : MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), - JAVA_BYTE.withName("elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)11); - structSegmt.set(JAVA_BYTE, 1, (byte)12); - structSegmt.set(JAVA_BYTE, 2, (byte)13); - structSegmt.set(JAVA_BYTE, 3, (byte)14); - structSegmt.set(JAVA_BYTE, 4, (byte)15); - - byte result = (byte)mh.invoke((byte)16, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 81); - } - } - - @Test - public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_BYTE, 0, (byte)12); - structSegmt.set(JAVA_BYTE, 1, (byte)14); - structSegmt.set(JAVA_BYTE, 2, (byte)16); - structSegmt.set(JAVA_BYTE, 3, (byte)18); - structSegmt.set(JAVA_BYTE, 4, (byte)20); - - byte result = (byte)mh.invoke((byte)22, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 102); - } - } - - @Test - public void test_add1ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add1ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add1ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - } - } - - @Test - public void test_add2ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - } - } - - @Test - public void test_add2ByteStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), 49); - Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), 24); - } - } - - @Test - public void test_add3ByteStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3")) : MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3ByteStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ByteStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)25); - byteHandle2.set(structSegmt1, (byte)11); - byteHandle3.set(structSegmt1, (byte)12); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)24); - byteHandle2.set(structSegmt2, (byte)13); - byteHandle3.set(structSegmt2, (byte)16); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((byte)byteHandle1.get(resultSegmt), (byte)49); - Assert.assertEquals((byte)byteHandle2.get(resultSegmt), (byte)24); - Assert.assertEquals((byte)byteHandle3.get(resultSegmt), (byte)28); - } - } - - @Test - public void test_addCharAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'A'); - charHandle2.set(structSegmt, 'B'); - - char result = (char)mh.invoke('C', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'D'); - } - } - - @Test - public void test_addCharAnd10CharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, - JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR, JAVA_CHAR); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAnd10CharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAnd10CharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'A'); - structSegmt.set(JAVA_CHAR, 4, 'B'); - structSegmt.set(JAVA_CHAR, 6, 'B'); - structSegmt.set(JAVA_CHAR, 8, 'C'); - structSegmt.set(JAVA_CHAR, 10, 'C'); - structSegmt.set(JAVA_CHAR, 12, 'D'); - structSegmt.set(JAVA_CHAR, 14, 'D'); - structSegmt.set(JAVA_CHAR, 16, 'E'); - structSegmt.set(JAVA_CHAR, 18, 'E'); - - char result = (char)mh.invoke('A', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'U'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct, - FunctionDescriptor.of(JAVA_CHAR, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - char result = (char)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'M'); - } - } - - @Test - public void test_addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharFromPointerAndCharsFromStruct_returnCharPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharFromPointerAndCharsFromStruct_returnCharPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment charSegmt = allocator.allocate(JAVA_CHAR, 'D'); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'E'); - charHandle2.set(structSegmt, 'F'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(charSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_CHAR.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'M'); - Assert.assertEquals(resultSegmt.address(), charSegmt.address()); - } - } - - @Test - public void test_addCharAndCharsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructPointer, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - charHandle1.set(structSegmt, 'H'); - charHandle2.set(structSegmt, 'I'); - - char result = (char)mh.invoke('G', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'V'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - - char result = (char)mh.invoke('H', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'W'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArrayByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2")) : MemoryLayout.structLayout(charArray.withName("array_elem1"), - JAVA_CHAR.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - charArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedCharArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'A'); - structSegmt.set(JAVA_CHAR, 2, 'B'); - structSegmt.set(JAVA_CHAR, 4, 'C'); - - char result = (char)mh.invoke('D', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'G'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), - structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_CHAR, 0, 'E'); - structSegmt.set(JAVA_CHAR, 2, 'F'); - structSegmt.set(JAVA_CHAR, 4, 'G'); - structSegmt.set(JAVA_CHAR, 6, 'H'); - structSegmt.set(JAVA_CHAR, 8, 'I'); - - char result = (char)mh.invoke('J', structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 'h'); - } - } - - @Test - public void test_add2CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'C'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'E'); - } - } - - @Test - public void test_add2CharStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'C'); - charHandle2.set(structSegmt2, 'D'); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); - Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); - } - } - - @Test - public void test_add3CharStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3")) : MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3CharStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3CharStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - charHandle1.set(structSegmt1, 'A'); - charHandle2.set(structSegmt1, 'B'); - charHandle3.set(structSegmt1, 'C'); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - charHandle1.set(structSegmt2, 'B'); - charHandle2.set(structSegmt2, 'C'); - charHandle3.set(structSegmt2, 'D'); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(charHandle1.get(resultSegmt), 'B'); - Assert.assertEquals(charHandle2.get(resultSegmt), 'D'); - Assert.assertEquals(charHandle3.get(resultSegmt), 'F'); - } - } - - @Test - public void test_addShortAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)888); - shortHandle2.set(structSegmt, (short)999); - - short result = (short)mh.invoke((short)777, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2664); - } - } - - @Test - public void test_addShortAnd10ShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, - JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT, JAVA_SHORT); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAnd10ShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAnd10ShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)10); - structSegmt.set(JAVA_SHORT, 2, (short)20); - structSegmt.set(JAVA_SHORT, 4, (short)30); - structSegmt.set(JAVA_SHORT, 6, (short)40); - structSegmt.set(JAVA_SHORT, 8, (short)50); - structSegmt.set(JAVA_SHORT, 10, (short)60); - structSegmt.set(JAVA_SHORT, 12, (short)70); - structSegmt.set(JAVA_SHORT, 14, (short)80); - structSegmt.set(JAVA_SHORT, 16, (short)90); - structSegmt.set(JAVA_SHORT, 18, (short)100); - - short result = (short)mh.invoke((short)110, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 660); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - short result = (short)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 3349); - } - } - - @Test - public void test_addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortFromPointerAndShortsFromStruct_returnShortPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortFromPointerAndShortsFromStruct_returnShortPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment shortSegmt = allocator.allocate(JAVA_SHORT, (short)1112); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)1118); - shortHandle2.set(structSegmt, (short)1119); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(shortSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_SHORT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 3349); - Assert.assertEquals(resultSegmt.address(), shortSegmt.address()); - } - } - - @Test - public void test_addShortAndShortsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructPointer, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)2222); - shortHandle2.set(structSegmt, (short)4444); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13332); - } - } - - @Test - public void test_addShortAndShortsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)331); - structSegmt.set(JAVA_SHORT, 2, (short)333); - structSegmt.set(JAVA_SHORT, 4, (short)335); - - short result = (short)mh.invoke((short)337, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1336); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArrayByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2")) : MemoryLayout.structLayout(shortArray.withName("array_elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - shortArray.withName("array_elem2"), MemoryLayout.paddingLayout(16)); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedShortArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - - short result = (short)mh.invoke((short)4444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11110); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_SHORT, 0, (short)1111); - structSegmt.set(JAVA_SHORT, 2, (short)2222); - structSegmt.set(JAVA_SHORT, 4, (short)3333); - structSegmt.set(JAVA_SHORT, 6, (short)4444); - structSegmt.set(JAVA_SHORT, 8, (short)5555); - - short result = (short)mh.invoke((short)6666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23331); - } - } - - @Test - public void test_add2ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)734); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)712); - } - } - - @Test - public void test_add2ShortStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)356); - shortHandle2.set(structSegmt1, (short)345); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)378); - shortHandle2.set(structSegmt2, (short)367); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 734); - Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 712); - } - } - - @Test - public void test_add3ShortStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3")) : MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3ShortStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ShortStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)325); - shortHandle2.set(structSegmt1, (short)326); - shortHandle3.set(structSegmt1, (short)327); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)334); - shortHandle2.set(structSegmt2, (short)335); - shortHandle3.set(structSegmt2, (short)336); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((short)shortHandle1.get(resultSegmt), (short)659); - Assert.assertEquals((short)shortHandle2.get(resultSegmt), (short)661); - Assert.assertEquals((short)shortHandle3.get(resultSegmt), (short)663); - } - } - - @Test - public void test_addIntAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1122334); - intHandle2.set(structSegmt, 1234567); - - int result = (int)mh.invoke(2244668, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 4601569); - } - } - - @Test - public void test_addIntAnd5IntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), - JAVA_INT.withName("elem3"), JAVA_INT.withName("elem4"), JAVA_INT.withName("elem5")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle intHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle intHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAnd5IntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAnd5IntsFromStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1111111); - intHandle2.set(structSegmt, 2222222); - intHandle3.set(structSegmt, 3333333); - intHandle4.set(structSegmt, 2222222); - intHandle5.set(structSegmt, 1111111); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 14444443); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct, - FunctionDescriptor.of(JAVA_INT, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 7654321); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 1234567); - intHandle2.set(structSegmt, 2468024); - - int result = (int)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11356912); - } - } - - @Test - public void test_addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntFromPointerAndIntsFromStruct_returnIntPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntFromPointerAndIntsFromStruct_returnIntPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 4455666); - intHandle2.set(structSegmt, 7788999); - MemorySegment resultAddr = (MemorySegment)mh.invoke(intSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 13366998); - Assert.assertEquals(resultSegmt.address(), intSegmt.address()); - } - } - - @Test - public void test_addIntAndIntsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructPointer, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - intHandle1.set(structSegmt, 11121314); - intHandle2.set(structSegmt, 15161718); - - int result = (int)mh.invoke(19202122, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 45485154); - } - } - - @Test - public void test_addIntAndIntsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_INT.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 21222324); - structSegmt.set(JAVA_INT, 4, 25262728); - structSegmt.set(JAVA_INT, 8, 29303132); - - int result = (int)mh.invoke(33343536, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 109131720); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArrayByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedIntArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - - int result = (int)mh.invoke(4444444, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 11111110); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout intStruct = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_INT, 0, 1111111); - structSegmt.set(JAVA_INT, 4, 2222222); - structSegmt.set(JAVA_INT, 8, 3333333); - structSegmt.set(JAVA_INT, 12, 4444444); - structSegmt.set(JAVA_INT, 16, 5555555); - - int result = (int)mh.invoke(6666666, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23333331); - } - } - - @Test - public void test_add2IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 110224466); - Assert.assertEquals(intHandle2.get(resultSegmt), 89113354); - } - } - - @Test - public void test_add2IntStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 99001122); - intHandle2.set(structSegmt2, 33445566); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); - } - } - - @Test - public void test_add3IntStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3IntStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 11223344); - intHandle2.set(structSegmt1, 55667788); - intHandle3.set(structSegmt1, 99001122); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 33445566); - intHandle2.set(structSegmt2, 77889900); - intHandle3.set(structSegmt2, 44332211); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(intHandle1.get(resultSegmt), 44668910); - Assert.assertEquals(intHandle2.get(resultSegmt), 133557688); - Assert.assertEquals(intHandle3.get(resultSegmt), 143333333); - } - } - - @Test - public void test_addLongAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 1234567890L); - longHandle2.set(structSegmt, 9876543210L); - - long result = (long)mh.invoke(2468024680L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 13579135780L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct, - FunctionDescriptor.of(JAVA_LONG, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1111111111L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 3333333333L); - longHandle2.set(structSegmt, 5555555555L); - - long result = (long)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 9999999999L); - } - } - - @Test - public void test_addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongFromPointerAndLongsFromStruct_returnLongPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongFromPointerAndLongsFromStruct_returnLongPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 6677889900L); - longHandle2.set(structSegmt, 1234567890L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(longSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 9034792245L); - Assert.assertEquals(resultSegmt.address(), longSegmt.address()); - } - } - - @Test - public void test_addLongAndLongsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructPointer, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - longHandle1.set(structSegmt, 224466880022L); - longHandle2.set(structSegmt, 446688002244L); - - long result = (long)mh.invoke(668800224466L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1339955106732L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 135791357913L); - structSegmt.set(JAVA_LONG, 8, 246802468024L); - structSegmt.set(JAVA_LONG, 16,112233445566L); - - long result = (long)mh.invoke(778899001122L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1273726272625L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArrayByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedLongArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - - long result = (long)mh.invoke(44444444444L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 111111111110L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_LONG, 0, 11111111111L); - structSegmt.set(JAVA_LONG, 8, 22222222222L); - structSegmt.set(JAVA_LONG, 16, 33333333333L); - structSegmt.set(JAVA_LONG, 24, 44444444444L); - structSegmt.set(JAVA_LONG, 32, 55555555555L); - - long result = (long)mh.invoke(66666666666L, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 233333333331L); - } - } - - @Test - public void test_add2LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - } - } - - @Test - public void test_add2LongStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 5566778899L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 9900112233L); - longHandle2.set(structSegmt2, 3344556677L); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 11022446688L); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 8911335576L); - } - } - - @Test - public void test_add3LongStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle longHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3LongStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3LongStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 987654321987L); - longHandle2.set(structSegmt1, 123456789123L); - longHandle3.set(structSegmt1, 112233445566L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 224466880022L); - longHandle2.set(structSegmt2, 113355779911L); - longHandle3.set(structSegmt2, 778899001122L); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L); - Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L); - Assert.assertEquals(longHandle3.get(resultSegmt), 891132446688L); - } - } - - @Test - public void test_addFloatAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 8.12F); - floatHandle2.set(structSegmt, 9.24F); - - float result = (float)mh.invoke(6.56F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 23.92F, 0.01F); - } - } - - @Test - public void test_addFloatAnd5FloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), - JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4"), JAVA_FLOAT.withName("elem5")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle floatHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle floatHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAnd5FloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAnd5FloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 1.01F); - floatHandle2.set(structSegmt, 1.02F); - floatHandle3.set(structSegmt, 1.03F); - floatHandle4.set(structSegmt, 1.04F); - floatHandle5.set(structSegmt, 1.05F); - - float result = (float)mh.invoke(1.06F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 6.21F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct, - FunctionDescriptor.of(JAVA_FLOAT, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - float result = (float)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 49.69F, 0.01F); - } - } - - @Test - public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatFromPointerAndFloatsFromStruct_returnFloatPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment floatSegmt = allocator.allocate(JAVA_FLOAT, 12.12F); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 18.23F); - floatHandle2.set(structSegmt, 19.34F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(floatSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_FLOAT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.69F, 0.01F); - Assert.assertEquals(resultSegmt.address(), floatSegmt.address()); - } - } - - @Test - public void test_addFloatAndFloatsFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructPointer, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - floatHandle1.set(structSegmt, 35.11F); - floatHandle2.set(structSegmt, 46.22F); - - float result = (float)mh.invoke(79.33F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 160.66F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 31.22F); - structSegmt.set(JAVA_FLOAT, 4, 33.44F); - structSegmt.set(JAVA_FLOAT, 8, 35.66F); - - float result = (float)mh.invoke(37.88F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.2F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - - float result = (float)mh.invoke(444.44F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.1F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_FLOAT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_FLOAT, 0, 111.11F); - structSegmt.set(JAVA_FLOAT, 4, 222.22F); - structSegmt.set(JAVA_FLOAT, 8, 333.33F); - structSegmt.set(JAVA_FLOAT, 12, 444.44F); - structSegmt.set(JAVA_FLOAT, 16, 555.55F); - - float result = (float)mh.invoke(666.66F, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.31F, 0.01F); - } - } - - @Test - public void test_add3FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - floatHandle3.set(structSegmt1, 45.67F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - floatHandle3.set(structSegmt2, 69.72F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - Assert.assertEquals((float)floatHandle3.get(resultSegmt), 115.39, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((float)floatHandle1.get(resultSegmt), 49.46F, 0.01F); - Assert.assertEquals((float)floatHandle2.get(resultSegmt), 24.68F, 0.01F); - } - } - - @Test - public void test_add2FloatStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 25.12F); - floatHandle2.set(structSegmt1, 11.23F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 24.34F); - floatHandle2.set(structSegmt2, 13.45F); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); - Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 24.68F, 0.01F); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 2228.111D); - doubleHandle2.set(structSegmt, 2229.221D); - - double result = (double)mh.invoke(3336.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 7793.665D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 112.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 118.456D); - doubleHandle2.set(structSegmt, 119.789D); - - double result = (double)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 350.368D, 0.001D); - } - } - - @Test - public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt = allocator.allocate(JAVA_DOUBLE, 212.123D); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 218.456D); - doubleHandle2.set(structSegmt, 219.789D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(doubleSegmt, structSegmt, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 650.368D, 0.001D); - Assert.assertEquals(resultSegmt.address(), doubleSegmt.address()); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructPointer, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt, 22.111D); - doubleHandle2.set(structSegmt, 44.222D); - - double result = (double)mh.invoke(66.333D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 132.666D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStructByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH() throws Throwable { - GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedStruct_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedStruct_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 31.789D); - structSegmt.set(JAVA_DOUBLE, 8, 33.456D); - structSegmt.set(JAVA_DOUBLE, 16, 35.123D); - - double result = (double)mh.invoke(37.864D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 138.232D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH() throws Throwable { - SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - - double result = (double)mh.invoke(444.444D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 1111.11D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArrayByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH() throws Throwable { - GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct); - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrderByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - structSegmt.set(JAVA_DOUBLE, 0, 111.111D); - structSegmt.set(JAVA_DOUBLE, 8, 222.222D); - structSegmt.set(JAVA_DOUBLE, 16, 333.333D); - structSegmt.set(JAVA_DOUBLE, 24, 444.444D); - structSegmt.set(JAVA_DOUBLE, 32, 555.555D); - - double result = (double)mh.invoke(666.666D, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, 2333.331D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - } - } - - @Test - public void test_add2DoubleStructs_returnStructPointerByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStructPointerByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStructPointer, - FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - - MemorySegment resultAddr = (MemorySegment)mh.invoke(structSegmt1, structSegmt2, upcallFuncAddr); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), structLayout.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); - } - } - - @Test - public void test_add3DoubleStructs_returnStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle doubleHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add3DoubleStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3DoubleStructs_returnStruct, - FunctionDescriptor.of(structLayout, structLayout, structLayout), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11.222D); - doubleHandle2.set(structSegmt1, 22.333D); - doubleHandle3.set(structSegmt1, 33.123D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 33.444D); - doubleHandle2.set(structSegmt2, 44.555D); - doubleHandle3.set(structSegmt2, 55.456D); - - MemorySegment resultSegmt = (MemorySegment)mh.invoke(allocator, structSegmt1, structSegmt2, upcallFuncAddr); - Assert.assertEquals((double)doubleHandle1.get(resultSegmt), 44.666D, 0.001D); - Assert.assertEquals((double)doubleHandle2.get(resultSegmt), 66.888D, 0.001D); - Assert.assertEquals((double)doubleHandle3.get(resultSegmt), 88.579D, 0.001D); - } - } - - @Test - public void test_addNegBytesFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addNegBytesFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addNegBytesFromStruct, - FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, JAVA_BYTE, JAVA_BYTE), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - byteHandle1.set(structSegmt, (byte)-8); - byteHandle2.set(structSegmt, (byte)-9); - - byte result = (byte)mh.invoke((byte)-6, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, (byte)-40); - } - } - - @Test - public void test_addNegShortsFromStructByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("addNegShortsFromStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addNegShortsFromStruct, - FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, JAVA_SHORT, JAVA_SHORT), arena.scope()); - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt = allocator.allocate(structLayout); - shortHandle1.set(structSegmt, (short)-888); - shortHandle2.set(structSegmt, (short)-999); - - short result = (short)mh.invoke((short)-777, structSegmt, upcallFuncAddr); - Assert.assertEquals(result, (short)-4551); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMethodHandles.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMethodHandles.java deleted file mode 100644 index 30d46bd0903..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/upcall/UpcallMethodHandles.java +++ /dev/null @@ -1,2072 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.upcall; - -import org.testng.Assert; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.Linker; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SegmentScope; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.ValueLayout; - -import static java.lang.foreign.Linker.*; -import static java.lang.foreign.ValueLayout.*; - -/** - * The helper class that contains all upcall method handles with primitive types or struct - * as arguments. - */ -public class UpcallMethodHandles { - private static final Lookup lookup = MethodHandles.lookup(); - private static SegmentScope scope = SegmentScope.auto(); - private static SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - - static final MethodType MT_Bool_Bool_MemSegmt = methodType(boolean.class, boolean.class, MemorySegment.class); - static final MethodType MT_Segmt_Bool_MemSegmt = methodType(MemorySegment.class, boolean.class, MemorySegment.class); - static final MethodType MT_Char_Char_MemSegmt = methodType(char.class, char.class, MemorySegment.class); - static final MethodType MT_Segmt_MemSegmt_Char = methodType(MemorySegment.class, MemorySegment.class, char.class); - static final MethodType MT_Byte_Byte_MemSegmt = methodType(byte.class, byte.class, MemorySegment.class); - static final MethodType MT_Segmt_Byte_MemSegmt = methodType(MemorySegment.class, byte.class, MemorySegment.class); - static final MethodType MT_Short_Short_MemSegmt = methodType(short.class, short.class, MemorySegment.class); - static final MethodType MT_Segmt_MemSegmt_Short = methodType(MemorySegment.class, MemorySegment.class, short.class); - static final MethodType MT_Int_Int_MemSegmt = methodType(int.class, int.class, MemorySegment.class); - static final MethodType MT_Segmt_Int_MemSegmt = methodType(MemorySegment.class, int.class, MemorySegment.class); - static final MethodType MT_Long_Long_MemSegmt = methodType(long.class, long.class, MemorySegment.class); - static final MethodType MT_Segmt_MemSegmt_Long = methodType(MemorySegment.class, MemorySegment.class, long.class); - static final MethodType MT_Long_Int_MemSegmt = methodType(long.class, int.class, MemorySegment.class); - static final MethodType MT_Float_Float_MemSegmt = methodType(float.class, float.class, MemorySegment.class); - static final MethodType MT_Segmt_Float_MemSegmt = methodType(MemorySegment.class, float.class, MemorySegment.class); - static final MethodType MT_Double_Double_MemSegmt = methodType(double.class, double.class, MemorySegment.class); - static final MethodType MT_Segmt_MemSegmt_Double = methodType(MemorySegment.class, MemorySegment.class, double.class); - static final MethodType MT_Segmt_MemSegmt_MemSegmt = methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - static final MethodType MT_MemSegmt_MemSegmt_MemSegmt = methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class); - static final MethodType MT_MemSegmt = methodType(MemorySegment.class); - - public static final MethodHandle MH_add2BoolsWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPointerWithOr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetPtr; - public static final MethodHandle MH_addBoolAndBoolFromPtrWithOr_RetArgPtr; - public static final MethodHandle MH_createNewCharFrom2Chars; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPointer; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetPtr; - public static final MethodHandle MH_createNewCharFromCharAndCharFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Bytes; - public static final MethodHandle MH_addByteAndByteFromPointer; - public static final MethodHandle MH_addByteAndByteFromPtr_RetPtr; - public static final MethodHandle MH_addByteAndByteFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Shorts; - public static final MethodHandle MH_addShortAndShortFromPointer; - public static final MethodHandle MH_addShortAndShortFromPtr_RetPtr; - public static final MethodHandle MH_addShortAndShortFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Ints; - public static final MethodHandle MH_addIntAndIntFromPointer; - public static final MethodHandle MH_addIntAndIntFromPtr_RetPtr; - public static final MethodHandle MH_addIntAndIntFromPtr_RetArgPtr; - public static final MethodHandle MH_add3Ints; - public static final MethodHandle MH_addIntAndChar; - public static final MethodHandle MH_add2IntsReturnVoid; - public static final MethodHandle MH_add2Longs; - public static final MethodHandle MH_addLongAndLongFromPointer; - public static final MethodHandle MH_addLongAndLongFromPtr_RetPtr; - public static final MethodHandle MH_addLongAndLongFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Floats; - public static final MethodHandle MH_addFloatAndFloatFromPointer; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetPtr; - public static final MethodHandle MH_addFloatAndFloatFromPtr_RetArgPtr; - public static final MethodHandle MH_add2Doubles; - public static final MethodHandle MH_addDoubleAndDoubleFromPointer; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetPtr; - public static final MethodHandle MH_addDoubleAndDoubleFromPtr_RetArgPtr; - public static final MethodHandle MH_compare; - - public static final MethodHandle MH_addBoolAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolAnd20BoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor; - public static final MethodHandle MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer; - public static final MethodHandle MH_addBoolAndBoolsFromStructPointerWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor; - public static final MethodHandle MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray; - public static final MethodHandle MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStruct; - public static final MethodHandle MH_add2BoolStructsWithXor_returnStructPointer; - public static final MethodHandle MH_add3BoolStructsWithXor_returnStruct; - - public static final MethodHandle MH_addByteAndBytesFromStruct; - public static final MethodHandle MH_addByteAnd20BytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct; - public static final MethodHandle MH_addByteFromPointerAndBytesFromStruct_returnBytePointer; - public static final MethodHandle MH_addByteAndBytesFromStructPointer; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct; - public static final MethodHandle MH_addByteAndBytesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray; - public static final MethodHandle MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add1ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStruct; - public static final MethodHandle MH_add2ByteStructs_returnStructPointer; - public static final MethodHandle MH_add3ByteStructs_returnStruct; - - public static final MethodHandle MH_addCharAndCharsFromStruct; - public static final MethodHandle MH_addCharAnd10CharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct; - public static final MethodHandle MH_addCharFromPointerAndCharsFromStruct_returnCharPointer; - public static final MethodHandle MH_addCharAndCharsFromStructPointer; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct; - public static final MethodHandle MH_addCharAndCharsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray; - public static final MethodHandle MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2CharStructs_returnStruct; - public static final MethodHandle MH_add2CharStructs_returnStructPointer; - public static final MethodHandle MH_add3CharStructs_returnStruct; - - public static final MethodHandle MH_addShortAndShortsFromStruct; - public static final MethodHandle MH_addShortAnd10ShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct; - public static final MethodHandle MH_addShortFromPointerAndShortsFromStruct_returnShortPointer; - public static final MethodHandle MH_addShortAndShortsFromStructPointer; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct; - public static final MethodHandle MH_addShortAndShortsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray; - public static final MethodHandle MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2ShortStructs_returnStruct; - public static final MethodHandle MH_add2ShortStructs_returnStructPointer; - public static final MethodHandle MH_add3ShortStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntsFromStruct; - public static final MethodHandle MH_addIntAnd5IntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct; - public static final MethodHandle MH_addIntFromPointerAndIntsFromStruct_returnIntPointer; - public static final MethodHandle MH_addIntAndIntsFromStructPointer; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct; - public static final MethodHandle MH_addIntAndIntsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray; - public static final MethodHandle MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2IntStructs_returnStruct; - public static final MethodHandle MH_add2IntStructs_returnStruct_throwException; - public static final MethodHandle MH_add2IntStructs_returnStruct_nestedUpcall; - public static final MethodHandle MH_add2IntStructs_returnStruct_nullValue; - public static final MethodHandle MH_add2IntStructs_returnStruct_nullSegmt; - public static final MethodHandle MH_add2IntStructs_returnStruct_heapSegmt; - public static final MethodHandle MH_add2IntStructs_returnStructPointer; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_nullValue; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_nullSegmt; - public static final MethodHandle MH_add2IntStructs_returnStructPointer_heapSegmt; - public static final MethodHandle MH_add3IntStructs_returnStruct; - - public static final MethodHandle MH_addLongAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct; - public static final MethodHandle MH_addLongFromPointerAndLongsFromStruct_returnLongPointer; - public static final MethodHandle MH_addLongAndLongsFromStructPointer; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct; - public static final MethodHandle MH_addLongAndLongsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray; - public static final MethodHandle MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2LongStructs_returnStruct; - public static final MethodHandle MH_add2LongStructs_returnStructPointer; - public static final MethodHandle MH_add3LongStructs_returnStruct; - - public static final MethodHandle MH_addFloatAndFloatsFromStruct; - public static final MethodHandle MH_addFloatAnd5FloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct; - public static final MethodHandle MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer; - public static final MethodHandle MH_addFloatAndFloatsFromStructPointer; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct; - public static final MethodHandle MH_addFloatAndFloatsFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray; - public static final MethodHandle MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2FloatStructs_returnStruct; - public static final MethodHandle MH_add2FloatStructs_returnStructPointer; - public static final MethodHandle MH_add3FloatStructs_returnStruct; - - public static final MethodHandle MH_addDoubleAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct; - public static final MethodHandle MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer; - public static final MethodHandle MH_addDoubleAndDoublesFromStructPointer; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct; - public static final MethodHandle MH_addDoubleAndDoublesFromNestedStruct_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray; - public static final MethodHandle MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder; - public static final MethodHandle MH_add2DoubleStructs_returnStruct; - public static final MethodHandle MH_add2DoubleStructs_returnStructPointer; - public static final MethodHandle MH_add3DoubleStructs_returnStruct; - - public static final MethodHandle MH_addIntAndIntShortFromStruct; - public static final MethodHandle MH_addIntAndShortIntFromStruct; - public static final MethodHandle MH_addIntAndIntLongFromStruct; - public static final MethodHandle MH_addIntAndLongIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAnd2FloatsDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndDouble2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndInt2FloatsFromStruct; - public static final MethodHandle MH_addFloatAndFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntFloatDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndFloatIntDoubleFromStruct; - public static final MethodHandle MH_addDoubleAndLongDoubleFromStruct; - public static final MethodHandle MH_addFloatAndInt3FloatsFromStruct; - public static final MethodHandle MH_addLongAndLong2FloatsFromStruct; - public static final MethodHandle MH_addFloatAnd3FloatsIntFromStruct; - public static final MethodHandle MH_addLongAndFloatLongFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleFloatIntFromStruct; - public static final MethodHandle MH_addDoubleAndDoubleLongFromStruct; - public static final MethodHandle MH_addLongAnd2FloatsLongFromStruct; - public static final MethodHandle MH_addShortAnd3ShortsCharFromStruct; - public static final MethodHandle MH_addFloatAndIntFloatIntFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleIntFromStruct; - public static final MethodHandle MH_addDoubleAndFloatDoubleFloatFromStruct; - public static final MethodHandle MH_addDoubleAndIntDoubleLongFromStruct; - public static final MethodHandle MH_return254BytesFromStruct; - public static final MethodHandle MH_return4KBytesFromStruct; - - public static final MethodHandle MH_addNegBytesFromStruct; - public static final MethodHandle MH_addNegShortsFromStruct; - - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - - try { - MH_add2BoolsWithOr = lookup.findStatic(UpcallMethodHandles.class, "add2BoolsWithOr", methodType(boolean.class, boolean.class, boolean.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPointerWithOr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPointerWithOr", methodType(boolean.class, boolean.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetPtr", MT_Segmt_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolFromPtrWithOr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolFromPtrWithOr_RetArgPtr", MT_Segmt_Bool_MemSegmt); //$NON-NLS-1$ - - MH_createNewCharFrom2Chars = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFrom2Chars", methodType(char.class, char.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPointer = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPointer", methodType(char.class, MemorySegment.class, char.class)); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetPtr", MT_Segmt_MemSegmt_Char); //$NON-NLS-1$ - MH_createNewCharFromCharAndCharFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "createNewCharFromCharAndCharFromPtr_RetArgPtr", MT_Segmt_MemSegmt_Char); //$NON-NLS-1$ - - MH_add2Bytes = lookup.findStatic(UpcallMethodHandles.class, "add2Bytes", methodType(byte.class, byte.class, byte.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPointer", methodType(byte.class, byte.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetPtr", MT_Segmt_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndByteFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndByteFromPtr_RetArgPtr", MT_Segmt_Byte_MemSegmt); //$NON-NLS-1$ - - MH_add2Shorts = lookup.findStatic(UpcallMethodHandles.class, "add2Shorts", methodType(short.class, short.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPointer", methodType(short.class, MemorySegment.class, short.class)); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetPtr", MT_Segmt_MemSegmt_Short); //$NON-NLS-1$ - MH_addShortAndShortFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromPtr_RetArgPtr", MT_Segmt_MemSegmt_Short); //$NON-NLS-1$ - - MH_add2Ints = lookup.findStatic(UpcallMethodHandles.class, "add2Ints", methodType(int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPointer", methodType(int.class, int.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetPtr", MT_Segmt_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromPtr_RetArgPtr", MT_Segmt_Int_MemSegmt); //$NON-NLS-1$ - MH_add3Ints = lookup.findStatic(UpcallMethodHandles.class, "add3Ints", methodType(int.class, int.class, int.class, int.class)); //$NON-NLS-1$ - MH_addIntAndChar = lookup.findStatic(UpcallMethodHandles.class, "addIntAndChar", methodType(int.class, int.class, char.class)); //$NON-NLS-1$ - MH_add2IntsReturnVoid = lookup.findStatic(UpcallMethodHandles.class, "add2IntsReturnVoid", methodType(void.class, int.class, int.class)); //$NON-NLS-1$ - - MH_add2Longs = lookup.findStatic(UpcallMethodHandles.class, "add2Longs", methodType(long.class, long.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPointer", methodType(long.class, MemorySegment.class, long.class)); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetPtr", MT_Segmt_MemSegmt_Long); //$NON-NLS-1$ - MH_addLongAndLongFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromPtr_RetArgPtr", MT_Segmt_MemSegmt_Long); //$NON-NLS-1$ - - MH_add2Floats = lookup.findStatic(UpcallMethodHandles.class, "add2Floats", methodType(float.class, float.class, float.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPointer", methodType(float.class, float.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetPtr", MT_Segmt_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromPtr_RetArgPtr", MT_Segmt_Float_MemSegmt); //$NON-NLS-1$ - - MH_add2Doubles = lookup.findStatic(UpcallMethodHandles.class, "add2Doubles", methodType(double.class, double.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPointer", methodType(double.class, MemorySegment.class, double.class)); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetPtr", MT_Segmt_MemSegmt_Double); //$NON-NLS-1$ - MH_addDoubleAndDoubleFromPtr_RetArgPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromPtr_RetArgPtr", MT_Segmt_MemSegmt_Double); //$NON-NLS-1$ - - MH_compare = lookup.findStatic(UpcallMethodHandles.class, "compare", methodType(int.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - - MH_addBoolAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAnd20BoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAnd20BoolsFromStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor", methodType(boolean.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer = lookup.findStatic(UpcallMethodHandles.class, "addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructPointerWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructPointerWithXor", methodType(boolean.class, boolean.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromNestedStructWithXor_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedStructWithXor_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2BoolStructsWithXor_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3BoolStructsWithXor_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3BoolStructsWithXor_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addByteAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAnd20BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAnd20BytesFromStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct", methodType(byte.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addByteFromPointerAndBytesFromStruct_returnBytePointer = lookup.findStatic(UpcallMethodHandles.class, "addByteFromPointerAndBytesFromStruct_returnBytePointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructPointer", methodType(byte.class, byte.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedStruct_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedByteArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedByteArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_addByteAndBytesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStructWithNestedStructArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ - MH_add1ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add1ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ByteStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3ByteStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ByteStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addCharAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAnd10CharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAnd10CharsFromStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct", methodType(char.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addCharFromPointerAndCharsFromStruct_returnCharPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharFromPointerAndCharsFromStruct_returnCharPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructPointer", methodType(char.class, char.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedStruct_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedCharArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedCharArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_addCharAndCharsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStructWithNestedStructArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2CharStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3CharStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3CharStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addShortAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd10ShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd10ShortsFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct", methodType(short.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addShortFromPointerAndShortsFromStruct_returnShortPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortFromPointerAndShortsFromStruct_returnShortPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructPointer", methodType(short.class, short.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedStruct_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedShortArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedShortArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addShortAndShortsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStructWithNestedStructArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2ShortStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3ShortStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3ShortStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAnd5IntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAnd5IntsFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct", methodType(int.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntFromPointerAndIntsFromStruct_returnIntPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntFromPointerAndIntsFromStruct_returnIntPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructPointer", methodType(int.class, int.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedStruct_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedIntArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedIntArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStructWithNestedStructArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_throwException = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_throwException", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_nestedUpcall = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_nestedUpcall", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_nullValue = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_nullValue", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_nullSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_nullSegmt", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStruct_heapSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_heapSegmt", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_nullValue = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_nullValue", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_nullSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_nullSegmt", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2IntStructs_returnStructPointer_heapSegmt = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStructPointer_heapSegmt", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3IntStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3IntStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addLongAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct", methodType(long.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addLongFromPointerAndLongsFromStruct_returnLongPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongFromPointerAndLongsFromStruct_returnLongPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructPointer", methodType(long.class, long.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedStruct_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedLongArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedLongArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLongsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStructWithNestedStructArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2LongStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3LongStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3LongStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addFloatAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd5FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd5FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct", methodType(float.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatFromPointerAndFloatsFromStruct_returnFloatPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructPointer", methodType(float.class, float.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedStruct_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2FloatStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3FloatStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3FloatStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addDoubleAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct", methodType(double.class, MemorySegment.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructPointer = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructPointer", methodType(double.class, double.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromNestedStruct_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedStruct_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add2DoubleStructs_returnStructPointer = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStructPointer", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - MH_add3DoubleStructs_returnStruct = lookup.findStatic(UpcallMethodHandles.class, "add3DoubleStructs_returnStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ - - MH_addIntAndIntShortFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntShortFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndShortIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortIntFromStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndIntLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntLongFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addIntAndLongIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndLongIntFromStruct", MT_Long_Int_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAnd2FloatsDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAnd2FloatsDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDouble2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDouble2FloatsFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt2FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntFloatDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntFloatDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatIntDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatIntDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndLongDoubleFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndLongDoubleFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndInt3FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndInt3FloatsFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndLong2FloatsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLong2FloatsFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addFloatAnd3FloatsIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAnd3FloatsIntFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addLongAndFloatLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndFloatLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleFloatIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFloatIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addLongAnd2FloatsLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAnd2FloatsLongFromStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ - MH_addShortAnd3ShortsCharFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAnd3ShortsCharFromStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ - MH_addFloatAndIntFloatIntFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndIntFloatIntFloatFromStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleIntFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleIntFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndFloatDoubleFloatFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatDoubleFloatFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_addDoubleAndIntDoubleLongFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntDoubleLongFromStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ - MH_return254BytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return254BytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - MH_return4KBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "return4KBytesFromStruct", MT_MemSegmt); //$NON-NLS-1$ - - MH_addNegBytesFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addNegBytesFromStruct", MT_Byte_Byte_MemSegmt.appendParameterTypes(byte.class, byte.class)); //$NON-NLS-1$ - MH_addNegShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addNegShortsFromStruct", MT_Short_Short_MemSegmt.appendParameterTypes(short.class, short.class)); //$NON-NLS-1$ - - } catch (IllegalAccessException | NoSuchMethodException e) { - throw new InternalError(e); - } - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - public static boolean add2BoolsWithOr(boolean boolArg1, boolean boolArg2) { - boolean result = boolArg1 || boolArg2; - return result; - } - - public static boolean addBoolAndBoolFromPointerWithOr(boolean boolArg1, MemorySegment boolArg2Addr) { - MemorySegment boolArg2Segmt = MemorySegment.ofAddress(boolArg2Addr.address(), JAVA_BOOLEAN.byteSize(), scope); - boolean result = boolArg1 || boolArg2Segmt.get(JAVA_BOOLEAN, 0); - return result; - } - - public static MemorySegment addBoolAndBoolFromPtrWithOr_RetPtr(boolean boolArg1, MemorySegment boolArg2Addr) { - MemorySegment boolArg2Segmt = MemorySegment.ofAddress(boolArg2Addr.address(), JAVA_BOOLEAN.byteSize(), scope); - boolean result = boolArg1 || boolArg2Segmt.get(JAVA_BOOLEAN, 0); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope); - resultSegmt.set(JAVA_BOOLEAN, 0, result); - return resultSegmt; - } - - public static MemorySegment addBoolAndBoolFromPtrWithOr_RetArgPtr(boolean boolArg1, MemorySegment boolArg2Addr) { - MemorySegment boolArg2Segmt = MemorySegment.ofAddress(boolArg2Addr.address(), JAVA_BOOLEAN.byteSize(), scope); - boolean result = boolArg1 || boolArg2Segmt.get(JAVA_BOOLEAN, 0); - boolArg2Segmt.set(JAVA_BOOLEAN, 0, result); - return boolArg2Segmt; - } - - public static char createNewCharFrom2Chars(char charArg1, char charArg2) { - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static char createNewCharFromCharAndCharFromPointer(MemorySegment charArg1Addr, char charArg2) { - MemorySegment charArg1Segmt = MemorySegment.ofAddress(charArg1Addr.address(), JAVA_CHAR.byteSize(), scope); - char charArg1 = charArg1Segmt.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - return result; - } - - public static MemorySegment createNewCharFromCharAndCharFromPtr_RetPtr(MemorySegment charArg1Addr, char charArg2) { - MemorySegment charArg1Segmt = MemorySegment.ofAddress(charArg1Addr.address(), JAVA_CHAR.byteSize(), scope); - char charArg1 = charArg1Segmt.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_CHAR.byteSize(), scope); - resultSegmt.set(JAVA_CHAR, 0, result); - return resultSegmt; - } - - public static MemorySegment createNewCharFromCharAndCharFromPtr_RetArgPtr(MemorySegment charArg1Addr, char charArg2) { - MemorySegment charArg1Segmt = MemorySegment.ofAddress(charArg1Addr.address(), JAVA_CHAR.byteSize(), scope); - char charArg1 = charArg1Segmt.get(JAVA_CHAR, 0); - int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); - diff = (diff > 5) ? 5 : diff; - char result = (char)(diff + 'A'); - charArg1Segmt.set(JAVA_CHAR, 0, result); - return charArg1Segmt; - } - - public static byte add2Bytes(byte byteArg1, byte byteArg2) { - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static byte addByteAndByteFromPointer(byte byteArg1, MemorySegment byteArg2Addr) { - MemorySegment byteArg2Segmt = MemorySegment.ofAddress(byteArg2Addr.address(), JAVA_BYTE.byteSize(), scope); - byte byteArg2 = byteArg2Segmt.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - return byteSum; - } - - public static MemorySegment addByteAndByteFromPtr_RetPtr(byte byteArg1, MemorySegment byteArg2Addr) { - MemorySegment byteArg2Segmt = MemorySegment.ofAddress(byteArg2Addr.address(), JAVA_BYTE.byteSize(), scope); - byte byteArg2 = byteArg2Segmt.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_BYTE.byteSize(), scope); - resultSegmt.set(JAVA_BYTE, 0, byteSum); - return resultSegmt; - } - - public static MemorySegment addByteAndByteFromPtr_RetArgPtr(byte byteArg1, MemorySegment byteArg2Addr) { - MemorySegment byteArg2Segmt = MemorySegment.ofAddress(byteArg2Addr.address(), JAVA_BYTE.byteSize(), scope); - byte byteArg2 = byteArg2Segmt.get(JAVA_BYTE, 0); - byte byteSum = (byte)(byteArg1 + byteArg2); - byteArg2Segmt.set(JAVA_BYTE, 0, byteSum); - return byteArg2Segmt; - } - - public static short add2Shorts(short shortArg1, short shortArg2) { - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static short addShortAndShortFromPointer(MemorySegment shortArg1Addr, short shortArg2) { - MemorySegment shortArg1Segmt = MemorySegment.ofAddress(shortArg1Addr.address(), JAVA_SHORT.byteSize(), scope); - short shortArg1 = shortArg1Segmt.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - return shortSum; - } - - public static MemorySegment addShortAndShortFromPtr_RetPtr(MemorySegment shortArg1Addr, short shortArg2) { - MemorySegment shortArg1Segmt = MemorySegment.ofAddress(shortArg1Addr.address(), JAVA_SHORT.byteSize(), scope); - short shortArg1 = shortArg1Segmt.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_SHORT.byteSize(), scope); - resultSegmt.set(JAVA_SHORT, 0, shortSum); - return resultSegmt; - } - - public static MemorySegment addShortAndShortFromPtr_RetArgPtr(MemorySegment shortArg1Addr, short shortArg2) { - MemorySegment shortArg1Segmt = MemorySegment.ofAddress(shortArg1Addr.address(), JAVA_SHORT.byteSize(), scope); - short shortArg1 = shortArg1Segmt.get(JAVA_SHORT, 0); - short shortSum = (short)(shortArg1 + shortArg2); - shortArg1Segmt.set(JAVA_SHORT, 0, shortSum); - return shortArg1Segmt; - } - - public static int add2Ints(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - return intSum; - } - - public static int addIntAndIntFromPointer(int intArg1, MemorySegment intArg2Addr) { - MemorySegment intArg2Segmt = MemorySegment.ofAddress(intArg2Addr.address(), JAVA_INT.byteSize(), scope); - int intArg2 = intArg2Segmt.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - return intSum; - } - - public static MemorySegment addIntAndIntFromPtr_RetPtr(int intArg1, MemorySegment intArg2Addr) { - MemorySegment intArg2Segmt = MemorySegment.ofAddress(intArg2Addr.address(), JAVA_INT.byteSize(), scope); - int intArg2 = intArg2Segmt.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_INT.byteSize(), scope); - resultSegmt.set(JAVA_INT, 0, intSum); - return resultSegmt; - } - - public static MemorySegment addIntAndIntFromPtr_RetArgPtr(int intArg1, MemorySegment intArg2Addr) { - MemorySegment intArg2Segmt = MemorySegment.ofAddress(intArg2Addr.address(), JAVA_INT.byteSize(), scope); - int intArg2 = intArg2Segmt.get(JAVA_INT, 0); - int intSum = intArg1 + intArg2; - intArg2Segmt.set(JAVA_INT, 0, intSum); - return intArg2Segmt; - } - - public static int add3Ints(int intArg1, int intArg2, int intArg3) { - int intSum = intArg1 + intArg2 + intArg3; - return intSum; - } - - public static int addIntAndChar(int intArg, char charArg) { - int sum = intArg + charArg; - return sum; - } - - public static void add2IntsReturnVoid(int intArg1, int intArg2) { - int intSum = intArg1 + intArg2; - System.out.println("add2IntsReturnVoid: intSum = " + intSum + "\n"); - } - - public static long add2Longs(long longArg1, long longArg2) { - long longSum = longArg1 + longArg2; - return longSum; - } - - public static long addLongAndLongFromPointer(MemorySegment longArg1Addr, long longArg2) { - MemorySegment longArg1Segmt = MemorySegment.ofAddress(longArg1Addr.address(), JAVA_LONG.byteSize(), scope); - long longArg1 = longArg1Segmt.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - return longSum; - } - - public static MemorySegment addLongAndLongFromPtr_RetPtr(MemorySegment longArg1Addr, long longArg2) { - MemorySegment longArg1Segmt = MemorySegment.ofAddress(longArg1Addr.address(), JAVA_LONG.byteSize(), scope); - long longArg1 = longArg1Segmt.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_LONG.byteSize(), scope); - resultSegmt.set(JAVA_LONG, 0, longSum); - return resultSegmt; - } - - public static MemorySegment addLongAndLongFromPtr_RetArgPtr(MemorySegment longArg1Addr, long longArg2) { - MemorySegment longArg1Segmt = MemorySegment.ofAddress(longArg1Addr.address(), JAVA_LONG.byteSize(), scope); - long longArg1 = longArg1Segmt.get(JAVA_LONG, 0); - long longSum = longArg1 + longArg2; - longArg1Segmt.set(JAVA_LONG, 0, longSum); - return longArg1Segmt; - } - - public static float add2Floats(float floatArg1, float floatArg2) { - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static float addFloatAndFloatFromPointer(float floatArg1, MemorySegment floatArg2Addr) { - MemorySegment floatArg2Segmt = MemorySegment.ofAddress(floatArg2Addr.address(), JAVA_FLOAT.byteSize(), scope); - float floatArg2 = floatArg2Segmt.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - return floatSum; - } - - public static MemorySegment addFloatAndFloatFromPtr_RetPtr(float floatArg1, MemorySegment floatArg2Addr) { - MemorySegment floatArg2Segmt = MemorySegment.ofAddress(floatArg2Addr.address(), JAVA_FLOAT.byteSize(), scope); - float floatArg2 = floatArg2Segmt.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_FLOAT.byteSize(), scope); - resultSegmt.set(JAVA_FLOAT, 0, floatSum); - return resultSegmt; - } - - public static MemorySegment addFloatAndFloatFromPtr_RetArgPtr(float floatArg1, MemorySegment floatArg2Addr) { - MemorySegment floatArg2Segmt = MemorySegment.ofAddress(floatArg2Addr.address(), JAVA_FLOAT.byteSize(), scope); - float floatArg2 = floatArg2Segmt.get(JAVA_FLOAT, 0); - float floatSum = floatArg1 + floatArg2; - floatArg2Segmt.set(JAVA_FLOAT, 0, floatSum); - return floatArg2Segmt; - } - - public static double add2Doubles(double doubleArg1, double doubleArg2) { - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static double addDoubleAndDoubleFromPointer(MemorySegment doubleArg1Addr, double doubleArg2) { - MemorySegment doubleArg1Segmt = MemorySegment.ofAddress(doubleArg1Addr.address(), JAVA_DOUBLE.byteSize(), scope); - double doubleArg1 = doubleArg1Segmt.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - return doubleSum; - } - - public static MemorySegment addDoubleAndDoubleFromPtr_RetPtr(MemorySegment doubleArg1Addr, double doubleArg2) { - MemorySegment doubleArg1Segmt = MemorySegment.ofAddress(doubleArg1Addr.address(), JAVA_DOUBLE.byteSize(), scope); - double doubleArg1 = doubleArg1Segmt.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - MemorySegment resultSegmt = MemorySegment.allocateNative(JAVA_DOUBLE.byteSize(), scope); - resultSegmt.set(JAVA_DOUBLE, 0, doubleSum); - return resultSegmt; - } - - public static MemorySegment addDoubleAndDoubleFromPtr_RetArgPtr(MemorySegment doubleArg1Addr, double doubleArg2) { - MemorySegment doubleArg1Segmt = MemorySegment.ofAddress(doubleArg1Addr.address(), JAVA_DOUBLE.byteSize(), scope); - double doubleArg1 = doubleArg1Segmt.get(JAVA_DOUBLE, 0); - double doubleSum = doubleArg1 + doubleArg2; - doubleArg1Segmt.set(JAVA_DOUBLE, 0, doubleSum); - return doubleArg1Segmt; - } - - public static int compare(MemorySegment argAddr1, MemorySegment argAddr2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(argAddr1.address(), JAVA_INT.byteSize(), scope); - MemorySegment arg2Segmt = MemorySegment.ofAddress(argAddr2.address(), JAVA_INT.byteSize(), scope); - int intArg1 = arg1Segmt.get(JAVA_INT, 0); - int intArg2 = arg2Segmt.get(JAVA_INT, 0); - return (intArg1 - intArg2); - } - - public static boolean addBoolAndBoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAnd20BoolsFromStructWithXor(boolean arg1, MemorySegment arg2) { - boolean boolSum = arg1 ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1) - ^ arg2.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 3) ^ arg2.get(JAVA_BOOLEAN, 4) - ^ arg2.get(JAVA_BOOLEAN, 5) ^ arg2.get(JAVA_BOOLEAN, 6) ^ arg2.get(JAVA_BOOLEAN, 7) - ^ arg2.get(JAVA_BOOLEAN, 8) ^ arg2.get(JAVA_BOOLEAN, 9) ^ arg2.get(JAVA_BOOLEAN, 10) - ^ arg2.get(JAVA_BOOLEAN, 11) ^ arg2.get(JAVA_BOOLEAN, 12) ^ arg2.get(JAVA_BOOLEAN, 13) - ^ arg2.get(JAVA_BOOLEAN, 14) ^ arg2.get(JAVA_BOOLEAN, 15) ^ arg2.get(JAVA_BOOLEAN, 16) - ^ arg2.get(JAVA_BOOLEAN, 17) ^ arg2.get(JAVA_BOOLEAN, 18) ^ arg2.get(JAVA_BOOLEAN, 19); - return boolSum; - } - - public static boolean addBoolFromPointerAndBoolsFromStructWithXor(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_BOOLEAN.byteSize(), scope); - boolean boolSum = arg1Segmt.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static MemorySegment addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_BOOLEAN.byteSize(), scope); - boolean boolSum = arg1Segmt.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Segmt.set(JAVA_BOOLEAN, 0, boolSum); - return arg1Addr; - } - - public static boolean addBoolAndBoolsFromStructPointerWithXor(boolean arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - boolean boolSum = arg1 ^ arg2Segmt.get(JAVA_BOOLEAN, 0) ^ arg2Segmt.get(JAVA_BOOLEAN, 1); - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor(boolean arg1, MemorySegment arg2) { - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ nestedStructElem1 ^ nestedStructElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromNestedStructWithXor_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean boolSum = arg1 ^ structElem1 ^ nestedStructElem1 ^ nestedStructElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray(boolean arg1, MemorySegment arg2) { - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2 ^ structElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 2); - - boolean boolSum = arg1 ^ structElem1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray(boolean arg1, MemorySegment arg2) { - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 3); - boolean structElem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem2 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static boolean addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder(boolean arg1, MemorySegment arg2) { - boolean structElem1 = arg2.get(JAVA_BOOLEAN, 0); - boolean nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 1); - boolean nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 2); - boolean nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 3); - boolean nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 4); - - boolean boolSum = arg1 ^ structElem1 - ^ nestedStructArrayElem1_Elem1 ^ nestedStructArrayElem1_Elem2 - ^ nestedStructArrayElem2_Elem1 ^ nestedStructArrayElem2_Elem2; - return boolSum; - } - - public static MemorySegment add2BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, scope); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return boolStructSegmt; - } - - public static MemorySegment add2BoolStructsWithXor_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - boolean boolStruct_Elem1 = arg1Segmt.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1Segmt.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - arg1Segmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - arg1Segmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3BoolStructsWithXor_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), - JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment boolStructSegmt = MemorySegment.allocateNative(structLayout, scope); - boolean boolStruct_Elem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); - boolean boolStruct_Elem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); - boolean boolStruct_Elem3 = arg1.get(JAVA_BOOLEAN, 2) ^ arg2.get(JAVA_BOOLEAN, 2); - boolStructSegmt.set(JAVA_BOOLEAN, 0, boolStruct_Elem1); - boolStructSegmt.set(JAVA_BOOLEAN, 1, boolStruct_Elem2); - boolStructSegmt.set(JAVA_BOOLEAN, 2, boolStruct_Elem3); - return boolStructSegmt; - } - - public static byte addByteAndBytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAnd20BytesFromStruct(byte arg1, MemorySegment arg2) { - byte byteSum = (byte)(arg1 + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1) - + arg2.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 3) + arg2.get(JAVA_BYTE, 4) - + arg2.get(JAVA_BYTE, 5) + arg2.get(JAVA_BYTE, 6) + arg2.get(JAVA_BYTE, 7) - + arg2.get(JAVA_BYTE, 8) + arg2.get(JAVA_BYTE, 9) + arg2.get(JAVA_BYTE, 10) - + arg2.get(JAVA_BYTE, 11) + arg2.get(JAVA_BYTE, 12) + arg2.get(JAVA_BYTE, 13) - + arg2.get(JAVA_BYTE, 14) + arg2.get(JAVA_BYTE, 15) + arg2.get(JAVA_BYTE, 16) - + arg2.get(JAVA_BYTE, 17) + arg2.get(JAVA_BYTE, 18) + arg2.get(JAVA_BYTE, 19)); - return byteSum; - } - - public static byte addByteFromPointerAndBytesFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_BYTE.byteSize(), scope); - byte byteSum = (byte)(arg1Segmt.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static MemorySegment addByteFromPointerAndBytesFromStruct_returnBytePointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_BYTE.byteSize(), scope); - byte byteSum = (byte)(arg1Segmt.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 1)); - arg1Segmt.set(JAVA_BYTE, 0, byteSum); - return arg1Addr; - } - - public static byte addByteAndBytesFromStructPointer(byte arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - byte byteSum = (byte)(arg1 + arg2Segmt.get(JAVA_BYTE, 0) + arg2Segmt.get(JAVA_BYTE, 1)); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct(byte arg1, MemorySegment arg2) { - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromNestedStruct_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray(byte arg1, MemorySegment arg2) { - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 1); - byte structElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + nestedByteArrayElem1 + nestedByteArrayElem2 + structElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedByteArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 1); - byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 2); - - byte byteSum = (byte)(arg1 + structElem1 + nestedByteArrayElem1 + nestedByteArrayElem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray(byte arg1, MemorySegment arg2) { - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 3); - byte structElem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static byte addByteAndBytesFromStructWithNestedStructArray_reverseOrder(byte arg1, MemorySegment arg2) { - byte structElem1 = arg2.get(JAVA_BYTE, 0); - byte nestedStructArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 1); - byte nestedStructArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 2); - byte nestedStructArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 3); - byte nestedStructArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 4); - - byte byteSum = (byte)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return byteSum; - } - - public static MemorySegment add1ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - return byteStructSegmt; - } - - public static MemorySegment add2ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - return byteStructSegmt; - } - - public static MemorySegment add2ByteStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - byte byteStruct_Elem1 = (byte)(arg1Segmt.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1Segmt.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - arg1Segmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - arg1Segmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ByteStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), - JAVA_BYTE.withName("elem3"), MemoryLayout.paddingLayout(8)); - MemorySegment byteStructSegmt = MemorySegment.allocateNative(structLayout, scope); - byte byteStruct_Elem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); - byte byteStruct_Elem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); - byte byteStruct_Elem3 = (byte)(arg1.get(JAVA_BYTE, 2) + arg2.get(JAVA_BYTE, 2)); - byteStructSegmt.set(JAVA_BYTE, 0, byteStruct_Elem1); - byteStructSegmt.set(JAVA_BYTE, 1, byteStruct_Elem2); - byteStructSegmt.set(JAVA_BYTE, 2, byteStruct_Elem3); - return byteStructSegmt; - } - - public static char addCharAndCharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAnd10CharsFromStruct(char arg1, MemorySegment arg2) { - char result = (char)(arg1 + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - + arg2.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 6) + arg2.get(JAVA_CHAR, 8) - + arg2.get(JAVA_CHAR, 10) + arg2.get(JAVA_CHAR, 12) + arg2.get(JAVA_CHAR, 14) - + arg2.get(JAVA_CHAR, 16) + arg2.get(JAVA_CHAR, 18) - 10 * 'A'); - return result; - } - - public static char addCharFromPointerAndCharsFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_CHAR.byteSize(), scope); - char result = (char)(arg1Segmt.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static MemorySegment addCharFromPointerAndCharsFromStruct_returnCharPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_CHAR.byteSize(), scope); - char result = (char)(arg1Segmt.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 2) - 2 * 'A'); - arg1Segmt.set(JAVA_CHAR, 0, result); - return arg1Addr; - } - - public static char addCharAndCharsFromStructPointer(char arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - char result = (char)(arg1 + arg2Segmt.get(JAVA_CHAR, 0) + arg2Segmt.get(JAVA_CHAR, 2) - 2 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct(char arg1, MemorySegment arg2) { - char nestedStructElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromNestedStruct_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructElem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray(char arg1, MemorySegment arg2) { - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 2); - char structElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + nestedCharArrayElem1 + nestedCharArrayElem2 + structElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedCharArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 2); - char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 4); - - char result = (char)(arg1 + structElem1 + nestedCharArrayElem1 + nestedCharArrayElem2 - 3 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray(char arg1, MemorySegment arg2) { - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 6); - char structElem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static char addCharAndCharsFromStructWithNestedStructArray_reverseOrder(char arg1, MemorySegment arg2) { - char structElem1 = arg2.get(JAVA_CHAR, 0); - char nestedStructArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 2); - char nestedStructArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 4); - char nestedStructArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 6); - char nestedStructArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 8); - - char result = (char)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2 - 5 * 'A'); - return result; - } - - public static MemorySegment add2CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, scope); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - return charStructSegmt; - } - - public static MemorySegment add2CharStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - char charStruct_Elem1 = (char)(arg1Segmt.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1Segmt.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - arg1Segmt.set(JAVA_CHAR, 0, charStruct_Elem1); - arg1Segmt.set(JAVA_CHAR, 2, charStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3CharStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), - JAVA_CHAR.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment charStructSegmt = MemorySegment.allocateNative(structLayout, scope); - char charStruct_Elem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); - char charStruct_Elem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); - char charStruct_Elem3 = (char)(arg1.get(JAVA_CHAR, 4) + arg2.get(JAVA_CHAR, 4) - 'A'); - charStructSegmt.set(JAVA_CHAR, 0, charStruct_Elem1); - charStructSegmt.set(JAVA_CHAR, 2, charStruct_Elem2); - charStructSegmt.set(JAVA_CHAR, 4, charStruct_Elem3); - return charStructSegmt; - } - - public static short addShortAndShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAnd10ShortsFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 6) + arg2.get(JAVA_SHORT, 8) - + arg2.get(JAVA_SHORT, 10) + arg2.get(JAVA_SHORT, 12) + arg2.get(JAVA_SHORT, 14) - + arg2.get(JAVA_SHORT, 16) + arg2.get(JAVA_SHORT, 18)); - return shortSum; - } - - public static short addShortFromPointerAndShortsFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_SHORT.byteSize(), scope); - short shortSum = (short)(arg1Segmt.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static MemorySegment addShortFromPointerAndShortsFromStruct_returnShortPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_SHORT.byteSize(), scope); - short shortSum = (short)(arg1Segmt.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2)); - arg1Segmt.set(JAVA_SHORT, 0, shortSum); - return arg1Addr; - } - - public static short addShortAndShortsFromStructPointer(short arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - short shortSum = (short)(arg1 + arg2Segmt.get(JAVA_SHORT, 0) + arg2Segmt.get(JAVA_SHORT, 2)); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct(short arg1, MemorySegment arg2) { - short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedStructElem1 + nestedStructElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromNestedStruct_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructElem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedStructElem1 + nestedStructElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray(short arg1, MemorySegment arg2) { - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 2); - short structElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + nestedShortArrayElem1 + nestedShortArrayElem2 + structElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedShortArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 2); - short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 4); - - short shortSum = (short)(arg1 + structElem1 + nestedShortArrayElem1 + nestedShortArrayElem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray(short arg1, MemorySegment arg2) { - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 6); - short structElem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static short addShortAndShortsFromStructWithNestedStructArray_reverseOrder(short arg1, MemorySegment arg2) { - short structElem1 = arg2.get(JAVA_SHORT, 0); - short nestedStructArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 2); - short nestedStructArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 4); - short nestedStructArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 6); - short nestedStructArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 8); - - short shortSum = (short)(arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2); - return shortSum; - } - - public static MemorySegment add2ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, scope); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - return shortStructSegmt; - } - - public static MemorySegment add2ShortStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - short shortStruct_Elem1 = (short)(arg1Segmt.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1Segmt.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - arg1Segmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - arg1Segmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - return arg1Addr; - } - - public static MemorySegment add3ShortStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), - JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(16)); - MemorySegment shortStructSegmt = MemorySegment.allocateNative(structLayout, scope); - short shortStruct_Elem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); - short shortStruct_Elem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); - short shortStruct_Elem3 = (short)(arg1.get(JAVA_SHORT, 4) + arg2.get(JAVA_SHORT, 4)); - shortStructSegmt.set(JAVA_SHORT, 0, shortStruct_Elem1); - shortStructSegmt.set(JAVA_SHORT, 2, shortStruct_Elem2); - shortStructSegmt.set(JAVA_SHORT, 4, shortStruct_Elem3); - return shortStructSegmt; - } - - public static int addIntAndIntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAnd5IntsFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4) - + arg2.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 12) + arg2.get(JAVA_INT, 16); - return intSum; - } - - public static int addIntFromPointerAndIntsFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_INT.byteSize(), scope); - int intSum = arg1Segmt.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static MemorySegment addIntFromPointerAndIntsFromStruct_returnIntPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_INT.byteSize(), scope); - int intSum = arg1Segmt.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 4); - arg1Segmt.set(JAVA_INT, 0, intSum); - return arg1Addr; - } - - public static int addIntAndIntsFromStructPointer(int arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - int intSum = arg1 + arg2Segmt.get(JAVA_INT, 0) + arg2Segmt.get(JAVA_INT, 4); - return intSum; - } - - public static int addIntAndIntsFromNestedStruct(int arg1, MemorySegment arg2) { - int nestedStructElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromNestedStruct_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructElem1 = arg2.get(JAVA_INT, 4); - int nestedStructElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray(int arg1, MemorySegment arg2) { - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 4); - int structElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + nestedIntArrayElem1 + nestedIntArrayElem2 + structElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedIntArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedIntArrayElem1 = arg2.get(JAVA_INT, 4); - int nestedIntArrayElem2 = arg2.get(JAVA_INT, 8); - - int intSum = arg1 + structElem1 + nestedIntArrayElem1 + nestedIntArrayElem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray(int arg1, MemorySegment arg2) { - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 12); - int structElem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static int addIntAndIntsFromStructWithNestedStructArray_reverseOrder(int arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - int nestedStructArrayElem1_Elem1 = arg2.get(JAVA_INT, 4); - int nestedStructArrayElem1_Elem2 = arg2.get(JAVA_INT, 8); - int nestedStructArrayElem2_Elem1 = arg2.get(JAVA_INT, 12); - int nestedStructArrayElem2_Elem2 = arg2.get(JAVA_INT, 16); - - int intSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return intSum; - } - - public static MemorySegment add2IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, scope); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - return intStructSegmt; - } - - public static MemorySegment add2IntStructs_returnStruct_throwException(MemorySegment arg1, MemorySegment arg2) { - throw new IllegalArgumentException("An exception is thrown from the upcall method"); - } - - public static MemorySegment add2IntStructs_returnStruct_nestedUpcall(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); - MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_throwException, - FunctionDescriptor.of(structLayout, structLayout, structLayout), scope); - MemorySegment resultSegmt = null; - try { - resultSegmt = (MemorySegment)mh.invoke(allocator, arg1, arg2, upcallFuncAddr); - } catch (Throwable e) { - throw (IllegalArgumentException)e; - } - return resultSegmt; - } - - public static MemorySegment add2IntStructs_returnStruct_nullValue(MemorySegment arg1, MemorySegment arg2) { - return null; - } - - public static MemorySegment add2IntStructs_returnStruct_nullSegmt(MemorySegment arg1, MemorySegment arg2) { - return MemorySegment.NULL; - } - - public static MemorySegment add2IntStructs_returnStruct_heapSegmt(MemorySegment arg1, MemorySegment arg2) { - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - return MemorySegment.ofArray(new int[]{intStruct_Elem1, intStruct_Elem2}); - } - - public static MemorySegment add2IntStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - int intSum_Elem1 = arg1Segmt.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intSum_Elem2 = arg1Segmt.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - arg1Segmt.set(JAVA_INT, 0, intSum_Elem1); - arg1Segmt.set(JAVA_INT, 4, intSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add2IntStructs_returnStructPointer_nullValue(MemorySegment arg1Addr, MemorySegment arg2) { - return null; - } - - public static MemorySegment add2IntStructs_returnStructPointer_nullSegmt(MemorySegment arg1Addr, MemorySegment arg2) { - return MemorySegment.NULL; - } - - public static MemorySegment add2IntStructs_returnStructPointer_heapSegmt(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - int intSum_Elem1 = arg1Segmt.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intSum_Elem2 = arg1Segmt.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - return MemorySegment.ofArray(new int[]{intSum_Elem1, intSum_Elem2}); - } - - public static MemorySegment add3IntStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - MemorySegment intStructSegmt = MemorySegment.allocateNative(structLayout, scope); - int intStruct_Elem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); - int intStruct_Elem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); - int intStruct_Elem3 = arg1.get(JAVA_INT, 8) + arg2.get(JAVA_INT, 8); - intStructSegmt.set(JAVA_INT, 0, intStruct_Elem1); - intStructSegmt.set(JAVA_INT, 4, intStruct_Elem2); - intStructSegmt.set(JAVA_INT, 8, intStruct_Elem3); - return intStructSegmt; - } - - public static long addLongAndLongsFromStruct(long arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongFromPointerAndLongsFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_LONG.byteSize(), scope); - long longSum = arg1Segmt.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static MemorySegment addLongFromPointerAndLongsFromStruct_returnLongPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_LONG.byteSize(), scope); - long longSum = arg1Segmt.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 8); - arg1Segmt.set(JAVA_LONG, 0, longSum); - return arg1Addr; - } - - public static long addLongAndLongsFromStructPointer(long arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - long longSum = arg1 + arg2Segmt.get(JAVA_LONG, 0) + arg2Segmt.get(JAVA_LONG, 8); - return longSum; - } - - public static long addLongAndLongsFromNestedStruct(long arg1, MemorySegment arg2) { - long nestedStructElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromNestedStruct_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructElem1 = arg2.get(JAVA_LONG, 8); - long nestedStructElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray(long arg1, MemorySegment arg2) { - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 8); - long structElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + nestedLongrrayElem1 + nestedLongrrayElem2 + structElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedLongArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 8); - long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 16); - - long longSum = arg1 + structElem1 + nestedLongrrayElem1 + nestedLongrrayElem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray(long arg1, MemorySegment arg2) { - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 24); - long structElem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static long addLongAndLongsFromStructWithNestedStructArray_reverseOrder(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long nestedStructArrayElem1_Elem1 = arg2.get(JAVA_LONG, 8); - long nestedStructArrayElem1_Elem2 = arg2.get(JAVA_LONG, 16); - long nestedStructArrayElem2_Elem1 = arg2.get(JAVA_LONG, 24); - long nestedStructArrayElem2_Elem2 = arg2.get(JAVA_LONG, 32); - - long longSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return longSum; - } - - public static MemorySegment add2LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, scope); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - return longStructSegmt; - } - - public static MemorySegment add2LongStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - long longSum_Elem1 = arg1Segmt.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longSum_Elem2 = arg1Segmt.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - arg1Segmt.set(JAVA_LONG, 0, longSum_Elem1); - arg1Segmt.set(JAVA_LONG, 8, longSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3LongStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); - MemorySegment longStructSegmt = MemorySegment.allocateNative(structLayout, scope); - long longStruct_Elem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); - long longStruct_Elem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); - long longStruct_Elem3 = arg1.get(JAVA_LONG, 16) + arg2.get(JAVA_LONG, 16); - longStructSegmt.set(JAVA_LONG, 0, longStruct_Elem1); - longStructSegmt.set(JAVA_LONG, 8, longStruct_Elem2); - longStructSegmt.set(JAVA_LONG, 16, longStruct_Elem3); - return longStructSegmt; - } - - public static float addFloatAndFloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAnd5FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12) + arg2.get(JAVA_FLOAT, 16); - return floatSum; - } - - public static float addFloatFromPointerAndFloatsFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_FLOAT.byteSize(), scope); - float floatSum = arg1Segmt.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static MemorySegment addFloatFromPointerAndFloatsFromStruct_returnFloatPointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_FLOAT.byteSize(), scope); - float floatSum = arg1Segmt.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4); - arg1Segmt.set(JAVA_FLOAT, 0, floatSum); - return arg1Addr; - } - - public static float addFloatAndFloatsFromStructPointer(float arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - float floatSum = arg1 + arg2Segmt.get(JAVA_FLOAT, 0) + arg2Segmt.get(JAVA_FLOAT, 4); - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct(float arg1, MemorySegment arg2) { - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromNestedStruct_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray(float arg1, MemorySegment arg2) { - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 4); - float structElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + nestedFloatArrayElem1 + nestedFloatArrayElem2 + structElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 4); - float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 8); - - float floatSum = arg1 + structElem1 + nestedFloatArrayElem1 + nestedFloatArrayElem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray(float arg1, MemorySegment arg2) { - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 12); - float structElem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static float addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder(float arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - float nestedStructArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 4); - float nestedStructArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 8); - float nestedStructArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 12); - float nestedStructArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 16); - - float floatSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return floatSum; - } - - public static MemorySegment add2FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, scope); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - return floatStructSegmt; - } - - public static MemorySegment add2FloatStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - float floatSum_Elem1 = arg1Segmt.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatSum_Elem2 = arg1Segmt.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - arg1Segmt.set(JAVA_FLOAT, 0, floatSum_Elem1); - arg1Segmt.set(JAVA_FLOAT, 4, floatSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment floatStructSegmt = MemorySegment.allocateNative(structLayout, scope); - float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); - float floatStruct_Elem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); - float floatStruct_Elem3 = arg1.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 8); - floatStructSegmt.set(JAVA_FLOAT, 0, floatStruct_Elem1); - floatStructSegmt.set(JAVA_FLOAT, 4, floatStruct_Elem2); - floatStructSegmt.set(JAVA_FLOAT, 8, floatStruct_Elem3); - return floatStructSegmt; - } - - public static double addDoubleAndDoublesFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleFromPointerAndDoublesFromStruct(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_DOUBLE.byteSize(), scope); - double doubleSum = arg1Segmt.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static MemorySegment addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer(MemorySegment arg1Addr, MemorySegment arg2) { - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), JAVA_DOUBLE.byteSize(), scope); - double doubleSum = arg1Segmt.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 8); - arg1Segmt.set(JAVA_DOUBLE, 0, doubleSum); - return arg1Addr; - } - - public static double addDoubleAndDoublesFromStructPointer(double arg1, MemorySegment arg2Addr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - MemorySegment arg2Segmt = MemorySegment.ofAddress(arg2Addr.address(), structLayout.byteSize(), scope); - double doubleSum = arg1 + arg2Segmt.get(JAVA_DOUBLE, 0) + arg2Segmt.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct(double arg1, MemorySegment arg2) { - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedStructElem1 + nestedStructElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromNestedStruct_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedStructElem1 + nestedStructElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray(double arg1, MemorySegment arg2) { - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 8); - double structElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 16); - - double doubleSum = arg1 + structElem1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray(double arg1, MemorySegment arg2) { - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 24); - double structElem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem2 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static double addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder(double arg1, MemorySegment arg2) { - double structElem1 = arg2.get(JAVA_DOUBLE, 0); - double nestedStructArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 8); - double nestedStructArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 16); - double nestedStructArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 24); - double nestedStructArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 32); - - double doubleSum = arg1 + structElem1 - + nestedStructArrayElem1_Elem1 + nestedStructArrayElem1_Elem2 - + nestedStructArrayElem2_Elem1 + nestedStructArrayElem2_Elem2; - return doubleSum; - } - - public static MemorySegment add2DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, scope); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - return doubleStructSegmt; - } - - public static MemorySegment add2DoubleStructs_returnStructPointer(MemorySegment arg1Addr, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - MemorySegment arg1Segmt = MemorySegment.ofAddress(arg1Addr.address(), structLayout.byteSize(), scope); - double doubleSum_Elem1 = arg1Segmt.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleSum_Elem2 = arg1Segmt.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - arg1Segmt.set(JAVA_DOUBLE, 0, doubleSum_Elem1); - arg1Segmt.set(JAVA_DOUBLE, 8, doubleSum_Elem2); - return arg1Addr; - } - - public static MemorySegment add3DoubleStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); - MemorySegment doubleStructSegmt = MemorySegment.allocateNative(structLayout, scope); - double doubleStruct_Elem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); - double doubleStruct_Elem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); - double doubleStruct_Elem3 = arg1.get(JAVA_DOUBLE, 16) + arg2.get(JAVA_DOUBLE, 16); - doubleStructSegmt.set(JAVA_DOUBLE, 0, doubleStruct_Elem1); - doubleStructSegmt.set(JAVA_DOUBLE, 8, doubleStruct_Elem2); - doubleStructSegmt.set(JAVA_DOUBLE, 16, doubleStruct_Elem3); - return doubleStructSegmt; - } - - public static int addIntAndIntShortFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_SHORT, 4); - return intSum; - } - - public static int addIntAndShortIntFromStruct(int arg1, MemorySegment arg2) { - int intSum = arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_INT, 4); - return intSum; - } - - public static long addIntAndIntLongFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_LONG, 8); - return longSum; - } - - public static long addIntAndLongIntFromStruct(int arg1, MemorySegment arg2) { - long longSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_INT, 8); - return longSum; - } - - public static double addDoubleAndIntDoubleFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_INT, 8); - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double doubleSum = arg1 + structElem1 + structElem2; - return doubleSum; - } - - public static double addDoubleAndDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8); - return doubleSum; - } - - public static double addDoubleAnd2FloatsDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndDouble2FloatsFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return doubleSum; - } - - public static float addFloatAndInt2FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static float addFloatAndFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float structElem2 = Integer.valueOf(arg2.get(JAVA_INT, 4)).floatValue(); - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + structElem2 + arg2.get(JAVA_FLOAT, 8); - return floatSum; - } - - public static double addDoubleAndIntFloatDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndFloatIntDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_INT, 4) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static double addDoubleAndLongDoubleFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_LONG, 0) + arg2.get(JAVA_DOUBLE, 8); - return doubleSum; - } - - public static float addFloatAndInt3FloatsFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static long addLongAndLong2FloatsFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = arg2.get(JAVA_LONG, 0); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 8)).longValue(); - long structElem3 = Float.valueOf(arg2.get(JAVA_FLOAT, 12)).longValue(); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static float addFloatAnd3FloatsIntFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 4) - + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return floatSum; - } - - public static long addLongAndFloatLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2; - return longSum; - } - - public static double addDoubleAndDoubleFloatIntFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_FLOAT, 8) + arg2.get(JAVA_INT, 12); - return doubleSum; - } - - public static double addDoubleAndDoubleLongFromStruct(double arg1, MemorySegment arg2) { - double doubleSum = arg1 + arg2.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_LONG, 8); - return doubleSum; - } - - public static long addLongAnd2FloatsLongFromStruct(long arg1, MemorySegment arg2) { - long structElem1 = Float.valueOf(arg2.get(JAVA_FLOAT, 0)).longValue(); - long structElem2 = Float.valueOf(arg2.get(JAVA_FLOAT, 4)).longValue(); - long structElem3 = arg2.get(JAVA_LONG, 8); - long longSum = arg1 + structElem1 + structElem2 + structElem3; - return longSum; - } - - public static short addShortAnd3ShortsCharFromStruct(short arg1, MemorySegment arg2) { - short shortSum = (short)(arg1 + arg2.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 2) - + arg2.get(JAVA_SHORT, 4) + arg2.get(JAVA_CHAR, 6)); - return shortSum; - } - - public static float addFloatAndIntFloatIntFloatFromStruct(float arg1, MemorySegment arg2) { - float floatSum = arg1 + arg2.get(JAVA_INT, 0) + arg2.get(JAVA_FLOAT, 4)+ arg2.get(JAVA_INT, 8) + arg2.get(JAVA_FLOAT, 12); - return floatSum; - } - - public static double addDoubleAndIntDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleIntFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleIntFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The size of [int, double, int] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - int structElem3 = arg2.get(JAVA_INT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndFloatDoubleFloatFromStruct(double arg1, MemorySegment arg2) { - float structElem1 = arg2.get(JAVA_FLOAT, 0); - /* The size of [float, double, float] on AIX/PPC 64-bit is 16 bytes without padding by default - * while the same struct is 20 bytes with padding on other platforms. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - float structElem3 = arg2.get(JAVA_FLOAT, isAixOS ? 12 : 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static double addDoubleAndIntDoubleLongFromStruct(double arg1, MemorySegment arg2) { - int structElem1 = arg2.get(JAVA_INT, 0); - /* The padding in the struct [int, double, long] on AIX/PPC 64-bit is different from - * other platforms as follows: - * 1) there is no padding between int and double. - * 2) there is a 4-byte padding between double and long. - */ - double structElem2 = arg2.get(JAVA_DOUBLE, isAixOS ? 4 : 8); - double structElem3 = arg2.get(JAVA_LONG, 16); - double doubleSum = arg1 + structElem1 + structElem2 + structElem3; - return doubleSum; - } - - public static MemorySegment return254BytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(254, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, scope); - - for (int i = 0; i < 254; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } - - public static MemorySegment return4KBytesFromStruct() { - SequenceLayout byteArray = MemoryLayout.sequenceLayout(4096, JAVA_BYTE); - GroupLayout structLayout = MemoryLayout.structLayout(byteArray); - MemorySegment byteArrStruSegment = MemorySegment.allocateNative(structLayout, scope); - - for (int i = 0; i < 4096; i++) { - byteArrStruSegment.set(JAVA_BYTE, i, (byte)i); - } - return byteArrStruSegment; - } - - public static byte addNegBytesFromStruct(byte arg1, MemorySegment arg2, byte arg3, byte arg4) { - byte arg2_elem1 = arg2.get(JAVA_BYTE, 0); - byte arg2_elem2 = arg2.get(JAVA_BYTE, 1); - - Assert.assertEquals((byte)-6, ((Byte)arg1).byteValue()); - Assert.assertEquals((byte)-8, ((Byte)arg2_elem1).byteValue()); - Assert.assertEquals((byte)-9, ((Byte)arg2_elem2).byteValue()); - Assert.assertEquals((byte)-8, ((Byte)arg3).byteValue()); - Assert.assertEquals((byte)-9, ((Byte)arg4).byteValue()); - - byte byteSum = (byte)(arg1 + arg2_elem1 + arg2_elem2 + arg3 + arg4); - return byteSum; - } - - public static short addNegShortsFromStruct(short arg1, MemorySegment arg2, short arg3, short arg4) { - short arg2_elem1 = arg2.get(JAVA_SHORT, 0); - short arg2_elem2 = arg2.get(JAVA_SHORT, 2); - - Assert.assertEquals((short)-777, ((Short)arg1).shortValue()); - Assert.assertEquals((short)-888, ((Short)arg2_elem1).shortValue()); - Assert.assertEquals((short)-999, ((Short)arg2_elem2).shortValue()); - Assert.assertEquals((short)-888, ((Short)arg3).shortValue()); - Assert.assertEquals((short)-999, ((Short)arg4).shortValue()); - - short shortSum = (short)(arg1 + arg2_elem1 + arg2_elem2 + arg3 + arg4); - return shortSum; - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/ApiTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/ApiTests.java deleted file mode 100644 index 8f0b06e8dff..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/ApiTests.java +++ /dev/null @@ -1,902 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; -import static org.testng.Assert.fail; - -import java.lang.invoke.VarHandle; -import java.util.NoSuchElementException; - -import java.lang.foreign.Arena; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for the vararg list in VaList API specific cases. - */ -@Test(groups = { "level.sanity" }) -public class ApiTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isMacOsAarch64 = osName.contains("mac") && arch.contains("aarch64"); - private static boolean isSysVPPC64le = osName.contains("linux") && arch.contains("ppc64"); - - @Test - public void test_emptyVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList emptyVaList = VaList.empty(); - /* As specified in the implemention of OpenJDK, a NULL address is set to - * the empty va_list on Windows/x86_64, MacOS/Aarch64, Linux/ppc64le and - * AIX/ppc64 while the va_list without any argument is created on a fixed - * address on other platforms. - */ - if (isWinX64 || isMacOsAarch64 || isSysVPPC64le || isAixOS) { - Assert.assertEquals(emptyVaList.segment(), MemorySegment.NULL); - } else { - Assert.assertNotEquals(emptyVaList.segment(), MemorySegment.NULL); - } - } - } - - @Test - public void test_checkIntVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_checkLongVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_checkDoubleVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0.0001D); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0.0001D); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test - public void test_checkIntPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt1); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt2); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_checkLongPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt1); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt2); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_checkDoublePtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3), arena.scope()); - - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt1); /* the 1st argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt2); /* the 2nd argument */ - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_checkIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_checkLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_checkLongStructVaListFromPrefixAllocator() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1), arena.scope()); - - MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout, arena.scope()); - MemorySegment argSegmt = vaList.nextVarg(structLayout, SegmentAllocator.prefixAllocator(structSegmt2)); - Assert.assertEquals(longHandle1.get(argSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - Assert.assertEquals(longHandle1.get(structSegmt2), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(structSegmt2), 6677889911L); /* the 2nd element of the 1st struct argument */ - } - } - - @Test - public void test_checkDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 111150.1001D); - doubleHandle2.set(structSegmt1, 111160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 111170.1001D); - doubleHandle2.set(structSegmt2, 111180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 111150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 111160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 111170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 111180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyIntVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_copyLongVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_copyDoubleVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0001D); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0001D); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0001D); /* the 3rd argument */ - } - } - - @Test - public void test_copyIntPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt1); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt2); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), intSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_copyLongPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt1); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt2); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), longSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_copyDoublePtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3), arena.scope()); - VaList resultVaList = vaList.copy(); - - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt1); /* the 1st argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt2); /* the 2nd argument */ - Assert.assertEquals(resultVaList.nextVarg(ADDRESS), doubleSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_copyIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(resultArgSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(resultArgSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(resultArgSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(resultArgSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(resultArgSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(resultArgSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(resultArgSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(resultArgSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_copyDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 111150.1001D); - doubleHandle2.set(structSegmt1, 111160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 111170.1001D); - doubleHandle2.set(structSegmt2, 111180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList resultVaList = vaList.copy(); - - MemorySegment resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(resultArgSegmt), 111150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(resultArgSegmt), 111160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - resultArgSegmt = resultVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(resultArgSegmt), 111170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(resultArgSegmt), 111180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreNextArg_IntVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), arena.scope()); - - vaList.nextVarg(JAVA_INT); /* the 1st argument */ - vaList.nextVarg(JAVA_INT); /* the 2nd argument */ - vaList.nextVarg(JAVA_INT); /* the 3rd argument */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.nextVarg(JAVA_INT); - fail("Failed to throw out NoSuchElementException when nextVarg() exceeds the memory region of VaList"); - } - } - - @Test - public void test_createIntVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 700); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_INT), 900); /* the 3rd argument */ - } - } - - @Test - public void test_createLongVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 700000L); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 800000L); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_createDoubleVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111150.1001D, 0.0001D); /* the 1st argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111160.2002D, 0.0001D); /* the 2nd argument */ - Assert.assertEquals(newVaList.nextVarg(JAVA_DOUBLE), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test - public void test_createIntPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment resultAddr = newVaList.nextVarg(ADDRESS); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 700); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 800); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_INT.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 900); /* the 3rd argument */ - } - } - - @Test - public void test_createLongPtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment resultAddr = newVaList.nextVarg(ADDRESS); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 700000L); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 800000L); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_LONG.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_createDoublePtrVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment resultAddr = newVaList.nextVarg(ADDRESS); - MemorySegment resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 111150.1001D, 0.0001D); /* the 1st argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 111160.2002D, 0.0001D); /* the 2nd argument */ - resultAddr = newVaList.nextVarg(ADDRESS); - resultSegmt = MemorySegment.ofAddress(resultAddr.address(), JAVA_DOUBLE.byteSize(), arena.scope()); - Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 111170.1001D, 0.0001D); /* the 3rd argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreNextArg_IntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - - vaList.nextVarg(structLayout, allocator); /* the 1st struct argument */ - vaList.nextVarg(structLayout, allocator); /* the 2nd struct argument */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.nextVarg(structLayout, allocator); - fail("Failed to throw out NoSuchElementException when nextVarg() exceeds the memory region of VaList"); - } - } - - @Test - public void test_createIntStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(newArgSegmt), 1122333); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(intHandle2.get(newArgSegmt), 4455666); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(newArgSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(newArgSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_createLongStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(newArgSegmt), 1122334455L); /* the 1st element of the 1st struct argument */ - Assert.assertEquals(longHandle2.get(newArgSegmt), 6677889911L); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(newArgSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(newArgSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_createDoubleStructVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - VaList newVaList = VaList.ofAddress(vaList.segment().address(), arena.scope()); - - MemorySegment newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(newArgSegmt), 11150.1001D, 0.0001D); /* the 1st element of the 1st struct argument */ - Assert.assertEquals((double)doubleHandle2.get(newArgSegmt), 11160.2002D, 0.0001D); /* the 2nd element of the 1st struct argument */ - newArgSegmt = newVaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(newArgSegmt), 11170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(newArgSegmt), 11180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreSkippedArg_IntArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), arena.scope()); - vaList.skip(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT); /* Skip over all arguments in VaList */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.skip(JAVA_INT); - fail("Failed to throw out NoSuchElementException when skip() exceeds the memory region of VaList"); - } - } - - @Test - public void test_skipIntArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), arena.scope()); - vaList.skip(JAVA_INT); - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 800); /* the 2nd argument */ - vaList.skip(JAVA_INT); - Assert.assertEquals(vaList.nextVarg(JAVA_INT), 1000); /* the 4th argument */ - } - } - - @Test - public void test_skipLongArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), arena.scope()); - vaList.skip(JAVA_LONG, JAVA_LONG); - Assert.assertEquals(vaList.nextVarg(JAVA_LONG), 900000L); /* the 3rd argument */ - } - } - - @Test - public void test_skipDoubleArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D) - .addVarg(JAVA_DOUBLE, 111180.2002D), arena.scope()); - vaList.skip(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE); - Assert.assertEquals(vaList.nextVarg(JAVA_DOUBLE), 111180.2002D, 0.0001D); /* the 4th argument */ - } - } - - @Test - public void test_skipIntPtrArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - MemorySegment intSegmt4 = allocator.allocate(JAVA_INT, 1000); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3) - .addVarg(ADDRESS, intSegmt4), arena.scope()); - vaList.skip(ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), intSegmt2); /* the 2nd argument */ - } - } - - @Test - public void test_skipLongPtrArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - MemorySegment longSegmt4 = allocator.allocate(JAVA_LONG, 1000000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3) - .addVarg(ADDRESS, longSegmt4), arena.scope()); - vaList.skip(ADDRESS, ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), longSegmt3); /* the 3rd argument */ - } - } - - @Test - public void test_skipDoublePtrArgOfVaList() throws Throwable { - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - MemorySegment doubleSegmt4 = allocator.allocate(JAVA_DOUBLE, 111180.1002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3) - .addVarg(ADDRESS, doubleSegmt4), arena.scope()); - vaList.skip(ADDRESS, ADDRESS, ADDRESS); - Assert.assertEquals(vaList.nextVarg(ADDRESS), doubleSegmt4); /* the 4th argument */ - } - } - - @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No such element.*") - public void test_NoMoreSkippedArg_IntStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - vaList.skip(structLayout, structLayout); /* Skip over all arguments in VaList */ - - /* An exception is thrown as there is no more argument in VaList */ - vaList.skip(structLayout); - fail("Failed to throw out NoSuchElementException when skip() exceeds the memory region of VaList"); - } - } - - @Test - public void test_skipIntStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(intHandle1.get(argSegmt), 2244668); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(intHandle2.get(argSegmt), 1133557); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_skipLongStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals(longHandle1.get(argSegmt), 2233445566L); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals(longHandle2.get(argSegmt), 7788991122L); /* the 2nd element of the 2nd struct argument */ - } - } - - @Test - public void test_skipDoubleStructOfVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - vaList.skip(structLayout); - - MemorySegment argSegmt = vaList.nextVarg(structLayout, allocator); - Assert.assertEquals((double)doubleHandle1.get(argSegmt), 11170.1001D, 0.0001D); /* the 1st element of the 2nd struct argument */ - Assert.assertEquals((double)doubleHandle2.get(argSegmt), 11180.2002D, 0.0001D); /* the 2nd element of the 2nd struct argument */ - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/DowncallTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/DowncallTests.java deleted file mode 100644 index 08578d82572..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/DowncallTests.java +++ /dev/null @@ -1,1089 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for the vararg list in downcall. - */ -@Test(groups = { "level.sanity" }) -public class DowncallTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxX64 = osName.contains("linux") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxAarch64 = osName.contains("linux") && arch.equals("aarch64"); - /* The padding of struct is not required on Power in terms of VaList */ - private static boolean isStructPaddingNotRequired = arch.startsWith("ppc64"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup defaultLibLookup = linker.defaultLookup(); - - @Test - public void test_addIntsFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addIntsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), arena.scope()); - int result = (int)mh.invoke(4, vaList.segment()); - Assert.assertEquals(result, 3400); - } - } - - @Test - public void test_addLongsFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addLongsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), arena.scope()); - long result = (long)mh.invoke(4, vaList.segment()); - Assert.assertEquals(result, 3400000L); - } - } - - @Test - public void test_addDoublesFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addDoublesFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 150.1001D) - .addVarg(JAVA_DOUBLE, 160.2002D) - .addVarg(JAVA_DOUBLE, 170.1001D) - .addVarg(JAVA_DOUBLE, 180.2002D), arena.scope()); - double result = (double)mh.invoke(4, vaList.segment()); - Assert.assertEquals(result, 660.6006D, 0.0001D); - } - } - - @Test - public void test_addMixedArgsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting this struct with - * the mixed elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - MemorySegment functionSymbol = nativeLibLookup.find("addMixedArgsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_DOUBLE, 160.2002D), arena.scope()); - double result = (double)mh.invoke(vaList.segment()); - Assert.assertEquals(result, 800860.2002D, 0.0001D); - } - } - } - - @Test - public void test_addMoreMixedArgsFromVaList() throws Throwable { - /* VaList on Linux/x86_64 and Windows/x86_64 in OpenJDK is unable to handle - * the va_list with over 8 arguments (confirmed by OpenJDK/Hotspot). So the - * test is disabled for now till the issue is fixed by OpenJDK. - */ - if (!isLinuxX64 && !isWinX64) { - MemorySegment functionSymbol = nativeLibLookup.find("addMoreMixedArgsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 100) - .addVarg(JAVA_LONG, 200000L) - .addVarg(JAVA_INT, 300) - .addVarg(JAVA_LONG, 400000L) - .addVarg(JAVA_INT, 500) - .addVarg(JAVA_LONG, 600000L) - .addVarg(JAVA_INT, 700) - .addVarg(JAVA_DOUBLE, 161.2001D) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_DOUBLE, 162.2002D) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_DOUBLE, 163.2003D) - .addVarg(JAVA_INT, 1000) - .addVarg(JAVA_DOUBLE, 164.2004D) - .addVarg(JAVA_INT, 1100) - .addVarg(JAVA_DOUBLE, 165.2005D), arena.scope()); - double result = (double)mh.invoke(vaList.segment()); - Assert.assertEquals(result, 1206216.0015D, 0.0001D); - } - } - } - - @Test - public void test_addIntsByPtrFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addIntsByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3), arena.scope()); - int result = (int)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 2400); - } - } - - @Test - public void test_addLongsByPtrFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addLongsByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3), arena.scope()); - long result = (long)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 2400000L); - } - } - - @Test - public void test_addDoublesByPtrFromVaList() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addDoublesByPtrFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 111150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 111160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 111170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3), arena.scope()); - double result = (double)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 333480.4004D, 0.0001D); - } - } - - @Test - public void test_add1ByteOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1ByteOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)2); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt3, (byte)3); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt4, (byte)4); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt5, (byte)5); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt6, (byte)6); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt7, (byte)7); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt8, (byte)8); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt9, (byte)9); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt10, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - byte result = (byte)mh.invoke(10, vaList.segment()); - Assert.assertEquals(result, 55); - } - } - } - - @Test - public void test_add2BytesOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two byte elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)3); - byteHandle2.set(structSegmt2, (byte)4); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - byte result = (byte)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 10); - } - } - } - - @Test - public void test_add3BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)4); - byteHandle2.set(structSegmt2, (byte)5); - byteHandle3.set(structSegmt2, (byte)6); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - byte result = (byte)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 21); - } - } - - @Test - public void test_add5BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - MemorySegment functionSymbol = nativeLibLookup.find("add5BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)6); - byteHandle2.set(structSegmt2, (byte)7); - byteHandle3.set(structSegmt2, (byte)8); - byteHandle4.set(structSegmt2, (byte)9); - byteHandle5.set(structSegmt2, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - byte result = (byte)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 55); - } - } - - @Test - public void test_add7BytesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), - JAVA_BYTE.withName("elem5"), JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle byteHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle byteHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - MemorySegment functionSymbol = nativeLibLookup.find("add7BytesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - byteHandle6.set(structSegmt1, (byte)6); - byteHandle7.set(structSegmt1, (byte)7); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)8); - byteHandle2.set(structSegmt2, (byte)9); - byteHandle3.set(structSegmt2, (byte)10); - byteHandle4.set(structSegmt2, (byte)11); - byteHandle5.set(structSegmt2, (byte)12); - byteHandle6.set(structSegmt2, (byte)13); - byteHandle7.set(structSegmt2, (byte)14); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - byte result = (byte)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 105); - } - } - - @Test - public void test_add1ShortOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1ShortOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt3, (short)333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt4, (short)444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt5, (short)555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt6, (short)666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt7, (short)777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt8, (short)888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt9, (short)999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt10, (short)123); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - short result = (short)mh.invoke(10, vaList.segment()); - Assert.assertEquals(result, 5118); - } - } - } - - @Test - public void test_add2ShortsOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two short elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)333); - shortHandle2.set(structSegmt2, (short)444); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - short result = (short)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 1110); - } - } - } - - @Test - public void test_add3ShortsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3ShortsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - shortHandle3.set(structSegmt1, (short)333); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)444); - shortHandle2.set(structSegmt2, (short)555); - shortHandle3.set(structSegmt2, (short)666); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - short result = (short)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add1IntOfStructsFromVaList() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1IntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - intHandle1.set(structSegmt3, 3333333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - intHandle1.set(structSegmt4, 4444444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - intHandle1.set(structSegmt5, 5555555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - intHandle1.set(structSegmt6, 6666666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - intHandle1.set(structSegmt7, 7777777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - intHandle1.set(structSegmt8, 8888888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - intHandle1.set(structSegmt9, 9999999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - intHandle1.set(structSegmt10, 1234567); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - int result = (int)mh.invoke(10, vaList.segment()); - Assert.assertEquals(result, 51234562); - } - } - } - - @Test - public void test_add2IntsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - int result = (int)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 8956224); - } - } - - @Test - public void test_add3IntsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3IntsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - intHandle3.set(structSegmt1, 7788999); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 1133555); - intHandle2.set(structSegmt2, 2244666); - intHandle3.set(structSegmt2, 3322111); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - int result = (int)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 20067330); - } - } - - @Test - public void test_add2LongsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2LongsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - long result = (long)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 17822661054L); - } - } - - @Test - public void test_add1FloatOfStructsFromVaList() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with only - * one float element (confirmed by OpenJDK/Hotspot). Thus, the test is disabled on - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1FloatOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 3.33F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 4.44F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 5.56F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), arena.scope()); - float result = (float)mh.invoke(5, vaList.segment()); - Assert.assertEquals(result, 16.66F, 0.01F); - } - } - } - - @Test - public void test_add2FloatsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 3.33F); - floatHandle2.set(structSegmt2, 4.44F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 5.55F); - floatHandle2.set(structSegmt3, 6.66F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 7.77F); - floatHandle2.set(structSegmt4, 8.88F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.99F); - floatHandle2.set(structSegmt5, 1.23F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 4.56F); - floatHandle2.set(structSegmt6, 7.89F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), arena.scope()); - float result = (float)mh.invoke(6, vaList.segment()); - Assert.assertEquals(result, 63.63F, 0.01F); - } - } - - @Test - public void test_add3FloatsOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), MemoryLayout.paddingLayout(32)); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3FloatsOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - floatHandle3.set(structSegmt1, 3.33F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 4.44F); - floatHandle2.set(structSegmt2, 5.55F); - floatHandle3.set(structSegmt2, 6.66F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 7.77F); - floatHandle2.set(structSegmt3, 8.88F); - floatHandle3.set(structSegmt3, 9.99F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 1.23F); - floatHandle2.set(structSegmt4, 4.56F); - floatHandle3.set(structSegmt4, 7.89F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.87F); - floatHandle2.set(structSegmt5, 6.54F); - floatHandle3.set(structSegmt5, 3.21F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 2.46F); - floatHandle2.set(structSegmt6, 8.13F); - floatHandle3.set(structSegmt6, 5.79F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), arena.scope()); - float result = (float)mh.invoke(6, vaList.segment()); - Assert.assertEquals(result, 99.63F, 0.01F); - } - } - - @Test - public void test_add1DoubleOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1DoubleOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 11111.1001D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 11111.1002D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 11111.1003D); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 11111.1004D); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 11111.1005D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), arena.scope()); - double result = (double)mh.invoke(5, vaList.segment()); - Assert.assertEquals(result, 55555.5015D, 0.0001D); - } - } - - @Test - public void test_add2DoublesOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2DoublesOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - double result = (double)mh.invoke(2, vaList.segment()); - Assert.assertEquals(result, 44660.6006D, 0.0001D); - } - } - - @Test - public void test_vprintfFromDefaultLibFromVaList() throws Throwable { - /* 1) Disable the test on Windows given a misaligned access exception coming from - * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString() - * is also captured on OpenJDK/Hotspot. - * 2) Disable the test on AIX as Valist is not yet implemented in OpenJDK. - */ - if (!isWinX64 && !isAixOS) { - MemorySegment functionSymbol = defaultLibLookup.find("vprintf").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment formatSegmt = allocator.allocateUtf8String("%d * %d = %d\n"); - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 7) - .addVarg(JAVA_INT, 8) - .addVarg(JAVA_INT, 56), arena.scope()); - mh.invoke(formatSegmt, vaList.segment()); - } - } - } - - @Test - public void test_addIntShortOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addIntShortOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, (short)123); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, (short)456); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, (short)789); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - int result = (int)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addShortIntOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addShortIntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, (short)123); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, (short)456); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, (short)789); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - int result = (int)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addIntLongOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addIntLongOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, 101010101010L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, 202020202020L); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, 303030303030L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - long result = (long)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addLongIntOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addLongIntOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 101010101010L); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 202020202020L); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 303030303030L); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - long result = (long)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addFloatDoubleOfStructsFromVaList() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addFloatDoubleOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1.11F); - elemHandle2.set(structSegmt1, 222.222D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2.22F); - elemHandle2.set(structSegmt2, 333.333D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3.33F); - elemHandle2.set(structSegmt3, 444.444D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - double result = (double)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } - - @Test - public void test_addDoubleFloatOfStructsFromVaList() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFloatOfStructsFromVaList").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 222.222D); - elemHandle2.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 333.333D); - elemHandle2.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 444.444D); - elemHandle2.set(structSegmt3, 3.33F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - double result = (double)mh.invoke(3, vaList.segment()); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/UpcallTests.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/UpcallTests.java deleted file mode 100644 index 82c48b6a730..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/UpcallTests.java +++ /dev/null @@ -1,1151 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.valist; - -import org.testng.annotations.Test; -import org.testng.Assert; -import org.testng.AssertJUnit; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import java.lang.foreign.Linker; -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SymbolLookup; -import java.lang.foreign.VaList; -import static java.lang.foreign.ValueLayout.*; -import static java.lang.foreign.VaList.Builder; - -/** - * Test cases for JEP 434: Foreign Linker API (Second Preview) for the vararg list in upcall. - */ -@Test(groups = { "level.sanity" }) -public class UpcallTests { - private static String osName = System.getProperty("os.name").toLowerCase(); - private static String arch = System.getProperty("os.arch").toLowerCase(); - private static boolean isAixOS = osName.contains("aix"); - private static boolean isWinX64 = osName.contains("win") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxX64 = osName.contains("linux") && (arch.equals("amd64") || arch.equals("x86_64")); - private static boolean isLinuxAarch64 = osName.contains("linux") && arch.equals("aarch64"); - /* The padding of struct is not required on Power in terms of VaList */ - private static boolean isStructPaddingNotRequired = arch.startsWith("ppc64"); - private static Linker linker = Linker.nativeLinker(); - - static { - System.loadLibrary("clinkerffitests"); - } - private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); - - @Test - public void test_addIntsFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addIntsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_INT, 1000), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(4, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 3400); - } - } - - @Test - public void test_addLongsFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addLongsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_LONG, 900000L) - .addVarg(JAVA_LONG, 1000000L), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), arena.scope()); - - long result = (long)mh.invoke(4, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 3400000L); - } - } - - @Test - public void test_addDoublesFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addDoublesFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 111150.1001D) - .addVarg(JAVA_DOUBLE, 111160.2002D) - .addVarg(JAVA_DOUBLE, 111170.1001D) - .addVarg(JAVA_DOUBLE, 111180.2002D), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoublesFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(4, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 444660.6006D, 0.0001D); - } - } - - @Test - public void test_addMixedArgsFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addMixedArgsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700) - .addVarg(JAVA_LONG, 800000L) - .addVarg(JAVA_DOUBLE, 160.2002D), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addMixedArgsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 800860.2002D, 0.0001D); - } - } - - @Test - public void test_addMoreMixedArgsFromVaListByUpcallMH() throws Throwable { - /* VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). So the test is disabled - * for now till the issue is fixed by OpenJDK on Linux/x86_64. - */ - if (!isLinuxX64) { - MemorySegment functionSymbol = nativeLibLookup.find("addMoreMixedArgsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 100) - .addVarg(JAVA_LONG, 200000L) - .addVarg(JAVA_INT, 300) - .addVarg(JAVA_LONG, 400000L) - .addVarg(JAVA_INT, 500) - .addVarg(JAVA_LONG, 600000L) - .addVarg(JAVA_INT, 700) - .addVarg(JAVA_DOUBLE, 161.2001D) - .addVarg(JAVA_INT, 800) - .addVarg(JAVA_DOUBLE, 162.2002D) - .addVarg(JAVA_INT, 900) - .addVarg(JAVA_DOUBLE, 163.2003D) - .addVarg(JAVA_INT, 1000) - .addVarg(JAVA_DOUBLE, 164.2004D) - .addVarg(JAVA_INT, 1100) - .addVarg(JAVA_DOUBLE, 165.2005D), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addMoreMixedArgsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 1206216.0015D, 0.0001D); - } - } - } - - @Test - public void test_addIntsByPtrFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addIntsByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment intSegmt1 = allocator.allocate(JAVA_INT, 700); - MemorySegment intSegmt2 = allocator.allocate(JAVA_INT, 800); - MemorySegment intSegmt3 = allocator.allocate(JAVA_INT, 900); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, intSegmt1) - .addVarg(ADDRESS, intSegmt2) - .addVarg(ADDRESS, intSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntsByPtrFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 2400); - } - } - - @Test - public void test_addLongsByPtrFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addLongsByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment longSegmt1 = allocator.allocate(JAVA_LONG, 700000L); - MemorySegment longSegmt2 = allocator.allocate(JAVA_LONG, 800000L); - MemorySegment longSegmt3 = allocator.allocate(JAVA_LONG, 900000L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, longSegmt1) - .addVarg(ADDRESS, longSegmt2) - .addVarg(ADDRESS, longSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongsByPtrFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), arena.scope()); - - long result = (long)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 2400000L); - } - } - - @Test - public void test_addDoublesByPtrFromVaListByUpcallMH() throws Throwable { - MemorySegment functionSymbol = nativeLibLookup.find("addDoublesByPtrFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment doubleSegmt1 = allocator.allocate(JAVA_DOUBLE, 150.1001D); - MemorySegment doubleSegmt2 = allocator.allocate(JAVA_DOUBLE, 160.2002D); - MemorySegment doubleSegmt3 = allocator.allocate(JAVA_DOUBLE, 170.1001D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(ADDRESS, doubleSegmt1) - .addVarg(ADDRESS, doubleSegmt2) - .addVarg(ADDRESS, doubleSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoublesByPtrFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 480.4004D, 0.0001D); - } - } - - @Test - public void test_add1ByteOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1ByteOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)2); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt3, (byte)3); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt4, (byte)4); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt5, (byte)5); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt6, (byte)6); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt7, (byte)7); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt8, (byte)8); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt9, (byte)9); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt10, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1ByteOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), arena.scope()); - - byte result = (byte)mh.invoke(10, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 55); - } - } - } - - @Test - public void test_add2BytesOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two byte elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)3); - byteHandle2.set(structSegmt2, (byte)4); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), arena.scope()); - - byte result = (byte)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 10); - } - } - } - - @Test - public void test_add3BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)4); - byteHandle2.set(structSegmt2, (byte)5); - byteHandle3.set(structSegmt2, (byte)6); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), arena.scope()); - - byte result = (byte)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 21); - } - } - - @Test - public void test_add5BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - MemorySegment functionSymbol = nativeLibLookup.find("add5BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)6); - byteHandle2.set(structSegmt2, (byte)7); - byteHandle3.set(structSegmt2, (byte)8); - byteHandle4.set(structSegmt2, (byte)9); - byteHandle5.set(structSegmt2, (byte)10); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add5BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), arena.scope()); - - byte result = (byte)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 55); - } - } - - @Test - public void test_add7BytesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4"), - JAVA_BYTE.withName("elem5"), JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle byteHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle byteHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle byteHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle byteHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle byteHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - MemorySegment functionSymbol = nativeLibLookup.find("add7BytesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt1, (byte)1); - byteHandle2.set(structSegmt1, (byte)2); - byteHandle3.set(structSegmt1, (byte)3); - byteHandle4.set(structSegmt1, (byte)4); - byteHandle5.set(structSegmt1, (byte)5); - byteHandle6.set(structSegmt1, (byte)6); - byteHandle7.set(structSegmt1, (byte)7); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - byteHandle1.set(structSegmt2, (byte)8); - byteHandle2.set(structSegmt2, (byte)9); - byteHandle3.set(structSegmt2, (byte)10); - byteHandle4.set(structSegmt2, (byte)11); - byteHandle5.set(structSegmt2, (byte)12); - byteHandle6.set(structSegmt2, (byte)13); - byteHandle7.set(structSegmt2, (byte)14); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add7BytesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_BYTE, JAVA_INT, ADDRESS), arena.scope()); - - byte result = (byte)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 105); - } - } - - @Test - public void test_add1ShortOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1ShortOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt3, (short)333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt4, (short)444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt5, (short)555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt6, (short)666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt7, (short)777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt8, (short)888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt9, (short)999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt10, (short)123); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1ShortOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), arena.scope()); - - short result = (short)mh.invoke(10, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 5118); - } - } - } - - @Test - public void test_add2ShortsOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with - * two short elements (confirmed by OpenJDK/Hotspot). Thus, the test is disabled - * on Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2ShortsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)333); - shortHandle2.set(structSegmt2, (short)444); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2ShortsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), arena.scope()); - - short result = (short)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 1110); - } - } - } - - @Test - public void test_add3ShortsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3ShortsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt1, (short)111); - shortHandle2.set(structSegmt1, (short)222); - shortHandle3.set(structSegmt1, (short)333); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - shortHandle1.set(structSegmt2, (short)444); - shortHandle2.set(structSegmt2, (short)555); - shortHandle3.set(structSegmt2, (short)666); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3ShortsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_SHORT, JAVA_INT, ADDRESS), arena.scope()); - - short result = (short)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 2331); - } - } - - @Test - public void test_add1IntOfStructsFromVaListByUpcallMH() throws Throwable { - /* There are a few issues with the test on some platforms as follows: - * 1) VaList on Linux/x86_64 in OpenJDK is unable to handle the va_list with - * over 8 arguments (confirmed by OpenJDK/Hotspot). - * 2) VaList on Linux/Aarch64 and Windows/x86_64 in OpenJDK has problem in supporting - * the struct with only one integral element (confirmed by OpenJDK/Hotspot). - * Thus, the test is disabled on both these platforms for now till these issues - * are fixed in OpenJDK and verified on OpenJDK/Hotspot in the future. - */ - if (!isLinuxX64 && !isLinuxAarch64 && !isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1IntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - intHandle1.set(structSegmt3, 3333333); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - intHandle1.set(structSegmt4, 4444444); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - intHandle1.set(structSegmt5, 5555555); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - intHandle1.set(structSegmt6, 6666666); - MemorySegment structSegmt7 = allocator.allocate(structLayout); - intHandle1.set(structSegmt7, 7777777); - MemorySegment structSegmt8 = allocator.allocate(structLayout); - intHandle1.set(structSegmt8, 8888888); - MemorySegment structSegmt9 = allocator.allocate(structLayout); - intHandle1.set(structSegmt9, 9999999); - MemorySegment structSegmt10 = allocator.allocate(structLayout); - intHandle1.set(structSegmt10, 1234567); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6) - .addVarg(structLayout, structSegmt7) - .addVarg(structLayout, structSegmt8) - .addVarg(structLayout, structSegmt9) - .addVarg(structLayout, structSegmt10), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1IntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(10, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 51234562); - } - } - } - - @Test - public void test_add2IntsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2IntsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 2244668); - intHandle2.set(structSegmt2, 1133557); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2IntsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 8956224); - } - } - - @Test - public void test_add3IntsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle intHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3IntsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - intHandle1.set(structSegmt1, 1122333); - intHandle2.set(structSegmt1, 4455666); - intHandle3.set(structSegmt1, 7788999); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - intHandle1.set(structSegmt2, 1133555); - intHandle2.set(structSegmt2, 2244666); - intHandle3.set(structSegmt2, 3322111); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3IntsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 20067330); - } - } - - @Test - public void test_add2LongsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2LongsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - longHandle1.set(structSegmt1, 1122334455L); - longHandle2.set(structSegmt1, 6677889911L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - longHandle1.set(structSegmt2, 2233445566L); - longHandle2.set(structSegmt2, 7788991122L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2LongsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), arena.scope()); - - long result = (long)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 17822661054L); - } - } - - @Test - public void test_add1FloatOfStructsFromVaListByUpcallMH() throws Throwable { - /* VaList on Windows/x86_64 in OpenJDK has problem in supporting the struct with only - * one float element (confirmed by OpenJDK/Hotspot). Thus, the test is disabled on - * Windows/x86_64 for now till the issue is fixed in OpenJDK and verified on - * OpenJDK/Hotspot in the future. - */ - if (!isWinX64) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1FloatOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 3.33F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 4.44F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 5.56F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1FloatOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), arena.scope()); - - float result = (float)mh.invoke(5, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 16.66F, 0.01F); - } - } - } - - @Test - public void test_add2FloatsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2FloatsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 3.33F); - floatHandle2.set(structSegmt2, 4.44F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 5.55F); - floatHandle2.set(structSegmt3, 6.66F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 7.77F); - floatHandle2.set(structSegmt4, 8.88F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.99F); - floatHandle2.set(structSegmt5, 1.23F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 4.56F); - floatHandle2.set(structSegmt6, 7.89F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2FloatsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), arena.scope()); - - float result = (float)mh.invoke(6, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 63.63F, 0.01F); - } - } - - @Test - public void test_add3FloatsOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = isStructPaddingNotRequired ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), MemoryLayout.paddingLayout(32)); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - MemorySegment functionSymbol = nativeLibLookup.find("add3FloatsOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt1, 1.11F); - floatHandle2.set(structSegmt1, 2.22F); - floatHandle3.set(structSegmt1, 3.33F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt2, 4.44F); - floatHandle2.set(structSegmt2, 5.55F); - floatHandle3.set(structSegmt2, 6.66F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt3, 7.77F); - floatHandle2.set(structSegmt3, 8.88F); - floatHandle3.set(structSegmt3, 9.99F); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt4, 1.23F); - floatHandle2.set(structSegmt4, 4.56F); - floatHandle3.set(structSegmt4, 7.89F); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt5, 9.87F); - floatHandle2.set(structSegmt5, 6.54F); - floatHandle3.set(structSegmt5, 3.21F); - MemorySegment structSegmt6 = allocator.allocate(structLayout); - floatHandle1.set(structSegmt6, 2.46F); - floatHandle2.set(structSegmt6, 8.13F); - floatHandle3.set(structSegmt6, 5.79F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5) - .addVarg(structLayout, structSegmt6), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add3FloatsOfStructsFromVaList, - FunctionDescriptor.of(JAVA_FLOAT, JAVA_INT, ADDRESS), arena.scope()); - - float result = (float)mh.invoke(6, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 99.63F, 0.01F); - } - } - - @Test - public void test_add1DoubleOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - MemorySegment functionSymbol = nativeLibLookup.find("add1DoubleOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11111.1001D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11111.1002D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt3, 11111.1003D); - MemorySegment structSegmt4 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt4, 11111.1004D); - MemorySegment structSegmt5 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt5, 11111.1005D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3) - .addVarg(structLayout, structSegmt4) - .addVarg(structLayout, structSegmt5), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add1DoubleOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(5, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 55555.5015D, 0.0001D); - } - } - - @Test - public void test_add2DoublesOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("add2DoublesOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt1, 11150.1001D); - doubleHandle2.set(structSegmt1, 11160.2002D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - doubleHandle1.set(structSegmt2, 11170.1001D); - doubleHandle2.set(structSegmt2, 11180.2002D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_add2DoublesOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(2, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 44660.6006D, 0.0001D); - } - } - - @Test - public void test_addIntShortOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addIntShortOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, (short)123); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, (short)456); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, (short)789); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntShortOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addShortIntOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addShortIntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, (short)123); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, (short)456); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, (short)789); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addShortIntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena.scope()); - - int result = (int)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 6668034); - } - } - - @Test - public void test_addIntLongOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addIntLongOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1111111); - elemHandle2.set(structSegmt1, 101010101010L); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2222222); - elemHandle2.set(structSegmt2, 202020202020L); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3333333); - elemHandle2.set(structSegmt3, 303030303030L); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addIntLongOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), arena.scope()); - - long result = (long)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addLongIntOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addLongIntOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 101010101010L); - elemHandle2.set(structSegmt1, 1111111); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 202020202020L); - elemHandle2.set(structSegmt2, 2222222); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 303030303030L); - elemHandle2.set(structSegmt3, 3333333); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addLongIntOfStructsFromVaList, - FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS), arena.scope()); - - long result = (long)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 606067272726L); - } - } - - @Test - public void test_addFloatDoubleOfStructsFromVaListByUpcallMH() throws Throwable { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addFloatDoubleOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 1.11F); - elemHandle2.set(structSegmt1, 222.222D); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 2.22F); - elemHandle2.set(structSegmt2, 333.333D); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 3.33F); - elemHandle2.set(structSegmt3, 444.444D); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addFloatDoubleOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } - - @Test - public void test_addDoubleFloatOfStructsFromVaListByUpcallMH() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - MemorySegment functionSymbol = nativeLibLookup.find("addDoubleFloatOfStructsFromVaListByUpcallMH").get(); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS, ADDRESS); - MethodHandle mh = linker.downcallHandle(functionSymbol, fd); - - try (Arena arena = Arena.openConfined()) { - SegmentAllocator allocator = SegmentAllocator.nativeAllocator(arena.scope()); - MemorySegment structSegmt1 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt1, 222.222D); - elemHandle2.set(structSegmt1, 1.11F); - MemorySegment structSegmt2 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt2, 333.333D); - elemHandle2.set(structSegmt2, 2.22F); - MemorySegment structSegmt3 = allocator.allocate(structLayout); - elemHandle1.set(structSegmt3, 444.444D); - elemHandle2.set(structSegmt3, 3.33F); - - VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(structLayout, structSegmt1) - .addVarg(structLayout, structSegmt2) - .addVarg(structLayout, structSegmt3), arena.scope()); - MemorySegment upcallFuncAddr = linker.upcallStub(VaListUpcallMethodHandles.MH_addDoubleFloatOfStructsFromVaList, - FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS), arena.scope()); - - double result = (double)mh.invoke(3, vaList.segment(), upcallFuncAddr); - Assert.assertEquals(result, 1006.659D, 0.001D); - } - } -} diff --git a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/VaListUpcallMethodHandles.java b/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/VaListUpcallMethodHandles.java deleted file mode 100644 index 0ed9df3fd04..00000000000 --- a/test/functional/Java20andUp/src/org/openj9/test/jep434/valist/VaListUpcallMethodHandles.java +++ /dev/null @@ -1,592 +0,0 @@ -/******************************************************************************* - * Copyright IBM Corp. and others 2023 - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] https://openjdk.org/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 - *******************************************************************************/ -package org.openj9.test.jep434.valist; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; - -import java.lang.foreign.Arena; -import static java.lang.foreign.Linker.*; -import java.lang.foreign.GroupLayout; -import java.lang.foreign.MemoryLayout; -import java.lang.foreign.MemoryLayout.PathElement; -import java.lang.foreign.MemorySegment; -import java.lang.foreign.SegmentAllocator; -import java.lang.foreign.SegmentScope; -import java.lang.foreign.SequenceLayout; -import java.lang.foreign.ValueLayout; -import static java.lang.foreign.ValueLayout.*; -import java.lang.foreign.VaList; - -/** - * The helper class that contains all upcall method handles with VaList as arguments - * - * Note: VaList is simply treated as a pointer (specified in OpenJDK) in java - * when va_list is passed as argument in native. - */ -public class VaListUpcallMethodHandles { - private static final Lookup lookup = MethodHandles.lookup(); - private static SegmentScope scope = SegmentScope.auto(); - private static SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope); - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); - - static final MethodType MT_Byte_Int_MemAddr = methodType(byte.class, int.class, MemorySegment.class); - static final MethodType MT_Short_Int_MemAddr = methodType(short.class, int.class, MemorySegment.class); - static final MethodType MT_Int_Int_MemAddr = methodType(int.class, int.class, MemorySegment.class); - static final MethodType MT_Long_Int_MemAddr = methodType(long.class, int.class, MemorySegment.class); - static final MethodType MT_Float_Int_MemAddr = methodType(float.class, int.class, MemorySegment.class); - static final MethodType MT_Double_Int_MemAddr = methodType(double.class, int.class, MemorySegment.class); - - public static final MethodHandle MH_addIntsFromVaList; - public static final MethodHandle MH_addLongsFromVaList; - public static final MethodHandle MH_addDoublesFromVaList; - public static final MethodHandle MH_addMixedArgsFromVaList; - public static final MethodHandle MH_addMoreMixedArgsFromVaList; - public static final MethodHandle MH_addIntsByPtrFromVaList; - public static final MethodHandle MH_addLongsByPtrFromVaList; - public static final MethodHandle MH_addDoublesByPtrFromVaList; - public static final MethodHandle MH_add1ByteOfStructsFromVaList; - public static final MethodHandle MH_add2BytesOfStructsFromVaList; - public static final MethodHandle MH_add3BytesOfStructsFromVaList; - public static final MethodHandle MH_add5BytesOfStructsFromVaList; - public static final MethodHandle MH_add7BytesOfStructsFromVaList; - public static final MethodHandle MH_add1ShortOfStructsFromVaList; - public static final MethodHandle MH_add2ShortsOfStructsFromVaList; - public static final MethodHandle MH_add3ShortsOfStructsFromVaList; - public static final MethodHandle MH_add1IntOfStructsFromVaList; - public static final MethodHandle MH_add2IntsOfStructsFromVaList; - public static final MethodHandle MH_add3IntsOfStructsFromVaList; - public static final MethodHandle MH_add2LongsOfStructsFromVaList; - public static final MethodHandle MH_add1FloatOfStructsFromVaList; - public static final MethodHandle MH_add2FloatsOfStructsFromVaList; - public static final MethodHandle MH_add3FloatsOfStructsFromVaList; - public static final MethodHandle MH_add1DoubleOfStructsFromVaList; - public static final MethodHandle MH_add2DoublesOfStructsFromVaList; - public static final MethodHandle MH_addIntShortOfStructsFromVaList; - public static final MethodHandle MH_addShortIntOfStructsFromVaList; - public static final MethodHandle MH_addIntLongOfStructsFromVaList; - public static final MethodHandle MH_addLongIntOfStructsFromVaList; - public static final MethodHandle MH_addFloatDoubleOfStructsFromVaList; - public static final MethodHandle MH_addDoubleFloatOfStructsFromVaList; - - static { - try { - MH_addIntsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addLongsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addDoublesFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoublesFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addMixedArgsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addMixedArgsFromVaList", methodType(double.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addMoreMixedArgsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addMoreMixedArgsFromVaList", methodType(double.class, MemorySegment.class)); //$NON-NLS-1$ - MH_addIntsByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntsByPtrFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addLongsByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongsByPtrFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addDoublesByPtrFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoublesByPtrFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_add1ByteOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1ByteOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add2BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add3BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add5BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add5BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add7BytesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add7BytesOfStructsFromVaList", MT_Byte_Int_MemAddr); //$NON-NLS-1$ - MH_add1ShortOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1ShortOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add2ShortsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2ShortsOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add3ShortsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3ShortsOfStructsFromVaList", MT_Short_Int_MemAddr); //$NON-NLS-1$ - MH_add1IntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1IntOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add2IntsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2IntsOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add3IntsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3IntsOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_add2LongsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2LongsOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_add1FloatOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1FloatOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add2FloatsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2FloatsOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add3FloatsOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add3FloatsOfStructsFromVaList", MT_Float_Int_MemAddr); //$NON-NLS-1$ - MH_add1DoubleOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add1DoubleOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_add2DoublesOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "add2DoublesOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addIntShortOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntShortOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addShortIntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addShortIntOfStructsFromVaList", MT_Int_Int_MemAddr); //$NON-NLS-1$ - MH_addIntLongOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addIntLongOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addLongIntOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addLongIntOfStructsFromVaList", MT_Long_Int_MemAddr); //$NON-NLS-1$ - MH_addFloatDoubleOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addFloatDoubleOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - MH_addDoubleFloatOfStructsFromVaList = lookup.findStatic(VaListUpcallMethodHandles.class, "addDoubleFloatOfStructsFromVaList", MT_Double_Int_MemAddr); //$NON-NLS-1$ - - } catch (IllegalAccessException | NoSuchMethodException e) { - throw new InternalError(e); - } - } - - public static int addIntsFromVaList(int argCount, MemorySegment intVaListAddr) { - VaList intVaList = VaList.ofAddress(intVaListAddr.address(), scope); - int intSum = 0; - while (argCount > 0) { - intSum += intVaList.nextVarg(JAVA_INT); - argCount--; - } - return intSum; - } - - public static long addLongsFromVaList(int argCount, MemorySegment longVaListAddr) { - VaList longVaList = VaList.ofAddress(longVaListAddr.address(), scope); - long longSum = 0; - while (argCount > 0) { - longSum += longVaList.nextVarg(JAVA_LONG); - argCount--; - } - return longSum; - } - - public static double addDoublesFromVaList(int argCount, MemorySegment doubleVaListAddr) { - VaList doubleVaList = VaList.ofAddress(doubleVaListAddr.address(), scope); - double doubleSum = 0; - while (argCount > 0) { - doubleSum += doubleVaList.nextVarg(JAVA_DOUBLE); - argCount--; - } - return doubleSum; - } - - public static double addMixedArgsFromVaList(MemorySegment argVaListAddr) { - VaList argVaList = VaList.ofAddress(argVaListAddr.address(), scope); - double doubleSum = argVaList.nextVarg(JAVA_INT) - + argVaList.nextVarg(JAVA_LONG) + argVaList.nextVarg(JAVA_DOUBLE); - return doubleSum; - } - - public static double addMoreMixedArgsFromVaList(MemorySegment argVaListAddr) { - VaList argVaList = VaList.ofAddress(argVaListAddr.address(), scope); - double doubleSum = argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_LONG) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE) - + argVaList.nextVarg(JAVA_INT) + argVaList.nextVarg(JAVA_DOUBLE); - return doubleSum; - } - - public static int addIntsByPtrFromVaList(int argCount, MemorySegment ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr.address(), scope); - int intSum = 0; - while (argCount > 0) { - MemorySegment vaListSegmt = MemorySegment.ofAddress(ptrVaList.nextVarg(ADDRESS).address(), JAVA_INT.byteSize(), scope); - intSum += vaListSegmt.get(JAVA_INT, 0); - argCount--; - } - return intSum; - } - - public static long addLongsByPtrFromVaList(int argCount, MemorySegment ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr.address(), scope); - long longSum = 0; - while (argCount > 0) { - MemorySegment vaListSegmt = MemorySegment.ofAddress(ptrVaList.nextVarg(ADDRESS).address(), JAVA_LONG.byteSize(), scope); - longSum += vaListSegmt.get(JAVA_LONG, 0); - argCount--; - } - return longSum; - } - - public static double addDoublesByPtrFromVaList(int argCount, MemorySegment ptrVaListAddr) { - VaList ptrVaList = VaList.ofAddress(ptrVaListAddr.address(), scope); - double doubleSum = 0; - while (argCount > 0) { - MemorySegment vaListSegmt = MemorySegment.ofAddress(ptrVaList.nextVarg(ADDRESS).address(), JAVA_DOUBLE.byteSize(), scope); - doubleSum += vaListSegmt.get(JAVA_DOUBLE, 0); - argCount--; - } - return doubleSum; - } - - public static byte add1ByteOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add2BytesOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add3BytesOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) + (byte)elemHandle3.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add5BytesOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), - JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle elemHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) - + (byte)elemHandle3.get(argSegmt) + (byte)elemHandle4.get(argSegmt) - + (byte)elemHandle5.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static byte add7BytesOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), - JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), - JAVA_BYTE.withName("elem4"), JAVA_BYTE.withName("elem5"), - JAVA_BYTE.withName("elem6"), JAVA_BYTE.withName("elem7")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - VarHandle elemHandle4 = structLayout.varHandle(PathElement.groupElement("elem4")); - VarHandle elemHandle5 = structLayout.varHandle(PathElement.groupElement("elem5")); - VarHandle elemHandle6 = structLayout.varHandle(PathElement.groupElement("elem6")); - VarHandle elemHandle7 = structLayout.varHandle(PathElement.groupElement("elem7")); - - byte byteSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - byteSum += (byte)elemHandle1.get(argSegmt) + (byte)elemHandle2.get(argSegmt) - + (byte)elemHandle3.get(argSegmt) + (byte)elemHandle4.get(argSegmt) - + (byte)elemHandle5.get(argSegmt) + (byte)elemHandle6.get(argSegmt) - + (byte)elemHandle7.get(argSegmt); - argCount--; - } - return byteSum; - } - - public static short add1ShortOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static short add2ShortsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static short add3ShortsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - short shortSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - shortSum += (short)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt) + (short)elemHandle3.get(argSegmt); - argCount--; - } - return shortSum; - } - - public static int add1IntOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int add2IntsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int add3IntsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt) + (int)elemHandle3.get(argSegmt); - argCount--; - } - return intSum; - } - - public static long add2LongsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (long)elemHandle1.get(argSegmt) + (long)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static float add1FloatOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static float add2FloatsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static float add3FloatsOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle elemHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); - - float floatSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - floatSum += (float)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt) + (float)elemHandle3.get(argSegmt); - argCount--; - } - return floatSum; - } - - public static double add1DoubleOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static double add2DoublesOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt) + (double)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static int addIntShortOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - JAVA_SHORT.withName("elem2"), MemoryLayout.paddingLayout(16)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (int)elemHandle1.get(argSegmt) + (short)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static int addShortIntOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), - MemoryLayout.paddingLayout(16), JAVA_INT.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - int intSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - intSum += (short)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return intSum; - } - - public static long addLongIntOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), - JAVA_INT.withName("elem2"), MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (long)elemHandle1.get(argSegmt) + (int)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static long addIntLongOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_LONG.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - long longSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - longSum += (int)elemHandle1.get(argSegmt) + (long)elemHandle2.get(argSegmt); - argCount--; - } - return longSum; - } - - public static double addFloatDoubleOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default - * while the same struct is 16 bytes with padding on other platforms. - */ - GroupLayout structLayout = isAixOS ? MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - JAVA_DOUBLE.withName("elem2")) : MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), - MemoryLayout.paddingLayout(32), JAVA_DOUBLE.withName("elem2")); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (float)elemHandle1.get(argSegmt) + (double)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } - - public static double addDoubleFloatOfStructsFromVaList(int argCount, MemorySegment struVaListAddr) { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), - JAVA_FLOAT.withName("elem2") , MemoryLayout.paddingLayout(32)); - VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - - double doubleSum = 0; - VaList struVaList = VaList.ofAddress(struVaListAddr.address(), scope); - while (argCount > 0) { - MemorySegment argSegmt = struVaList.nextVarg(structLayout, allocator); - doubleSum += (double)elemHandle1.get(argSegmt) + (float)elemHandle2.get(argSegmt); - argCount--; - } - return doubleSum; - } -} diff --git a/test/functional/Java20andUp/testng_200.xml b/test/functional/Java20andUp/testng_200.xml deleted file mode 100644 index 8d1b04a07ce..00000000000 --- a/test/functional/Java20andUp/testng_200.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/InvalidDownCallTests.java b/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/InvalidDownCallTests.java index b6078eb50b5..9b3a5fc7a98 100644 --- a/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/InvalidDownCallTests.java +++ b/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/InvalidDownCallTests.java @@ -86,10 +86,6 @@ public void test_invalidMemoryLayoutForReturnType() throws Throwable { @Test(expectedExceptions = NullPointerException.class) public void test_nullValueForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); @@ -119,10 +115,6 @@ public void test_nullValueForStructArgument() throws Throwable { } public void test_nullSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("validateNullAddrArgument").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); @@ -153,10 +145,6 @@ public void test_nullSegmentForStructArgument() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Heap segment not allowed.*") public void test_heapSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); diff --git a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/InvalidUpCallTests.java b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/InvalidUpCallTests.java index 65bd24dcb81..e340b6dcb2c 100644 --- a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/InvalidUpCallTests.java +++ b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/InvalidUpCallTests.java @@ -259,8 +259,6 @@ public void test_heapSegmentForReturnStruct() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_firstVariadicArg() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); @@ -276,8 +274,6 @@ public void test_InvalidLinkerOptions_firstVariadicArg() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_captureCallState() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); @@ -293,8 +289,6 @@ public void test_InvalidLinkerOptions_captureCallState() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_isTrivial_1() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); diff --git a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMethodHandles.java b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMethodHandles.java index 9847aac23a6..fea4c36732b 100644 --- a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMethodHandles.java +++ b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMethodHandles.java @@ -28,7 +28,6 @@ import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; import java.lang.foreign.Arena; import java.lang.foreign.FunctionDescriptor; @@ -1688,9 +1687,6 @@ public static MemorySegment add2FloatStructs_returnStructPointer(MemorySegment a public static MemorySegment add3FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); MemorySegment floatStructSegmt = arena.allocate(structLayout); float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/InvalidDownCallTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/InvalidDownCallTests.java index fe77e9eb988..67a62547908 100644 --- a/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/InvalidDownCallTests.java +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/InvalidDownCallTests.java @@ -86,10 +86,6 @@ public void test_invalidMemoryLayoutForReturnType() throws Throwable { @Test(expectedExceptions = NullPointerException.class) public void test_nullValueForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); @@ -119,10 +115,6 @@ public void test_nullValueForStructArgument() throws Throwable { } public void test_nullSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("validateNullAddrArgument").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); @@ -153,10 +145,6 @@ public void test_nullSegmentForStructArgument() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Heap segment not allowed.*") public void test_heapSegmentForPtrArgument() throws Throwable { - GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStructPointer").get(); MethodHandle mh = linker.downcallHandle(functionSymbol, fd); diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/InvalidUpCallTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/InvalidUpCallTests.java index 99445d281b0..4e918d80a4f 100644 --- a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/InvalidUpCallTests.java +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/InvalidUpCallTests.java @@ -259,8 +259,6 @@ public void test_heapSegmentForReturnStruct() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_firstVariadicArg() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); @@ -276,8 +274,6 @@ public void test_InvalidLinkerOptions_firstVariadicArg() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_captureCallState() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); @@ -293,8 +289,6 @@ public void test_InvalidLinkerOptions_captureCallState() throws Throwable { @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Not supported for upcall.*") public void test_InvalidLinkerOptions_isTrivial_1() throws Throwable { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); - VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStructByUpcallMH").get(); diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java index 4169d64d179..e26d33467a5 100644 --- a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java @@ -28,7 +28,6 @@ import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import static java.lang.invoke.MethodType.methodType; -import java.lang.invoke.VarHandle; import java.lang.foreign.Arena; import java.lang.foreign.FunctionDescriptor; @@ -1688,9 +1687,6 @@ public static MemorySegment add2FloatStructs_returnStructPointer(MemorySegment a public static MemorySegment add3FloatStructs_returnStruct(MemorySegment arg1, MemorySegment arg2) { GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); - VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1")); - VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2")); - VarHandle floatHandle3 = structLayout.varHandle(PathElement.groupElement("elem3")); MemorySegment floatStructSegmt = arena.allocate(structLayout); float floatStruct_Elem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0);