< prev index next >

jdk/src/java.base/share/classes/jdk/internal/misc/Unsafe.java

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import java.lang.reflect.Field;
  29 import java.security.ProtectionDomain;
  30 
  31 import sun.reflect.CallerSensitive;
  32 import sun.reflect.Reflection;
  33 import jdk.internal.misc.VM;
  34 
  35 import jdk.internal.HotSpotIntrinsicCandidate;
  36 
  37 
  38 /**
  39  * A collection of methods for performing low-level, unsafe operations.
  40  * Although the class and all methods are public, use of this class is
  41  * limited because only trusted code can obtain instances of it.
  42  *
  43  * @author John R. Rose
  44  * @see #getUnsafe
  45  */
  46 
  47 public final class Unsafe {
  48 
  49     private static native void registerNatives();
  50     static {
  51         registerNatives();
  52         sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  53     }


1064      * in memory is raw data, and need not correspond to any Java
1065      * variable.  Unless <code>o</code> is null, the value accessed
1066      * must be entirely within the allocated object.  The endianness
1067      * of the value in memory is the endianness of the native platform.
1068      *
1069      * <p> The read will be atomic with respect to the largest power
1070      * of two that divides the GCD of the offset and the storage size.
1071      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
1072      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1073      * respectively.  There are no other guarantees of atomicity.
1074      * <p>
1075      * 8-byte atomicity is only guaranteed on platforms on which
1076      * support atomic accesses to longs.
1077      *
1078      * @param o Java heap object in which the value resides, if any, else
1079      *        null
1080      * @param offset The offset in bytes from the start of the object
1081      * @return the value fetched from the indicated object
1082      * @throws RuntimeException No defined exceptions are thrown, not even
1083      *         {@link NullPointerException}
1084      * @since 1.9
1085      */
1086     @HotSpotIntrinsicCandidate
1087     public final long getLongUnaligned(Object o, long offset) {
1088         if ((offset & 7) == 0) {
1089             return getLong(o, offset);
1090         } else if ((offset & 3) == 0) {
1091             return makeLong(getInt(o, offset),
1092                             getInt(o, offset + 4));
1093         } else if ((offset & 1) == 0) {
1094             return makeLong(getShort(o, offset),
1095                             getShort(o, offset + 2),
1096                             getShort(o, offset + 4),
1097                             getShort(o, offset + 6));
1098         } else {
1099             return makeLong(getByte(o, offset),
1100                             getByte(o, offset + 1),
1101                             getByte(o, offset + 2),
1102                             getByte(o, offset + 3),
1103                             getByte(o, offset + 4),
1104                             getByte(o, offset + 5),
1105                             getByte(o, offset + 6),
1106                             getByte(o, offset + 7));
1107         }
1108     }
1109     /**
1110      * As {@link #getLongUnaligned(Object, long)} but with an
1111      * additional argument which specifies the endianness of the value
1112      * as stored in memory.
1113      *
1114      * @param o Java heap object in which the variable resides
1115      * @param offset The offset in bytes from the start of the object
1116      * @param bigEndian The endianness of the value
1117      * @return the value fetched from the indicated object
1118      * @since 1.9
1119      */
1120     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
1121         return convEndian(bigEndian, getLongUnaligned(o, offset));
1122     }
1123 
1124     /** @see #getLongUnaligned(Object, long) */
1125     @HotSpotIntrinsicCandidate
1126     public final int getIntUnaligned(Object o, long offset) {
1127         if ((offset & 3) == 0) {
1128             return getInt(o, offset);
1129         } else if ((offset & 1) == 0) {
1130             return makeInt(getShort(o, offset),
1131                            getShort(o, offset + 2));
1132         } else {
1133             return makeInt(getByte(o, offset),
1134                            getByte(o, offset + 1),
1135                            getByte(o, offset + 2),
1136                            getByte(o, offset + 3));
1137         }
1138     }


