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