< prev index next >

src/java.base/share/classes/jdk/internal/misc/Unsafe.java

Print this page

   1 /*
   2  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import jdk.internal.ref.Cleaner;

  29 import jdk.internal.vm.annotation.ForceInline;
  30 import jdk.internal.vm.annotation.IntrinsicCandidate;
  31 import sun.nio.ch.DirectBuffer;
  32 
  33 import java.lang.reflect.Field;
  34 import java.security.ProtectionDomain;
  35 
  36 import static jdk.internal.misc.UnsafeConstants.*;
  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 responsibility 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,

 160      * The first two parameters are interpreted exactly as with
 161      * {@link #getInt(Object, long)} to refer to a specific
 162      * Java variable (field or array element).  The given value
 163      * is stored into that variable.
 164      * <p>
 165      * The variable must be of the same type as the method
 166      * parameter {@code x}.
 167      *
 168      * @param o Java heap object in which the variable resides, if any, else
 169      *        null
 170      * @param offset indication of where the variable resides in a Java heap
 171      *        object, if any, else a memory address locating the variable
 172      *        statically
 173      * @param x the value to store into the indicated Java variable
 174      * @throws RuntimeException No defined exceptions are thrown, not even
 175      *         {@link NullPointerException}
 176      */
 177     @IntrinsicCandidate
 178     public native void putInt(Object o, long offset, int x);
 179 


















 180     /**
 181      * Fetches a reference value from a given Java variable.



 182      * @see #getInt(Object, long)
 183      */
 184     @IntrinsicCandidate
 185     public native Object getReference(Object o, long offset);
 186 
 187     /**
 188      * Stores a reference value into a given Java variable.


 189      * <p>
 190      * Unless the reference {@code x} being stored is either null
 191      * or matches the field type, the results are undefined.
 192      * If the reference {@code o} is non-null, card marks or
 193      * other store barriers for that object (if the VM requires them)
 194      * are updated.
 195      * @see #putInt(Object, long, int)
 196      */
 197     @IntrinsicCandidate
 198     public native void putReference(Object o, long offset, Object x);
 199 





































































































 200     /** @see #getInt(Object, long) */
 201     @IntrinsicCandidate
 202     public native boolean getBoolean(Object o, long offset);
 203 
 204     /** @see #putInt(Object, long, int) */
 205     @IntrinsicCandidate
 206     public native void    putBoolean(Object o, long offset, boolean x);
 207 
 208     /** @see #getInt(Object, long) */
 209     @IntrinsicCandidate
 210     public native byte    getByte(Object o, long offset);
 211 
 212     /** @see #putInt(Object, long, int) */
 213     @IntrinsicCandidate
 214     public native void    putByte(Object o, long offset, byte x);
 215 
 216     /** @see #getInt(Object, long) */
 217     @IntrinsicCandidate
 218     public native short   getShort(Object o, long offset);
 219 

1217 
1218     /**
1219      * Reports the scale factor for addressing elements in the storage
1220      * allocation of a given array class.  However, arrays of "narrow" types
1221      * will generally not work properly with accessors like {@link
1222      * #getByte(Object, long)}, so the scale factor for such classes is reported
1223      * as zero.
1224      *
1225      * @see #arrayBaseOffset
1226      * @see #getInt(Object, long)
1227      * @see #putInt(Object, long, int)
1228      */
1229     public int arrayIndexScale(Class<?> arrayClass) {
1230         if (arrayClass == null) {
1231             throw new NullPointerException();
1232         }
1233 
1234         return arrayIndexScale0(arrayClass);
1235     }
1236 











1237 
1238     /** The value of {@code arrayIndexScale(boolean[].class)} */
1239     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1240             = theUnsafe.arrayIndexScale(boolean[].class);
1241 
1242     /** The value of {@code arrayIndexScale(byte[].class)} */
1243     public static final int ARRAY_BYTE_INDEX_SCALE
1244             = theUnsafe.arrayIndexScale(byte[].class);
1245 
1246     /** The value of {@code arrayIndexScale(short[].class)} */
1247     public static final int ARRAY_SHORT_INDEX_SCALE
1248             = theUnsafe.arrayIndexScale(short[].class);
1249 
1250     /** The value of {@code arrayIndexScale(char[].class)} */
1251     public static final int ARRAY_CHAR_INDEX_SCALE
1252             = theUnsafe.arrayIndexScale(char[].class);
1253 
1254     /** The value of {@code arrayIndexScale(int[].class)} */
1255     public static final int ARRAY_INT_INDEX_SCALE
1256             = theUnsafe.arrayIndexScale(int[].class);

1395        return null;
1396     }
1397 
1398     /** Throws the exception without telling the verifier. */
1399     public native void throwException(Throwable ee);
1400 
1401     /**
1402      * Atomically updates Java variable to {@code x} if it is currently
1403      * holding {@code expected}.
1404      *
1405      * <p>This operation has memory semantics of a {@code volatile} read
1406      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1407      *
1408      * @return {@code true} if successful
1409      */
1410     @IntrinsicCandidate
1411     public final native boolean compareAndSetReference(Object o, long offset,
1412                                                        Object expected,
1413                                                        Object x);
1414 















































1415     @IntrinsicCandidate
1416     public final native Object compareAndExchangeReference(Object o, long offset,
1417                                                            Object expected,
1418                                                            Object x);
1419 































1420     @IntrinsicCandidate
1421     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1422                                                            Object expected,
1423                                                            Object x) {
1424         return compareAndExchangeReference(o, offset, expected, x);
1425     }
1426 















1427     @IntrinsicCandidate
1428     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1429                                                            Object expected,
1430                                                            Object x) {
1431         return compareAndExchangeReference(o, offset, expected, x);
1432     }
1433 















