Mercurial > hg > graal-jvmci-8
annotate jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/UnsafeAccess.java @ 23677:eb166b568645
Merge
author | Tom Rodriguez <tom.rodriguez@oracle.com> |
---|---|
date | Thu, 09 Jun 2016 18:58:09 -0700 |
parents | 1bbd4a7c274b |
children |
rev | line source |
---|---|
22541
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
1 /* |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
4 * |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
8 * |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
14 * |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
18 * |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
21 * questions. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
22 */ |
22672
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22542
diff
changeset
|
23 package jdk.vm.ci.hotspot; |
22541
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
24 |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
25 import java.lang.reflect.Field; |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
26 |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
27 import sun.misc.Unsafe; |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
28 |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
29 /** |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
30 * Package private access to the {@link Unsafe} capability. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
31 */ |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
32 class UnsafeAccess { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
33 |
22542
15579668ec3a
renamed constant field to UNSAFE
Doug Simon <doug.simon@oracle.com>
parents:
22541
diff
changeset
|
34 static final Unsafe UNSAFE = initUnsafe(); |
22541
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
35 |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
36 private static Unsafe initUnsafe() { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
37 try { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
38 // Fast path when we are trusted. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
39 return Unsafe.getUnsafe(); |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
40 } catch (SecurityException se) { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
41 // Slow path when we are not trusted. |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
42 try { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
43 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
44 theUnsafe.setAccessible(true); |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
45 return (Unsafe) theUnsafe.get(Unsafe.class); |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
46 } catch (Exception e) { |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
47 throw new RuntimeException("exception while trying to get Unsafe", e); |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
48 } |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
49 } |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
50 } |
d2035d841611
restricted Unsafe capability access to package or class scope
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
51 } |