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