1 /*
   2  * Copyright (c) 2000, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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     }
  54 
  55     private Unsafe() {}
  56 
  57     private static final Unsafe theUnsafe = new Unsafe();
  58 
  59     /**
  60      * Provides the caller with the capability of performing unsafe
  61      * operations.
  62      *
  63      * <p>The returned {@code Unsafe} object should be carefully guarded
  64      * by the caller, since it can be used to read and write data at arbitrary
  65      * memory addresses.  It must never be passed to untrusted code.
  66      *
  67      * <p>Most methods in this class are very low-level, and correspond to a
  68      * small number of hardware instructions (on typical machines).  Compilers
  69      * are encouraged to optimize these methods accordingly.
  70      *
  71      * <p>Here is a suggested idiom for using unsafe operations:
  72      *
  73      * <pre> {@code
  74      * class MyTrustedClass {
  75      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  76      *   ...
  77      *   private long myCountAddress = ...;
  78      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  79      * }}</pre>
  80      *
  81      * (It may assist compilers to make the local variable {@code final}.)
  82      *
  83      * @throws  SecurityException  if a security manager exists and its
  84      *          {@code checkPropertiesAccess} method doesn't allow
  85      *          access to the system properties.
  86      */
  87     @CallerSensitive
  88     public static Unsafe getUnsafe() {
  89         Class<?> caller = Reflection.getCallerClass();
  90         if (!VM.isSystemDomainLoader(caller.getClassLoader()))
  91             throw new SecurityException("Unsafe");
  92         return theUnsafe;
  93     }
  94 
  95     /// peek and poke operations
  96     /// (compilers should optimize these to memory ops)
  97 
  98     // These work on object fields in the Java heap.
  99     // They will not work on elements of packed arrays.
 100 
 101     /**
 102      * Fetches a value from a given Java variable.
 103      * More specifically, fetches a field or array element within the given
 104      * object {@code o} at the given offset, or (if {@code o} is null)
 105      * from the memory address whose numerical value is the given offset.
 106      * <p>
 107      * The results are undefined unless one of the following cases is true:
 108      * <ul>
 109      * <li>The offset was obtained from {@link #objectFieldOffset} on
 110      * the {@link java.lang.reflect.Field} of some Java field and the object
 111      * referred to by {@code o} is of a class compatible with that
 112      * field's class.
 113      *
 114      * <li>The offset and object reference {@code o} (either null or
 115      * non-null) were both obtained via {@link #staticFieldOffset}
 116      * and {@link #staticFieldBase} (respectively) from the
 117      * reflective {@link Field} representation of some Java field.
 118      *
 119      * <li>The object referred to by {@code o} is an array, and the offset
 120      * is an integer of the form {@code B+N*S}, where {@code N} is
 121      * a valid index into the array, and {@code B} and {@code S} are
 122      * the values obtained by {@link #arrayBaseOffset} and {@link
 123      * #arrayIndexScale} (respectively) from the array's class.  The value
 124      * referred to is the {@code N}<em>th</em> element of the array.
 125      *
 126      * </ul>
 127      * <p>
 128      * If one of the above cases is true, the call references a specific Java
 129      * variable (field or array element).  However, the results are undefined
 130      * if that variable is not in fact of the type returned by this method.
 131      * <p>
 132      * This method refers to a variable by means of two parameters, and so
 133      * it provides (in effect) a <em>double-register</em> addressing mode
 134      * for Java variables.  When the object reference is null, this method
 135      * uses its offset as an absolute address.  This is similar in operation
 136      * to methods such as {@link #getInt(long)}, which provide (in effect) a
 137      * <em>single-register</em> addressing mode for non-Java variables.
 138      * However, because Java variables may have a different layout in memory
 139      * from non-Java variables, programmers should not assume that these
 140      * two addressing modes are ever equivalent.  Also, programmers should
 141      * remember that offsets from the double-register addressing mode cannot
 142      * be portably confused with longs used in the single-register addressing
 143      * mode.
 144      *
 145      * @param o Java heap object in which the variable resides, if any, else
 146      *        null
 147      * @param offset indication of where the variable resides in a Java heap
 148      *        object, if any, else a memory address locating the variable
 149      *        statically
 150      * @return the value fetched from the indicated Java variable
 151      * @throws RuntimeException No defined exceptions are thrown, not even
 152      *         {@link NullPointerException}
 153      */
 154     @HotSpotIntrinsicCandidate
 155     public native int getInt(Object o, long offset);
 156 
 157     /**
 158      * Stores a value into a given Java variable.
 159      * <p>
 160      * The first two parameters are interpreted exactly as with
 161      * {@link #getInt(Object, long)} to refer to a specific
 162      * Java variable (field or array element).  The given value
 163      * is stored into that variable.
 164      * <p>
 165      * The variable must be of the same type as the method
 166      * parameter {@code x}.
 167      *
 168      * @param o Java heap object in which the variable resides, if any, else
 169      *        null
 170      * @param offset indication of where the variable resides in a Java heap
 171      *        object, if any, else a memory address locating the variable
 172      *        statically
 173      * @param x the value to store into the indicated Java variable
 174      * @throws RuntimeException No defined exceptions are thrown, not even
 175      *         {@link NullPointerException}
 176      */
 177     @HotSpotIntrinsicCandidate
 178     public native void putInt(Object o, long offset, int x);
 179 
 180     /**
 181      * Fetches a reference value from a given Java variable.
 182      * @see #getInt(Object, long)
 183      */
 184     @HotSpotIntrinsicCandidate
 185     public native Object getObject(Object o, long offset);
 186 
 187     /**
 188      * Stores a reference value into a given Java variable.
 189      * <p>
 190      * Unless the reference {@code x} being stored is either null
 191      * or matches the field type, the results are undefined.
 192      * If the reference {@code o} is non-null, card marks or
 193      * other store barriers for that object (if the VM requires them)
 194      * are updated.
 195      * @see #putInt(Object, long, int)
 196      */
 197     @HotSpotIntrinsicCandidate
 198     public native void putObject(Object o, long offset, Object x);
 199 
 200     /** @see #getInt(Object, long) */
 201     @HotSpotIntrinsicCandidate
 202     public native boolean getBoolean(Object o, long offset);
 203     /** @see #putInt(Object, long, int) */
 204     @HotSpotIntrinsicCandidate
 205     public native void    putBoolean(Object o, long offset, boolean x);
 206     /** @see #getInt(Object, long) */
 207     @HotSpotIntrinsicCandidate
 208     public native byte    getByte(Object o, long offset);
 209     /** @see #putInt(Object, long, int) */
 210     @HotSpotIntrinsicCandidate
 211     public native void    putByte(Object o, long offset, byte x);
 212     /** @see #getInt(Object, long) */
 213     @HotSpotIntrinsicCandidate
 214     public native short   getShort(Object o, long offset);
 215     /** @see #putInt(Object, long, int) */
 216     @HotSpotIntrinsicCandidate
 217     public native void    putShort(Object o, long offset, short x);
 218     /** @see #getInt(Object, long) */
 219     @HotSpotIntrinsicCandidate
 220     public native char    getChar(Object o, long offset);
 221     /** @see #putInt(Object, long, int) */
 222     @HotSpotIntrinsicCandidate
 223     public native void    putChar(Object o, long offset, char x);
 224     /** @see #getInt(Object, long) */
 225     @HotSpotIntrinsicCandidate
 226     public native long    getLong(Object o, long offset);
 227     /** @see #putInt(Object, long, int) */
 228     @HotSpotIntrinsicCandidate
 229     public native void    putLong(Object o, long offset, long x);
 230     /** @see #getInt(Object, long) */
 231     @HotSpotIntrinsicCandidate
 232     public native float   getFloat(Object o, long offset);
 233     /** @see #putInt(Object, long, int) */
 234     @HotSpotIntrinsicCandidate
 235     public native void    putFloat(Object o, long offset, float x);
 236     /** @see #getInt(Object, long) */
 237     @HotSpotIntrinsicCandidate
 238     public native double  getDouble(Object o, long offset);
 239     /** @see #putInt(Object, long, int) */
 240     @HotSpotIntrinsicCandidate
 241     public native void    putDouble(Object o, long offset, double x);
 242 
 243     // These read VM internal data.
 244 
 245     /**
 246      * Fetches an uncompressed reference value from a given native variable
 247      * ignoring the VM's compressed references mode.
 248      *
 249      * @param address a memory address locating the variable
 250      * @return the value fetched from the indicated native variable
 251      */
 252     public native Object getUncompressedObject(long address);
 253 
 254     /**
 255      * Fetches the {@link java.lang.Class} Java mirror for the given native
 256      * metaspace {@code Klass} pointer.
 257      *
 258      * @param metaspaceKlass a native metaspace {@code Klass} pointer
 259      * @return the {@link java.lang.Class} Java mirror
 260      */
 261     public native Class<?> getJavaMirror(long metaspaceKlass);
 262 
 263     /**
 264      * Fetches a native metaspace {@code Klass} pointer for the given Java
 265      * object.
 266      *
 267      * @param o Java heap object for which to fetch the class pointer
 268      * @return a native metaspace {@code Klass} pointer
 269      */
 270     public native long getKlassPointer(Object o);
 271 
 272     // These work on values in the C heap.
 273 
 274     /**
 275      * Fetches a value from a given memory address.  If the address is zero, or
 276      * does not point into a block obtained from {@link #allocateMemory}, the
 277      * results are undefined.
 278      *
 279      * @see #allocateMemory
 280      */
 281     @HotSpotIntrinsicCandidate
 282     public native byte    getByte(long address);
 283 
 284     /**
 285      * Stores a value into a given memory address.  If the address is zero, or
 286      * does not point into a block obtained from {@link #allocateMemory}, the
 287      * results are undefined.
 288      *
 289      * @see #getByte(long)
 290      */
 291     @HotSpotIntrinsicCandidate
 292     public native void    putByte(long address, byte x);
 293 
 294     /** @see #getByte(long) */
 295     @HotSpotIntrinsicCandidate
 296     public native short   getShort(long address);
 297     /** @see #putByte(long, byte) */
 298     @HotSpotIntrinsicCandidate
 299     public native void    putShort(long address, short x);
 300     /** @see #getByte(long) */
 301     @HotSpotIntrinsicCandidate
 302     public native char    getChar(long address);
 303     /** @see #putByte(long, byte) */
 304     @HotSpotIntrinsicCandidate
 305     public native void    putChar(long address, char x);
 306     /** @see #getByte(long) */
 307     @HotSpotIntrinsicCandidate
 308     public native int     getInt(long address);
 309     /** @see #putByte(long, byte) */
 310     @HotSpotIntrinsicCandidate
 311     public native void    putInt(long address, int x);
 312     /** @see #getByte(long) */
 313     @HotSpotIntrinsicCandidate
 314     public native long    getLong(long address);
 315     /** @see #putByte(long, byte) */
 316     @HotSpotIntrinsicCandidate
 317     public native void    putLong(long address, long x);
 318     /** @see #getByte(long) */
 319     @HotSpotIntrinsicCandidate
 320     public native float   getFloat(long address);
 321     /** @see #putByte(long, byte) */
 322     @HotSpotIntrinsicCandidate
 323     public native void    putFloat(long address, float x);
 324     /** @see #getByte(long) */
 325     @HotSpotIntrinsicCandidate
 326     public native double  getDouble(long address);
 327     /** @see #putByte(long, byte) */
 328     @HotSpotIntrinsicCandidate
 329     public native void    putDouble(long address, double x);
 330 
 331     /**
 332      * Fetches a native pointer from a given memory address.  If the address is
 333      * zero, or does not point into a block obtained from {@link
 334      * #allocateMemory}, the results are undefined.
 335      *
 336      * <p>If the native pointer is less than 64 bits wide, it is extended as
 337      * an unsigned number to a Java long.  The pointer may be indexed by any
 338      * given byte offset, simply by adding that offset (as a simple integer) to
 339      * the long representing the pointer.  The number of bytes actually read
 340      * from the target address may be determined by consulting {@link
 341      * #addressSize}.
 342      *
 343      * @see #allocateMemory
 344      */
 345     @HotSpotIntrinsicCandidate
 346     public native long getAddress(long address);
 347 
 348     /**
 349      * Stores a native pointer into a given memory address.  If the address is
 350      * zero, or does not point into a block obtained from {@link
 351      * #allocateMemory}, the results are undefined.
 352      *
 353      * <p>The number of bytes actually written at the target address may be
 354      * determined by consulting {@link #addressSize}.
 355      *
 356      * @see #getAddress(long)
 357      */
 358     @HotSpotIntrinsicCandidate
 359     public native void putAddress(long address, long x);
 360 
 361     /// wrappers for malloc, realloc, free:
 362 
 363     /**
 364      * Allocates a new block of native memory, of the given size in bytes.  The
 365      * contents of the memory are uninitialized; they will generally be
 366      * garbage.  The resulting native pointer will never be zero, and will be
 367      * aligned for all value types.  Dispose of this memory by calling {@link
 368      * #freeMemory}, or resize it with {@link #reallocateMemory}.
 369      *
 370      * @throws IllegalArgumentException if the size is negative or too large
 371      *         for the native size_t type
 372      *
 373      * @throws OutOfMemoryError if the allocation is refused by the system
 374      *
 375      * @see #getByte(long)
 376      * @see #putByte(long, byte)
 377      */
 378     public native long allocateMemory(long bytes);
 379 
 380     /**
 381      * Resizes a new block of native memory, to the given size in bytes.  The
 382      * contents of the new block past the size of the old block are
 383      * uninitialized; they will generally be garbage.  The resulting native
 384      * pointer will be zero if and only if the requested size is zero.  The
 385      * resulting native pointer will be aligned for all value types.  Dispose
 386      * of this memory by calling {@link #freeMemory}, or resize it with {@link
 387      * #reallocateMemory}.  The address passed to this method may be null, in
 388      * which case an allocation will be performed.
 389      *
 390      * @throws IllegalArgumentException if the size is negative or too large
 391      *         for the native size_t type
 392      *
 393      * @throws OutOfMemoryError if the allocation is refused by the system
 394      *
 395      * @see #allocateMemory
 396      */
 397     public native long reallocateMemory(long address, long bytes);
 398 
 399     /**
 400      * Sets all bytes in a given block of memory to a fixed value
 401      * (usually zero).
 402      *
 403      * <p>This method determines a block's base address by means of two parameters,
 404      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 405      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 406      * the offset supplies an absolute base address.
 407      *
 408      * <p>The stores are in coherent (atomic) units of a size determined
 409      * by the address and length parameters.  If the effective address and
 410      * length are all even modulo 8, the stores take place in 'long' units.
 411      * If the effective address and length are (resp.) even modulo 4 or 2,
 412      * the stores take place in units of 'int' or 'short'.
 413      *
 414      * @since 1.7
 415      */
 416     public native void setMemory(Object o, long offset, long bytes, byte value);
 417 
 418     /**
 419      * Sets all bytes in a given block of memory to a fixed value
 420      * (usually zero).  This provides a <em>single-register</em> addressing mode,
 421      * as discussed in {@link #getInt(Object,long)}.
 422      *
 423      * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
 424      */
 425     public void setMemory(long address, long bytes, byte value) {
 426         setMemory(null, address, bytes, value);
 427     }
 428 
 429     /**
 430      * Sets all bytes in a given block of memory to a copy of another
 431      * block.
 432      *
 433      * <p>This method determines each block's base address by means of two parameters,
 434      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 435      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 436      * the offset supplies an absolute base address.
 437      *
 438      * <p>The transfers are in coherent (atomic) units of a size determined
 439      * by the address and length parameters.  If the effective addresses and
 440      * length are all even modulo 8, the transfer takes place in 'long' units.
 441      * If the effective addresses and length are (resp.) even modulo 4 or 2,
 442      * the transfer takes place in units of 'int' or 'short'.
 443      *
 444      * @since 1.7
 445      */
 446     @HotSpotIntrinsicCandidate
 447     public native void copyMemory(Object srcBase, long srcOffset,
 448                                   Object destBase, long destOffset,
 449                                   long bytes);
 450     /**
 451      * Sets all bytes in a given block of memory to a copy of another
 452      * block.  This provides a <em>single-register</em> addressing mode,
 453      * as discussed in {@link #getInt(Object,long)}.
 454      *
 455      * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
 456      */
 457     public void copyMemory(long srcAddress, long destAddress, long bytes) {
 458         copyMemory(null, srcAddress, null, destAddress, bytes);
 459     }
 460 
 461     private boolean isPrimitiveArray(Class<?> c) {
 462         Class<?> componentType = c.getComponentType();
 463         return componentType != null && componentType.isPrimitive();
 464     }
 465 
 466     private native void copySwapMemory0(Object srcBase, long srcOffset,
 467                                         Object destBase, long destOffset,
 468                                         long bytes, long elemSize);
 469 
 470     /**
 471      * Copies all elements from one block of memory to another block,
 472      * *unconditionally* byte swapping the elements on the fly.
 473      *
 474      * <p>This method determines each block's base address by means of two parameters,
 475      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 476      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 477      * the offset supplies an absolute base address.
 478      *
 479      * @since 9
 480      */
 481     public void copySwapMemory(Object srcBase, long srcOffset,
 482                                Object destBase, long destOffset,
 483                                long bytes, long elemSize) {
 484         if (bytes < 0) {
 485             throw new IllegalArgumentException();
 486         }
 487         if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
 488             throw new IllegalArgumentException();
 489         }
 490         if (bytes % elemSize != 0) {
 491             throw new IllegalArgumentException();
 492         }
 493         if ((srcBase == null && srcOffset == 0) ||
 494             (destBase == null && destOffset == 0)) {
 495             throw new NullPointerException();
 496         }
 497 
 498         // Must be off-heap, or primitive heap arrays
 499         if (srcBase != null && (srcOffset < 0 || !isPrimitiveArray(srcBase.getClass()))) {
 500             throw new IllegalArgumentException();
 501         }
 502         if (destBase != null && (destOffset < 0 || !isPrimitiveArray(destBase.getClass()))) {
 503             throw new IllegalArgumentException();
 504         }
 505 
 506         // Sanity check size and offsets on 32-bit platforms. Most
 507         // significant 32 bits must be zero.
 508         if (ADDRESS_SIZE == 4 &&
 509             (bytes >>> 32 != 0 || srcOffset >>> 32 != 0 || destOffset >>> 32 != 0)) {
 510             throw new IllegalArgumentException();
 511         }
 512 
 513         if (bytes == 0) {
 514             return;
 515         }
 516 
 517         copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 518     }
 519 
 520    /**
 521      * Copies all elements from one block of memory to another block, byte swapping the
 522      * elements on the fly.
 523      *
 524      * This provides a <em>single-register</em> addressing mode, as
 525      * discussed in {@link #getInt(Object,long)}.
 526      *
 527      * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
 528      */
 529     public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
 530         copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
 531     }
 532 
 533     /**
 534      * Disposes of a block of native memory, as obtained from {@link
 535      * #allocateMemory} or {@link #reallocateMemory}.  The address passed to
 536      * this method may be null, in which case no action is taken.
 537      *
 538      * @see #allocateMemory
 539      */
 540     public native void freeMemory(long address);
 541 
 542     /// random queries
 543 
 544     /**
 545      * This constant differs from all results that will ever be returned from
 546      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 547      * or {@link #arrayBaseOffset}.
 548      */
 549     public static final int INVALID_FIELD_OFFSET   = -1;
 550 
 551     /**
 552      * Reports the location of a given field in the storage allocation of its
 553      * class.  Do not expect to perform any sort of arithmetic on this offset;
 554      * it is just a cookie which is passed to the unsafe heap memory accessors.
 555      *
 556      * <p>Any given field will always have the same offset and base, and no
 557      * two distinct fields of the same class will ever have the same offset
 558      * and base.
 559      *
 560      * <p>As of 1.4.1, offsets for fields are represented as long values,
 561      * although the Sun JVM does not use the most significant 32 bits.
 562      * However, JVM implementations which store static fields at absolute
 563      * addresses can use long offsets and null base pointers to express
 564      * the field locations in a form usable by {@link #getInt(Object,long)}.
 565      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 566      * must preserve all bits of static field offsets.
 567      * @see #getInt(Object, long)
 568      */
 569     public native long objectFieldOffset(Field f);
 570 
 571     /**
 572      * Reports the location of a given static field, in conjunction with {@link
 573      * #staticFieldBase}.
 574      * <p>Do not expect to perform any sort of arithmetic on this offset;
 575      * it is just a cookie which is passed to the unsafe heap memory accessors.
 576      *
 577      * <p>Any given field will always have the same offset, and no two distinct
 578      * fields of the same class will ever have the same offset.
 579      *
 580      * <p>As of 1.4.1, offsets for fields are represented as long values,
 581      * although the Sun JVM does not use the most significant 32 bits.
 582      * It is hard to imagine a JVM technology which needs more than
 583      * a few bits to encode an offset within a non-array object,
 584      * However, for consistency with other methods in this class,
 585      * this method reports its result as a long value.
 586      * @see #getInt(Object, long)
 587      */
 588     public native long staticFieldOffset(Field f);
 589 
 590     /**
 591      * Reports the location of a given static field, in conjunction with {@link
 592      * #staticFieldOffset}.
 593      * <p>Fetch the base "Object", if any, with which static fields of the
 594      * given class can be accessed via methods like {@link #getInt(Object,
 595      * long)}.  This value may be null.  This value may refer to an object
 596      * which is a "cookie", not guaranteed to be a real Object, and it should
 597      * not be used in any way except as argument to the get and put routines in
 598      * this class.
 599      */
 600     public native Object staticFieldBase(Field f);
 601 
 602     /**
 603      * Detects if the given class may need to be initialized. This is often
 604      * needed in conjunction with obtaining the static field base of a
 605      * class.
 606      * @return false only if a call to {@code ensureClassInitialized} would have no effect
 607      */
 608     public native boolean shouldBeInitialized(Class<?> c);
 609 
 610     /**
 611      * Ensures the given class has been initialized. This is often
 612      * needed in conjunction with obtaining the static field base of a
 613      * class.
 614      */
 615     public native void ensureClassInitialized(Class<?> c);
 616 
 617     /**
 618      * Reports the offset of the first element in the storage allocation of a
 619      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 620      * for the same class, you may use that scale factor, together with this
 621      * base offset, to form new offsets to access elements of arrays of the
 622      * given class.
 623      *
 624      * @see #getInt(Object, long)
 625      * @see #putInt(Object, long, int)
 626      */
 627     public native int arrayBaseOffset(Class<?> arrayClass);
 628 
 629     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 630     public static final int ARRAY_BOOLEAN_BASE_OFFSET
 631             = theUnsafe.arrayBaseOffset(boolean[].class);
 632 
 633     /** The value of {@code arrayBaseOffset(byte[].class)} */
 634     public static final int ARRAY_BYTE_BASE_OFFSET
 635             = theUnsafe.arrayBaseOffset(byte[].class);
 636 
 637     /** The value of {@code arrayBaseOffset(short[].class)} */
 638     public static final int ARRAY_SHORT_BASE_OFFSET
 639             = theUnsafe.arrayBaseOffset(short[].class);
 640 
 641     /** The value of {@code arrayBaseOffset(char[].class)} */
 642     public static final int ARRAY_CHAR_BASE_OFFSET
 643             = theUnsafe.arrayBaseOffset(char[].class);
 644 
 645     /** The value of {@code arrayBaseOffset(int[].class)} */
 646     public static final int ARRAY_INT_BASE_OFFSET
 647             = theUnsafe.arrayBaseOffset(int[].class);
 648 
 649     /** The value of {@code arrayBaseOffset(long[].class)} */
 650     public static final int ARRAY_LONG_BASE_OFFSET
 651             = theUnsafe.arrayBaseOffset(long[].class);
 652 
 653     /** The value of {@code arrayBaseOffset(float[].class)} */
 654     public static final int ARRAY_FLOAT_BASE_OFFSET
 655             = theUnsafe.arrayBaseOffset(float[].class);
 656 
 657     /** The value of {@code arrayBaseOffset(double[].class)} */
 658     public static final int ARRAY_DOUBLE_BASE_OFFSET
 659             = theUnsafe.arrayBaseOffset(double[].class);
 660 
 661     /** The value of {@code arrayBaseOffset(Object[].class)} */
 662     public static final int ARRAY_OBJECT_BASE_OFFSET
 663             = theUnsafe.arrayBaseOffset(Object[].class);
 664 
 665     /**
 666      * Reports the scale factor for addressing elements in the storage
 667      * allocation of a given array class.  However, arrays of "narrow" types
 668      * will generally not work properly with accessors like {@link
 669      * #getByte(Object, long)}, so the scale factor for such classes is reported
 670      * as zero.
 671      *
 672      * @see #arrayBaseOffset
 673      * @see #getInt(Object, long)
 674      * @see #putInt(Object, long, int)
 675      */
 676     public native int arrayIndexScale(Class<?> arrayClass);
 677 
 678     /** The value of {@code arrayIndexScale(boolean[].class)} */
 679     public static final int ARRAY_BOOLEAN_INDEX_SCALE
 680             = theUnsafe.arrayIndexScale(boolean[].class);
 681 
 682     /** The value of {@code arrayIndexScale(byte[].class)} */
 683     public static final int ARRAY_BYTE_INDEX_SCALE
 684             = theUnsafe.arrayIndexScale(byte[].class);
 685 
 686     /** The value of {@code arrayIndexScale(short[].class)} */
 687     public static final int ARRAY_SHORT_INDEX_SCALE
 688             = theUnsafe.arrayIndexScale(short[].class);
 689 
 690     /** The value of {@code arrayIndexScale(char[].class)} */
 691     public static final int ARRAY_CHAR_INDEX_SCALE
 692             = theUnsafe.arrayIndexScale(char[].class);
 693 
 694     /** The value of {@code arrayIndexScale(int[].class)} */
 695     public static final int ARRAY_INT_INDEX_SCALE
 696             = theUnsafe.arrayIndexScale(int[].class);
 697 
 698     /** The value of {@code arrayIndexScale(long[].class)} */
 699     public static final int ARRAY_LONG_INDEX_SCALE
 700             = theUnsafe.arrayIndexScale(long[].class);
 701 
 702     /** The value of {@code arrayIndexScale(float[].class)} */
 703     public static final int ARRAY_FLOAT_INDEX_SCALE
 704             = theUnsafe.arrayIndexScale(float[].class);
 705 
 706     /** The value of {@code arrayIndexScale(double[].class)} */
 707     public static final int ARRAY_DOUBLE_INDEX_SCALE
 708             = theUnsafe.arrayIndexScale(double[].class);
 709 
 710     /** The value of {@code arrayIndexScale(Object[].class)} */
 711     public static final int ARRAY_OBJECT_INDEX_SCALE
 712             = theUnsafe.arrayIndexScale(Object[].class);
 713 
 714     /**
 715      * Reports the size in bytes of a native pointer, as stored via {@link
 716      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
 717      * other primitive types (as stored in native memory blocks) is determined
 718      * fully by their information content.
 719      */
 720     public native int addressSize();
 721 
 722     /** The value of {@code addressSize()} */
 723     public static final int ADDRESS_SIZE = theUnsafe.addressSize();
 724 
 725     /**
 726      * Reports the size in bytes of a native memory page (whatever that is).
 727      * This value will always be a power of two.
 728      */
 729     public native int pageSize();
 730 
 731 
 732     /// random trusted operations from JNI:
 733 
 734     /**
 735      * Tells the VM to define a class, without security checks.  By default, the
 736      * class loader and protection domain come from the caller's class.
 737      */
 738     public native Class<?> defineClass(String name, byte[] b, int off, int len,
 739                                        ClassLoader loader,
 740                                        ProtectionDomain protectionDomain);
 741 
 742     /**
 743      * Defines a class but does not make it known to the class loader or system dictionary.
 744      * <p>
 745      * For each CP entry, the corresponding CP patch must either be null or have
 746      * the a format that matches its tag:
 747      * <ul>
 748      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
 749      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
 750      * <li>Class: any java.lang.Class object
 751      * <li>String: any object (not just a java.lang.String)
 752      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
 753      * </ul>
 754      * @param hostClass context for linkage, access control, protection domain, and class loader
 755      * @param data      bytes of a class file
 756      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
 757      */
 758     public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
 759 
 760     /**
 761      * Allocates an instance but does not run any constructor.
 762      * Initializes the class if it has not yet been.
 763      */
 764     @HotSpotIntrinsicCandidate
 765     public native Object allocateInstance(Class<?> cls)
 766         throws InstantiationException;
 767 
 768     /**
 769      * Allocates an array of a given type, but does not do zeroing.
 770      * <p>
 771      * This method should only be used in the very rare cases where a high-performance code
 772      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
 773      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
 774      * <p>
 775      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
 776      * before allowing untrusted code, or code in other threads, to observe the reference
 777      * to the newly allocated array. In addition, the publication of the array reference must be
 778      * safe according to the Java Memory Model requirements.
 779      * <p>
 780      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
 781      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
 782      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
 783      * or issuing a {@link #storeFence} before publishing the reference.
 784      * <p>
 785      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
 786      * elements that could break heap integrity.
 787      *
 788      * @param componentType array component type to allocate
 789      * @param length array size to allocate
 790      * @throws IllegalArgumentException if component type is null, or not a primitive class;
 791      *                                  or the length is negative
 792      */
 793     public Object allocateUninitializedArray(Class<?> componentType, int length) {
 794        if (componentType == null) {
 795            throw new IllegalArgumentException("Component type is null");
 796        }
 797        if (!componentType.isPrimitive()) {
 798            throw new IllegalArgumentException("Component type is not primitive");
 799        }
 800        if (length < 0) {
 801            throw new IllegalArgumentException("Negative length");
 802        }
 803        return allocateUninitializedArray0(componentType, length);
 804     }
 805 
 806     @HotSpotIntrinsicCandidate
 807     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
 808        // These fallbacks provide zeroed arrays, but intrinsic is not required to
 809        // return the zeroed arrays.
 810        if (componentType == byte.class)    return new byte[length];
 811        if (componentType == boolean.class) return new boolean[length];
 812        if (componentType == short.class)   return new short[length];
 813        if (componentType == char.class)    return new char[length];
 814        if (componentType == int.class)     return new int[length];
 815        if (componentType == float.class)   return new float[length];
 816        if (componentType == long.class)    return new long[length];
 817        if (componentType == double.class)  return new double[length];
 818        return null;
 819     }
 820 
 821     /** Throws the exception without telling the verifier. */
 822     public native void throwException(Throwable ee);
 823 
 824     /**
 825      * Atomically updates Java variable to {@code x} if it is currently
 826      * holding {@code expected}.
 827      *
 828      * <p>This operation has memory semantics of a {@code volatile} read
 829      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
 830      *
 831      * @return {@code true} if successful
 832      */
 833     @HotSpotIntrinsicCandidate
 834     public final native boolean compareAndSwapObject(Object o, long offset,
 835                                                      Object expected,
 836                                                      Object x);
 837 
 838     @HotSpotIntrinsicCandidate
 839     public final native Object compareAndExchangeObjectVolatile(Object o, long offset,
 840                                                                 Object expected,
 841                                                                 Object x);
 842 
 843     @HotSpotIntrinsicCandidate
 844     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
 845                                                                Object expected,
 846                                                                Object x) {
 847         return compareAndExchangeObjectVolatile(o, offset, expected, x);
 848     }
 849 
 850     @HotSpotIntrinsicCandidate
 851     public final Object compareAndExchangeObjectRelease(Object o, long offset,
 852                                                                Object expected,
 853                                                                Object x) {
 854         return compareAndExchangeObjectVolatile(o, offset, expected, x);
 855     }
 856 
 857     @HotSpotIntrinsicCandidate
 858     public final boolean weakCompareAndSwapObject(Object o, long offset,
 859                                                          Object expected,
 860                                                          Object x) {
 861         return compareAndSwapObject(o, offset, expected, x);
 862     }
 863 
 864     @HotSpotIntrinsicCandidate
 865     public final boolean weakCompareAndSwapObjectAcquire(Object o, long offset,
 866                                                                 Object expected,
 867                                                                 Object x) {
 868         return compareAndSwapObject(o, offset, expected, x);
 869     }
 870 
 871     @HotSpotIntrinsicCandidate
 872     public final boolean weakCompareAndSwapObjectRelease(Object o, long offset,
 873                                                                 Object expected,
 874                                                                 Object x) {
 875         return compareAndSwapObject(o, offset, expected, x);
 876     }
 877 
 878     /**
 879      * Atomically updates Java variable to {@code x} if it is currently
 880      * holding {@code expected}.
 881      *
 882      * <p>This operation has memory semantics of a {@code volatile} read
 883      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
 884      *
 885      * @return {@code true} if successful
 886      */
 887     @HotSpotIntrinsicCandidate
 888     public final native boolean compareAndSwapInt(Object o, long offset,
 889                                                   int expected,
 890                                                   int x);
 891 
 892     @HotSpotIntrinsicCandidate
 893     public final native int compareAndExchangeIntVolatile(Object o, long offset,
 894                                                           int expected,
 895                                                           int x);
 896 
 897     @HotSpotIntrinsicCandidate
 898     public final int compareAndExchangeIntAcquire(Object o, long offset,
 899                                                          int expected,
 900                                                          int x) {
 901         return compareAndExchangeIntVolatile(o, offset, expected, x);
 902     }
 903 
 904     @HotSpotIntrinsicCandidate
 905     public final int compareAndExchangeIntRelease(Object o, long offset,
 906                                                          int expected,
 907                                                          int x) {
 908         return compareAndExchangeIntVolatile(o, offset, expected, x);
 909     }
 910 
 911     @HotSpotIntrinsicCandidate
 912     public final boolean weakCompareAndSwapInt(Object o, long offset,
 913                                                       int expected,
 914                                                       int x) {
 915         return compareAndSwapInt(o, offset, expected, x);
 916     }
 917 
 918     @HotSpotIntrinsicCandidate
 919     public final boolean weakCompareAndSwapIntAcquire(Object o, long offset,
 920                                                              int expected,
 921                                                              int x) {
 922         return compareAndSwapInt(o, offset, expected, x);
 923     }
 924 
 925     @HotSpotIntrinsicCandidate
 926     public final boolean weakCompareAndSwapIntRelease(Object o, long offset,
 927                                                              int expected,
 928                                                              int x) {
 929         return compareAndSwapInt(o, offset, expected, x);
 930     }
 931 
 932     /**
 933      * Atomically updates Java variable to {@code x} if it is currently
 934      * holding {@code expected}.
 935      *
 936      * <p>This operation has memory semantics of a {@code volatile} read
 937      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
 938      *
 939      * @return {@code true} if successful
 940      */
 941     @HotSpotIntrinsicCandidate
 942     public final native boolean compareAndSwapLong(Object o, long offset,
 943                                                    long expected,
 944                                                    long x);
 945 
 946     @HotSpotIntrinsicCandidate
 947     public final native long compareAndExchangeLongVolatile(Object o, long offset,
 948                                                             long expected,
 949                                                             long x);
 950 
 951     @HotSpotIntrinsicCandidate
 952     public final long compareAndExchangeLongAcquire(Object o, long offset,
 953                                                            long expected,
 954                                                            long x) {
 955         return compareAndExchangeLongVolatile(o, offset, expected, x);
 956     }
 957 
 958     @HotSpotIntrinsicCandidate
 959     public final long compareAndExchangeLongRelease(Object o, long offset,
 960                                                            long expected,
 961                                                            long x) {
 962         return compareAndExchangeLongVolatile(o, offset, expected, x);
 963     }
 964 
 965     @HotSpotIntrinsicCandidate
 966     public final boolean weakCompareAndSwapLong(Object o, long offset,
 967                                                        long expected,
 968                                                        long x) {
 969         return compareAndSwapLong(o, offset, expected, x);
 970     }
 971 
 972     @HotSpotIntrinsicCandidate
 973     public final boolean weakCompareAndSwapLongAcquire(Object o, long offset,
 974                                                               long expected,
 975                                                               long x) {
 976         return compareAndSwapLong(o, offset, expected, x);
 977     }
 978 
 979     @HotSpotIntrinsicCandidate
 980     public final boolean weakCompareAndSwapLongRelease(Object o, long offset,
 981                                                               long expected,
 982                                                               long x) {
 983         return compareAndSwapLong(o, offset, expected, x);
 984     }
 985 
 986     /**
 987      * Fetches a reference value from a given Java variable, with volatile
 988      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
 989      */
 990     @HotSpotIntrinsicCandidate
 991     public native Object getObjectVolatile(Object o, long offset);
 992 
 993     /**
 994      * Stores a reference value into a given Java variable, with
 995      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
 996      */
 997     @HotSpotIntrinsicCandidate
 998     public native void    putObjectVolatile(Object o, long offset, Object x);
 999 