1434     @IntrinsicCandidate
1435     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1436                                                          Object expected,
1437                                                          Object x) {
1438         return compareAndSetReference(o, offset, expected, x);
1439     }
1440 



















1441     @IntrinsicCandidate
1442     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1443                                                            Object expected,
1444                                                            Object x) {
1445         return compareAndSetReference(o, offset, expected, x);
1446     }
1447 



















1448     @IntrinsicCandidate
1449     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1450                                                            Object expected,
1451                                                            Object x) {
1452         return compareAndSetReference(o, offset, expected, x);
1453     }
1454 



















1455     @IntrinsicCandidate
1456     public final boolean weakCompareAndSetReference(Object o, long offset,
1457                                                     Object expected,
1458                                                     Object x) {
1459         return compareAndSetReference(o, offset, expected, x);
1460     }
1461 



















1462     /**
1463      * Atomically updates Java variable to {@code x} if it is currently
1464      * holding {@code expected}.
1465      *
1466      * <p>This operation has memory semantics of a {@code volatile} read
1467      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1468      *
1469      * @return {@code true} if successful
1470      */
1471     @IntrinsicCandidate
1472     public final native boolean compareAndSetInt(Object o, long offset,
1473                                                  int expected,
1474                                                  int x);
1475 
1476     @IntrinsicCandidate
1477     public final native int compareAndExchangeInt(Object o, long offset,
1478                                                   int expected,
1479                                                   int x);
1480 
1481     @IntrinsicCandidate

2057     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2058                                                       long expected,
2059                                                       long x) {
2060         return compareAndSetLong(o, offset, expected, x);
2061     }
2062 
2063     @IntrinsicCandidate
2064     public final boolean weakCompareAndSetLong(Object o, long offset,
2065                                                long expected,
2066                                                long x) {
2067         return compareAndSetLong(o, offset, expected, x);
2068     }
2069 
2070     /**
2071      * Fetches a reference value from a given Java variable, with volatile
2072      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2073      */
2074     @IntrinsicCandidate
2075     public native Object getReferenceVolatile(Object o, long offset);
2076 













2077     /**
2078      * Stores a reference value into a given Java variable, with
2079      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2080      */
2081     @IntrinsicCandidate
2082     public native void putReferenceVolatile(Object o, long offset, Object x);
2083 






2084     /** Volatile version of {@link #getInt(Object, long)}  */
2085     @IntrinsicCandidate
2086     public native int     getIntVolatile(Object o, long offset);
2087 
2088     /** Volatile version of {@link #putInt(Object, long, int)}  */
2089     @IntrinsicCandidate
2090     public native void    putIntVolatile(Object o, long offset, int x);
2091 
2092     /** Volatile version of {@link #getBoolean(Object, long)}  */
2093     @IntrinsicCandidate
2094     public native boolean getBooleanVolatile(Object o, long offset);
2095 
2096     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2097     @IntrinsicCandidate
2098     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2099 
2100     /** Volatile version of {@link #getByte(Object, long)}  */
2101     @IntrinsicCandidate
2102     public native byte    getByteVolatile(Object o, long offset);
2103 

2136     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2137     @IntrinsicCandidate
2138     public native void    putFloatVolatile(Object o, long offset, float x);
2139 
2140     /** Volatile version of {@link #getDouble(Object, long)}  */
2141     @IntrinsicCandidate
2142     public native double  getDoubleVolatile(Object o, long offset);
2143 
2144     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2145     @IntrinsicCandidate
2146     public native void    putDoubleVolatile(Object o, long offset, double x);
2147 
2148 
2149 
2150     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2151     @IntrinsicCandidate
2152     public final Object getReferenceAcquire(Object o, long offset) {
2153         return getReferenceVolatile(o, offset);
2154     }
2155 




2156     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2157     @IntrinsicCandidate
2158     public final boolean getBooleanAcquire(Object o, long offset) {
2159         return getBooleanVolatile(o, offset);
2160     }
2161 
2162     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2163     @IntrinsicCandidate
2164     public final byte getByteAcquire(Object o, long offset) {
2165         return getByteVolatile(o, offset);
2166     }
2167 
2168     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2169     @IntrinsicCandidate
2170     public final short getShortAcquire(Object o, long offset) {
2171         return getShortVolatile(o, offset);
2172     }
2173 
2174     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2175     @IntrinsicCandidate

2200     public final double getDoubleAcquire(Object o, long offset) {
2201         return getDoubleVolatile(o, offset);
2202     }
2203 
2204     /*
2205      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2206      * that do not guarantee immediate visibility of the store to
2207      * other threads. This method is generally only useful if the
2208      * underlying field is a Java volatile (or if an array cell, one
2209      * that is otherwise only accessed using volatile accesses).
2210      *
2211      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2212      */
2213 
2214     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2215     @IntrinsicCandidate
2216     public final void putReferenceRelease(Object o, long offset, Object x) {
2217         putReferenceVolatile(o, offset, x);
2218     }
2219 




2220     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2221     @IntrinsicCandidate
2222     public final void putBooleanRelease(Object o, long offset, boolean x) {
2223         putBooleanVolatile(o, offset, x);
2224     }
2225 
2226     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2227     @IntrinsicCandidate
2228     public final void putByteRelease(Object o, long offset, byte x) {
2229         putByteVolatile(o, offset, x);
2230     }
2231 
2232     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2233     @IntrinsicCandidate
2234     public final void putShortRelease(Object o, long offset, short x) {
2235         putShortVolatile(o, offset, x);
2236     }
2237 
2238     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2239     @IntrinsicCandidate

2256     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2257     @IntrinsicCandidate
2258     public final void putLongRelease(Object o, long offset, long x) {
2259         putLongVolatile(o, offset, x);
2260     }
2261 
2262     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2263     @IntrinsicCandidate
2264     public final void putDoubleRelease(Object o, long offset, double x) {
2265         putDoubleVolatile(o, offset, x);
2266     }
2267 
2268     // ------------------------------ Opaque --------------------------------------
2269 
2270     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2271     @IntrinsicCandidate
2272     public final Object getReferenceOpaque(Object o, long offset) {
2273         return getReferenceVolatile(o, offset);
2274     }
2275 




