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