001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle.unsafe;
024
025import java.lang.reflect.*;
026
027import sun.misc.*;
028
029import com.oracle.truffle.api.unsafe.*;
030
031@SuppressWarnings("unused")
032public final class UnsafeAccessImpl implements UnsafeAccess {
033    public UnsafeAccessImpl(Unsafe unsafe) {
034    }
035
036    public <T> T uncheckedCast(Object value, Class<T> type, boolean condition, boolean nonNull) {
037        return unsafeCast(value, type, condition, nonNull);
038    }
039
040    public boolean getBoolean(Object receiver, long offset, boolean condition, Object locationIdentity) {
041        return unsafeGetBoolean(receiver, offset, condition, locationIdentity);
042    }
043
044    public byte getByte(Object receiver, long offset, boolean condition, Object locationIdentity) {
045        return unsafeGetByte(receiver, offset, condition, locationIdentity);
046    }
047
048    public short getShort(Object receiver, long offset, boolean condition, Object locationIdentity) {
049        return unsafeGetShort(receiver, offset, condition, locationIdentity);
050    }
051
052    public int getInt(Object receiver, long offset, boolean condition, Object locationIdentity) {
053        return unsafeGetInt(receiver, offset, condition, locationIdentity);
054    }
055
056    public long getLong(Object receiver, long offset, boolean condition, Object locationIdentity) {
057        return unsafeGetLong(receiver, offset, condition, locationIdentity);
058    }
059
060    public float getFloat(Object receiver, long offset, boolean condition, Object locationIdentity) {
061        return unsafeGetFloat(receiver, offset, condition, locationIdentity);
062    }
063
064    public double getDouble(Object receiver, long offset, boolean condition, Object locationIdentity) {
065        return unsafeGetDouble(receiver, offset, condition, locationIdentity);
066    }
067
068    public Object getObject(Object receiver, long offset, boolean condition, Object locationIdentity) {
069        return unsafeGetObject(receiver, offset, condition, locationIdentity);
070    }
071
072    public void putBoolean(Object receiver, long offset, boolean value, Object locationIdentity) {
073        unsafePutBoolean(receiver, offset, value, locationIdentity);
074    }
075
076    public void putByte(Object receiver, long offset, byte value, Object locationIdentity) {
077        unsafePutByte(receiver, offset, value, locationIdentity);
078    }
079
080    public void putShort(Object receiver, long offset, short value, Object locationIdentity) {
081        unsafePutShort(receiver, offset, value, locationIdentity);
082    }
083
084    public void putInt(Object receiver, long offset, int value, Object locationIdentity) {
085        unsafePutInt(receiver, offset, value, locationIdentity);
086    }
087
088    public void putLong(Object receiver, long offset, long value, Object locationIdentity) {
089        unsafePutLong(receiver, offset, value, locationIdentity);
090    }
091
092    public void putFloat(Object receiver, long offset, float value, Object locationIdentity) {
093        unsafePutFloat(receiver, offset, value, locationIdentity);
094    }
095
096    public void putDouble(Object receiver, long offset, double value, Object locationIdentity) {
097        unsafePutDouble(receiver, offset, value, locationIdentity);
098    }
099
100    public void putObject(Object receiver, long offset, Object value, Object locationIdentity) {
101        unsafePutObject(receiver, offset, value, locationIdentity);
102    }
103
104    @SuppressWarnings("unchecked")
105    private static <T> T unsafeCast(Object value, Class<T> type, boolean condition, boolean nonNull) {
106        return (T) value;
107    }
108
109    private static boolean unsafeGetBoolean(Object receiver, long offset, boolean condition, Object locationIdentity) {
110        return UNSAFE.getBoolean(receiver, offset);
111    }
112
113    private static byte unsafeGetByte(Object receiver, long offset, boolean condition, Object locationIdentity) {
114        return UNSAFE.getByte(receiver, offset);
115    }
116
117    private static short unsafeGetShort(Object receiver, long offset, boolean condition, Object locationIdentity) {
118        return UNSAFE.getShort(receiver, offset);
119    }
120
121    private static int unsafeGetInt(Object receiver, long offset, boolean condition, Object locationIdentity) {
122        return UNSAFE.getInt(receiver, offset);
123    }
124
125    private static long unsafeGetLong(Object receiver, long offset, boolean condition, Object locationIdentity) {
126        return UNSAFE.getLong(receiver, offset);
127    }
128
129    private static float unsafeGetFloat(Object receiver, long offset, boolean condition, Object locationIdentity) {
130        return UNSAFE.getFloat(receiver, offset);
131    }
132
133    private static double unsafeGetDouble(Object receiver, long offset, boolean condition, Object locationIdentity) {
134        return UNSAFE.getDouble(receiver, offset);
135    }
136
137    private static Object unsafeGetObject(Object receiver, long offset, boolean condition, Object locationIdentity) {
138        return UNSAFE.getObject(receiver, offset);
139    }
140
141    private static void unsafePutBoolean(Object receiver, long offset, boolean value, Object locationIdentity) {
142        UNSAFE.putBoolean(receiver, offset, value);
143    }
144
145    private static void unsafePutByte(Object receiver, long offset, byte value, Object locationIdentity) {
146        UNSAFE.putByte(receiver, offset, value);
147    }
148
149    private static void unsafePutShort(Object receiver, long offset, short value, Object locationIdentity) {
150        UNSAFE.putShort(receiver, offset, value);
151    }
152
153    private static void unsafePutInt(Object receiver, long offset, int value, Object locationIdentity) {
154        UNSAFE.putInt(receiver, offset, value);
155    }
156
157    private static void unsafePutLong(Object receiver, long offset, long value, Object locationIdentity) {
158        UNSAFE.putLong(receiver, offset, value);
159    }
160
161    private static void unsafePutFloat(Object receiver, long offset, float value, Object locationIdentity) {
162        UNSAFE.putFloat(receiver, offset, value);
163    }
164
165    private static void unsafePutDouble(Object receiver, long offset, double value, Object locationIdentity) {
166        UNSAFE.putDouble(receiver, offset, value);
167    }
168
169    private static void unsafePutObject(Object receiver, long offset, Object value, Object locationIdentity) {
170        UNSAFE.putObject(receiver, offset, value);
171    }
172
173    private static final Unsafe UNSAFE = getUnsafe();
174
175    private static Unsafe getUnsafe() {
176        try {
177            return Unsafe.getUnsafe();
178        } catch (SecurityException e) {
179        }
180        try {
181            Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
182            theUnsafeInstance.setAccessible(true);
183            return (Unsafe) theUnsafeInstance.get(Unsafe.class);
184        } catch (Exception e) {
185            throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
186        }
187    }
188}