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