1 /*
 2  * Copyright (c) 2021, 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 package org.openjdk.bench.jdk.incubator.foreign;
25 
26 import jdk.incubator.foreign.Addressable;
27 import jdk.incubator.foreign.CLinker;
28 import jdk.incubator.foreign.FunctionDescriptor;
29 import jdk.incubator.foreign.MemoryAddress;
30 import jdk.incubator.foreign.ValueLayout;
31 
32 import java.lang.invoke.MethodHandle;
33 
34 public class CLayouts {
35 
36     // the constants below are useful aliases for C types. The type/carrier association is only valid for 64-bit platforms.
37 
38     /**
39      * The layout for the {@code bool} C type
40      */
41     public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
42     /**
43      * The layout for the {@code char} C type
44      */
45     public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
46     /**
47      * The layout for the {@code short} C type
48      */
49     public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT;
50     /**
51      * The layout for the {@code int} C type
52      */
53     public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT;
54 
55     /**
56      * The layout for the {@code long long} C type.
57      */
58     public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG;
59     /**
60      * The layout for the {@code float} C type
61      */
62     public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT;
63     /**
64      * The layout for the {@code double} C type
65      */
66     public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE;
67     /**
68      * The {@code T*} native type.
69      */
70     public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS;
71 
72     private static CLinker LINKER = CLinker.systemCLinker();
73 
74     private static final MethodHandle FREE = LINKER.downcallHandle(
75             LINKER.lookup("free").get(), FunctionDescriptor.ofVoid(ValueLayout.ADDRESS));
76 
77     private static final MethodHandle MALLOC = LINKER.downcallHandle(
78             LINKER.lookup("malloc").get(), FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG));
79 
80     public static void freeMemory(Addressable address) {
81         try {
82             FREE.invokeExact(address);
83         } catch (Throwable ex) {
84             throw new IllegalStateException(ex);
85         }
86     }
87 
88     public static MemoryAddress allocateMemory(long size) {
89         try {
90             return (MemoryAddress)MALLOC.invokeExact(size);
91         } catch (Throwable ex) {
92             throw new IllegalStateException(ex);
93         }
94     }
95 }