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