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