1 /* 2 * Copyright (c) 2019, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 */ 26 27 package jdk.incubator.foreign.unsafe; 28 29 import jdk.incubator.foreign.MemoryAddress; 30 import jdk.internal.foreign.MemoryAddressImpl; 31 32 /** 33 * Unsafe methods to allow interop between sun.misc.unsafe and memory access API. 34 */ 35 public final class ForeignUnsafe { 36 37 private ForeignUnsafe() { 38 //just the one, please 39 } 40 41 // The following methods can be used in conjunction with the java.foreign API. 42 43 /** 44 * Obtain the base object (if any) associated with this address. This can be used in conjunction with 45 * {@link #getUnsafeOffset(MemoryAddress)} in order to obtain a base/offset addressing coordinate pair 46 * to be used with methods like {@link sun.misc.Unsafe#getInt(Object, long)} and the likes. 47 * 48 * @param address the address whose base object is to be obtained. 49 * @return the base object associated with the address, or {@code null}. 50 */ 51 public static Object getUnsafeBase(MemoryAddress address) { 52 return ((MemoryAddressImpl)address).unsafeGetBase(); 53 } 54 55 /** 56 * Obtain the offset associated with this address. If {@link #getUnsafeBase(MemoryAddress)} returns {@code null} on the passed 57 * address, then the offset is to be interpreted as the (absolute) numerical value associated said address. 58 * Alternatively, the offset represents the displacement of a field or an array element within the containing 59 * base object. This can be used in conjunction with {@link #getUnsafeBase(MemoryAddress)} in order to obtain a base/offset 60 * addressing coordinate pair to be used with methods like {@link sun.misc.Unsafe#getInt(Object, long)} and the likes. 61 * 62 * @param address the address whose offset is to be obtained. 63 * @return the offset associated with the address. 64 */ 65 public static long getUnsafeOffset(MemoryAddress address) { 66 return ((MemoryAddressImpl)address).unsafeGetOffset(); 67 } 68 }