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