001/*
002 * Copyright (c) 2012, 2012, 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 jdk.internal.jvmci.common;
024
025import java.lang.reflect.*;
026
027import sun.misc.*;
028
029public class UnsafeAccess {
030
031    /**
032     * An instance of {@link Unsafe} for use within JVMCI.
033     */
034    public static final Unsafe unsafe;
035
036    static {
037        try {
038            Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
039            theUnsafeInstance.setAccessible(true);
040            unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
041        } catch (Exception e) {
042            throw new RuntimeException("exception while trying to get Unsafe", e);
043        }
044    }
045
046    /**
047     * Copies the contents of a {@link String} to a native memory buffer as a {@code '\0'}
048     * terminated C string. The native memory buffer is allocated via
049     * {@link Unsafe#allocateMemory(long)}. The caller is responsible for releasing the buffer when
050     * it is no longer needed via {@link Unsafe#freeMemory(long)}.
051     *
052     * @return the native memory pointer of the C string created from {@code s}
053     */
054    public static long createCString(String s) {
055        return writeCString(s, unsafe.allocateMemory(s.length() + 1));
056    }
057
058    /**
059     * Reads a {@code '\0'} terminated C string from native memory and converts it to a
060     * {@link String}.
061     *
062     * @return a Java string
063     */
064    public static String readCString(long address) {
065        if (address == 0) {
066            return null;
067        }
068        StringBuilder sb = new StringBuilder();
069        for (int i = 0;; i++) {
070            char c = (char) unsafe.getByte(address + i);
071            if (c == 0) {
072                break;
073            }
074            sb.append(c);
075        }
076        return sb.toString();
077    }
078
079    /**
080     * Writes the contents of a {@link String} to a native memory buffer as a {@code '\0'}
081     * terminated C string. The caller is responsible for ensuring the buffer is at least
082     * {@code s.length() + 1} bytes long. The caller is also responsible for releasing the buffer
083     * when it is no longer.
084     *
085     * @return the value of {@code buf}
086     */
087    public static long writeCString(String s, long buf) {
088        int size = s.length();
089        for (int i = 0; i < size; i++) {
090            unsafe.putByte(buf + i, (byte) s.charAt(i));
091        }
092        unsafe.putByte(buf + size, (byte) '\0');
093        return buf;
094    }
095}