1000     /** Volatile version of {@link #getInt(Object, long)}  */
1001     @HotSpotIntrinsicCandidate
1002     public native int     getIntVolatile(Object o, long offset);
1003 
1004     /** Volatile version of {@link #putInt(Object, long, int)}  */
1005     @HotSpotIntrinsicCandidate
1006     public native void    putIntVolatile(Object o, long offset, int x);
1007 
1008     /** Volatile version of {@link #getBoolean(Object, long)}  */
1009     @HotSpotIntrinsicCandidate
1010     public native boolean getBooleanVolatile(Object o, long offset);
1011 
1012     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
1013     @HotSpotIntrinsicCandidate
1014     public native void    putBooleanVolatile(Object o, long offset, boolean x);
1015 
1016     /** Volatile version of {@link #getByte(Object, long)}  */
1017     @HotSpotIntrinsicCandidate
1018     public native byte    getByteVolatile(Object o, long offset);
1019 
1020     /** Volatile version of {@link #putByte(Object, long, byte)}  */
1021     @HotSpotIntrinsicCandidate
1022     public native void    putByteVolatile(Object o, long offset, byte x);
1023 
1024     /** Volatile version of {@link #getShort(Object, long)}  */
1025     @HotSpotIntrinsicCandidate
1026     public native short   getShortVolatile(Object o, long offset);
1027 
1028     /** Volatile version of {@link #putShort(Object, long, short)}  */
1029     @HotSpotIntrinsicCandidate
1030     public native void    putShortVolatile(Object o, long offset, short x);
1031 
1032     /** Volatile version of {@link #getChar(Object, long)}  */
1033     @HotSpotIntrinsicCandidate
1034     public native char    getCharVolatile(Object o, long offset);
1035 
1036     /** Volatile version of {@link #putChar(Object, long, char)}  */
1037     @HotSpotIntrinsicCandidate
1038     public native void    putCharVolatile(Object o, long offset, char x);
1039 
1040     /** Volatile version of {@link #getLong(Object, long)}  */
1041     @HotSpotIntrinsicCandidate
1042     public native long    getLongVolatile(Object o, long offset);
1043 
1044     /** Volatile version of {@link #putLong(Object, long, long)}  */
1045     @HotSpotIntrinsicCandidate
1046     public native void    putLongVolatile(Object o, long offset, long x);
1047 
1048     /** Volatile version of {@link #getFloat(Object, long)}  */
1049     @HotSpotIntrinsicCandidate
1050     public native float   getFloatVolatile(Object o, long offset);
1051 
1052     /** Volatile version of {@link #putFloat(Object, long, float)}  */
1053     @HotSpotIntrinsicCandidate
1054     public native void    putFloatVolatile(Object o, long offset, float x);
1055 
1056     /** Volatile version of {@link #getDouble(Object, long)}  */
1057     @HotSpotIntrinsicCandidate
1058     public native double  getDoubleVolatile(Object o, long offset);
1059 
1060     /** Volatile version of {@link #putDouble(Object, long, double)}  */
1061     @HotSpotIntrinsicCandidate
1062     public native void    putDoubleVolatile(Object o, long offset, double x);
1063 
1064     /**
1065      * Version of {@link #putObjectVolatile(Object, long, Object)}
1066      * that does not guarantee immediate visibility of the store to
1067      * other threads. This method is generally only useful if the
1068      * underlying field is a Java volatile (or if an array cell, one
1069      * that is otherwise only accessed using volatile accesses).
1070      *
1071      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
1072      */
1073     @HotSpotIntrinsicCandidate
1074     public native void    putOrderedObject(Object o, long offset, Object x);
1075 
1076     /** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)}  */
1077     @HotSpotIntrinsicCandidate
1078     public native void    putOrderedInt(Object o, long offset, int x);
1079 
1080     /** Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)} */
1081     @HotSpotIntrinsicCandidate
1082     public native void    putOrderedLong(Object o, long offset, long x);
1083 
1084     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
1085     @HotSpotIntrinsicCandidate
1086     public final Object getObjectAcquire(Object o, long offset) {
1087         return getObjectVolatile(o, offset);
1088     }
1089 
1090     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
1091     @HotSpotIntrinsicCandidate
1092     public final boolean getBooleanAcquire(Object o, long offset) {
1093         return getBooleanVolatile(o, offset);
1094     }
1095 
1096     /** Acquire version of {@link #getByteVolatile(Object, long)} */
1097     @HotSpotIntrinsicCandidate
1098     public final byte getByteAcquire(Object o, long offset) {
1099         return getByteVolatile(o, offset);
1100     }
1101 
1102     /** Acquire version of {@link #getShortVolatile(Object, long)} */
1103     @HotSpotIntrinsicCandidate
1104     public final short getShortAcquire(Object o, long offset) {
1105         return getShortVolatile(o, offset);
1106     }
1107 
1108     /** Acquire version of {@link #getCharVolatile(Object, long)} */
1109     @HotSpotIntrinsicCandidate
1110     public final char getCharAcquire(Object o, long offset) {
1111         return getCharVolatile(o, offset);
1112     }
1113 
1114     /** Acquire version of {@link #getIntVolatile(Object, long)} */
1115     @HotSpotIntrinsicCandidate
1116     public final int getIntAcquire(Object o, long offset) {
1117         return getIntVolatile(o, offset);
1118     }
1119 
1120     /** Acquire version of {@link #getFloatVolatile(Object, long)} */
1121     @HotSpotIntrinsicCandidate
1122     public final float getFloatAcquire(Object o, long offset) {
1123         return getFloatVolatile(o, offset);
1124     }
1125 
1126     /** Acquire version of {@link #getLongVolatile(Object, long)} */
1127     @HotSpotIntrinsicCandidate
1128     public final long getLongAcquire(Object o, long offset) {
1129         return getLongVolatile(o, offset);
1130     }
1131 
1132     /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
1133     @HotSpotIntrinsicCandidate
1134     public final double getDoubleAcquire(Object o, long offset) {
1135         return getDoubleVolatile(o, offset);
1136     }
1137 
1138     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
1139     @HotSpotIntrinsicCandidate
1140     public final void putObjectRelease(Object o, long offset, Object x) {
1141         putObjectVolatile(o, offset, x);
1142     }
1143 
1144     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
1145     @HotSpotIntrinsicCandidate
1146     public final void putBooleanRelease(Object o, long offset, boolean x) {
1147         putBooleanVolatile(o, offset, x);
1148     }
1149 
1150     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
1151     @HotSpotIntrinsicCandidate
1152     public final void putByteRelease(Object o, long offset, byte x) {
1153         putByteVolatile(o, offset, x);
1154     }
1155 
1156     /** Release version of {@link #putShortVolatile(Object, long, short)} */
1157     @HotSpotIntrinsicCandidate
1158     public final void putShortRelease(Object o, long offset, short x) {
1159         putShortVolatile(o, offset, x);
1160     }
1161 
1162     /** Release version of {@link #putCharVolatile(Object, long, char)} */
1163     @HotSpotIntrinsicCandidate
1164     public final void putCharRelease(Object o, long offset, char x) {
1165         putCharVolatile(o, offset, x);
1166     }
1167 
1168     /** Release version of {@link #putIntVolatile(Object, long, int)} */
1169     @HotSpotIntrinsicCandidate
1170     public final void putIntRelease(Object o, long offset, int x) {
1171         putIntVolatile(o, offset, x);
1172     }
1173 
1174     /** Release version of {@link #putFloatVolatile(Object, long, float)} */
1175     @HotSpotIntrinsicCandidate
1176     public final void putFloatRelease(Object o, long offset, float x) {
1177         putFloatVolatile(o, offset, x);
1178     }
1179 
1180     /** Release version of {@link #putLongVolatile(Object, long, long)} */
1181     @HotSpotIntrinsicCandidate
1182     public final void putLongRelease(Object o, long offset, long x) {
1183         putLongVolatile(o, offset, x);
1184     }
1185 
1186     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
1187     @HotSpotIntrinsicCandidate
1188     public final void putDoubleRelease(Object o, long offset, double x) {
1189         putDoubleVolatile(o, offset, x);
1190     }
1191 
1192     // ------------------------------ Opaque --------------------------------------
1193 
1194     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
1195     @HotSpotIntrinsicCandidate
1196     public final Object getObjectOpaque(Object o, long offset) {
1197         return getObjectVolatile(o, offset);
1198     }
1199 
1200     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
1201     @HotSpotIntrinsicCandidate
1202     public final boolean getBooleanOpaque(Object o, long offset) {
1203         return getBooleanVolatile(o, offset);
1204     }
1205 
1206     /** Opaque version of {@link #getByteVolatile(Object, long)} */
1207     @HotSpotIntrinsicCandidate
1208     public final byte getByteOpaque(Object o, long offset) {
1209         return getByteVolatile(o, offset);
1210     }
1211 
1212     /** Opaque version of {@link #getShortVolatile(Object, long)} */
1213     @HotSpotIntrinsicCandidate
1214     public final short getShortOpaque(Object o, long offset) {
1215         return getShortVolatile(o, offset);
1216     }
1217 
1218     /** Opaque version of {@link #getCharVolatile(Object, long)} */
1219     @HotSpotIntrinsicCandidate
1220     public final char getCharOpaque(Object o, long offset) {
1221         return getCharVolatile(o, offset);
1222     }
1223 
1224     /** Opaque version of {@link #getIntVolatile(Object, long)} */
1225     @HotSpotIntrinsicCandidate
1226     public final int getIntOpaque(Object o, long offset) {
1227         return getIntVolatile(o, offset);
1228     }
1229 
1230     /** Opaque version of {@link #getFloatVolatile(Object, long)} */
1231     @HotSpotIntrinsicCandidate
1232     public final float getFloatOpaque(Object o, long offset) {
1233         return getFloatVolatile(o, offset);
1234     }
1235 
1236     /** Opaque version of {@link #getLongVolatile(Object, long)} */
1237     @HotSpotIntrinsicCandidate
1238     public final long getLongOpaque(Object o, long offset) {
1239         return getLongVolatile(o, offset);
1240     }
1241 
1242     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
1243     @HotSpotIntrinsicCandidate
1244     public final double getDoubleOpaque(Object o, long offset) {
1245         return getDoubleVolatile(o, offset);
1246     }
1247 
1248     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
1249     @HotSpotIntrinsicCandidate
1250     public final void putObjectOpaque(Object o, long offset, Object x) {
1251         putObjectVolatile(o, offset, x);
1252     }
1253 
1254     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
1255     @HotSpotIntrinsicCandidate
1256     public final void putBooleanOpaque(Object o, long offset, boolean x) {
1257         putBooleanVolatile(o, offset, x);
1258     }
1259 
1260     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
1261     @HotSpotIntrinsicCandidate
1262     public final void putByteOpaque(Object o, long offset, byte x) {
1263         putByteVolatile(o, offset, x);
1264     }
1265 
1266     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
1267     @HotSpotIntrinsicCandidate
1268     public final void putShortOpaque(Object o, long offset, short x) {
1269         putShortVolatile(o, offset, x);
1270     }
1271 
1272     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
1273     @HotSpotIntrinsicCandidate
1274     public final void putCharOpaque(Object o, long offset, char x) {
1275         putCharVolatile(o, offset, x);
1276     }
1277 
1278     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
1279     @HotSpotIntrinsicCandidate
1280     public final void putIntOpaque(Object o, long offset, int x) {
1281         putIntVolatile(o, offset, x);
1282     }
1283 
1284     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
1285     @HotSpotIntrinsicCandidate
1286     public final void putFloatOpaque(Object o, long offset, float x) {
1287         putFloatVolatile(o, offset, x);
1288     }
1289 
1290     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
1291     @HotSpotIntrinsicCandidate
1292     public final void putLongOpaque(Object o, long offset, long x) {
1293         putLongVolatile(o, offset, x);
1294     }
1295 
1296     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
1297     @HotSpotIntrinsicCandidate
1298     public final void putDoubleOpaque(Object o, long offset, double x) {
1299         putDoubleVolatile(o, offset, x);
1300     }
1301 
1302     /**
1303      * Unblocks the given thread blocked on {@code park}, or, if it is
1304      * not blocked, causes the subsequent call to {@code park} not to
1305      * block.  Note: this operation is "unsafe" solely because the
1306      * caller must somehow ensure that the thread has not been
1307      * destroyed. Nothing special is usually required to ensure this
1308      * when called from Java (in which there will ordinarily be a live
1309      * reference to the thread) but this is not nearly-automatically
1310      * so when calling from native code.
1311      *
1312      * @param thread the thread to unpark.
1313      */
1314     @HotSpotIntrinsicCandidate
1315     public native void unpark(Object thread);
1316 
1317     /**
1318      * Blocks current thread, returning when a balancing
1319      * {@code unpark} occurs, or a balancing {@code unpark} has
1320      * already occurred, or the thread is interrupted, or, if not
1321      * absolute and time is not zero, the given time nanoseconds have
1322      * elapsed, or if absolute, the given deadline in milliseconds
1323      * since Epoch has passed, or spuriously (i.e., returning for no
1324      * "reason"). Note: This operation is in the Unsafe class only
1325      * because {@code unpark} is, so it would be strange to place it
1326      * elsewhere.
1327      */
1328     @HotSpotIntrinsicCandidate
1329     public native void park(boolean isAbsolute, long time);
1330 
1331     /**
1332      * Gets the load average in the system run queue assigned
1333      * to the available processors averaged over various periods of time.
1334      * This method retrieves the given {@code nelem} samples and
1335      * assigns to the elements of the given {@code loadavg} array.
1336      * The system imposes a maximum of 3 samples, representing
1337      * averages over the last 1,  5,  and  15 minutes, respectively.
1338      *
1339      * @param loadavg an array of double of size nelems
1340      * @param nelems the number of samples to be retrieved and
1341      *        must be 1 to 3.
1342      *
1343      * @return the number of samples actually retrieved; or -1
1344      *         if the load average is unobtainable.
1345      */
1346     public native int getLoadAverage(double[] loadavg, int nelems);
1347 
1348     // The following contain CAS-based Java implementations used on
1349     // platforms not supporting native instructions
1350 
1351     /**
1352      * Atomically adds the given value to the current value of a field
1353      * or array element within the given object {@code o}
1354      * at the given {@code offset}.
1355      *
1356      * @param o object/array to update the field/element in
1357      * @param offset field/element offset
1358      * @param delta the value to add
1359      * @return the previous value
1360      * @since 1.8
1361      */
1362     @HotSpotIntrinsicCandidate
1363     public final int getAndAddInt(Object o, long offset, int delta) {
1364         int v;
1365         do {
1366             v = getIntVolatile(o, offset);
1367         } while (!compareAndSwapInt(o, offset, v, v + delta));
1368         return v;
1369     }
1370 
1371     /**
1372      * Atomically adds the given value to the current value of a field
1373      * or array element within the given object {@code o}
1374      * at the given {@code offset}.
1375      *
1376      * @param o object/array to update the field/element in
1377      * @param offset field/element offset
1378      * @param delta the value to add
1379      * @return the previous value
1380      * @since 1.8
1381      */
1382     @HotSpotIntrinsicCandidate
1383     public final long getAndAddLong(Object o, long offset, long delta) {
1384         long v;
1385         do {
1386             v = getLongVolatile(o, offset);
1387         } while (!compareAndSwapLong(o, offset, v, v + delta));
1388         return v;
1389     }
1390 
1391     /**
1392      * Atomically exchanges the given value with the current value of
1393      * a field or array element within the given object {@code o}
1394      * at the given {@code offset}.
1395      *
1396      * @param o object/array to update the field/element in
1397      * @param offset field/element offset
1398      * @param newValue new value
1399      * @return the previous value
1400      * @since 1.8
1401      */
1402     @HotSpotIntrinsicCandidate
1403     public final int getAndSetInt(Object o, long offset, int newValue) {
1404         int v;
1405         do {
1406             v = getIntVolatile(o, offset);
1407         } while (!compareAndSwapInt(o, offset, v, newValue));
1408         return v;
1409     }
1410 
1411     /**
1412      * Atomically exchanges the given value with the current value of
1413      * a field or array element within the given object {@code o}
1414      * at the given {@code offset}.
1415      *
1416      * @param o object/array to update the field/element in
1417      * @param offset field/element offset
1418      * @param newValue new value
1419      * @return the previous value
1420      * @since 1.8
1421      */
1422     @HotSpotIntrinsicCandidate
1423     public final long getAndSetLong(Object o, long offset, long newValue) {
1424         long v;
1425         do {
1426             v = getLongVolatile(o, offset);
1427         } while (!compareAndSwapLong(o, offset, v, newValue));
1428         return v;
1429     }
1430 
1431     /**
1432      * Atomically exchanges the given reference value with the current
1433      * reference value of a field or array element within the given
1434      * object {@code o} at the given {@code offset}.
1435      *
1436      * @param o object/array to update the field/element in
1437      * @param offset field/element offset
1438      * @param newValue new value
1439      * @return the previous value
1440      * @since 1.8
1441      */
1442     @HotSpotIntrinsicCandidate
1443     public final Object getAndSetObject(Object o, long offset, Object newValue) {
1444         Object v;
1445         do {
1446             v = getObjectVolatile(o, offset);
1447         } while (!compareAndSwapObject(o, offset, v, newValue));
1448         return v;
1449     }
1450 
1451 
1452     /**
1453      * Ensures that loads before the fence will not be reordered with loads and
1454      * stores after the fence; a "LoadLoad plus LoadStore barrier".
1455      *
1456      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
1457      * (an "acquire fence").
1458      *
1459      * A pure LoadLoad fence is not provided, since the addition of LoadStore
1460      * is almost always desired, and most current hardware instructions that
1461      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
1462      * @since 1.8
1463      */
1464     @HotSpotIntrinsicCandidate
1465     public native void loadFence();
1466 
1467     /**
1468      * Ensures that loads and stores before the fence will not be reordered with
1469      * stores after the fence; a "StoreStore plus LoadStore barrier".
1470      *
1471      * Corresponds to C11 atomic_thread_fence(memory_order_release)
1472      * (a "release fence").
1473      *
1474      * A pure StoreStore fence is not provided, since the addition of LoadStore
1475      * is almost always desired, and most current hardware instructions that
1476      * provide a StoreStore barrier also provide a LoadStore barrier for free.
1477      * @since 1.8
1478      */
1479     @HotSpotIntrinsicCandidate
1480     public native void storeFence();
1481 
1482     /**
1483      * Ensures that loads and stores before the fence will not be reordered
1484      * with loads and stores after the fence.  Implies the effects of both
1485      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
1486      * barrier.
1487      *
1488      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
1489      * @since 1.8
1490      */
1491     @HotSpotIntrinsicCandidate
1492     public native void fullFence();
1493 
1494     /**
1495      * Ensures that loads before the fence will not be reordered with
1496      * loads after the fence.
1497      */
1498     public final void loadLoadFence() {
1499         loadFence();
1500     }
1501 
1502     /**
1503      * Ensures that stores before the fence will not be reordered with
1504      * stores after the fence.
1505      */
1506     public final void storeStoreFence() {
1507         storeFence();
1508     }
1509 
1510 
1511     /**
1512      * Throws IllegalAccessError; for use by the VM for access control
1513      * error support.
1514      * @since 1.8
1515      */
1516     private static void throwIllegalAccessError() {
1517         throw new IllegalAccessError();
1518     }
1519 
1520     /**
1521      * @return Returns true if the native byte ordering of this
1522      * platform is big-endian, false if it is little-endian.
1523      */
1524     public final boolean isBigEndian() { return BE; }
1525 
1526     /**
1527      * @return Returns true if this platform is capable of performing
1528      * accesses at addresses which are not aligned for the type of the
1529      * primitive type being accessed, false otherwise.
1530      */
1531     public final boolean unalignedAccess() { return unalignedAccess; }
1532 
1533     /**
1534      * Fetches a value at some byte offset into a given Java object.
1535      * More specifically, fetches a value within the given object
1536      * <code>o</code> at the given offset, or (if <code>o</code> is
1537      * null) from the memory address whose numerical value is the
1538      * given offset.  <p>
1539      *
1540      * The specification of this method is the same as {@link
1541      * #getLong(Object, long)} except that the offset does not need to
1542      * have been obtained from {@link #objectFieldOffset} on the
1543      * {@link java.lang.reflect.Field} of some Java field.  The value
1544      * in memory is raw data, and need not correspond to any Java
1545      * variable.  Unless <code>o</code> is null, the value accessed
1546      * must be entirely within the allocated object.  The endianness
1547      * of the value in memory is the endianness of the native platform.
1548      *
1549      * <p> The read will be atomic with respect to the largest power
1550      * of two that divides the GCD of the offset and the storage size.
1551      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
1552      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1553      * respectively.  There are no other guarantees of atomicity.
1554      * <p>
1555      * 8-byte atomicity is only guaranteed on platforms on which
1556      * support atomic accesses to longs.
1557      *
1558      * @param o Java heap object in which the value resides, if any, else
1559      *        null
1560      * @param offset The offset in bytes from the start of the object
1561      * @return the value fetched from the indicated object
1562      * @throws RuntimeException No defined exceptions are thrown, not even
1563      *         {@link NullPointerException}
1564      * @since 9
1565      */
1566     @HotSpotIntrinsicCandidate
1567     public final long getLongUnaligned(Object o, long offset) {
1568         if ((offset & 7) == 0) {
1569             return getLong(o, offset);
1570         } else if ((offset & 3) == 0) {
1571             return makeLong(getInt(o, offset),
1572                             getInt(o, offset + 4));
1573         } else if ((offset & 1) == 0) {
1574             return makeLong(getShort(o, offset),
1575                             getShort(o, offset + 2),
1576                             getShort(o, offset + 4),
1577                             getShort(o, offset + 6));
1578         } else {
1579             return makeLong(getByte(o, offset),
1580                             getByte(o, offset + 1),
1581                             getByte(o, offset + 2),
1582                             getByte(o, offset + 3),
1583                             getByte(o, offset + 4),
1584                             getByte(o, offset + 5),
1585                             getByte(o, offset + 6),
1586                             getByte(o, offset + 7));
1587         }
1588     }
1589     /**
1590      * As {@link #getLongUnaligned(Object, long)} but with an
1591      * additional argument which specifies the endianness of the value
1592      * as stored in memory.
1593      *
1594      * @param o Java heap object in which the variable resides
1595      * @param offset The offset in bytes from the start of the object
1596      * @param bigEndian The endianness of the value
1597      * @return the value fetched from the indicated object
1598      * @since 9
1599      */
1600     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
1601         return convEndian(bigEndian, getLongUnaligned(o, offset));
1602     }
1603 
1604     /** @see #getLongUnaligned(Object, long) */
1605     @HotSpotIntrinsicCandidate
1606     public final int getIntUnaligned(Object o, long offset) {
1607         if ((offset & 3) == 0) {
1608             return getInt(o, offset);
1609         } else if ((offset & 1) == 0) {
1610             return makeInt(getShort(o, offset),
1611                            getShort(o, offset + 2));
1612         } else {
1613             return makeInt(getByte(o, offset),
1614                            getByte(o, offset + 1),
1615                            getByte(o, offset + 2),
1616                            getByte(o, offset + 3));
1617         }
1618     }
1619     /** @see #getLongUnaligned(Object, long, boolean) */
1620     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
1621         return convEndian(bigEndian, getIntUnaligned(o, offset));
1622     }
1623 
1624     /** @see #getLongUnaligned(Object, long) */
1625     @HotSpotIntrinsicCandidate
1626     public final short getShortUnaligned(Object o, long offset) {
1627         if ((offset & 1) == 0) {
1628             return getShort(o, offset);
1629         } else {
1630             return makeShort(getByte(o, offset),
1631                              getByte(o, offset + 1));
1632         }
1633     }
1634     /** @see #getLongUnaligned(Object, long, boolean) */
1635     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
1636         return convEndian(bigEndian, getShortUnaligned(o, offset));
1637     }
1638 
1639     /** @see #getLongUnaligned(Object, long) */
1640     @HotSpotIntrinsicCandidate
1641     public final char getCharUnaligned(Object o, long offset) {
1642         if ((offset & 1) == 0) {
1643             return getChar(o, offset);
1644         } else {
1645             return (char)makeShort(getByte(o, offset),
1646                                    getByte(o, offset + 1));
1647         }
1648     }
1649 
1650     /** @see #getLongUnaligned(Object, long, boolean) */
1651     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
1652         return convEndian(bigEndian, getCharUnaligned(o, offset));
1653     }
1654 
1655     /**
1656      * Stores a value at some byte offset into a given Java object.
1657      * <p>
1658      * The specification of this method is the same as {@link
1659      * #getLong(Object, long)} except that the offset does not need to
1660      * have been obtained from {@link #objectFieldOffset} on the
1661      * {@link java.lang.reflect.Field} of some Java field.  The value
1662      * in memory is raw data, and need not correspond to any Java
1663      * variable.  The endianness of the value in memory is the
1664      * endianness of the native platform.
1665      * <p>
1666      * The write will be atomic with respect to the largest power of
1667      * two that divides the GCD of the offset and the storage size.
1668      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
1669      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1670      * respectively.  There are no other guarantees of atomicity.
1671      * <p>
1672      * 8-byte atomicity is only guaranteed on platforms on which
1673      * support atomic accesses to longs.
1674      *
1675      * @param o Java heap object in which the value resides, if any, else
1676      *        null
1677      * @param offset The offset in bytes from the start of the object
1678      * @param x the value to store
1679      * @throws RuntimeException No defined exceptions are thrown, not even
1680      *         {@link NullPointerException}
1681      * @since 9
1682      */
1683     @HotSpotIntrinsicCandidate
1684     public final void putLongUnaligned(Object o, long offset, long x) {
1685         if ((offset & 7) == 0) {
1686             putLong(o, offset, x);
1687         } else if ((offset & 3) == 0) {
1688             putLongParts(o, offset,
1689                          (int)(x >> 0),
1690                          (int)(x >>> 32));
1691         } else if ((offset & 1) == 0) {
1692             putLongParts(o, offset,
1693                          (short)(x >>> 0),
1694                          (short)(x >>> 16),
1695                          (short)(x >>> 32),
1696                          (short)(x >>> 48));
1697         } else {
1698             putLongParts(o, offset,
1699                          (byte)(x >>> 0),
1700                          (byte)(x >>> 8),
1701                          (byte)(x >>> 16),
1702                          (byte)(x >>> 24),
1703                          (byte)(x >>> 32),
1704                          (byte)(x >>> 40),
1705                          (byte)(x >>> 48),
1706                          (byte)(x >>> 56));
1707         }
1708     }
1709 
1710     /**
1711      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
1712      * argument which specifies the endianness of the value as stored in memory.
1713      * @param o Java heap object in which the value resides
1714      * @param offset The offset in bytes from the start of the object
1715      * @param x the value to store
1716      * @param bigEndian The endianness of the value
1717      * @throws RuntimeException No defined exceptions are thrown, not even
1718      *         {@link NullPointerException}
1719      * @since 9
1720      */
1721     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
1722         putLongUnaligned(o, offset, convEndian(bigEndian, x));
1723     }
1724 
1725     /** @see #putLongUnaligned(Object, long, long) */
1726     @HotSpotIntrinsicCandidate
1727     public final void putIntUnaligned(Object o, long offset, int x) {
1728         if ((offset & 3) == 0) {
1729             putInt(o, offset, x);
1730         } else if ((offset & 1) == 0) {
1731             putIntParts(o, offset,
1732                         (short)(x >> 0),
1733                         (short)(x >>> 16));
1734         } else {
1735             putIntParts(o, offset,
1736                         (byte)(x >>> 0),
1737                         (byte)(x >>> 8),
1738                         (byte)(x >>> 16),
1739                         (byte)(x >>> 24));
1740         }
1741     }
1742     /** @see #putLongUnaligned(Object, long, long, boolean) */
1743     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
1744         putIntUnaligned(o, offset, convEndian(bigEndian, x));
1745     }
1746 
1747     /** @see #putLongUnaligned(Object, long, long) */
1748     @HotSpotIntrinsicCandidate
1749     public final void putShortUnaligned(Object o, long offset, short x) {
1750         if ((offset & 1) == 0) {
1751             putShort(o, offset, x);
1752         } else {
1753             putShortParts(o, offset,
1754                           (byte)(x >>> 0),
1755                           (byte)(x >>> 8));
1756         }
1757     }
1758     /** @see #putLongUnaligned(Object, long, long, boolean) */
1759     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
1760         putShortUnaligned(o, offset, convEndian(bigEndian, x));
1761     }
1762 
1763     /** @see #putLongUnaligned(Object, long, long) */
1764     @HotSpotIntrinsicCandidate
1765     public final void putCharUnaligned(Object o, long offset, char x) {
1766         putShortUnaligned(o, offset, (short)x);
1767     }
1768     /** @see #putLongUnaligned(Object, long, long, boolean) */
1769     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
1770         putCharUnaligned(o, offset, convEndian(bigEndian, x));
1771     }
1772 
1773     // JVM interface methods
1774     private native boolean unalignedAccess0();
1775     private native boolean isBigEndian0();
1776 
1777     // BE is true iff the native endianness of this platform is big.
1778     private static final boolean BE = theUnsafe.isBigEndian0();
1779 
1780     // unalignedAccess is true iff this platform can perform unaligned accesses.
1781     private static final boolean unalignedAccess = theUnsafe.unalignedAccess0();
1782 
1783     private static int pickPos(int top, int pos) { return BE ? top - pos : pos; }
1784 
1785     // These methods construct integers from bytes.  The byte ordering
1786     // is the native endianness of this platform.
1787     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
1788         return ((toUnsignedLong(i0) << pickPos(56, 0))
1789               | (toUnsignedLong(i1) << pickPos(56, 8))
1790               | (toUnsignedLong(i2) << pickPos(56, 16))
1791               | (toUnsignedLong(i3) << pickPos(56, 24))
1792               | (toUnsignedLong(i4) << pickPos(56, 32))
1793               | (toUnsignedLong(i5) << pickPos(56, 40))
1794               | (toUnsignedLong(i6) << pickPos(56, 48))
1795               | (toUnsignedLong(i7) << pickPos(56, 56)));
1796     }
1797     private static long makeLong(short i0, short i1, short i2, short i3) {
1798         return ((toUnsignedLong(i0) << pickPos(48, 0))
1799               | (toUnsignedLong(i1) << pickPos(48, 16))
1800               | (toUnsignedLong(i2) << pickPos(48, 32))
1801               | (toUnsignedLong(i3) << pickPos(48, 48)));
1802     }
1803     private static long makeLong(int i0, int i1) {
1804         return (toUnsignedLong(i0) << pickPos(32, 0))
1805              | (toUnsignedLong(i1) << pickPos(32, 32));
1806     }
1807     private static int makeInt(short i0, short i1) {
1808         return (toUnsignedInt(i0) << pickPos(16, 0))
1809              | (toUnsignedInt(i1) << pickPos(16, 16));
1810     }
1811     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
1812         return ((toUnsignedInt(i0) << pickPos(24, 0))
1813               | (toUnsignedInt(i1) << pickPos(24, 8))
1814               | (toUnsignedInt(i2) << pickPos(24, 16))
1815               | (toUnsignedInt(i3) << pickPos(24, 24)));
1816     }
1817     private static short makeShort(byte i0, byte i1) {
1818         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
1819                      | (toUnsignedInt(i1) << pickPos(8, 8)));
1820     }
1821 
1822     private static byte  pick(byte  le, byte  be) { return BE ? be : le; }
1823     private static short pick(short le, short be) { return BE ? be : le; }
1824     private static int   pick(int   le, int   be) { return BE ? be : le; }
1825 
1826     // These methods write integers to memory from smaller parts
1827     // provided by their caller.  The ordering in which these parts
1828     // are written is the native endianness of this platform.
1829     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
1830         putByte(o, offset + 0, pick(i0, i7));
1831         putByte(o, offset + 1, pick(i1, i6));
1832         putByte(o, offset + 2, pick(i2, i5));
1833         putByte(o, offset + 3, pick(i3, i4));
1834         putByte(o, offset + 4, pick(i4, i3));
1835         putByte(o, offset + 5, pick(i5, i2));
1836         putByte(o, offset + 6, pick(i6, i1));
1837         putByte(o, offset + 7, pick(i7, i0));
1838     }
1839     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
1840         putShort(o, offset + 0, pick(i0, i3));
1841         putShort(o, offset + 2, pick(i1, i2));
1842         putShort(o, offset + 4, pick(i2, i1));
1843         putShort(o, offset + 6, pick(i3, i0));
1844     }
1845     private void putLongParts(Object o, long offset, int i0, int i1) {
1846         putInt(o, offset + 0, pick(i0, i1));
1847         putInt(o, offset + 4, pick(i1, i0));
1848     }
1849     private void putIntParts(Object o, long offset, short i0, short i1) {
1850         putShort(o, offset + 0, pick(i0, i1));
1851         putShort(o, offset + 2, pick(i1, i0));
1852     }
1853     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
1854         putByte(o, offset + 0, pick(i0, i3));
1855         putByte(o, offset + 1, pick(i1, i2));
1856         putByte(o, offset + 2, pick(i2, i1));
1857         putByte(o, offset + 3, pick(i3, i0));
1858     }
1859     private void putShortParts(Object o, long offset, byte i0, byte i1) {
1860         putByte(o, offset + 0, pick(i0, i1));
1861         putByte(o, offset + 1, pick(i1, i0));
1862     }
1863 
1864     // Zero-extend an integer
1865     private static int toUnsignedInt(byte n)    { return n & 0xff; }
1866     private static int toUnsignedInt(short n)   { return n & 0xffff; }
1867     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
1868     private static long toUnsignedLong(short n) { return n & 0xffffl; }
1869     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
1870 
1871     // Maybe byte-reverse an integer
1872     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
1873     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
1874     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
1875     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
1876 }