< prev index next >

test/jdk/java/foreign/TestAddressDereference.java

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @enablePreview
 27  * @library ../ /test/lib
 28  * @requires jdk.foreign.linker != "UNSUPPORTED"
 29  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestAddressDereference
 30  */
 31 
 32 import java.lang.foreign.Arena;
 33 import java.lang.foreign.FunctionDescriptor;
 34 import java.lang.foreign.Linker;
 35 import java.lang.foreign.MemorySegment;
 36 import java.lang.foreign.SymbolLookup;
 37 import java.lang.foreign.ValueLayout;
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodHandles;
 40 import java.lang.invoke.MethodType;
 41 import java.util.ArrayList;
 42 import java.util.List;


 43 
 44 import org.testng.annotations.*;
 45 
 46 import static org.testng.Assert.*;
 47 
 48 public class TestAddressDereference extends UpcallTestHelper {
 49 
 50     static final Linker LINKER = Linker.nativeLinker();
 51     static final MemorySegment GET_ADDR_SYM;
 52     static final MethodHandle GET_ADDR_CB_HANDLE, TEST_ARG_HANDLE;
 53 
 54     static {
 55         System.loadLibrary("AddressDereference");
 56         GET_ADDR_SYM = SymbolLookup.loaderLookup().find("get_addr").get();
 57         GET_ADDR_CB_HANDLE = LINKER.downcallHandle(
 58                 SymbolLookup.loaderLookup().find("get_addr_cb").get(),
 59                 FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS));
 60         try {
 61             TEST_ARG_HANDLE = MethodHandles.lookup().findStatic(TestAddressDereference.class, "testArg",
 62                     MethodType.methodType(void.class, MemorySegment.class, long.class));

167         return layoutsAndAlignments.toArray(Object[][]::new);
168     }
169 
170     enum LayoutKind {
171         BOOL(ValueLayout.JAVA_BOOLEAN),
172         CHAR(ValueLayout.JAVA_CHAR),
173         SHORT(ValueLayout.JAVA_SHORT),
174         INT(ValueLayout.JAVA_INT),
175         FLOAT(ValueLayout.JAVA_FLOAT),
176         LONG(ValueLayout.JAVA_LONG),
177         DOUBLE(ValueLayout.JAVA_DOUBLE),
178         ADDRESS(ValueLayout.ADDRESS);
179 
180 
181         final ValueLayout layout;
182 
183         LayoutKind(ValueLayout segment) {
184             this.layout = segment;
185         }
186 


187         static LayoutKind parse(String layoutString) {
188             return switch (layoutString.charAt(0)) {
189                 case 'A','a' -> ADDRESS;
190                 case 'z','Z' -> BOOL;
191                 case 'c','C' -> CHAR;
192                 case 's','S' -> SHORT;
193                 case 'i','I' -> INT;
194                 case 'f','F' -> FLOAT;
195                 case 'j','J' -> LONG;
196                 case 'd','D' -> DOUBLE;
197                 default -> throw new AssertionError("Invalid layout string: " + layoutString);
198             };



199         }
200     }
201 }

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test

 26  * @library ../ /test/lib

 27  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestAddressDereference
 28  */
 29 
 30 import java.lang.foreign.Arena;
 31 import java.lang.foreign.FunctionDescriptor;
 32 import java.lang.foreign.Linker;
 33 import java.lang.foreign.MemorySegment;
 34 import java.lang.foreign.SymbolLookup;
 35 import java.lang.foreign.ValueLayout;
 36 import java.lang.invoke.MethodHandle;
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.invoke.MethodType;
 39 import java.util.ArrayList;
 40 import java.util.List;
 41 import java.util.regex.Matcher;
 42 import java.util.regex.Pattern;
 43 
 44 import org.testng.annotations.*;
 45 
 46 import static org.testng.Assert.*;
 47 
 48 public class TestAddressDereference extends UpcallTestHelper {
 49 
 50     static final Linker LINKER = Linker.nativeLinker();
 51     static final MemorySegment GET_ADDR_SYM;
 52     static final MethodHandle GET_ADDR_CB_HANDLE, TEST_ARG_HANDLE;
 53 
 54     static {
 55         System.loadLibrary("AddressDereference");
 56         GET_ADDR_SYM = SymbolLookup.loaderLookup().find("get_addr").get();
 57         GET_ADDR_CB_HANDLE = LINKER.downcallHandle(
 58                 SymbolLookup.loaderLookup().find("get_addr_cb").get(),
 59                 FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS));
 60         try {
 61             TEST_ARG_HANDLE = MethodHandles.lookup().findStatic(TestAddressDereference.class, "testArg",
 62                     MethodType.methodType(void.class, MemorySegment.class, long.class));

167         return layoutsAndAlignments.toArray(Object[][]::new);
168     }
169 
170     enum LayoutKind {
171         BOOL(ValueLayout.JAVA_BOOLEAN),
172         CHAR(ValueLayout.JAVA_CHAR),
173         SHORT(ValueLayout.JAVA_SHORT),
174         INT(ValueLayout.JAVA_INT),
175         FLOAT(ValueLayout.JAVA_FLOAT),
176         LONG(ValueLayout.JAVA_LONG),
177         DOUBLE(ValueLayout.JAVA_DOUBLE),
178         ADDRESS(ValueLayout.ADDRESS);
179 
180 
181         final ValueLayout layout;
182 
183         LayoutKind(ValueLayout segment) {
184             this.layout = segment;
185         }
186 
187         private static final Pattern LAYOUT_PATTERN = Pattern.compile("^(?<align>\\d+%)?(?<char>[azcsifjdAZCSIFJD])\\d+$");
188 
189         static LayoutKind parse(String layoutString) {
190             Matcher matcher = LAYOUT_PATTERN.matcher(layoutString);
191             if (matcher.matches()) {
192                 switch (matcher.group("char")) {
193                     case "A","a": return ADDRESS;
194                     case "z","Z": return BOOL;
195                     case "c","C": return CHAR;
196                     case "s","S": return SHORT;
197                     case "i","I": return INT;
198                     case "f","F": return FLOAT;
199                     case "j","J": return LONG;
200                     case "d","D": return DOUBLE;
201                 };
202             }
203             throw new AssertionError("Invalid layout string: " + layoutString);
204         }
205     }
206 }
< prev index next >