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