< prev index next >

src/jdk.unsupported/share/classes/sun/misc/Unsafe.java

Print this page




   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 sun.misc;
  27 

  28 import jdk.internal.vm.annotation.ForceInline;
  29 import jdk.internal.misc.VM;
  30 import jdk.internal.ref.Cleaner;
  31 import jdk.internal.reflect.CallerSensitive;
  32 import jdk.internal.reflect.Reflection;
  33 import sun.nio.ch.DirectBuffer;
  34 
  35 import java.lang.reflect.Field;
  36 import java.util.Set;
  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     static {
  60         Reflection.registerMethodsToFilter(Unsafe.class, Set.of("getUnsafe"));
  61     }
  62 
  63     private Unsafe() {}
  64 
  65     private static final Unsafe theUnsafe = new Unsafe();
  66     private static final jdk.internal.misc.Unsafe theInternalUnsafe = jdk.internal.misc.Unsafe.getUnsafe();
  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 the class loader of the caller
  93      *          class is not in the system domain in which all permissions
  94      *          are granted.
  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


 599      * compiler, some or all checks (if any) may be elided. Hence, the
 600      * caller must not rely on the checks and corresponding
 601      * exceptions!
 602      *
 603      * @throws RuntimeException if any of the arguments is invalid
 604      *
 605      * @see #allocateMemory
 606      */
 607     @ForceInline
 608     public void freeMemory(long address) {
 609         theInternalUnsafe.freeMemory(address);
 610     }
 611 
 612     /// random queries
 613 
 614     /**
 615      * This constant differs from all results that will ever be returned from
 616      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 617      * or {@link #arrayBaseOffset}.
 618      */
 619     public static final int INVALID_FIELD_OFFSET = jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
 620 
 621     /**
 622      * Reports the location of a given field in the storage allocation of its
 623      * class.  Do not expect to perform any sort of arithmetic on this offset;
 624      * it is just a cookie which is passed to the unsafe heap memory accessors.
 625      *
 626      * <p>Any given field will always have the same offset and base, and no
 627      * two distinct fields of the same class will ever have the same offset
 628      * and base.
 629      *
 630      * <p>As of 1.4.1, offsets for fields are represented as long values,
 631      * although the Sun JVM does not use the most significant 32 bits.
 632      * However, JVM implementations which store static fields at absolute
 633      * addresses can use long offsets and null base pointers to express
 634      * the field locations in a form usable by {@link #getInt(Object,long)}.
 635      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 636      * must preserve all bits of static field offsets.
 637      * @see #getInt(Object, long)
 638      */
 639     @ForceInline


 698     public void ensureClassInitialized(Class<?> c) {
 699         theInternalUnsafe.ensureClassInitialized(c);
 700     }
 701 
 702     /**
 703      * Reports the offset of the first element in the storage allocation of a
 704      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 705      * for the same class, you may use that scale factor, together with this
 706      * base offset, to form new offsets to access elements of arrays of the
 707      * given class.
 708      *
 709      * @see #getInt(Object, long)
 710      * @see #putInt(Object, long, int)
 711      */
 712     @ForceInline
 713     public int arrayBaseOffset(Class<?> arrayClass) {
 714         return theInternalUnsafe.arrayBaseOffset(arrayClass);
 715     }
 716 
 717     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 718     public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
 719 
 720     /** The value of {@code arrayBaseOffset(byte[].class)} */
 721     public static final int ARRAY_BYTE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
 722 
 723     /** The value of {@code arrayBaseOffset(short[].class)} */
 724     public static final int ARRAY_SHORT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET;
 725 
 726     /** The value of {@code arrayBaseOffset(char[].class)} */
 727     public static final int ARRAY_CHAR_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_CHAR_BASE_OFFSET;
 728 
 729     /** The value of {@code arrayBaseOffset(int[].class)} */
 730     public static final int ARRAY_INT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_INT_BASE_OFFSET;
 731 
 732     /** The value of {@code arrayBaseOffset(long[].class)} */
 733     public static final int ARRAY_LONG_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_LONG_BASE_OFFSET;
 734 
 735     /** The value of {@code arrayBaseOffset(float[].class)} */
 736     public static final int ARRAY_FLOAT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_FLOAT_BASE_OFFSET;
 737 
 738     /** The value of {@code arrayBaseOffset(double[].class)} */
 739     public static final int ARRAY_DOUBLE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
 740 
 741     /** The value of {@code arrayBaseOffset(Object[].class)} */
 742     public static final int ARRAY_OBJECT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET;
 743 
 744     /**
 745      * Reports the scale factor for addressing elements in the storage
 746      * allocation of a given array class.  However, arrays of "narrow" types
 747      * will generally not work properly with accessors like {@link
 748      * #getByte(Object, long)}, so the scale factor for such classes is reported
 749      * as zero.
 750      *
 751      * @see #arrayBaseOffset
 752      * @see #getInt(Object, long)
 753      * @see #putInt(Object, long, int)
 754      */
 755     @ForceInline
 756     public int arrayIndexScale(Class<?> arrayClass) {
 757         return theInternalUnsafe.arrayIndexScale(arrayClass);
 758     }
 759 
 760     /** The value of {@code arrayIndexScale(boolean[].class)} */
 761     public static final int ARRAY_BOOLEAN_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
 762 
 763     /** The value of {@code arrayIndexScale(byte[].class)} */
 764     public static final int ARRAY_BYTE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE;
 765 
 766     /** The value of {@code arrayIndexScale(short[].class)} */
 767     public static final int ARRAY_SHORT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
 768 
 769     /** The value of {@code arrayIndexScale(char[].class)} */
 770     public static final int ARRAY_CHAR_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_CHAR_INDEX_SCALE;
 771 
 772     /** The value of {@code arrayIndexScale(int[].class)} */
 773     public static final int ARRAY_INT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
 774 
 775     /** The value of {@code arrayIndexScale(long[].class)} */
 776     public static final int ARRAY_LONG_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
 777 
 778     /** The value of {@code arrayIndexScale(float[].class)} */
 779     public static final int ARRAY_FLOAT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
 780 
 781     /** The value of {@code arrayIndexScale(double[].class)} */
 782     public static final int ARRAY_DOUBLE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
 783 
 784     /** The value of {@code arrayIndexScale(Object[].class)} */
 785     public static final int ARRAY_OBJECT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
 786 
 787     /**
 788      * Reports the size in bytes of a native pointer, as stored via {@link
 789      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
 790      * other primitive types (as stored in native memory blocks) is determined
 791      * fully by their information content.
 792      */
 793     @ForceInline
 794     public int addressSize() {
 795         return theInternalUnsafe.addressSize();
 796     }
 797 
 798     /** The value of {@code addressSize()} */
 799     public static final int ADDRESS_SIZE = theInternalUnsafe.addressSize();
 800 
 801     /**
 802      * Reports the size in bytes of a native memory page (whatever that is).
 803      * This value will always be a power of two.
 804      */
 805     @ForceInline


