1 /*
   2  * Copyright (c) 2000, 2016, 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 java.lang.reflect.Field;
  29 import java.security.ProtectionDomain;
  30 
  31 import jdk.internal.reflect.CallerSensitive;
  32 import jdk.internal.reflect.Reflection;
  33 import jdk.internal.misc.VM;
  34 
  35 import jdk.internal.HotSpotIntrinsicCandidate;
  36 import jdk.internal.vm.annotation.ForceInline;
  37 
  38 
  39 /**
  40  * A collection of methods for performing low-level, unsafe operations.
  41  * Although the class and all methods are public, use of this class is
  42  * limited because only trusted code can obtain instances of it.
  43  *
  44  * <em>Note:</em> It is the resposibility of the caller to make sure
  45  * arguments are checked before methods of this class are
  46  * called. While some rudimentary checks are performed on the input,
  47  * the checks are best effort and when performance is an overriding
  48  * priority, as when methods of this class are optimized by the
  49  * runtime compiler, some or all checks (if any) may be elided. Hence,
  50  * the caller must not rely on the checks and corresponding
  51  * exceptions!
  52  *
  53  * @author John R. Rose
  54  * @see #getUnsafe
  55  */
  56 
  57 public final class Unsafe {
  58 
  59     private static native void registerNatives();
  60     static {
  61         registerNatives();
  62         Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  63     }
  64 
  65     private Unsafe() {}
  66 
  67     private static final Unsafe theUnsafe = new Unsafe();
  68 
  69     /**
  70      * Provides the caller with the capability of performing unsafe
  71      * operations.
  72      *
  73      * <p>The returned {@code Unsafe} object should be carefully guarded
  74      * by the caller, since it can be used to read and write data at arbitrary
  75      * memory addresses.  It must never be passed to untrusted code.
  76      *
  77      * <p>Most methods in this class are very low-level, and correspond to a
  78      * small number of hardware instructions (on typical machines).  Compilers
  79      * are encouraged to optimize these methods accordingly.
  80      *
  81      * <p>Here is a suggested idiom for using unsafe operations:
  82      *
  83      * <pre> {@code
  84      * class MyTrustedClass {
  85      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  86      *   ...
  87      *   private long myCountAddress = ...;
  88      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  89      * }}</pre>
  90      *
  91      * (It may assist compilers to make the local variable {@code final}.)
  92      *
  93      * @throws  SecurityException  if a security manager exists and its
  94      *          {@code checkPropertiesAccess} method doesn't allow
  95      *          access to the system properties.
  96      */
  97     @CallerSensitive
  98     public static Unsafe getUnsafe() {
  99         Class<?> caller = Reflection.getCallerClass();
 100         if (!VM.isSystemDomainLoader(caller.getClassLoader()))
 101             throw new SecurityException("Unsafe");
 102         return theUnsafe;
 103     }
 104 
 105     /// peek and poke operations
 106     /// (compilers should optimize these to memory ops)
 107 
 108     // These work on object fields in the Java heap.
 109     // They will not work on elements of packed arrays.
 110 
 111     /**
 112      * Fetches a value from a given Java variable.
 113      * More specifically, fetches a field or array element within the given
 114      * object {@code o} at the given offset, or (if {@code o} is null)
 115      * from the memory address whose numerical value is the given offset.
 116      * <p>
 117      * The results are undefined unless one of the following cases is true:
 118      * <ul>
 119      * <li>The offset was obtained from {@link #objectFieldOffset} on
 120      * the {@link java.lang.reflect.Field} of some Java field and the object
 121      * referred to by {@code o} is of a class compatible with that
 122      * field's class.
 123      *
 124      * <li>The offset and object reference {@code o} (either null or
 125      * non-null) were both obtained via {@link #staticFieldOffset}
 126      * and {@link #staticFieldBase} (respectively) from the
 127      * reflective {@link Field} representation of some Java field.
 128      *
 129      * <li>The object referred to by {@code o} is an array, and the offset
 130      * is an integer of the form {@code B+N*S}, where {@code N} is
 131      * a valid index into the array, and {@code B} and {@code S} are
 132      * the values obtained by {@link #arrayBaseOffset} and {@link
 133      * #arrayIndexScale} (respectively) from the array's class.  The value
 134      * referred to is the {@code N}<em>th</em> element of the array.
 135      *
 136      * </ul>
 137      * <p>
 138      * If one of the above cases is true, the call references a specific Java
 139      * variable (field or array element).  However, the results are undefined
 140      * if that variable is not in fact of the type returned by this method.
 141      * <p>
 142      * This method refers to a variable by means of two parameters, and so
 143      * it provides (in effect) a <em>double-register</em> addressing mode
 144      * for Java variables.  When the object reference is null, this method
 145      * uses its offset as an absolute address.  This is similar in operation
 146      * to methods such as {@link #getInt(long)}, which provide (in effect) a
 147      * <em>single-register</em> addressing mode for non-Java variables.
 148      * However, because Java variables may have a different layout in memory
 149      * from non-Java variables, programmers should not assume that these
 150      * two addressing modes are ever equivalent.  Also, programmers should
 151      * remember that offsets from the double-register addressing mode cannot
 152      * be portably confused with longs used in the single-register addressing
 153      * mode.
 154      *
 155      * @param o Java heap object in which the variable resides, if any, else
 156      *        null
 157      * @param offset indication of where the variable resides in a Java heap
 158      *        object, if any, else a memory address locating the variable
 159      *        statically
 160      * @return the value fetched from the indicated Java variable
 161      * @throws RuntimeException No defined exceptions are thrown, not even
 162      *         {@link NullPointerException}
 163      */
 164     @HotSpotIntrinsicCandidate
 165     public native int getInt(Object o, long offset);
 166 
 167     /**
 168      * Stores a value into a given Java variable.
 169      * <p>
 170      * The first two parameters are interpreted exactly as with
 171      * {@link #getInt(Object, long)} to refer to a specific
 172      * Java variable (field or array element).  The given value
 173      * is stored into that variable.
 174      * <p>
 175      * The variable must be of the same type as the method
 176      * parameter {@code x}.
 177      *
 178      * @param o Java heap object in which the variable resides, if any, else
 179      *        null
 180      * @param offset indication of where the variable resides in a Java heap
 181      *        object, if any, else a memory address locating the variable
 182      *        statically
 183      * @param x the value to store into the indicated Java variable
 184      * @throws RuntimeException No defined exceptions are thrown, not even
 185      *         {@link NullPointerException}
 186      */
 187     @HotSpotIntrinsicCandidate
 188     public native void putInt(Object o, long offset, int x);
 189 
 190     /**
 191      * Fetches a reference value from a given Java variable.
 192      * @see #getInt(Object, long)
 193      */
 194     @HotSpotIntrinsicCandidate
 195     public native Object getObject(Object o, long offset);
 196 
 197     /**
 198      * Stores a reference value into a given Java variable.
 199      * <p>
 200      * Unless the reference {@code x} being stored is either null
 201      * or matches the field type, the results are undefined.
 202      * If the reference {@code o} is non-null, card marks or
 203      * other store barriers for that object (if the VM requires them)
 204      * are updated.
 205      * @see #putInt(Object, long, int)
 206      */
 207     @HotSpotIntrinsicCandidate
 208     public native void putObject(Object o, long offset, Object x);
 209 
 210     /** @see #getInt(Object, long) */
 211     @HotSpotIntrinsicCandidate
 212     public native boolean getBoolean(Object o, long offset);
 213 
 214     /** @see #putInt(Object, long, int) */
 215     @HotSpotIntrinsicCandidate
 216     public native void    putBoolean(Object o, long offset, boolean x);
 217 
 218     /** @see #getInt(Object, long) */
 219     @HotSpotIntrinsicCandidate
 220     public native byte    getByte(Object o, long offset);
 221 
 222     /** @see #putInt(Object, long, int) */
 223     @HotSpotIntrinsicCandidate
 224     public native void    putByte(Object o, long offset, byte x);
 225 
 226     /** @see #getInt(Object, long) */
 227     @HotSpotIntrinsicCandidate
 228     public native short   getShort(Object o, long offset);
 229 
 230     /** @see #putInt(Object, long, int) */
 231     @HotSpotIntrinsicCandidate
 232     public native void    putShort(Object o, long offset, short x);
 233 
 234     /** @see #getInt(Object, long) */
 235     @HotSpotIntrinsicCandidate
 236     public native char    getChar(Object o, long offset);
 237 
 238     /** @see #putInt(Object, long, int) */
 239     @HotSpotIntrinsicCandidate
 240     public native void    putChar(Object o, long offset, char x);
 241 
 242     /** @see #getInt(Object, long) */
 243     @HotSpotIntrinsicCandidate
 244     public native long    getLong(Object o, long offset);
 245 
 246     /** @see #putInt(Object, long, int) */
 247     @HotSpotIntrinsicCandidate
 248     public native void    putLong(Object o, long offset, long x);
 249 
 250     /** @see #getInt(Object, long) */
 251     @HotSpotIntrinsicCandidate
 252     public native float   getFloat(Object o, long offset);
 253 
 254     /** @see #putInt(Object, long, int) */
 255     @HotSpotIntrinsicCandidate
 256     public native void    putFloat(Object o, long offset, float x);
 257 
 258     /** @see #getInt(Object, long) */
 259     @HotSpotIntrinsicCandidate
 260     public native double  getDouble(Object o, long offset);
 261 
 262     /** @see #putInt(Object, long, int) */
 263     @HotSpotIntrinsicCandidate
 264     public native void    putDouble(Object o, long offset, double x);
 265 
 266     /** @see #getInt(Object, long) */
 267     @ForceInline
 268     public long getAddress(Object o, long offset) {
 269         if (ADDRESS_SIZE == 4) {
 270             return Integer.toUnsignedLong(getInt(o, offset));
 271         } else {
 272             return getLong(o, offset);
 273         }
 274     }
 275 
 276     /** @see #putInt(Object, long, int) */
 277     @ForceInline
 278     public void putAddress(Object o, long offset, long x) {
 279         if (ADDRESS_SIZE == 4) {
 280             putInt(o, offset, (int)x);
 281         } else {
 282             putLong(o, offset, x);
 283         }
 284     }
 285 
 286     // These read VM internal data.
 287 
 288     /**
 289      * Fetches an uncompressed reference value from a given native variable
 290      * ignoring the VM's compressed references mode.
 291      *
 292      * @param address a memory address locating the variable
 293      * @return the value fetched from the indicated native variable
 294      */
 295     public native Object getUncompressedObject(long address);
 296 
 297     /**
 298      * Fetches the {@link java.lang.Class} Java mirror for the given native
 299      * metaspace {@code Klass} pointer.
 300      *
 301      * @param metaspaceKlass a native metaspace {@code Klass} pointer
 302      * @return the {@link java.lang.Class} Java mirror
 303      */
 304     public native Class<?> getJavaMirror(long metaspaceKlass);
 305 
 306     /**
 307      * Fetches a native metaspace {@code Klass} pointer for the given Java
 308      * object.
 309      *
 310      * @param o Java heap object for which to fetch the class pointer
 311      * @return a native metaspace {@code Klass} pointer
 312      */
 313     public native long getKlassPointer(Object o);
 314 
 315     // These work on values in the C heap.
 316 
 317     /**
 318      * Fetches a value from a given memory address.  If the address is zero, or
 319      * does not point into a block obtained from {@link #allocateMemory}, the
 320      * results are undefined.
 321      *
 322      * @see #allocateMemory
 323      */
 324     @ForceInline
 325     public byte getByte(long address) {
 326         return getByte(null, address);
 327     }
 328 
 329     /**
 330      * Stores a value into a given memory address.  If the address is zero, or
 331      * does not point into a block obtained from {@link #allocateMemory}, the
 332      * results are undefined.
 333      *
 334      * @see #getByte(long)
 335      */
 336     @ForceInline
 337     public void putByte(long address, byte x) {
 338         putByte(null, address, x);
 339     }
 340 
 341     /** @see #getByte(long) */
 342     @ForceInline
 343     public short getShort(long address) {
 344         return getShort(null, address);
 345     }
 346 
 347     /** @see #putByte(long, byte) */
 348     @ForceInline
 349     public void putShort(long address, short x) {
 350         putShort(null, address, x);
 351     }
 352 
 353     /** @see #getByte(long) */
 354     @ForceInline
 355     public char getChar(long address) {
 356         return getChar(null, address);
 357     }
 358 
 359     /** @see #putByte(long, byte) */
 360     @ForceInline
 361     public void putChar(long address, char x) {
 362         putChar(null, address, x);
 363     }
 364 
 365     /** @see #getByte(long) */
 366     @ForceInline
 367     public int getInt(long address) {
 368         return getInt(null, address);
 369     }
 370 
 371     /** @see #putByte(long, byte) */
 372     @ForceInline
 373     public void putInt(long address, int x) {
 374         putInt(null, address, x);
 375     }
 376 
 377     /** @see #getByte(long) */
 378     @ForceInline
 379     public long getLong(long address) {
 380         return getLong(null, address);
 381     }
 382 
 383     /** @see #putByte(long, byte) */
 384     @ForceInline
 385     public void putLong(long address, long x) {
 386         putLong(null, address, x);
 387     }
 388 
 389     /** @see #getByte(long) */
 390     @ForceInline
 391     public float getFloat(long address) {
 392         return getFloat(null, address);
 393     }
 394 
 395     /** @see #putByte(long, byte) */
 396     @ForceInline
 397     public void putFloat(long address, float x) {
 398         putFloat(null, address, x);
 399     }
 400 
 401     /** @see #getByte(long) */
 402     @ForceInline
 403     public double getDouble(long address) {
 404         return getDouble(null, address);
 405     }
 406 
 407     /** @see #putByte(long, byte) */
 408     @ForceInline
 409     public void putDouble(long address, double x) {
 410         putDouble(null, address, x);
 411     }
 412 
 413     /**
 414      * Fetches a native pointer from a given memory address.  If the address is
 415      * zero, or does not point into a block obtained from {@link
 416      * #allocateMemory}, the results are undefined.
 417      *
 418      * <p>If the native pointer is less than 64 bits wide, it is extended as
 419      * an unsigned number to a Java long.  The pointer may be indexed by any
 420      * given byte offset, simply by adding that offset (as a simple integer) to
 421      * the long representing the pointer.  The number of bytes actually read
 422      * from the target address may be determined by consulting {@link
 423      * #addressSize}.
 424      *
 425      * @see #allocateMemory
 426      */
 427     @ForceInline
 428     public long getAddress(long address) {
 429         return getAddress(null, address);
 430     }
 431 
 432     /**
 433      * Stores a native pointer into a given memory address.  If the address is
 434      * zero, or does not point into a block obtained from {@link
 435      * #allocateMemory}, the results are undefined.
 436      *
 437      * <p>The number of bytes actually written at the target address may be
 438      * determined by consulting {@link #addressSize}.
 439      *
 440      * @see #getAddress(long)
 441      */
 442     @ForceInline
 443     public void putAddress(long address, long x) {
 444         putAddress(null, address, x);
 445     }
 446 
 447 
 448 
 449     /// helper methods for validating various types of objects/values
 450 
 451     /**
 452      * Create an exception reflecting that some of the input was invalid
 453      *
 454      * <em>Note:</em> It is the resposibility of the caller to make
 455      * sure arguments are checked before the methods are called. While
 456      * some rudimentary checks are performed on the input, the checks
 457      * are best effort and when performance is an overriding priority,
 458      * as when methods of this class are optimized by the runtime
 459      * compiler, some or all checks (if any) may be elided. Hence, the
 460      * caller must not rely on the checks and corresponding
 461      * exceptions!
 462      *
 463      * @return an exception object
 464      */
 465     private RuntimeException invalidInput() {
 466         return new IllegalArgumentException();
 467     }
 468 
 469     /**
 470      * Check if a value is 32-bit clean (32 MSB are all zero)
 471      *
 472      * @param value the 64-bit value to check
 473      *
 474      * @return true if the value is 32-bit clean
 475      */
 476     private boolean is32BitClean(long value) {
 477         return value >>> 32 == 0;
 478     }
 479 
 480     /**
 481      * Check the validity of a size (the equivalent of a size_t)
 482      *
 483      * @throws RuntimeException if the size is invalid
 484      *         (<em>Note:</em> after optimization, invalid inputs may
 485      *         go undetected, which will lead to unpredictable
 486      *         behavior)
 487      */
 488     private void checkSize(long size) {
 489         if (ADDRESS_SIZE == 4) {
 490             // Note: this will also check for negative sizes
 491             if (!is32BitClean(size)) {
 492                 throw invalidInput();
 493             }
 494         } else if (size < 0) {
 495             throw invalidInput();
 496         }
 497     }
 498 
 499     /**
 500      * Check the validity of a native address (the equivalent of void*)
 501      *
 502      * @throws RuntimeException if the address 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 checkNativeAddress(long address) {
 508         if (ADDRESS_SIZE == 4) {
 509             // Accept both zero and sign extended pointers. A valid
 510             // pointer will, after the +1 below, either have produced
 511             // the value 0x0 or 0x1. Masking off the low bit allows
 512             // for testing against 0.
 513             if ((((address >> 32) + 1) & ~1) != 0) {
 514                 throw invalidInput();
 515             }
 516         }
 517     }
 518 
 519     /**
 520      * Check the validity of an offset, relative to a base object
 521      *
 522      * @param o the base object
 523      * @param offset the offset to check
 524      *
 525      * @throws RuntimeException if the size is invalid
 526      *         (<em>Note:</em> after optimization, invalid inputs may
 527      *         go undetected, which will lead to unpredictable
 528      *         behavior)
 529      */
 530     private void checkOffset(Object o, long offset) {
 531         if (ADDRESS_SIZE == 4) {
 532             // Note: this will also check for negative offsets
 533             if (!is32BitClean(offset)) {
 534                 throw invalidInput();
 535             }
 536         } else if (offset < 0) {
 537             throw invalidInput();
 538         }
 539     }
 540 
 541     /**
 542      * Check the validity of a double-register pointer
 543      *
 544      * Note: This code deliberately does *not* check for NPE for (at
 545      * least) three reasons:
 546      *
 547      * 1) NPE is not just NULL/0 - there is a range of values all
 548      * resulting in an NPE, which is not trivial to check for
 549      *
 550      * 2) It is the responsibility of the callers of Unsafe methods
 551      * to verify the input, so throwing an exception here is not really
 552      * useful - passing in a NULL pointer is a critical error and the
 553      * must not expect an exception to be thrown anyway.
 554      *
 555      * 3) the actual operations will detect NULL pointers anyway by
 556      * means of traps and signals (like SIGSEGV).
 557      *
 558      * @param o Java heap object, or null
 559      * @param offset indication of where the variable resides in a Java heap
 560      *        object, if any, else a memory address locating the variable
 561      *        statically
 562      *
 563      * @throws RuntimeException if the pointer is invalid
 564      *         (<em>Note:</em> after optimization, invalid inputs may
 565      *         go undetected, which will lead to unpredictable
 566      *         behavior)
 567      */
 568     private void checkPointer(Object o, long offset) {
 569         if (o == null) {
 570             checkNativeAddress(offset);
 571         } else {
 572             checkOffset(o, offset);
 573         }
 574     }
 575 
 576     /**
 577      * Check if a type is a primitive array type
 578      *
 579      * @param c the type to check
 580      *
 581      * @return true if the type is a primitive array type
 582      */
 583     private void checkPrimitiveArray(Class<?> c) {
 584         Class<?> componentType = c.getComponentType();
 585         if (componentType == null || !componentType.isPrimitive()) {
 586             throw invalidInput();
 587         }
 588     }
 589 
 590     /**
 591      * Check that a pointer is a valid primitive array type pointer
 592      *
 593      * Note: pointers off-heap are considered to be primitive arrays
 594      *
 595      * @throws RuntimeException if the pointer is invalid
 596      *         (<em>Note:</em> after optimization, invalid inputs may
 597      *         go undetected, which will lead to unpredictable
 598      *         behavior)
 599      */
 600     private void checkPrimitivePointer(Object o, long offset) {
 601         checkPointer(o, offset);
 602 
 603         if (o != null) {
 604             // If on heap, it it must be a primitive array
 605             checkPrimitiveArray(o.getClass());
 606         }
 607     }
 608 
 609 
 610     /// wrappers for malloc, realloc, free:
 611 
 612     /**
 613      * Allocates a new block of native memory, of the given size in bytes.  The
 614      * contents of the memory are uninitialized; they will generally be
 615      * garbage.  The resulting native pointer will never be zero, and will be
 616      * aligned for all value types.  Dispose of this memory by calling {@link
 617      * #freeMemory}, or resize it with {@link #reallocateMemory}.
 618      *
 619      * <em>Note:</em> It is the resposibility of the caller to make
 620      * sure arguments are checked before the methods are called. While
 621      * some rudimentary checks are performed on the input, the checks
 622      * are best effort and when performance is an overriding priority,
 623      * as when methods of this class are optimized by the runtime
 624      * compiler, some or all checks (if any) may be elided. Hence, the
 625      * caller must not rely on the checks and corresponding
 626      * exceptions!
 627      *
 628      * @throws RuntimeException if the size is negative or too large
 629      *         for the native size_t type
 630      *
 631      * @throws OutOfMemoryError if the allocation is refused by the system
 632      *
 633      * @see #getByte(long)
 634      * @see #putByte(long, byte)
 635      */
 636     public long allocateMemory(long bytes) {
 637         allocateMemoryChecks(bytes);
 638 
 639         if (bytes == 0) {
 640             return 0;
 641         }
 642 
 643         long p = allocateMemory0(bytes);
 644         if (p == 0) {
 645             throw new OutOfMemoryError();
 646         }
 647 
 648         return p;
 649     }
 650 
 651     /**
 652      * Validate the arguments to allocateMemory
 653      *
 654      * @throws RuntimeException if the arguments are invalid
 655      *         (<em>Note:</em> after optimization, invalid inputs may
 656      *         go undetected, which will lead to unpredictable
 657      *         behavior)
 658      */
 659     private void allocateMemoryChecks(long bytes) {
 660         checkSize(bytes);
 661     }
 662 
 663     /**
 664      * Resizes a new block of native memory, to the given size in bytes.  The
 665      * contents of the new block past the size of the old block are
 666      * uninitialized; they will generally be garbage.  The resulting native
 667      * pointer will be zero if and only if the requested size is zero.  The
 668      * resulting native pointer will be aligned for all value types.  Dispose
 669      * of this memory by calling {@link #freeMemory}, or resize it with {@link
 670      * #reallocateMemory}.  The address passed to this method may be null, in
 671      * which case an allocation will be performed.
 672      *
 673      * <em>Note:</em> It is the resposibility of the caller to make
 674      * sure arguments are checked before the methods are called. While
 675      * some rudimentary checks are performed on the input, the checks
 676      * are best effort and when performance is an overriding priority,
 677      * as when methods of this class are optimized by the runtime
 678      * compiler, some or all checks (if any) may be elided. Hence, the
 679      * caller must not rely on the checks and corresponding
 680      * exceptions!
 681      *
 682      * @throws RuntimeException if the size is negative or too large
 683      *         for the native size_t type
 684      *
 685      * @throws OutOfMemoryError if the allocation is refused by the system
 686      *
 687      * @see #allocateMemory
 688      */
 689     public long reallocateMemory(long address, long bytes) {
 690         reallocateMemoryChecks(address, bytes);
 691 
 692         if (bytes == 0) {
 693             freeMemory(address);
 694             return 0;
 695         }
 696 
 697         long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
 698         if (p == 0) {
 699             throw new OutOfMemoryError();
 700         }
 701 
 702         return p;
 703     }
 704 
 705     /**
 706      * Validate the arguments to reallocateMemory
 707      *
 708      * @throws RuntimeException if the arguments are invalid
 709      *         (<em>Note:</em> after optimization, invalid inputs may
 710      *         go undetected, which will lead to unpredictable
 711      *         behavior)
 712      */
 713     private void reallocateMemoryChecks(long address, long bytes) {
 714         checkPointer(null, address);
 715         checkSize(bytes);
 716     }
 717 
 718     /**
 719      * Sets all bytes in a given block of memory to a fixed value
 720      * (usually zero).
 721      *
 722      * <p>This method determines a block's base address by means of two parameters,
 723      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 724      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 725      * the offset supplies an absolute base address.
 726      *
 727      * <p>The stores are in coherent (atomic) units of a size determined
 728      * by the address and length parameters.  If the effective address and
 729      * length are all even modulo 8, the stores take place in 'long' units.
 730      * If the effective address and length are (resp.) even modulo 4 or 2,
 731      * the stores take place in units of 'int' or 'short'.
 732      *
 733      * <em>Note:</em> It is the resposibility of the caller to make
 734      * sure arguments are checked before the methods are called. While
 735      * some rudimentary checks are performed on the input, the checks
 736      * are best effort and when performance is an overriding priority,
 737      * as when methods of this class are optimized by the runtime
 738      * compiler, some or all checks (if any) may be elided. Hence, the
 739      * caller must not rely on the checks and corresponding
 740      * exceptions!
 741      *
 742      * @throws RuntimeException if any of the arguments is invalid
 743      *
 744      * @since 1.7
 745      */
 746     public void setMemory(Object o, long offset, long bytes, byte value) {
 747         setMemoryChecks(o, offset, bytes, value);
 748 
 749         if (bytes == 0) {
 750             return;
 751         }
 752 
 753         setMemory0(o, offset, bytes, value);
 754     }
 755 
 756     /**
 757      * Sets all bytes in a given block of memory to a fixed value
 758      * (usually zero).  This provides a <em>single-register</em> addressing mode,
 759      * as discussed in {@link #getInt(Object,long)}.
 760      *
 761      * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
 762      */
 763     public void setMemory(long address, long bytes, byte value) {
 764         setMemory(null, address, bytes, value);
 765     }
 766 
 767     /**
 768      * Validate the arguments to setMemory
 769      *
 770      * @throws RuntimeException if the arguments are invalid
 771      *         (<em>Note:</em> after optimization, invalid inputs may
 772      *         go undetected, which will lead to unpredictable
 773      *         behavior)
 774      */
 775     private void setMemoryChecks(Object o, long offset, long bytes, byte value) {
 776         checkPrimitivePointer(o, offset);
 777         checkSize(bytes);
 778     }
 779 
 780     /**
 781      * Sets all bytes in a given block of memory to a copy of another
 782      * block.
 783      *
 784      * <p>This method determines each block's base address by means of two parameters,
 785      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 786      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 787      * the offset supplies an absolute base address.
 788      *
 789      * <p>The transfers are in coherent (atomic) units of a size determined
 790      * by the address and length parameters.  If the effective addresses and
 791      * length are all even modulo 8, the transfer takes place in 'long' units.
 792      * If the effective addresses and length are (resp.) even modulo 4 or 2,
 793      * the transfer takes place in units of 'int' or 'short'.
 794      *
 795      * <em>Note:</em> It is the resposibility of the caller to make
 796      * sure arguments are checked before the methods are called. While
 797      * some rudimentary checks are performed on the input, the checks
 798      * are best effort and when performance is an overriding priority,
 799      * as when methods of this class are optimized by the runtime
 800      * compiler, some or all checks (if any) may be elided. Hence, the
 801      * caller must not rely on the checks and corresponding
 802      * exceptions!
 803      *
 804      * @throws RuntimeException if any of the arguments is invalid
 805      *
 806      * @since 1.7
 807      */
 808     public void copyMemory(Object srcBase, long srcOffset,
 809                            Object destBase, long destOffset,
 810                            long bytes) {
 811         copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
 812 
 813         if (bytes == 0) {
 814             return;
 815         }
 816 
 817         copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
 818     }
 819 
 820     /**
 821      * Sets all bytes in a given block of memory to a copy of another
 822      * block.  This provides a <em>single-register</em> addressing mode,
 823      * as discussed in {@link #getInt(Object,long)}.
 824      *
 825      * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
 826      */
 827     public void copyMemory(long srcAddress, long destAddress, long bytes) {
 828         copyMemory(null, srcAddress, null, destAddress, bytes);
 829     }
 830 
 831     /**
 832      * Validate the arguments to copyMemory
 833      *
 834      * @throws RuntimeException if any of the arguments is invalid
 835      *         (<em>Note:</em> after optimization, invalid inputs may
 836      *         go undetected, which will lead to unpredictable
 837      *         behavior)
 838      */
 839     private void copyMemoryChecks(Object srcBase, long srcOffset,
 840                                   Object destBase, long destOffset,
 841                                   long bytes) {
 842         checkSize(bytes);
 843         checkPrimitivePointer(srcBase, srcOffset);
 844         checkPrimitivePointer(destBase, destOffset);
 845     }
 846 
 847     /**
 848      * Copies all elements from one block of memory to another block,
 849      * *unconditionally* byte swapping the elements on the fly.
 850      *
 851      * <p>This method determines each block's base address by means of two parameters,
 852      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 853      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 854      * the offset supplies an absolute base address.
 855      *
 856      * <em>Note:</em> It is the resposibility of the caller to make
 857      * sure arguments are checked before the methods are called. While
 858      * some rudimentary checks are performed on the input, the checks
 859      * are best effort and when performance is an overriding priority,
 860      * as when methods of this class are optimized by the runtime
 861      * compiler, some or all checks (if any) may be elided. Hence, the
 862      * caller must not rely on the checks and corresponding
 863      * exceptions!
 864      *
 865      * @throws RuntimeException if any of the arguments is invalid
 866      *
 867      * @since 9
 868      */
 869     public void copySwapMemory(Object srcBase, long srcOffset,
 870                                Object destBase, long destOffset,
 871                                long bytes, long elemSize) {
 872         copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 873 
 874         if (bytes == 0) {
 875             return;
 876         }
 877 
 878         copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 879     }
 880 
 881     private void copySwapMemoryChecks(Object srcBase, long srcOffset,
 882                                       Object destBase, long destOffset,
 883                                       long bytes, long elemSize) {
 884         checkSize(bytes);
 885 
 886         if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
 887             throw invalidInput();
 888         }
 889         if (bytes % elemSize != 0) {
 890             throw invalidInput();
 891         }
 892 
 893         checkPrimitivePointer(srcBase, srcOffset);
 894         checkPrimitivePointer(destBase, destOffset);
 895     }
 896 
 897    /**
 898      * Copies all elements from one block of memory to another block, byte swapping the
 899      * elements on the fly.
 900      *
 901      * This provides a <em>single-register</em> addressing mode, as
 902      * discussed in {@link #getInt(Object,long)}.
 903      *
 904      * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
 905      */
 906     public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
 907         copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
 908     }
 909 
 910     /**
 911      * Disposes of a block of native memory, as obtained from {@link
 912      * #allocateMemory} or {@link #reallocateMemory}.  The address passed to
 913      * this method may be null, in which case no action is taken.
 914      *
 915      * <em>Note:</em> It is the resposibility of the caller to make
 916      * sure arguments are checked before the methods are called. While
 917      * some rudimentary checks are performed on the input, the checks
 918      * are best effort and when performance is an overriding priority,
 919      * as when methods of this class are optimized by the runtime
 920      * compiler, some or all checks (if any) may be elided. Hence, the
 921      * caller must not rely on the checks and corresponding
 922      * exceptions!
 923      *
 924      * @throws RuntimeException if any of the arguments is invalid
 925      *
 926      * @see #allocateMemory
 927      */
 928     public void freeMemory(long address) {
 929         freeMemoryChecks(address);
 930 
 931         if (address == 0) {
 932             return;
 933         }
 934 
 935         freeMemory0(address);
 936     }
 937 
 938     /**
 939      * Validate the arguments to freeMemory
 940      *
 941      * @throws RuntimeException if the arguments are invalid
 942      *         (<em>Note:</em> after optimization, invalid inputs may
 943      *         go undetected, which will lead to unpredictable
 944      *         behavior)
 945      */
 946     private void freeMemoryChecks(long address) {
 947         checkPointer(null, address);
 948     }
 949 
 950     /// random queries
 951 
 952     /**
 953      * This constant differs from all results that will ever be returned from
 954      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 955      * or {@link #arrayBaseOffset}.
 956      */
 957     public static final int INVALID_FIELD_OFFSET = -1;
 958 
 959     /**
 960      * Reports the location of a given field in the storage allocation of its
 961      * class.  Do not expect to perform any sort of arithmetic on this offset;
 962      * it is just a cookie which is passed to the unsafe heap memory accessors.
 963      *
 964      * <p>Any given field will always have the same offset and base, and no
 965      * two distinct fields of the same class will ever have the same offset
 966      * and base.
 967      *
 968      * <p>As of 1.4.1, offsets for fields are represented as long values,
 969      * although the Sun JVM does not use the most significant 32 bits.
 970      * However, JVM implementations which store static fields at absolute
 971      * addresses can use long offsets and null base pointers to express
 972      * the field locations in a form usable by {@link #getInt(Object,long)}.
 973      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 974      * must preserve all bits of static field offsets.
 975      * @see #getInt(Object, long)
 976      */
 977     public long objectFieldOffset(Field f) {
 978         if (f == null) {
 979             throw new NullPointerException();
 980         }
 981 
 982         return objectFieldOffset0(f);
 983     }
 984 
 985     /**
 986      * Reports the location of a given static field, in conjunction with {@link
 987      * #staticFieldBase}.
 988      * <p>Do not expect to perform any sort of arithmetic on this offset;
 989      * it is just a cookie which is passed to the unsafe heap memory accessors.
 990      *
 991      * <p>Any given field will always have the same offset, and no two distinct
 992      * fields of the same class will ever have the same offset.
 993      *
 994      * <p>As of 1.4.1, offsets for fields are represented as long values,
 995      * although the Sun JVM does not use the most significant 32 bits.
 996      * It is hard to imagine a JVM technology which needs more than
 997      * a few bits to encode an offset within a non-array object,
 998      * However, for consistency with other methods in this class,
 999      * this method reports its result as a long value.
1000      * @see #getInt(Object, long)
1001      */
1002     public long staticFieldOffset(Field f) {
1003         if (f == null) {
1004             throw new NullPointerException();
1005         }
1006 
1007         return staticFieldOffset0(f);
1008     }
1009 
1010     /**
1011      * Reports the location of a given static field, in conjunction with {@link
1012      * #staticFieldOffset}.
1013      * <p>Fetch the base "Object", if any, with which static fields of the
1014      * given class can be accessed via methods like {@link #getInt(Object,
1015      * long)}.  This value may be null.  This value may refer to an object
1016      * which is a "cookie", not guaranteed to be a real Object, and it should
1017      * not be used in any way except as argument to the get and put routines in
1018      * this class.
1019      */
1020     public Object staticFieldBase(Field f) {
1021         if (f == null) {
1022             throw new NullPointerException();
1023         }
1024 
1025         return staticFieldBase0(f);
1026     }
1027 
1028     /**
1029      * Detects if the given class may need to be initialized. This is often
1030      * needed in conjunction with obtaining the static field base of a
1031      * class.
1032      * @return false only if a call to {@code ensureClassInitialized} would have no effect
1033      */
1034     public boolean shouldBeInitialized(Class<?> c) {
1035         if (c == null) {
1036             throw new NullPointerException();
1037         }
1038 
1039         return shouldBeInitialized0(c);
1040     }
1041 
1042     /**
1043      * Ensures the given class has been initialized. This is often
1044      * needed in conjunction with obtaining the static field base of a
1045      * class.
1046      */
1047     public void ensureClassInitialized(Class<?> c) {
1048         if (c == null) {
1049             throw new NullPointerException();
1050         }
1051 
1052         ensureClassInitialized0(c);
1053     }
1054 
1055     /**
1056      * Reports the offset of the first element in the storage allocation of a
1057      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
1058      * for the same class, you may use that scale factor, together with this
1059      * base offset, to form new offsets to access elements of arrays of the
1060      * given class.
1061      *
1062      * @see #getInt(Object, long)
1063      * @see #putInt(Object, long, int)
1064      */
1065     public int arrayBaseOffset(Class<?> arrayClass) {
1066         if (arrayClass == null) {
1067             throw new NullPointerException();
1068         }
1069 
1070         return arrayBaseOffset0(arrayClass);
1071     }
1072 
1073 
1074     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1075     public static final int ARRAY_BOOLEAN_BASE_OFFSET
1076             = theUnsafe.arrayBaseOffset(boolean[].class);
1077 
1078     /** The value of {@code arrayBaseOffset(byte[].class)} */
1079     public static final int ARRAY_BYTE_BASE_OFFSET
1080             = theUnsafe.arrayBaseOffset(byte[].class);
1081 
1082     /** The value of {@code arrayBaseOffset(short[].class)} */
1083     public static final int ARRAY_SHORT_BASE_OFFSET
1084             = theUnsafe.arrayBaseOffset(short[].class);
1085 
1086     /** The value of {@code arrayBaseOffset(char[].class)} */
1087     public static final int ARRAY_CHAR_BASE_OFFSET
1088             = theUnsafe.arrayBaseOffset(char[].class);
1089 
1090     /** The value of {@code arrayBaseOffset(int[].class)} */
1091     public static final int ARRAY_INT_BASE_OFFSET
1092             = theUnsafe.arrayBaseOffset(int[].class);
1093 
1094     /** The value of {@code arrayBaseOffset(long[].class)} */
1095     public static final int ARRAY_LONG_BASE_OFFSET
1096             = theUnsafe.arrayBaseOffset(long[].class);
1097 
1098     /** The value of {@code arrayBaseOffset(float[].class)} */
1099     public static final int ARRAY_FLOAT_BASE_OFFSET
1100             = theUnsafe.arrayBaseOffset(float[].class);
1101 
1102     /** The value of {@code arrayBaseOffset(double[].class)} */
1103     public static final int ARRAY_DOUBLE_BASE_OFFSET
1104             = theUnsafe.arrayBaseOffset(double[].class);
1105 
1106     /** The value of {@code arrayBaseOffset(Object[].class)} */
1107     public static final int ARRAY_OBJECT_BASE_OFFSET
1108             = theUnsafe.arrayBaseOffset(Object[].class);
1109 
1110     /**
1111      * Reports the scale factor for addressing elements in the storage
1112      * allocation of a given array class.  However, arrays of "narrow" types
1113      * will generally not work properly with accessors like {@link
1114      * #getByte(Object, long)}, so the scale factor for such classes is reported
1115      * as zero.
1116      *
1117      * @see #arrayBaseOffset
1118      * @see #getInt(Object, long)
1119      * @see #putInt(Object, long, int)
1120      */
1121     public int arrayIndexScale(Class<?> arrayClass) {
1122         if (arrayClass == null) {
1123             throw new NullPointerException();
1124         }
1125 
1126         return arrayIndexScale0(arrayClass);
1127     }
1128 
1129 
1130     /** The value of {@code arrayIndexScale(boolean[].class)} */
1131     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1132             = theUnsafe.arrayIndexScale(boolean[].class);
1133 
1134     /** The value of {@code arrayIndexScale(byte[].class)} */
1135     public static final int ARRAY_BYTE_INDEX_SCALE
1136             = theUnsafe.arrayIndexScale(byte[].class);
1137 
1138     /** The value of {@code arrayIndexScale(short[].class)} */
1139     public static final int ARRAY_SHORT_INDEX_SCALE
1140             = theUnsafe.arrayIndexScale(short[].class);
1141 
1142     /** The value of {@code arrayIndexScale(char[].class)} */
1143     public static final int ARRAY_CHAR_INDEX_SCALE
1144             = theUnsafe.arrayIndexScale(char[].class);
1145 
1146     /** The value of {@code arrayIndexScale(int[].class)} */
1147     public static final int ARRAY_INT_INDEX_SCALE
1148             = theUnsafe.arrayIndexScale(int[].class);
1149 
1150     /** The value of {@code arrayIndexScale(long[].class)} */
1151     public static final int ARRAY_LONG_INDEX_SCALE
1152             = theUnsafe.arrayIndexScale(long[].class);
1153 
1154     /** The value of {@code arrayIndexScale(float[].class)} */
1155     public static final int ARRAY_FLOAT_INDEX_SCALE
1156             = theUnsafe.arrayIndexScale(float[].class);
1157 
1158     /** The value of {@code arrayIndexScale(double[].class)} */
1159     public static final int ARRAY_DOUBLE_INDEX_SCALE
1160             = theUnsafe.arrayIndexScale(double[].class);
1161 
1162     /** The value of {@code arrayIndexScale(Object[].class)} */
1163     public static final int ARRAY_OBJECT_INDEX_SCALE
1164             = theUnsafe.arrayIndexScale(Object[].class);
1165 
1166     /**
1167      * Reports the size in bytes of a native pointer, as stored via {@link
1168      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1169      * other primitive types (as stored in native memory blocks) is determined
1170      * fully by their information content.
1171      */
1172     public int addressSize() {
1173         return ADDRESS_SIZE;
1174     }
1175 
1176     /** The value of {@code addressSize()} */
1177     public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1178 
1179     /**
1180      * Reports the size in bytes of a native memory page (whatever that is).
1181      * This value will always be a power of two.
1182      */
1183     public native int pageSize();
1184 
1185 
1186     /// random trusted operations from JNI:
1187 
1188     /**
1189      * Tells the VM to define a class, without security checks.  By default, the
1190      * class loader and protection domain come from the caller's class.
1191      */
1192     public Class<?> defineClass(String name, byte[] b, int off, int len,
1193                                 ClassLoader loader,
1194                                 ProtectionDomain protectionDomain) {
1195         if (b == null) {
1196             throw new NullPointerException();
1197         }
1198         if (len < 0) {
1199             throw new ArrayIndexOutOfBoundsException();
1200         }
1201 
1202         return defineClass0(name, b, off, len, loader, protectionDomain);
1203     }
1204 
1205     public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1206                                         ClassLoader loader,
1207                                         ProtectionDomain protectionDomain);
1208 
1209     /**
1210      * Defines a class but does not make it known to the class loader or system dictionary.
1211      * <p>
1212      * For each CP entry, the corresponding CP patch must either be null or have
1213      * the a format that matches its tag:
1214      * <ul>
1215      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
1216      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
1217      * <li>Class: any java.lang.Class object
1218      * <li>String: any object (not just a java.lang.String)
1219      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1220      * </ul>
1221      * @param hostClass context for linkage, access control, protection domain, and class loader
1222      * @param data      bytes of a class file
1223      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
1224      */
1225     public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
1226         if (hostClass == null || data == null) {
1227             throw new NullPointerException();
1228         }
1229 
1230         return defineAnonymousClass0(hostClass, data, cpPatches);
1231     }
1232 
1233     /**
1234      * Allocates an instance but does not run any constructor.
1235      * Initializes the class if it has not yet been.
1236      */
1237     @HotSpotIntrinsicCandidate
1238     public native Object allocateInstance(Class<?> cls)
1239         throws InstantiationException;
1240 
1241     /**
1242      * Allocates an array of a given type, but does not do zeroing.
1243      * <p>
1244      * This method should only be used in the very rare cases where a high-performance code
1245      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1246      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1247      * <p>
1248      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1249      * before allowing untrusted code, or code in other threads, to observe the reference
1250      * to the newly allocated array. In addition, the publication of the array reference must be
1251      * safe according to the Java Memory Model requirements.
1252      * <p>
1253      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1254      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1255      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1256      * or issuing a {@link #storeFence} before publishing the reference.
1257      * <p>
1258      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1259      * elements that could break heap integrity.
1260      *
1261      * @param componentType array component type to allocate
1262      * @param length array size to allocate
1263      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1264      *                                  or the length is negative
1265      */
1266     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1267        if (componentType == null) {
1268            throw new IllegalArgumentException("Component type is null");
1269        }
1270        if (!componentType.isPrimitive()) {
1271            throw new IllegalArgumentException("Component type is not primitive");
1272        }
1273        if (length < 0) {
1274            throw new IllegalArgumentException("Negative length");
1275        }
1276        return allocateUninitializedArray0(componentType, length);
1277     }
1278 
1279     @HotSpotIntrinsicCandidate
1280     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1281        // These fallbacks provide zeroed arrays, but intrinsic is not required to
1282        // return the zeroed arrays.
1283        if (componentType == byte.class)    return new byte[length];
1284        if (componentType == boolean.class) return new boolean[length];
1285        if (componentType == short.class)   return new short[length];
1286        if (componentType == char.class)    return new char[length];
1287        if (componentType == int.class)     return new int[length];
1288        if (componentType == float.class)   return new float[length];
1289        if (componentType == long.class)    return new long[length];
1290        if (componentType == double.class)  return new double[length];
1291        return null;
1292     }
1293 
1294     /** Throws the exception without telling the verifier. */
1295     public native void throwException(Throwable ee);
1296 
1297     /**
1298      * Atomically updates Java variable to {@code x} if it is currently
1299      * holding {@code expected}.
1300      *
1301      * <p>This operation has memory semantics of a {@code volatile} read
1302      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1303      *
1304      * @return {@code true} if successful
1305      */
1306     @HotSpotIntrinsicCandidate
1307     public final native boolean compareAndSwapObject(Object o, long offset,
1308                                                      Object expected,
1309                                                      Object x);
1310 
1311     @HotSpotIntrinsicCandidate
1312     public final native Object compareAndExchangeObjectVolatile(Object o, long offset,
1313                                                                 Object expected,
1314                                                                 Object x);
1315 
1316     @HotSpotIntrinsicCandidate
1317     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
1318                                                                Object expected,
1319                                                                Object x) {
1320         return compareAndExchangeObjectVolatile(o, offset, expected, x);
1321     }
1322 
1323     @HotSpotIntrinsicCandidate
1324     public final Object compareAndExchangeObjectRelease(Object o, long offset,
1325                                                                Object expected,
1326                                                                Object x) {
1327         return compareAndExchangeObjectVolatile(o, offset, expected, x);
1328     }
1329 
1330     @HotSpotIntrinsicCandidate
1331     public final boolean weakCompareAndSwapObject(Object o, long offset,
1332                                                          Object expected,
1333                                                          Object x) {
1334         return compareAndSwapObject(o, offset, expected, x);
1335     }
1336 
1337     @HotSpotIntrinsicCandidate
1338     public final boolean weakCompareAndSwapObjectAcquire(Object o, long offset,
1339                                                                 Object expected,
1340                                                                 Object x) {
1341         return compareAndSwapObject(o, offset, expected, x);
1342     }
1343 
1344     @HotSpotIntrinsicCandidate
1345     public final boolean weakCompareAndSwapObjectRelease(Object o, long offset,
1346                                                                 Object expected,
1347                                                                 Object x) {
1348         return compareAndSwapObject(o, offset, expected, x);
1349     }
1350 
1351     /**
1352      * Atomically updates Java variable to {@code x} if it is currently
1353      * holding {@code expected}.
1354      *
1355      * <p>This operation has memory semantics of a {@code volatile} read
1356      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1357      *
1358      * @return {@code true} if successful
1359      */
1360     @HotSpotIntrinsicCandidate
1361     public final native boolean compareAndSwapInt(Object o, long offset,
1362                                                   int expected,
1363                                                   int x);
1364 
1365     @HotSpotIntrinsicCandidate
1366     public final native int compareAndExchangeIntVolatile(Object o, long offset,
1367                                                           int expected,
1368                                                           int x);
1369 
1370     @HotSpotIntrinsicCandidate
1371     public final int compareAndExchangeIntAcquire(Object o, long offset,
1372                                                          int expected,
1373                                                          int x) {
1374         return compareAndExchangeIntVolatile(o, offset, expected, x);
1375     }
1376 
1377     @HotSpotIntrinsicCandidate
1378     public final int compareAndExchangeIntRelease(Object o, long offset,
1379                                                          int expected,
1380                                                          int x) {
1381         return compareAndExchangeIntVolatile(o, offset, expected, x);
1382     }
1383 
1384     @HotSpotIntrinsicCandidate
1385     public final boolean weakCompareAndSwapInt(Object o, long offset,
1386                                                       int expected,
1387                                                       int x) {
1388         return compareAndSwapInt(o, offset, expected, x);
1389     }
1390 
1391     @HotSpotIntrinsicCandidate
1392     public final boolean weakCompareAndSwapIntAcquire(Object o, long offset,
1393                                                              int expected,
1394                                                              int x) {
1395         return compareAndSwapInt(o, offset, expected, x);
1396     }
1397 
1398     @HotSpotIntrinsicCandidate
1399     public final boolean weakCompareAndSwapIntRelease(Object o, long offset,
1400                                                              int expected,
1401                                                              int x) {
1402         return compareAndSwapInt(o, offset, expected, x);
1403     }
1404 
1405     /**
1406      * Atomically updates Java variable to {@code x} if it is currently
1407      * holding {@code expected}.
1408      *
1409      * <p>This operation has memory semantics of a {@code volatile} read
1410      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1411      *
1412      * @return {@code true} if successful
1413      */
1414     @HotSpotIntrinsicCandidate
1415     public final native boolean compareAndSwapLong(Object o, long offset,
1416                                                    long expected,
1417                                                    long x);
1418 
1419     @HotSpotIntrinsicCandidate
1420     public final native long compareAndExchangeLongVolatile(Object o, long offset,
1421                                                             long expected,
1422                                                             long x);
1423 
1424     @HotSpotIntrinsicCandidate
1425     public final long compareAndExchangeLongAcquire(Object o, long offset,
1426                                                            long expected,
1427                                                            long x) {
1428         return compareAndExchangeLongVolatile(o, offset, expected, x);
1429     }
1430 
1431     @HotSpotIntrinsicCandidate
1432     public final long compareAndExchangeLongRelease(Object o, long offset,
1433                                                            long expected,
1434                                                            long x) {
1435         return compareAndExchangeLongVolatile(o, offset, expected, x);
1436     }
1437 
1438     @HotSpotIntrinsicCandidate
1439     public final boolean weakCompareAndSwapLong(Object o, long offset,
1440                                                        long expected,
1441                                                        long x) {
1442         return compareAndSwapLong(o, offset, expected, x);
1443     }
1444 
1445     @HotSpotIntrinsicCandidate
1446     public final boolean weakCompareAndSwapLongAcquire(Object o, long offset,
1447                                                               long expected,
1448                                                               long x) {
1449         return compareAndSwapLong(o, offset, expected, x);
1450     }
1451 
1452     @HotSpotIntrinsicCandidate
1453     public final boolean weakCompareAndSwapLongRelease(Object o, long offset,
1454                                                               long expected,
1455                                                               long x) {
1456         return compareAndSwapLong(o, offset, expected, x);
1457     }
1458 
1459     /**
1460      * Fetches a reference value from a given Java variable, with volatile
1461      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
1462      */
1463     @HotSpotIntrinsicCandidate
1464     public native Object getObjectVolatile(Object o, long offset);
1465 
1466     /**
1467      * Stores a reference value into a given Java variable, with
1468      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
1469      */
1470     @HotSpotIntrinsicCandidate
1471     public native void    putObjectVolatile(Object o, long offset, Object x);
1472 
1473     /** Volatile version of {@link #getInt(Object, long)}  */
1474     @HotSpotIntrinsicCandidate
1475     public native int     getIntVolatile(Object o, long offset);
1476 
1477     /** Volatile version of {@link #putInt(Object, long, int)}  */
1478     @HotSpotIntrinsicCandidate
1479     public native void    putIntVolatile(Object o, long offset, int x);
1480 
1481     /** Volatile version of {@link #getBoolean(Object, long)}  */
1482     @HotSpotIntrinsicCandidate
1483     public native boolean getBooleanVolatile(Object o, long offset);
1484 
1485     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
1486     @HotSpotIntrinsicCandidate
1487     public native void    putBooleanVolatile(Object o, long offset, boolean x);
1488 
1489     /** Volatile version of {@link #getByte(Object, long)}  */
1490     @HotSpotIntrinsicCandidate
1491     public native byte    getByteVolatile(Object o, long offset);
1492 
1493     /** Volatile version of {@link #putByte(Object, long, byte)}  */
1494     @HotSpotIntrinsicCandidate
1495     public native void    putByteVolatile(Object o, long offset, byte x);
1496 
1497     /** Volatile version of {@link #getShort(Object, long)}  */
1498     @HotSpotIntrinsicCandidate
1499     public native short   getShortVolatile(Object o, long offset);
1500 
1501     /** Volatile version of {@link #putShort(Object, long, short)}  */
1502     @HotSpotIntrinsicCandidate
1503     public native void    putShortVolatile(Object o, long offset, short x);
1504 
1505     /** Volatile version of {@link #getChar(Object, long)}  */
1506     @HotSpotIntrinsicCandidate
1507     public native char    getCharVolatile(Object o, long offset);
1508 
1509     /** Volatile version of {@link #putChar(Object, long, char)}  */
1510     @HotSpotIntrinsicCandidate
1511     public native void    putCharVolatile(Object o, long offset, char x);
1512 
1513     /** Volatile version of {@link #getLong(Object, long)}  */
1514     @HotSpotIntrinsicCandidate
1515     public native long    getLongVolatile(Object o, long offset);
1516 
1517     /** Volatile version of {@link #putLong(Object, long, long)}  */
1518     @HotSpotIntrinsicCandidate
1519     public native void    putLongVolatile(Object o, long offset, long x);
1520 
1521     /** Volatile version of {@link #getFloat(Object, long)}  */
1522     @HotSpotIntrinsicCandidate
1523     public native float   getFloatVolatile(Object o, long offset);
1524 
1525     /** Volatile version of {@link #putFloat(Object, long, float)}  */
1526     @HotSpotIntrinsicCandidate
1527     public native void    putFloatVolatile(Object o, long offset, float x);
1528 
1529     /** Volatile version of {@link #getDouble(Object, long)}  */
1530     @HotSpotIntrinsicCandidate
1531     public native double  getDoubleVolatile(Object o, long offset);
1532 
1533     /** Volatile version of {@link #putDouble(Object, long, double)}  */
1534     @HotSpotIntrinsicCandidate
1535     public native void    putDoubleVolatile(Object o, long offset, double x);
1536 
1537 
1538 
1539     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
1540     @HotSpotIntrinsicCandidate
1541     public final Object getObjectAcquire(Object o, long offset) {
1542         return getObjectVolatile(o, offset);
1543     }
1544 
1545     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
1546     @HotSpotIntrinsicCandidate
1547     public final boolean getBooleanAcquire(Object o, long offset) {
1548         return getBooleanVolatile(o, offset);
1549     }
1550 
1551     /** Acquire version of {@link #getByteVolatile(Object, long)} */
1552     @HotSpotIntrinsicCandidate
1553     public final byte getByteAcquire(Object o, long offset) {
1554         return getByteVolatile(o, offset);
1555     }
1556 
1557     /** Acquire version of {@link #getShortVolatile(Object, long)} */
1558     @HotSpotIntrinsicCandidate
1559     public final short getShortAcquire(Object o, long offset) {
1560         return getShortVolatile(o, offset);
1561     }
1562 
1563     /** Acquire version of {@link #getCharVolatile(Object, long)} */
1564     @HotSpotIntrinsicCandidate
1565     public final char getCharAcquire(Object o, long offset) {
1566         return getCharVolatile(o, offset);
1567     }
1568 
1569     /** Acquire version of {@link #getIntVolatile(Object, long)} */
1570     @HotSpotIntrinsicCandidate
1571     public final int getIntAcquire(Object o, long offset) {
1572         return getIntVolatile(o, offset);
1573     }
1574 
1575     /** Acquire version of {@link #getFloatVolatile(Object, long)} */
1576     @HotSpotIntrinsicCandidate
1577     public final float getFloatAcquire(Object o, long offset) {
1578         return getFloatVolatile(o, offset);
1579     }
1580 
1581     /** Acquire version of {@link #getLongVolatile(Object, long)} */
1582     @HotSpotIntrinsicCandidate
1583     public final long getLongAcquire(Object o, long offset) {
1584         return getLongVolatile(o, offset);
1585     }
1586 
1587     /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
1588     @HotSpotIntrinsicCandidate
1589     public final double getDoubleAcquire(Object o, long offset) {
1590         return getDoubleVolatile(o, offset);
1591     }
1592 
1593     /*
1594       * Versions of {@link #putObjectVolatile(Object, long, Object)}
1595       * that do not guarantee immediate visibility of the store to
1596       * other threads. This method is generally only useful if the
1597       * underlying field is a Java volatile (or if an array cell, one
1598       * that is otherwise only accessed using volatile accesses).
1599       *
1600       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
1601       */
1602 
1603     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
1604     @HotSpotIntrinsicCandidate
1605     public final void putObjectRelease(Object o, long offset, Object x) {
1606         putObjectVolatile(o, offset, x);
1607     }
1608 
1609     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
1610     @HotSpotIntrinsicCandidate
1611     public final void putBooleanRelease(Object o, long offset, boolean x) {
1612         putBooleanVolatile(o, offset, x);
1613     }
1614 
1615     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
1616     @HotSpotIntrinsicCandidate
1617     public final void putByteRelease(Object o, long offset, byte x) {
1618         putByteVolatile(o, offset, x);
1619     }
1620 
1621     /** Release version of {@link #putShortVolatile(Object, long, short)} */
1622     @HotSpotIntrinsicCandidate
1623     public final void putShortRelease(Object o, long offset, short x) {
1624         putShortVolatile(o, offset, x);
1625     }
1626 
1627     /** Release version of {@link #putCharVolatile(Object, long, char)} */
1628     @HotSpotIntrinsicCandidate
1629     public final void putCharRelease(Object o, long offset, char x) {
1630         putCharVolatile(o, offset, x);
1631     }
1632 
1633     /** Release version of {@link #putIntVolatile(Object, long, int)} */
1634     @HotSpotIntrinsicCandidate
1635     public final void putIntRelease(Object o, long offset, int x) {
1636         putIntVolatile(o, offset, x);
1637     }
1638 
1639     /** Release version of {@link #putFloatVolatile(Object, long, float)} */
1640     @HotSpotIntrinsicCandidate
1641     public final void putFloatRelease(Object o, long offset, float x) {
1642         putFloatVolatile(o, offset, x);
1643     }
1644 
1645     /** Release version of {@link #putLongVolatile(Object, long, long)} */
1646     @HotSpotIntrinsicCandidate
1647     public final void putLongRelease(Object o, long offset, long x) {
1648         putLongVolatile(o, offset, x);
1649     }
1650 
1651     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
1652     @HotSpotIntrinsicCandidate
1653     public final void putDoubleRelease(Object o, long offset, double x) {
1654         putDoubleVolatile(o, offset, x);
1655     }
1656 
1657     // ------------------------------ Opaque --------------------------------------
1658 
1659     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
1660     @HotSpotIntrinsicCandidate
1661     public final Object getObjectOpaque(Object o, long offset) {
1662         return getObjectVolatile(o, offset);
1663     }
1664 
1665     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
1666     @HotSpotIntrinsicCandidate
1667     public final boolean getBooleanOpaque(Object o, long offset) {
1668         return getBooleanVolatile(o, offset);
1669     }
1670 
1671     /** Opaque version of {@link #getByteVolatile(Object, long)} */
1672     @HotSpotIntrinsicCandidate
1673     public final byte getByteOpaque(Object o, long offset) {
1674         return getByteVolatile(o, offset);
1675     }
1676 
1677     /** Opaque version of {@link #getShortVolatile(Object, long)} */
1678     @HotSpotIntrinsicCandidate
1679     public final short getShortOpaque(Object o, long offset) {
1680         return getShortVolatile(o, offset);
1681     }
1682 
1683     /** Opaque version of {@link #getCharVolatile(Object, long)} */
1684     @HotSpotIntrinsicCandidate
1685     public final char getCharOpaque(Object o, long offset) {
1686         return getCharVolatile(o, offset);
1687     }
1688 
1689     /** Opaque version of {@link #getIntVolatile(Object, long)} */
1690     @HotSpotIntrinsicCandidate
1691     public final int getIntOpaque(Object o, long offset) {
1692         return getIntVolatile(o, offset);
1693     }
1694 
1695     /** Opaque version of {@link #getFloatVolatile(Object, long)} */
1696     @HotSpotIntrinsicCandidate
1697     public final float getFloatOpaque(Object o, long offset) {
1698         return getFloatVolatile(o, offset);
1699     }
1700 
1701     /** Opaque version of {@link #getLongVolatile(Object, long)} */
1702     @HotSpotIntrinsicCandidate
1703     public final long getLongOpaque(Object o, long offset) {
1704         return getLongVolatile(o, offset);
1705     }
1706 
1707     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
1708     @HotSpotIntrinsicCandidate
1709     public final double getDoubleOpaque(Object o, long offset) {
1710         return getDoubleVolatile(o, offset);
1711     }
1712 
1713     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
1714     @HotSpotIntrinsicCandidate
1715     public final void putObjectOpaque(Object o, long offset, Object x) {
1716         putObjectVolatile(o, offset, x);
1717     }
1718 
1719     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
1720     @HotSpotIntrinsicCandidate
1721     public final void putBooleanOpaque(Object o, long offset, boolean x) {
1722         putBooleanVolatile(o, offset, x);
1723     }
1724 
1725     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
1726     @HotSpotIntrinsicCandidate
1727     public final void putByteOpaque(Object o, long offset, byte x) {
1728         putByteVolatile(o, offset, x);
1729     }
1730 
1731     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
1732     @HotSpotIntrinsicCandidate
1733     public final void putShortOpaque(Object o, long offset, short x) {
1734         putShortVolatile(o, offset, x);
1735     }
1736 
1737     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
1738     @HotSpotIntrinsicCandidate
1739     public final void putCharOpaque(Object o, long offset, char x) {
1740         putCharVolatile(o, offset, x);
1741     }
1742 
1743     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
1744     @HotSpotIntrinsicCandidate
1745     public final void putIntOpaque(Object o, long offset, int x) {
1746         putIntVolatile(o, offset, x);
1747     }
1748 
1749     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
1750     @HotSpotIntrinsicCandidate
1751     public final void putFloatOpaque(Object o, long offset, float x) {
1752         putFloatVolatile(o, offset, x);
1753     }
1754 
1755     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
1756     @HotSpotIntrinsicCandidate
1757     public final void putLongOpaque(Object o, long offset, long x) {
1758         putLongVolatile(o, offset, x);
1759     }
1760 
1761     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
1762     @HotSpotIntrinsicCandidate
1763     public final void putDoubleOpaque(Object o, long offset, double x) {
1764         putDoubleVolatile(o, offset, x);
1765     }
1766 
1767     /**
1768      * Unblocks the given thread blocked on {@code park}, or, if it is
1769      * not blocked, causes the subsequent call to {@code park} not to
1770      * block.  Note: this operation is "unsafe" solely because the
1771      * caller must somehow ensure that the thread has not been
1772      * destroyed. Nothing special is usually required to ensure this
1773      * when called from Java (in which there will ordinarily be a live
1774      * reference to the thread) but this is not nearly-automatically
1775      * so when calling from native code.
1776      *
1777      * @param thread the thread to unpark.
1778      */
1779     @HotSpotIntrinsicCandidate
1780     public native void unpark(Object thread);
1781 
1782     /**
1783      * Blocks current thread, returning when a balancing
1784      * {@code unpark} occurs, or a balancing {@code unpark} has
1785      * already occurred, or the thread is interrupted, or, if not
1786      * absolute and time is not zero, the given time nanoseconds have
1787      * elapsed, or if absolute, the given deadline in milliseconds
1788      * since Epoch has passed, or spuriously (i.e., returning for no
1789      * "reason"). Note: This operation is in the Unsafe class only
1790      * because {@code unpark} is, so it would be strange to place it
1791      * elsewhere.
1792      */
1793     @HotSpotIntrinsicCandidate
1794     public native void park(boolean isAbsolute, long time);
1795 
1796     /**
1797      * Gets the load average in the system run queue assigned
1798      * to the available processors averaged over various periods of time.
1799      * This method retrieves the given {@code nelem} samples and
1800      * assigns to the elements of the given {@code loadavg} array.
1801      * The system imposes a maximum of 3 samples, representing
1802      * averages over the last 1,  5,  and  15 minutes, respectively.
1803      *
1804      * @param loadavg an array of double of size nelems
1805      * @param nelems the number of samples to be retrieved and
1806      *        must be 1 to 3.
1807      *
1808      * @return the number of samples actually retrieved; or -1
1809      *         if the load average is unobtainable.
1810      */
1811     public int getLoadAverage(double[] loadavg, int nelems) {
1812         if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
1813             throw new ArrayIndexOutOfBoundsException();
1814         }
1815 
1816         return getLoadAverage0(loadavg, nelems);
1817     }
1818 
1819     // The following contain CAS-based Java implementations used on
1820     // platforms not supporting native instructions
1821 
1822     /**
1823      * Atomically adds the given value to the current value of a field
1824      * or array element within the given object {@code o}
1825      * at the given {@code offset}.
1826      *
1827      * @param o object/array to update the field/element in
1828      * @param offset field/element offset
1829      * @param delta the value to add
1830      * @return the previous value
1831      * @since 1.8
1832      */
1833     @HotSpotIntrinsicCandidate
1834     public final int getAndAddInt(Object o, long offset, int delta) {
1835         int v;
1836         do {
1837             v = getIntVolatile(o, offset);
1838         } while (!compareAndSwapInt(o, offset, v, v + delta));
1839         return v;
1840     }
1841 
1842     /**
1843      * Atomically adds the given value to the current value of a field
1844      * or array element within the given object {@code o}
1845      * at the given {@code offset}.
1846      *
1847      * @param o object/array to update the field/element in
1848      * @param offset field/element offset
1849      * @param delta the value to add
1850      * @return the previous value
1851      * @since 1.8
1852      */
1853     @HotSpotIntrinsicCandidate
1854     public final long getAndAddLong(Object o, long offset, long delta) {
1855         long v;
1856         do {
1857             v = getLongVolatile(o, offset);
1858         } while (!compareAndSwapLong(o, offset, v, v + delta));
1859         return v;
1860     }
1861 
1862     /**
1863      * Atomically exchanges the given value with the current value of
1864      * a field or array element within the given object {@code o}
1865      * at the given {@code offset}.
1866      *
1867      * @param o object/array to update the field/element in
1868      * @param offset field/element offset
1869      * @param newValue new value
1870      * @return the previous value
1871      * @since 1.8
1872      */
1873     @HotSpotIntrinsicCandidate
1874     public final int getAndSetInt(Object o, long offset, int newValue) {
1875         int v;
1876         do {
1877             v = getIntVolatile(o, offset);
1878         } while (!compareAndSwapInt(o, offset, v, newValue));
1879         return v;
1880     }
1881 
1882     /**
1883      * Atomically exchanges the given value with the current value of
1884      * a field or array element within the given object {@code o}
1885      * at the given {@code offset}.
1886      *
1887      * @param o object/array to update the field/element in
1888      * @param offset field/element offset
1889      * @param newValue new value
1890      * @return the previous value
1891      * @since 1.8
1892      */
1893     @HotSpotIntrinsicCandidate
1894     public final long getAndSetLong(Object o, long offset, long newValue) {
1895         long v;
1896         do {
1897             v = getLongVolatile(o, offset);
1898         } while (!compareAndSwapLong(o, offset, v, newValue));
1899         return v;
1900     }
1901 
1902     /**
1903      * Atomically exchanges the given reference value with the current
1904      * reference value of a field or array element within the given
1905      * object {@code o} at the given {@code offset}.
1906      *
1907      * @param o object/array to update the field/element in
1908      * @param offset field/element offset
1909      * @param newValue new value
1910      * @return the previous value
1911      * @since 1.8
1912      */
1913     @HotSpotIntrinsicCandidate
1914     public final Object getAndSetObject(Object o, long offset, Object newValue) {
1915         Object v;
1916         do {
1917             v = getObjectVolatile(o, offset);
1918         } while (!compareAndSwapObject(o, offset, v, newValue));
1919         return v;
1920     }
1921 
1922 
1923     /**
1924      * Ensures that loads before the fence will not be reordered with loads and
1925      * stores after the fence; a "LoadLoad plus LoadStore barrier".
1926      *
1927      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
1928      * (an "acquire fence").
1929      *
1930      * A pure LoadLoad fence is not provided, since the addition of LoadStore
1931      * is almost always desired, and most current hardware instructions that
1932      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
1933      * @since 1.8
1934      */
1935     @HotSpotIntrinsicCandidate
1936     public native void loadFence();
1937 
1938     /**
1939      * Ensures that loads and stores before the fence will not be reordered with
1940      * stores after the fence; a "StoreStore plus LoadStore barrier".
1941      *
1942      * Corresponds to C11 atomic_thread_fence(memory_order_release)
1943      * (a "release fence").
1944      *
1945      * A pure StoreStore fence is not provided, since the addition of LoadStore
1946      * is almost always desired, and most current hardware instructions that
1947      * provide a StoreStore barrier also provide a LoadStore barrier for free.
1948      * @since 1.8
1949      */
1950     @HotSpotIntrinsicCandidate
1951     public native void storeFence();
1952 
1953     /**
1954      * Ensures that loads and stores before the fence will not be reordered
1955      * with loads and stores after the fence.  Implies the effects of both
1956      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
1957      * barrier.
1958      *
1959      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
1960      * @since 1.8
1961      */
1962     @HotSpotIntrinsicCandidate
1963     public native void fullFence();
1964 
1965     /**
1966      * Ensures that loads before the fence will not be reordered with
1967      * loads after the fence.
1968      */
1969     public final void loadLoadFence() {
1970         loadFence();
1971     }
1972 
1973     /**
1974      * Ensures that stores before the fence will not be reordered with
1975      * stores after the fence.
1976      */
1977     public final void storeStoreFence() {
1978         storeFence();
1979     }
1980 
1981 
1982     /**
1983      * Throws IllegalAccessError; for use by the VM for access control
1984      * error support.
1985      * @since 1.8
1986      */
1987     private static void throwIllegalAccessError() {
1988         throw new IllegalAccessError();
1989     }
1990 
1991     /**
1992      * @return Returns true if the native byte ordering of this
1993      * platform is big-endian, false if it is little-endian.
1994      */
1995     public final boolean isBigEndian() { return BE; }
1996 
1997     /**
1998      * @return Returns true if this platform is capable of performing
1999      * accesses at addresses which are not aligned for the type of the
2000      * primitive type being accessed, false otherwise.
2001      */
2002     public final boolean unalignedAccess() { return unalignedAccess; }
2003 
2004     /**
2005      * Fetches a value at some byte offset into a given Java object.
2006      * More specifically, fetches a value within the given object
2007      * <code>o</code> at the given offset, or (if <code>o</code> is
2008      * null) from the memory address whose numerical value is the
2009      * given offset.  <p>
2010      *
2011      * The specification of this method is the same as {@link
2012      * #getLong(Object, long)} except that the offset does not need to
2013      * have been obtained from {@link #objectFieldOffset} on the
2014      * {@link java.lang.reflect.Field} of some Java field.  The value
2015      * in memory is raw data, and need not correspond to any Java
2016      * variable.  Unless <code>o</code> is null, the value accessed
2017      * must be entirely within the allocated object.  The endianness
2018      * of the value in memory is the endianness of the native platform.
2019      *
2020      * <p> The read will be atomic with respect to the largest power
2021      * of two that divides the GCD of the offset and the storage size.
2022      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
2023      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
2024      * respectively.  There are no other guarantees of atomicity.
2025      * <p>
2026      * 8-byte atomicity is only guaranteed on platforms on which
2027      * support atomic accesses to longs.
2028      *
2029      * @param o Java heap object in which the value resides, if any, else
2030      *        null
2031      * @param offset The offset in bytes from the start of the object
2032      * @return the value fetched from the indicated object
2033      * @throws RuntimeException No defined exceptions are thrown, not even
2034      *         {@link NullPointerException}
2035      * @since 9
2036      */
2037     @HotSpotIntrinsicCandidate
2038     public final long getLongUnaligned(Object o, long offset) {
2039         if ((offset & 7) == 0) {
2040             return getLong(o, offset);
2041         } else if ((offset & 3) == 0) {
2042             return makeLong(getInt(o, offset),
2043                             getInt(o, offset + 4));
2044         } else if ((offset & 1) == 0) {
2045             return makeLong(getShort(o, offset),
2046                             getShort(o, offset + 2),
2047                             getShort(o, offset + 4),
2048                             getShort(o, offset + 6));
2049         } else {
2050             return makeLong(getByte(o, offset),
2051                             getByte(o, offset + 1),
2052                             getByte(o, offset + 2),
2053                             getByte(o, offset + 3),
2054                             getByte(o, offset + 4),
2055                             getByte(o, offset + 5),
2056                             getByte(o, offset + 6),
2057                             getByte(o, offset + 7));
2058         }
2059     }
2060     /**
2061      * As {@link #getLongUnaligned(Object, long)} but with an
2062      * additional argument which specifies the endianness of the value
2063      * as stored in memory.
2064      *
2065      * @param o Java heap object in which the variable resides
2066      * @param offset The offset in bytes from the start of the object
2067      * @param bigEndian The endianness of the value
2068      * @return the value fetched from the indicated object
2069      * @since 9
2070      */
2071     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
2072         return convEndian(bigEndian, getLongUnaligned(o, offset));
2073     }
2074 
2075     /** @see #getLongUnaligned(Object, long) */
2076     @HotSpotIntrinsicCandidate
2077     public final int getIntUnaligned(Object o, long offset) {
2078         if ((offset & 3) == 0) {
2079             return getInt(o, offset);
2080         } else if ((offset & 1) == 0) {
2081             return makeInt(getShort(o, offset),
2082                            getShort(o, offset + 2));
2083         } else {
2084             return makeInt(getByte(o, offset),
2085                            getByte(o, offset + 1),
2086                            getByte(o, offset + 2),
2087                            getByte(o, offset + 3));
2088         }
2089     }
2090     /** @see #getLongUnaligned(Object, long, boolean) */
2091     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
2092         return convEndian(bigEndian, getIntUnaligned(o, offset));
2093     }
2094 
2095     /** @see #getLongUnaligned(Object, long) */
2096     @HotSpotIntrinsicCandidate
2097     public final short getShortUnaligned(Object o, long offset) {
2098         if ((offset & 1) == 0) {
2099             return getShort(o, offset);
2100         } else {
2101             return makeShort(getByte(o, offset),
2102                              getByte(o, offset + 1));
2103         }
2104     }
2105     /** @see #getLongUnaligned(Object, long, boolean) */
2106     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
2107         return convEndian(bigEndian, getShortUnaligned(o, offset));
2108     }
2109 
2110     /** @see #getLongUnaligned(Object, long) */
2111     @HotSpotIntrinsicCandidate
2112     public final char getCharUnaligned(Object o, long offset) {
2113         if ((offset & 1) == 0) {
2114             return getChar(o, offset);
2115         } else {
2116             return (char)makeShort(getByte(o, offset),
2117                                    getByte(o, offset + 1));
2118         }
2119     }
2120 
2121     /** @see #getLongUnaligned(Object, long, boolean) */
2122     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
2123         return convEndian(bigEndian, getCharUnaligned(o, offset));
2124     }
2125 
2126     /**
2127      * Stores a value at some byte offset into a given Java object.
2128      * <p>
2129      * The specification of this method is the same as {@link
2130      * #getLong(Object, long)} except that the offset does not need to
2131      * have been obtained from {@link #objectFieldOffset} on the
2132      * {@link java.lang.reflect.Field} of some Java field.  The value
2133      * in memory is raw data, and need not correspond to any Java
2134      * variable.  The endianness of the value in memory is the
2135      * endianness of the native platform.
2136      * <p>
2137      * The write will be atomic with respect to the largest power of
2138      * two that divides the GCD of the offset and the storage size.
2139      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
2140      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
2141      * respectively.  There are no other guarantees of atomicity.
2142      * <p>
2143      * 8-byte atomicity is only guaranteed on platforms on which
2144      * support atomic accesses to longs.
2145      *
2146      * @param o Java heap object in which the value resides, if any, else
2147      *        null
2148      * @param offset The offset in bytes from the start of the object
2149      * @param x the value to store
2150      * @throws RuntimeException No defined exceptions are thrown, not even
2151      *         {@link NullPointerException}
2152      * @since 9
2153      */
2154     @HotSpotIntrinsicCandidate
2155     public final void putLongUnaligned(Object o, long offset, long x) {
2156         if ((offset & 7) == 0) {
2157             putLong(o, offset, x);
2158         } else if ((offset & 3) == 0) {
2159             putLongParts(o, offset,
2160                          (int)(x >> 0),
2161                          (int)(x >>> 32));
2162         } else if ((offset & 1) == 0) {
2163             putLongParts(o, offset,
2164                          (short)(x >>> 0),
2165                          (short)(x >>> 16),
2166                          (short)(x >>> 32),
2167                          (short)(x >>> 48));
2168         } else {
2169             putLongParts(o, offset,
2170                          (byte)(x >>> 0),
2171                          (byte)(x >>> 8),
2172                          (byte)(x >>> 16),
2173                          (byte)(x >>> 24),
2174                          (byte)(x >>> 32),
2175                          (byte)(x >>> 40),
2176                          (byte)(x >>> 48),
2177                          (byte)(x >>> 56));
2178         }
2179     }
2180 
2181     /**
2182      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
2183      * argument which specifies the endianness of the value as stored in memory.
2184      * @param o Java heap object in which the value resides
2185      * @param offset The offset in bytes from the start of the object
2186      * @param x the value to store
2187      * @param bigEndian The endianness of the value
2188      * @throws RuntimeException No defined exceptions are thrown, not even
2189      *         {@link NullPointerException}
2190      * @since 9
2191      */
2192     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
2193         putLongUnaligned(o, offset, convEndian(bigEndian, x));
2194     }
2195 
2196     /** @see #putLongUnaligned(Object, long, long) */
2197     @HotSpotIntrinsicCandidate
2198     public final void putIntUnaligned(Object o, long offset, int x) {
2199         if ((offset & 3) == 0) {
2200             putInt(o, offset, x);
2201         } else if ((offset & 1) == 0) {
2202             putIntParts(o, offset,
2203                         (short)(x >> 0),
2204                         (short)(x >>> 16));
2205         } else {
2206             putIntParts(o, offset,
2207                         (byte)(x >>> 0),
2208                         (byte)(x >>> 8),
2209                         (byte)(x >>> 16),
2210                         (byte)(x >>> 24));
2211         }
2212     }
2213     /** @see #putLongUnaligned(Object, long, long, boolean) */
2214     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
2215         putIntUnaligned(o, offset, convEndian(bigEndian, x));
2216     }
2217 
2218     /** @see #putLongUnaligned(Object, long, long) */
2219     @HotSpotIntrinsicCandidate
2220     public final void putShortUnaligned(Object o, long offset, short x) {
2221         if ((offset & 1) == 0) {
2222             putShort(o, offset, x);
2223         } else {
2224             putShortParts(o, offset,
2225                           (byte)(x >>> 0),
2226                           (byte)(x >>> 8));
2227         }
2228     }
2229     /** @see #putLongUnaligned(Object, long, long, boolean) */
2230     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
2231         putShortUnaligned(o, offset, convEndian(bigEndian, x));
2232     }
2233 
2234     /** @see #putLongUnaligned(Object, long, long) */
2235     @HotSpotIntrinsicCandidate
2236     public final void putCharUnaligned(Object o, long offset, char x) {
2237         putShortUnaligned(o, offset, (short)x);
2238     }
2239     /** @see #putLongUnaligned(Object, long, long, boolean) */
2240     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
2241         putCharUnaligned(o, offset, convEndian(bigEndian, x));
2242     }
2243 
2244     // JVM interface methods
2245     // BE is true iff the native endianness of this platform is big.
2246     private static final boolean BE = theUnsafe.isBigEndian0();
2247 
2248     // unalignedAccess is true iff this platform can perform unaligned accesses.
2249     private static final boolean unalignedAccess = theUnsafe.unalignedAccess0();
2250 
2251     private static int pickPos(int top, int pos) { return BE ? top - pos : pos; }
2252 
2253     // These methods construct integers from bytes.  The byte ordering
2254     // is the native endianness of this platform.
2255     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
2256         return ((toUnsignedLong(i0) << pickPos(56, 0))
2257               | (toUnsignedLong(i1) << pickPos(56, 8))
2258               | (toUnsignedLong(i2) << pickPos(56, 16))
2259               | (toUnsignedLong(i3) << pickPos(56, 24))
2260               | (toUnsignedLong(i4) << pickPos(56, 32))
2261               | (toUnsignedLong(i5) << pickPos(56, 40))
2262               | (toUnsignedLong(i6) << pickPos(56, 48))
2263               | (toUnsignedLong(i7) << pickPos(56, 56)));
2264     }
2265     private static long makeLong(short i0, short i1, short i2, short i3) {
2266         return ((toUnsignedLong(i0) << pickPos(48, 0))
2267               | (toUnsignedLong(i1) << pickPos(48, 16))
2268               | (toUnsignedLong(i2) << pickPos(48, 32))
2269               | (toUnsignedLong(i3) << pickPos(48, 48)));
2270     }
2271     private static long makeLong(int i0, int i1) {
2272         return (toUnsignedLong(i0) << pickPos(32, 0))
2273              | (toUnsignedLong(i1) << pickPos(32, 32));
2274     }
2275     private static int makeInt(short i0, short i1) {
2276         return (toUnsignedInt(i0) << pickPos(16, 0))
2277              | (toUnsignedInt(i1) << pickPos(16, 16));
2278     }
2279     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
2280         return ((toUnsignedInt(i0) << pickPos(24, 0))
2281               | (toUnsignedInt(i1) << pickPos(24, 8))
2282               | (toUnsignedInt(i2) << pickPos(24, 16))
2283               | (toUnsignedInt(i3) << pickPos(24, 24)));
2284     }
2285     private static short makeShort(byte i0, byte i1) {
2286         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
2287                      | (toUnsignedInt(i1) << pickPos(8, 8)));
2288     }
2289 
2290     private static byte  pick(byte  le, byte  be) { return BE ? be : le; }
2291     private static short pick(short le, short be) { return BE ? be : le; }
2292     private static int   pick(int   le, int   be) { return BE ? be : le; }
2293 
2294     // These methods write integers to memory from smaller parts
2295     // provided by their caller.  The ordering in which these parts
2296     // are written is the native endianness of this platform.
2297     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
2298         putByte(o, offset + 0, pick(i0, i7));
2299         putByte(o, offset + 1, pick(i1, i6));
2300         putByte(o, offset + 2, pick(i2, i5));
2301         putByte(o, offset + 3, pick(i3, i4));
2302         putByte(o, offset + 4, pick(i4, i3));
2303         putByte(o, offset + 5, pick(i5, i2));
2304         putByte(o, offset + 6, pick(i6, i1));
2305         putByte(o, offset + 7, pick(i7, i0));
2306     }
2307     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
2308         putShort(o, offset + 0, pick(i0, i3));
2309         putShort(o, offset + 2, pick(i1, i2));
2310         putShort(o, offset + 4, pick(i2, i1));
2311         putShort(o, offset + 6, pick(i3, i0));
2312     }
2313     private void putLongParts(Object o, long offset, int i0, int i1) {
2314         putInt(o, offset + 0, pick(i0, i1));
2315         putInt(o, offset + 4, pick(i1, i0));
2316     }
2317     private void putIntParts(Object o, long offset, short i0, short i1) {
2318         putShort(o, offset + 0, pick(i0, i1));
2319         putShort(o, offset + 2, pick(i1, i0));
2320     }
2321     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
2322         putByte(o, offset + 0, pick(i0, i3));
2323         putByte(o, offset + 1, pick(i1, i2));
2324         putByte(o, offset + 2, pick(i2, i1));
2325         putByte(o, offset + 3, pick(i3, i0));
2326     }
2327     private void putShortParts(Object o, long offset, byte i0, byte i1) {
2328         putByte(o, offset + 0, pick(i0, i1));
2329         putByte(o, offset + 1, pick(i1, i0));
2330     }
2331 
2332     // Zero-extend an integer
2333     private static int toUnsignedInt(byte n)    { return n & 0xff; }
2334     private static int toUnsignedInt(short n)   { return n & 0xffff; }
2335     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
2336     private static long toUnsignedLong(short n) { return n & 0xffffl; }
2337     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
2338 
2339     // Maybe byte-reverse an integer
2340     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
2341     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
2342     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
2343     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
2344 
2345 
2346 
2347     private native long allocateMemory0(long bytes);
2348     private native long reallocateMemory0(long address, long bytes);
2349     private native void freeMemory0(long address);
2350     private native void setMemory0(Object o, long offset, long bytes, byte value);
2351     @HotSpotIntrinsicCandidate
2352     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
2353     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
2354     private native long objectFieldOffset0(Field f);
2355     private native long staticFieldOffset0(Field f);
2356     private native Object staticFieldBase0(Field f);
2357     private native boolean shouldBeInitialized0(Class<?> c);
2358     private native void ensureClassInitialized0(Class<?> c);
2359     private native int arrayBaseOffset0(Class<?> arrayClass);
2360     private native int arrayIndexScale0(Class<?> arrayClass);
2361     private native int addressSize0();
2362     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
2363     private native int getLoadAverage0(double[] loadavg, int nelems);
2364     private native boolean unalignedAccess0();
2365     private native boolean isBigEndian0();
2366 }