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     /**
1045      * Ensure writeback of a specified virtual memory address range
1046      * from cache to physical memory. All bytes in the address range
1047      * are guaranteed to have been written back to physical memory on
1048      * return from this call i.e. subsequently executed store
1049      * instructions are guaranteed not to be visible before the
1050      * writeback is completed.
1051      *
1052      * @param address
1053      *        the lowest byte address that must be guaranteed written
1054      *        back to memory. bytes at lower addresses may also be
1055      *        written back.
1056      *
1057      * @param length
1058      *        the length in bytes of the region starting at address
1059      *        that must be guaranteed written back to memory.
1060      *
1061      * @throws RuntimeException if memory writeback is not supported
1062      *         on the current hardware of if the arguments are invalid.
1063      *         (<em>Note:</em> after optimization, invalid inputs may
1064      *         go undetected, which will lead to unpredictable
1065      *         behavior)
1066      *
1067      * @since 14
1068      */
1069 
1070     public void writebackMemory(long address, long length) {
1071         checkWritebackEnabled();
1072         checkWritebackMemory(address, length);
1073 
1074         // perform any required pre-writeback barrier
1075         writebackPreSync0();
1076 
1077         // write back one cache line at a time
1078         long line = dataCacheLineAlignDown(address);
1079         long end = address + length;
1080         while (line < end) {
1081             writeback0(line);
1082             line += dataCacheLineFlushSize();
1083         }
1084 
1085         // perform any required post-writeback barrier
1086         writebackPostSync0();
1087     }
1088 
1089     /**
1090      * Validate the arguments to writebackMemory
1091      *
1092      * @throws RuntimeException if the arguments are invalid
1093      *         (<em>Note:</em> after optimization, invalid inputs may
1094      *         go undetected, which will lead to unpredictable
1095      *         behavior)
1096      */
1097     private void checkWritebackMemory(long address, long length) {
1098         checkNativeAddress(address);
1099         checkSize(length);
1100     }
1101 
1102     /**
1103      * Validate that the current hardware supports memory writeback.
1104      * (<em>Note:</em> this is a belt and braces check.  Clients are
1105      * expected to test whether writeback is enabled by calling
1106      * ({@link isWritebackEnabled #isWritebackEnabled} and avoid
1107      * calling method {@link writeback #writeback} if it is disabled).
1108      *
1109      *
1110      * @throws RuntimeException if memory writeback is not supported
1111      */
1112     private void checkWritebackEnabled() {
1113         if (!isWritebackEnabled()) {
1114             throw new RuntimeException("writebackMemory not enabled!");
1115         }
1116     }
1117 
1118     /**
1119      * force writeback of an individual cache line.
1120      *
1121      * @param address
1122      *        the start address of the cache line to be written back
1123      */
1124     @HotSpotIntrinsicCandidate
1125     private native void writeback0(long address);
1126 
1127      /**
1128       * Serialize writeback operations relative to preceding memory writes.
1129       */
1130     @HotSpotIntrinsicCandidate
1131     private native void writebackPreSync0();
1132 
1133      /**
1134       * Serialize writeback operations relative to following memory writes.
1135       */
1136     @HotSpotIntrinsicCandidate
1137     private native void writebackPostSync0();
1138 
1139     /// random queries
1140 
1141     /**
1142      * This constant differs from all results that will ever be returned from
1143      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1144      * or {@link #arrayBaseOffset}.
1145      */
1146     public static final int INVALID_FIELD_OFFSET = -1;
1147 
1148     /**
1149      * Reports the location of a given field in the storage allocation of its
1150      * class.  Do not expect to perform any sort of arithmetic on this offset;
1151      * it is just a cookie which is passed to the unsafe heap memory accessors.
1152      *
1153      * <p>Any given field will always have the same offset and base, and no
1154      * two distinct fields of the same class will ever have the same offset
1155      * and base.
1156      *
1157      * <p>As of 1.4.1, offsets for fields are represented as long values,
1158      * although the Sun JVM does not use the most significant 32 bits.
1159      * However, JVM implementations which store static fields at absolute
1160      * addresses can use long offsets and null base pointers to express
1161      * the field locations in a form usable by {@link #getInt(Object,long)}.
1162      * Therefore, code which will be ported to such JVMs on 64-bit platforms
1163      * must preserve all bits of static field offsets.
1164      * @see #getInt(Object, long)
1165      */
1166     public long objectFieldOffset(Field f) {
1167         if (f == null) {
1168             throw new NullPointerException();
1169         }
1170 
1171         return objectFieldOffset0(f);
1172     }
1173 
1174     /**
1175      * Reports the location of the field with a given name in the storage
1176      * allocation of its class.
1177      *
1178      * @throws NullPointerException if any parameter is {@code null}.
1179      * @throws InternalError if there is no field named {@code name} declared
1180      *         in class {@code c}, i.e., if {@code c.getDeclaredField(name)}
1181      *         would throw {@code java.lang.NoSuchFieldException}.
1182      *
1183      * @see #objectFieldOffset(Field)
1184      */
1185     public long objectFieldOffset(Class<?> c, String name) {
1186         if (c == null || name == null) {
1187             throw new NullPointerException();
1188         }
1189 
1190         return objectFieldOffset1(c, name);
1191     }
1192 
1193     /**
1194      * Reports the location of a given static field, in conjunction with {@link
1195      * #staticFieldBase}.
1196      * <p>Do not expect to perform any sort of arithmetic on this offset;
1197      * it is just a cookie which is passed to the unsafe heap memory accessors.
1198      *
1199      * <p>Any given field will always have the same offset, and no two distinct
1200      * fields of the same class will ever have the same offset.
1201      *
1202      * <p>As of 1.4.1, offsets for fields are represented as long values,
1203      * although the Sun JVM does not use the most significant 32 bits.
1204      * It is hard to imagine a JVM technology which needs more than
1205      * a few bits to encode an offset within a non-array object,
1206      * However, for consistency with other methods in this class,
1207      * this method reports its result as a long value.
1208      * @see #getInt(Object, long)
1209      */
1210     public long staticFieldOffset(Field f) {
1211         if (f == null) {
1212             throw new NullPointerException();
1213         }
1214 
1215         return staticFieldOffset0(f);
1216     }
1217 
1218     /**
1219      * Reports the location of a given static field, in conjunction with {@link
1220      * #staticFieldOffset}.
1221      * <p>Fetch the base "Object", if any, with which static fields of the
1222      * given class can be accessed via methods like {@link #getInt(Object,
1223      * long)}.  This value may be null.  This value may refer to an object
1224      * which is a "cookie", not guaranteed to be a real Object, and it should
1225      * not be used in any way except as argument to the get and put routines in
1226      * this class.
1227      */
1228     public Object staticFieldBase(Field f) {
1229         if (f == null) {
1230             throw new NullPointerException();
1231         }
1232 
1233         return staticFieldBase0(f);
1234     }
1235 
1236     /**
1237      * Detects if the given class may need to be initialized. This is often
1238      * needed in conjunction with obtaining the static field base of a
1239      * class.
1240      * @return false only if a call to {@code ensureClassInitialized} would have no effect
1241      */
1242     public boolean shouldBeInitialized(Class<?> c) {
1243         if (c == null) {
1244             throw new NullPointerException();
1245         }
1246 
1247         return shouldBeInitialized0(c);
1248     }
1249 
1250     /**
1251      * Ensures the given class has been initialized. This is often
1252      * needed in conjunction with obtaining the static field base of a
1253      * class.
1254      */
1255     public void ensureClassInitialized(Class<?> c) {
1256         if (c == null) {
1257             throw new NullPointerException();
1258         }
1259 
1260         ensureClassInitialized0(c);
1261     }
1262 
1263     /**
1264      * Reports the offset of the first element in the storage allocation of a
1265      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
1266      * for the same class, you may use that scale factor, together with this
1267      * base offset, to form new offsets to access elements of arrays of the
1268      * given class.
1269      *
1270      * @see #getInt(Object, long)
1271      * @see #putInt(Object, long, int)
1272      */
1273     public int arrayBaseOffset(Class<?> arrayClass) {
1274         if (arrayClass == null) {
1275             throw new NullPointerException();
1276         }
1277 
1278         return arrayBaseOffset0(arrayClass);
1279     }
1280 
1281 
1282     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1283     public static final int ARRAY_BOOLEAN_BASE_OFFSET
1284             = theUnsafe.arrayBaseOffset(boolean[].class);
1285 
1286     /** The value of {@code arrayBaseOffset(byte[].class)} */
1287     public static final int ARRAY_BYTE_BASE_OFFSET
1288             = theUnsafe.arrayBaseOffset(byte[].class);
1289 
1290     /** The value of {@code arrayBaseOffset(short[].class)} */
1291     public static final int ARRAY_SHORT_BASE_OFFSET
1292             = theUnsafe.arrayBaseOffset(short[].class);
1293 
1294     /** The value of {@code arrayBaseOffset(char[].class)} */
1295     public static final int ARRAY_CHAR_BASE_OFFSET
1296             = theUnsafe.arrayBaseOffset(char[].class);
1297 
1298     /** The value of {@code arrayBaseOffset(int[].class)} */
1299     public static final int ARRAY_INT_BASE_OFFSET
1300             = theUnsafe.arrayBaseOffset(int[].class);
1301 
1302     /** The value of {@code arrayBaseOffset(long[].class)} */
1303     public static final int ARRAY_LONG_BASE_OFFSET
1304             = theUnsafe.arrayBaseOffset(long[].class);
1305 
1306     /** The value of {@code arrayBaseOffset(float[].class)} */
1307     public static final int ARRAY_FLOAT_BASE_OFFSET
1308             = theUnsafe.arrayBaseOffset(float[].class);
1309 
1310     /** The value of {@code arrayBaseOffset(double[].class)} */
1311     public static final int ARRAY_DOUBLE_BASE_OFFSET
1312             = theUnsafe.arrayBaseOffset(double[].class);
1313 
1314     /** The value of {@code arrayBaseOffset(Object[].class)} */
1315     public static final int ARRAY_OBJECT_BASE_OFFSET
1316             = theUnsafe.arrayBaseOffset(Object[].class);
1317 
1318     /**
1319      * Reports the scale factor for addressing elements in the storage
1320      * allocation of a given array class.  However, arrays of "narrow" types
1321      * will generally not work properly with accessors like {@link
1322      * #getByte(Object, long)}, so the scale factor for such classes is reported
1323      * as zero.
1324      *
1325      * @see #arrayBaseOffset
1326      * @see #getInt(Object, long)
1327      * @see #putInt(Object, long, int)
1328      */
1329     public int arrayIndexScale(Class<?> arrayClass) {
1330         if (arrayClass == null) {
1331             throw new NullPointerException();
1332         }
1333 
1334         return arrayIndexScale0(arrayClass);
1335     }
1336 
1337     /**
1338      * Return the size of the object in the heap.
1339      * @param o an object
1340      * @return the objects's size
1341      * @since Valhalla
1342      */
1343     public long getObjectSize(Object o) {
1344         if (o == null)
1345             throw new NullPointerException();
1346         return getObjectSize0(o);
1347     }
1348 
1349     /** The value of {@code arrayIndexScale(boolean[].class)} */
1350     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1351             = theUnsafe.arrayIndexScale(boolean[].class);
1352 
1353     /** The value of {@code arrayIndexScale(byte[].class)} */
1354     public static final int ARRAY_BYTE_INDEX_SCALE
1355             = theUnsafe.arrayIndexScale(byte[].class);
1356 
1357     /** The value of {@code arrayIndexScale(short[].class)} */
1358     public static final int ARRAY_SHORT_INDEX_SCALE
1359             = theUnsafe.arrayIndexScale(short[].class);
1360 
1361     /** The value of {@code arrayIndexScale(char[].class)} */
1362     public static final int ARRAY_CHAR_INDEX_SCALE
1363             = theUnsafe.arrayIndexScale(char[].class);
1364 
1365     /** The value of {@code arrayIndexScale(int[].class)} */
1366     public static final int ARRAY_INT_INDEX_SCALE
1367             = theUnsafe.arrayIndexScale(int[].class);
1368 
1369     /** The value of {@code arrayIndexScale(long[].class)} */
1370     public static final int ARRAY_LONG_INDEX_SCALE
1371             = theUnsafe.arrayIndexScale(long[].class);
1372 
1373     /** The value of {@code arrayIndexScale(float[].class)} */
1374     public static final int ARRAY_FLOAT_INDEX_SCALE
1375             = theUnsafe.arrayIndexScale(float[].class);
1376 
1377     /** The value of {@code arrayIndexScale(double[].class)} */
1378     public static final int ARRAY_DOUBLE_INDEX_SCALE
1379             = theUnsafe.arrayIndexScale(double[].class);
1380 
1381     /** The value of {@code arrayIndexScale(Object[].class)} */
1382     public static final int ARRAY_OBJECT_INDEX_SCALE
1383             = theUnsafe.arrayIndexScale(Object[].class);
1384 
1385     /**
1386      * Reports the size in bytes of a native pointer, as stored via {@link
1387      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1388      * other primitive types (as stored in native memory blocks) is determined
1389      * fully by their information content.
1390      */
1391     public int addressSize() {
1392         return ADDRESS_SIZE;
1393     }
1394 
1395     /** The value of {@code addressSize()} */
1396     public static final int ADDRESS_SIZE = ADDRESS_SIZE0;
1397 
1398     /**
1399      * Reports the size in bytes of a native memory page (whatever that is).
1400      * This value will always be a power of two.
1401      */
1402     public int pageSize() { return PAGE_SIZE; }
1403 
1404     /**
1405      * Reports the size in bytes of a data cache line written back by
1406      * the hardware cache line flush operation available to the JVM or
1407      * 0 if data cache line flushing is not enabled.
1408      */
1409     public int dataCacheLineFlushSize() { return DATA_CACHE_LINE_FLUSH_SIZE; }
1410 
1411     /**
1412      * Rounds down address to a data cache line boundary as
1413      * determined by {@link #dataCacheLineFlushSize}
1414      * @return the rounded down address
1415      */
1416     public long dataCacheLineAlignDown(long address) {
1417         return (address & ~(DATA_CACHE_LINE_FLUSH_SIZE - 1));
1418     }
1419 
1420     /**
1421      * Returns true if data cache line writeback
1422      */
1423     public static boolean isWritebackEnabled() { return DATA_CACHE_LINE_FLUSH_SIZE != 0; }
1424 
1425     /// random trusted operations from JNI:
1426 
1427     /**
1428      * Tells the VM to define a class, without security checks.  By default, the
1429      * class loader and protection domain come from the caller's class.
1430      */
1431     public Class<?> defineClass(String name, byte[] b, int off, int len,
1432                                 ClassLoader loader,
1433                                 ProtectionDomain protectionDomain) {
1434         if (b == null) {
1435             throw new NullPointerException();
1436         }
1437         if (len < 0) {
1438             throw new ArrayIndexOutOfBoundsException();
1439         }
1440 
1441         return defineClass0(name, b, off, len, loader, protectionDomain);
1442     }
1443 
1444     public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1445                                         ClassLoader loader,
1446                                         ProtectionDomain protectionDomain);
1447 
1448     /**
1449      * Defines a class but does not make it known to the class loader or system dictionary.
1450      * <p>
1451      * For each CP entry, the corresponding CP patch must either be null or have
1452      * the a format that matches its tag:
1453      * <ul>
1454      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
1455      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
1456      * <li>Class: any java.lang.Class object
1457      * <li>String: any object (not just a java.lang.String)
1458      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1459      * </ul>
1460      * @param hostClass context for linkage, access control, protection domain, and class loader
1461      * @param data      bytes of a class file
1462      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
1463      */
1464     public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
1465         if (hostClass == null || data == null) {
1466             throw new NullPointerException();
1467         }
1468         if (hostClass.isArray() || hostClass.isPrimitive()) {
1469             throw new IllegalArgumentException();
1470         }
1471 
1472         return defineAnonymousClass0(hostClass, data, cpPatches);
1473     }
1474 
1475     /**
1476      * Allocates an instance but does not run any constructor.
1477      * Initializes the class if it has not yet been.
1478      */
1479     @HotSpotIntrinsicCandidate
1480     public native Object allocateInstance(Class<?> cls)
1481         throws InstantiationException;
1482 
1483     /**
1484      * Allocates an array of a given type, but does not do zeroing.
1485      * <p>
1486      * This method should only be used in the very rare cases where a high-performance code
1487      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1488      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1489      * <p>
1490      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1491      * before allowing untrusted code, or code in other threads, to observe the reference
1492      * to the newly allocated array. In addition, the publication of the array reference must be
1493      * safe according to the Java Memory Model requirements.
1494      * <p>
1495      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1496      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1497      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1498      * or issuing a {@link #storeFence} before publishing the reference.
1499      * <p>
1500      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1501      * elements that could break heap integrity.
1502      *
1503      * @param componentType array component type to allocate
1504      * @param length array size to allocate
1505      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1506      *                                  or the length is negative
1507      */
1508     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1509        if (componentType == null) {
1510            throw new IllegalArgumentException("Component type is null");
1511        }
1512        if (!componentType.isPrimitive()) {
1513            throw new IllegalArgumentException("Component type is not primitive");
1514        }
1515        if (length < 0) {
1516            throw new IllegalArgumentException("Negative length");
1517        }
1518        return allocateUninitializedArray0(componentType, length);
1519     }
1520 
1521     @HotSpotIntrinsicCandidate
1522     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1523        // These fallbacks provide zeroed arrays, but intrinsic is not required to
1524        // return the zeroed arrays.
1525        if (componentType == byte.class)    return new byte[length];
1526        if (componentType == boolean.class) return new boolean[length];
1527        if (componentType == short.class)   return new short[length];
1528        if (componentType == char.class)    return new char[length];
1529        if (componentType == int.class)     return new int[length];
1530        if (componentType == float.class)   return new float[length];
1531        if (componentType == long.class)    return new long[length];
1532        if (componentType == double.class)  return new double[length];
1533        return null;
1534     }
1535 
1536     /** Throws the exception without telling the verifier. */
1537     public native void throwException(Throwable ee);
1538 
1539     /**
1540      * Atomically updates Java variable to {@code x} if it is currently
1541      * holding {@code expected}.
1542      *
1543      * <p>This operation has memory semantics of a {@code volatile} read
1544      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1545      *
1546      * @return {@code true} if successful
1547      */
1548     @HotSpotIntrinsicCandidate
1549     public final native boolean compareAndSetReference(Object o, long offset,
1550                                                        Object expected,
1551                                                        Object x);
1552 
1553     private final boolean isInlineType(Object o) {
1554         return o != null && o.getClass().isInlineClass();
1555     }
1556 
1557     /*
1558      * For inline type, CAS should do substitutability test as opposed
1559      * to two pointers comparison.
1560      *
1561      * Perhaps we can keep the xxxObject methods for compatibility and
1562      * change the JDK 13 xxxReference method signature freely.
1563      */
1564     public final <V> boolean compareAndSetReference(Object o, long offset,
1565                                                     Class<?> valueType,
1566                                                     V expected,
1567                                                     V x) {
1568         if (valueType.isInlineClass() || isInlineType(expected)) {
1569             synchronized (valueLock) {
1570                 Object witness = getReference(o, offset);
1571                 if (witness == expected) {
1572                     putReference(o, offset, x);
1573                     return true;
1574                 } else {
1575                     return false;
1576                 }
1577             }
1578         } else {
1579             return compareAndSetReference(o, offset, expected, x);
1580         }
1581     }
1582 
1583     @ForceInline
1584     public final <V> boolean compareAndSetValue(Object o, long offset,
1585                                                 Class<?> valueType,
1586                                                 V expected,
1587                                                 V x) {
1588         synchronized (valueLock) {
1589             Object witness = getValue(o, offset, valueType);
1590             if (witness == expected) {
1591                 putValue(o, offset, valueType, x);
1592                 return true;
1593             }
1594             else {
1595                 return false;
1596             }
1597         }
1598     }
1599 
1600     @HotSpotIntrinsicCandidate
1601     public final native Object compareAndExchangeReference(Object o, long offset,
1602                                                            Object expected,
1603                                                            Object x);
1604 
1605     public final <V> Object compareAndExchangeReference(Object o, long offset,
1606                                                         Class<?> valueType,
1607                                                         V expected,
1608                                                         V x) {
1609         if (valueType.isInlineClass() || isInlineType(expected)) {
1610             synchronized (valueLock) {
1611                 Object witness = getReference(o, offset);
1612                 if (witness == expected) {
1613                     putReference(o, offset, x);
1614                 }
1615                 return witness;
1616             }
1617         } else {
1618             return compareAndExchangeReference(o, offset, expected, x);
1619         }
1620     }
1621 
1622     @ForceInline
1623     public final <V> Object compareAndExchangeValue(Object o, long offset,
1624                                                     Class<?> valueType,
1625                                                     V expected,
1626                                                     V x) {
1627         synchronized (valueLock) {
1628             Object witness = getValue(o, offset, valueType);
1629             if (witness == expected) {
1630                 putValue(o, offset, valueType, x);
1631             }
1632             return witness;
1633         }
1634     }
1635 
1636     @HotSpotIntrinsicCandidate
1637     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1638                                                            Object expected,
1639                                                            Object x) {
1640         return compareAndExchangeReference(o, offset, expected, x);
1641     }
1642 
1643     public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1644                                                                Class<?> valueType,
1645                                                                V expected,
1646                                                                V x) {
1647         return compareAndExchangeReference(o, offset, valueType, expected, x);
1648     }
1649 
1650     @ForceInline
1651     public final <V> Object compareAndExchangeValueAcquire(Object o, long offset,
1652                                                            Class<?> valueType,
1653                                                            V expected,
1654                                                            V x) {
1655         return compareAndExchangeValue(o, offset, valueType, expected, x);
1656     }
1657 
1658     @HotSpotIntrinsicCandidate
1659     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1660                                                            Object expected,
1661                                                            Object x) {
1662         return compareAndExchangeReference(o, offset, expected, x);
1663     }
1664 
1665     public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1666                                                                Class<?> valueType,
1667                                                                V expected,
1668                                                                V x) {
1669         return compareAndExchangeReference(o, offset, valueType, expected, x);
1670     }
1671 
1672     @ForceInline
1673     public final <V> Object compareAndExchangeValueRelease(Object o, long offset,
1674                                                            Class<?> valueType,
1675                                                            V expected,
1676                                                            V x) {
1677         return compareAndExchangeValue(o, offset, valueType, expected, x);
1678     }
1679 
1680     @HotSpotIntrinsicCandidate
1681     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1682                                                          Object expected,
1683                                                          Object x) {
1684         return compareAndSetReference(o, offset, expected, x);
1685     }
1686 
1687     public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1688                                                              Class<?> valueType,
1689                                                              V expected,
1690                                                              V x) {
1691         if (valueType.isInlineClass() || isInlineType(expected)) {
1692             return compareAndSetReference(o, offset, valueType, expected, x);
1693         } else {
1694             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1695         }
1696     }
1697 
1698     @ForceInline
1699     public final <V> boolean weakCompareAndSetValuePlain(Object o, long offset,
1700                                                          Class<?> valueType,
1701                                                          V expected,
1702                                                          V x) {
1703         return compareAndSetValue(o, offset, valueType, expected, x);
1704     }
1705 
1706     @HotSpotIntrinsicCandidate
1707     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1708                                                            Object expected,
1709                                                            Object x) {
1710         return compareAndSetReference(o, offset, expected, x);
1711     }
1712 
1713     public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1714                                                                Class<?> valueType,
1715                                                                V expected,
1716                                                                V x) {
1717         if (valueType.isInlineClass() || isInlineType(expected)) {
1718             return compareAndSetReference(o, offset, valueType, expected, x);
1719         } else {
1720             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1721         }
1722     }
1723 
1724     @ForceInline
1725     public final <V> boolean weakCompareAndSetValueAcquire(Object o, long offset,
1726                                                            Class<?> valueType,
1727                                                            V expected,
1728                                                            V x) {
1729         return compareAndSetValue(o, offset, valueType, expected, x);
1730     }
1731 
1732     @HotSpotIntrinsicCandidate
1733     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1734                                                            Object expected,
1735                                                            Object x) {
1736         return compareAndSetReference(o, offset, expected, x);
1737     }
1738 
1739     public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1740                                                                Class<?> valueType,
1741                                                                V expected,
1742                                                                V x) {
1743         if (valueType.isInlineClass() || isInlineType(expected)) {
1744             return compareAndSetReference(o, offset, valueType, expected, x);
1745         } else {
1746             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1747         }
1748     }
1749 
1750     @ForceInline
1751     public final <V> boolean weakCompareAndSetValueRelease(Object o, long offset,
1752                                                            Class<?> valueType,
1753                                                            V expected,
1754                                                            V x) {
1755         return compareAndSetValue(o, offset, valueType, expected, x);
1756     }
1757 
1758     @HotSpotIntrinsicCandidate
1759     public final boolean weakCompareAndSetReference(Object o, long offset,
1760                                                     Object expected,
1761                                                     Object x) {
1762         return compareAndSetReference(o, offset, expected, x);
1763     }
1764 
1765     public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1766                                                         Class<?> valueType,
1767                                                         V expected,
1768                                                         V x) {
1769         if (valueType.isInlineClass() || isInlineType(expected)) {
1770             return compareAndSetReference(o, offset, valueType, expected, x);
1771         } else {
1772             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1773         }
1774     }
1775 
1776     @ForceInline
1777     public final <V> boolean weakCompareAndSetValue(Object o, long offset,
1778                                                     Class<?> valueType,
1779                                                     V expected,
1780                                                     V x) {
1781         return compareAndSetValue(o, offset, valueType, expected, x);
1782     }
1783 
1784     /**
1785      * Atomically updates Java variable to {@code x} if it is currently
1786      * holding {@code expected}.
1787      *
1788      * <p>This operation has memory semantics of a {@code volatile} read
1789      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1790      *
1791      * @return {@code true} if successful
1792      */
1793     @HotSpotIntrinsicCandidate
1794     public final native boolean compareAndSetInt(Object o, long offset,
1795                                                  int expected,
1796                                                  int x);
1797 
1798     @HotSpotIntrinsicCandidate
1799     public final native int compareAndExchangeInt(Object o, long offset,
1800                                                   int expected,
1801                                                   int x);
1802 
1803     @HotSpotIntrinsicCandidate
1804     public final int compareAndExchangeIntAcquire(Object o, long offset,
1805                                                          int expected,
1806                                                          int x) {
1807         return compareAndExchangeInt(o, offset, expected, x);
1808     }
1809 
1810     @HotSpotIntrinsicCandidate
1811     public final int compareAndExchangeIntRelease(Object o, long offset,
1812                                                          int expected,
1813                                                          int x) {
1814         return compareAndExchangeInt(o, offset, expected, x);
1815     }
1816 
1817     @HotSpotIntrinsicCandidate
1818     public final boolean weakCompareAndSetIntPlain(Object o, long offset,
1819                                                    int expected,
1820                                                    int x) {
1821         return compareAndSetInt(o, offset, expected, x);
1822     }
1823 
1824     @HotSpotIntrinsicCandidate
1825     public final boolean weakCompareAndSetIntAcquire(Object o, long offset,
1826                                                      int expected,
1827                                                      int x) {
1828         return compareAndSetInt(o, offset, expected, x);
1829     }
1830 
1831     @HotSpotIntrinsicCandidate
1832     public final boolean weakCompareAndSetIntRelease(Object o, long offset,
1833                                                      int expected,
1834                                                      int x) {
1835         return compareAndSetInt(o, offset, expected, x);
1836     }
1837 
1838     @HotSpotIntrinsicCandidate
1839     public final boolean weakCompareAndSetInt(Object o, long offset,
1840                                               int expected,
1841                                               int x) {
1842         return compareAndSetInt(o, offset, expected, x);
1843     }
1844 
1845     @HotSpotIntrinsicCandidate
1846     public final byte compareAndExchangeByte(Object o, long offset,
1847                                              byte expected,
1848                                              byte x) {
1849         long wordOffset = offset & ~3;
1850         int shift = (int) (offset & 3) << 3;
1851         if (BIG_ENDIAN) {
1852             shift = 24 - shift;
1853         }
1854         int mask           = 0xFF << shift;
1855         int maskedExpected = (expected & 0xFF) << shift;
1856         int maskedX        = (x & 0xFF) << shift;
1857         int fullWord;
1858         do {
1859             fullWord = getIntVolatile(o, wordOffset);
1860             if ((fullWord & mask) != maskedExpected)
1861                 return (byte) ((fullWord & mask) >> shift);
1862         } while (!weakCompareAndSetInt(o, wordOffset,
1863                                                 fullWord, (fullWord & ~mask) | maskedX));
1864         return expected;
1865     }
1866 
1867     @HotSpotIntrinsicCandidate
1868     public final boolean compareAndSetByte(Object o, long offset,
1869                                            byte expected,
1870                                            byte x) {
1871         return compareAndExchangeByte(o, offset, expected, x) == expected;
1872     }
1873 
1874     @HotSpotIntrinsicCandidate
1875     public final boolean weakCompareAndSetByte(Object o, long offset,
1876                                                byte expected,
1877                                                byte x) {
1878         return compareAndSetByte(o, offset, expected, x);
1879     }
1880 
1881     @HotSpotIntrinsicCandidate
1882     public final boolean weakCompareAndSetByteAcquire(Object o, long offset,
1883                                                       byte expected,
1884                                                       byte x) {
1885         return weakCompareAndSetByte(o, offset, expected, x);
1886     }
1887 
1888     @HotSpotIntrinsicCandidate
1889     public final boolean weakCompareAndSetByteRelease(Object o, long offset,
1890                                                       byte expected,
1891                                                       byte x) {
1892         return weakCompareAndSetByte(o, offset, expected, x);
1893     }
1894 
1895     @HotSpotIntrinsicCandidate
1896     public final boolean weakCompareAndSetBytePlain(Object o, long offset,
1897                                                     byte expected,
1898                                                     byte x) {
1899         return weakCompareAndSetByte(o, offset, expected, x);
1900     }
1901 
1902     @HotSpotIntrinsicCandidate
1903     public final byte compareAndExchangeByteAcquire(Object o, long offset,
1904                                                     byte expected,
1905                                                     byte x) {
1906         return compareAndExchangeByte(o, offset, expected, x);
1907     }
1908 
1909     @HotSpotIntrinsicCandidate
1910     public final byte compareAndExchangeByteRelease(Object o, long offset,
1911                                                     byte expected,
1912                                                     byte x) {
1913         return compareAndExchangeByte(o, offset, expected, x);
1914     }
1915 
1916     @HotSpotIntrinsicCandidate
1917     public final short compareAndExchangeShort(Object o, long offset,
1918                                                short expected,
1919                                                short x) {
1920         if ((offset & 3) == 3) {
1921             throw new IllegalArgumentException("Update spans the word, not supported");
1922         }
1923         long wordOffset = offset & ~3;
1924         int shift = (int) (offset & 3) << 3;
1925         if (BIG_ENDIAN) {
1926             shift = 16 - shift;
1927         }
1928         int mask           = 0xFFFF << shift;
1929         int maskedExpected = (expected & 0xFFFF) << shift;
1930         int maskedX        = (x & 0xFFFF) << shift;
1931         int fullWord;
1932         do {
1933             fullWord = getIntVolatile(o, wordOffset);
1934             if ((fullWord & mask) != maskedExpected) {
1935                 return (short) ((fullWord & mask) >> shift);
1936             }
1937         } while (!weakCompareAndSetInt(o, wordOffset,
1938                                                 fullWord, (fullWord & ~mask) | maskedX));
1939         return expected;
1940     }
1941 
1942     @HotSpotIntrinsicCandidate
1943     public final boolean compareAndSetShort(Object o, long offset,
1944                                             short expected,
1945                                             short x) {
1946         return compareAndExchangeShort(o, offset, expected, x) == expected;
1947     }
1948 
1949     @HotSpotIntrinsicCandidate
1950     public final boolean weakCompareAndSetShort(Object o, long offset,
1951                                                 short expected,
1952                                                 short x) {
1953         return compareAndSetShort(o, offset, expected, x);
1954     }
1955 
1956     @HotSpotIntrinsicCandidate
1957     public final boolean weakCompareAndSetShortAcquire(Object o, long offset,
1958                                                        short expected,
1959                                                        short x) {
1960         return weakCompareAndSetShort(o, offset, expected, x);
1961     }
1962 
1963     @HotSpotIntrinsicCandidate
1964     public final boolean weakCompareAndSetShortRelease(Object o, long offset,
1965                                                        short expected,
1966                                                        short x) {
1967         return weakCompareAndSetShort(o, offset, expected, x);
1968     }
1969 
1970     @HotSpotIntrinsicCandidate
1971     public final boolean weakCompareAndSetShortPlain(Object o, long offset,
1972                                                      short expected,
1973                                                      short x) {
1974         return weakCompareAndSetShort(o, offset, expected, x);
1975     }
1976 
1977 
1978     @HotSpotIntrinsicCandidate
1979     public final short compareAndExchangeShortAcquire(Object o, long offset,
1980                                                      short expected,
1981                                                      short x) {
1982         return compareAndExchangeShort(o, offset, expected, x);
1983     }
1984 
1985     @HotSpotIntrinsicCandidate
1986     public final short compareAndExchangeShortRelease(Object o, long offset,
1987                                                     short expected,
1988                                                     short x) {
1989         return compareAndExchangeShort(o, offset, expected, x);
1990     }
1991 
1992     @ForceInline
1993     private char s2c(short s) {
1994         return (char) s;
1995     }
1996 
1997     @ForceInline
1998     private short c2s(char s) {
1999         return (short) s;
2000     }
2001 
2002     @ForceInline
2003     public final boolean compareAndSetChar(Object o, long offset,
2004                                            char expected,
2005                                            char x) {
2006         return compareAndSetShort(o, offset, c2s(expected), c2s(x));
2007     }
2008 
2009     @ForceInline
2010     public final char compareAndExchangeChar(Object o, long offset,
2011                                              char expected,
2012                                              char x) {
2013         return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x)));
2014     }
2015 
2016     @ForceInline
2017     public final char compareAndExchangeCharAcquire(Object o, long offset,
2018                                             char expected,
2019                                             char x) {
2020         return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x)));
2021     }
2022 
2023     @ForceInline
2024     public final char compareAndExchangeCharRelease(Object o, long offset,
2025                                             char expected,
2026                                             char x) {
2027         return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x)));
2028     }
2029 
2030     @ForceInline
2031     public final boolean weakCompareAndSetChar(Object o, long offset,
2032                                                char expected,
2033                                                char x) {
2034         return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x));
2035     }
2036 
2037     @ForceInline
2038     public final boolean weakCompareAndSetCharAcquire(Object o, long offset,
2039                                                       char expected,
2040                                                       char x) {
2041         return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x));
2042     }
2043 
2044     @ForceInline
2045     public final boolean weakCompareAndSetCharRelease(Object o, long offset,
2046                                                       char expected,
2047                                                       char x) {
2048         return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x));
2049     }
2050 
2051     @ForceInline
2052     public final boolean weakCompareAndSetCharPlain(Object o, long offset,
2053                                                     char expected,
2054                                                     char x) {
2055         return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x));
2056     }
2057 
2058     /**
2059      * The JVM converts integral values to boolean values using two
2060      * different conventions, byte testing against zero and truncation
2061      * to least-significant bit.
2062      *
2063      * <p>The JNI documents specify that, at least for returning
2064      * values from native methods, a Java boolean value is converted
2065      * to the value-set 0..1 by first truncating to a byte (0..255 or
2066      * maybe -128..127) and then testing against zero. Thus, Java
2067      * booleans in non-Java data structures are by convention
2068      * represented as 8-bit containers containing either zero (for
2069      * false) or any non-zero value (for true).
2070      *
2071      * <p>Java booleans in the heap are also stored in bytes, but are
2072      * strongly normalized to the value-set 0..1 (i.e., they are
2073      * truncated to the least-significant bit).
2074      *
2075      * <p>The main reason for having different conventions for
2076      * conversion is performance: Truncation to the least-significant
2077      * bit can be usually implemented with fewer (machine)
2078      * instructions than byte testing against zero.
2079      *
2080      * <p>A number of Unsafe methods load boolean values from the heap
2081      * as bytes. Unsafe converts those values according to the JNI
2082      * rules (i.e, using the "testing against zero" convention). The
2083      * method {@code byte2bool} implements that conversion.
2084      *
2085      * @param b the byte to be converted to boolean
2086      * @return the result of the conversion
2087      */
2088     @ForceInline
2089     private boolean byte2bool(byte b) {
2090         return b != 0;
2091     }
2092 
2093     /**
2094      * Convert a boolean value to a byte. The return value is strongly
2095      * normalized to the value-set 0..1 (i.e., the value is truncated
2096      * to the least-significant bit). See {@link #byte2bool(byte)} for
2097      * more details on conversion conventions.
2098      *
2099      * @param b the boolean to be converted to byte (and then normalized)
2100      * @return the result of the conversion
2101      */
2102     @ForceInline
2103     private byte bool2byte(boolean b) {
2104         return b ? (byte)1 : (byte)0;
2105     }
2106 
2107     @ForceInline
2108     public final boolean compareAndSetBoolean(Object o, long offset,
2109                                               boolean expected,
2110                                               boolean x) {
2111         return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2112     }
2113 
2114     @ForceInline
2115     public final boolean compareAndExchangeBoolean(Object o, long offset,
2116                                                    boolean expected,
2117                                                    boolean x) {
2118         return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x)));
2119     }
2120 
2121     @ForceInline
2122     public final boolean compareAndExchangeBooleanAcquire(Object o, long offset,
2123                                                     boolean expected,
2124                                                     boolean x) {
2125         return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x)));
2126     }
2127 
2128     @ForceInline
2129     public final boolean compareAndExchangeBooleanRelease(Object o, long offset,
2130                                                        boolean expected,
2131                                                        boolean x) {
2132         return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x)));
2133     }
2134 
2135     @ForceInline
2136     public final boolean weakCompareAndSetBoolean(Object o, long offset,
2137                                                   boolean expected,
2138                                                   boolean x) {
2139         return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2140     }
2141 
2142     @ForceInline
2143     public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset,
2144                                                          boolean expected,
2145                                                          boolean x) {
2146         return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x));
2147     }
2148 
2149     @ForceInline
2150     public final boolean weakCompareAndSetBooleanRelease(Object o, long offset,
2151                                                          boolean expected,
2152                                                          boolean x) {
2153         return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x));
2154     }
2155 
2156     @ForceInline
2157     public final boolean weakCompareAndSetBooleanPlain(Object o, long offset,
2158                                                        boolean expected,
2159                                                        boolean x) {
2160         return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x));
2161     }
2162 
2163     /**
2164      * Atomically updates Java variable to {@code x} if it is currently
2165      * holding {@code expected}.
2166      *
2167      * <p>This operation has memory semantics of a {@code volatile} read
2168      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2169      *
2170      * @return {@code true} if successful
2171      */
2172     @ForceInline
2173     public final boolean compareAndSetFloat(Object o, long offset,
2174                                             float expected,
2175                                             float x) {
2176         return compareAndSetInt(o, offset,
2177                                  Float.floatToRawIntBits(expected),
2178                                  Float.floatToRawIntBits(x));
2179     }
2180 
2181     @ForceInline
2182     public final float compareAndExchangeFloat(Object o, long offset,
2183                                                float expected,
2184                                                float x) {
2185         int w = compareAndExchangeInt(o, offset,
2186                                       Float.floatToRawIntBits(expected),
2187                                       Float.floatToRawIntBits(x));
2188         return Float.intBitsToFloat(w);
2189     }
2190 
2191     @ForceInline
2192     public final float compareAndExchangeFloatAcquire(Object o, long offset,
2193                                                   float expected,
2194                                                   float x) {
2195         int w = compareAndExchangeIntAcquire(o, offset,
2196                                              Float.floatToRawIntBits(expected),
2197                                              Float.floatToRawIntBits(x));
2198         return Float.intBitsToFloat(w);
2199     }
2200 
2201     @ForceInline
2202     public final float compareAndExchangeFloatRelease(Object o, long offset,
2203                                                   float expected,
2204                                                   float x) {
2205         int w = compareAndExchangeIntRelease(o, offset,
2206                                              Float.floatToRawIntBits(expected),
2207                                              Float.floatToRawIntBits(x));
2208         return Float.intBitsToFloat(w);
2209     }
2210 
2211     @ForceInline
2212     public final boolean weakCompareAndSetFloatPlain(Object o, long offset,
2213                                                      float expected,
2214                                                      float x) {
2215         return weakCompareAndSetIntPlain(o, offset,
2216                                      Float.floatToRawIntBits(expected),
2217                                      Float.floatToRawIntBits(x));
2218     }
2219 
2220     @ForceInline
2221     public final boolean weakCompareAndSetFloatAcquire(Object o, long offset,
2222                                                        float expected,
2223                                                        float x) {
2224         return weakCompareAndSetIntAcquire(o, offset,
2225                                             Float.floatToRawIntBits(expected),
2226                                             Float.floatToRawIntBits(x));
2227     }
2228 
2229     @ForceInline
2230     public final boolean weakCompareAndSetFloatRelease(Object o, long offset,
2231                                                        float expected,
2232                                                        float x) {
2233         return weakCompareAndSetIntRelease(o, offset,
2234                                             Float.floatToRawIntBits(expected),
2235                                             Float.floatToRawIntBits(x));
2236     }
2237 
2238     @ForceInline
2239     public final boolean weakCompareAndSetFloat(Object o, long offset,
2240                                                 float expected,
2241                                                 float x) {
2242         return weakCompareAndSetInt(o, offset,
2243                                              Float.floatToRawIntBits(expected),
2244                                              Float.floatToRawIntBits(x));
2245     }
2246 
2247     /**
2248      * Atomically updates Java variable to {@code x} if it is currently
2249      * holding {@code expected}.
2250      *
2251      * <p>This operation has memory semantics of a {@code volatile} read
2252      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2253      *
2254      * @return {@code true} if successful
2255      */
2256     @ForceInline
2257     public final boolean compareAndSetDouble(Object o, long offset,
2258                                              double expected,
2259                                              double x) {
2260         return compareAndSetLong(o, offset,
2261                                  Double.doubleToRawLongBits(expected),
2262                                  Double.doubleToRawLongBits(x));
2263     }
2264 
2265     @ForceInline
2266     public final double compareAndExchangeDouble(Object o, long offset,
2267                                                  double expected,
2268                                                  double x) {
2269         long w = compareAndExchangeLong(o, offset,
2270                                         Double.doubleToRawLongBits(expected),
2271                                         Double.doubleToRawLongBits(x));
2272         return Double.longBitsToDouble(w);
2273     }
2274 
2275     @ForceInline
2276     public final double compareAndExchangeDoubleAcquire(Object o, long offset,
2277                                                         double expected,
2278                                                         double x) {
2279         long w = compareAndExchangeLongAcquire(o, offset,
2280                                                Double.doubleToRawLongBits(expected),
2281                                                Double.doubleToRawLongBits(x));
2282         return Double.longBitsToDouble(w);
2283     }
2284 
2285     @ForceInline
2286     public final double compareAndExchangeDoubleRelease(Object o, long offset,
2287                                                         double expected,
2288                                                         double x) {
2289         long w = compareAndExchangeLongRelease(o, offset,
2290                                                Double.doubleToRawLongBits(expected),
2291                                                Double.doubleToRawLongBits(x));
2292         return Double.longBitsToDouble(w);
2293     }
2294 
2295     @ForceInline
2296     public final boolean weakCompareAndSetDoublePlain(Object o, long offset,
2297                                                       double expected,
2298                                                       double x) {
2299         return weakCompareAndSetLongPlain(o, offset,
2300                                      Double.doubleToRawLongBits(expected),
2301                                      Double.doubleToRawLongBits(x));
2302     }
2303 
2304     @ForceInline
2305     public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset,
2306                                                         double expected,
2307                                                         double x) {
2308         return weakCompareAndSetLongAcquire(o, offset,
2309                                              Double.doubleToRawLongBits(expected),
2310                                              Double.doubleToRawLongBits(x));
2311     }
2312 
2313     @ForceInline
2314     public final boolean weakCompareAndSetDoubleRelease(Object o, long offset,
2315                                                         double expected,
2316                                                         double x) {
2317         return weakCompareAndSetLongRelease(o, offset,
2318                                              Double.doubleToRawLongBits(expected),
2319                                              Double.doubleToRawLongBits(x));
2320     }
2321 
2322     @ForceInline
2323     public final boolean weakCompareAndSetDouble(Object o, long offset,
2324                                                  double expected,
2325                                                  double x) {
2326         return weakCompareAndSetLong(o, offset,
2327                                               Double.doubleToRawLongBits(expected),
2328                                               Double.doubleToRawLongBits(x));
2329     }
2330 
2331     /**
2332      * Atomically updates Java variable to {@code x} if it is currently
2333      * holding {@code expected}.
2334      *
2335      * <p>This operation has memory semantics of a {@code volatile} read
2336      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2337      *
2338      * @return {@code true} if successful
2339      */
2340     @HotSpotIntrinsicCandidate
2341     public final native boolean compareAndSetLong(Object o, long offset,
2342                                                   long expected,
2343                                                   long x);
2344 
2345     @HotSpotIntrinsicCandidate
2346     public final native long compareAndExchangeLong(Object o, long offset,
2347                                                     long expected,
2348                                                     long x);
2349 
2350     @HotSpotIntrinsicCandidate
2351     public final long compareAndExchangeLongAcquire(Object o, long offset,
2352                                                            long expected,
2353                                                            long x) {
2354         return compareAndExchangeLong(o, offset, expected, x);
2355     }
2356 
2357     @HotSpotIntrinsicCandidate
2358     public final long compareAndExchangeLongRelease(Object o, long offset,
2359                                                            long expected,
2360                                                            long x) {
2361         return compareAndExchangeLong(o, offset, expected, x);
2362     }
2363 
2364     @HotSpotIntrinsicCandidate
2365     public final boolean weakCompareAndSetLongPlain(Object o, long offset,
2366                                                     long expected,
2367                                                     long x) {
2368         return compareAndSetLong(o, offset, expected, x);
2369     }
2370 
2371     @HotSpotIntrinsicCandidate
2372     public final boolean weakCompareAndSetLongAcquire(Object o, long offset,
2373                                                       long expected,
2374                                                       long x) {
2375         return compareAndSetLong(o, offset, expected, x);
2376     }
2377 
2378     @HotSpotIntrinsicCandidate
2379     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2380                                                       long expected,
2381                                                       long x) {
2382         return compareAndSetLong(o, offset, expected, x);
2383     }
2384 
2385     @HotSpotIntrinsicCandidate
2386     public final boolean weakCompareAndSetLong(Object o, long offset,
2387                                                long expected,
2388                                                long x) {
2389         return compareAndSetLong(o, offset, expected, x);
2390     }
2391 
2392     /**
2393      * Fetches a reference value from a given Java variable, with volatile
2394      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2395      */
2396     @HotSpotIntrinsicCandidate
2397     public native Object getReferenceVolatile(Object o, long offset);
2398 
2399     /**
2400      * Global lock for atomic and volatile strength access to any value of
2401      * an inline type.  This is a temporary workaround until better localized
2402      * atomic access mechanisms are supported for inline types.
2403      */
2404     private static final Object valueLock = new Object();
2405 
2406     public final <V> Object getValueVolatile(Object base, long offset, Class<?> valueType) {
2407         synchronized (valueLock) {
2408             return getValue(base, offset, valueType);
2409         }
2410     }
2411 
2412     /**
2413      * Stores a reference value into a given Java variable, with
2414      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2415      */
2416     @HotSpotIntrinsicCandidate
2417     public native void putReferenceVolatile(Object o, long offset, Object x);
2418 
2419     public final <V> void putValueVolatile(Object o, long offset, Class<?> valueType, V x) {
2420         synchronized (valueLock) {
2421             putValue(o, offset, valueType, x);
2422         }
2423     }
2424 
2425     /** Volatile version of {@link #getInt(Object, long)}  */
2426     @HotSpotIntrinsicCandidate
2427     public native int     getIntVolatile(Object o, long offset);
2428 
2429     /** Volatile version of {@link #putInt(Object, long, int)}  */
2430     @HotSpotIntrinsicCandidate
2431     public native void    putIntVolatile(Object o, long offset, int x);
2432 
2433     /** Volatile version of {@link #getBoolean(Object, long)}  */
2434     @HotSpotIntrinsicCandidate
2435     public native boolean getBooleanVolatile(Object o, long offset);
2436 
2437     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2438     @HotSpotIntrinsicCandidate
2439     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2440 
2441     /** Volatile version of {@link #getByte(Object, long)}  */
2442     @HotSpotIntrinsicCandidate
2443     public native byte    getByteVolatile(Object o, long offset);
2444 
2445     /** Volatile version of {@link #putByte(Object, long, byte)}  */
2446     @HotSpotIntrinsicCandidate
2447     public native void    putByteVolatile(Object o, long offset, byte x);
2448 
2449     /** Volatile version of {@link #getShort(Object, long)}  */
2450     @HotSpotIntrinsicCandidate
2451     public native short   getShortVolatile(Object o, long offset);
2452 
2453     /** Volatile version of {@link #putShort(Object, long, short)}  */
2454     @HotSpotIntrinsicCandidate
2455     public native void    putShortVolatile(Object o, long offset, short x);
2456 
2457     /** Volatile version of {@link #getChar(Object, long)}  */
2458     @HotSpotIntrinsicCandidate
2459     public native char    getCharVolatile(Object o, long offset);
2460 
2461     /** Volatile version of {@link #putChar(Object, long, char)}  */
2462     @HotSpotIntrinsicCandidate
2463     public native void    putCharVolatile(Object o, long offset, char x);
2464 
2465     /** Volatile version of {@link #getLong(Object, long)}  */
2466     @HotSpotIntrinsicCandidate
2467     public native long    getLongVolatile(Object o, long offset);
2468 
2469     /** Volatile version of {@link #putLong(Object, long, long)}  */
2470     @HotSpotIntrinsicCandidate
2471     public native void    putLongVolatile(Object o, long offset, long x);
2472 
2473     /** Volatile version of {@link #getFloat(Object, long)}  */
2474     @HotSpotIntrinsicCandidate
2475     public native float   getFloatVolatile(Object o, long offset);
2476 
2477     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2478     @HotSpotIntrinsicCandidate
2479     public native void    putFloatVolatile(Object o, long offset, float x);
2480 
2481     /** Volatile version of {@link #getDouble(Object, long)}  */
2482     @HotSpotIntrinsicCandidate
2483     public native double  getDoubleVolatile(Object o, long offset);
2484 
2485     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2486     @HotSpotIntrinsicCandidate
2487     public native void    putDoubleVolatile(Object o, long offset, double x);
2488 
2489 
2490 
2491     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2492     @HotSpotIntrinsicCandidate
2493     public final Object getReferenceAcquire(Object o, long offset) {
2494         return getReferenceVolatile(o, offset);
2495     }
2496 
2497     public final <V> Object getValueAcquire(Object base, long offset, Class<?> valueType) {
2498         return getValueVolatile(base, offset, valueType);
2499     }
2500 
2501     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2502     @HotSpotIntrinsicCandidate
2503     public final boolean getBooleanAcquire(Object o, long offset) {
2504         return getBooleanVolatile(o, offset);
2505     }
2506 
2507     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2508     @HotSpotIntrinsicCandidate
2509     public final byte getByteAcquire(Object o, long offset) {
2510         return getByteVolatile(o, offset);
2511     }
2512 
2513     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2514     @HotSpotIntrinsicCandidate
2515     public final short getShortAcquire(Object o, long offset) {
2516         return getShortVolatile(o, offset);
2517     }
2518 
2519     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2520     @HotSpotIntrinsicCandidate
2521     public final char getCharAcquire(Object o, long offset) {
2522         return getCharVolatile(o, offset);
2523     }
2524 
2525     /** Acquire version of {@link #getIntVolatile(Object, long)} */
2526     @HotSpotIntrinsicCandidate
2527     public final int getIntAcquire(Object o, long offset) {
2528         return getIntVolatile(o, offset);
2529     }
2530 
2531     /** Acquire version of {@link #getFloatVolatile(Object, long)} */
2532     @HotSpotIntrinsicCandidate
2533     public final float getFloatAcquire(Object o, long offset) {
2534         return getFloatVolatile(o, offset);
2535     }
2536 
2537     /** Acquire version of {@link #getLongVolatile(Object, long)} */
2538     @HotSpotIntrinsicCandidate
2539     public final long getLongAcquire(Object o, long offset) {
2540         return getLongVolatile(o, offset);
2541     }
2542 
2543     /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
2544     @HotSpotIntrinsicCandidate
2545     public final double getDoubleAcquire(Object o, long offset) {
2546         return getDoubleVolatile(o, offset);
2547     }
2548 
2549     /*
2550       * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2551       * that do not guarantee immediate visibility of the store to
2552       * other threads. This method is generally only useful if the
2553       * underlying field is a Java volatile (or if an array cell, one
2554       * that is otherwise only accessed using volatile accesses).
2555       *
2556       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2557       */
2558 
2559     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2560     @HotSpotIntrinsicCandidate
2561     public final void putReferenceRelease(Object o, long offset, Object x) {
2562         putReferenceVolatile(o, offset, x);
2563     }
2564 
2565     public final <V> void putValueRelease(Object o, long offset, Class<?> valueType, V x) {
2566         putValueVolatile(o, offset, valueType, x);
2567     }
2568 
2569     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2570     @HotSpotIntrinsicCandidate
2571     public final void putBooleanRelease(Object o, long offset, boolean x) {
2572         putBooleanVolatile(o, offset, x);
2573     }
2574 
2575     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2576     @HotSpotIntrinsicCandidate
2577     public final void putByteRelease(Object o, long offset, byte x) {
2578         putByteVolatile(o, offset, x);
2579     }
2580 
2581     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2582     @HotSpotIntrinsicCandidate
2583     public final void putShortRelease(Object o, long offset, short x) {
2584         putShortVolatile(o, offset, x);
2585     }
2586 
2587     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2588     @HotSpotIntrinsicCandidate
2589     public final void putCharRelease(Object o, long offset, char x) {
2590         putCharVolatile(o, offset, x);
2591     }
2592 
2593     /** Release version of {@link #putIntVolatile(Object, long, int)} */
2594     @HotSpotIntrinsicCandidate
2595     public final void putIntRelease(Object o, long offset, int x) {
2596         putIntVolatile(o, offset, x);
2597     }
2598 
2599     /** Release version of {@link #putFloatVolatile(Object, long, float)} */
2600     @HotSpotIntrinsicCandidate
2601     public final void putFloatRelease(Object o, long offset, float x) {
2602         putFloatVolatile(o, offset, x);
2603     }
2604 
2605     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2606     @HotSpotIntrinsicCandidate
2607     public final void putLongRelease(Object o, long offset, long x) {
2608         putLongVolatile(o, offset, x);
2609     }
2610 
2611     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2612     @HotSpotIntrinsicCandidate
2613     public final void putDoubleRelease(Object o, long offset, double x) {
2614         putDoubleVolatile(o, offset, x);
2615     }
2616 
2617     // ------------------------------ Opaque --------------------------------------
2618 
2619     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2620     @HotSpotIntrinsicCandidate
2621     public final Object getReferenceOpaque(Object o, long offset) {
2622         return getReferenceVolatile(o, offset);
2623     }
2624 
2625     public final <V> Object getValueOpaque(Object base, long offset, Class<?> valueType) {
2626         return getValueVolatile(base, offset, valueType);
2627     }
2628 
2629     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2630     @HotSpotIntrinsicCandidate
2631     public final boolean getBooleanOpaque(Object o, long offset) {
2632         return getBooleanVolatile(o, offset);
2633     }
2634 
2635     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2636     @HotSpotIntrinsicCandidate
2637     public final byte getByteOpaque(Object o, long offset) {
2638         return getByteVolatile(o, offset);
2639     }
2640 
2641     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2642     @HotSpotIntrinsicCandidate
2643     public final short getShortOpaque(Object o, long offset) {
2644         return getShortVolatile(o, offset);
2645     }
2646 
2647     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2648     @HotSpotIntrinsicCandidate
2649     public final char getCharOpaque(Object o, long offset) {
2650         return getCharVolatile(o, offset);
2651     }
2652 
2653     /** Opaque version of {@link #getIntVolatile(Object, long)} */
2654     @HotSpotIntrinsicCandidate
2655     public final int getIntOpaque(Object o, long offset) {
2656         return getIntVolatile(o, offset);
2657     }
2658 
2659     /** Opaque version of {@link #getFloatVolatile(Object, long)} */
2660     @HotSpotIntrinsicCandidate
2661     public final float getFloatOpaque(Object o, long offset) {
2662         return getFloatVolatile(o, offset);
2663     }
2664 
2665     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2666     @HotSpotIntrinsicCandidate
2667     public final long getLongOpaque(Object o, long offset) {
2668         return getLongVolatile(o, offset);
2669     }
2670 
2671     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2672     @HotSpotIntrinsicCandidate
2673     public final double getDoubleOpaque(Object o, long offset) {
2674         return getDoubleVolatile(o, offset);
2675     }
2676 
2677     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2678     @HotSpotIntrinsicCandidate
2679     public final void putReferenceOpaque(Object o, long offset, Object x) {
2680         putReferenceVolatile(o, offset, x);
2681     }
2682 
2683     public final <V> void putValueOpaque(Object o, long offset, Class<?> valueType, V x) {
2684         putValueVolatile(o, offset, valueType, x);
2685     }
2686 
2687     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2688     @HotSpotIntrinsicCandidate
2689     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2690         putBooleanVolatile(o, offset, x);
2691     }
2692 
2693     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2694     @HotSpotIntrinsicCandidate
2695     public final void putByteOpaque(Object o, long offset, byte x) {
2696         putByteVolatile(o, offset, x);
2697     }
2698 
2699     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2700     @HotSpotIntrinsicCandidate
2701     public final void putShortOpaque(Object o, long offset, short x) {
2702         putShortVolatile(o, offset, x);
2703     }
2704 
2705     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2706     @HotSpotIntrinsicCandidate
2707     public final void putCharOpaque(Object o, long offset, char x) {
2708         putCharVolatile(o, offset, x);
2709     }
2710 
2711     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
2712     @HotSpotIntrinsicCandidate
2713     public final void putIntOpaque(Object o, long offset, int x) {
2714         putIntVolatile(o, offset, x);
2715     }
2716 
2717     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2718     @HotSpotIntrinsicCandidate
2719     public final void putFloatOpaque(Object o, long offset, float x) {
2720         putFloatVolatile(o, offset, x);
2721     }
2722 
2723     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2724     @HotSpotIntrinsicCandidate
2725     public final void putLongOpaque(Object o, long offset, long x) {
2726         putLongVolatile(o, offset, x);
2727     }
2728 
2729     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2730     @HotSpotIntrinsicCandidate
2731     public final void putDoubleOpaque(Object o, long offset, double x) {
2732         putDoubleVolatile(o, offset, x);
2733     }
2734 
2735     /**
2736      * Unblocks the given thread blocked on {@code park}, or, if it is
2737      * not blocked, causes the subsequent call to {@code park} not to
2738      * block.  Note: this operation is "unsafe" solely because the
2739      * caller must somehow ensure that the thread has not been
2740      * destroyed. Nothing special is usually required to ensure this
2741      * when called from Java (in which there will ordinarily be a live
2742      * reference to the thread) but this is not nearly-automatically
2743      * so when calling from native code.
2744      *
2745      * @param thread the thread to unpark.
2746      */
2747     @HotSpotIntrinsicCandidate
2748     public native void unpark(Object thread);
2749 
2750     /**
2751      * Blocks current thread, returning when a balancing
2752      * {@code unpark} occurs, or a balancing {@code unpark} has
2753      * already occurred, or the thread is interrupted, or, if not
2754      * absolute and time is not zero, the given time nanoseconds have
2755      * elapsed, or if absolute, the given deadline in milliseconds
2756      * since Epoch has passed, or spuriously (i.e., returning for no
2757      * "reason"). Note: This operation is in the Unsafe class only
2758      * because {@code unpark} is, so it would be strange to place it
2759      * elsewhere.
2760      */
2761     @HotSpotIntrinsicCandidate
2762     public native void park(boolean isAbsolute, long time);
2763 
2764     /**
2765      * Gets the load average in the system run queue assigned
2766      * to the available processors averaged over various periods of time.
2767      * This method retrieves the given {@code nelem} samples and
2768      * assigns to the elements of the given {@code loadavg} array.
2769      * The system imposes a maximum of 3 samples, representing
2770      * averages over the last 1,  5,  and  15 minutes, respectively.
2771      *
2772      * @param loadavg an array of double of size nelems
2773      * @param nelems the number of samples to be retrieved and
2774      *        must be 1 to 3.
2775      *
2776      * @return the number of samples actually retrieved; or -1
2777      *         if the load average is unobtainable.
2778      */
2779     public int getLoadAverage(double[] loadavg, int nelems) {
2780         if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
2781             throw new ArrayIndexOutOfBoundsException();
2782         }
2783 
2784         return getLoadAverage0(loadavg, nelems);
2785     }
2786 
2787     // The following contain CAS-based Java implementations used on
2788     // platforms not supporting native instructions
2789 
2790     /**
2791      * Atomically adds the given value to the current value of a field
2792      * or array element within the given object {@code o}
2793      * at the given {@code offset}.
2794      *
2795      * @param o object/array to update the field/element in
2796      * @param offset field/element offset
2797      * @param delta the value to add
2798      * @return the previous value
2799      * @since 1.8
2800      */
2801     @HotSpotIntrinsicCandidate
2802     public final int getAndAddInt(Object o, long offset, int delta) {
2803         int v;
2804         do {
2805             v = getIntVolatile(o, offset);
2806         } while (!weakCompareAndSetInt(o, offset, v, v + delta));
2807         return v;
2808     }
2809 
2810     @ForceInline
2811     public final int getAndAddIntRelease(Object o, long offset, int delta) {
2812         int v;
2813         do {
2814             v = getInt(o, offset);
2815         } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta));
2816         return v;
2817     }
2818 
2819     @ForceInline
2820     public final int getAndAddIntAcquire(Object o, long offset, int delta) {
2821         int v;
2822         do {
2823             v = getIntAcquire(o, offset);
2824         } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta));
2825         return v;
2826     }
2827 
2828     /**
2829      * Atomically adds the given value to the current value of a field
2830      * or array element within the given object {@code o}
2831      * at the given {@code offset}.
2832      *
2833      * @param o object/array to update the field/element in
2834      * @param offset field/element offset
2835      * @param delta the value to add
2836      * @return the previous value
2837      * @since 1.8
2838      */
2839     @HotSpotIntrinsicCandidate
2840     public final long getAndAddLong(Object o, long offset, long delta) {
2841         long v;
2842         do {
2843             v = getLongVolatile(o, offset);
2844         } while (!weakCompareAndSetLong(o, offset, v, v + delta));
2845         return v;
2846     }
2847 
2848     @ForceInline
2849     public final long getAndAddLongRelease(Object o, long offset, long delta) {
2850         long v;
2851         do {
2852             v = getLong(o, offset);
2853         } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta));
2854         return v;
2855     }
2856 
2857     @ForceInline
2858     public final long getAndAddLongAcquire(Object o, long offset, long delta) {
2859         long v;
2860         do {
2861             v = getLongAcquire(o, offset);
2862         } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta));
2863         return v;
2864     }
2865 
2866     @HotSpotIntrinsicCandidate
2867     public final byte getAndAddByte(Object o, long offset, byte delta) {
2868         byte v;
2869         do {
2870             v = getByteVolatile(o, offset);
2871         } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta)));
2872         return v;
2873     }
2874 
2875     @ForceInline
2876     public final byte getAndAddByteRelease(Object o, long offset, byte delta) {
2877         byte v;
2878         do {
2879             v = getByte(o, offset);
2880         } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta)));
2881         return v;
2882     }
2883 
2884     @ForceInline
2885     public final byte getAndAddByteAcquire(Object o, long offset, byte delta) {
2886         byte v;
2887         do {
2888             v = getByteAcquire(o, offset);
2889         } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta)));
2890         return v;
2891     }
2892 
2893     @HotSpotIntrinsicCandidate
2894     public final short getAndAddShort(Object o, long offset, short delta) {
2895         short v;
2896         do {
2897             v = getShortVolatile(o, offset);
2898         } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta)));
2899         return v;
2900     }
2901 
2902     @ForceInline
2903     public final short getAndAddShortRelease(Object o, long offset, short delta) {
2904         short v;
2905         do {
2906             v = getShort(o, offset);
2907         } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta)));
2908         return v;
2909     }
2910 
2911     @ForceInline
2912     public final short getAndAddShortAcquire(Object o, long offset, short delta) {
2913         short v;
2914         do {
2915             v = getShortAcquire(o, offset);
2916         } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta)));
2917         return v;
2918     }
2919 
2920     @ForceInline
2921     public final char getAndAddChar(Object o, long offset, char delta) {
2922         return (char) getAndAddShort(o, offset, (short) delta);
2923     }
2924 
2925     @ForceInline
2926     public final char getAndAddCharRelease(Object o, long offset, char delta) {
2927         return (char) getAndAddShortRelease(o, offset, (short) delta);
2928     }
2929 
2930     @ForceInline
2931     public final char getAndAddCharAcquire(Object o, long offset, char delta) {
2932         return (char) getAndAddShortAcquire(o, offset, (short) delta);
2933     }
2934 
2935     @ForceInline
2936     public final float getAndAddFloat(Object o, long offset, float delta) {
2937         int expectedBits;
2938         float v;
2939         do {
2940             // Load and CAS with the raw bits to avoid issues with NaNs and
2941             // possible bit conversion from signaling NaNs to quiet NaNs that
2942             // may result in the loop not terminating.
2943             expectedBits = getIntVolatile(o, offset);
2944             v = Float.intBitsToFloat(expectedBits);
2945         } while (!weakCompareAndSetInt(o, offset,
2946                                                 expectedBits, Float.floatToRawIntBits(v + delta)));
2947         return v;
2948     }
2949 
2950     @ForceInline
2951     public final float getAndAddFloatRelease(Object o, long offset, float delta) {
2952         int expectedBits;
2953         float v;
2954         do {
2955             // Load and CAS with the raw bits to avoid issues with NaNs and
2956             // possible bit conversion from signaling NaNs to quiet NaNs that
2957             // may result in the loop not terminating.
2958             expectedBits = getInt(o, offset);
2959             v = Float.intBitsToFloat(expectedBits);
2960         } while (!weakCompareAndSetIntRelease(o, offset,
2961                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2962         return v;
2963     }
2964 
2965     @ForceInline
2966     public final float getAndAddFloatAcquire(Object o, long offset, float delta) {
2967         int expectedBits;
2968         float v;
2969         do {
2970             // Load and CAS with the raw bits to avoid issues with NaNs and
2971             // possible bit conversion from signaling NaNs to quiet NaNs that
2972             // may result in the loop not terminating.
2973             expectedBits = getIntAcquire(o, offset);
2974             v = Float.intBitsToFloat(expectedBits);
2975         } while (!weakCompareAndSetIntAcquire(o, offset,
2976                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2977         return v;
2978     }
2979 
2980     @ForceInline
2981     public final double getAndAddDouble(Object o, long offset, double delta) {
2982         long expectedBits;
2983         double v;
2984         do {
2985             // Load and CAS with the raw bits to avoid issues with NaNs and
2986             // possible bit conversion from signaling NaNs to quiet NaNs that
2987             // may result in the loop not terminating.
2988             expectedBits = getLongVolatile(o, offset);
2989             v = Double.longBitsToDouble(expectedBits);
2990         } while (!weakCompareAndSetLong(o, offset,
2991                                                  expectedBits, Double.doubleToRawLongBits(v + delta)));
2992         return v;
2993     }
2994 
2995     @ForceInline
2996     public final double getAndAddDoubleRelease(Object o, long offset, double delta) {
2997         long expectedBits;
2998         double v;
2999         do {
3000             // Load and CAS with the raw bits to avoid issues with NaNs and
3001             // possible bit conversion from signaling NaNs to quiet NaNs that
3002             // may result in the loop not terminating.
3003             expectedBits = getLong(o, offset);
3004             v = Double.longBitsToDouble(expectedBits);
3005         } while (!weakCompareAndSetLongRelease(o, offset,
3006                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
3007         return v;
3008     }
3009 
3010     @ForceInline
3011     public final double getAndAddDoubleAcquire(Object o, long offset, double delta) {
3012         long expectedBits;
3013         double v;
3014         do {
3015             // Load and CAS with the raw bits to avoid issues with NaNs and
3016             // possible bit conversion from signaling NaNs to quiet NaNs that
3017             // may result in the loop not terminating.
3018             expectedBits = getLongAcquire(o, offset);
3019             v = Double.longBitsToDouble(expectedBits);
3020         } while (!weakCompareAndSetLongAcquire(o, offset,
3021                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
3022         return v;
3023     }
3024 
3025     /**
3026      * Atomically exchanges the given value with the current value of
3027      * a field or array element within the given object {@code o}
3028      * at the given {@code offset}.
3029      *
3030      * @param o object/array to update the field/element in
3031      * @param offset field/element offset
3032      * @param newValue new value
3033      * @return the previous value
3034      * @since 1.8
3035      */
3036     @HotSpotIntrinsicCandidate
3037     public final int getAndSetInt(Object o, long offset, int newValue) {
3038         int v;
3039         do {
3040             v = getIntVolatile(o, offset);
3041         } while (!weakCompareAndSetInt(o, offset, v, newValue));
3042         return v;
3043     }
3044 
3045     @ForceInline
3046     public final int getAndSetIntRelease(Object o, long offset, int newValue) {
3047         int v;
3048         do {
3049             v = getInt(o, offset);
3050         } while (!weakCompareAndSetIntRelease(o, offset, v, newValue));
3051         return v;
3052     }
3053 
3054     @ForceInline
3055     public final int getAndSetIntAcquire(Object o, long offset, int newValue) {
3056         int v;
3057         do {
3058             v = getIntAcquire(o, offset);
3059         } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue));
3060         return v;
3061     }
3062 
3063     /**
3064      * Atomically exchanges the given value with the current value of
3065      * a field or array element within the given object {@code o}
3066      * at the given {@code offset}.
3067      *
3068      * @param o object/array to update the field/element in
3069      * @param offset field/element offset
3070      * @param newValue new value
3071      * @return the previous value
3072      * @since 1.8
3073      */
3074     @HotSpotIntrinsicCandidate
3075     public final long getAndSetLong(Object o, long offset, long newValue) {
3076         long v;
3077         do {
3078             v = getLongVolatile(o, offset);
3079         } while (!weakCompareAndSetLong(o, offset, v, newValue));
3080         return v;
3081     }
3082 
3083     @ForceInline
3084     public final long getAndSetLongRelease(Object o, long offset, long newValue) {
3085         long v;
3086         do {
3087             v = getLong(o, offset);
3088         } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
3089         return v;
3090     }
3091 
3092     @ForceInline
3093     public final long getAndSetLongAcquire(Object o, long offset, long newValue) {
3094         long v;
3095         do {
3096             v = getLongAcquire(o, offset);
3097         } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue));
3098         return v;
3099     }
3100 
3101     /**
3102      * Atomically exchanges the given reference value with the current
3103      * reference value of a field or array element within the given
3104      * object {@code o} at the given {@code offset}.
3105      *
3106      * @param o object/array to update the field/element in
3107      * @param offset field/element offset
3108      * @param newValue new value
3109      * @return the previous value
3110      * @since 1.8
3111      */
3112     @HotSpotIntrinsicCandidate
3113     public final Object getAndSetReference(Object o, long offset, Object newValue) {
3114         Object v;
3115         do {
3116             v = getReferenceVolatile(o, offset);
3117         } while (!weakCompareAndSetReference(o, offset, v, newValue));
3118         return v;
3119     }
3120 
3121     @SuppressWarnings("unchecked")
3122     public final <V> Object getAndSetValue(Object o, long offset, Class<?> valueType, V newValue) {
3123         synchronized (valueLock) {
3124             Object oldValue = getValue(o, offset, valueType);
3125             putValue(o, offset, valueType, newValue);
3126             return oldValue;
3127         }
3128     }
3129 
3130     @ForceInline
3131     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3132         Object v;
3133         do {
3134             v = getReference(o, offset);
3135         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3136         return v;
3137     }
3138 
3139     @ForceInline
3140     public final <V> Object getAndSetValueRelease(Object o, long offset, Class<?> valueType, V newValue) {
3141         return getAndSetValue(o, offset, valueType, newValue);
3142     }
3143 
3144     @ForceInline
3145     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3146         Object v;
3147         do {
3148             v = getReferenceAcquire(o, offset);
3149         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3150         return v;
3151     }
3152 
3153     @ForceInline
3154     public final <V> Object getAndSetValueAcquire(Object o, long offset, Class<?> valueType, V newValue) {
3155         return getAndSetValue(o, offset, valueType, newValue);
3156     }
3157 
3158     @HotSpotIntrinsicCandidate
3159     public final byte getAndSetByte(Object o, long offset, byte newValue) {
3160         byte v;
3161         do {
3162             v = getByteVolatile(o, offset);
3163         } while (!weakCompareAndSetByte(o, offset, v, newValue));
3164         return v;
3165     }
3166 
3167     @ForceInline
3168     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3169         byte v;
3170         do {
3171             v = getByte(o, offset);
3172         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3173         return v;
3174     }
3175 
3176     @ForceInline
3177     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
3178         byte v;
3179         do {
3180             v = getByteAcquire(o, offset);
3181         } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue));
3182         return v;
3183     }
3184 
3185     @ForceInline
3186     public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) {
3187         return byte2bool(getAndSetByte(o, offset, bool2byte(newValue)));
3188     }
3189 
3190     @ForceInline
3191     public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) {
3192         return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue)));
3193     }
3194 
3195     @ForceInline
3196     public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) {
3197         return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue)));
3198     }
3199 
3200     @HotSpotIntrinsicCandidate
3201     public final short getAndSetShort(Object o, long offset, short newValue) {
3202         short v;
3203         do {
3204             v = getShortVolatile(o, offset);
3205         } while (!weakCompareAndSetShort(o, offset, v, newValue));
3206         return v;
3207     }
3208 
3209     @ForceInline
3210     public final short getAndSetShortRelease(Object o, long offset, short newValue) {
3211         short v;
3212         do {
3213             v = getShort(o, offset);
3214         } while (!weakCompareAndSetShortRelease(o, offset, v, newValue));
3215         return v;
3216     }
3217 
3218     @ForceInline
3219     public final short getAndSetShortAcquire(Object o, long offset, short newValue) {
3220         short v;
3221         do {
3222             v = getShortAcquire(o, offset);
3223         } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue));
3224         return v;
3225     }
3226 
3227     @ForceInline
3228     public final char getAndSetChar(Object o, long offset, char newValue) {
3229         return s2c(getAndSetShort(o, offset, c2s(newValue)));
3230     }
3231 
3232     @ForceInline
3233     public final char getAndSetCharRelease(Object o, long offset, char newValue) {
3234         return s2c(getAndSetShortRelease(o, offset, c2s(newValue)));
3235     }
3236 
3237     @ForceInline
3238     public final char getAndSetCharAcquire(Object o, long offset, char newValue) {
3239         return s2c(getAndSetShortAcquire(o, offset, c2s(newValue)));
3240     }
3241 
3242     @ForceInline
3243     public final float getAndSetFloat(Object o, long offset, float newValue) {
3244         int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue));
3245         return Float.intBitsToFloat(v);
3246     }
3247 
3248     @ForceInline
3249     public final float getAndSetFloatRelease(Object o, long offset, float newValue) {
3250         int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue));
3251         return Float.intBitsToFloat(v);
3252     }
3253 
3254     @ForceInline
3255     public final float getAndSetFloatAcquire(Object o, long offset, float newValue) {
3256         int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue));
3257         return Float.intBitsToFloat(v);
3258     }
3259 
3260     @ForceInline
3261     public final double getAndSetDouble(Object o, long offset, double newValue) {
3262         long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue));
3263         return Double.longBitsToDouble(v);
3264     }
3265 
3266     @ForceInline
3267     public final double getAndSetDoubleRelease(Object o, long offset, double newValue) {
3268         long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue));
3269         return Double.longBitsToDouble(v);
3270     }
3271 
3272     @ForceInline
3273     public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) {
3274         long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue));
3275         return Double.longBitsToDouble(v);
3276     }
3277 
3278 
3279     // The following contain CAS-based Java implementations used on
3280     // platforms not supporting native instructions
3281 
3282     @ForceInline
3283     public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) {
3284         return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask)));
3285     }
3286 
3287     @ForceInline
3288     public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) {
3289         return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask)));
3290     }
3291 
3292     @ForceInline
3293     public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) {
3294         return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask)));
3295     }
3296 
3297     @ForceInline
3298     public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) {
3299         return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask)));
3300     }
3301 
3302     @ForceInline
3303     public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) {
3304         return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask)));
3305     }
3306 
3307     @ForceInline
3308     public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) {
3309         return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask)));
3310     }
3311 
3312     @ForceInline
3313     public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) {
3314         return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask)));
3315     }
3316 
3317     @ForceInline
3318     public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) {
3319         return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask)));
3320     }
3321 
3322     @ForceInline
3323     public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) {
3324         return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask)));
3325     }
3326 
3327 
3328     @ForceInline
3329     public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) {
3330         byte current;
3331         do {
3332             current = getByteVolatile(o, offset);
3333         } while (!weakCompareAndSetByte(o, offset,
3334                                                   current, (byte) (current | mask)));
3335         return current;
3336     }
3337 
3338     @ForceInline
3339     public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) {
3340         byte current;
3341         do {
3342             current = getByte(o, offset);
3343         } while (!weakCompareAndSetByteRelease(o, offset,
3344                                                  current, (byte) (current | mask)));
3345         return current;
3346     }
3347 
3348     @ForceInline
3349     public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) {
3350         byte current;
3351         do {
3352             // Plain read, the value is a hint, the acquire CAS does the work
3353             current = getByte(o, offset);
3354         } while (!weakCompareAndSetByteAcquire(o, offset,
3355                                                  current, (byte) (current | mask)));
3356         return current;
3357     }
3358 
3359     @ForceInline
3360     public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) {
3361         byte current;
3362         do {
3363             current = getByteVolatile(o, offset);
3364         } while (!weakCompareAndSetByte(o, offset,
3365                                                   current, (byte) (current & mask)));
3366         return current;
3367     }
3368 
3369     @ForceInline
3370     public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) {
3371         byte current;
3372         do {
3373             current = getByte(o, offset);
3374         } while (!weakCompareAndSetByteRelease(o, offset,
3375                                                  current, (byte) (current & mask)));
3376         return current;
3377     }
3378 
3379     @ForceInline
3380     public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) {
3381         byte current;
3382         do {
3383             // Plain read, the value is a hint, the acquire CAS does the work
3384             current = getByte(o, offset);
3385         } while (!weakCompareAndSetByteAcquire(o, offset,
3386                                                  current, (byte) (current & mask)));
3387         return current;
3388     }
3389 
3390     @ForceInline
3391     public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) {
3392         byte current;
3393         do {
3394             current = getByteVolatile(o, offset);
3395         } while (!weakCompareAndSetByte(o, offset,
3396                                                   current, (byte) (current ^ mask)));
3397         return current;
3398     }
3399 
3400     @ForceInline
3401     public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) {
3402         byte current;
3403         do {
3404             current = getByte(o, offset);
3405         } while (!weakCompareAndSetByteRelease(o, offset,
3406                                                  current, (byte) (current ^ mask)));
3407         return current;
3408     }
3409 
3410     @ForceInline
3411     public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) {
3412         byte current;
3413         do {
3414             // Plain read, the value is a hint, the acquire CAS does the work
3415             current = getByte(o, offset);
3416         } while (!weakCompareAndSetByteAcquire(o, offset,
3417                                                  current, (byte) (current ^ mask)));
3418         return current;
3419     }
3420 
3421 
3422     @ForceInline
3423     public final char getAndBitwiseOrChar(Object o, long offset, char mask) {
3424         return s2c(getAndBitwiseOrShort(o, offset, c2s(mask)));
3425     }
3426 
3427     @ForceInline
3428     public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) {
3429         return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask)));
3430     }
3431 
3432     @ForceInline
3433     public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) {
3434         return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask)));
3435     }
3436 
3437     @ForceInline
3438     public final char getAndBitwiseAndChar(Object o, long offset, char mask) {
3439         return s2c(getAndBitwiseAndShort(o, offset, c2s(mask)));
3440     }
3441 
3442     @ForceInline
3443     public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) {
3444         return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask)));
3445     }
3446 
3447     @ForceInline
3448     public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) {
3449         return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask)));
3450     }
3451 
3452     @ForceInline
3453     public final char getAndBitwiseXorChar(Object o, long offset, char mask) {
3454         return s2c(getAndBitwiseXorShort(o, offset, c2s(mask)));
3455     }
3456 
3457     @ForceInline
3458     public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) {
3459         return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask)));
3460     }
3461 
3462     @ForceInline
3463     public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) {
3464         return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask)));
3465     }
3466 
3467 
3468     @ForceInline
3469     public final short getAndBitwiseOrShort(Object o, long offset, short mask) {
3470         short current;
3471         do {
3472             current = getShortVolatile(o, offset);
3473         } while (!weakCompareAndSetShort(o, offset,
3474                                                 current, (short) (current | mask)));
3475         return current;
3476     }
3477 
3478     @ForceInline
3479     public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) {
3480         short current;
3481         do {
3482             current = getShort(o, offset);
3483         } while (!weakCompareAndSetShortRelease(o, offset,
3484                                                current, (short) (current | mask)));
3485         return current;
3486     }
3487 
3488     @ForceInline
3489     public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) {
3490         short current;
3491         do {
3492             // Plain read, the value is a hint, the acquire CAS does the work
3493             current = getShort(o, offset);
3494         } while (!weakCompareAndSetShortAcquire(o, offset,
3495                                                current, (short) (current | mask)));
3496         return current;
3497     }
3498 
3499     @ForceInline
3500     public final short getAndBitwiseAndShort(Object o, long offset, short mask) {
3501         short current;
3502         do {
3503             current = getShortVolatile(o, offset);
3504         } while (!weakCompareAndSetShort(o, offset,
3505                                                 current, (short) (current & mask)));
3506         return current;
3507     }
3508 
3509     @ForceInline
3510     public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) {
3511         short current;
3512         do {
3513             current = getShort(o, offset);
3514         } while (!weakCompareAndSetShortRelease(o, offset,
3515                                                current, (short) (current & mask)));
3516         return current;
3517     }
3518 
3519     @ForceInline
3520     public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) {
3521         short current;
3522         do {
3523             // Plain read, the value is a hint, the acquire CAS does the work
3524             current = getShort(o, offset);
3525         } while (!weakCompareAndSetShortAcquire(o, offset,
3526                                                current, (short) (current & mask)));
3527         return current;
3528     }
3529 
3530     @ForceInline
3531     public final short getAndBitwiseXorShort(Object o, long offset, short mask) {
3532         short current;
3533         do {
3534             current = getShortVolatile(o, offset);
3535         } while (!weakCompareAndSetShort(o, offset,
3536                                                 current, (short) (current ^ mask)));
3537         return current;
3538     }
3539 
3540     @ForceInline
3541     public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) {
3542         short current;
3543         do {
3544             current = getShort(o, offset);
3545         } while (!weakCompareAndSetShortRelease(o, offset,
3546                                                current, (short) (current ^ mask)));
3547         return current;
3548     }
3549 
3550     @ForceInline
3551     public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) {
3552         short current;
3553         do {
3554             // Plain read, the value is a hint, the acquire CAS does the work
3555             current = getShort(o, offset);
3556         } while (!weakCompareAndSetShortAcquire(o, offset,
3557                                                current, (short) (current ^ mask)));
3558         return current;
3559     }
3560 
3561 
3562     @ForceInline
3563     public final int getAndBitwiseOrInt(Object o, long offset, int mask) {
3564         int current;
3565         do {
3566             current = getIntVolatile(o, offset);
3567         } while (!weakCompareAndSetInt(o, offset,
3568                                                 current, current | mask));
3569         return current;
3570     }
3571 
3572     @ForceInline
3573     public final int getAndBitwiseOrIntRelease(Object o, long offset, int mask) {
3574         int current;
3575         do {
3576             current = getInt(o, offset);
3577         } while (!weakCompareAndSetIntRelease(o, offset,
3578                                                current, current | mask));
3579         return current;
3580     }
3581 
3582     @ForceInline
3583     public final int getAndBitwiseOrIntAcquire(Object o, long offset, int mask) {
3584         int current;
3585         do {
3586             // Plain read, the value is a hint, the acquire CAS does the work
3587             current = getInt(o, offset);
3588         } while (!weakCompareAndSetIntAcquire(o, offset,
3589                                                current, current | mask));
3590         return current;
3591     }
3592 
3593     /**
3594      * Atomically replaces the current value of a field or array element within
3595      * the given object with the result of bitwise AND between the current value
3596      * and mask.
3597      *
3598      * @param o object/array to update the field/element in
3599      * @param offset field/element offset
3600      * @param mask the mask value
3601      * @return the previous value
3602      * @since 9
3603      */
3604     @ForceInline
3605     public final int getAndBitwiseAndInt(Object o, long offset, int mask) {
3606         int current;
3607         do {
3608             current = getIntVolatile(o, offset);
3609         } while (!weakCompareAndSetInt(o, offset,
3610                                                 current, current & mask));
3611         return current;
3612     }
3613 
3614     @ForceInline
3615     public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) {
3616         int current;
3617         do {
3618             current = getInt(o, offset);
3619         } while (!weakCompareAndSetIntRelease(o, offset,
3620                                                current, current & mask));
3621         return current;
3622     }
3623 
3624     @ForceInline
3625     public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) {
3626         int current;
3627         do {
3628             // Plain read, the value is a hint, the acquire CAS does the work
3629             current = getInt(o, offset);
3630         } while (!weakCompareAndSetIntAcquire(o, offset,
3631                                                current, current & mask));
3632         return current;
3633     }
3634 
3635     @ForceInline
3636     public final int getAndBitwiseXorInt(Object o, long offset, int mask) {
3637         int current;
3638         do {
3639             current = getIntVolatile(o, offset);
3640         } while (!weakCompareAndSetInt(o, offset,
3641                                                 current, current ^ mask));
3642         return current;
3643     }
3644 
3645     @ForceInline
3646     public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) {
3647         int current;
3648         do {
3649             current = getInt(o, offset);
3650         } while (!weakCompareAndSetIntRelease(o, offset,
3651                                                current, current ^ mask));
3652         return current;
3653     }
3654 
3655     @ForceInline
3656     public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) {
3657         int current;
3658         do {
3659             // Plain read, the value is a hint, the acquire CAS does the work
3660             current = getInt(o, offset);
3661         } while (!weakCompareAndSetIntAcquire(o, offset,
3662                                                current, current ^ mask));
3663         return current;
3664     }
3665 
3666 
3667     @ForceInline
3668     public final long getAndBitwiseOrLong(Object o, long offset, long mask) {
3669         long current;
3670         do {
3671             current = getLongVolatile(o, offset);
3672         } while (!weakCompareAndSetLong(o, offset,
3673                                                 current, current | mask));
3674         return current;
3675     }
3676 
3677     @ForceInline
3678     public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) {
3679         long current;
3680         do {
3681             current = getLong(o, offset);
3682         } while (!weakCompareAndSetLongRelease(o, offset,
3683                                                current, current | mask));
3684         return current;
3685     }
3686 
3687     @ForceInline
3688     public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) {
3689         long current;
3690         do {
3691             // Plain read, the value is a hint, the acquire CAS does the work
3692             current = getLong(o, offset);
3693         } while (!weakCompareAndSetLongAcquire(o, offset,
3694                                                current, current | mask));
3695         return current;
3696     }
3697 
3698     @ForceInline
3699     public final long getAndBitwiseAndLong(Object o, long offset, long mask) {
3700         long current;
3701         do {
3702             current = getLongVolatile(o, offset);
3703         } while (!weakCompareAndSetLong(o, offset,
3704                                                 current, current & mask));
3705         return current;
3706     }
3707 
3708     @ForceInline
3709     public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) {
3710         long current;
3711         do {
3712             current = getLong(o, offset);
3713         } while (!weakCompareAndSetLongRelease(o, offset,
3714                                                current, current & mask));
3715         return current;
3716     }
3717 
3718     @ForceInline
3719     public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) {
3720         long current;
3721         do {
3722             // Plain read, the value is a hint, the acquire CAS does the work
3723             current = getLong(o, offset);
3724         } while (!weakCompareAndSetLongAcquire(o, offset,
3725                                                current, current & mask));
3726         return current;
3727     }
3728 
3729     @ForceInline
3730     public final long getAndBitwiseXorLong(Object o, long offset, long mask) {
3731         long current;
3732         do {
3733             current = getLongVolatile(o, offset);
3734         } while (!weakCompareAndSetLong(o, offset,
3735                                                 current, current ^ mask));
3736         return current;
3737     }
3738 
3739     @ForceInline
3740     public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) {
3741         long current;
3742         do {
3743             current = getLong(o, offset);
3744         } while (!weakCompareAndSetLongRelease(o, offset,
3745                                                current, current ^ mask));
3746         return current;
3747     }
3748 
3749     @ForceInline
3750     public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) {
3751         long current;
3752         do {
3753             // Plain read, the value is a hint, the acquire CAS does the work
3754             current = getLong(o, offset);
3755         } while (!weakCompareAndSetLongAcquire(o, offset,
3756                                                current, current ^ mask));
3757         return current;
3758     }
3759 
3760 
3761 
3762     /**
3763      * Ensures that loads before the fence will not be reordered with loads and
3764      * stores after the fence; a "LoadLoad plus LoadStore barrier".
3765      *
3766      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
3767      * (an "acquire fence").
3768      *
3769      * A pure LoadLoad fence is not provided, since the addition of LoadStore
3770      * is almost always desired, and most current hardware instructions that
3771      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
3772      * @since 1.8
3773      */
3774     @HotSpotIntrinsicCandidate
3775     public native void loadFence();
3776 
3777     /**
3778      * Ensures that loads and stores before the fence will not be reordered with
3779      * stores after the fence; a "StoreStore plus LoadStore barrier".
3780      *
3781      * Corresponds to C11 atomic_thread_fence(memory_order_release)
3782      * (a "release fence").
3783      *
3784      * A pure StoreStore fence is not provided, since the addition of LoadStore
3785      * is almost always desired, and most current hardware instructions that
3786      * provide a StoreStore barrier also provide a LoadStore barrier for free.
3787      * @since 1.8
3788      */
3789     @HotSpotIntrinsicCandidate
3790     public native void storeFence();
3791 
3792     /**
3793      * Ensures that loads and stores before the fence will not be reordered
3794      * with loads and stores after the fence.  Implies the effects of both
3795      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
3796      * barrier.
3797      *
3798      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
3799      * @since 1.8
3800      */
3801     @HotSpotIntrinsicCandidate
3802     public native void fullFence();
3803 
3804     /**
3805      * Ensures that loads before the fence will not be reordered with
3806      * loads after the fence.
3807      */
3808     public final void loadLoadFence() {
3809         loadFence();
3810     }
3811 
3812     /**
3813      * Ensures that stores before the fence will not be reordered with
3814      * stores after the fence.
3815      */
3816     public final void storeStoreFence() {
3817         storeFence();
3818     }
3819 
3820 
3821     /**
3822      * Throws IllegalAccessError; for use by the VM for access control
3823      * error support.
3824      * @since 1.8
3825      */
3826     private static void throwIllegalAccessError() {
3827         throw new IllegalAccessError();
3828     }
3829 
3830     /**
3831      * Throws NoSuchMethodError; for use by the VM for redefinition support.
3832      * @since 13
3833      */
3834     private static void throwNoSuchMethodError() {
3835         throw new NoSuchMethodError();
3836     }
3837 
3838     /**
3839      * @return Returns true if the native byte ordering of this
3840      * platform is big-endian, false if it is little-endian.
3841      */
3842     public final boolean isBigEndian() { return BIG_ENDIAN; }
3843 
3844     /**
3845      * @return Returns true if this platform is capable of performing
3846      * accesses at addresses which are not aligned for the type of the
3847      * primitive type being accessed, false otherwise.
3848      */
3849     public final boolean unalignedAccess() { return UNALIGNED_ACCESS; }
3850 
3851     /**
3852      * Fetches a value at some byte offset into a given Java object.
3853      * More specifically, fetches a value within the given object
3854      * <code>o</code> at the given offset, or (if <code>o</code> is
3855      * null) from the memory address whose numerical value is the
3856      * given offset.  <p>
3857      *
3858      * The specification of this method is the same as {@link
3859      * #getLong(Object, long)} except that the offset does not need to
3860      * have been obtained from {@link #objectFieldOffset} on the
3861      * {@link java.lang.reflect.Field} of some Java field.  The value
3862      * in memory is raw data, and need not correspond to any Java
3863      * variable.  Unless <code>o</code> is null, the value accessed
3864      * must be entirely within the allocated object.  The endianness
3865      * of the value in memory is the endianness of the native platform.
3866      *
3867      * <p> The read will be atomic with respect to the largest power
3868      * of two that divides the GCD of the offset and the storage size.
3869      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
3870      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3871      * respectively.  There are no other guarantees of atomicity.
3872      * <p>
3873      * 8-byte atomicity is only guaranteed on platforms on which
3874      * support atomic accesses to longs.
3875      *
3876      * @param o Java heap object in which the value resides, if any, else
3877      *        null
3878      * @param offset The offset in bytes from the start of the object
3879      * @return the value fetched from the indicated object
3880      * @throws RuntimeException No defined exceptions are thrown, not even
3881      *         {@link NullPointerException}
3882      * @since 9
3883      */
3884     @HotSpotIntrinsicCandidate
3885     public final long getLongUnaligned(Object o, long offset) {
3886         if ((offset & 7) == 0) {
3887             return getLong(o, offset);
3888         } else if ((offset & 3) == 0) {
3889             return makeLong(getInt(o, offset),
3890                             getInt(o, offset + 4));
3891         } else if ((offset & 1) == 0) {
3892             return makeLong(getShort(o, offset),
3893                             getShort(o, offset + 2),
3894                             getShort(o, offset + 4),
3895                             getShort(o, offset + 6));
3896         } else {
3897             return makeLong(getByte(o, offset),
3898                             getByte(o, offset + 1),
3899                             getByte(o, offset + 2),
3900                             getByte(o, offset + 3),
3901                             getByte(o, offset + 4),
3902                             getByte(o, offset + 5),
3903                             getByte(o, offset + 6),
3904                             getByte(o, offset + 7));
3905         }
3906     }
3907     /**
3908      * As {@link #getLongUnaligned(Object, long)} but with an
3909      * additional argument which specifies the endianness of the value
3910      * as stored in memory.
3911      *
3912      * @param o Java heap object in which the variable resides
3913      * @param offset The offset in bytes from the start of the object
3914      * @param bigEndian The endianness of the value
3915      * @return the value fetched from the indicated object
3916      * @since 9
3917      */
3918     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
3919         return convEndian(bigEndian, getLongUnaligned(o, offset));
3920     }
3921 
3922     /** @see #getLongUnaligned(Object, long) */
3923     @HotSpotIntrinsicCandidate
3924     public final int getIntUnaligned(Object o, long offset) {
3925         if ((offset & 3) == 0) {
3926             return getInt(o, offset);
3927         } else if ((offset & 1) == 0) {
3928             return makeInt(getShort(o, offset),
3929                            getShort(o, offset + 2));
3930         } else {
3931             return makeInt(getByte(o, offset),
3932                            getByte(o, offset + 1),
3933                            getByte(o, offset + 2),
3934                            getByte(o, offset + 3));
3935         }
3936     }
3937     /** @see #getLongUnaligned(Object, long, boolean) */
3938     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
3939         return convEndian(bigEndian, getIntUnaligned(o, offset));
3940     }
3941 
3942     /** @see #getLongUnaligned(Object, long) */
3943     @HotSpotIntrinsicCandidate
3944     public final short getShortUnaligned(Object o, long offset) {
3945         if ((offset & 1) == 0) {
3946             return getShort(o, offset);
3947         } else {
3948             return makeShort(getByte(o, offset),
3949                              getByte(o, offset + 1));
3950         }
3951     }
3952     /** @see #getLongUnaligned(Object, long, boolean) */
3953     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
3954         return convEndian(bigEndian, getShortUnaligned(o, offset));
3955     }
3956 
3957     /** @see #getLongUnaligned(Object, long) */
3958     @HotSpotIntrinsicCandidate
3959     public final char getCharUnaligned(Object o, long offset) {
3960         if ((offset & 1) == 0) {
3961             return getChar(o, offset);
3962         } else {
3963             return (char)makeShort(getByte(o, offset),
3964                                    getByte(o, offset + 1));
3965         }
3966     }
3967 
3968     /** @see #getLongUnaligned(Object, long, boolean) */
3969     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
3970         return convEndian(bigEndian, getCharUnaligned(o, offset));
3971     }
3972 
3973     /**
3974      * Stores a value at some byte offset into a given Java object.
3975      * <p>
3976      * The specification of this method is the same as {@link
3977      * #getLong(Object, long)} except that the offset does not need to
3978      * have been obtained from {@link #objectFieldOffset} on the
3979      * {@link java.lang.reflect.Field} of some Java field.  The value
3980      * in memory is raw data, and need not correspond to any Java
3981      * variable.  The endianness of the value in memory is the
3982      * endianness of the native platform.
3983      * <p>
3984      * The write will be atomic with respect to the largest power of
3985      * two that divides the GCD of the offset and the storage size.
3986      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
3987      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3988      * respectively.  There are no other guarantees of atomicity.
3989      * <p>
3990      * 8-byte atomicity is only guaranteed on platforms on which
3991      * support atomic accesses to longs.
3992      *
3993      * @param o Java heap object in which the value resides, if any, else
3994      *        null
3995      * @param offset The offset in bytes from the start of the object
3996      * @param x the value to store
3997      * @throws RuntimeException No defined exceptions are thrown, not even
3998      *         {@link NullPointerException}
3999      * @since 9
4000      */
4001     @HotSpotIntrinsicCandidate
4002     public final void putLongUnaligned(Object o, long offset, long x) {
4003         if ((offset & 7) == 0) {
4004             putLong(o, offset, x);
4005         } else if ((offset & 3) == 0) {
4006             putLongParts(o, offset,
4007                          (int)(x >> 0),
4008                          (int)(x >>> 32));
4009         } else if ((offset & 1) == 0) {
4010             putLongParts(o, offset,
4011                          (short)(x >>> 0),
4012                          (short)(x >>> 16),
4013                          (short)(x >>> 32),
4014                          (short)(x >>> 48));
4015         } else {
4016             putLongParts(o, offset,
4017                          (byte)(x >>> 0),
4018                          (byte)(x >>> 8),
4019                          (byte)(x >>> 16),
4020                          (byte)(x >>> 24),
4021                          (byte)(x >>> 32),
4022                          (byte)(x >>> 40),
4023                          (byte)(x >>> 48),
4024                          (byte)(x >>> 56));
4025         }
4026     }
4027 
4028     /**
4029      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
4030      * argument which specifies the endianness of the value as stored in memory.
4031      * @param o Java heap object in which the value resides
4032      * @param offset The offset in bytes from the start of the object
4033      * @param x the value to store
4034      * @param bigEndian The endianness of the value
4035      * @throws RuntimeException No defined exceptions are thrown, not even
4036      *         {@link NullPointerException}
4037      * @since 9
4038      */
4039     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
4040         putLongUnaligned(o, offset, convEndian(bigEndian, x));
4041     }
4042 
4043     /** @see #putLongUnaligned(Object, long, long) */
4044     @HotSpotIntrinsicCandidate
4045     public final void putIntUnaligned(Object o, long offset, int x) {
4046         if ((offset & 3) == 0) {
4047             putInt(o, offset, x);
4048         } else if ((offset & 1) == 0) {
4049             putIntParts(o, offset,
4050                         (short)(x >> 0),
4051                         (short)(x >>> 16));
4052         } else {
4053             putIntParts(o, offset,
4054                         (byte)(x >>> 0),
4055                         (byte)(x >>> 8),
4056                         (byte)(x >>> 16),
4057                         (byte)(x >>> 24));
4058         }
4059     }
4060     /** @see #putLongUnaligned(Object, long, long, boolean) */
4061     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
4062         putIntUnaligned(o, offset, convEndian(bigEndian, x));
4063     }
4064 
4065     /** @see #putLongUnaligned(Object, long, long) */
4066     @HotSpotIntrinsicCandidate
4067     public final void putShortUnaligned(Object o, long offset, short x) {
4068         if ((offset & 1) == 0) {
4069             putShort(o, offset, x);
4070         } else {
4071             putShortParts(o, offset,
4072                           (byte)(x >>> 0),
4073                           (byte)(x >>> 8));
4074         }
4075     }
4076     /** @see #putLongUnaligned(Object, long, long, boolean) */
4077     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
4078         putShortUnaligned(o, offset, convEndian(bigEndian, x));
4079     }
4080 
4081     /** @see #putLongUnaligned(Object, long, long) */
4082     @HotSpotIntrinsicCandidate
4083     public final void putCharUnaligned(Object o, long offset, char x) {
4084         putShortUnaligned(o, offset, (short)x);
4085     }
4086     /** @see #putLongUnaligned(Object, long, long, boolean) */
4087     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
4088         putCharUnaligned(o, offset, convEndian(bigEndian, x));
4089     }
4090 
4091     private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; }
4092 
4093     // These methods construct integers from bytes.  The byte ordering
4094     // is the native endianness of this platform.
4095     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4096         return ((toUnsignedLong(i0) << pickPos(56, 0))
4097               | (toUnsignedLong(i1) << pickPos(56, 8))
4098               | (toUnsignedLong(i2) << pickPos(56, 16))
4099               | (toUnsignedLong(i3) << pickPos(56, 24))
4100               | (toUnsignedLong(i4) << pickPos(56, 32))
4101               | (toUnsignedLong(i5) << pickPos(56, 40))
4102               | (toUnsignedLong(i6) << pickPos(56, 48))
4103               | (toUnsignedLong(i7) << pickPos(56, 56)));
4104     }
4105     private static long makeLong(short i0, short i1, short i2, short i3) {
4106         return ((toUnsignedLong(i0) << pickPos(48, 0))
4107               | (toUnsignedLong(i1) << pickPos(48, 16))
4108               | (toUnsignedLong(i2) << pickPos(48, 32))
4109               | (toUnsignedLong(i3) << pickPos(48, 48)));
4110     }
4111     private static long makeLong(int i0, int i1) {
4112         return (toUnsignedLong(i0) << pickPos(32, 0))
4113              | (toUnsignedLong(i1) << pickPos(32, 32));
4114     }
4115     private static int makeInt(short i0, short i1) {
4116         return (toUnsignedInt(i0) << pickPos(16, 0))
4117              | (toUnsignedInt(i1) << pickPos(16, 16));
4118     }
4119     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
4120         return ((toUnsignedInt(i0) << pickPos(24, 0))
4121               | (toUnsignedInt(i1) << pickPos(24, 8))
4122               | (toUnsignedInt(i2) << pickPos(24, 16))
4123               | (toUnsignedInt(i3) << pickPos(24, 24)));
4124     }
4125     private static short makeShort(byte i0, byte i1) {
4126         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
4127                      | (toUnsignedInt(i1) << pickPos(8, 8)));
4128     }
4129 
4130     private static byte  pick(byte  le, byte  be) { return BIG_ENDIAN ? be : le; }
4131     private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; }
4132     private static int   pick(int   le, int   be) { return BIG_ENDIAN ? be : le; }
4133 
4134     // These methods write integers to memory from smaller parts
4135     // provided by their caller.  The ordering in which these parts
4136     // are written is the native endianness of this platform.
4137     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4138         putByte(o, offset + 0, pick(i0, i7));
4139         putByte(o, offset + 1, pick(i1, i6));
4140         putByte(o, offset + 2, pick(i2, i5));
4141         putByte(o, offset + 3, pick(i3, i4));
4142         putByte(o, offset + 4, pick(i4, i3));
4143         putByte(o, offset + 5, pick(i5, i2));
4144         putByte(o, offset + 6, pick(i6, i1));
4145         putByte(o, offset + 7, pick(i7, i0));
4146     }
4147     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
4148         putShort(o, offset + 0, pick(i0, i3));
4149         putShort(o, offset + 2, pick(i1, i2));
4150         putShort(o, offset + 4, pick(i2, i1));
4151         putShort(o, offset + 6, pick(i3, i0));
4152     }
4153     private void putLongParts(Object o, long offset, int i0, int i1) {
4154         putInt(o, offset + 0, pick(i0, i1));
4155         putInt(o, offset + 4, pick(i1, i0));
4156     }
4157     private void putIntParts(Object o, long offset, short i0, short i1) {
4158         putShort(o, offset + 0, pick(i0, i1));
4159         putShort(o, offset + 2, pick(i1, i0));
4160     }
4161     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
4162         putByte(o, offset + 0, pick(i0, i3));
4163         putByte(o, offset + 1, pick(i1, i2));
4164         putByte(o, offset + 2, pick(i2, i1));
4165         putByte(o, offset + 3, pick(i3, i0));
4166     }
4167     private void putShortParts(Object o, long offset, byte i0, byte i1) {
4168         putByte(o, offset + 0, pick(i0, i1));
4169         putByte(o, offset + 1, pick(i1, i0));
4170     }
4171 
4172     // Zero-extend an integer
4173     private static int toUnsignedInt(byte n)    { return n & 0xff; }
4174     private static int toUnsignedInt(short n)   { return n & 0xffff; }
4175     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
4176     private static long toUnsignedLong(short n) { return n & 0xffffl; }
4177     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
4178 
4179     // Maybe byte-reverse an integer
4180     private static char convEndian(boolean big, char n)   { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); }
4181     private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n)    ; }
4182     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
4183     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
4184 
4185 
4186 
4187     private native long allocateMemory0(long bytes);
4188     private native long reallocateMemory0(long address, long bytes);
4189     private native void freeMemory0(long address);
4190     private native void setMemory0(Object o, long offset, long bytes, byte value);
4191     @HotSpotIntrinsicCandidate
4192     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4193     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4194     private native long objectFieldOffset0(Field f);
4195     private native long objectFieldOffset1(Class<?> c, String name);
4196     private native long staticFieldOffset0(Field f);
4197     private native Object staticFieldBase0(Field f);
4198     private native boolean shouldBeInitialized0(Class<?> c);
4199     private native void ensureClassInitialized0(Class<?> c);
4200     private native int arrayBaseOffset0(Class<?> arrayClass);
4201     private native int arrayIndexScale0(Class<?> arrayClass);
4202     private native long getObjectSize0(Object o);
4203     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
4204     private native int getLoadAverage0(double[] loadavg, int nelems);
4205 
4206 
4207     /**
4208      * Invokes the given direct byte buffer's cleaner, if any.
4209      *
4210      * @param directBuffer a direct byte buffer
4211      * @throws NullPointerException     if {@code directBuffer} is null
4212      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4213      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
4214      *                                  {@link java.nio.Buffer#duplicate duplicate}
4215      */
4216     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4217         if (!directBuffer.isDirect())
4218             throw new IllegalArgumentException("buffer is non-direct");
4219 
4220         DirectBuffer db = (DirectBuffer) directBuffer;
4221         if (db.attachment() != null)
4222             throw new IllegalArgumentException("duplicate or slice");
4223 
4224         Cleaner cleaner = db.cleaner();
4225         if (cleaner != null) {
4226             cleaner.clean();
4227         }
4228     }
4229 
4230     // The following deprecated methods are used by JSR 166.
4231 
4232     @Deprecated(since="12", forRemoval=true)
4233     public final Object getObject(Object o, long offset) {
4234         return getReference(o, offset);
4235     }
4236     @Deprecated(since="12", forRemoval=true)
4237     public final Object getObjectVolatile(Object o, long offset) {
4238         return getReferenceVolatile(o, offset);
4239     }
4240     @Deprecated(since="12", forRemoval=true)
4241     public final Object getObjectAcquire(Object o, long offset) {
4242         return getReferenceAcquire(o, offset);
4243     }
4244     @Deprecated(since="12", forRemoval=true)
4245     public final Object getObjectOpaque(Object o, long offset) {
4246         return getReferenceOpaque(o, offset);
4247     }
4248 
4249 
4250     @Deprecated(since="12", forRemoval=true)
4251     public final void putObject(Object o, long offset, Object x) {
4252         putReference(o, offset, x);
4253     }
4254     @Deprecated(since="12", forRemoval=true)
4255     public final void putObjectVolatile(Object o, long offset, Object x) {
4256         putReferenceVolatile(o, offset, x);
4257     }
4258     @Deprecated(since="12", forRemoval=true)
4259     public final void putObjectOpaque(Object o, long offset, Object x) {
4260         putReferenceOpaque(o, offset, x);
4261     }
4262     @Deprecated(since="12", forRemoval=true)
4263     public final void putObjectRelease(Object o, long offset, Object x) {
4264         putReferenceRelease(o, offset, x);
4265     }
4266 
4267 
4268     @Deprecated(since="12", forRemoval=true)
4269     public final Object getAndSetObject(Object o, long offset, Object newValue) {
4270         return getAndSetReference(o, offset, newValue);
4271     }
4272     @Deprecated(since="12", forRemoval=true)
4273     public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) {
4274         return getAndSetReferenceAcquire(o, offset, newValue);
4275     }
4276     @Deprecated(since="12", forRemoval=true)
4277     public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) {
4278         return getAndSetReferenceRelease(o, offset, newValue);
4279     }
4280 
4281 
4282     @Deprecated(since="12", forRemoval=true)
4283     public final boolean compareAndSetObject(Object o, long offset, Object expected, Object x) {
4284         return compareAndSetReference(o, offset, expected, x);
4285     }
4286     @Deprecated(since="12", forRemoval=true)
4287     public final Object compareAndExchangeObject(Object o, long offset, Object expected, Object x) {
4288         return compareAndExchangeReference(o, offset, expected, x);
4289     }
4290     @Deprecated(since="12", forRemoval=true)
4291     public final Object compareAndExchangeObjectAcquire(Object o, long offset, Object expected, Object x) {
4292         return compareAndExchangeReferenceAcquire(o, offset, expected, x);
4293     }
4294     @Deprecated(since="12", forRemoval=true)
4295     public final Object compareAndExchangeObjectRelease(Object o, long offset, Object expected, Object x) {
4296         return compareAndExchangeReferenceRelease(o, offset, expected, x);
4297     }
4298 
4299 
4300     @Deprecated(since="12", forRemoval=true)
4301     public final boolean weakCompareAndSetObject(Object o, long offset, Object expected, Object x) {
4302         return weakCompareAndSetReference(o, offset, expected, x);
4303     }
4304     @Deprecated(since="12", forRemoval=true)
4305     public final boolean weakCompareAndSetObjectAcquire(Object o, long offset, Object expected, Object x) {
4306         return weakCompareAndSetReferenceAcquire(o, offset, expected, x);
4307     }
4308     @Deprecated(since="12", forRemoval=true)
4309     public final boolean weakCompareAndSetObjectPlain(Object o, long offset, Object expected, Object x) {
4310         return weakCompareAndSetReferencePlain(o, offset, expected, x);
4311     }
4312     @Deprecated(since="12", forRemoval=true)
4313     public final boolean weakCompareAndSetObjectRelease(Object o, long offset, Object expected, Object x) {
4314         return weakCompareAndSetReferenceRelease(o, offset, expected, x);
4315     }
4316 }