Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.jvmci.common/src/com/oracle/jvmci/common/UnsafeAccess.java @ 21541:5e868236654f
moved UnsafeAccess to com.oracle.jvmci.common (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 25 May 2015 22:17:10 +0200 |
parents | graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/UnsafeAccess.java@f137f1974f60 |
children | 5b9adb645217 |
comparison
equal
deleted
inserted
replaced
21540:ccb1b1391192 | 21541:5e868236654f |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.jvmci.common; | |
24 | |
25 import java.lang.reflect.*; | |
26 | |
27 import sun.misc.*; | |
28 | |
29 public class UnsafeAccess { | |
30 | |
31 /** | |
32 * An instance of {@link Unsafe} for use within Graal. | |
33 */ | |
34 public static final Unsafe unsafe; | |
35 | |
36 static { | |
37 try { | |
38 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe"); | |
39 theUnsafeInstance.setAccessible(true); | |
40 unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class); | |
41 } catch (Exception e) { | |
42 throw new RuntimeException("exception while trying to get Unsafe", e); | |
43 } | |
44 } | |
45 | |
46 /** | |
47 * Copies the contents of a {@link String} to a native memory buffer as a {@code '\0'} | |
48 * terminated C string. The native memory buffer is allocated via | |
49 * {@link Unsafe#allocateMemory(long)}. The caller is responsible for releasing the buffer when | |
50 * it is no longer needed via {@link Unsafe#freeMemory(long)}. | |
51 * | |
52 * @return the native memory pointer of the C string created from {@code s} | |
53 */ | |
54 public static long createCString(String s) { | |
55 return writeCString(s, unsafe.allocateMemory(s.length() + 1)); | |
56 } | |
57 | |
58 /** | |
59 * Reads a {@code '\0'} terminated C string from native memory and converts it to a | |
60 * {@link String}. | |
61 * | |
62 * @return a Java string | |
63 */ | |
64 public static String readCString(long address) { | |
65 if (address == 0) { | |
66 return null; | |
67 } | |
68 StringBuilder sb = new StringBuilder(); | |
69 for (int i = 0;; i++) { | |
70 char c = (char) unsafe.getByte(address + i); | |
71 if (c == 0) { | |
72 break; | |
73 } | |
74 sb.append(c); | |
75 } | |
76 return sb.toString(); | |
77 } | |
78 | |
79 /** | |
80 * Writes the contents of a {@link String} to a native memory buffer as a {@code '\0'} | |
81 * terminated C string. The caller is responsible for ensuring the buffer is at least | |
82 * {@code s.length() + 1} bytes long. The caller is also responsible for releasing the buffer | |
83 * when it is no longer. | |
84 * | |
85 * @return the value of {@code buf} | |
86 */ | |
87 public static long writeCString(String s, long buf) { | |
88 int size = s.length(); | |
89 for (int i = 0; i < size; i++) { | |
90 unsafe.putByte(buf + i, (byte) s.charAt(i)); | |
91 } | |
92 unsafe.putByte(buf + size, (byte) '\0'); | |
93 return buf; | |
94 } | |
95 } |