1176      * {@link java.lang.reflect.Field} of some Java field.  The value
1177      * in memory is raw data, and need not correspond to any Java
1178      * variable.  The endianness of the value in memory is the
1179      * endianness of the native platform.
1180      * <p>
1181      * The write will be atomic with respect to the largest power of
1182      * two that divides the GCD of the offset and the storage size.
1183      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
1184      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1185      * respectively.  There are no other guarantees of atomicity.
1186      * <p>
1187      * 8-byte atomicity is only guaranteed on platforms on which
1188      * support atomic accesses to longs.
1189      *
1190      * @param o Java heap object in which the value resides, if any, else
1191      *        null
1192      * @param offset The offset in bytes from the start of the object
1193      * @param x the value to store
1194      * @throws RuntimeException No defined exceptions are thrown, not even
1195      *         {@link NullPointerException}
1196      * @since 1.9
1197      */
1198     @HotSpotIntrinsicCandidate
1199     public final void putLongUnaligned(Object o, long offset, long x) {
1200         if ((offset & 7) == 0) {
1201             putLong(o, offset, x);
1202         } else if ((offset & 3) == 0) {
1203             putLongParts(o, offset,
1204                          (int)(x >> 0),
1205                          (int)(x >>> 32));
1206         } else if ((offset & 1) == 0) {
1207             putLongParts(o, offset,
1208                          (short)(x >>> 0),
1209                          (short)(x >>> 16),
1210                          (short)(x >>> 32),
1211                          (short)(x >>> 48));
1212         } else {
1213             putLongParts(o, offset,
1214                          (byte)(x >>> 0),
1215                          (byte)(x >>> 8),
1216                          (byte)(x >>> 16),
1217                          (byte)(x >>> 24),
1218                          (byte)(x >>> 32),
1219                          (byte)(x >>> 40),
1220                          (byte)(x >>> 48),
1221                          (byte)(x >>> 56));
1222         }
1223     }
1224 
1225     /**
1226      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
1227      * argument which specifies the endianness of the value as stored in memory.
1228      * @param o Java heap object in which the value resides
1229      * @param offset The offset in bytes from the start of the object
1230      * @param x the value to store
1231      * @param bigEndian The endianness of the value
1232      * @throws RuntimeException No defined exceptions are thrown, not even
1233      *         {@link NullPointerException}
1234      * @since 1.9
1235      */
1236     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
1237         putLongUnaligned(o, offset, convEndian(bigEndian, x));
1238     }
1239 
1240     /** @see #putLongUnaligned(Object, long, long) */
1241     @HotSpotIntrinsicCandidate
1242     public final void putIntUnaligned(Object o, long offset, int x) {
1243         if ((offset & 3) == 0) {
1244             putInt(o, offset, x);
1245         } else if ((offset & 1) == 0) {
1246             putIntParts(o, offset,
1247                         (short)(x >> 0),
1248                         (short)(x >>> 16));
1249         } else {
1250             putIntParts(o, offset,
1251                         (byte)(x >>> 0),
1252                         (byte)(x >>> 8),
1253                         (byte)(x >>> 16),
1254                         (byte)(x >>> 24));




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import java.lang.reflect.Field;
  29 import java.security.ProtectionDomain;
  30 
  31 import sun.reflect.CallerSensitive;
  32 import sun.reflect.Reflection;
  33 import sun.misc.VM;
  34 
  35 import jdk.internal.HotSpotIntrinsicCandidate;
  36 
  37 
  38 /**
  39  * A collection of methods for performing low-level, unsafe operations.
  40  * Although the class and all methods are public, use of this class is
  41  * limited because only trusted code can obtain instances of it.
  42  *
  43  * @author John R. Rose
  44  * @see #getUnsafe
  45  */
  46 
  47 public final class Unsafe {
  48 
  49     private static native void registerNatives();
  50     static {
  51         registerNatives();
  52         sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  53     }


1064      * in memory is raw data, and need not correspond to any Java
1065      * variable.  Unless <code>o</code> is null, the value accessed
1066      * must be entirely within the allocated object.  The endianness
1067      * of the value in memory is the endianness of the native platform.
1068      *
1069      * <p> The read will be atomic with respect to the largest power
1070      * of two that divides the GCD of the offset and the storage size.
1071      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
1072      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1073      * respectively.  There are no other guarantees of atomicity.
1074      * <p>
1075      * 8-byte atomicity is only guaranteed on platforms on which
1076      * support atomic accesses to longs.
1077      *
1078      * @param o Java heap object in which the value resides, if any, else
1079      *        null
1080      * @param offset The offset in bytes from the start of the object
1081      * @return the value fetched from the indicated object
1082      * @throws RuntimeException No defined exceptions are thrown, not even
1083      *         {@link NullPointerException}
1084      * @since 9
1085      */
1086     @HotSpotIntrinsicCandidate
1087     public final long getLongUnaligned(Object o, long offset) {
1088         if ((offset & 7) == 0) {
1089             return getLong(o, offset);
1090         } else if ((offset & 3) == 0) {
1091             return makeLong(getInt(o, offset),
1092                             getInt(o, offset + 4));
1093         } else if ((offset & 1) == 0) {
1094             return makeLong(getShort(o, offset),
1095                             getShort(o, offset + 2),
1096                             getShort(o, offset + 4),
1097                             getShort(o, offset + 6));
1098         } else {
1099             return makeLong(getByte(o, offset),
1100                             getByte(o, offset + 1),
1101                             getByte(o, offset + 2),
1102                             getByte(o, offset + 3),
1103                             getByte(o, offset + 4),
1104                             getByte(o, offset + 5),
1105                             getByte(o, offset + 6),
1106                             getByte(o, offset + 7));
1107         }
1108     }
1109     /**
1110      * As {@link #getLongUnaligned(Object, long)} but with an
1111      * additional argument which specifies the endianness of the value
1112      * as stored in memory.
1113      *
1114      * @param o Java heap object in which the variable resides
1115      * @param offset The offset in bytes from the start of the object
1116      * @param bigEndian The endianness of the value
1117      * @return the value fetched from the indicated object
1118      * @since 9
1119      */
1120     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
1121         return convEndian(bigEndian, getLongUnaligned(o, offset));
1122     }
1123 
1124     /** @see #getLongUnaligned(Object, long) */
1125     @HotSpotIntrinsicCandidate
1126     public final int getIntUnaligned(Object o, long offset) {
1127         if ((offset & 3) == 0) {
1128             return getInt(o, offset);
1129         } else if ((offset & 1) == 0) {
1130             return makeInt(getShort(o, offset),
1131                            getShort(o, offset + 2));
1132         } else {
1133             return makeInt(getByte(o, offset),
1134                            getByte(o, offset + 1),
1135                            getByte(o, offset + 2),
1136                            getByte(o, offset + 3));
1137         }
1138     }


1176      * {@link java.lang.reflect.Field} of some Java field.  The value
1177      * in memory is raw data, and need not correspond to any Java
1178      * variable.  The endianness of the value in memory is the
1179      * endianness of the native platform.
1180      * <p>
1181      * The write will be atomic with respect to the largest power of
1182      * two that divides the GCD of the offset and the storage size.
1183      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
1184      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1185      * respectively.  There are no other guarantees of atomicity.
1186      * <p>
1187      * 8-byte atomicity is only guaranteed on platforms on which
1188      * support atomic accesses to longs.
1189      *
1190      * @param o Java heap object in which the value resides, if any, else
1191      *        null
1192      * @param offset The offset in bytes from the start of the object
1193      * @param x the value to store
1194      * @throws RuntimeException No defined exceptions are thrown, not even
1195      *         {@link NullPointerException}
1196      * @since 9
1197      */
1198     @HotSpotIntrinsicCandidate
1199     public final void putLongUnaligned(Object o, long offset, long x) {
1200         if ((offset & 7) == 0) {
1201             putLong(o, offset, x);
1202         } else if ((offset & 3) == 0) {
1203             putLongParts(o, offset,
1204                          (int)(x >> 0),
1205                          (int)(x >>> 32));
1206         } else if ((offset & 1) == 0) {
1207             putLongParts(o, offset,
1208                          (short)(x >>> 0),
1209                          (short)(x >>> 16),
1210                          (short)(x >>> 32),
1211                          (short)(x >>> 48));
1212         } else {
1213             putLongParts(o, offset,
1214                          (byte)(x >>> 0),
1215                          (byte)(x >>> 8),
1216                          (byte)(x >>> 16),
1217                          (byte)(x >>> 24),
1218                          (byte)(x >>> 32),
1219                          (byte)(x >>> 40),
1220                          (byte)(x >>> 48),
1221                          (byte)(x >>> 56));
1222         }
1223     }
1224 
1225     /**
1226      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
1227      * argument which specifies the endianness of the value as stored in memory.
1228      * @param o Java heap object in which the value resides
1229      * @param offset The offset in bytes from the start of the object
1230      * @param x the value to store
1231      * @param bigEndian The endianness of the value
1232      * @throws RuntimeException No defined exceptions are thrown, not even
1233      *         {@link NullPointerException}
1234      * @since 9
1235      */
1236     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
1237         putLongUnaligned(o, offset, convEndian(bigEndian, x));
1238     }
1239 
1240     /** @see #putLongUnaligned(Object, long, long) */
1241     @HotSpotIntrinsicCandidate
1242     public final void putIntUnaligned(Object o, long offset, int x) {
1243         if ((offset & 3) == 0) {
1244             putInt(o, offset, x);
1245         } else if ((offset & 1) == 0) {
1246             putIntParts(o, offset,
1247                         (short)(x >> 0),
1248                         (short)(x >>> 16));
1249         } else {
1250             putIntParts(o, offset,
1251                         (byte)(x >>> 0),
1252                         (byte)(x >>> 8),
1253                         (byte)(x >>> 16),
1254                         (byte)(x >>> 24));


< prev index next >