2276     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2277     @IntrinsicCandidate
2278     public final boolean getBooleanOpaque(Object o, long offset) {
2279         return getBooleanVolatile(o, offset);
2280     }
2281 
2282     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2283     @IntrinsicCandidate
2284     public final byte getByteOpaque(Object o, long offset) {
2285         return getByteVolatile(o, offset);
2286     }
2287 
2288     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2289     @IntrinsicCandidate
2290     public final short getShortOpaque(Object o, long offset) {
2291         return getShortVolatile(o, offset);
2292     }
2293 
2294     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2295     @IntrinsicCandidate

2310     }
2311 
2312     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2313     @IntrinsicCandidate
2314     public final long getLongOpaque(Object o, long offset) {
2315         return getLongVolatile(o, offset);
2316     }
2317 
2318     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2319     @IntrinsicCandidate
2320     public final double getDoubleOpaque(Object o, long offset) {
2321         return getDoubleVolatile(o, offset);
2322     }
2323 
2324     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2325     @IntrinsicCandidate
2326     public final void putReferenceOpaque(Object o, long offset, Object x) {
2327         putReferenceVolatile(o, offset, x);
2328     }
2329 




2330     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2331     @IntrinsicCandidate
2332     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2333         putBooleanVolatile(o, offset, x);
2334     }
2335 
2336     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2337     @IntrinsicCandidate
2338     public final void putByteOpaque(Object o, long offset, byte x) {
2339         putByteVolatile(o, offset, x);
2340     }
2341 
2342     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2343     @IntrinsicCandidate
2344     public final void putShortOpaque(Object o, long offset, short x) {
2345         putShortVolatile(o, offset, x);
2346     }
2347 
2348     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2349     @IntrinsicCandidate

2744     /**
2745      * Atomically exchanges the given reference value with the current
2746      * reference value of a field or array element within the given
2747      * object {@code o} at the given {@code offset}.
2748      *
2749      * @param o object/array to update the field/element in
2750      * @param offset field/element offset
2751      * @param newValue new value
2752      * @return the previous value
2753      * @since 1.8
2754      */
2755     @IntrinsicCandidate
2756     public final Object getAndSetReference(Object o, long offset, Object newValue) {
2757         Object v;
2758         do {
2759             v = getReferenceVolatile(o, offset);
2760         } while (!weakCompareAndSetReference(o, offset, v, newValue));
2761         return v;
2762     }
2763 









2764     @ForceInline
2765     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2766         Object v;
2767         do {
2768             v = getReference(o, offset);
2769         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2770         return v;
2771     }
2772 





2773     @ForceInline
2774     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2775         Object v;
2776         do {
2777             v = getReferenceAcquire(o, offset);
2778         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2779         return v;
2780     }
2781 





2782     @IntrinsicCandidate
2783     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2784         byte v;
2785         do {
2786             v = getByteVolatile(o, offset);
2787         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2788         return v;
2789     }
2790 
2791     @ForceInline
2792     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2793         byte v;
2794         do {
2795             v = getByte(o, offset);
2796         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2797         return v;
2798     }
2799 
2800     @ForceInline
2801     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

3818     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
3819     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
3820 
3821 
3822 
3823     private native long allocateMemory0(long bytes);
3824     private native long reallocateMemory0(long address, long bytes);
3825     private native void freeMemory0(long address);
3826     private native void setMemory0(Object o, long offset, long bytes, byte value);
3827     @IntrinsicCandidate
3828     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3829     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3830     private native long objectFieldOffset0(Field f);
3831     private native long objectFieldOffset1(Class<?> c, String name);
3832     private native long staticFieldOffset0(Field f);
3833     private native Object staticFieldBase0(Field f);
3834     private native boolean shouldBeInitialized0(Class<?> c);
3835     private native void ensureClassInitialized0(Class<?> c);
3836     private native int arrayBaseOffset0(Class<?> arrayClass);
3837     private native int arrayIndexScale0(Class<?> arrayClass);

