UnsafeAccess.java revision 12651:6ef01bd40ce2
1198893Srdivacky/* 2198893Srdivacky * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. 3198893Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4198893Srdivacky * 5198893Srdivacky * This code is free software; you can redistribute it and/or modify it 6198893Srdivacky * under the terms of the GNU General Public License version 2 only, as 7198893Srdivacky * published by the Free Software Foundation. 8198893Srdivacky * 9198893Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT 10198893Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11198893Srdivacky * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12198893Srdivacky * version 2 for more details (a copy is included in the LICENSE file that 13198893Srdivacky * accompanied this code). 14198893Srdivacky * 15198893Srdivacky * You should have received a copy of the GNU General Public License version 16218893Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17198893Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18199990Srdivacky * 19198893Srdivacky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20218893Sdim * or visit www.oracle.com if you need additional information or have any 21218893Sdim * questions. 22198893Srdivacky */ 23208600Srdivackypackage org.graalvm.compiler.core.common; 24218893Sdim 25234353Sdimimport java.lang.reflect.Field; 26249423Sdim 27218893Sdimimport sun.misc.Unsafe; 28198893Srdivacky 29198893Srdivacky/** 30198893Srdivacky * Package private access to the {@link Unsafe} capability. 31218893Sdim */ 32218893Sdimclass UnsafeAccess { 33218893Sdim 34218893Sdim static final Unsafe UNSAFE = initUnsafe(); 35218893Sdim 36280031Sdim private static Unsafe initUnsafe() { 37280031Sdim try { 38218893Sdim // Fast path when we are trusted. 39280031Sdim return Unsafe.getUnsafe(); 40218893Sdim } catch (SecurityException se) { 41239462Sdim // Slow path when we are not trusted. 42218893Sdim try { 43280031Sdim Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); 44280031Sdim theUnsafe.setAccessible(true); 45280031Sdim return (Unsafe) theUnsafe.get(Unsafe.class); 46280031Sdim } catch (Exception e) { 47280031Sdim throw new RuntimeException("exception while trying to get Unsafe", e); 48280031Sdim } 49280031Sdim } 50280031Sdim } 51280031Sdim} 52280031Sdim