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