1059      * absolute and time is not zero, the given time nanoseconds have
1060      * elapsed, or if absolute, the given deadline in milliseconds
1061      * since Epoch has passed, or spuriously (i.e., returning for no
1062      * "reason"). Note: This operation is in the Unsafe class only
1063      * because {@code unpark} is, so it would be strange to place it
1064      * elsewhere.
1065      */
1066     @ForceInline
1067     public void park(boolean isAbsolute, long time) {
1068         theInternalUnsafe.park(isAbsolute, time);
1069     }
1070 
1071     /**
1072      * Gets the load average in the system run queue assigned
1073      * to the available processors averaged over various periods of time.
1074      * This method retrieves the given {@code nelem} samples and
1075      * assigns to the elements of the given {@code loadavg} array.
1076      * The system imposes a maximum of 3 samples, representing
1077      * averages over the last 1,  5,  and  15 minutes, respectively.
1078      *


1079      * @param loadavg an array of double of size nelems
1080      * @param nelems the number of samples to be retrieved and
1081      *        must be 1 to 3.
1082      *
1083      * @return the number of samples actually retrieved; or -1
1084      *         if the load average is unobtainable.





1085      */
1086     @ForceInline
1087     public int getLoadAverage(double[] loadavg, int nelems) {
1088         return theInternalUnsafe.getLoadAverage(loadavg, nelems);
1089     }
1090 
1091     // The following contain CAS-based Java implementations used on
1092     // platforms not supporting native instructions
1093 
1094     /**
1095      * Atomically adds the given value to the current value of a field
1096      * or array element within the given object {@code o}
1097      * at the given {@code offset}.
1098      *
1099      * @param o object/array to update the field/element in
1100      * @param offset field/element offset
1101      * @param delta the value to add
1102      * @return the previous value
1103      * @since 1.8
1104      */
1105     @ForceInline
1106     public final int getAndAddInt(Object o, long offset, int delta) {
1107         return theInternalUnsafe.getAndAddInt(o, offset, delta);
1108     }


