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