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