1217      */
1218     @ForceInline
1219     public void fullFence() {
1220         theInternalUnsafe.fullFence();
1221     }
1222 
1223     /**
1224      * Invokes the given direct byte buffer's cleaner, if any.
1225      *
1226      * @param directBuffer a direct byte buffer
1227      * @throws NullPointerException if {@code directBuffer} is null
1228      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
1229      * or is a {@link java.nio.Buffer#slice slice}, or is a
1230      * {@link java.nio.Buffer#duplicate duplicate}
1231      * @since 9
1232      */
1233     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
1234         if (!directBuffer.isDirect())
1235             throw new IllegalArgumentException("buffer is non-direct");
1236 
1237         DirectBuffer db = (DirectBuffer)directBuffer;
1238         if (db.attachment() != null)
1239             throw new IllegalArgumentException("duplicate or slice");
1240 
1241         Cleaner cleaner = db.cleaner();
1242         if (cleaner != null) {
1243             cleaner.clean();
1244         }
1245     }
1246 }


   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 sun.misc;
  27 
  28 import jdk.internal.unsupported.UnsafeForwarder;
  29 import jdk.internal.vm.annotation.ForceInline;


  30 import jdk.internal.reflect.CallerSensitive;
  31 import jdk.internal.reflect.Reflection;

  32 
  33 import java.lang.reflect.Field;
  34 import java.util.Set;
  35 
  36 
  37 /**
  38  * A collection of methods for performing low-level, unsafe operations.
  39  * Although the class and all methods are public, use of this class is
  40  * limited because only trusted code can obtain instances of it.
  41  *
  42  * <em>Note:</em> It is the resposibility of the caller to make sure
  43  * arguments are checked before methods of this class are
  44  * called. While some rudimentary checks are performed on the input,
  45  * the checks are best effort and when performance is an overriding
  46  * priority, as when methods of this class are optimized by the
  47  * runtime compiler, some or all checks (if any) may be elided. Hence,
  48  * the caller must not rely on the checks and corresponding
  49  * exceptions!
  50  *
  51  * @author John R. Rose
  52  * @see #getUnsafe
  53  */
  54 
  55 public final class Unsafe {
  56 
  57     static {
  58         Reflection.registerMethodsToFilter(Unsafe.class, Set.of("getUnsafe"));
  59     }
  60 
  61     private Unsafe() {}
  62 
  63     private static final Unsafe theUnsafe = new Unsafe();
  64     private static final UnsafeForwarder theInternalUnsafe = UnsafeForwarder.getUnsafeHelper();
  65 
  66     /**
  67      * Provides the caller with the capability of performing unsafe
  68      * operations.
  69      *
  70      * <p>The returned {@code Unsafe} object should be carefully guarded
  71      * by the caller, since it can be used to read and write data at arbitrary
  72      * memory addresses.  It must never be passed to untrusted code.
  73      *
  74      * <p>Most methods in this class are very low-level, and correspond to a
  75      * small number of hardware instructions (on typical machines).  Compilers
  76      * are encouraged to optimize these methods accordingly.
  77      *
  78      * <p>Here is a suggested idiom for using unsafe operations:
  79      *
  80      * <pre> {@code
  81      * class MyTrustedClass {
  82      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  83      *   ...
  84      *   private long myCountAddress = ...;
  85      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  86      * }}</pre>
  87      *
  88      * (It may assist compilers to make the local variable {@code final}.)
  89      *
  90      * @throws  SecurityException if the class loader of the caller
  91      *          class is not in the system domain in which all permissions
  92      *          are granted.
  93      */
  94     @CallerSensitive
  95     public static Unsafe getUnsafe() {
  96         Class<?> caller = Reflection.getCallerClass();
  97         ClassLoader loader = caller.getClassLoader();
  98         if (loader != null && loader != ClassLoader.getPlatformClassLoader())
  99          throw new SecurityException("Unsafe");
 100         return theUnsafe;
 101     }
 102 
 103     /// peek and poke operations
 104     /// (compilers should optimize these to memory ops)
 105 
 106     // These work on object fields in the Java heap.
 107     // They will not work on elements of packed arrays.
 108 
 109     /**
 110      * Fetches a value from a given Java variable.
 111      * More specifically, fetches a field or array element within the given
 112      * object {@code o} at the given offset, or (if {@code o} is null)
 113      * from the memory address whose numerical value is the given offset.
 114      * <p>
 115      * The results are undefined unless one of the following cases is true:
 116      * <ul>
 117      * <li>The offset was obtained from {@link #objectFieldOffset} on
 118      * the {@link java.lang.reflect.Field} of some Java field and the object


 598      * compiler, some or all checks (if any) may be elided. Hence, the
 599      * caller must not rely on the checks and corresponding
 600      * exceptions!
 601      *
 602      * @throws RuntimeException if any of the arguments is invalid
 603      *
 604      * @see #allocateMemory
 605      */
 606     @ForceInline
 607     public void freeMemory(long address) {
 608         theInternalUnsafe.freeMemory(address);
 609     }
 610 
 611     /// random queries
 612 
 613     /**
 614      * This constant differs from all results that will ever be returned from
 615      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 616      * or {@link #arrayBaseOffset}.
 617      */
 618     public static final int INVALID_FIELD_OFFSET = jdk.internal.unsupported.UnsafeForwarder.INVALID_FIELD_OFFSET;
 619 
 620     /**
 621      * Reports the location of a given field in the storage allocation of its
 622      * class.  Do not expect to perform any sort of arithmetic on this offset;
 623      * it is just a cookie which is passed to the unsafe heap memory accessors.
 624      *
 625      * <p>Any given field will always have the same offset and base, and no
 626      * two distinct fields of the same class will ever have the same offset
 627      * and base.
 628      *
 629      * <p>As of 1.4.1, offsets for fields are represented as long values,
 630      * although the Sun JVM does not use the most significant 32 bits.
 631      * However, JVM implementations which store static fields at absolute
 632      * addresses can use long offsets and null base pointers to express
 633      * the field locations in a form usable by {@link #getInt(Object,long)}.
 634      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 635      * must preserve all bits of static field offsets.
 636      * @see #getInt(Object, long)
 637      */
 638     @ForceInline


 697     public void ensureClassInitialized(Class<?> c) {
 698         theInternalUnsafe.ensureClassInitialized(c);
 699     }
 700 
 701     /**
 702      * Reports the offset of the first element in the storage allocation of a
 703      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 704      * for the same class, you may use that scale factor, together with this
 705      * base offset, to form new offsets to access elements of arrays of the
 706      * given class.
 707      *
 708      * @see #getInt(Object, long)
 709      * @see #putInt(Object, long, int)
 710      */
 711     @ForceInline
 712     public int arrayBaseOffset(Class<?> arrayClass) {
 713         return theInternalUnsafe.arrayBaseOffset(arrayClass);
 714     }
 715 
 716     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 717     public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_BOOLEAN_BASE_OFFSET;
 718 
 719     /** The value of {@code arrayBaseOffset(byte[].class)} */
 720     public static final int ARRAY_BYTE_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_BYTE_BASE_OFFSET;
 721 
 722     /** The value of {@code arrayBaseOffset(short[].class)} */
 723     public static final int ARRAY_SHORT_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_SHORT_BASE_OFFSET;
 724 
 725     /** The value of {@code arrayBaseOffset(char[].class)} */
 726     public static final int ARRAY_CHAR_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_CHAR_BASE_OFFSET;
 727 
 728     /** The value of {@code arrayBaseOffset(int[].class)} */
 729     public static final int ARRAY_INT_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_INT_BASE_OFFSET;
 730 
 731     /** The value of {@code arrayBaseOffset(long[].class)} */
 732     public static final int ARRAY_LONG_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_LONG_BASE_OFFSET;
 733 
 734     /** The value of {@code arrayBaseOffset(float[].class)} */
 735     public static final int ARRAY_FLOAT_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_FLOAT_BASE_OFFSET;
 736 
 737     /** The value of {@code arrayBaseOffset(double[].class)} */
 738     public static final int ARRAY_DOUBLE_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_DOUBLE_BASE_OFFSET;
 739 
 740     /** The value of {@code arrayBaseOffset(Object[].class)} */
 741     public static final int ARRAY_OBJECT_BASE_OFFSET = jdk.internal.unsupported.UnsafeForwarder.ARRAY_OBJECT_BASE_OFFSET;
 742 
 743     /**
 744      * Reports the scale factor for addressing elements in the storage
 745      * allocation of a given array class.  However, arrays of "narrow" types
 746      * will generally not work properly with accessors like {@link
 747      * #getByte(Object, long)}, so the scale factor for such classes is reported
 748      * as zero.
 749      *
 750      * @see #arrayBaseOffset
 751      * @see #getInt(Object, long)
 752      * @see #putInt(Object, long, int)
 753      */
 754     @ForceInline
 755     public int arrayIndexScale(Class<?> arrayClass) {
 756         return theInternalUnsafe.arrayIndexScale(arrayClass);
 757     }
 758 
 759     /** The value of {@code arrayIndexScale(boolean[].class)} */
 760     public static final int ARRAY_BOOLEAN_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_BOOLEAN_INDEX_SCALE;
 761 
 762     /** The value of {@code arrayIndexScale(byte[].class)} */
 763     public static final int ARRAY_BYTE_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_BYTE_INDEX_SCALE;
 764 
 765     /** The value of {@code arrayIndexScale(short[].class)} */
 766     public static final int ARRAY_SHORT_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_SHORT_INDEX_SCALE;
 767 
 768     /** The value of {@code arrayIndexScale(char[].class)} */
 769     public static final int ARRAY_CHAR_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_CHAR_INDEX_SCALE;
 770 
 771     /** The value of {@code arrayIndexScale(int[].class)} */
 772     public static final int ARRAY_INT_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_INT_INDEX_SCALE;
 773 
 774     /** The value of {@code arrayIndexScale(long[].class)} */
 775     public static final int ARRAY_LONG_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_LONG_INDEX_SCALE;
 776 
 777     /** The value of {@code arrayIndexScale(float[].class)} */
 778     public static final int ARRAY_FLOAT_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_FLOAT_INDEX_SCALE;
 779 
 780     /** The value of {@code arrayIndexScale(double[].class)} */
 781     public static final int ARRAY_DOUBLE_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_DOUBLE_INDEX_SCALE;
 782 
 783     /** The value of {@code arrayIndexScale(Object[].class)} */
 784     public static final int ARRAY_OBJECT_INDEX_SCALE = jdk.internal.unsupported.UnsafeForwarder.ARRAY_OBJECT_INDEX_SCALE;
 785 
 786     /**
 787      * Reports the size in bytes of a native pointer, as stored via {@link
 788      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
 789      * other primitive types (as stored in native memory blocks) is determined
 790      * fully by their information content.
 791      */
 792     @ForceInline
 793     public int addressSize() {
 794         return theInternalUnsafe.addressSize();
 795     }
 796 
 797     /** The value of {@code addressSize()} */
 798     public static final int ADDRESS_SIZE = theInternalUnsafe.addressSize();
 799 
 800     /**
 801      * Reports the size in bytes of a native memory page (whatever that is).
 802      * This value will always be a power of two.
 803      */
 804     @ForceInline


1058      * absolute and time is not zero, the given time nanoseconds have
1059      * elapsed, or if absolute, the given deadline in milliseconds
1060      * since Epoch has passed, or spuriously (i.e., returning for no
1061      * "reason"). Note: This operation is in the Unsafe class only
1062      * because {@code unpark} is, so it would be strange to place it
1063      * elsewhere.
1064      */
1065     @ForceInline
1066     public void park(boolean isAbsolute, long time) {
1067         theInternalUnsafe.park(isAbsolute, time);
1068     }
1069 
1070     /**
1071      * Gets the load average in the system run queue assigned
1072      * to the available processors averaged over various periods of time.
1073      * This method retrieves the given {@code nelem} samples and
1074      * assigns to the elements of the given {@code loadavg} array.
1075      * The system imposes a maximum of 3 samples, representing
1076      * averages over the last 1,  5,  and  15 minutes, respectively.
1077      *
1078      * @implNote This method returns -1.
1079      *
1080      * @param loadavg an array of double of size nelems
1081      * @param nelems the number of samples to be retrieved and
1082      *        must be 1 to 3.
1083      *
1084      * @return the number of samples actually retrieved; or -1
1085      *         if the load average is unobtainable.
1086      *
1087      * @deprecated This method is planned to be removed in a future release.
1088      * To get system load average, use
1089      * {@link java.lang.management.OperatingSystemMXBean#getSystemLoadAverage()}
1090      * instead.
1091      */
1092     @Deprecated(since="12", forRemoval=true)
1093     public int getLoadAverage(double[] loadavg, int nelems) {
1094         return -1;
1095     }
1096 
1097     // The following contain CAS-based Java implementations used on
1098     // platforms not supporting native instructions
1099 
1100     /**
1101      * Atomically adds the given value to the current value of a field
1102      * or array element within the given object {@code o}
1103      * at the given {@code offset}.
1104      *
1105      * @param o object/array to update the field/element in
1106      * @param offset field/element offset
1107      * @param delta the value to add
1108      * @return the previous value
1109      * @since 1.8
1110      */
1111     @ForceInline
1112     public final int getAndAddInt(Object o, long offset, int delta) {
1113         return theInternalUnsafe.getAndAddInt(o, offset, delta);
1114     }


1223      */
1224     @ForceInline
1225     public void fullFence() {
1226         theInternalUnsafe.fullFence();
1227     }
1228 
1229     /**
1230      * Invokes the given direct byte buffer's cleaner, if any.
1231      *
1232      * @param directBuffer a direct byte buffer
1233      * @throws NullPointerException if {@code directBuffer} is null
1234      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
1235      * or is a {@link java.nio.Buffer#slice slice}, or is a
1236      * {@link java.nio.Buffer#duplicate duplicate}
1237      * @since 9
1238      */
1239     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
1240         if (!directBuffer.isDirect())
1241             throw new IllegalArgumentException("buffer is non-direct");
1242 
1243         theInternalUnsafe.invokeCleaner(directBuffer);







1244     }
1245 }
< prev index next >