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