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