3838     private native int getLoadAverage0(double[] loadavg, int nelems);
3839 
3840 
3841     /**
3842      * Invokes the given direct byte buffer's cleaner, if any.
3843      *
3844      * @param directBuffer a direct byte buffer
3845      * @throws NullPointerException     if {@code directBuffer} is null
3846      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3847      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
3848      *                                  {@link java.nio.Buffer#duplicate duplicate}
3849      */
3850     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3851         if (!directBuffer.isDirect())
3852             throw new IllegalArgumentException("buffer is non-direct");
3853 
3854         DirectBuffer db = (DirectBuffer) directBuffer;
3855         if (db.attachment() != null)
3856             throw new IllegalArgumentException("duplicate or slice");
3857 

   1 /*
   2  * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import jdk.internal.ref.Cleaner;
  29 import jdk.internal.value.PrimitiveClass;
  30 import jdk.internal.vm.annotation.ForceInline;
  31 import jdk.internal.vm.annotation.IntrinsicCandidate;
  32 import sun.nio.ch.DirectBuffer;
  33 
  34 import java.lang.reflect.Field;
  35 import java.security.ProtectionDomain;
  36 
  37 import static jdk.internal.misc.UnsafeConstants.*;
  38 
  39 /**
  40  * A collection of methods for performing low-level, unsafe operations.
  41  * Although the class and all methods are public, use of this class is
  42  * limited because only trusted code can obtain instances of it.
  43  *
  44  * <em>Note:</em> It is the responsibility of the caller to make sure
  45  * arguments are checked before methods of this class are
  46  * called. While some rudimentary checks are performed on the input,
  47  * the checks are best effort and when performance is an overriding
  48  * priority, as when methods of this class are optimized by the
  49  * runtime compiler, some or all checks (if any) may be elided. Hence,

 161      * The first two parameters are interpreted exactly as with
 162      * {@link #getInt(Object, long)} to refer to a specific
 163      * Java variable (field or array element).  The given value
 164      * is stored into that variable.
 165      * <p>
 166      * The variable must be of the same type as the method
 167      * parameter {@code x}.
 168      *
 169      * @param o Java heap object in which the variable resides, if any, else
 170      *        null
 171      * @param offset indication of where the variable resides in a Java heap
 172      *        object, if any, else a memory address locating the variable
 173      *        statically
 174      * @param x the value to store into the indicated Java variable
 175      * @throws RuntimeException No defined exceptions are thrown, not even
 176      *         {@link NullPointerException}
 177      */
 178     @IntrinsicCandidate
 179     public native void putInt(Object o, long offset, int x);
 180 
 181 
 182     /**
 183      * Returns true if the given field is flattened.
 184      */
 185     public boolean isFlattened(Field f) {
 186         if (f == null) {
 187             throw new NullPointerException();
 188         }
 189         return isFlattenedField0(f);
 190     }
 191 
 192     private native boolean isFlattenedField0(Object o);
 193 
 194     /**
 195      * Returns true if the given class is a flattened array.
 196      */
 197     public native boolean isFlattenedArray(Class<?> arrayClass);
 198 
 199     /**
 200      * Fetches a reference value from a given Java variable.
 201      * This method can return a reference to either an object or value
 202      * or a null reference.
 203      *
 204      * @see #getInt(Object, long)
 205      */
 206     @IntrinsicCandidate
 207     public native Object getReference(Object o, long offset);
 208 
 209     /**
 210      * Stores a reference value into a given Java variable.
 211      * This method can store a reference to either an object or value
 212      * or a null reference.
 213      * <p>
 214      * Unless the reference {@code x} being stored is either null
 215      * or matches the field type, the results are undefined.
 216      * If the reference {@code o} is non-null, card marks or
 217      * other store barriers for that object (if the VM requires them)
 218      * are updated.
 219      * @see #putInt(Object, long, int)
 220      */
 221     @IntrinsicCandidate
 222     public native void putReference(Object o, long offset, Object x);
 223 
 224     /**
 225      * Fetches a value of type {@code <V>} from a given Java variable.
 226      * More specifically, fetches a field or array element within the given
 227      * {@code o} object at the given offset, or (if {@code o} is null)
 228      * from the memory address whose numerical value is the given offset.
 229      *
 230      * @param o Java heap object in which the variable resides, if any, else
 231      *        null
 232      * @param offset indication of where the variable resides in a Java heap
 233      *        object, if any, else a memory address locating the variable
 234      *        statically
 235      * @param pc primitive class
 236      * @param <V> the type of a value
 237      * @return the value fetched from the indicated Java variable
 238      * @throws RuntimeException No defined exceptions are thrown, not even
 239      *         {@link NullPointerException}
 240      */
 241     @IntrinsicCandidate
 242     public native <V> V getValue(Object o, long offset, Class<?> pc);
 243 
 244     /**
 245      * Stores the given value into a given Java variable.
 246      *
 247      * Unless the reference {@code o} being stored is either null
 248      * or matches the field type, the results are undefined.
 249      *
 250      * @param o Java heap object in which the variable resides, if any, else
 251      *        null
 252      * @param offset indication of where the variable resides in a Java heap
 253      *        object, if any, else a memory address locating the variable
 254      *        statically
 255      * @param pc primitive class
 256      * @param v the value to store into the indicated Java variable
 257      * @param <V> the type of a value
 258      * @throws RuntimeException No defined exceptions are thrown, not even
 259      *         {@link NullPointerException}
 260      */
 261     @IntrinsicCandidate
 262     public native <V> void putValue(Object o, long offset, Class<?> pc, V v);
 263 
 264     /**
 265      * Fetches a reference value of type {@code pc} from a given Java variable.
 266      * This method can return a reference to a value or a null reference
 267      * for a nullable reference of a primitive type.
 268      *
 269      * @param pc primitive class
 270      */
 271     public Object getReference(Object o, long offset, Class<?> pc) {
 272         Object ref = getReference(o, offset);
 273         if (ref == null && PrimitiveClass.isPrimitiveValueType(pc)) {
 274             // If the type of the returned reference is a regular primitive type
 275             // return an uninitialized default value if null
 276             ref = uninitializedDefaultValue(pc);
 277         }
 278         return ref;
 279     }
 280 
 281     public Object getReferenceVolatile(Object o, long offset, Class<?> pc) {
 282         Object ref = getReferenceVolatile(o, offset);
 283         if (ref == null && PrimitiveClass.isPrimitiveValueType(pc)) {
 284             // If the type of the returned reference is a regular primitive type
 285             // return an uninitialized default value if null
 286             ref = uninitializedDefaultValue(pc);
 287         }
 288         return ref;
 289     }
 290 
 291     /**
 292      * Returns an uninitialized default value of the given primitive class.
 293      */
 294     public native <V> V uninitializedDefaultValue(Class<?> pc);
 295 
 296     /**
 297      * Returns an object instance with a private buffered value whose layout
 298      * and contents is exactly the given value instance.  The return object
 299      * is in the larval state that can be updated using the unsafe put operation.
 300      *
 301      * @param value a value instance
 302      * @param <V> the type of the given value instance
 303      */
 304     @IntrinsicCandidate
 305     public native <V> V makePrivateBuffer(V value);
 306 
 307     /**
 308      * Exits the larval state and returns a value instance.
 309      *
 310      * @param value a value instance
 311      * @param <V> the type of the given value instance
 312      */
 313     @IntrinsicCandidate
 314     public native <V> V finishPrivateBuffer(V value);
 315 
 316     /**
 317      * Returns the header size of the given primitive class.
 318      *
 319      * @param pc primitive class
 320      * @param <V> value clas
 321      * @return the header size of the primitive class
 322      */
 323     public native <V> long valueHeaderSize(Class<V> pc);
 324 
 325     /** @see #getInt(Object, long) */
 326     @IntrinsicCandidate
 327     public native boolean getBoolean(Object o, long offset);
 328 
 329     /** @see #putInt(Object, long, int) */
 330     @IntrinsicCandidate
 331     public native void    putBoolean(Object o, long offset, boolean x);
 332 
 333     /** @see #getInt(Object, long) */
 334     @IntrinsicCandidate
 335     public native byte    getByte(Object o, long offset);
 336 
 337     /** @see #putInt(Object, long, int) */
 338     @IntrinsicCandidate
 339     public native void    putByte(Object o, long offset, byte x);
 340 
 341     /** @see #getInt(Object, long) */
 342     @IntrinsicCandidate
 343     public native short   getShort(Object o, long offset);
 344 

