1 /*
  2  *  Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  3  *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  *  This code is free software; you can redistribute it and/or modify it
  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 import jdk.incubator.foreign.Addressable;
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.FunctionDescriptor;
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemoryLayout;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.foreign.SegmentAllocator;
 33 import jdk.incubator.foreign.ValueLayout;
 34 
 35 import java.lang.invoke.MethodHandle;
 36 
 37 public class NativeTestHelper {
 38 
 39     public static boolean isIntegral(MemoryLayout layout) {
 40         return layout instanceof ValueLayout valueLayout && isIntegral(valueLayout.carrier());
 41     }
 42 
 43     static boolean isIntegral(Class<?> clazz) {
 44         return clazz == byte.class || clazz == char.class || clazz == short.class
 45                 || clazz == int.class || clazz == long.class;
 46     }
 47 
 48     public static boolean isPointer(MemoryLayout layout) {
 49         return layout instanceof ValueLayout valueLayout && valueLayout.carrier() == MemoryAddress.class;
 50     }
 51 
 52     // the constants below are useful aliases for C types. The type/carrier association is only valid for 64-bit platforms.
 53 
 54     /**
 55      * The layout for the {@code bool} C type
 56      */
 57     public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
 58     /**
 59      * The layout for the {@code char} C type
 60      */
 61     public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
 62     /**
 63      * The layout for the {@code short} C type
 64      */
 65     public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT.withBitAlignment(16);
 66     /**
 67      * The layout for the {@code int} C type
 68      */
 69     public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT.withBitAlignment(32);
 70 
 71     /**
 72      * The layout for the {@code long long} C type.
 73      */
 74     public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
 75     /**
 76      * The layout for the {@code float} C type
 77      */
 78     public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT.withBitAlignment(32);
 79     /**
 80      * The layout for the {@code double} C type
 81      */
 82     public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE.withBitAlignment(64);
 83     /**
 84      * The {@code T*} native type.
 85      */
 86     public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS.withBitAlignment(64);
 87 
 88     private static CLinker LINKER = CLinker.systemCLinker();
 89 
 90     private static final MethodHandle FREE = LINKER.downcallHandle(
 91             LINKER.lookup("free").get(), FunctionDescriptor.ofVoid(ValueLayout.ADDRESS));
 92 
 93     private static final MethodHandle MALLOC = LINKER.downcallHandle(
 94             LINKER.lookup("malloc").get(), FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG));
 95 
 96     public static void freeMemory(Addressable address) {
 97         try {
 98             FREE.invokeExact(address);
 99         } catch (Throwable ex) {
100             throw new IllegalStateException(ex);
101         }
102     }
103 
104     public static MemoryAddress allocateMemory(long size) {
105         try {
106             return (MemoryAddress)MALLOC.invokeExact(size);
107         } catch (Throwable ex) {
108             throw new IllegalStateException(ex);
109         }
110     }
111 }