1 /*
   2  * Copyright (c) 2000, 2017, 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 jdk.internal.HotSpotIntrinsicCandidate;
  29 import jdk.internal.vm.annotation.ForceInline;
  30 
  31 import java.lang.reflect.Field;
  32 import java.security.ProtectionDomain;
  33 
  34 
  35 /**
  36  * A collection of methods for performing low-level, unsafe operations.
  37  * Although the class and all methods are public, use of this class is
  38  * limited because only trusted code can obtain instances of it.
  39  *
  40  * <em>Note:</em> It is the resposibility of the caller to make sure
  41  * arguments are checked before methods of this class are
  42  * called. While some rudimentary checks are performed on the input,
  43  * the checks are best effort and when performance is an overriding
  44  * priority, as when methods of this class are optimized by the
  45  * runtime compiler, some or all checks (if any) may be elided. Hence,
  46  * the caller must not rely on the checks and corresponding
  47  * exceptions!
  48  *
  49  * @author John R. Rose
  50  * @see #getUnsafe
  51  */
  52 
  53 public final class Unsafe {
  54 
  55     private static native void registerNatives();
  56     static {
  57         registerNatives();
  58     }
  59 
  60     private Unsafe() {}
  61 
  62     private static final Unsafe theUnsafe = new Unsafe();
  63 
  64     /**
  65      * Provides the caller with the capability of performing unsafe
  66      * operations.
  67      *
  68      * <p>The returned {@code Unsafe} object should be carefully guarded
  69      * by the caller, since it can be used to read and write data at arbitrary
  70      * memory addresses.  It must never be passed to untrusted code.
  71      *
  72      * <p>Most methods in this class are very low-level, and correspond to a
  73      * small number of hardware instructions (on typical machines).  Compilers
  74      * are encouraged to optimize these methods accordingly.
  75      *
  76      * <p>Here is a suggested idiom for using unsafe operations:
  77      *
  78      * <pre> {@code
  79      * class MyTrustedClass {
  80      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  81      *   ...
  82      *   private long myCountAddress = ...;
  83      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  84      * }}</pre>
  85      *
  86      * (It may assist compilers to make the local variable {@code final}.)
  87      */
  88     public static Unsafe getUnsafe() {
  89         return theUnsafe;
  90     }
  91 
  92     /// peek and poke operations
  93     /// (compilers should optimize these to memory ops)
  94 
  95     // These work on object fields in the Java heap.
  96     // They will not work on elements of packed arrays.
  97 
  98     /**
  99      * Fetches a value from a given Java variable.
 100      * More specifically, fetches a field or array element within the given
 101      * object {@code o} at the given offset, or (if {@code o} is null)
 102      * from the memory address whose numerical value is the given offset.
 103      * <p>
 104      * The results are undefined unless one of the following cases is true:
 105      * <ul>
 106      * <li>The offset was obtained from {@link #objectFieldOffset} on
 107      * the {@link java.lang.reflect.Field} of some Java field and the object
 108      * referred to by {@code o} is of a class compatible with that
 109      * field's class.
 110      *
 111      * <li>The offset and object reference {@code o} (either null or
 112      * non-null) were both obtained via {@link #staticFieldOffset}
 113      * and {@link #staticFieldBase} (respectively) from the
 114      * reflective {@link Field} representation of some Java field.
 115      *
 116      * <li>The object referred to by {@code o} is an array, and the offset
 117      * is an integer of the form {@code B+N*S}, where {@code N} is
 118      * a valid index into the array, and {@code B} and {@code S} are
 119      * the values obtained by {@link #arrayBaseOffset} and {@link
 120      * #arrayIndexScale} (respectively) from the array's class.  The value
 121      * referred to is the {@code N}<em>th</em> element of the array.
 122      *
 123      * </ul>
 124      * <p>
 125      * If one of the above cases is true, the call references a specific Java
 126      * variable (field or array element).  However, the results are undefined
 127      * if that variable is not in fact of the type returned by this method.
 128      * <p>
 129      * This method refers to a variable by means of two parameters, and so
 130      * it provides (in effect) a <em>double-register</em> addressing mode
 131      * for Java variables.  When the object reference is null, this method
 132      * uses its offset as an absolute address.  This is similar in operation
 133      * to methods such as {@link #getInt(long)}, which provide (in effect) a
 134      * <em>single-register</em> addressing mode for non-Java variables.
 135      * However, because Java variables may have a different layout in memory
 136      * from non-Java variables, programmers should not assume that these
 137      * two addressing modes are ever equivalent.  Also, programmers should
 138      * remember that offsets from the double-register addressing mode cannot
 139      * be portably confused with longs used in the single-register addressing
 140      * mode.
 141      *
 142      * @param o Java heap object in which the variable resides, if any, else
 143      *        null
 144      * @param offset indication of where the variable resides in a Java heap
 145      *        object, if any, else a memory address locating the variable
 146      *        statically
 147      * @return the value fetched from the indicated Java variable
 148      * @throws RuntimeException No defined exceptions are thrown, not even
 149      *         {@link NullPointerException}
 150      */
 151     @HotSpotIntrinsicCandidate
 152     public native int getInt(Object o, long offset);
 153 
 154     /**
 155      * Stores a value into a given Java variable.
 156      * <p>
 157      * The first two parameters are interpreted exactly as with
 158      * {@link #getInt(Object, long)} to refer to a specific
 159      * Java variable (field or array element).  The given value
 160      * is stored into that variable.
 161      * <p>
 162      * The variable must be of the same type as the method
 163      * parameter {@code x}.
 164      *
 165      * @param o Java heap object in which the variable resides, if any, else
 166      *        null
 167      * @param offset indication of where the variable resides in a Java heap
 168      *        object, if any, else a memory address locating the variable
 169      *        statically
 170      * @param x the value to store into the indicated Java variable
 171      * @throws RuntimeException No defined exceptions are thrown, not even
 172      *         {@link NullPointerException}
 173      */
 174     @HotSpotIntrinsicCandidate
 175     public native void putInt(Object o, long offset, int x);
 176 
 177     /**
 178      * Returns true if the given class is a regular value type.
 179      */
 180     public boolean isValueType(Class<?> c) {
 181         return c.isValue() && c == c.asValueType();
 182     }
 183 
 184     private static final int JVM_ACC_FLATTENED = 0x00008000; // HotSpot-specific bit
 185 
 186     /**
 187      * Returns true if the given field is flattened.
 188      */
 189     public boolean isFlattened(Field f) {
 190         return (f.getModifiers() & JVM_ACC_FLATTENED) == JVM_ACC_FLATTENED;
 191     }
 192 
 193     /**
 194      * Returns true if the given class is a flattened array.
 195      */
 196     public native boolean isFlattenedArray(Class<?> arrayClass);
 197 
 198     /**
 199      * Fetches a reference value from a given Java variable.
 200      * This method can return a reference to either an object or value
 201      * or a null reference.
 202      *
 203      * @see #getInt(Object, long)
 204      */
 205     @HotSpotIntrinsicCandidate
 206     public native Object getObject(Object o, long offset);
 207 
 208     /**
 209      * Stores a reference value into a given Java variable.
 210      * This method can store a reference to either an object or value
 211      * or a null reference.
 212      * <p>
 213      * Unless the reference {@code x} being stored is either null
 214      * or matches the field type, the results are undefined.
 215      * If the reference {@code o} is non-null, card marks or
 216      * other store barriers for that object (if the VM requires them)
 217      * are updated.
 218      * @see #putInt(Object, long, int)
 219      */
 220     @HotSpotIntrinsicCandidate
 221     public native void putObject(Object o, long offset, Object x);
 222 
 223     /**
 224      * Fetches a value of type {@code <V>} from a given Java variable.
 225      * More specifically, fetches a field or array element within the given
 226      * {@code o} object at the given offset, or (if {@code o} is null)
 227      * from the memory address whose numerical value is the given offset.
 228      *
 229      * @param o Java heap object in which the variable resides, if any, else
 230      *        null
 231      * @param offset indication of where the variable resides in a Java heap
 232      *        object, if any, else a memory address locating the variable
 233      *        statically
 234      * @param vc value class
 235      * @param <V> the type of a value
 236      * @return the value fetched from the indicated Java variable
 237      * @throws RuntimeException No defined exceptions are thrown, not even
 238      *         {@link NullPointerException}
 239      */
 240     public native <V> V getValue(Object o, long offset, Class<?> vc);
 241 
 242     /**
 243      * Stores the given value into a given Java variable.
 244      *
 245      * Unless the reference {@code o} being stored is either null
 246      * or matches the field type and not in a value container,
 247      * the results are undefined.
 248      *
 249      * @param o Java heap object in which the variable resides, if any, else
 250      *        null
 251      * @param offset indication of where the variable resides in a Java heap
 252      *        object, if any, else a memory address locating the variable
 253      *        statically
 254      * @param vc value class
 255      * @param v the value to store into the indicated Java variable
 256      * @param <V> the type of a value
 257      * @throws RuntimeException No defined exceptions are thrown, not even
 258      *         {@link NullPointerException}
 259      */
 260     public native <V> void putValue(Object o, long offset, Class<?> vc, V v);
 261 
 262     /**
 263      * Starts a private buffer
 264      */
 265     public native <V> V startPrivateBuffer(V value);
 266 
 267     public native <V> V finishPrivateBuffer(V value);
 268 
 269     /**
 270      * Updates an int field of a value instance of type {@code V}
 271      * addressed by the given {@code o} object at the given offset
 272      * with {@code x} and returns the new value.
 273      *
 274      * Unless the reference {@code o} being stored is either null
 275      * or matches the field type, the results are undefined.
 276      *
 277      * @param vc Value class V
 278      * @param o Java heap object in which the variable resides, if any, else
 279      *        null
 280      * @param offset indication of where the variable resides in a Java heap
 281      *        object, if any, else a memory address locating the variable
 282      *        statically
 283      * @param x the value to store into the indicated Java variable
 284      * @param <V> Value class
 285      * @return an instance of the given value class
 286      */
 287     public native <V> V withInt(Class<V> vc, Object o, long offset, int x);
 288 
 289     /** @see #withInt(Class, Object, long, int)  */
 290     public native <V> V withBoolean(Class<V> vc, Object o, long offset, boolean x);
 291 
 292     /** @see #withInt(Class, Object, long, int)  */
 293     public native <V> V withByte(Class<V> vc, Object o, long offset, byte x);
 294 
 295     /** @see #withInt(Class, Object, long, int)  */
 296     public native <V> V withShort(Class<V> vc, Object o, long offset, short x);
 297 
 298     /** @see #withInt(Class, Object, long, int)  */
 299     public native <V> V withChar(Class<V> vc, Object o, long offset, char x);
 300 
 301     /** @see #withInt(Class, Object, long, int)  */
 302     public native <V> V withLong(Class<V> vc, Object o, long offset, long x);
 303 
 304     /** @see #withInt(Class, Object, long, int)  */
 305     public native <V> V withFloat(Class<V> vc, Object o, long offset, float x);
 306 
 307     /** @see #withInt(Class, Object, long, int)  */
 308     public native <V> V withDouble(Class<V> vc, Object o, long offset, double x);
 309 
 310     /** @see #withInt(Class, Object, long, int)  */
 311     public native <V> V withReference(Class<V> vc, Object o, long offset, Object x);
 312 
 313     /**
 314      * Updates a field of a value instance of type {@code V} addressed by
 315      * the given {@code o} object at the given offset with {@code x}
 316      * and returns the new value.
 317      *
 318      * Unless the reference {@code o} being stored is either null
 319      * or matches the field type, the results are undefined.
 320      *
 321      * @param vc Value class V
 322      * @param o Java heap object in which the variable resides, if any, else null
 323      * @param vc_offset indication of where the variable resides in a Java heap
 324      *        object, if any, else a memory address locating the variable
 325      *        statically
 326      * @param xc_offset offset from the field addressed by o and vc_offset
 327      * @param xc The type of the field to be updated
 328      * @param x the value to store into the indicated Java variable
 329      * @param <V> the containing class
 330      * @param <X> the field type
 331      * @return an instance of the given containing class
 332      */
 333     public native <V,X> V withValue(Class<V> vc, Object o, long vc_offset, long xc_offset, Class<X> xc, X x);
 334 
 335     /**
 336      * Returns the header size of the given value class
 337      *
 338      * @param vc Value class
 339      * @param <V> value clas
 340      * @return the header size of the value class
 341      */
 342     public native <V> long valueHeaderSize(Class<V> vc);
 343 
 344     /** @see #getInt(Object, long) */
 345     @HotSpotIntrinsicCandidate
 346     public native boolean getBoolean(Object o, long offset);
 347 
 348     /** @see #putInt(Object, long, int) */
 349     @HotSpotIntrinsicCandidate
 350     public native void    putBoolean(Object o, long offset, boolean x);
 351 
 352     /** @see #getInt(Object, long) */
 353     @HotSpotIntrinsicCandidate
 354     public native byte    getByte(Object o, long offset);
 355 
 356     /** @see #putInt(Object, long, int) */
 357     @HotSpotIntrinsicCandidate
 358     public native void    putByte(Object o, long offset, byte x);
 359 
 360     /** @see #getInt(Object, long) */
 361     @HotSpotIntrinsicCandidate
 362     public native short   getShort(Object o, long offset);
 363 
 364     /** @see #putInt(Object, long, int) */
 365     @HotSpotIntrinsicCandidate
 366     public native void    putShort(Object o, long offset, short x);
 367 
 368     /** @see #getInt(Object, long) */
 369     @HotSpotIntrinsicCandidate
 370     public native char    getChar(Object o, long offset);
 371 
 372     /** @see #putInt(Object, long, int) */
 373     @HotSpotIntrinsicCandidate
 374     public native void    putChar(Object o, long offset, char x);
 375 
 376     /** @see #getInt(Object, long) */
 377     @HotSpotIntrinsicCandidate
 378     public native long    getLong(Object o, long offset);
 379 
 380     /** @see #putInt(Object, long, int) */
 381     @HotSpotIntrinsicCandidate
 382     public native void    putLong(Object o, long offset, long x);
 383 
 384     /** @see #getInt(Object, long) */
 385     @HotSpotIntrinsicCandidate
 386     public native float   getFloat(Object o, long offset);
 387 
 388     /** @see #putInt(Object, long, int) */
 389     @HotSpotIntrinsicCandidate
 390     public native void    putFloat(Object o, long offset, float x);
 391 
 392     /** @see #getInt(Object, long) */
 393     @HotSpotIntrinsicCandidate
 394     public native double  getDouble(Object o, long offset);
 395 
 396     /** @see #putInt(Object, long, int) */
 397     @HotSpotIntrinsicCandidate
 398     public native void    putDouble(Object o, long offset, double x);
 399 
 400     /**
 401      * Fetches a native pointer from a given memory address.  If the address is
 402      * zero, or does not point into a block obtained from {@link
 403      * #allocateMemory}, the results are undefined.
 404      *
 405      * <p>If the native pointer is less than 64 bits wide, it is extended as
 406      * an unsigned number to a Java long.  The pointer may be indexed by any
 407      * given byte offset, simply by adding that offset (as a simple integer) to
 408      * the long representing the pointer.  The number of bytes actually read
 409      * from the target address may be determined by consulting {@link
 410      * #addressSize}.
 411      *
 412      * @see #allocateMemory
 413      * @see #getInt(Object, long)
 414      */
 415     @ForceInline
 416     public long getAddress(Object o, long offset) {
 417         if (ADDRESS_SIZE == 4) {
 418             return Integer.toUnsignedLong(getInt(o, offset));
 419         } else {
 420             return getLong(o, offset);
 421         }
 422     }
 423 
 424     /**
 425      * Stores a native pointer into a given memory address.  If the address is
 426      * zero, or does not point into a block obtained from {@link
 427      * #allocateMemory}, the results are undefined.
 428      *
 429      * <p>The number of bytes actually written at the target address may be
 430      * determined by consulting {@link #addressSize}.
 431      *
 432      * @see #allocateMemory
 433      * @see #putInt(Object, long, int)
 434      */
 435     @ForceInline
 436     public void putAddress(Object o, long offset, long x) {
 437         if (ADDRESS_SIZE == 4) {
 438             putInt(o, offset, (int)x);
 439         } else {
 440             putLong(o, offset, x);
 441         }
 442     }
 443 
 444     // These read VM internal data.
 445 
 446     /**
 447      * Fetches an uncompressed reference value from a given native variable
 448      * ignoring the VM's compressed references mode.
 449      *
 450      * @param address a memory address locating the variable
 451      * @return the value fetched from the indicated native variable
 452      */
 453     public native Object getUncompressedObject(long address);
 454 
 455     // These work on values in the C heap.
 456 
 457     /**
 458      * Fetches a value from a given memory address.  If the address is zero, or
 459      * does not point into a block obtained from {@link #allocateMemory}, the
 460      * results are undefined.
 461      *
 462      * @see #allocateMemory
 463      */
 464     @ForceInline
 465     public byte getByte(long address) {
 466         return getByte(null, address);
 467     }
 468 
 469     /**
 470      * Stores a value into a given memory address.  If the address is zero, or
 471      * does not point into a block obtained from {@link #allocateMemory}, the
 472      * results are undefined.
 473      *
 474      * @see #getByte(long)
 475      */
 476     @ForceInline
 477     public void putByte(long address, byte x) {
 478         putByte(null, address, x);
 479     }
 480 
 481     /** @see #getByte(long) */
 482     @ForceInline
 483     public short getShort(long address) {
 484         return getShort(null, address);
 485     }
 486 
 487     /** @see #putByte(long, byte) */
 488     @ForceInline
 489     public void putShort(long address, short x) {
 490         putShort(null, address, x);
 491     }
 492 
 493     /** @see #getByte(long) */
 494     @ForceInline
 495     public char getChar(long address) {
 496         return getChar(null, address);
 497     }
 498 
 499     /** @see #putByte(long, byte) */
 500     @ForceInline
 501     public void putChar(long address, char x) {
 502         putChar(null, address, x);
 503     }
 504 
 505     /** @see #getByte(long) */
 506     @ForceInline
 507     public int getInt(long address) {
 508         return getInt(null, address);
 509     }
 510 
 511     /** @see #putByte(long, byte) */
 512     @ForceInline
 513     public void putInt(long address, int x) {
 514         putInt(null, address, x);
 515     }
 516 
 517     /** @see #getByte(long) */
 518     @ForceInline
 519     public long getLong(long address) {
 520         return getLong(null, address);
 521     }
 522 
 523     /** @see #putByte(long, byte) */
 524     @ForceInline
 525     public void putLong(long address, long x) {
 526         putLong(null, address, x);
 527     }
 528 
 529     /** @see #getByte(long) */
 530     @ForceInline
 531     public float getFloat(long address) {
 532         return getFloat(null, address);
 533     }
 534 
 535     /** @see #putByte(long, byte) */
 536     @ForceInline
 537     public void putFloat(long address, float x) {
 538         putFloat(null, address, x);
 539     }
 540 
 541     /** @see #getByte(long) */
 542     @ForceInline
 543     public double getDouble(long address) {
 544         return getDouble(null, address);
 545     }
 546 
 547     /** @see #putByte(long, byte) */
 548     @ForceInline
 549     public void putDouble(long address, double x) {
 550         putDouble(null, address, x);
 551     }
 552 
 553     /** @see #getAddress(Object, long) */
 554     @ForceInline
 555     public long getAddress(long address) {
 556         return getAddress(null, address);
 557     }
 558 
 559     /** @see #putAddress(Object, long, long) */
 560     @ForceInline
 561     public void putAddress(long address, long x) {
 562         putAddress(null, address, x);
 563     }
 564 
 565 
 566 
 567     /// helper methods for validating various types of objects/values
 568 
 569     /**
 570      * Create an exception reflecting that some of the input was invalid
 571      *
 572      * <em>Note:</em> It is the resposibility of the caller to make
 573      * sure arguments are checked before the methods are called. While
 574      * some rudimentary checks are performed on the input, the checks
 575      * are best effort and when performance is an overriding priority,
 576      * as when methods of this class are optimized by the runtime
 577      * compiler, some or all checks (if any) may be elided. Hence, the
 578      * caller must not rely on the checks and corresponding
 579      * exceptions!
 580      *
 581      * @return an exception object
 582      */
 583     private RuntimeException invalidInput() {
 584         return new IllegalArgumentException();
 585     }
 586 
 587     /**
 588      * Check if a value is 32-bit clean (32 MSB are all zero)
 589      *
 590      * @param value the 64-bit value to check
 591      *
 592      * @return true if the value is 32-bit clean
 593      */
 594     private boolean is32BitClean(long value) {
 595         return value >>> 32 == 0;
 596     }
 597 
 598     /**
 599      * Check the validity of a size (the equivalent of a size_t)
 600      *
 601      * @throws RuntimeException if the size is invalid
 602      *         (<em>Note:</em> after optimization, invalid inputs may
 603      *         go undetected, which will lead to unpredictable
 604      *         behavior)
 605      */
 606     private void checkSize(long size) {
 607         if (ADDRESS_SIZE == 4) {
 608             // Note: this will also check for negative sizes
 609             if (!is32BitClean(size)) {
 610                 throw invalidInput();
 611             }
 612         } else if (size < 0) {
 613             throw invalidInput();
 614         }
 615     }
 616 
 617     /**
 618      * Check the validity of a native address (the equivalent of void*)
 619      *
 620      * @throws RuntimeException if the address is invalid
 621      *         (<em>Note:</em> after optimization, invalid inputs may
 622      *         go undetected, which will lead to unpredictable
 623      *         behavior)
 624      */
 625     private void checkNativeAddress(long address) {
 626         if (ADDRESS_SIZE == 4) {
 627             // Accept both zero and sign extended pointers. A valid
 628             // pointer will, after the +1 below, either have produced
 629             // the value 0x0 or 0x1. Masking off the low bit allows
 630             // for testing against 0.
 631             if ((((address >> 32) + 1) & ~1) != 0) {
 632                 throw invalidInput();
 633             }
 634         }
 635     }
 636 
 637     /**
 638      * Check the validity of an offset, relative to a base object
 639      *
 640      * @param o the base object
 641      * @param offset the offset to check
 642      *
 643      * @throws RuntimeException if the size is invalid
 644      *         (<em>Note:</em> after optimization, invalid inputs may
 645      *         go undetected, which will lead to unpredictable
 646      *         behavior)
 647      */
 648     private void checkOffset(Object o, long offset) {
 649         if (ADDRESS_SIZE == 4) {
 650             // Note: this will also check for negative offsets
 651             if (!is32BitClean(offset)) {
 652                 throw invalidInput();
 653             }
 654         } else if (offset < 0) {
 655             throw invalidInput();
 656         }
 657     }
 658 
 659     /**
 660      * Check the validity of a double-register pointer
 661      *
 662      * Note: This code deliberately does *not* check for NPE for (at
 663      * least) three reasons:
 664      *
 665      * 1) NPE is not just NULL/0 - there is a range of values all
 666      * resulting in an NPE, which is not trivial to check for
 667      *
 668      * 2) It is the responsibility of the callers of Unsafe methods
 669      * to verify the input, so throwing an exception here is not really
 670      * useful - passing in a NULL pointer is a critical error and the
 671      * must not expect an exception to be thrown anyway.
 672      *
 673      * 3) the actual operations will detect NULL pointers anyway by
 674      * means of traps and signals (like SIGSEGV).
 675      *
 676      * @param o Java heap object, or null
 677      * @param offset indication of where the variable resides in a Java heap
 678      *        object, if any, else a memory address locating the variable
 679      *        statically
 680      *
 681      * @throws RuntimeException if the pointer is invalid
 682      *         (<em>Note:</em> after optimization, invalid inputs may
 683      *         go undetected, which will lead to unpredictable
 684      *         behavior)
 685      */
 686     private void checkPointer(Object o, long offset) {
 687         if (o == null) {
 688             checkNativeAddress(offset);
 689         } else {
 690             checkOffset(o, offset);
 691         }
 692     }
 693 
 694     /**
 695      * Check if a type is a primitive array type
 696      *
 697      * @param c the type to check
 698      *
 699      * @return true if the type is a primitive array type
 700      */
 701     private void checkPrimitiveArray(Class<?> c) {
 702         Class<?> componentType = c.getComponentType();
 703         if (componentType == null || !componentType.isPrimitive()) {
 704             throw invalidInput();
 705         }
 706     }
 707 
 708     /**
 709      * Check that a pointer is a valid primitive array type pointer
 710      *
 711      * Note: pointers off-heap are considered to be primitive arrays
 712      *
 713      * @throws RuntimeException if the pointer is invalid
 714      *         (<em>Note:</em> after optimization, invalid inputs may
 715      *         go undetected, which will lead to unpredictable
 716      *         behavior)
 717      */
 718     private void checkPrimitivePointer(Object o, long offset) {
 719         checkPointer(o, offset);
 720 
 721         if (o != null) {
 722             // If on heap, it must be a primitive array
 723             checkPrimitiveArray(o.getClass());
 724         }
 725     }
 726 
 727 
 728     /// wrappers for malloc, realloc, free:
 729 
 730     /**
 731      * Allocates a new block of native memory, of the given size in bytes.  The
 732      * contents of the memory are uninitialized; they will generally be
 733      * garbage.  The resulting native pointer will never be zero, and will be
 734      * aligned for all value types.  Dispose of this memory by calling {@link
 735      * #freeMemory}, or resize it with {@link #reallocateMemory}.
 736      *
 737      * <em>Note:</em> It is the resposibility of the caller to make
 738      * sure arguments are checked before the methods are called. While
 739      * some rudimentary checks are performed on the input, the checks
 740      * are best effort and when performance is an overriding priority,
 741      * as when methods of this class are optimized by the runtime
 742      * compiler, some or all checks (if any) may be elided. Hence, the
 743      * caller must not rely on the checks and corresponding
 744      * exceptions!
 745      *
 746      * @throws RuntimeException if the size is negative or too large
 747      *         for the native size_t type
 748      *
 749      * @throws OutOfMemoryError if the allocation is refused by the system
 750      *
 751      * @see #getByte(long)
 752      * @see #putByte(long, byte)
 753      */
 754     public long allocateMemory(long bytes) {
 755         allocateMemoryChecks(bytes);
 756 
 757         if (bytes == 0) {
 758             return 0;
 759         }
 760 
 761         long p = allocateMemory0(bytes);
 762         if (p == 0) {
 763             throw new OutOfMemoryError();
 764         }
 765 
 766         return p;
 767     }
 768 
 769     /**
 770      * Validate the arguments to allocateMemory
 771      *
 772      * @throws RuntimeException if the arguments are invalid
 773      *         (<em>Note:</em> after optimization, invalid inputs may
 774      *         go undetected, which will lead to unpredictable
 775      *         behavior)
 776      */
 777     private void allocateMemoryChecks(long bytes) {
 778         checkSize(bytes);
 779     }
 780 
 781     /**
 782      * Resizes a new block of native memory, to the given size in bytes.  The
 783      * contents of the new block past the size of the old block are
 784      * uninitialized; they will generally be garbage.  The resulting native
 785      * pointer will be zero if and only if the requested size is zero.  The
 786      * resulting native pointer will be aligned for all value types.  Dispose
 787      * of this memory by calling {@link #freeMemory}, or resize it with {@link
 788      * #reallocateMemory}.  The address passed to this method may be null, in
 789      * which case an allocation will be performed.
 790      *
 791      * <em>Note:</em> It is the resposibility of the caller to make
 792      * sure arguments are checked before the methods are called. While
 793      * some rudimentary checks are performed on the input, the checks
 794      * are best effort and when performance is an overriding priority,
 795      * as when methods of this class are optimized by the runtime
 796      * compiler, some or all checks (if any) may be elided. Hence, the
 797      * caller must not rely on the checks and corresponding
 798      * exceptions!
 799      *
 800      * @throws RuntimeException if the size is negative or too large
 801      *         for the native size_t type
 802      *
 803      * @throws OutOfMemoryError if the allocation is refused by the system
 804      *
 805      * @see #allocateMemory
 806      */
 807     public long reallocateMemory(long address, long bytes) {
 808         reallocateMemoryChecks(address, bytes);
 809 
 810         if (bytes == 0) {
 811             freeMemory(address);
 812             return 0;
 813         }
 814 
 815         long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
 816         if (p == 0) {
 817             throw new OutOfMemoryError();
 818         }
 819 
 820         return p;
 821     }
 822 
 823     /**
 824      * Validate the arguments to reallocateMemory
 825      *
 826      * @throws RuntimeException if the arguments are invalid
 827      *         (<em>Note:</em> after optimization, invalid inputs may
 828      *         go undetected, which will lead to unpredictable
 829      *         behavior)
 830      */
 831     private void reallocateMemoryChecks(long address, long bytes) {
 832         checkPointer(null, address);
 833         checkSize(bytes);
 834     }
 835 
 836     /**
 837      * Sets all bytes in a given block of memory to a fixed value
 838      * (usually zero).
 839      *
 840      * <p>This method determines a block's base address by means of two parameters,
 841      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 842      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 843      * the offset supplies an absolute base address.
 844      *
 845      * <p>The stores are in coherent (atomic) units of a size determined
 846      * by the address and length parameters.  If the effective address and
 847      * length are all even modulo 8, the stores take place in 'long' units.
 848      * If the effective address and length are (resp.) even modulo 4 or 2,
 849      * the stores take place in units of 'int' or 'short'.
 850      *
 851      * <em>Note:</em> It is the resposibility of the caller to make
 852      * sure arguments are checked before the methods are called. While
 853      * some rudimentary checks are performed on the input, the checks
 854      * are best effort and when performance is an overriding priority,
 855      * as when methods of this class are optimized by the runtime
 856      * compiler, some or all checks (if any) may be elided. Hence, the
 857      * caller must not rely on the checks and corresponding
 858      * exceptions!
 859      *
 860      * @throws RuntimeException if any of the arguments is invalid
 861      *
 862      * @since 1.7
 863      */
 864     public void setMemory(Object o, long offset, long bytes, byte value) {
 865         setMemoryChecks(o, offset, bytes, value);
 866 
 867         if (bytes == 0) {
 868             return;
 869         }
 870 
 871         setMemory0(o, offset, bytes, value);
 872     }
 873 
 874     /**
 875      * Sets all bytes in a given block of memory to a fixed value
 876      * (usually zero).  This provides a <em>single-register</em> addressing mode,
 877      * as discussed in {@link #getInt(Object,long)}.
 878      *
 879      * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
 880      */
 881     public void setMemory(long address, long bytes, byte value) {
 882         setMemory(null, address, bytes, value);
 883     }
 884 
 885     /**
 886      * Validate the arguments to setMemory
 887      *
 888      * @throws RuntimeException if the arguments are invalid
 889      *         (<em>Note:</em> after optimization, invalid inputs may
 890      *         go undetected, which will lead to unpredictable
 891      *         behavior)
 892      */
 893     private void setMemoryChecks(Object o, long offset, long bytes, byte value) {
 894         checkPrimitivePointer(o, offset);
 895         checkSize(bytes);
 896     }
 897 
 898     /**
 899      * Sets all bytes in a given block of memory to a copy of another
 900      * block.
 901      *
 902      * <p>This method determines each block's base address by means of two parameters,
 903      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 904      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 905      * the offset supplies an absolute base address.
 906      *
 907      * <p>The transfers are in coherent (atomic) units of a size determined
 908      * by the address and length parameters.  If the effective addresses and
 909      * length are all even modulo 8, the transfer takes place in 'long' units.
 910      * If the effective addresses and length are (resp.) even modulo 4 or 2,
 911      * the transfer takes place in units of 'int' or 'short'.
 912      *
 913      * <em>Note:</em> It is the resposibility of the caller to make
 914      * sure arguments are checked before the methods are called. While
 915      * some rudimentary checks are performed on the input, the checks
 916      * are best effort and when performance is an overriding priority,
 917      * as when methods of this class are optimized by the runtime
 918      * compiler, some or all checks (if any) may be elided. Hence, the
 919      * caller must not rely on the checks and corresponding
 920      * exceptions!
 921      *
 922      * @throws RuntimeException if any of the arguments is invalid
 923      *
 924      * @since 1.7
 925      */
 926     public void copyMemory(Object srcBase, long srcOffset,
 927                            Object destBase, long destOffset,
 928                            long bytes) {
 929         copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
 930 
 931         if (bytes == 0) {
 932             return;
 933         }
 934 
 935         copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
 936     }
 937 
 938     /**
 939      * Sets all bytes in a given block of memory to a copy of another
 940      * block.  This provides a <em>single-register</em> addressing mode,
 941      * as discussed in {@link #getInt(Object,long)}.
 942      *
 943      * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
 944      */
 945     public void copyMemory(long srcAddress, long destAddress, long bytes) {
 946         copyMemory(null, srcAddress, null, destAddress, bytes);
 947     }
 948 
 949     /**
 950      * Validate the arguments to copyMemory
 951      *
 952      * @throws RuntimeException if any of the arguments is invalid
 953      *         (<em>Note:</em> after optimization, invalid inputs may
 954      *         go undetected, which will lead to unpredictable
 955      *         behavior)
 956      */
 957     private void copyMemoryChecks(Object srcBase, long srcOffset,
 958                                   Object destBase, long destOffset,
 959                                   long bytes) {
 960         checkSize(bytes);
 961         checkPrimitivePointer(srcBase, srcOffset);
 962         checkPrimitivePointer(destBase, destOffset);
 963     }
 964 
 965     /**
 966      * Copies all elements from one block of memory to another block,
 967      * *unconditionally* byte swapping the elements on the fly.
 968      *
 969      * <p>This method determines each block's base address by means of two parameters,
 970      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 971      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 972      * the offset supplies an absolute base address.
 973      *
 974      * <em>Note:</em> It is the resposibility of the caller to make
 975      * sure arguments are checked before the methods are called. While
 976      * some rudimentary checks are performed on the input, the checks
 977      * are best effort and when performance is an overriding priority,
 978      * as when methods of this class are optimized by the runtime
 979      * compiler, some or all checks (if any) may be elided. Hence, the
 980      * caller must not rely on the checks and corresponding
 981      * exceptions!
 982      *
 983      * @throws RuntimeException if any of the arguments is invalid
 984      *
 985      * @since 9
 986      */
 987     public void copySwapMemory(Object srcBase, long srcOffset,
 988                                Object destBase, long destOffset,
 989                                long bytes, long elemSize) {
 990         copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 991 
 992         if (bytes == 0) {
 993             return;
 994         }
 995 
 996         copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 997     }
 998 
 999     private void copySwapMemoryChecks(Object srcBase, long srcOffset,
1000                                       Object destBase, long destOffset,
1001                                       long bytes, long elemSize) {
1002         checkSize(bytes);
1003 
1004         if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
1005             throw invalidInput();
1006         }
1007         if (bytes % elemSize != 0) {
1008             throw invalidInput();
1009         }
1010 
1011         checkPrimitivePointer(srcBase, srcOffset);
1012         checkPrimitivePointer(destBase, destOffset);
1013     }
1014 
1015    /**
1016      * Copies all elements from one block of memory to another block, byte swapping the
1017      * elements on the fly.
1018      *
1019      * This provides a <em>single-register</em> addressing mode, as
1020      * discussed in {@link #getInt(Object,long)}.
1021      *
1022      * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
1023      */
1024     public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
1025         copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
1026     }
1027 
1028     /**
1029      * Disposes of a block of native memory, as obtained from {@link
1030      * #allocateMemory} or {@link #reallocateMemory}.  The address passed to
1031      * this method may be null, in which case no action is taken.
1032      *
1033      * <em>Note:</em> It is the resposibility of the caller to make
1034      * sure arguments are checked before the methods are called. While
1035      * some rudimentary checks are performed on the input, the checks
1036      * are best effort and when performance is an overriding priority,
1037      * as when methods of this class are optimized by the runtime
1038      * compiler, some or all checks (if any) may be elided. Hence, the
1039      * caller must not rely on the checks and corresponding
1040      * exceptions!
1041      *
1042      * @throws RuntimeException if any of the arguments is invalid
1043      *
1044      * @see #allocateMemory
1045      */
1046     public void freeMemory(long address) {
1047         freeMemoryChecks(address);
1048 
1049         if (address == 0) {
1050             return;
1051         }
1052 
1053         freeMemory0(address);
1054     }
1055 
1056     /**
1057      * Validate the arguments to freeMemory
1058      *
1059      * @throws RuntimeException if the arguments are invalid
1060      *         (<em>Note:</em> after optimization, invalid inputs may
1061      *         go undetected, which will lead to unpredictable
1062      *         behavior)
1063      */
1064     private void freeMemoryChecks(long address) {
1065         checkPointer(null, address);
1066     }
1067 
1068     /// random queries
1069 
1070     /**
1071      * This constant differs from all results that will ever be returned from
1072      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1073      * or {@link #arrayBaseOffset}.
1074      */
1075     public static final int INVALID_FIELD_OFFSET = -1;
1076 
1077     /**
1078      * Reports the location of a given field in the storage allocation of its
1079      * class.  Do not expect to perform any sort of arithmetic on this offset;
1080      * it is just a cookie which is passed to the unsafe heap memory accessors.
1081      *
1082      * <p>Any given field will always have the same offset and base, and no
1083      * two distinct fields of the same class will ever have the same offset
1084      * and base.
1085      *
1086      * <p>As of 1.4.1, offsets for fields are represented as long values,
1087      * although the Sun JVM does not use the most significant 32 bits.
1088      * However, JVM implementations which store static fields at absolute
1089      * addresses can use long offsets and null base pointers to express
1090      * the field locations in a form usable by {@link #getInt(Object,long)}.
1091      * Therefore, code which will be ported to such JVMs on 64-bit platforms
1092      * must preserve all bits of static field offsets.
1093      * @see #getInt(Object, long)
1094      */
1095     public long objectFieldOffset(Field f) {
1096         if (f == null) {
1097             throw new NullPointerException();
1098         }
1099 
1100         return objectFieldOffset0(f);
1101     }
1102 
1103     /**
1104      * Reports the location of the field with a given name in the storage
1105      * allocation of its class.
1106      *
1107      * @throws NullPointerException if any parameter is {@code null}.
1108      * @throws InternalError if there is no field named {@code name} declared
1109      *         in class {@code c}, i.e., if {@code c.getDeclaredField(name)}
1110      *         would throw {@code java.lang.NoSuchFieldException}.
1111      *
1112      * @see #objectFieldOffset(Field)
1113      */
1114     public long objectFieldOffset(Class<?> c, String name) {
1115         if (c == null || name == null) {
1116             throw new NullPointerException();
1117         }
1118 
1119         return objectFieldOffset1(c, name);
1120     }
1121 
1122     /**
1123      * Reports the location of a given static field, in conjunction with {@link
1124      * #staticFieldBase}.
1125      * <p>Do not expect to perform any sort of arithmetic on this offset;
1126      * it is just a cookie which is passed to the unsafe heap memory accessors.
1127      *
1128      * <p>Any given field will always have the same offset, and no two distinct
1129      * fields of the same class will ever have the same offset.
1130      *
1131      * <p>As of 1.4.1, offsets for fields are represented as long values,
1132      * although the Sun JVM does not use the most significant 32 bits.
1133      * It is hard to imagine a JVM technology which needs more than
1134      * a few bits to encode an offset within a non-array object,
1135      * However, for consistency with other methods in this class,
1136      * this method reports its result as a long value.
1137      * @see #getInt(Object, long)
1138      */
1139     public long staticFieldOffset(Field f) {
1140         if (f == null) {
1141             throw new NullPointerException();
1142         }
1143 
1144         return staticFieldOffset0(f);
1145     }
1146 
1147     /**
1148      * Reports the location of a given static field, in conjunction with {@link
1149      * #staticFieldOffset}.
1150      * <p>Fetch the base "Object", if any, with which static fields of the
1151      * given class can be accessed via methods like {@link #getInt(Object,
1152      * long)}.  This value may be null.  This value may refer to an object
1153      * which is a "cookie", not guaranteed to be a real Object, and it should
1154      * not be used in any way except as argument to the get and put routines in
1155      * this class.
1156      */
1157     public Object staticFieldBase(Field f) {
1158         if (f == null) {
1159             throw new NullPointerException();
1160         }
1161 
1162         return staticFieldBase0(f);
1163     }
1164 
1165     /**
1166      * Detects if the given class may need to be initialized. This is often
1167      * needed in conjunction with obtaining the static field base of a
1168      * class.
1169      * @return false only if a call to {@code ensureClassInitialized} would have no effect
1170      */
1171     public boolean shouldBeInitialized(Class<?> c) {
1172         if (c == null) {
1173             throw new NullPointerException();
1174         }
1175 
1176         return shouldBeInitialized0(c);
1177     }
1178 
1179     /**
1180      * Ensures the given class has been initialized. This is often
1181      * needed in conjunction with obtaining the static field base of a
1182      * class.
1183      */
1184     public void ensureClassInitialized(Class<?> c) {
1185         if (c == null) {
1186             throw new NullPointerException();
1187         }
1188 
1189         ensureClassInitialized0(c);
1190     }
1191 
1192     /**
1193      * Reports the offset of the first element in the storage allocation of a
1194      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
1195      * for the same class, you may use that scale factor, together with this
1196      * base offset, to form new offsets to access elements of arrays of the
1197      * given class.
1198      *
1199      * @see #getInt(Object, long)
1200      * @see #putInt(Object, long, int)
1201      */
1202     public int arrayBaseOffset(Class<?> arrayClass) {
1203         if (arrayClass == null) {
1204             throw new NullPointerException();
1205         }
1206 
1207         return arrayBaseOffset0(arrayClass);
1208     }
1209 
1210 
1211     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1212     public static final int ARRAY_BOOLEAN_BASE_OFFSET
1213             = theUnsafe.arrayBaseOffset(boolean[].class);
1214 
1215     /** The value of {@code arrayBaseOffset(byte[].class)} */
1216     public static final int ARRAY_BYTE_BASE_OFFSET
1217             = theUnsafe.arrayBaseOffset(byte[].class);
1218 
1219     /** The value of {@code arrayBaseOffset(short[].class)} */
1220     public static final int ARRAY_SHORT_BASE_OFFSET
1221             = theUnsafe.arrayBaseOffset(short[].class);
1222 
1223     /** The value of {@code arrayBaseOffset(char[].class)} */
1224     public static final int ARRAY_CHAR_BASE_OFFSET
1225             = theUnsafe.arrayBaseOffset(char[].class);
1226 
1227     /** The value of {@code arrayBaseOffset(int[].class)} */
1228     public static final int ARRAY_INT_BASE_OFFSET
1229             = theUnsafe.arrayBaseOffset(int[].class);
1230 
1231     /** The value of {@code arrayBaseOffset(long[].class)} */
1232     public static final int ARRAY_LONG_BASE_OFFSET
1233             = theUnsafe.arrayBaseOffset(long[].class);
1234 
1235     /** The value of {@code arrayBaseOffset(float[].class)} */
1236     public static final int ARRAY_FLOAT_BASE_OFFSET
1237             = theUnsafe.arrayBaseOffset(float[].class);
1238 
1239     /** The value of {@code arrayBaseOffset(double[].class)} */
1240     public static final int ARRAY_DOUBLE_BASE_OFFSET
1241             = theUnsafe.arrayBaseOffset(double[].class);
1242 
1243     /** The value of {@code arrayBaseOffset(Object[].class)} */
1244     public static final int ARRAY_OBJECT_BASE_OFFSET
1245             = theUnsafe.arrayBaseOffset(Object[].class);
1246 
1247     /**
1248      * Reports the scale factor for addressing elements in the storage
1249      * allocation of a given array class.  However, arrays of "narrow" types
1250      * will generally not work properly with accessors like {@link
1251      * #getByte(Object, long)}, so the scale factor for such classes is reported
1252      * as zero.
1253      *
1254      * @see #arrayBaseOffset
1255      * @see #getInt(Object, long)
1256      * @see #putInt(Object, long, int)
1257      */
1258     public int arrayIndexScale(Class<?> arrayClass) {
1259         if (arrayClass == null) {
1260             throw new NullPointerException();
1261         }
1262 
1263         return arrayIndexScale0(arrayClass);
1264     }
1265 
1266 
1267     /** The value of {@code arrayIndexScale(boolean[].class)} */
1268     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1269             = theUnsafe.arrayIndexScale(boolean[].class);
1270 
1271     /** The value of {@code arrayIndexScale(byte[].class)} */
1272     public static final int ARRAY_BYTE_INDEX_SCALE
1273             = theUnsafe.arrayIndexScale(byte[].class);
1274 
1275     /** The value of {@code arrayIndexScale(short[].class)} */
1276     public static final int ARRAY_SHORT_INDEX_SCALE
1277             = theUnsafe.arrayIndexScale(short[].class);
1278 
1279     /** The value of {@code arrayIndexScale(char[].class)} */
1280     public static final int ARRAY_CHAR_INDEX_SCALE
1281             = theUnsafe.arrayIndexScale(char[].class);
1282 
1283     /** The value of {@code arrayIndexScale(int[].class)} */
1284     public static final int ARRAY_INT_INDEX_SCALE
1285             = theUnsafe.arrayIndexScale(int[].class);
1286 
1287     /** The value of {@code arrayIndexScale(long[].class)} */
1288     public static final int ARRAY_LONG_INDEX_SCALE
1289             = theUnsafe.arrayIndexScale(long[].class);
1290 
1291     /** The value of {@code arrayIndexScale(float[].class)} */
1292     public static final int ARRAY_FLOAT_INDEX_SCALE
1293             = theUnsafe.arrayIndexScale(float[].class);
1294 
1295     /** The value of {@code arrayIndexScale(double[].class)} */
1296     public static final int ARRAY_DOUBLE_INDEX_SCALE
1297             = theUnsafe.arrayIndexScale(double[].class);
1298 
1299     /** The value of {@code arrayIndexScale(Object[].class)} */
1300     public static final int ARRAY_OBJECT_INDEX_SCALE
1301             = theUnsafe.arrayIndexScale(Object[].class);
1302 
1303     /**
1304      * Reports the size in bytes of a native pointer, as stored via {@link
1305      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1306      * other primitive types (as stored in native memory blocks) is determined
1307      * fully by their information content.
1308      */
1309     public int addressSize() {
1310         return ADDRESS_SIZE;
1311     }
1312 
1313     /** The value of {@code addressSize()} */
1314     public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1315 
1316     /**
1317      * Reports the size in bytes of a native memory page (whatever that is).
1318      * This value will always be a power of two.
1319      */
1320     public native int pageSize();
1321 
1322 
1323     /// random trusted operations from JNI:
1324 
1325     /**
1326      * Tells the VM to define a class, without security checks.  By default, the
1327      * class loader and protection domain come from the caller's class.
1328      */
1329     public Class<?> defineClass(String name, byte[] b, int off, int len,
1330                                 ClassLoader loader,
1331                                 ProtectionDomain protectionDomain) {
1332         if (b == null) {
1333             throw new NullPointerException();
1334         }
1335         if (len < 0) {
1336             throw new ArrayIndexOutOfBoundsException();
1337         }
1338 
1339         return defineClass0(name, b, off, len, loader, protectionDomain);
1340     }
1341 
1342     public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1343                                         ClassLoader loader,
1344                                         ProtectionDomain protectionDomain);
1345 
1346     /**
1347      * Defines a class but does not make it known to the class loader or system dictionary.
1348      * <p>
1349      * For each CP entry, the corresponding CP patch must either be null or have
1350      * the a format that matches its tag:
1351      * <ul>
1352      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
1353      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
1354      * <li>Class: any java.lang.Class object
1355      * <li>String: any object (not just a java.lang.String)
1356      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1357      * </ul>
1358      * @param hostClass context for linkage, access control, protection domain, and class loader
1359      * @param data      bytes of a class file
1360      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
1361      */
1362     public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
1363         if (hostClass == null || data == null) {
1364             throw new NullPointerException();
1365         }
1366         if (hostClass.isArray() || hostClass.isPrimitive()) {
1367             throw new IllegalArgumentException();
1368         }
1369 
1370         return defineAnonymousClass0(hostClass, data, cpPatches);
1371     }
1372 
1373     /**
1374      * Allocates an instance but does not run any constructor.
1375      * Initializes the class if it has not yet been.
1376      */
1377     @HotSpotIntrinsicCandidate
1378     public native Object allocateInstance(Class<?> cls)
1379         throws InstantiationException;
1380 
1381     /**
1382      * Allocates an array of a given type, but does not do zeroing.
1383      * <p>
1384      * This method should only be used in the very rare cases where a high-performance code
1385      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1386      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1387      * <p>
1388      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1389      * before allowing untrusted code, or code in other threads, to observe the reference
1390      * to the newly allocated array. In addition, the publication of the array reference must be
1391      * safe according to the Java Memory Model requirements.
1392      * <p>
1393      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1394      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1395      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1396      * or issuing a {@link #storeFence} before publishing the reference.
1397      * <p>
1398      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1399      * elements that could break heap integrity.
1400      *
1401      * @param componentType array component type to allocate
1402      * @param length array size to allocate
1403      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1404      *                                  or the length is negative
1405      */
1406     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1407        if (componentType == null) {
1408            throw new IllegalArgumentException("Component type is null");
1409        }
1410        if (!componentType.isPrimitive()) {
1411            throw new IllegalArgumentException("Component type is not primitive");
1412        }
1413        if (length < 0) {
1414            throw new IllegalArgumentException("Negative length");
1415        }
1416        return allocateUninitializedArray0(componentType, length);
1417     }
1418 
1419     @HotSpotIntrinsicCandidate
1420     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1421        // These fallbacks provide zeroed arrays, but intrinsic is not required to
1422        // return the zeroed arrays.
1423        if (componentType == byte.class)    return new byte[length];
1424        if (componentType == boolean.class) return new boolean[length];
1425        if (componentType == short.class)   return new short[length];
1426        if (componentType == char.class)    return new char[length];
1427        if (componentType == int.class)     return new int[length];
1428        if (componentType == float.class)   return new float[length];
1429        if (componentType == long.class)    return new long[length];
1430        if (componentType == double.class)  return new double[length];
1431        return null;
1432     }
1433 
1434     /** Throws the exception without telling the verifier. */
1435     public native void throwException(Throwable ee);
1436 
1437     /**
1438      * Atomically updates Java variable to {@code x} if it is currently
1439      * holding {@code expected}.
1440      *
1441      * <p>This operation has memory semantics of a {@code volatile} read
1442      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1443      *
1444      * @return {@code true} if successful
1445      */
1446     @HotSpotIntrinsicCandidate
1447     public final native boolean compareAndSetObject(Object o, long offset,
1448                                                     Object expected,
1449                                                     Object x);
1450 
1451     @ForceInline
1452     public final <V> boolean compareAndSetValue(Object o, long offset,
1453                                                 Class<?> valueType,
1454                                                 V expected,
1455                                                 V x) {
1456         synchronized (valueLock) {
1457             Object witness = getValue(o, offset, valueType);
1458             if (witness.equals(expected)) {
1459                 putValue(o, offset, valueType, x);
1460                 return true;
1461             }
1462             else {
1463                 return false;
1464             }
1465         }
1466     }
1467 
1468     @HotSpotIntrinsicCandidate
1469     public final native Object compareAndExchangeObject(Object o, long offset,
1470                                                         Object expected,
1471                                                         Object x);
1472     @ForceInline
1473     public final <V> Object compareAndExchangeValue(Object o, long offset,
1474                                                     Class<?> valueType,
1475                                                     V expected,
1476                                                     V x) {
1477         synchronized (valueLock) {
1478             Object witness = getValue(o, offset, valueType);
1479             if (witness.equals(expected)) {
1480                 putValue(o, offset, valueType, x);
1481             }
1482             return witness;
1483         }
1484     }
1485 
1486     @HotSpotIntrinsicCandidate
1487     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
1488                                                         Object expected,
1489                                                         Object x) {
1490         return compareAndExchangeObject(o, offset, expected, x);
1491     }
1492 
1493     @ForceInline
1494     public final <V> Object compareAndExchangeValueAcquire(Object o, long offset,
1495                                                            Class<?> valueType,
1496                                                            V expected,
1497                                                            V x) {
1498         return compareAndExchangeValue(o, offset, valueType, expected, x);
1499     }
1500 
1501     @HotSpotIntrinsicCandidate
1502     public final Object compareAndExchangeObjectRelease(Object o, long offset,
1503                                                         Object expected,
1504                                                         Object x) {
1505         return compareAndExchangeObject(o, offset, expected, x);
1506     }
1507 
1508     @ForceInline
1509     public final <V> Object compareAndExchangeValueRelease(Object o, long offset,
1510                                                            Class<?> valueType,
1511                                                            V expected,
1512                                                            V x) {
1513         return compareAndExchangeValue(o, offset, valueType, expected, x);
1514     }
1515 
1516     @HotSpotIntrinsicCandidate
1517     public final boolean weakCompareAndSetObjectPlain(Object o, long offset,
1518                                                       Object expected,
1519                                                       Object x) {
1520         return compareAndSetObject(o, offset, expected, x);
1521     }
1522 
1523     @ForceInline
1524     public final <V> boolean weakCompareAndSetValuePlain(Object o, long offset,
1525                                                          Class<?> valueType,
1526                                                          V expected,
1527                                                          V x) {
1528         return compareAndSetValue(o, offset, valueType, expected, x);
1529     }
1530 
1531     @HotSpotIntrinsicCandidate
1532     public final boolean weakCompareAndSetObjectAcquire(Object o, long offset,
1533                                                         Object expected,
1534                                                         Object x) {
1535         return compareAndSetObject(o, offset, expected, x);
1536     }
1537 
1538     @ForceInline
1539     public final <V> boolean weakCompareAndSetValueAcquire(Object o, long offset,
1540                                                            Class<?> valueType,
1541                                                            V expected,
1542                                                            V x) {
1543         return compareAndSetValue(o, offset, valueType, expected, x);
1544     }
1545 
1546     @HotSpotIntrinsicCandidate
1547     public final boolean weakCompareAndSetObjectRelease(Object o, long offset,
1548                                                         Object expected,
1549                                                         Object x) {
1550         return compareAndSetObject(o, offset, expected, x);
1551     }
1552 
1553     @ForceInline
1554     public final <V> boolean weakCompareAndSetValueRelease(Object o, long offset,
1555                                                            Class<?> valueType,
1556                                                            V expected,
1557                                                            V x) {
1558         return compareAndSetValue(o, offset, valueType, expected, x);
1559     }
1560 
1561     @HotSpotIntrinsicCandidate
1562     public final boolean weakCompareAndSetObject(Object o, long offset,
1563                                                  Object expected,
1564                                                  Object x) {
1565         return compareAndSetObject(o, offset, expected, x);
1566     }
1567 
1568     @ForceInline
1569     public final <V> boolean weakCompareAndSetValue(Object o, long offset,
1570                                                     Class<?> valueType,
1571                                                     V expected,
1572                                                     V x) {
1573         return compareAndSetValue(o, offset, valueType, expected, x);
1574     }
1575 
1576     /**
1577      * Atomically updates Java variable to {@code x} if it is currently
1578      * holding {@code expected}.
1579      *
1580      * <p>This operation has memory semantics of a {@code volatile} read
1581      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1582      *
1583      * @return {@code true} if successful
1584      */
1585     @HotSpotIntrinsicCandidate
1586     public final native boolean compareAndSetInt(Object o, long offset,
1587                                                  int expected,
1588                                                  int x);
1589 
1590     @HotSpotIntrinsicCandidate
1591     public final native int compareAndExchangeInt(Object o, long offset,
1592                                                   int expected,
1593                                                   int x);
1594 
1595     @HotSpotIntrinsicCandidate
1596     public final int compareAndExchangeIntAcquire(Object o, long offset,
1597                                                          int expected,
1598                                                          int x) {
1599         return compareAndExchangeInt(o, offset, expected, x);
1600     }
1601 
1602     @HotSpotIntrinsicCandidate
1603     public final int compareAndExchangeIntRelease(Object o, long offset,
1604                                                          int expected,
1605                                                          int x) {
1606         return compareAndExchangeInt(o, offset, expected, x);
1607     }
1608 
1609     @HotSpotIntrinsicCandidate
1610     public final boolean weakCompareAndSetIntPlain(Object o, long offset,
1611                                                    int expected,
1612                                                    int x) {
1613         return compareAndSetInt(o, offset, expected, x);
1614     }
1615 
1616     @HotSpotIntrinsicCandidate
1617     public final boolean weakCompareAndSetIntAcquire(Object o, long offset,
1618                                                      int expected,
1619                                                      int x) {
1620         return compareAndSetInt(o, offset, expected, x);
1621     }
1622 
1623     @HotSpotIntrinsicCandidate
1624     public final boolean weakCompareAndSetIntRelease(Object o, long offset,
1625                                                      int expected,
1626                                                      int x) {
1627         return compareAndSetInt(o, offset, expected, x);
1628     }
1629 
1630     @HotSpotIntrinsicCandidate
1631     public final boolean weakCompareAndSetInt(Object o, long offset,
1632                                               int expected,
1633                                               int x) {
1634         return compareAndSetInt(o, offset, expected, x);
1635     }
1636 
1637     @HotSpotIntrinsicCandidate
1638     public final byte compareAndExchangeByte(Object o, long offset,
1639                                              byte expected,
1640                                              byte x) {
1641         long wordOffset = offset & ~3;
1642         int shift = (int) (offset & 3) << 3;
1643         if (BE) {
1644             shift = 24 - shift;
1645         }
1646         int mask           = 0xFF << shift;
1647         int maskedExpected = (expected & 0xFF) << shift;
1648         int maskedX        = (x & 0xFF) << shift;
1649         int fullWord;
1650         do {
1651             fullWord = getIntVolatile(o, wordOffset);
1652             if ((fullWord & mask) != maskedExpected)
1653                 return (byte) ((fullWord & mask) >> shift);
1654         } while (!weakCompareAndSetInt(o, wordOffset,
1655                                                 fullWord, (fullWord & ~mask) | maskedX));
1656         return expected;
1657     }
1658 
1659     @HotSpotIntrinsicCandidate
1660     public final boolean compareAndSetByte(Object o, long offset,
1661                                            byte expected,
1662                                            byte x) {
1663         return compareAndExchangeByte(o, offset, expected, x) == expected;
1664     }
1665 
1666     @HotSpotIntrinsicCandidate
1667     public final boolean weakCompareAndSetByte(Object o, long offset,
1668                                                byte expected,
1669                                                byte x) {
1670         return compareAndSetByte(o, offset, expected, x);
1671     }
1672 
1673     @HotSpotIntrinsicCandidate
1674     public final boolean weakCompareAndSetByteAcquire(Object o, long offset,
1675                                                       byte expected,
1676                                                       byte x) {
1677         return weakCompareAndSetByte(o, offset, expected, x);
1678     }
1679 
1680     @HotSpotIntrinsicCandidate
1681     public final boolean weakCompareAndSetByteRelease(Object o, long offset,
1682                                                       byte expected,
1683                                                       byte x) {
1684         return weakCompareAndSetByte(o, offset, expected, x);
1685     }
1686 
1687     @HotSpotIntrinsicCandidate
1688     public final boolean weakCompareAndSetBytePlain(Object o, long offset,
1689                                                     byte expected,
1690                                                     byte x) {
1691         return weakCompareAndSetByte(o, offset, expected, x);
1692     }
1693 
1694     @HotSpotIntrinsicCandidate
1695     public final byte compareAndExchangeByteAcquire(Object o, long offset,
1696                                                     byte expected,
1697                                                     byte x) {
1698         return compareAndExchangeByte(o, offset, expected, x);
1699     }
1700 
1701     @HotSpotIntrinsicCandidate
1702     public final byte compareAndExchangeByteRelease(Object o, long offset,
1703                                                     byte expected,
1704                                                     byte x) {
1705         return compareAndExchangeByte(o, offset, expected, x);
1706     }
1707 
1708     @HotSpotIntrinsicCandidate
1709     public final short compareAndExchangeShort(Object o, long offset,
1710                                                short expected,
1711                                                short x) {
1712         if ((offset & 3) == 3) {
1713             throw new IllegalArgumentException("Update spans the word, not supported");
1714         }
1715         long wordOffset = offset & ~3;
1716         int shift = (int) (offset & 3) << 3;
1717         if (BE) {
1718             shift = 16 - shift;
1719         }
1720         int mask           = 0xFFFF << shift;
1721         int maskedExpected = (expected & 0xFFFF) << shift;
1722         int maskedX        = (x & 0xFFFF) << shift;
1723         int fullWord;
1724         do {
1725             fullWord = getIntVolatile(o, wordOffset);
1726             if ((fullWord & mask) != maskedExpected) {
1727                 return (short) ((fullWord & mask) >> shift);
1728             }
1729         } while (!weakCompareAndSetInt(o, wordOffset,
1730                                                 fullWord, (fullWord & ~mask) | maskedX));
1731         return expected;
1732     }
1733 
1734     @HotSpotIntrinsicCandidate
1735     public final boolean compareAndSetShort(Object o, long offset,
1736                                             short expected,
1737                                             short x) {
1738         return compareAndExchangeShort(o, offset, expected, x) == expected;
1739     }
1740 
1741     @HotSpotIntrinsicCandidate
1742     public final boolean weakCompareAndSetShort(Object o, long offset,
1743                                                 short expected,
1744                                                 short x) {
1745         return compareAndSetShort(o, offset, expected, x);
1746     }
1747 
1748     @HotSpotIntrinsicCandidate
1749     public final boolean weakCompareAndSetShortAcquire(Object o, long offset,
1750                                                        short expected,
1751                                                        short x) {
1752         return weakCompareAndSetShort(o, offset, expected, x);
1753     }
1754 
1755     @HotSpotIntrinsicCandidate
1756     public final boolean weakCompareAndSetShortRelease(Object o, long offset,
1757                                                        short expected,
1758                                                        short x) {
1759         return weakCompareAndSetShort(o, offset, expected, x);
1760     }
1761 
1762     @HotSpotIntrinsicCandidate
1763     public final boolean weakCompareAndSetShortPlain(Object o, long offset,
1764                                                      short expected,
1765                                                      short x) {
1766         return weakCompareAndSetShort(o, offset, expected, x);
1767     }
1768 
1769 
1770     @HotSpotIntrinsicCandidate
1771     public final short compareAndExchangeShortAcquire(Object o, long offset,
1772                                                      short expected,
1773                                                      short x) {
1774         return compareAndExchangeShort(o, offset, expected, x);
1775     }
1776 
1777     @HotSpotIntrinsicCandidate
1778     public final short compareAndExchangeShortRelease(Object o, long offset,
1779                                                     short expected,
1780                                                     short x) {
1781         return compareAndExchangeShort(o, offset, expected, x);
1782     }
1783 
1784     @ForceInline
1785     private char s2c(short s) {
1786         return (char) s;
1787     }
1788 
1789     @ForceInline
1790     private short c2s(char s) {
1791         return (short) s;
1792     }
1793 
1794     @ForceInline
1795     public final boolean compareAndSetChar(Object o, long offset,
1796                                            char expected,
1797                                            char x) {
1798         return compareAndSetShort(o, offset, c2s(expected), c2s(x));
1799     }
1800 
1801     @ForceInline
1802     public final char compareAndExchangeChar(Object o, long offset,
1803                                              char expected,
1804                                              char x) {
1805         return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x)));
1806     }
1807 
1808     @ForceInline
1809     public final char compareAndExchangeCharAcquire(Object o, long offset,
1810                                             char expected,
1811                                             char x) {
1812         return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x)));
1813     }
1814 
1815     @ForceInline
1816     public final char compareAndExchangeCharRelease(Object o, long offset,
1817                                             char expected,
1818                                             char x) {
1819         return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x)));
1820     }
1821 
1822     @ForceInline
1823     public final boolean weakCompareAndSetChar(Object o, long offset,
1824                                                char expected,
1825                                                char x) {
1826         return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x));
1827     }
1828 
1829     @ForceInline
1830     public final boolean weakCompareAndSetCharAcquire(Object o, long offset,
1831                                                       char expected,
1832                                                       char x) {
1833         return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x));
1834     }
1835 
1836     @ForceInline
1837     public final boolean weakCompareAndSetCharRelease(Object o, long offset,
1838                                                       char expected,
1839                                                       char x) {
1840         return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x));
1841     }
1842 
1843     @ForceInline
1844     public final boolean weakCompareAndSetCharPlain(Object o, long offset,
1845                                                     char expected,
1846                                                     char x) {
1847         return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x));
1848     }
1849 
1850     /**
1851      * The JVM converts integral values to boolean values using two
1852      * different conventions, byte testing against zero and truncation
1853      * to least-significant bit.
1854      *
1855      * <p>The JNI documents specify that, at least for returning
1856      * values from native methods, a Java boolean value is converted
1857      * to the value-set 0..1 by first truncating to a byte (0..255 or
1858      * maybe -128..127) and then testing against zero. Thus, Java
1859      * booleans in non-Java data structures are by convention
1860      * represented as 8-bit containers containing either zero (for
1861      * false) or any non-zero value (for true).
1862      *
1863      * <p>Java booleans in the heap are also stored in bytes, but are
1864      * strongly normalized to the value-set 0..1 (i.e., they are
1865      * truncated to the least-significant bit).
1866      *
1867      * <p>The main reason for having different conventions for
1868      * conversion is performance: Truncation to the least-significant
1869      * bit can be usually implemented with fewer (machine)
1870      * instructions than byte testing against zero.
1871      *
1872      * <p>A number of Unsafe methods load boolean values from the heap
1873      * as bytes. Unsafe converts those values according to the JNI
1874      * rules (i.e, using the "testing against zero" convention). The
1875      * method {@code byte2bool} implements that conversion.
1876      *
1877      * @param b the byte to be converted to boolean
1878      * @return the result of the conversion
1879      */
1880     @ForceInline
1881     private boolean byte2bool(byte b) {
1882         return b != 0;
1883     }
1884 
1885     /**
1886      * Convert a boolean value to a byte. The return value is strongly
1887      * normalized to the value-set 0..1 (i.e., the value is truncated
1888      * to the least-significant bit). See {@link #byte2bool(byte)} for
1889      * more details on conversion conventions.
1890      *
1891      * @param b the boolean to be converted to byte (and then normalized)
1892      * @return the result of the conversion
1893      */
1894     @ForceInline
1895     private byte bool2byte(boolean b) {
1896         return b ? (byte)1 : (byte)0;
1897     }
1898 
1899     @ForceInline
1900     public final boolean compareAndSetBoolean(Object o, long offset,
1901                                               boolean expected,
1902                                               boolean x) {
1903         return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
1904     }
1905 
1906     @ForceInline
1907     public final boolean compareAndExchangeBoolean(Object o, long offset,
1908                                                    boolean expected,
1909                                                    boolean x) {
1910         return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x)));
1911     }
1912 
1913     @ForceInline
1914     public final boolean compareAndExchangeBooleanAcquire(Object o, long offset,
1915                                                     boolean expected,
1916                                                     boolean x) {
1917         return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x)));
1918     }
1919 
1920     @ForceInline
1921     public final boolean compareAndExchangeBooleanRelease(Object o, long offset,
1922                                                        boolean expected,
1923                                                        boolean x) {
1924         return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x)));
1925     }
1926 
1927     @ForceInline
1928     public final boolean weakCompareAndSetBoolean(Object o, long offset,
1929                                                   boolean expected,
1930                                                   boolean x) {
1931         return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
1932     }
1933 
1934     @ForceInline
1935     public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset,
1936                                                          boolean expected,
1937                                                          boolean x) {
1938         return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x));
1939     }
1940 
1941     @ForceInline
1942     public final boolean weakCompareAndSetBooleanRelease(Object o, long offset,
1943                                                          boolean expected,
1944                                                          boolean x) {
1945         return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x));
1946     }
1947 
1948     @ForceInline
1949     public final boolean weakCompareAndSetBooleanPlain(Object o, long offset,
1950                                                        boolean expected,
1951                                                        boolean x) {
1952         return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x));
1953     }
1954 
1955     /**
1956      * Atomically updates Java variable to {@code x} if it is currently
1957      * holding {@code expected}.
1958      *
1959      * <p>This operation has memory semantics of a {@code volatile} read
1960      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1961      *
1962      * @return {@code true} if successful
1963      */
1964     @ForceInline
1965     public final boolean compareAndSetFloat(Object o, long offset,
1966                                             float expected,
1967                                             float x) {
1968         return compareAndSetInt(o, offset,
1969                                  Float.floatToRawIntBits(expected),
1970                                  Float.floatToRawIntBits(x));
1971     }
1972 
1973     @ForceInline
1974     public final float compareAndExchangeFloat(Object o, long offset,
1975                                                float expected,
1976                                                float x) {
1977         int w = compareAndExchangeInt(o, offset,
1978                                       Float.floatToRawIntBits(expected),
1979                                       Float.floatToRawIntBits(x));
1980         return Float.intBitsToFloat(w);
1981     }
1982 
1983     @ForceInline
1984     public final float compareAndExchangeFloatAcquire(Object o, long offset,
1985                                                   float expected,
1986                                                   float x) {
1987         int w = compareAndExchangeIntAcquire(o, offset,
1988                                              Float.floatToRawIntBits(expected),
1989                                              Float.floatToRawIntBits(x));
1990         return Float.intBitsToFloat(w);
1991     }
1992 
1993     @ForceInline
1994     public final float compareAndExchangeFloatRelease(Object o, long offset,
1995                                                   float expected,
1996                                                   float x) {
1997         int w = compareAndExchangeIntRelease(o, offset,
1998                                              Float.floatToRawIntBits(expected),
1999                                              Float.floatToRawIntBits(x));
2000         return Float.intBitsToFloat(w);
2001     }
2002 
2003     @ForceInline
2004     public final boolean weakCompareAndSetFloatPlain(Object o, long offset,
2005                                                      float expected,
2006                                                      float x) {
2007         return weakCompareAndSetIntPlain(o, offset,
2008                                      Float.floatToRawIntBits(expected),
2009                                      Float.floatToRawIntBits(x));
2010     }
2011 
2012     @ForceInline
2013     public final boolean weakCompareAndSetFloatAcquire(Object o, long offset,
2014                                                        float expected,
2015                                                        float x) {
2016         return weakCompareAndSetIntAcquire(o, offset,
2017                                             Float.floatToRawIntBits(expected),
2018                                             Float.floatToRawIntBits(x));
2019     }
2020 
2021     @ForceInline
2022     public final boolean weakCompareAndSetFloatRelease(Object o, long offset,
2023                                                        float expected,
2024                                                        float x) {
2025         return weakCompareAndSetIntRelease(o, offset,
2026                                             Float.floatToRawIntBits(expected),
2027                                             Float.floatToRawIntBits(x));
2028     }
2029 
2030     @ForceInline
2031     public final boolean weakCompareAndSetFloat(Object o, long offset,
2032                                                 float expected,
2033                                                 float x) {
2034         return weakCompareAndSetInt(o, offset,
2035                                              Float.floatToRawIntBits(expected),
2036                                              Float.floatToRawIntBits(x));
2037     }
2038 
2039     /**
2040      * Atomically updates Java variable to {@code x} if it is currently
2041      * holding {@code expected}.
2042      *
2043      * <p>This operation has memory semantics of a {@code volatile} read
2044      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2045      *
2046      * @return {@code true} if successful
2047      */
2048     @ForceInline
2049     public final boolean compareAndSetDouble(Object o, long offset,
2050                                              double expected,
2051                                              double x) {
2052         return compareAndSetLong(o, offset,
2053                                  Double.doubleToRawLongBits(expected),
2054                                  Double.doubleToRawLongBits(x));
2055     }
2056 
2057     @ForceInline
2058     public final double compareAndExchangeDouble(Object o, long offset,
2059                                                  double expected,
2060                                                  double x) {
2061         long w = compareAndExchangeLong(o, offset,
2062                                         Double.doubleToRawLongBits(expected),
2063                                         Double.doubleToRawLongBits(x));
2064         return Double.longBitsToDouble(w);
2065     }
2066 
2067     @ForceInline
2068     public final double compareAndExchangeDoubleAcquire(Object o, long offset,
2069                                                         double expected,
2070                                                         double x) {
2071         long w = compareAndExchangeLongAcquire(o, offset,
2072                                                Double.doubleToRawLongBits(expected),
2073                                                Double.doubleToRawLongBits(x));
2074         return Double.longBitsToDouble(w);
2075     }
2076 
2077     @ForceInline
2078     public final double compareAndExchangeDoubleRelease(Object o, long offset,
2079                                                         double expected,
2080                                                         double x) {
2081         long w = compareAndExchangeLongRelease(o, offset,
2082                                                Double.doubleToRawLongBits(expected),
2083                                                Double.doubleToRawLongBits(x));
2084         return Double.longBitsToDouble(w);
2085     }
2086 
2087     @ForceInline
2088     public final boolean weakCompareAndSetDoublePlain(Object o, long offset,
2089                                                       double expected,
2090                                                       double x) {
2091         return weakCompareAndSetLongPlain(o, offset,
2092                                      Double.doubleToRawLongBits(expected),
2093                                      Double.doubleToRawLongBits(x));
2094     }
2095 
2096     @ForceInline
2097     public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset,
2098                                                         double expected,
2099                                                         double x) {
2100         return weakCompareAndSetLongAcquire(o, offset,
2101                                              Double.doubleToRawLongBits(expected),
2102                                              Double.doubleToRawLongBits(x));
2103     }
2104 
2105     @ForceInline
2106     public final boolean weakCompareAndSetDoubleRelease(Object o, long offset,
2107                                                         double expected,
2108                                                         double x) {
2109         return weakCompareAndSetLongRelease(o, offset,
2110                                              Double.doubleToRawLongBits(expected),
2111                                              Double.doubleToRawLongBits(x));
2112     }
2113 
2114     @ForceInline
2115     public final boolean weakCompareAndSetDouble(Object o, long offset,
2116                                                  double expected,
2117                                                  double x) {
2118         return weakCompareAndSetLong(o, offset,
2119                                               Double.doubleToRawLongBits(expected),
2120                                               Double.doubleToRawLongBits(x));
2121     }
2122 
2123     /**
2124      * Atomically updates Java variable to {@code x} if it is currently
2125      * holding {@code expected}.
2126      *
2127      * <p>This operation has memory semantics of a {@code volatile} read
2128      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2129      *
2130      * @return {@code true} if successful
2131      */
2132     @HotSpotIntrinsicCandidate
2133     public final native boolean compareAndSetLong(Object o, long offset,
2134                                                   long expected,
2135                                                   long x);
2136 
2137     @HotSpotIntrinsicCandidate
2138     public final native long compareAndExchangeLong(Object o, long offset,
2139                                                     long expected,
2140                                                     long x);
2141 
2142     @HotSpotIntrinsicCandidate
2143     public final long compareAndExchangeLongAcquire(Object o, long offset,
2144                                                            long expected,
2145                                                            long x) {
2146         return compareAndExchangeLong(o, offset, expected, x);
2147     }
2148 
2149     @HotSpotIntrinsicCandidate
2150     public final long compareAndExchangeLongRelease(Object o, long offset,
2151                                                            long expected,
2152                                                            long x) {
2153         return compareAndExchangeLong(o, offset, expected, x);
2154     }
2155 
2156     @HotSpotIntrinsicCandidate
2157     public final boolean weakCompareAndSetLongPlain(Object o, long offset,
2158                                                     long expected,
2159                                                     long x) {
2160         return compareAndSetLong(o, offset, expected, x);
2161     }
2162 
2163     @HotSpotIntrinsicCandidate
2164     public final boolean weakCompareAndSetLongAcquire(Object o, long offset,
2165                                                       long expected,
2166                                                       long x) {
2167         return compareAndSetLong(o, offset, expected, x);
2168     }
2169 
2170     @HotSpotIntrinsicCandidate
2171     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2172                                                       long expected,
2173                                                       long x) {
2174         return compareAndSetLong(o, offset, expected, x);
2175     }
2176 
2177     @HotSpotIntrinsicCandidate
2178     public final boolean weakCompareAndSetLong(Object o, long offset,
2179                                                long expected,
2180                                                long x) {
2181         return compareAndSetLong(o, offset, expected, x);
2182     }
2183 
2184     /**
2185      * Fetches a reference value from a given Java variable, with volatile
2186      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
2187      */
2188     @HotSpotIntrinsicCandidate
2189     public native Object getObjectVolatile(Object o, long offset);
2190 
2191     /**
2192      * Global lock for atomic and volatile strength access to any value of
2193      * a value type.  This is a temporary workaround until better localized
2194      * atomic access mechanisms are supported for value types.
2195      */
2196     private static final Object valueLock = new Object();
2197 
2198     public final <V> Object getValueVolatile(Object base, long offset, Class<?> valueType) {
2199         synchronized (valueLock) {
2200             return getValue(base, offset, valueType);
2201         }
2202     }
2203 
2204     /**
2205      * Stores a reference value into a given Java variable, with
2206      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
2207      */
2208     @HotSpotIntrinsicCandidate
2209     public native void    putObjectVolatile(Object o, long offset, Object x);
2210 
2211     public final <V> void putValueVolatile(Object o, long offset, Class<?> valueType, V x) {
2212         synchronized (valueLock) {
2213             putValue(o, offset, valueType, x);
2214         }
2215     }
2216 
2217     /** Volatile version of {@link #getInt(Object, long)}  */
2218     @HotSpotIntrinsicCandidate
2219     public native int     getIntVolatile(Object o, long offset);
2220 
2221     /** Volatile version of {@link #putInt(Object, long, int)}  */
2222     @HotSpotIntrinsicCandidate
2223     public native void    putIntVolatile(Object o, long offset, int x);
2224 
2225     /** Volatile version of {@link #getBoolean(Object, long)}  */
2226     @HotSpotIntrinsicCandidate
2227     public native boolean getBooleanVolatile(Object o, long offset);
2228 
2229     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2230     @HotSpotIntrinsicCandidate
2231     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2232 
2233     /** Volatile version of {@link #getByte(Object, long)}  */
2234     @HotSpotIntrinsicCandidate
2235     public native byte    getByteVolatile(Object o, long offset);
2236 
2237     /** Volatile version of {@link #putByte(Object, long, byte)}  */
2238     @HotSpotIntrinsicCandidate
2239     public native void    putByteVolatile(Object o, long offset, byte x);
2240 
2241     /** Volatile version of {@link #getShort(Object, long)}  */
2242     @HotSpotIntrinsicCandidate
2243     public native short   getShortVolatile(Object o, long offset);
2244 
2245     /** Volatile version of {@link #putShort(Object, long, short)}  */
2246     @HotSpotIntrinsicCandidate
2247     public native void    putShortVolatile(Object o, long offset, short x);
2248 
2249     /** Volatile version of {@link #getChar(Object, long)}  */
2250     @HotSpotIntrinsicCandidate
2251     public native char    getCharVolatile(Object o, long offset);
2252 
2253     /** Volatile version of {@link #putChar(Object, long, char)}  */
2254     @HotSpotIntrinsicCandidate
2255     public native void    putCharVolatile(Object o, long offset, char x);
2256 
2257     /** Volatile version of {@link #getLong(Object, long)}  */
2258     @HotSpotIntrinsicCandidate
2259     public native long    getLongVolatile(Object o, long offset);
2260 
2261     /** Volatile version of {@link #putLong(Object, long, long)}  */
2262     @HotSpotIntrinsicCandidate
2263     public native void    putLongVolatile(Object o, long offset, long x);
2264 
2265     /** Volatile version of {@link #getFloat(Object, long)}  */
2266     @HotSpotIntrinsicCandidate
2267     public native float   getFloatVolatile(Object o, long offset);
2268 
2269     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2270     @HotSpotIntrinsicCandidate
2271     public native void    putFloatVolatile(Object o, long offset, float x);
2272 
2273     /** Volatile version of {@link #getDouble(Object, long)}  */
2274     @HotSpotIntrinsicCandidate
2275     public native double  getDoubleVolatile(Object o, long offset);
2276 
2277     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2278     @HotSpotIntrinsicCandidate
2279     public native void    putDoubleVolatile(Object o, long offset, double x);
2280 
2281 
2282 
2283     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
2284     @HotSpotIntrinsicCandidate
2285     public final Object getObjectAcquire(Object o, long offset) {
2286         return getObjectVolatile(o, offset);
2287     }
2288 
2289     public final <V> Object getValueAcquire(Object base, long offset, Class<?> valueType) {
2290         return getValueVolatile(base, offset, valueType);
2291     }
2292 
2293     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2294     @HotSpotIntrinsicCandidate
2295     public final boolean getBooleanAcquire(Object o, long offset) {
2296         return getBooleanVolatile(o, offset);
2297     }
2298 
2299     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2300     @HotSpotIntrinsicCandidate
2301     public final byte getByteAcquire(Object o, long offset) {
2302         return getByteVolatile(o, offset);
2303     }
2304 
2305     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2306     @HotSpotIntrinsicCandidate
2307     public final short getShortAcquire(Object o, long offset) {
2308         return getShortVolatile(o, offset);
2309     }
2310 
2311     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2312     @HotSpotIntrinsicCandidate
2313     public final char getCharAcquire(Object o, long offset) {
2314         return getCharVolatile(o, offset);
2315     }
2316 
2317     /** Acquire version of {@link #getIntVolatile(Object, long)} */
2318     @HotSpotIntrinsicCandidate
2319     public final int getIntAcquire(Object o, long offset) {
2320         return getIntVolatile(o, offset);
2321     }
2322 
2323     /** Acquire version of {@link #getFloatVolatile(Object, long)} */
2324     @HotSpotIntrinsicCandidate
2325     public final float getFloatAcquire(Object o, long offset) {
2326         return getFloatVolatile(o, offset);
2327     }
2328 
2329     /** Acquire version of {@link #getLongVolatile(Object, long)} */
2330     @HotSpotIntrinsicCandidate
2331     public final long getLongAcquire(Object o, long offset) {
2332         return getLongVolatile(o, offset);
2333     }
2334 
2335     /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
2336     @HotSpotIntrinsicCandidate
2337     public final double getDoubleAcquire(Object o, long offset) {
2338         return getDoubleVolatile(o, offset);
2339     }
2340 
2341     /*
2342       * Versions of {@link #putObjectVolatile(Object, long, Object)}
2343       * that do not guarantee immediate visibility of the store to
2344       * other threads. This method is generally only useful if the
2345       * underlying field is a Java volatile (or if an array cell, one
2346       * that is otherwise only accessed using volatile accesses).
2347       *
2348       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2349       */
2350 
2351     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
2352     @HotSpotIntrinsicCandidate
2353     public final void putObjectRelease(Object o, long offset, Object x) {
2354         putObjectVolatile(o, offset, x);
2355     }
2356 
2357     public final <V> void putValueRelease(Object o, long offset, Class<?> valueType, V x) {
2358         putValueVolatile(o, offset, valueType, x);
2359     }
2360 
2361     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2362     @HotSpotIntrinsicCandidate
2363     public final void putBooleanRelease(Object o, long offset, boolean x) {
2364         putBooleanVolatile(o, offset, x);
2365     }
2366 
2367     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2368     @HotSpotIntrinsicCandidate
2369     public final void putByteRelease(Object o, long offset, byte x) {
2370         putByteVolatile(o, offset, x);
2371     }
2372 
2373     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2374     @HotSpotIntrinsicCandidate
2375     public final void putShortRelease(Object o, long offset, short x) {
2376         putShortVolatile(o, offset, x);
2377     }
2378 
2379     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2380     @HotSpotIntrinsicCandidate
2381     public final void putCharRelease(Object o, long offset, char x) {
2382         putCharVolatile(o, offset, x);
2383     }
2384 
2385     /** Release version of {@link #putIntVolatile(Object, long, int)} */
2386     @HotSpotIntrinsicCandidate
2387     public final void putIntRelease(Object o, long offset, int x) {
2388         putIntVolatile(o, offset, x);
2389     }
2390 
2391     /** Release version of {@link #putFloatVolatile(Object, long, float)} */
2392     @HotSpotIntrinsicCandidate
2393     public final void putFloatRelease(Object o, long offset, float x) {
2394         putFloatVolatile(o, offset, x);
2395     }
2396 
2397     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2398     @HotSpotIntrinsicCandidate
2399     public final void putLongRelease(Object o, long offset, long x) {
2400         putLongVolatile(o, offset, x);
2401     }
2402 
2403     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2404     @HotSpotIntrinsicCandidate
2405     public final void putDoubleRelease(Object o, long offset, double x) {
2406         putDoubleVolatile(o, offset, x);
2407     }
2408 
2409     // ------------------------------ Opaque --------------------------------------
2410 
2411     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
2412     @HotSpotIntrinsicCandidate
2413     public final Object getObjectOpaque(Object o, long offset) {
2414         return getObjectVolatile(o, offset);
2415     }
2416 
2417     public final <V> Object getValueOpaque(Object base, long offset, Class<?> valueType) {
2418         return getValueVolatile(base, offset, valueType);
2419     }
2420 
2421     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2422     @HotSpotIntrinsicCandidate
2423     public final boolean getBooleanOpaque(Object o, long offset) {
2424         return getBooleanVolatile(o, offset);
2425     }
2426 
2427     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2428     @HotSpotIntrinsicCandidate
2429     public final byte getByteOpaque(Object o, long offset) {
2430         return getByteVolatile(o, offset);
2431     }
2432 
2433     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2434     @HotSpotIntrinsicCandidate
2435     public final short getShortOpaque(Object o, long offset) {
2436         return getShortVolatile(o, offset);
2437     }
2438 
2439     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2440     @HotSpotIntrinsicCandidate
2441     public final char getCharOpaque(Object o, long offset) {
2442         return getCharVolatile(o, offset);
2443     }
2444 
2445     /** Opaque version of {@link #getIntVolatile(Object, long)} */
2446     @HotSpotIntrinsicCandidate
2447     public final int getIntOpaque(Object o, long offset) {
2448         return getIntVolatile(o, offset);
2449     }
2450 
2451     /** Opaque version of {@link #getFloatVolatile(Object, long)} */
2452     @HotSpotIntrinsicCandidate
2453     public final float getFloatOpaque(Object o, long offset) {
2454         return getFloatVolatile(o, offset);
2455     }
2456 
2457     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2458     @HotSpotIntrinsicCandidate
2459     public final long getLongOpaque(Object o, long offset) {
2460         return getLongVolatile(o, offset);
2461     }
2462 
2463     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2464     @HotSpotIntrinsicCandidate
2465     public final double getDoubleOpaque(Object o, long offset) {
2466         return getDoubleVolatile(o, offset);
2467     }
2468 
2469     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
2470     @HotSpotIntrinsicCandidate
2471     public final void putObjectOpaque(Object o, long offset, Object x) {
2472         putObjectVolatile(o, offset, x);
2473     }
2474 
2475     public final <V> void putValueOpaque(Object o, long offset, Class<?> valueType, V x) {
2476         putValueVolatile(o, offset, valueType, x);
2477     }
2478 
2479     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2480     @HotSpotIntrinsicCandidate
2481     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2482         putBooleanVolatile(o, offset, x);
2483     }
2484 
2485     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2486     @HotSpotIntrinsicCandidate
2487     public final void putByteOpaque(Object o, long offset, byte x) {
2488         putByteVolatile(o, offset, x);
2489     }
2490 
2491     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2492     @HotSpotIntrinsicCandidate
2493     public final void putShortOpaque(Object o, long offset, short x) {
2494         putShortVolatile(o, offset, x);
2495     }
2496 
2497     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2498     @HotSpotIntrinsicCandidate
2499     public final void putCharOpaque(Object o, long offset, char x) {
2500         putCharVolatile(o, offset, x);
2501     }
2502 
2503     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
2504     @HotSpotIntrinsicCandidate
2505     public final void putIntOpaque(Object o, long offset, int x) {
2506         putIntVolatile(o, offset, x);
2507     }
2508 
2509     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2510     @HotSpotIntrinsicCandidate
2511     public final void putFloatOpaque(Object o, long offset, float x) {
2512         putFloatVolatile(o, offset, x);
2513     }
2514 
2515     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2516     @HotSpotIntrinsicCandidate
2517     public final void putLongOpaque(Object o, long offset, long x) {
2518         putLongVolatile(o, offset, x);
2519     }
2520 
2521     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2522     @HotSpotIntrinsicCandidate
2523     public final void putDoubleOpaque(Object o, long offset, double x) {
2524         putDoubleVolatile(o, offset, x);
2525     }
2526 
2527     /**
2528      * Unblocks the given thread blocked on {@code park}, or, if it is
2529      * not blocked, causes the subsequent call to {@code park} not to
2530      * block.  Note: this operation is "unsafe" solely because the
2531      * caller must somehow ensure that the thread has not been
2532      * destroyed. Nothing special is usually required to ensure this
2533      * when called from Java (in which there will ordinarily be a live
2534      * reference to the thread) but this is not nearly-automatically
2535      * so when calling from native code.
2536      *
2537      * @param thread the thread to unpark.
2538      */
2539     @HotSpotIntrinsicCandidate
2540     public native void unpark(Object thread);
2541 
2542     /**
2543      * Blocks current thread, returning when a balancing
2544      * {@code unpark} occurs, or a balancing {@code unpark} has
2545      * already occurred, or the thread is interrupted, or, if not
2546      * absolute and time is not zero, the given time nanoseconds have
2547      * elapsed, or if absolute, the given deadline in milliseconds
2548      * since Epoch has passed, or spuriously (i.e., returning for no
2549      * "reason"). Note: This operation is in the Unsafe class only
2550      * because {@code unpark} is, so it would be strange to place it
2551      * elsewhere.
2552      */
2553     @HotSpotIntrinsicCandidate
2554     public native void park(boolean isAbsolute, long time);
2555 
2556     /**
2557      * Gets the load average in the system run queue assigned
2558      * to the available processors averaged over various periods of time.
2559      * This method retrieves the given {@code nelem} samples and
2560      * assigns to the elements of the given {@code loadavg} array.
2561      * The system imposes a maximum of 3 samples, representing
2562      * averages over the last 1,  5,  and  15 minutes, respectively.
2563      *
2564      * @param loadavg an array of double of size nelems
2565      * @param nelems the number of samples to be retrieved and
2566      *        must be 1 to 3.
2567      *
2568      * @return the number of samples actually retrieved; or -1
2569      *         if the load average is unobtainable.
2570      */
2571     public int getLoadAverage(double[] loadavg, int nelems) {
2572         if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
2573             throw new ArrayIndexOutOfBoundsException();
2574         }
2575 
2576         return getLoadAverage0(loadavg, nelems);
2577     }
2578 
2579     // The following contain CAS-based Java implementations used on
2580     // platforms not supporting native instructions
2581 
2582     /**
2583      * Atomically adds the given value to the current value of a field
2584      * or array element within the given object {@code o}
2585      * at the given {@code offset}.
2586      *
2587      * @param o object/array to update the field/element in
2588      * @param offset field/element offset
2589      * @param delta the value to add
2590      * @return the previous value
2591      * @since 1.8
2592      */
2593     @HotSpotIntrinsicCandidate
2594     public final int getAndAddInt(Object o, long offset, int delta) {
2595         int v;
2596         do {
2597             v = getIntVolatile(o, offset);
2598         } while (!weakCompareAndSetInt(o, offset, v, v + delta));
2599         return v;
2600     }
2601 
2602     @ForceInline
2603     public final int getAndAddIntRelease(Object o, long offset, int delta) {
2604         int v;
2605         do {
2606             v = getInt(o, offset);
2607         } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta));
2608         return v;
2609     }
2610 
2611     @ForceInline
2612     public final int getAndAddIntAcquire(Object o, long offset, int delta) {
2613         int v;
2614         do {
2615             v = getIntAcquire(o, offset);
2616         } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta));
2617         return v;
2618     }
2619 
2620     /**
2621      * Atomically adds the given value to the current value of a field
2622      * or array element within the given object {@code o}
2623      * at the given {@code offset}.
2624      *
2625      * @param o object/array to update the field/element in
2626      * @param offset field/element offset
2627      * @param delta the value to add
2628      * @return the previous value
2629      * @since 1.8
2630      */
2631     @HotSpotIntrinsicCandidate
2632     public final long getAndAddLong(Object o, long offset, long delta) {
2633         long v;
2634         do {
2635             v = getLongVolatile(o, offset);
2636         } while (!weakCompareAndSetLong(o, offset, v, v + delta));
2637         return v;
2638     }
2639 
2640     @ForceInline
2641     public final long getAndAddLongRelease(Object o, long offset, long delta) {
2642         long v;
2643         do {
2644             v = getLong(o, offset);
2645         } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta));
2646         return v;
2647     }
2648 
2649     @ForceInline
2650     public final long getAndAddLongAcquire(Object o, long offset, long delta) {
2651         long v;
2652         do {
2653             v = getLongAcquire(o, offset);
2654         } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta));
2655         return v;
2656     }
2657 
2658     @HotSpotIntrinsicCandidate
2659     public final byte getAndAddByte(Object o, long offset, byte delta) {
2660         byte v;
2661         do {
2662             v = getByteVolatile(o, offset);
2663         } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta)));
2664         return v;
2665     }
2666 
2667     @ForceInline
2668     public final byte getAndAddByteRelease(Object o, long offset, byte delta) {
2669         byte v;
2670         do {
2671             v = getByte(o, offset);
2672         } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta)));
2673         return v;
2674     }
2675 
2676     @ForceInline
2677     public final byte getAndAddByteAcquire(Object o, long offset, byte delta) {
2678         byte v;
2679         do {
2680             v = getByteAcquire(o, offset);
2681         } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta)));
2682         return v;
2683     }
2684 
2685     @HotSpotIntrinsicCandidate
2686     public final short getAndAddShort(Object o, long offset, short delta) {
2687         short v;
2688         do {
2689             v = getShortVolatile(o, offset);
2690         } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta)));
2691         return v;
2692     }
2693 
2694     @ForceInline
2695     public final short getAndAddShortRelease(Object o, long offset, short delta) {
2696         short v;
2697         do {
2698             v = getShort(o, offset);
2699         } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta)));
2700         return v;
2701     }
2702 
2703     @ForceInline
2704     public final short getAndAddShortAcquire(Object o, long offset, short delta) {
2705         short v;
2706         do {
2707             v = getShortAcquire(o, offset);
2708         } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta)));
2709         return v;
2710     }
2711 
2712     @ForceInline
2713     public final char getAndAddChar(Object o, long offset, char delta) {
2714         return (char) getAndAddShort(o, offset, (short) delta);
2715     }
2716 
2717     @ForceInline
2718     public final char getAndAddCharRelease(Object o, long offset, char delta) {
2719         return (char) getAndAddShortRelease(o, offset, (short) delta);
2720     }
2721 
2722     @ForceInline
2723     public final char getAndAddCharAcquire(Object o, long offset, char delta) {
2724         return (char) getAndAddShortAcquire(o, offset, (short) delta);
2725     }
2726 
2727     @ForceInline
2728     public final float getAndAddFloat(Object o, long offset, float delta) {
2729         int expectedBits;
2730         float v;
2731         do {
2732             // Load and CAS with the raw bits to avoid issues with NaNs and
2733             // possible bit conversion from signaling NaNs to quiet NaNs that
2734             // may result in the loop not terminating.
2735             expectedBits = getIntVolatile(o, offset);
2736             v = Float.intBitsToFloat(expectedBits);
2737         } while (!weakCompareAndSetInt(o, offset,
2738                                                 expectedBits, Float.floatToRawIntBits(v + delta)));
2739         return v;
2740     }
2741 
2742     @ForceInline
2743     public final float getAndAddFloatRelease(Object o, long offset, float delta) {
2744         int expectedBits;
2745         float v;
2746         do {
2747             // Load and CAS with the raw bits to avoid issues with NaNs and
2748             // possible bit conversion from signaling NaNs to quiet NaNs that
2749             // may result in the loop not terminating.
2750             expectedBits = getInt(o, offset);
2751             v = Float.intBitsToFloat(expectedBits);
2752         } while (!weakCompareAndSetIntRelease(o, offset,
2753                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2754         return v;
2755     }
2756 
2757     @ForceInline
2758     public final float getAndAddFloatAcquire(Object o, long offset, float delta) {
2759         int expectedBits;
2760         float v;
2761         do {
2762             // Load and CAS with the raw bits to avoid issues with NaNs and
2763             // possible bit conversion from signaling NaNs to quiet NaNs that
2764             // may result in the loop not terminating.
2765             expectedBits = getIntAcquire(o, offset);
2766             v = Float.intBitsToFloat(expectedBits);
2767         } while (!weakCompareAndSetIntAcquire(o, offset,
2768                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2769         return v;
2770     }
2771 
2772     @ForceInline
2773     public final double getAndAddDouble(Object o, long offset, double delta) {
2774         long expectedBits;
2775         double v;
2776         do {
2777             // Load and CAS with the raw bits to avoid issues with NaNs and
2778             // possible bit conversion from signaling NaNs to quiet NaNs that
2779             // may result in the loop not terminating.
2780             expectedBits = getLongVolatile(o, offset);
2781             v = Double.longBitsToDouble(expectedBits);
2782         } while (!weakCompareAndSetLong(o, offset,
2783                                                  expectedBits, Double.doubleToRawLongBits(v + delta)));
2784         return v;
2785     }
2786 
2787     @ForceInline
2788     public final double getAndAddDoubleRelease(Object o, long offset, double delta) {
2789         long expectedBits;
2790         double v;
2791         do {
2792             // Load and CAS with the raw bits to avoid issues with NaNs and
2793             // possible bit conversion from signaling NaNs to quiet NaNs that
2794             // may result in the loop not terminating.
2795             expectedBits = getLong(o, offset);
2796             v = Double.longBitsToDouble(expectedBits);
2797         } while (!weakCompareAndSetLongRelease(o, offset,
2798                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
2799         return v;
2800     }
2801 
2802     @ForceInline
2803     public final double getAndAddDoubleAcquire(Object o, long offset, double delta) {
2804         long expectedBits;
2805         double v;
2806         do {
2807             // Load and CAS with the raw bits to avoid issues with NaNs and
2808             // possible bit conversion from signaling NaNs to quiet NaNs that
2809             // may result in the loop not terminating.
2810             expectedBits = getLongAcquire(o, offset);
2811             v = Double.longBitsToDouble(expectedBits);
2812         } while (!weakCompareAndSetLongAcquire(o, offset,
2813                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
2814         return v;
2815     }
2816 
2817     /**
2818      * Atomically exchanges the given value with the current value of
2819      * a field or array element within the given object {@code o}
2820      * at the given {@code offset}.
2821      *
2822      * @param o object/array to update the field/element in
2823      * @param offset field/element offset
2824      * @param newValue new value
2825      * @return the previous value
2826      * @since 1.8
2827      */
2828     @HotSpotIntrinsicCandidate
2829     public final int getAndSetInt(Object o, long offset, int newValue) {
2830         int v;
2831         do {
2832             v = getIntVolatile(o, offset);
2833         } while (!weakCompareAndSetInt(o, offset, v, newValue));
2834         return v;
2835     }
2836 
2837     @ForceInline
2838     public final int getAndSetIntRelease(Object o, long offset, int newValue) {
2839         int v;
2840         do {
2841             v = getInt(o, offset);
2842         } while (!weakCompareAndSetIntRelease(o, offset, v, newValue));
2843         return v;
2844     }
2845 
2846     @ForceInline
2847     public final int getAndSetIntAcquire(Object o, long offset, int newValue) {
2848         int v;
2849         do {
2850             v = getIntAcquire(o, offset);
2851         } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue));
2852         return v;
2853     }
2854 
2855     /**
2856      * Atomically exchanges the given value with the current value of
2857      * a field or array element within the given object {@code o}
2858      * at the given {@code offset}.
2859      *
2860      * @param o object/array to update the field/element in
2861      * @param offset field/element offset
2862      * @param newValue new value
2863      * @return the previous value
2864      * @since 1.8
2865      */
2866     @HotSpotIntrinsicCandidate
2867     public final long getAndSetLong(Object o, long offset, long newValue) {
2868         long v;
2869         do {
2870             v = getLongVolatile(o, offset);
2871         } while (!weakCompareAndSetLong(o, offset, v, newValue));
2872         return v;
2873     }
2874 
2875     @ForceInline
2876     public final long getAndSetLongRelease(Object o, long offset, long newValue) {
2877         long v;
2878         do {
2879             v = getLong(o, offset);
2880         } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
2881         return v;
2882     }
2883 
2884     @ForceInline
2885     public final long getAndSetLongAcquire(Object o, long offset, long newValue) {
2886         long v;
2887         do {
2888             v = getLongAcquire(o, offset);
2889         } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue));
2890         return v;
2891     }
2892 
2893     /**
2894      * Atomically exchanges the given reference value with the current
2895      * reference value of a field or array element within the given
2896      * object {@code o} at the given {@code offset}.
2897      *
2898      * @param o object/array to update the field/element in
2899      * @param offset field/element offset
2900      * @param newValue new value
2901      * @return the previous value
2902      * @since 1.8
2903      */
2904     @HotSpotIntrinsicCandidate
2905     public final Object getAndSetObject(Object o, long offset, Object newValue) {
2906         Object v;
2907         do {
2908             v = getObjectVolatile(o, offset);
2909         } while (!weakCompareAndSetObject(o, offset, v, newValue));
2910         return v;
2911     }
2912 
2913     @SuppressWarnings("unchecked")
2914     public final <V> Object getAndSetValue(Object o, long offset, Class<?> valueType, V newValue) {
2915         synchronized (valueLock) {
2916             Object oldValue = getValue(o, offset, valueType);
2917             putValue(o, offset, valueType, newValue);
2918             return oldValue;
2919         }
2920     }
2921 
2922     @ForceInline
2923     public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) {
2924         Object v;
2925         do {
2926             v = getObject(o, offset);
2927         } while (!weakCompareAndSetObjectRelease(o, offset, v, newValue));
2928         return v;
2929     }
2930 
2931     @ForceInline
2932     public final <V> Object getAndSetValueRelease(Object o, long offset, Class<?> valueType, V newValue) {
2933         return getAndSetValue(o, offset, valueType, newValue);
2934     }
2935 
2936     @ForceInline
2937     public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) {
2938         Object v;
2939         do {
2940             v = getObjectAcquire(o, offset);
2941         } while (!weakCompareAndSetObjectAcquire(o, offset, v, newValue));
2942         return v;
2943     }
2944 
2945     @ForceInline
2946     public final <V> Object getAndSetValueAcquire(Object o, long offset, Class<?> valueType, V newValue) {
2947         return getAndSetValue(o, offset, valueType, newValue);
2948     }
2949 
2950     @HotSpotIntrinsicCandidate
2951     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2952         byte v;
2953         do {
2954             v = getByteVolatile(o, offset);
2955         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2956         return v;
2957     }
2958 
2959     @ForceInline
2960     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2961         byte v;
2962         do {
2963             v = getByte(o, offset);
2964         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2965         return v;
2966     }
2967 
2968     @ForceInline
2969     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
2970         byte v;
2971         do {
2972             v = getByteAcquire(o, offset);
2973         } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue));
2974         return v;
2975     }
2976 
2977     @ForceInline
2978     public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) {
2979         return byte2bool(getAndSetByte(o, offset, bool2byte(newValue)));
2980     }
2981 
2982     @ForceInline
2983     public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) {
2984         return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue)));
2985     }
2986 
2987     @ForceInline
2988     public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) {
2989         return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue)));
2990     }
2991 
2992     @HotSpotIntrinsicCandidate
2993     public final short getAndSetShort(Object o, long offset, short newValue) {
2994         short v;
2995         do {
2996             v = getShortVolatile(o, offset);
2997         } while (!weakCompareAndSetShort(o, offset, v, newValue));
2998         return v;
2999     }
3000 
3001     @ForceInline
3002     public final short getAndSetShortRelease(Object o, long offset, short newValue) {
3003         short v;
3004         do {
3005             v = getShort(o, offset);
3006         } while (!weakCompareAndSetShortRelease(o, offset, v, newValue));
3007         return v;
3008     }
3009 
3010     @ForceInline
3011     public final short getAndSetShortAcquire(Object o, long offset, short newValue) {
3012         short v;
3013         do {
3014             v = getShortAcquire(o, offset);
3015         } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue));
3016         return v;
3017     }
3018 
3019     @ForceInline
3020     public final char getAndSetChar(Object o, long offset, char newValue) {
3021         return s2c(getAndSetShort(o, offset, c2s(newValue)));
3022     }
3023 
3024     @ForceInline
3025     public final char getAndSetCharRelease(Object o, long offset, char newValue) {
3026         return s2c(getAndSetShortRelease(o, offset, c2s(newValue)));
3027     }
3028 
3029     @ForceInline
3030     public final char getAndSetCharAcquire(Object o, long offset, char newValue) {
3031         return s2c(getAndSetShortAcquire(o, offset, c2s(newValue)));
3032     }
3033 
3034     @ForceInline
3035     public final float getAndSetFloat(Object o, long offset, float newValue) {
3036         int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue));
3037         return Float.intBitsToFloat(v);
3038     }
3039 
3040     @ForceInline
3041     public final float getAndSetFloatRelease(Object o, long offset, float newValue) {
3042         int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue));
3043         return Float.intBitsToFloat(v);
3044     }
3045 
3046     @ForceInline
3047     public final float getAndSetFloatAcquire(Object o, long offset, float newValue) {
3048         int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue));
3049         return Float.intBitsToFloat(v);
3050     }
3051 
3052     @ForceInline
3053     public final double getAndSetDouble(Object o, long offset, double newValue) {
3054         long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue));
3055         return Double.longBitsToDouble(v);
3056     }
3057 
3058     @ForceInline
3059     public final double getAndSetDoubleRelease(Object o, long offset, double newValue) {
3060         long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue));
3061         return Double.longBitsToDouble(v);
3062     }
3063 
3064     @ForceInline
3065     public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) {
3066         long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue));
3067         return Double.longBitsToDouble(v);
3068     }
3069 
3070 
3071     // The following contain CAS-based Java implementations used on
3072     // platforms not supporting native instructions
3073 
3074     @ForceInline
3075     public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) {
3076         return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask)));
3077     }
3078 
3079     @ForceInline
3080     public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) {
3081         return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask)));
3082     }
3083 
3084     @ForceInline
3085     public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) {
3086         return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask)));
3087     }
3088 
3089     @ForceInline
3090     public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) {
3091         return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask)));
3092     }
3093 
3094     @ForceInline
3095     public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) {
3096         return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask)));
3097     }
3098 
3099     @ForceInline
3100     public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) {
3101         return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask)));
3102     }
3103 
3104     @ForceInline
3105     public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) {
3106         return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask)));
3107     }
3108 
3109     @ForceInline
3110     public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) {
3111         return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask)));
3112     }
3113 
3114     @ForceInline
3115     public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) {
3116         return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask)));
3117     }
3118 
3119 
3120     @ForceInline
3121     public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) {
3122         byte current;
3123         do {
3124             current = getByteVolatile(o, offset);
3125         } while (!weakCompareAndSetByte(o, offset,
3126                                                   current, (byte) (current | mask)));
3127         return current;
3128     }
3129 
3130     @ForceInline
3131     public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) {
3132         byte current;
3133         do {
3134             current = getByte(o, offset);
3135         } while (!weakCompareAndSetByteRelease(o, offset,
3136                                                  current, (byte) (current | mask)));
3137         return current;
3138     }
3139 
3140     @ForceInline
3141     public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) {
3142         byte current;
3143         do {
3144             // Plain read, the value is a hint, the acquire CAS does the work
3145             current = getByte(o, offset);
3146         } while (!weakCompareAndSetByteAcquire(o, offset,
3147                                                  current, (byte) (current | mask)));
3148         return current;
3149     }
3150 
3151     @ForceInline
3152     public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) {
3153         byte current;
3154         do {
3155             current = getByteVolatile(o, offset);
3156         } while (!weakCompareAndSetByte(o, offset,
3157                                                   current, (byte) (current & mask)));
3158         return current;
3159     }
3160 
3161     @ForceInline
3162     public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) {
3163         byte current;
3164         do {
3165             current = getByte(o, offset);
3166         } while (!weakCompareAndSetByteRelease(o, offset,
3167                                                  current, (byte) (current & mask)));
3168         return current;
3169     }
3170 
3171     @ForceInline
3172     public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) {
3173         byte current;
3174         do {
3175             // Plain read, the value is a hint, the acquire CAS does the work
3176             current = getByte(o, offset);
3177         } while (!weakCompareAndSetByteAcquire(o, offset,
3178                                                  current, (byte) (current & mask)));
3179         return current;
3180     }
3181 
3182     @ForceInline
3183     public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) {
3184         byte current;
3185         do {
3186             current = getByteVolatile(o, offset);
3187         } while (!weakCompareAndSetByte(o, offset,
3188                                                   current, (byte) (current ^ mask)));
3189         return current;
3190     }
3191 
3192     @ForceInline
3193     public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) {
3194         byte current;
3195         do {
3196             current = getByte(o, offset);
3197         } while (!weakCompareAndSetByteRelease(o, offset,
3198                                                  current, (byte) (current ^ mask)));
3199         return current;
3200     }
3201 
3202     @ForceInline
3203     public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) {
3204         byte current;
3205         do {
3206             // Plain read, the value is a hint, the acquire CAS does the work
3207             current = getByte(o, offset);
3208         } while (!weakCompareAndSetByteAcquire(o, offset,
3209                                                  current, (byte) (current ^ mask)));
3210         return current;
3211     }
3212 
3213 
3214     @ForceInline
3215     public final char getAndBitwiseOrChar(Object o, long offset, char mask) {
3216         return s2c(getAndBitwiseOrShort(o, offset, c2s(mask)));
3217     }
3218 
3219     @ForceInline
3220     public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) {
3221         return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask)));
3222     }
3223 
3224     @ForceInline
3225     public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) {
3226         return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask)));
3227     }
3228 
3229     @ForceInline
3230     public final char getAndBitwiseAndChar(Object o, long offset, char mask) {
3231         return s2c(getAndBitwiseAndShort(o, offset, c2s(mask)));
3232     }
3233 
3234     @ForceInline
3235     public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) {
3236         return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask)));
3237     }
3238 
3239     @ForceInline
3240     public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) {
3241         return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask)));
3242     }
3243 
3244     @ForceInline
3245     public final char getAndBitwiseXorChar(Object o, long offset, char mask) {
3246         return s2c(getAndBitwiseXorShort(o, offset, c2s(mask)));
3247     }
3248 
3249     @ForceInline
3250     public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) {
3251         return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask)));
3252     }
3253 
3254     @ForceInline
3255     public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) {
3256         return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask)));
3257     }
3258 
3259 
3260     @ForceInline
3261     public final short getAndBitwiseOrShort(Object o, long offset, short mask) {
3262         short current;
3263         do {
3264             current = getShortVolatile(o, offset);
3265         } while (!weakCompareAndSetShort(o, offset,
3266                                                 current, (short) (current | mask)));
3267         return current;
3268     }
3269 
3270     @ForceInline
3271     public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) {
3272         short current;
3273         do {
3274             current = getShort(o, offset);
3275         } while (!weakCompareAndSetShortRelease(o, offset,
3276                                                current, (short) (current | mask)));
3277         return current;
3278     }
3279 
3280     @ForceInline
3281     public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) {
3282         short current;
3283         do {
3284             // Plain read, the value is a hint, the acquire CAS does the work
3285             current = getShort(o, offset);
3286         } while (!weakCompareAndSetShortAcquire(o, offset,
3287                                                current, (short) (current | mask)));
3288         return current;
3289     }
3290 
3291     @ForceInline
3292     public final short getAndBitwiseAndShort(Object o, long offset, short mask) {
3293         short current;
3294         do {
3295             current = getShortVolatile(o, offset);
3296         } while (!weakCompareAndSetShort(o, offset,
3297                                                 current, (short) (current & mask)));
3298         return current;
3299     }
3300 
3301     @ForceInline
3302     public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) {
3303         short current;
3304         do {
3305             current = getShort(o, offset);
3306         } while (!weakCompareAndSetShortRelease(o, offset,
3307                                                current, (short) (current & mask)));
3308         return current;
3309     }
3310 
3311     @ForceInline
3312     public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) {
3313         short current;
3314         do {
3315             // Plain read, the value is a hint, the acquire CAS does the work
3316             current = getShort(o, offset);
3317         } while (!weakCompareAndSetShortAcquire(o, offset,
3318                                                current, (short) (current & mask)));
3319         return current;
3320     }
3321 
3322     @ForceInline
3323     public final short getAndBitwiseXorShort(Object o, long offset, short mask) {
3324         short current;
3325         do {
3326             current = getShortVolatile(o, offset);
3327         } while (!weakCompareAndSetShort(o, offset,
3328                                                 current, (short) (current ^ mask)));
3329         return current;
3330     }
3331 
3332     @ForceInline
3333     public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) {
3334         short current;
3335         do {
3336             current = getShort(o, offset);
3337         } while (!weakCompareAndSetShortRelease(o, offset,
3338                                                current, (short) (current ^ mask)));
3339         return current;
3340     }
3341 
3342     @ForceInline
3343     public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) {
3344         short current;
3345         do {
3346             // Plain read, the value is a hint, the acquire CAS does the work
3347             current = getShort(o, offset);
3348         } while (!weakCompareAndSetShortAcquire(o, offset,
3349                                                current, (short) (current ^ mask)));
3350         return current;
3351     }
3352 
3353 
3354     @ForceInline
3355     public final int getAndBitwiseOrInt(Object o, long offset, int mask) {
3356         int current;
3357         do {
3358             current = getIntVolatile(o, offset);
3359         } while (!weakCompareAndSetInt(o, offset,
3360                                                 current, current | mask));
3361         return current;
3362     }
3363 
3364     @ForceInline
3365     public final int getAndBitwiseOrIntRelease(Object o, long offset, int mask) {
3366         int current;
3367         do {
3368             current = getInt(o, offset);
3369         } while (!weakCompareAndSetIntRelease(o, offset,
3370                                                current, current | mask));
3371         return current;
3372     }
3373 
3374     @ForceInline
3375     public final int getAndBitwiseOrIntAcquire(Object o, long offset, int mask) {
3376         int current;
3377         do {
3378             // Plain read, the value is a hint, the acquire CAS does the work
3379             current = getInt(o, offset);
3380         } while (!weakCompareAndSetIntAcquire(o, offset,
3381                                                current, current | mask));
3382         return current;
3383     }
3384 
3385     /**
3386      * Atomically replaces the current value of a field or array element within
3387      * the given object with the result of bitwise AND between the current value
3388      * and mask.
3389      *
3390      * @param o object/array to update the field/element in
3391      * @param offset field/element offset
3392      * @param mask the mask value
3393      * @return the previous value
3394      * @since 1.9
3395      */
3396     @ForceInline
3397     public final int getAndBitwiseAndInt(Object o, long offset, int mask) {
3398         int current;
3399         do {
3400             current = getIntVolatile(o, offset);
3401         } while (!weakCompareAndSetInt(o, offset,
3402                                                 current, current & mask));
3403         return current;
3404     }
3405 
3406     @ForceInline
3407     public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) {
3408         int current;
3409         do {
3410             current = getInt(o, offset);
3411         } while (!weakCompareAndSetIntRelease(o, offset,
3412                                                current, current & mask));
3413         return current;
3414     }
3415 
3416     @ForceInline
3417     public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) {
3418         int current;
3419         do {
3420             // Plain read, the value is a hint, the acquire CAS does the work
3421             current = getInt(o, offset);
3422         } while (!weakCompareAndSetIntAcquire(o, offset,
3423                                                current, current & mask));
3424         return current;
3425     }
3426 
3427     @ForceInline
3428     public final int getAndBitwiseXorInt(Object o, long offset, int mask) {
3429         int current;
3430         do {
3431             current = getIntVolatile(o, offset);
3432         } while (!weakCompareAndSetInt(o, offset,
3433                                                 current, current ^ mask));
3434         return current;
3435     }
3436 
3437     @ForceInline
3438     public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) {
3439         int current;
3440         do {
3441             current = getInt(o, offset);
3442         } while (!weakCompareAndSetIntRelease(o, offset,
3443                                                current, current ^ mask));
3444         return current;
3445     }
3446 
3447     @ForceInline
3448     public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) {
3449         int current;
3450         do {
3451             // Plain read, the value is a hint, the acquire CAS does the work
3452             current = getInt(o, offset);
3453         } while (!weakCompareAndSetIntAcquire(o, offset,
3454                                                current, current ^ mask));
3455         return current;
3456     }
3457 
3458 
3459     @ForceInline
3460     public final long getAndBitwiseOrLong(Object o, long offset, long mask) {
3461         long current;
3462         do {
3463             current = getLongVolatile(o, offset);
3464         } while (!weakCompareAndSetLong(o, offset,
3465                                                 current, current | mask));
3466         return current;
3467     }
3468 
3469     @ForceInline
3470     public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) {
3471         long current;
3472         do {
3473             current = getLong(o, offset);
3474         } while (!weakCompareAndSetLongRelease(o, offset,
3475                                                current, current | mask));
3476         return current;
3477     }
3478 
3479     @ForceInline
3480     public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) {
3481         long current;
3482         do {
3483             // Plain read, the value is a hint, the acquire CAS does the work
3484             current = getLong(o, offset);
3485         } while (!weakCompareAndSetLongAcquire(o, offset,
3486                                                current, current | mask));
3487         return current;
3488     }
3489 
3490     @ForceInline
3491     public final long getAndBitwiseAndLong(Object o, long offset, long mask) {
3492         long current;
3493         do {
3494             current = getLongVolatile(o, offset);
3495         } while (!weakCompareAndSetLong(o, offset,
3496                                                 current, current & mask));
3497         return current;
3498     }
3499 
3500     @ForceInline
3501     public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) {
3502         long current;
3503         do {
3504             current = getLong(o, offset);
3505         } while (!weakCompareAndSetLongRelease(o, offset,
3506                                                current, current & mask));
3507         return current;
3508     }
3509 
3510     @ForceInline
3511     public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) {
3512         long current;
3513         do {
3514             // Plain read, the value is a hint, the acquire CAS does the work
3515             current = getLong(o, offset);
3516         } while (!weakCompareAndSetLongAcquire(o, offset,
3517                                                current, current & mask));
3518         return current;
3519     }
3520 
3521     @ForceInline
3522     public final long getAndBitwiseXorLong(Object o, long offset, long mask) {
3523         long current;
3524         do {
3525             current = getLongVolatile(o, offset);
3526         } while (!weakCompareAndSetLong(o, offset,
3527                                                 current, current ^ mask));
3528         return current;
3529     }
3530 
3531     @ForceInline
3532     public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) {
3533         long current;
3534         do {
3535             current = getLong(o, offset);
3536         } while (!weakCompareAndSetLongRelease(o, offset,
3537                                                current, current ^ mask));
3538         return current;
3539     }
3540 
3541     @ForceInline
3542     public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) {
3543         long current;
3544         do {
3545             // Plain read, the value is a hint, the acquire CAS does the work
3546             current = getLong(o, offset);
3547         } while (!weakCompareAndSetLongAcquire(o, offset,
3548                                                current, current ^ mask));
3549         return current;
3550     }
3551 
3552 
3553 
3554     /**
3555      * Ensures that loads before the fence will not be reordered with loads and
3556      * stores after the fence; a "LoadLoad plus LoadStore barrier".
3557      *
3558      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
3559      * (an "acquire fence").
3560      *
3561      * A pure LoadLoad fence is not provided, since the addition of LoadStore
3562      * is almost always desired, and most current hardware instructions that
3563      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
3564      * @since 1.8
3565      */
3566     @HotSpotIntrinsicCandidate
3567     public native void loadFence();
3568 
3569     /**
3570      * Ensures that loads and stores before the fence will not be reordered with
3571      * stores after the fence; a "StoreStore plus LoadStore barrier".
3572      *
3573      * Corresponds to C11 atomic_thread_fence(memory_order_release)
3574      * (a "release fence").
3575      *
3576      * A pure StoreStore fence is not provided, since the addition of LoadStore
3577      * is almost always desired, and most current hardware instructions that
3578      * provide a StoreStore barrier also provide a LoadStore barrier for free.
3579      * @since 1.8
3580      */
3581     @HotSpotIntrinsicCandidate
3582     public native void storeFence();
3583 
3584     /**
3585      * Ensures that loads and stores before the fence will not be reordered
3586      * with loads and stores after the fence.  Implies the effects of both
3587      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
3588      * barrier.
3589      *
3590      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
3591      * @since 1.8
3592      */
3593     @HotSpotIntrinsicCandidate
3594     public native void fullFence();
3595 
3596     /**
3597      * Ensures that loads before the fence will not be reordered with
3598      * loads after the fence.
3599      */
3600     public final void loadLoadFence() {
3601         loadFence();
3602     }
3603 
3604     /**
3605      * Ensures that stores before the fence will not be reordered with
3606      * stores after the fence.
3607      */
3608     public final void storeStoreFence() {
3609         storeFence();
3610     }
3611 
3612 
3613     /**
3614      * Throws IllegalAccessError; for use by the VM for access control
3615      * error support.
3616      * @since 1.8
3617      */
3618     private static void throwIllegalAccessError() {
3619         throw new IllegalAccessError();
3620     }
3621 
3622     /**
3623      * @return Returns true if the native byte ordering of this
3624      * platform is big-endian, false if it is little-endian.
3625      */
3626     public final boolean isBigEndian() { return BE; }
3627 
3628     /**
3629      * @return Returns true if this platform is capable of performing
3630      * accesses at addresses which are not aligned for the type of the
3631      * primitive type being accessed, false otherwise.
3632      */
3633     public final boolean unalignedAccess() { return unalignedAccess; }
3634 
3635     /**
3636      * Fetches a value at some byte offset into a given Java object.
3637      * More specifically, fetches a value within the given object
3638      * <code>o</code> at the given offset, or (if <code>o</code> is
3639      * null) from the memory address whose numerical value is the
3640      * given offset.  <p>
3641      *
3642      * The specification of this method is the same as {@link
3643      * #getLong(Object, long)} except that the offset does not need to
3644      * have been obtained from {@link #objectFieldOffset} on the
3645      * {@link java.lang.reflect.Field} of some Java field.  The value
3646      * in memory is raw data, and need not correspond to any Java
3647      * variable.  Unless <code>o</code> is null, the value accessed
3648      * must be entirely within the allocated object.  The endianness
3649      * of the value in memory is the endianness of the native platform.
3650      *
3651      * <p> The read will be atomic with respect to the largest power
3652      * of two that divides the GCD of the offset and the storage size.
3653      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
3654      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3655      * respectively.  There are no other guarantees of atomicity.
3656      * <p>
3657      * 8-byte atomicity is only guaranteed on platforms on which
3658      * support atomic accesses to longs.
3659      *
3660      * @param o Java heap object in which the value resides, if any, else
3661      *        null
3662      * @param offset The offset in bytes from the start of the object
3663      * @return the value fetched from the indicated object
3664      * @throws RuntimeException No defined exceptions are thrown, not even
3665      *         {@link NullPointerException}
3666      * @since 9
3667      */
3668     @HotSpotIntrinsicCandidate
3669     public final long getLongUnaligned(Object o, long offset) {
3670         if ((offset & 7) == 0) {
3671             return getLong(o, offset);
3672         } else if ((offset & 3) == 0) {
3673             return makeLong(getInt(o, offset),
3674                             getInt(o, offset + 4));
3675         } else if ((offset & 1) == 0) {
3676             return makeLong(getShort(o, offset),
3677                             getShort(o, offset + 2),
3678                             getShort(o, offset + 4),
3679                             getShort(o, offset + 6));
3680         } else {
3681             return makeLong(getByte(o, offset),
3682                             getByte(o, offset + 1),
3683                             getByte(o, offset + 2),
3684                             getByte(o, offset + 3),
3685                             getByte(o, offset + 4),
3686                             getByte(o, offset + 5),
3687                             getByte(o, offset + 6),
3688                             getByte(o, offset + 7));
3689         }
3690     }
3691     /**
3692      * As {@link #getLongUnaligned(Object, long)} but with an
3693      * additional argument which specifies the endianness of the value
3694      * as stored in memory.
3695      *
3696      * @param o Java heap object in which the variable resides
3697      * @param offset The offset in bytes from the start of the object
3698      * @param bigEndian The endianness of the value
3699      * @return the value fetched from the indicated object
3700      * @since 9
3701      */
3702     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
3703         return convEndian(bigEndian, getLongUnaligned(o, offset));
3704     }
3705 
3706     /** @see #getLongUnaligned(Object, long) */
3707     @HotSpotIntrinsicCandidate
3708     public final int getIntUnaligned(Object o, long offset) {
3709         if ((offset & 3) == 0) {
3710             return getInt(o, offset);
3711         } else if ((offset & 1) == 0) {
3712             return makeInt(getShort(o, offset),
3713                            getShort(o, offset + 2));
3714         } else {
3715             return makeInt(getByte(o, offset),
3716                            getByte(o, offset + 1),
3717                            getByte(o, offset + 2),
3718                            getByte(o, offset + 3));
3719         }
3720     }
3721     /** @see #getLongUnaligned(Object, long, boolean) */
3722     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
3723         return convEndian(bigEndian, getIntUnaligned(o, offset));
3724     }
3725 
3726     /** @see #getLongUnaligned(Object, long) */
3727     @HotSpotIntrinsicCandidate
3728     public final short getShortUnaligned(Object o, long offset) {
3729         if ((offset & 1) == 0) {
3730             return getShort(o, offset);
3731         } else {
3732             return makeShort(getByte(o, offset),
3733                              getByte(o, offset + 1));
3734         }
3735     }
3736     /** @see #getLongUnaligned(Object, long, boolean) */
3737     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
3738         return convEndian(bigEndian, getShortUnaligned(o, offset));
3739     }
3740 
3741     /** @see #getLongUnaligned(Object, long) */
3742     @HotSpotIntrinsicCandidate
3743     public final char getCharUnaligned(Object o, long offset) {
3744         if ((offset & 1) == 0) {
3745             return getChar(o, offset);
3746         } else {
3747             return (char)makeShort(getByte(o, offset),
3748                                    getByte(o, offset + 1));
3749         }
3750     }
3751 
3752     /** @see #getLongUnaligned(Object, long, boolean) */
3753     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
3754         return convEndian(bigEndian, getCharUnaligned(o, offset));
3755     }
3756 
3757     /**
3758      * Stores a value at some byte offset into a given Java object.
3759      * <p>
3760      * The specification of this method is the same as {@link
3761      * #getLong(Object, long)} except that the offset does not need to
3762      * have been obtained from {@link #objectFieldOffset} on the
3763      * {@link java.lang.reflect.Field} of some Java field.  The value
3764      * in memory is raw data, and need not correspond to any Java
3765      * variable.  The endianness of the value in memory is the
3766      * endianness of the native platform.
3767      * <p>
3768      * The write will be atomic with respect to the largest power of
3769      * two that divides the GCD of the offset and the storage size.
3770      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
3771      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3772      * respectively.  There are no other guarantees of atomicity.
3773      * <p>
3774      * 8-byte atomicity is only guaranteed on platforms on which
3775      * support atomic accesses to longs.
3776      *
3777      * @param o Java heap object in which the value resides, if any, else
3778      *        null
3779      * @param offset The offset in bytes from the start of the object
3780      * @param x the value to store
3781      * @throws RuntimeException No defined exceptions are thrown, not even
3782      *         {@link NullPointerException}
3783      * @since 9
3784      */
3785     @HotSpotIntrinsicCandidate
3786     public final void putLongUnaligned(Object o, long offset, long x) {
3787         if ((offset & 7) == 0) {
3788             putLong(o, offset, x);
3789         } else if ((offset & 3) == 0) {
3790             putLongParts(o, offset,
3791                          (int)(x >> 0),
3792                          (int)(x >>> 32));
3793         } else if ((offset & 1) == 0) {
3794             putLongParts(o, offset,
3795                          (short)(x >>> 0),
3796                          (short)(x >>> 16),
3797                          (short)(x >>> 32),
3798                          (short)(x >>> 48));
3799         } else {
3800             putLongParts(o, offset,
3801                          (byte)(x >>> 0),
3802                          (byte)(x >>> 8),
3803                          (byte)(x >>> 16),
3804                          (byte)(x >>> 24),
3805                          (byte)(x >>> 32),
3806                          (byte)(x >>> 40),
3807                          (byte)(x >>> 48),
3808                          (byte)(x >>> 56));
3809         }
3810     }
3811 
3812     /**
3813      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
3814      * argument which specifies the endianness of the value as stored in memory.
3815      * @param o Java heap object in which the value resides
3816      * @param offset The offset in bytes from the start of the object
3817      * @param x the value to store
3818      * @param bigEndian The endianness of the value
3819      * @throws RuntimeException No defined exceptions are thrown, not even
3820      *         {@link NullPointerException}
3821      * @since 9
3822      */
3823     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
3824         putLongUnaligned(o, offset, convEndian(bigEndian, x));
3825     }
3826 
3827     /** @see #putLongUnaligned(Object, long, long) */
3828     @HotSpotIntrinsicCandidate
3829     public final void putIntUnaligned(Object o, long offset, int x) {
3830         if ((offset & 3) == 0) {
3831             putInt(o, offset, x);
3832         } else if ((offset & 1) == 0) {
3833             putIntParts(o, offset,
3834                         (short)(x >> 0),
3835                         (short)(x >>> 16));
3836         } else {
3837             putIntParts(o, offset,
3838                         (byte)(x >>> 0),
3839                         (byte)(x >>> 8),
3840                         (byte)(x >>> 16),
3841                         (byte)(x >>> 24));
3842         }
3843     }
3844     /** @see #putLongUnaligned(Object, long, long, boolean) */
3845     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
3846         putIntUnaligned(o, offset, convEndian(bigEndian, x));
3847     }
3848 
3849     /** @see #putLongUnaligned(Object, long, long) */
3850     @HotSpotIntrinsicCandidate
3851     public final void putShortUnaligned(Object o, long offset, short x) {
3852         if ((offset & 1) == 0) {
3853             putShort(o, offset, x);
3854         } else {
3855             putShortParts(o, offset,
3856                           (byte)(x >>> 0),
3857                           (byte)(x >>> 8));
3858         }
3859     }
3860     /** @see #putLongUnaligned(Object, long, long, boolean) */
3861     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
3862         putShortUnaligned(o, offset, convEndian(bigEndian, x));
3863     }
3864 
3865     /** @see #putLongUnaligned(Object, long, long) */
3866     @HotSpotIntrinsicCandidate
3867     public final void putCharUnaligned(Object o, long offset, char x) {
3868         putShortUnaligned(o, offset, (short)x);
3869     }
3870     /** @see #putLongUnaligned(Object, long, long, boolean) */
3871     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
3872         putCharUnaligned(o, offset, convEndian(bigEndian, x));
3873     }
3874 
3875     // JVM interface methods
3876     // BE is true iff the native endianness of this platform is big.
3877     private static final boolean BE = theUnsafe.isBigEndian0();
3878 
3879     // unalignedAccess is true iff this platform can perform unaligned accesses.
3880     private static final boolean unalignedAccess = theUnsafe.unalignedAccess0();
3881 
3882     private static int pickPos(int top, int pos) { return BE ? top - pos : pos; }
3883 
3884     // These methods construct integers from bytes.  The byte ordering
3885     // is the native endianness of this platform.
3886     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
3887         return ((toUnsignedLong(i0) << pickPos(56, 0))
3888               | (toUnsignedLong(i1) << pickPos(56, 8))
3889               | (toUnsignedLong(i2) << pickPos(56, 16))
3890               | (toUnsignedLong(i3) << pickPos(56, 24))
3891               | (toUnsignedLong(i4) << pickPos(56, 32))
3892               | (toUnsignedLong(i5) << pickPos(56, 40))
3893               | (toUnsignedLong(i6) << pickPos(56, 48))
3894               | (toUnsignedLong(i7) << pickPos(56, 56)));
3895     }
3896     private static long makeLong(short i0, short i1, short i2, short i3) {
3897         return ((toUnsignedLong(i0) << pickPos(48, 0))
3898               | (toUnsignedLong(i1) << pickPos(48, 16))
3899               | (toUnsignedLong(i2) << pickPos(48, 32))
3900               | (toUnsignedLong(i3) << pickPos(48, 48)));
3901     }
3902     private static long makeLong(int i0, int i1) {
3903         return (toUnsignedLong(i0) << pickPos(32, 0))
3904              | (toUnsignedLong(i1) << pickPos(32, 32));
3905     }
3906     private static int makeInt(short i0, short i1) {
3907         return (toUnsignedInt(i0) << pickPos(16, 0))
3908              | (toUnsignedInt(i1) << pickPos(16, 16));
3909     }
3910     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
3911         return ((toUnsignedInt(i0) << pickPos(24, 0))
3912               | (toUnsignedInt(i1) << pickPos(24, 8))
3913               | (toUnsignedInt(i2) << pickPos(24, 16))
3914               | (toUnsignedInt(i3) << pickPos(24, 24)));
3915     }
3916     private static short makeShort(byte i0, byte i1) {
3917         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
3918                      | (toUnsignedInt(i1) << pickPos(8, 8)));
3919     }
3920 
3921     private static byte  pick(byte  le, byte  be) { return BE ? be : le; }
3922     private static short pick(short le, short be) { return BE ? be : le; }
3923     private static int   pick(int   le, int   be) { return BE ? be : le; }
3924 
3925     // These methods write integers to memory from smaller parts
3926     // provided by their caller.  The ordering in which these parts
3927     // are written is the native endianness of this platform.
3928     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
3929         putByte(o, offset + 0, pick(i0, i7));
3930         putByte(o, offset + 1, pick(i1, i6));
3931         putByte(o, offset + 2, pick(i2, i5));
3932         putByte(o, offset + 3, pick(i3, i4));
3933         putByte(o, offset + 4, pick(i4, i3));
3934         putByte(o, offset + 5, pick(i5, i2));
3935         putByte(o, offset + 6, pick(i6, i1));
3936         putByte(o, offset + 7, pick(i7, i0));
3937     }
3938     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
3939         putShort(o, offset + 0, pick(i0, i3));
3940         putShort(o, offset + 2, pick(i1, i2));
3941         putShort(o, offset + 4, pick(i2, i1));
3942         putShort(o, offset + 6, pick(i3, i0));
3943     }
3944     private void putLongParts(Object o, long offset, int i0, int i1) {
3945         putInt(o, offset + 0, pick(i0, i1));
3946         putInt(o, offset + 4, pick(i1, i0));
3947     }
3948     private void putIntParts(Object o, long offset, short i0, short i1) {
3949         putShort(o, offset + 0, pick(i0, i1));
3950         putShort(o, offset + 2, pick(i1, i0));
3951     }
3952     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
3953         putByte(o, offset + 0, pick(i0, i3));
3954         putByte(o, offset + 1, pick(i1, i2));
3955         putByte(o, offset + 2, pick(i2, i1));
3956         putByte(o, offset + 3, pick(i3, i0));
3957     }
3958     private void putShortParts(Object o, long offset, byte i0, byte i1) {
3959         putByte(o, offset + 0, pick(i0, i1));
3960         putByte(o, offset + 1, pick(i1, i0));
3961     }
3962 
3963     // Zero-extend an integer
3964     private static int toUnsignedInt(byte n)    { return n & 0xff; }
3965     private static int toUnsignedInt(short n)   { return n & 0xffff; }
3966     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
3967     private static long toUnsignedLong(short n) { return n & 0xffffl; }
3968     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
3969 
3970     // Maybe byte-reverse an integer
3971     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
3972     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
3973     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
3974     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
3975 
3976 
3977 
3978     private native long allocateMemory0(long bytes);
3979     private native long reallocateMemory0(long address, long bytes);
3980     private native void freeMemory0(long address);
3981     private native void setMemory0(Object o, long offset, long bytes, byte value);
3982     @HotSpotIntrinsicCandidate
3983     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3984     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3985     private native long objectFieldOffset0(Field f);
3986     private native long objectFieldOffset1(Class<?> c, String name);
3987     private native long staticFieldOffset0(Field f);
3988     private native Object staticFieldBase0(Field f);
3989     private native boolean shouldBeInitialized0(Class<?> c);
3990     private native void ensureClassInitialized0(Class<?> c);
3991     private native int arrayBaseOffset0(Class<?> arrayClass);
3992     private native int arrayIndexScale0(Class<?> arrayClass);
3993     private native int addressSize0();
3994     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3995     private native int getLoadAverage0(double[] loadavg, int nelems);
3996     private native boolean unalignedAccess0();
3997     private native boolean isBigEndian0();
3998 }