1342 
1343     /**
1344      * Reports the scale factor for addressing elements in the storage
1345      * allocation of a given array class.  However, arrays of "narrow" types
1346      * will generally not work properly with accessors like {@link
1347      * #getByte(Object, long)}, so the scale factor for such classes is reported
1348      * as zero.
1349      *
1350      * @see #arrayBaseOffset
1351      * @see #getInt(Object, long)
1352      * @see #putInt(Object, long, int)
1353      */
1354     public int arrayIndexScale(Class<?> arrayClass) {
1355         if (arrayClass == null) {
1356             throw new NullPointerException();
1357         }
1358 
1359         return arrayIndexScale0(arrayClass);
1360     }
1361 
1362     /**
1363      * Return the size of the object in the heap.
1364      * @param o an object
1365      * @return the objects's size
1366      * @since Valhalla
1367      */
1368     public long getObjectSize(Object o) {
1369         if (o == null)
1370             throw new NullPointerException();
1371         return getObjectSize0(o);
1372     }
1373 
1374     /** The value of {@code arrayIndexScale(boolean[].class)} */
1375     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1376             = theUnsafe.arrayIndexScale(boolean[].class);
1377 
1378     /** The value of {@code arrayIndexScale(byte[].class)} */
1379     public static final int ARRAY_BYTE_INDEX_SCALE
1380             = theUnsafe.arrayIndexScale(byte[].class);
1381 
1382     /** The value of {@code arrayIndexScale(short[].class)} */
1383     public static final int ARRAY_SHORT_INDEX_SCALE
1384             = theUnsafe.arrayIndexScale(short[].class);
1385 
1386     /** The value of {@code arrayIndexScale(char[].class)} */
1387     public static final int ARRAY_CHAR_INDEX_SCALE
1388             = theUnsafe.arrayIndexScale(char[].class);
1389 
1390     /** The value of {@code arrayIndexScale(int[].class)} */
1391     public static final int ARRAY_INT_INDEX_SCALE
1392             = theUnsafe.arrayIndexScale(int[].class);

