Mercurial > hg > graal-compiler
annotate graal/com.oracle.jvmci.common/src/com/oracle/jvmci/common/UnsafeAccess.java @ 21591:6c5327245831
LinearScan: fix intervals context.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Fri, 29 May 2015 10:59:03 +0200 |
parents | 5e868236654f |
children | 5b9adb645217 |
rev | line source |
---|---|
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
1 /* |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
4 * |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
8 * |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
14 * |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
18 * |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
21 * questions. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
22 */ |
21541
5e868236654f
moved UnsafeAccess to com.oracle.jvmci.common (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
20040
diff
changeset
|
23 package com.oracle.jvmci.common; |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
24 |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 import java.lang.reflect.*; |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
27 import sun.misc.*; |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
28 |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
29 public class UnsafeAccess { |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
30 |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
31 /** |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
32 * An instance of {@link Unsafe} for use within Graal. |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
33 */ |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
34 public static final Unsafe unsafe; |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
35 |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
36 static { |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
37 try { |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
38 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe"); |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
39 theUnsafeInstance.setAccessible(true); |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
40 unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class); |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
41 } catch (Exception e) { |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
42 throw new RuntimeException("exception while trying to get Unsafe", e); |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
43 } |
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 } |
13894
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
45 |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
46 /** |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
47 * Copies the contents of a {@link String} to a native memory buffer as a {@code '\0'} |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
48 * terminated C string. The native memory buffer is allocated via |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
49 * {@link Unsafe#allocateMemory(long)}. The caller is responsible for releasing the buffer when |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
50 * it is no longer needed via {@link Unsafe#freeMemory(long)}. |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
51 * |
13894
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
52 * @return the native memory pointer of the C string created from {@code s} |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
53 */ |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
54 public static long createCString(String s) { |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
55 return writeCString(s, unsafe.allocateMemory(s.length() + 1)); |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
56 } |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
57 |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
58 /** |
14051
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
59 * Reads a {@code '\0'} terminated C string from native memory and converts it to a |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
60 * {@link String}. |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
61 * |
14051
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
62 * @return a Java string |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
63 */ |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
64 public static String readCString(long address) { |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
65 if (address == 0) { |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
66 return null; |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
67 } |
20040
f137f1974f60
removed uses of StringBuffer, Hashtable and Stack
Doug Simon <doug.simon@oracle.com>
parents:
16626
diff
changeset
|
68 StringBuilder sb = new StringBuilder(); |
14051
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
69 for (int i = 0;; i++) { |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
70 char c = (char) unsafe.getByte(address + i); |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
71 if (c == 0) { |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
72 break; |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
73 } |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
74 sb.append(c); |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
75 } |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
76 return sb.toString(); |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
77 } |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
78 |
e85575f63b60
moved readCString to UnsafeAccess next to the other methods
twisti
parents:
13894
diff
changeset
|
79 /** |
13894
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
80 * Writes the contents of a {@link String} to a native memory buffer as a {@code '\0'} |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
81 * terminated C string. The caller is responsible for ensuring the buffer is at least |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
82 * {@code s.length() + 1} bytes long. The caller is also responsible for releasing the buffer |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
83 * when it is no longer. |
16626
7ce626e1952c
removed direct use of Unsafe.getUnsafe() since graal.jar is no longer on boot class path and so reflection method of accessed Unsafe must be used
Doug Simon <doug.simon@oracle.com>
parents:
15291
diff
changeset
|
84 * |
13894
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
85 * @return the value of {@code buf} |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
86 */ |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
87 public static long writeCString(String s, long buf) { |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
88 int size = s.length(); |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
89 for (int i = 0; i < size; i++) { |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
90 unsafe.putByte(buf + i, (byte) s.charAt(i)); |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
91 } |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
92 unsafe.putByte(buf + size, (byte) '\0'); |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
93 return buf; |
4fa77c58ad8f
added utility methods for writing a Java string to a native memory buffer as a C string
Doug Simon <doug.simon@oracle.com>
parents:
7300
diff
changeset
|
94 } |
7300
2912b72d840a
More complete and reusable Word type
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
95 } |