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