1531        return null;
1532     }
1533 
1534     /** Throws the exception without telling the verifier. */
1535     public native void throwException(Throwable ee);
1536 
1537     /**
1538      * Atomically updates Java variable to {@code x} if it is currently
1539      * holding {@code expected}.
1540      *
1541      * <p>This operation has memory semantics of a {@code volatile} read
1542      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1543      *
1544      * @return {@code true} if successful
1545      */
1546     @IntrinsicCandidate
1547     public final native boolean compareAndSetReference(Object o, long offset,
1548                                                        Object expected,
1549                                                        Object x);
1550 
1551     private final boolean isInlineType(Object o) {
1552         return o != null && PrimitiveClass.isPrimitiveClass(o.getClass());
1553     }
1554 
1555     /*
1556      * For primitive type, CAS should do substitutability test as opposed
1557      * to two pointers comparison.
1558      *
1559      * Perhaps we can keep the xxxObject methods for compatibility and
1560      * change the JDK 13 xxxReference method signature freely.
1561      */
1562     public final <V> boolean compareAndSetReference(Object o, long offset,
1563                                                     Class<?> valueType,
1564                                                     V expected,
1565                                                     V x) {
1566         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1567             synchronized (valueLock) {
1568                 Object witness = getReference(o, offset);
1569                 if (witness == expected) {
1570                     putReference(o, offset, x);
1571                     return true;
1572                 } else {
1573                     return false;
1574                 }
1575             }
1576         } else {
1577             return compareAndSetReference(o, offset, expected, x);
1578         }
1579     }
1580 
1581     @ForceInline
1582     public final <V> boolean compareAndSetValue(Object o, long offset,
1583                                                 Class<?> valueType,
1584                                                 V expected,
1585                                                 V x) {
1586         synchronized (valueLock) {
1587             Object witness = getValue(o, offset, valueType);
1588             if (witness == expected) {
1589                 putValue(o, offset, valueType, x);
1590                 return true;
1591             }
1592             else {
1593                 return false;
1594             }
1595         }
1596     }
1597 
1598     @IntrinsicCandidate
1599     public final native Object compareAndExchangeReference(Object o, long offset,
1600                                                            Object expected,
1601                                                            Object x);
1602 
1603     public final <V> Object compareAndExchangeReference(Object o, long offset,
1604                                                         Class<?> valueType,
1605                                                         V expected,
1606                                                         V x) {
1607         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1608             synchronized (valueLock) {
1609                 Object witness = getReference(o, offset);
1610                 if (witness == expected) {
1611                     putReference(o, offset, x);
1612                 }
1613                 return witness;
1614             }
1615         } else {
1616             return compareAndExchangeReference(o, offset, expected, x);
1617         }
1618     }
1619 
1620     @ForceInline
1621     public final <V> Object compareAndExchangeValue(Object o, long offset,
1622                                                     Class<?> valueType,
1623                                                     V expected,
1624                                                     V x) {
1625         synchronized (valueLock) {
1626             Object witness = getValue(o, offset, valueType);
1627             if (witness == expected) {
1628                 putValue(o, offset, valueType, x);
1629             }
1630             return witness;
1631         }
1632     }
1633 
1634     @IntrinsicCandidate
1635     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1636                                                            Object expected,
1637                                                            Object x) {
1638         return compareAndExchangeReference(o, offset, expected, x);
1639     }
1640 
1641     public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1642                                                                Class<?> valueType,
1643                                                                V expected,
1644                                                                V x) {
1645         return compareAndExchangeReference(o, offset, valueType, expected, x);
1646     }
1647 
1648     @ForceInline
1649     public final <V> Object compareAndExchangeValueAcquire(Object o, long offset,
1650                                                            Class<?> valueType,
1651                                                            V expected,
1652                                                            V x) {
1653         return compareAndExchangeValue(o, offset, valueType, expected, x);
1654     }
1655 
1656     @IntrinsicCandidate
1657     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1658                                                            Object expected,
1659                                                            Object x) {
1660         return compareAndExchangeReference(o, offset, expected, x);
1661     }
1662 
1663     public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1664                                                                Class<?> valueType,
1665                                                                V expected,
1666                                                                V x) {
1667         return compareAndExchangeReference(o, offset, valueType, expected, x);
1668     }
1669 
1670     @ForceInline
1671     public final <V> Object compareAndExchangeValueRelease(Object o, long offset,
1672                                                            Class<?> valueType,
1673                                                            V expected,
1674                                                            V x) {
1675         return compareAndExchangeValue(o, offset, valueType, expected, x);
1676     }
1677 
1678     @IntrinsicCandidate
1679     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1680                                                          Object expected,
1681                                                          Object x) {
1682         return compareAndSetReference(o, offset, expected, x);
1683     }
1684 
1685     public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1686                                                              Class<?> valueType,
1687                                                              V expected,
1688                                                              V x) {
1689         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1690             return compareAndSetReference(o, offset, valueType, expected, x);
1691         } else {
1692             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1693         }
1694     }
1695 
1696     @ForceInline
1697     public final <V> boolean weakCompareAndSetValuePlain(Object o, long offset,
1698                                                          Class<?> valueType,
1699                                                          V expected,
1700                                                          V x) {
1701         return compareAndSetValue(o, offset, valueType, expected, x);
1702     }
1703 
1704     @IntrinsicCandidate
1705     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1706                                                            Object expected,
1707                                                            Object x) {
1708         return compareAndSetReference(o, offset, expected, x);
1709     }
1710 
1711     public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1712                                                                Class<?> valueType,
1713                                                                V expected,
1714                                                                V x) {
1715         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1716             return compareAndSetReference(o, offset, valueType, expected, x);
1717         } else {
1718             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1719         }
1720     }
1721 
1722     @ForceInline
1723     public final <V> boolean weakCompareAndSetValueAcquire(Object o, long offset,
1724                                                            Class<?> valueType,
1725                                                            V expected,
1726                                                            V x) {
1727         return compareAndSetValue(o, offset, valueType, expected, x);
1728     }
1729 
1730     @IntrinsicCandidate
1731     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1732                                                            Object expected,
1733                                                            Object x) {
1734         return compareAndSetReference(o, offset, expected, x);
1735     }
1736 
1737     public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1738                                                                Class<?> valueType,
1739                                                                V expected,
1740                                                                V x) {
1741         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1742             return compareAndSetReference(o, offset, valueType, expected, x);
1743         } else {
1744             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1745         }
1746     }
1747 
1748     @ForceInline
1749     public final <V> boolean weakCompareAndSetValueRelease(Object o, long offset,
1750                                                            Class<?> valueType,
1751                                                            V expected,
1752                                                            V x) {
1753         return compareAndSetValue(o, offset, valueType, expected, x);
1754     }
1755 
1756     @IntrinsicCandidate
1757     public final boolean weakCompareAndSetReference(Object o, long offset,
1758                                                     Object expected,
1759                                                     Object x) {
1760         return compareAndSetReference(o, offset, expected, x);
1761     }
1762 
1763     public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1764                                                         Class<?> valueType,
1765                                                         V expected,
1766                                                         V x) {
1767         if (PrimitiveClass.isPrimitiveClass(valueType) || isInlineType(expected)) {
1768             return compareAndSetReference(o, offset, valueType, expected, x);
1769         } else {
1770             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1771         }
1772     }
1773 
1774     @ForceInline
1775     public final <V> boolean weakCompareAndSetValue(Object o, long offset,
1776                                                     Class<?> valueType,
1777                                                     V expected,
1778                                                     V x) {
1779         return compareAndSetValue(o, offset, valueType, expected, x);
1780     }
1781 
1782     /**
1783      * Atomically updates Java variable to {@code x} if it is currently
1784      * holding {@code expected}.
1785      *
1786      * <p>This operation has memory semantics of a {@code volatile} read
1787      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1788      *
1789      * @return {@code true} if successful
1790      */
1791     @IntrinsicCandidate
1792     public final native boolean compareAndSetInt(Object o, long offset,
1793                                                  int expected,
1794                                                  int x);
1795 
1796     @IntrinsicCandidate
1797     public final native int compareAndExchangeInt(Object o, long offset,
1798                                                   int expected,
1799                                                   int x);
1800 
1801     @IntrinsicCandidate

