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 jdk.vm.ci.common;
  24 
  25 import sun.misc.Unsafe;
  26 
  27 /**
  28  * Utilities for operating on raw memory with {@link Unsafe}.
  29  */
  30 public class UnsafeUtil {
  31 
  32     /**
  33      * Copies the contents of a {@link String} to a native memory buffer as a {@code '\0'}
  34      * terminated C string. The native memory buffer is allocated via
  35      * {@link Unsafe#allocateMemory(long)}. The caller is responsible for releasing the buffer when
  36      * it is no longer needed via {@link Unsafe#freeMemory(long)}.
  37      *
  38      * @return the native memory pointer of the C string created from {@code s}
  39      */
  40     public static long createCString(Unsafe unsafe, String s) {
  41         return writeCString(unsafe, s, unsafe.allocateMemory(s.length() + 1));
  42     }
  43 
  44     /**
  45      * Reads a {@code '\0'} terminated C string from native memory and converts it to a
  46      * {@link String}.
  47      *
  48      * @return a Java string
  49      */
  50     public static String readCString(Unsafe unsafe, long address) {
  51         if (address == 0) {
  52             return null;
  53         }
  54         StringBuilder sb = new StringBuilder();
  55         for (int i = 0;; i++) {
  56             char c = (char) unsafe.getByte(address + i);
  57             if (c == 0) {
  58                 break;
  59             }
  60             sb.append(c);
  61         }
  62         return sb.toString();
  63     }
  64 
  65     /**
  66      * Writes the contents of a {@link String} to a native memory buffer as a {@code '\0'}
  67      * terminated C string. The caller is responsible for ensuring the buffer is at least
  68      * {@code s.length() + 1} bytes long. The caller is also responsible for releasing the buffer
  69      * when it is no longer.
  70      *
  71      * @return the value of {@code buf}
  72      */
  73     public static long writeCString(Unsafe unsafe, String s, long buf) {
  74         int size = s.length();
  75         for (int i = 0; i < size; i++) {
  76             unsafe.putByte(buf + i, (byte) s.charAt(i));
  77         }
  78         unsafe.putByte(buf + size, (byte) '\0');
  79         return buf;
  80     }
  81 }