2377     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2378                                                       long expected,
2379                                                       long x) {
2380         return compareAndSetLong(o, offset, expected, x);
2381     }
2382 
2383     @IntrinsicCandidate
2384     public final boolean weakCompareAndSetLong(Object o, long offset,
2385                                                long expected,
2386                                                long x) {
2387         return compareAndSetLong(o, offset, expected, x);
2388     }
2389 
2390     /**
2391      * Fetches a reference value from a given Java variable, with volatile
2392      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2393      */
2394     @IntrinsicCandidate
2395     public native Object getReferenceVolatile(Object o, long offset);
2396 
2397     /**
2398      * Global lock for atomic and volatile strength access to any value of
2399      * a primitive type.  This is a temporary workaround until better localized
2400      * atomic access mechanisms are supported for primitive types.
2401      */
2402     private static final Object valueLock = new Object();
2403 
2404     public final <V> Object getValueVolatile(Object base, long offset, Class<?> valueType) {
2405         synchronized (valueLock) {
2406             return getValue(base, offset, valueType);
2407         }
2408     }
2409 
2410     /**
2411      * Stores a reference value into a given Java variable, with
2412      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2413      */
2414     @IntrinsicCandidate
2415     public native void putReferenceVolatile(Object o, long offset, Object x);
2416 
2417     public final <V> void putValueVolatile(Object o, long offset, Class<?> valueType, V x) {
2418         synchronized (valueLock) {
2419             putValue(o, offset, valueType, x);
2420         }
2421     }
2422 
2423     /** Volatile version of {@link #getInt(Object, long)}  */
2424     @IntrinsicCandidate
2425     public native int     getIntVolatile(Object o, long offset);
2426 
2427     /** Volatile version of {@link #putInt(Object, long, int)}  */
2428     @IntrinsicCandidate
2429     public native void    putIntVolatile(Object o, long offset, int x);
2430 
2431     /** Volatile version of {@link #getBoolean(Object, long)}  */
2432     @IntrinsicCandidate
2433     public native boolean getBooleanVolatile(Object o, long offset);
2434 
2435     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2436     @IntrinsicCandidate
2437     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2438 
2439     /** Volatile version of {@link #getByte(Object, long)}  */
2440     @IntrinsicCandidate
2441     public native byte    getByteVolatile(Object o, long offset);
2442 

2475     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2476     @IntrinsicCandidate
2477     public native void    putFloatVolatile(Object o, long offset, float x);
2478 
2479     /** Volatile version of {@link #getDouble(Object, long)}  */
2480     @IntrinsicCandidate
2481     public native double  getDoubleVolatile(Object o, long offset);
2482 
2483     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2484     @IntrinsicCandidate
2485     public native void    putDoubleVolatile(Object o, long offset, double x);
2486 
2487 
2488 
2489     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2490     @IntrinsicCandidate
2491     public final Object getReferenceAcquire(Object o, long offset) {
2492         return getReferenceVolatile(o, offset);
2493     }
2494 
2495     public final <V> Object getValueAcquire(Object base, long offset, Class<?> valueType) {
2496         return getValueVolatile(base, offset, valueType);
2497     }
2498 
2499     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2500     @IntrinsicCandidate
2501     public final boolean getBooleanAcquire(Object o, long offset) {
2502         return getBooleanVolatile(o, offset);
2503     }
2504 
2505     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2506     @IntrinsicCandidate
2507     public final byte getByteAcquire(Object o, long offset) {
2508         return getByteVolatile(o, offset);
2509     }
2510 
2511     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2512     @IntrinsicCandidate
2513     public final short getShortAcquire(Object o, long offset) {
2514         return getShortVolatile(o, offset);
2515     }
2516 
2517     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2518     @IntrinsicCandidate

2543     public final double getDoubleAcquire(Object o, long offset) {
2544         return getDoubleVolatile(o, offset);
2545     }
2546 
2547     /*
2548      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2549      * that do not guarantee immediate visibility of the store to
2550      * other threads. This method is generally only useful if the
2551      * underlying field is a Java volatile (or if an array cell, one
2552      * that is otherwise only accessed using volatile accesses).
2553      *
2554      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2555      */
2556 
2557     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2558     @IntrinsicCandidate
2559     public final void putReferenceRelease(Object o, long offset, Object x) {
2560         putReferenceVolatile(o, offset, x);
2561     }
2562 
2563     public final <V> void putValueRelease(Object o, long offset, Class<?> valueType, V x) {
2564         putValueVolatile(o, offset, valueType, x);
2565     }
2566 
2567     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2568     @IntrinsicCandidate
2569     public final void putBooleanRelease(Object o, long offset, boolean x) {
2570         putBooleanVolatile(o, offset, x);
2571     }
2572 
2573     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2574     @IntrinsicCandidate
2575     public final void putByteRelease(Object o, long offset, byte x) {
2576         putByteVolatile(o, offset, x);
2577     }
2578 
2579     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2580     @IntrinsicCandidate
2581     public final void putShortRelease(Object o, long offset, short x) {
2582         putShortVolatile(o, offset, x);
2583     }
2584 
2585     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2586     @IntrinsicCandidate

2603     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2604     @IntrinsicCandidate
2605     public final void putLongRelease(Object o, long offset, long x) {
2606         putLongVolatile(o, offset, x);
2607     }
2608 
2609     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2610     @IntrinsicCandidate
2611     public final void putDoubleRelease(Object o, long offset, double x) {
2612         putDoubleVolatile(o, offset, x);
2613     }
2614 
2615     // ------------------------------ Opaque --------------------------------------
2616 
2617     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2618     @IntrinsicCandidate
2619     public final Object getReferenceOpaque(Object o, long offset) {
2620         return getReferenceVolatile(o, offset);
2621     }
2622 
2623     public final <V> Object getValueOpaque(Object base, long offset, Class<?> valueType) {
2624         return getValueVolatile(base, offset, valueType);
2625     }
2626 
2627     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2628     @IntrinsicCandidate
2629     public final boolean getBooleanOpaque(Object o, long offset) {
2630         return getBooleanVolatile(o, offset);
2631     }
2632 
2633     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2634     @IntrinsicCandidate
2635     public final byte getByteOpaque(Object o, long offset) {
2636         return getByteVolatile(o, offset);
2637     }
2638 
2639     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2640     @IntrinsicCandidate
2641     public final short getShortOpaque(Object o, long offset) {
2642         return getShortVolatile(o, offset);
2643     }
2644 
2645     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2646     @IntrinsicCandidate

2661     }
2662 
2663     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2664     @IntrinsicCandidate
2665     public final long getLongOpaque(Object o, long offset) {
2666         return getLongVolatile(o, offset);
2667     }
2668 
2669     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2670     @IntrinsicCandidate
2671     public final double getDoubleOpaque(Object o, long offset) {
2672         return getDoubleVolatile(o, offset);
2673     }
2674 
2675     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2676     @IntrinsicCandidate
2677     public final void putReferenceOpaque(Object o, long offset, Object x) {
2678         putReferenceVolatile(o, offset, x);
2679     }
2680 
2681     public final <V> void putValueOpaque(Object o, long offset, Class<?> valueType, V x) {
2682         putValueVolatile(o, offset, valueType, x);
2683     }
2684 
2685     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2686     @IntrinsicCandidate
2687     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2688         putBooleanVolatile(o, offset, x);
2689     }
2690 
2691     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2692     @IntrinsicCandidate
2693     public final void putByteOpaque(Object o, long offset, byte x) {
2694         putByteVolatile(o, offset, x);
2695     }
2696 
2697     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2698     @IntrinsicCandidate
2699     public final void putShortOpaque(Object o, long offset, short x) {
2700         putShortVolatile(o, offset, x);
2701     }
2702 
2703     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2704     @IntrinsicCandidate

3099     /**
3100      * Atomically exchanges the given reference value with the current
3101      * reference value of a field or array element within the given
3102      * object {@code o} at the given {@code offset}.
3103      *
3104      * @param o object/array to update the field/element in
3105      * @param offset field/element offset
3106      * @param newValue new value
3107      * @return the previous value
3108      * @since 1.8
3109      */
3110     @IntrinsicCandidate
3111     public final Object getAndSetReference(Object o, long offset, Object newValue) {
3112         Object v;
3113         do {
3114             v = getReferenceVolatile(o, offset);
3115         } while (!weakCompareAndSetReference(o, offset, v, newValue));
3116         return v;
3117     }
3118 
3119     @SuppressWarnings("unchecked")
3120     public final <V> Object getAndSetValue(Object o, long offset, Class<?> valueType, V newValue) {
3121         synchronized (valueLock) {
3122             Object oldValue = getValue(o, offset, valueType);
3123             putValue(o, offset, valueType, newValue);
3124             return oldValue;
3125         }
3126     }
3127 
3128     @ForceInline
3129     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3130         Object v;
3131         do {
3132             v = getReference(o, offset);
3133         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3134         return v;
3135     }
3136 
3137     @ForceInline
3138     public final <V> Object getAndSetValueRelease(Object o, long offset, Class<?> valueType, V newValue) {
3139         return getAndSetValue(o, offset, valueType, newValue);
3140     }
3141 
3142     @ForceInline
3143     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3144         Object v;
3145         do {
3146             v = getReferenceAcquire(o, offset);
3147         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3148         return v;
3149     }
3150 
3151     @ForceInline
3152     public final <V> Object getAndSetValueAcquire(Object o, long offset, Class<?> valueType, V newValue) {
3153         return getAndSetValue(o, offset, valueType, newValue);
3154     }
3155 
3156     @IntrinsicCandidate
3157     public final byte getAndSetByte(Object o, long offset, byte newValue) {
3158         byte v;
3159         do {
3160             v = getByteVolatile(o, offset);
3161         } while (!weakCompareAndSetByte(o, offset, v, newValue));
3162         return v;
3163     }
3164 
3165     @ForceInline
3166     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3167         byte v;
3168         do {
3169             v = getByte(o, offset);
3170         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3171         return v;
3172     }
3173 
3174     @ForceInline
3175     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

4192     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
4193     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
4194 
4195 
4196 
4197     private native long allocateMemory0(long bytes);
4198     private native long reallocateMemory0(long address, long bytes);
4199     private native void freeMemory0(long address);
4200     private native void setMemory0(Object o, long offset, long bytes, byte value);
4201     @IntrinsicCandidate
4202     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4203     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4204     private native long objectFieldOffset0(Field f);
4205     private native long objectFieldOffset1(Class<?> c, String name);
4206     private native long staticFieldOffset0(Field f);
4207     private native Object staticFieldBase0(Field f);
4208     private native boolean shouldBeInitialized0(Class<?> c);
4209     private native void ensureClassInitialized0(Class<?> c);
4210     private native int arrayBaseOffset0(Class<?> arrayClass);
4211     private native int arrayIndexScale0(Class<?> arrayClass);
4212     private native long getObjectSize0(Object o);
4213     private native int getLoadAverage0(double[] loadavg, int nelems);
4214 
4215 
4216     /**
4217      * Invokes the given direct byte buffer's cleaner, if any.
4218      *
4219      * @param directBuffer a direct byte buffer
4220      * @throws NullPointerException     if {@code directBuffer} is null
4221      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4222      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
4223      *                                  {@link java.nio.Buffer#duplicate duplicate}
4224      */
4225     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4226         if (!directBuffer.isDirect())
4227             throw new IllegalArgumentException("buffer is non-direct");
4228 
4229         DirectBuffer db = (DirectBuffer) directBuffer;
4230         if (db.attachment() != null)
4231             throw new IllegalArgumentException("duplicate or slice");
4232 
< prev index next >