< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 2000, 2025, 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,

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






















































































 185     /**
 186      * Fetches a reference value from a given Java variable.



 187      * @see #getInt(Object, long)
 188      */
 189     @IntrinsicCandidate
 190     public native Object getReference(Object o, long offset);
 191 
 192     /**
 193      * Stores a reference value into a given Java variable.


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























































































































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

1233      * allocation of a given array class.  However, arrays of "narrow" types
1234      * will generally not work properly with accessors like {@link
1235      * #getByte(Object, long)}, so the scale factor for such classes is reported
1236      * as zero.
1237      * <p>
1238      * The computation of the actual memory offset should always use {@code
1239      * long} arithmetic to avoid overflows.
1240      *
1241      * @see #arrayBaseOffset
1242      * @see #getInt(Object, long)
1243      * @see #putInt(Object, long, int)
1244      */
1245     public int arrayIndexScale(Class<?> arrayClass) {
1246         if (arrayClass == null) {
1247             throw new NullPointerException();
1248         }
1249 
1250         return arrayIndexScale0(arrayClass);
1251     }
1252 











1253 
1254     /** The value of {@code arrayIndexScale(boolean[].class)} */
1255     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1256             = theUnsafe.arrayIndexScale(boolean[].class);
1257 
1258     /** The value of {@code arrayIndexScale(byte[].class)} */
1259     public static final int ARRAY_BYTE_INDEX_SCALE
1260             = theUnsafe.arrayIndexScale(byte[].class);
1261 
1262     /** The value of {@code arrayIndexScale(short[].class)} */
1263     public static final int ARRAY_SHORT_INDEX_SCALE
1264             = theUnsafe.arrayIndexScale(short[].class);
1265 
1266     /** The value of {@code arrayIndexScale(char[].class)} */
1267     public static final int ARRAY_CHAR_INDEX_SCALE
1268             = theUnsafe.arrayIndexScale(char[].class);
1269 
1270     /** The value of {@code arrayIndexScale(int[].class)} */
1271     public static final int ARRAY_INT_INDEX_SCALE
1272             = theUnsafe.arrayIndexScale(int[].class);

1411        return null;
1412     }
1413 
1414     /** Throws the exception without telling the verifier. */
1415     public native void throwException(Throwable ee);
1416 
1417     /**
1418      * Atomically updates Java variable to {@code x} if it is currently
1419      * holding {@code expected}.
1420      *
1421      * <p>This operation has memory semantics of a {@code volatile} read
1422      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1423      *
1424      * @return {@code true} if successful
1425      */
1426     @IntrinsicCandidate
1427     public final native boolean compareAndSetReference(Object o, long offset,
1428                                                        Object expected,
1429                                                        Object x);
1430 













































1431     @IntrinsicCandidate
1432     public final native Object compareAndExchangeReference(Object o, long offset,
1433                                                            Object expected,
1434                                                            Object x);
1435 





































1436     @IntrinsicCandidate
1437     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1438                                                            Object expected,
1439                                                            Object x) {
1440         return compareAndExchangeReference(o, offset, expected, x);
1441     }
1442 
















1443     @IntrinsicCandidate
1444     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1445                                                            Object expected,
1446                                                            Object x) {
1447         return compareAndExchangeReference(o, offset, expected, x);
1448     }
1449 
















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




















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




















1464     @IntrinsicCandidate
1465     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1466                                                            Object expected,
1467                                                            Object x) {
1468         return compareAndSetReference(o, offset, expected, x);
1469     }
1470 




















1471     @IntrinsicCandidate
1472     public final boolean weakCompareAndSetReference(Object o, long offset,
1473                                                     Object expected,
1474                                                     Object x) {
1475         return compareAndSetReference(o, offset, expected, x);
1476     }
1477 




















1478     /**
1479      * Atomically updates Java variable to {@code x} if it is currently
1480      * holding {@code expected}.
1481      *
1482      * <p>This operation has memory semantics of a {@code volatile} read
1483      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1484      *
1485      * @return {@code true} if successful
1486      */
1487     @IntrinsicCandidate
1488     public final native boolean compareAndSetInt(Object o, long offset,
1489                                                  int expected,
1490                                                  int x);
1491 
1492     @IntrinsicCandidate
1493     public final native int compareAndExchangeInt(Object o, long offset,
1494                                                   int expected,
1495                                                   int x);
1496 
1497     @IntrinsicCandidate

2073     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2074                                                       long expected,
2075                                                       long x) {
2076         return compareAndSetLong(o, offset, expected, x);
2077     }
2078 
2079     @IntrinsicCandidate
2080     public final boolean weakCompareAndSetLong(Object o, long offset,
2081                                                long expected,
2082                                                long x) {
2083         return compareAndSetLong(o, offset, expected, x);
2084     }
2085 
2086     /**
2087      * Fetches a reference value from a given Java variable, with volatile
2088      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2089      */
2090     @IntrinsicCandidate
2091     public native Object getReferenceVolatile(Object o, long offset);
2092 








2093     /**
2094      * Stores a reference value into a given Java variable, with
2095      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2096      */
2097     @IntrinsicCandidate
2098     public native void putReferenceVolatile(Object o, long offset, Object x);
2099 







2100     /** Volatile version of {@link #getInt(Object, long)}  */
2101     @IntrinsicCandidate
2102     public native int     getIntVolatile(Object o, long offset);
2103 
2104     /** Volatile version of {@link #putInt(Object, long, int)}  */
2105     @IntrinsicCandidate
2106     public native void    putIntVolatile(Object o, long offset, int x);
2107 
2108     /** Volatile version of {@link #getBoolean(Object, long)}  */
2109     @IntrinsicCandidate
2110     public native boolean getBooleanVolatile(Object o, long offset);
2111 
2112     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2113     @IntrinsicCandidate
2114     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2115 
2116     /** Volatile version of {@link #getByte(Object, long)}  */
2117     @IntrinsicCandidate
2118     public native byte    getByteVolatile(Object o, long offset);
2119 

2152     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2153     @IntrinsicCandidate
2154     public native void    putFloatVolatile(Object o, long offset, float x);
2155 
2156     /** Volatile version of {@link #getDouble(Object, long)}  */
2157     @IntrinsicCandidate
2158     public native double  getDoubleVolatile(Object o, long offset);
2159 
2160     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2161     @IntrinsicCandidate
2162     public native void    putDoubleVolatile(Object o, long offset, double x);
2163 
2164 
2165 
2166     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2167     @IntrinsicCandidate
2168     public final Object getReferenceAcquire(Object o, long offset) {
2169         return getReferenceVolatile(o, offset);
2170     }
2171 








2172     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2173     @IntrinsicCandidate
2174     public final boolean getBooleanAcquire(Object o, long offset) {
2175         return getBooleanVolatile(o, offset);
2176     }
2177 
2178     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2179     @IntrinsicCandidate
2180     public final byte getByteAcquire(Object o, long offset) {
2181         return getByteVolatile(o, offset);
2182     }
2183 
2184     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2185     @IntrinsicCandidate
2186     public final short getShortAcquire(Object o, long offset) {
2187         return getShortVolatile(o, offset);
2188     }
2189 
2190     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2191     @IntrinsicCandidate

2216     public final double getDoubleAcquire(Object o, long offset) {
2217         return getDoubleVolatile(o, offset);
2218     }
2219 
2220     /*
2221      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2222      * that do not guarantee immediate visibility of the store to
2223      * other threads. This method is generally only useful if the
2224      * underlying field is a Java volatile (or if an array cell, one
2225      * that is otherwise only accessed using volatile accesses).
2226      *
2227      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2228      */
2229 
2230     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2231     @IntrinsicCandidate
2232     public final void putReferenceRelease(Object o, long offset, Object x) {
2233         putReferenceVolatile(o, offset, x);
2234     }
2235 







2236     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2237     @IntrinsicCandidate
2238     public final void putBooleanRelease(Object o, long offset, boolean x) {
2239         putBooleanVolatile(o, offset, x);
2240     }
2241 
2242     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2243     @IntrinsicCandidate
2244     public final void putByteRelease(Object o, long offset, byte x) {
2245         putByteVolatile(o, offset, x);
2246     }
2247 
2248     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2249     @IntrinsicCandidate
2250     public final void putShortRelease(Object o, long offset, short x) {
2251         putShortVolatile(o, offset, x);
2252     }
2253 
2254     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2255     @IntrinsicCandidate

2272     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2273     @IntrinsicCandidate
2274     public final void putLongRelease(Object o, long offset, long x) {
2275         putLongVolatile(o, offset, x);
2276     }
2277 
2278     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2279     @IntrinsicCandidate
2280     public final void putDoubleRelease(Object o, long offset, double x) {
2281         putDoubleVolatile(o, offset, x);
2282     }
2283 
2284     // ------------------------------ Opaque --------------------------------------
2285 
2286     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2287     @IntrinsicCandidate
2288     public final Object getReferenceOpaque(Object o, long offset) {
2289         return getReferenceVolatile(o, offset);
2290     }
2291 






2292     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2293     @IntrinsicCandidate
2294     public final boolean getBooleanOpaque(Object o, long offset) {
2295         return getBooleanVolatile(o, offset);
2296     }
2297 
2298     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2299     @IntrinsicCandidate
2300     public final byte getByteOpaque(Object o, long offset) {
2301         return getByteVolatile(o, offset);
2302     }
2303 
2304     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2305     @IntrinsicCandidate
2306     public final short getShortOpaque(Object o, long offset) {
2307         return getShortVolatile(o, offset);
2308     }
2309 
2310     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2311     @IntrinsicCandidate

2326     }
2327 
2328     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2329     @IntrinsicCandidate
2330     public final long getLongOpaque(Object o, long offset) {
2331         return getLongVolatile(o, offset);
2332     }
2333 
2334     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2335     @IntrinsicCandidate
2336     public final double getDoubleOpaque(Object o, long offset) {
2337         return getDoubleVolatile(o, offset);
2338     }
2339 
2340     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2341     @IntrinsicCandidate
2342     public final void putReferenceOpaque(Object o, long offset, Object x) {
2343         putReferenceVolatile(o, offset, x);
2344     }
2345 






2346     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2347     @IntrinsicCandidate
2348     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2349         putBooleanVolatile(o, offset, x);
2350     }
2351 
2352     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2353     @IntrinsicCandidate
2354     public final void putByteOpaque(Object o, long offset, byte x) {
2355         putByteVolatile(o, offset, x);
2356     }
2357 
2358     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2359     @IntrinsicCandidate
2360     public final void putShortOpaque(Object o, long offset, short x) {
2361         putShortVolatile(o, offset, x);
2362     }
2363 
2364     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2365     @IntrinsicCandidate

2374     }
2375 
2376     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2377     @IntrinsicCandidate
2378     public final void putFloatOpaque(Object o, long offset, float x) {
2379         putFloatVolatile(o, offset, x);
2380     }
2381 
2382     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2383     @IntrinsicCandidate
2384     public final void putLongOpaque(Object o, long offset, long x) {
2385         putLongVolatile(o, offset, x);
2386     }
2387 
2388     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2389     @IntrinsicCandidate
2390     public final void putDoubleOpaque(Object o, long offset, double x) {
2391         putDoubleVolatile(o, offset, x);
2392     }
2393 








































2394     /**
2395      * Unblocks the given thread blocked on {@code park}, or, if it is
2396      * not blocked, causes the subsequent call to {@code park} not to
2397      * block.  Note: this operation is "unsafe" solely because the
2398      * caller must somehow ensure that the thread has not been
2399      * destroyed. Nothing special is usually required to ensure this
2400      * when called from Java (in which there will ordinarily be a live
2401      * reference to the thread) but this is not nearly-automatically
2402      * so when calling from native code.
2403      *
2404      * @param thread the thread to unpark.
2405      */
2406     @IntrinsicCandidate
2407     public native void unpark(Object thread);
2408 
2409     /**
2410      * Blocks current thread, returning when a balancing
2411      * {@code unpark} occurs, or a balancing {@code unpark} has
2412      * already occurred, or the thread is interrupted, or, if not
2413      * absolute and time is not zero, the given time nanoseconds have

2760     /**
2761      * Atomically exchanges the given reference value with the current
2762      * reference value of a field or array element within the given
2763      * object {@code o} at the given {@code offset}.
2764      *
2765      * @param o object/array to update the field/element in
2766      * @param offset field/element offset
2767      * @param newValue new value
2768      * @return the previous value
2769      * @since 1.8
2770      */
2771     @IntrinsicCandidate
2772     public final Object getAndSetReference(Object o, long offset, Object newValue) {
2773         Object v;
2774         do {
2775             v = getReferenceVolatile(o, offset);
2776         } while (!weakCompareAndSetReference(o, offset, v, newValue));
2777         return v;
2778     }
2779 


















2780     @ForceInline
2781     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2782         Object v;
2783         do {
2784             v = getReference(o, offset);
2785         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2786         return v;
2787     }
2788 










2789     @ForceInline
2790     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2791         Object v;
2792         do {
2793             v = getReferenceAcquire(o, offset);
2794         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2795         return v;
2796     }
2797 










2798     @IntrinsicCandidate
2799     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2800         byte v;
2801         do {
2802             v = getByteVolatile(o, offset);
2803         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2804         return v;
2805     }
2806 
2807     @ForceInline
2808     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2809         byte v;
2810         do {
2811             v = getByte(o, offset);
2812         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2813         return v;
2814     }
2815 
2816     @ForceInline
2817     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

3835     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
3836 
3837 
3838 
3839     private native long allocateMemory0(long bytes);
3840     private native long reallocateMemory0(long address, long bytes);
3841     private native void freeMemory0(long address);
3842     @IntrinsicCandidate
3843     private native void setMemory0(Object o, long offset, long bytes, byte value);
3844     @IntrinsicCandidate
3845     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3846     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3847     private native long objectFieldOffset0(Field f);
3848     private native long objectFieldOffset1(Class<?> c, String name);
3849     private native long staticFieldOffset0(Field f);
3850     private native Object staticFieldBase0(Field f);
3851     private native boolean shouldBeInitialized0(Class<?> c);
3852     private native void ensureClassInitialized0(Class<?> c);
3853     private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
3854     private native int arrayIndexScale0(Class<?> arrayClass);

3855     private native int getLoadAverage0(double[] loadavg, int nelems);
3856 
3857 
3858     /**
3859      * Invokes the given direct byte buffer's cleaner, if any.
3860      *
3861      * @param directBuffer a direct byte buffer
3862      * @throws NullPointerException     if {@code directBuffer} is null
3863      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3864      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
3865      *                                  {@link java.nio.Buffer#duplicate duplicate}
3866      */
3867     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3868         if (!directBuffer.isDirect())
3869             throw new IllegalArgumentException("buffer is non-direct");
3870 
3871         DirectBuffer db = (DirectBuffer) directBuffer;
3872         if (db.attachment() != null)
3873             throw new IllegalArgumentException("duplicate or slice");
3874 

   1 /*
   2  * Copyright (c) 2000, 2024, 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.ValueClass;
  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,

 166      * The first two parameters are interpreted exactly as with
 167      * {@link #getInt(Object, long)} to refer to a specific
 168      * Java variable (field or array element).  The given value
 169      * is stored into that variable.
 170      * <p>
 171      * The variable must be of the same type as the method
 172      * parameter {@code x}.
 173      *
 174      * @param o Java heap object in which the variable resides, if any, else
 175      *        null
 176      * @param offset indication of where the variable resides in a Java heap
 177      *        object, if any, else a memory address locating the variable
 178      *        statically
 179      * @param x the value to store into the indicated Java variable
 180      * @throws RuntimeException No defined exceptions are thrown, not even
 181      *         {@link NullPointerException}
 182      */
 183     @IntrinsicCandidate
 184     public native void putInt(Object o, long offset, int x);
 185 
 186 
 187     /**
 188      * Returns true if the given field is flattened.
 189      */
 190     public boolean isFlatField(Field f) {
 191         if (f == null) {
 192             throw new NullPointerException();
 193         }
 194         return isFlatField0(f);
 195     }
 196 
 197     private native boolean isFlatField0(Object o);
 198 
 199     /* Returns true if the given field has a null marker
 200      * <p>
 201      * Nullable flat fields are stored in a flattened representation
 202      * and have an associated null marker to indicate if the the field value is
 203      * null or the one stored with the flat representation
 204      */
 205 
 206     public boolean hasNullMarker(Field f) {
 207         if (f == null) {
 208             throw new NullPointerException();
 209         }
 210         return hasNullMarker0(f);
 211     }
 212 
 213     private native boolean hasNullMarker0(Object o);
 214 
 215     /* Returns the offset of the null marker of the field,
 216     * or -1 if the field doesn't have a null marker
 217     */
 218 
 219     public int nullMarkerOffset(Field f) {
 220         if (f == null) {
 221             throw new NullPointerException();
 222         }
 223         return nullMarkerOffset0(f);
 224     }
 225 
 226     private native int nullMarkerOffset0(Object o);
 227 
 228     public static final int NON_FLAT_LAYOUT = 0;
 229 
 230     /* Reports the kind of layout used for an element in the storage
 231      * allocation of the given array. Do not expect to perform any logic
 232      * or layout control with this value, it is just an opaque token
 233      * used for performance reasons.
 234      *
 235      * A layout of 0 indicates this array is not flat.
 236      */
 237     public int arrayLayout(Class<?> arrayClass) {
 238         if (arrayClass == null) {
 239             throw new NullPointerException();
 240         }
 241         return arrayLayout0(arrayClass);
 242     }
 243 
 244     private native int arrayLayout0(Object o);
 245 
 246 
 247     /* Reports the kind of layout used for a given field in the storage
 248      * allocation of its class.  Do not expect to perform any logic
 249      * or layout control with this value, it is just an opaque token
 250      * used for performance reasons.
 251      *
 252      * A layout of 0 indicates this field is not flat.
 253      */
 254     public int fieldLayout(Field f) {
 255         if (f == null) {
 256             throw new NullPointerException();
 257         }
 258         return fieldLayout0(f);
 259     }
 260 
 261     private native int fieldLayout0(Object o);
 262 
 263     public native Object[] newSpecialArray(Class<?> componentType,
 264                                                   int length, int layoutKind);
 265 
 266     /**
 267      * Returns true if the given class is a flattened array.
 268      */
 269     @IntrinsicCandidate
 270     public native boolean isFlatArray(Class<?> arrayClass);
 271 
 272     /**
 273      * Fetches a reference value from a given Java variable.
 274      * This method can return a reference to either an object or value
 275      * or a null reference.
 276      *
 277      * @see #getInt(Object, long)
 278      */
 279     @IntrinsicCandidate
 280     public native Object getReference(Object o, long offset);
 281 
 282     /**
 283      * Stores a reference value into a given Java variable.
 284      * This method can store a reference to either an object or value
 285      * or a null reference.
 286      * <p>
 287      * Unless the reference {@code x} being stored is either null
 288      * or matches the field type, the results are undefined.
 289      * If the reference {@code o} is non-null, card marks or
 290      * other store barriers for that object (if the VM requires them)
 291      * are updated.
 292      * @see #putInt(Object, long, int)
 293      */
 294     @IntrinsicCandidate
 295     public native void putReference(Object o, long offset, Object x);
 296 
 297     /**
 298      * Fetches a value of type {@code <V>} from a given Java variable.
 299      * More specifically, fetches a field or array element within the given
 300      * {@code o} object at the given offset, or (if {@code o} is null)
 301      * from the memory address whose numerical value is the given offset.
 302      *
 303      * @param o Java heap object in which the variable resides, if any, else
 304      *        null
 305      * @param offset indication of where the variable resides in a Java heap
 306      *        object, if any, else a memory address locating the variable
 307      *        statically
 308      * @param valueType value type
 309      * @param <V> the type of a value
 310      * @return the value fetched from the indicated Java variable
 311      * @throws RuntimeException No defined exceptions are thrown, not even
 312      *         {@link NullPointerException}
 313      */
 314     @IntrinsicCandidate
 315     public native <V> V getValue(Object o, long offset, Class<?> valueType);
 316 
 317     /**
 318      * Fetches a value of type {@code <V>} from a given Java variable.
 319      * More specifically, fetches a field or array element within the given
 320      * {@code o} object at the given offset, or (if {@code o} is null)
 321      * from the memory address whose numerical value is the given offset.
 322      *
 323      * @param o Java heap object in which the variable resides, if any, else
 324      *        null
 325      * @param offset indication of where the variable resides in a Java heap
 326      *        object, if any, else a memory address locating the variable
 327      *        statically
 328      * @param layoutKind opaque value used by the VM to know the layout
 329      *        the field or array element. This value must be retrieved with
 330      *        {@link #fieldLayout} or {@link #arrayLayout}.
 331      * @param valueType value type
 332      * @param <V> the type of a value
 333      * @return the value fetched from the indicated Java variable
 334      * @throws RuntimeException No defined exceptions are thrown, not even
 335      *         {@link NullPointerException}
 336      */
 337     public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
 338 
 339 
 340     /**
 341      * Stores the given value into a given Java variable.
 342      *
 343      * Unless the reference {@code o} being stored is either null
 344      * or matches the field type, the results are undefined.
 345      *
 346      * @param o Java heap object in which the variable resides, if any, else
 347      *        null
 348      * @param offset indication of where the variable resides in a Java heap
 349      *        object, if any, else a memory address locating the variable
 350      *        statically
 351      * @param valueType value type
 352      * @param v the value to store into the indicated Java variable
 353      * @param <V> the type of a value
 354      * @throws RuntimeException No defined exceptions are thrown, not even
 355      *         {@link NullPointerException}
 356      */
 357     @IntrinsicCandidate
 358     public native <V> void putValue(Object o, long offset, Class<?> valueType, V v);
 359 
 360     /**
 361      * Stores the given value into a given Java variable.
 362      *
 363      * Unless the reference {@code o} being stored is either null
 364      * or matches the field type, the results are undefined.
 365      *
 366      * @param o Java heap object in which the variable resides, if any, else
 367      *        null
 368      * @param offset indication of where the variable resides in a Java heap
 369      *        object, if any, else a memory address locating the variable
 370      *        statically
 371      * @param layoutKind opaque value used by the VM to know the layout
 372      *        the field or array element. This value must be retrieved with
 373      *        {@link #fieldLayout} or {@link #arrayLayout}.
 374      * @param valueType value type
 375      * @param v the value to store into the indicated Java variable
 376      * @param <V> the type of a value
 377      * @throws RuntimeException No defined exceptions are thrown, not even
 378      *         {@link NullPointerException}
 379      */
 380     public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
 381 
 382 
 383     /**
 384      * Returns an uninitialized default instance of the given value class.
 385      */
 386     public native <V> V uninitializedDefaultValue(Class<?> type);
 387 
 388     /**
 389      * Returns an object instance with a private buffered value whose layout
 390      * and contents is exactly the given value instance.  The return object
 391      * is in the larval state that can be updated using the unsafe put operation.
 392      *
 393      * @param value a value instance
 394      * @param <V> the type of the given value instance
 395      */
 396     @IntrinsicCandidate
 397     public native <V> V makePrivateBuffer(V value);
 398 
 399     /**
 400      * Exits the larval state and returns a value instance.
 401      *
 402      * @param value a value instance
 403      * @param <V> the type of the given value instance
 404      */
 405     @IntrinsicCandidate
 406     public native <V> V finishPrivateBuffer(V value);
 407 
 408     /**
 409      * Returns the header size of the given value type.
 410      *
 411      * @param valueType value type
 412      * @return the header size of the value type
 413      */
 414     public native <V> long valueHeaderSize(Class<V> valueType);
 415 
 416     /** @see #getInt(Object, long) */
 417     @IntrinsicCandidate
 418     public native boolean getBoolean(Object o, long offset);
 419 
 420     /** @see #putInt(Object, long, int) */
 421     @IntrinsicCandidate
 422     public native void    putBoolean(Object o, long offset, boolean x);
 423 
 424     /** @see #getInt(Object, long) */
 425     @IntrinsicCandidate
 426     public native byte    getByte(Object o, long offset);
 427 
 428     /** @see #putInt(Object, long, int) */
 429     @IntrinsicCandidate
 430     public native void    putByte(Object o, long offset, byte x);
 431 
 432     /** @see #getInt(Object, long) */
 433     @IntrinsicCandidate
 434     public native short   getShort(Object o, long offset);
 435 

1444      * allocation of a given array class.  However, arrays of "narrow" types
1445      * will generally not work properly with accessors like {@link
1446      * #getByte(Object, long)}, so the scale factor for such classes is reported
1447      * as zero.
1448      * <p>
1449      * The computation of the actual memory offset should always use {@code
1450      * long} arithmetic to avoid overflows.
1451      *
1452      * @see #arrayBaseOffset
1453      * @see #getInt(Object, long)
1454      * @see #putInt(Object, long, int)
1455      */
1456     public int arrayIndexScale(Class<?> arrayClass) {
1457         if (arrayClass == null) {
1458             throw new NullPointerException();
1459         }
1460 
1461         return arrayIndexScale0(arrayClass);
1462     }
1463 
1464     /**
1465      * Return the size of the object in the heap.
1466      * @param o an object
1467      * @return the objects's size
1468      * @since Valhalla
1469      */
1470     public long getObjectSize(Object o) {
1471         if (o == null)
1472             throw new NullPointerException();
1473         return getObjectSize0(o);
1474     }
1475 
1476     /** The value of {@code arrayIndexScale(boolean[].class)} */
1477     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1478             = theUnsafe.arrayIndexScale(boolean[].class);
1479 
1480     /** The value of {@code arrayIndexScale(byte[].class)} */
1481     public static final int ARRAY_BYTE_INDEX_SCALE
1482             = theUnsafe.arrayIndexScale(byte[].class);
1483 
1484     /** The value of {@code arrayIndexScale(short[].class)} */
1485     public static final int ARRAY_SHORT_INDEX_SCALE
1486             = theUnsafe.arrayIndexScale(short[].class);
1487 
1488     /** The value of {@code arrayIndexScale(char[].class)} */
1489     public static final int ARRAY_CHAR_INDEX_SCALE
1490             = theUnsafe.arrayIndexScale(char[].class);
1491 
1492     /** The value of {@code arrayIndexScale(int[].class)} */
1493     public static final int ARRAY_INT_INDEX_SCALE
1494             = theUnsafe.arrayIndexScale(int[].class);

1633        return null;
1634     }
1635 
1636     /** Throws the exception without telling the verifier. */
1637     public native void throwException(Throwable ee);
1638 
1639     /**
1640      * Atomically updates Java variable to {@code x} if it is currently
1641      * holding {@code expected}.
1642      *
1643      * <p>This operation has memory semantics of a {@code volatile} read
1644      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1645      *
1646      * @return {@code true} if successful
1647      */
1648     @IntrinsicCandidate
1649     public final native boolean compareAndSetReference(Object o, long offset,
1650                                                        Object expected,
1651                                                        Object x);
1652 
1653     private final boolean isValueObject(Object o) {
1654         return o != null && o.getClass().isValue();
1655     }
1656 
1657     /*
1658      * For value type, CAS should do substitutability test as opposed
1659      * to two pointers comparison.
1660      */
1661     @ForceInline
1662     public final <V> boolean compareAndSetReference(Object o, long offset,
1663                                                     Class<?> type,
1664                                                     V expected,
1665                                                     V x) {
1666         if (type.isValue() || isValueObject(expected)) {
1667             while (true) {
1668                 Object witness = getReferenceVolatile(o, offset);
1669                 if (witness != expected) {
1670                     return false;
1671                 }
1672                 if (compareAndSetReference(o, offset, witness, x)) {
1673                     return true;
1674                 }
1675             }
1676         } else {
1677             return compareAndSetReference(o, offset, expected, x);
1678         }
1679     }
1680 
1681     @ForceInline
1682     public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1683                                                 int layout,
1684                                                 Class<?> valueType,
1685                                                 V expected,
1686                                                 V x) {
1687         while (true) {
1688             Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1689             if (witness != expected) {
1690                 return false;
1691             }
1692             if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1693                 return true;
1694             }
1695         }
1696     }
1697 
1698     @IntrinsicCandidate
1699     public final native Object compareAndExchangeReference(Object o, long offset,
1700                                                            Object expected,
1701                                                            Object x);
1702 
1703     @ForceInline
1704     public final <V> Object compareAndExchangeReference(Object o, long offset,
1705                                                         Class<?> valueType,
1706                                                         V expected,
1707                                                         V x) {
1708         if (valueType.isValue() || isValueObject(expected)) {
1709             while (true) {
1710                 Object witness = getReferenceVolatile(o, offset);
1711                 if (witness != expected) {
1712                     return witness;
1713                 }
1714                 if (compareAndSetReference(o, offset, witness, x)) {
1715                     return witness;
1716                 }
1717             }
1718         } else {
1719             return compareAndExchangeReference(o, offset, expected, x);
1720         }
1721     }
1722 
1723     @ForceInline
1724     public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1725                                                     int layout,
1726                                                     Class<?> valueType,
1727                                                     V expected,
1728                                                     V x) {
1729         while (true) {
1730             Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1731             if (witness != expected) {
1732                 return witness;
1733             }
1734             if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1735                 return witness;
1736             }
1737         }
1738     }
1739 
1740     @IntrinsicCandidate
1741     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1742                                                            Object expected,
1743                                                            Object x) {
1744         return compareAndExchangeReference(o, offset, expected, x);
1745     }
1746 
1747     public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1748                                                                Class<?> valueType,
1749                                                                V expected,
1750                                                                V x) {
1751         return compareAndExchangeReference(o, offset, valueType, expected, x);
1752     }
1753 
1754     @ForceInline
1755     public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1756                                                            int layout,
1757                                                            Class<?> valueType,
1758                                                            V expected,
1759                                                            V x) {
1760         return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1761     }
1762 
1763     @IntrinsicCandidate
1764     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1765                                                            Object expected,
1766                                                            Object x) {
1767         return compareAndExchangeReference(o, offset, expected, x);
1768     }
1769 
1770     public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1771                                                                Class<?> valueType,
1772                                                                V expected,
1773                                                                V x) {
1774         return compareAndExchangeReference(o, offset, valueType, expected, x);
1775     }
1776 
1777     @ForceInline
1778     public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1779                                                            int layout,
1780                                                            Class<?> valueType,
1781                                                            V expected,
1782                                                            V x) {
1783         return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1784     }
1785 
1786     @IntrinsicCandidate
1787     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1788                                                          Object expected,
1789                                                          Object x) {
1790         return compareAndSetReference(o, offset, expected, x);
1791     }
1792 
1793     public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1794                                                              Class<?> valueType,
1795                                                              V expected,
1796                                                              V x) {
1797         if (valueType.isValue() || isValueObject(expected)) {
1798             return compareAndSetReference(o, offset, valueType, expected, x);
1799         } else {
1800             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1801         }
1802     }
1803 
1804     @ForceInline
1805     public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1806                                                          int layout,
1807                                                          Class<?> valueType,
1808                                                          V expected,
1809                                                          V x) {
1810         return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1811     }
1812 
1813     @IntrinsicCandidate
1814     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1815                                                            Object expected,
1816                                                            Object x) {
1817         return compareAndSetReference(o, offset, expected, x);
1818     }
1819 
1820     public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1821                                                                Class<?> valueType,
1822                                                                V expected,
1823                                                                V x) {
1824         if (valueType.isValue() || isValueObject(expected)) {
1825             return compareAndSetReference(o, offset, valueType, expected, x);
1826         } else {
1827             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1828         }
1829     }
1830 
1831     @ForceInline
1832     public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1833                                                            int layout,
1834                                                            Class<?> valueType,
1835                                                            V expected,
1836                                                            V x) {
1837         return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1838     }
1839 
1840     @IntrinsicCandidate
1841     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1842                                                            Object expected,
1843                                                            Object x) {
1844         return compareAndSetReference(o, offset, expected, x);
1845     }
1846 
1847     public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1848                                                                Class<?> valueType,
1849                                                                V expected,
1850                                                                V x) {
1851         if (valueType.isValue() || isValueObject(expected)) {
1852             return compareAndSetReference(o, offset, valueType, expected, x);
1853         } else {
1854             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1855         }
1856     }
1857 
1858     @ForceInline
1859     public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1860                                                            int layout,
1861                                                            Class<?> valueType,
1862                                                            V expected,
1863                                                            V x) {
1864         return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1865     }
1866 
1867     @IntrinsicCandidate
1868     public final boolean weakCompareAndSetReference(Object o, long offset,
1869                                                     Object expected,
1870                                                     Object x) {
1871         return compareAndSetReference(o, offset, expected, x);
1872     }
1873 
1874     public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1875                                                         Class<?> valueType,
1876                                                         V expected,
1877                                                         V x) {
1878         if (valueType.isValue() || isValueObject(expected)) {
1879             return compareAndSetReference(o, offset, valueType, expected, x);
1880         } else {
1881             return weakCompareAndSetReferencePlain(o, offset, expected, x);
1882         }
1883     }
1884 
1885     @ForceInline
1886     public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1887                                                     int layout,
1888                                                     Class<?> valueType,
1889                                                     V expected,
1890                                                     V x) {
1891         return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1892     }
1893 
1894     /**
1895      * Atomically updates Java variable to {@code x} if it is currently
1896      * holding {@code expected}.
1897      *
1898      * <p>This operation has memory semantics of a {@code volatile} read
1899      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1900      *
1901      * @return {@code true} if successful
1902      */
1903     @IntrinsicCandidate
1904     public final native boolean compareAndSetInt(Object o, long offset,
1905                                                  int expected,
1906                                                  int x);
1907 
1908     @IntrinsicCandidate
1909     public final native int compareAndExchangeInt(Object o, long offset,
1910                                                   int expected,
1911                                                   int x);
1912 
1913     @IntrinsicCandidate

2489     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2490                                                       long expected,
2491                                                       long x) {
2492         return compareAndSetLong(o, offset, expected, x);
2493     }
2494 
2495     @IntrinsicCandidate
2496     public final boolean weakCompareAndSetLong(Object o, long offset,
2497                                                long expected,
2498                                                long x) {
2499         return compareAndSetLong(o, offset, expected, x);
2500     }
2501 
2502     /**
2503      * Fetches a reference value from a given Java variable, with volatile
2504      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2505      */
2506     @IntrinsicCandidate
2507     public native Object getReferenceVolatile(Object o, long offset);
2508 
2509     @ForceInline
2510     public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2511         // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2512         Object res = getFlatValue(o, offset, layout, valueType);
2513         fullFence();
2514         return res;
2515     }
2516 
2517     /**
2518      * Stores a reference value into a given Java variable, with
2519      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2520      */
2521     @IntrinsicCandidate
2522     public native void putReferenceVolatile(Object o, long offset, Object x);
2523 
2524     @ForceInline
2525     public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2526         // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2527         putFlatValueRelease(o, offset, layout, valueType, x);
2528         fullFence();
2529     }
2530 
2531     /** Volatile version of {@link #getInt(Object, long)}  */
2532     @IntrinsicCandidate
2533     public native int     getIntVolatile(Object o, long offset);
2534 
2535     /** Volatile version of {@link #putInt(Object, long, int)}  */
2536     @IntrinsicCandidate
2537     public native void    putIntVolatile(Object o, long offset, int x);
2538 
2539     /** Volatile version of {@link #getBoolean(Object, long)}  */
2540     @IntrinsicCandidate
2541     public native boolean getBooleanVolatile(Object o, long offset);
2542 
2543     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2544     @IntrinsicCandidate
2545     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2546 
2547     /** Volatile version of {@link #getByte(Object, long)}  */
2548     @IntrinsicCandidate
2549     public native byte    getByteVolatile(Object o, long offset);
2550 

2583     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2584     @IntrinsicCandidate
2585     public native void    putFloatVolatile(Object o, long offset, float x);
2586 
2587     /** Volatile version of {@link #getDouble(Object, long)}  */
2588     @IntrinsicCandidate
2589     public native double  getDoubleVolatile(Object o, long offset);
2590 
2591     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2592     @IntrinsicCandidate
2593     public native void    putDoubleVolatile(Object o, long offset, double x);
2594 
2595 
2596 
2597     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2598     @IntrinsicCandidate
2599     public final Object getReferenceAcquire(Object o, long offset) {
2600         return getReferenceVolatile(o, offset);
2601     }
2602 
2603     @ForceInline
2604     public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2605         // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2606         Object res = getFlatValue(o, offset, layout, valueType);
2607         loadFence();
2608         return res;
2609     }
2610 
2611     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2612     @IntrinsicCandidate
2613     public final boolean getBooleanAcquire(Object o, long offset) {
2614         return getBooleanVolatile(o, offset);
2615     }
2616 
2617     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2618     @IntrinsicCandidate
2619     public final byte getByteAcquire(Object o, long offset) {
2620         return getByteVolatile(o, offset);
2621     }
2622 
2623     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2624     @IntrinsicCandidate
2625     public final short getShortAcquire(Object o, long offset) {
2626         return getShortVolatile(o, offset);
2627     }
2628 
2629     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2630     @IntrinsicCandidate

2655     public final double getDoubleAcquire(Object o, long offset) {
2656         return getDoubleVolatile(o, offset);
2657     }
2658 
2659     /*
2660      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2661      * that do not guarantee immediate visibility of the store to
2662      * other threads. This method is generally only useful if the
2663      * underlying field is a Java volatile (or if an array cell, one
2664      * that is otherwise only accessed using volatile accesses).
2665      *
2666      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2667      */
2668 
2669     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2670     @IntrinsicCandidate
2671     public final void putReferenceRelease(Object o, long offset, Object x) {
2672         putReferenceVolatile(o, offset, x);
2673     }
2674 
2675     @ForceInline
2676     public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2677         // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2678         storeFence();
2679         putFlatValue(o, offset, layout, valueType, x);
2680     }
2681 
2682     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2683     @IntrinsicCandidate
2684     public final void putBooleanRelease(Object o, long offset, boolean x) {
2685         putBooleanVolatile(o, offset, x);
2686     }
2687 
2688     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2689     @IntrinsicCandidate
2690     public final void putByteRelease(Object o, long offset, byte x) {
2691         putByteVolatile(o, offset, x);
2692     }
2693 
2694     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2695     @IntrinsicCandidate
2696     public final void putShortRelease(Object o, long offset, short x) {
2697         putShortVolatile(o, offset, x);
2698     }
2699 
2700     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2701     @IntrinsicCandidate

2718     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2719     @IntrinsicCandidate
2720     public final void putLongRelease(Object o, long offset, long x) {
2721         putLongVolatile(o, offset, x);
2722     }
2723 
2724     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2725     @IntrinsicCandidate
2726     public final void putDoubleRelease(Object o, long offset, double x) {
2727         putDoubleVolatile(o, offset, x);
2728     }
2729 
2730     // ------------------------------ Opaque --------------------------------------
2731 
2732     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2733     @IntrinsicCandidate
2734     public final Object getReferenceOpaque(Object o, long offset) {
2735         return getReferenceVolatile(o, offset);
2736     }
2737 
2738     @ForceInline
2739     public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2740         // this is stronger than opaque semantics
2741         return getFlatValueAcquire(o, offset, layout, valueType);
2742     }
2743 
2744     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2745     @IntrinsicCandidate
2746     public final boolean getBooleanOpaque(Object o, long offset) {
2747         return getBooleanVolatile(o, offset);
2748     }
2749 
2750     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2751     @IntrinsicCandidate
2752     public final byte getByteOpaque(Object o, long offset) {
2753         return getByteVolatile(o, offset);
2754     }
2755 
2756     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2757     @IntrinsicCandidate
2758     public final short getShortOpaque(Object o, long offset) {
2759         return getShortVolatile(o, offset);
2760     }
2761 
2762     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2763     @IntrinsicCandidate

2778     }
2779 
2780     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2781     @IntrinsicCandidate
2782     public final long getLongOpaque(Object o, long offset) {
2783         return getLongVolatile(o, offset);
2784     }
2785 
2786     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2787     @IntrinsicCandidate
2788     public final double getDoubleOpaque(Object o, long offset) {
2789         return getDoubleVolatile(o, offset);
2790     }
2791 
2792     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2793     @IntrinsicCandidate
2794     public final void putReferenceOpaque(Object o, long offset, Object x) {
2795         putReferenceVolatile(o, offset, x);
2796     }
2797 
2798     @ForceInline
2799     public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2800         // this is stronger than opaque semantics
2801         putFlatValueRelease(o, offset, layout, valueType, x);
2802     }
2803 
2804     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2805     @IntrinsicCandidate
2806     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2807         putBooleanVolatile(o, offset, x);
2808     }
2809 
2810     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2811     @IntrinsicCandidate
2812     public final void putByteOpaque(Object o, long offset, byte x) {
2813         putByteVolatile(o, offset, x);
2814     }
2815 
2816     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2817     @IntrinsicCandidate
2818     public final void putShortOpaque(Object o, long offset, short x) {
2819         putShortVolatile(o, offset, x);
2820     }
2821 
2822     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2823     @IntrinsicCandidate

2832     }
2833 
2834     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2835     @IntrinsicCandidate
2836     public final void putFloatOpaque(Object o, long offset, float x) {
2837         putFloatVolatile(o, offset, x);
2838     }
2839 
2840     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2841     @IntrinsicCandidate
2842     public final void putLongOpaque(Object o, long offset, long x) {
2843         putLongVolatile(o, offset, x);
2844     }
2845 
2846     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2847     @IntrinsicCandidate
2848     public final void putDoubleOpaque(Object o, long offset, double x) {
2849         putDoubleVolatile(o, offset, x);
2850     }
2851 
2852     @ForceInline
2853     private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2854         // We turn the payload of an atomic value into a numeric value (of suitable type)
2855         // by storing the value into an array element (of matching layout) and by reading
2856         // back the array element as an integral value. After which we can implement the CAS
2857         // as a plain numeric CAS. Note: this only works if the payload contains no oops
2858         // (see VarHandles::isAtomicFlat).
2859         Object expectedArray = newSpecialArray(valueType, 1, layout);
2860         Object xArray = newSpecialArray(valueType, 1, layout);
2861         long base = arrayBaseOffset(expectedArray.getClass());
2862         int scale = arrayIndexScale(expectedArray.getClass());
2863         putFlatValue(expectedArray, base, layout, valueType, expected);
2864         putFlatValue(xArray, base, layout, valueType, x);
2865         switch (scale) {
2866             case 1: {
2867                 byte expectedByte = getByte(expectedArray, base);
2868                 byte xByte = getByte(xArray, base);
2869                 return compareAndSetByte(o, offset, expectedByte, xByte);
2870             }
2871             case 2: {
2872                 short expectedShort = getShort(expectedArray, base);
2873                 short xShort = getShort(xArray, base);
2874                 return compareAndSetShort(o, offset, expectedShort, xShort);
2875             }
2876             case 4: {
2877                 int expectedInt = getInt(expectedArray, base);
2878                 int xInt = getInt(xArray, base);
2879                 return compareAndSetInt(o, offset, expectedInt, xInt);
2880             }
2881             case 8: {
2882                 long expectedLong = getLong(expectedArray, base);
2883                 long xLong = getLong(xArray, base);
2884                 return compareAndSetLong(o, offset, expectedLong, xLong);
2885             }
2886             default: {
2887                 throw new UnsupportedOperationException();
2888             }
2889         }
2890     }
2891 
2892     /**
2893      * Unblocks the given thread blocked on {@code park}, or, if it is
2894      * not blocked, causes the subsequent call to {@code park} not to
2895      * block.  Note: this operation is "unsafe" solely because the
2896      * caller must somehow ensure that the thread has not been
2897      * destroyed. Nothing special is usually required to ensure this
2898      * when called from Java (in which there will ordinarily be a live
2899      * reference to the thread) but this is not nearly-automatically
2900      * so when calling from native code.
2901      *
2902      * @param thread the thread to unpark.
2903      */
2904     @IntrinsicCandidate
2905     public native void unpark(Object thread);
2906 
2907     /**
2908      * Blocks current thread, returning when a balancing
2909      * {@code unpark} occurs, or a balancing {@code unpark} has
2910      * already occurred, or the thread is interrupted, or, if not
2911      * absolute and time is not zero, the given time nanoseconds have

3258     /**
3259      * Atomically exchanges the given reference value with the current
3260      * reference value of a field or array element within the given
3261      * object {@code o} at the given {@code offset}.
3262      *
3263      * @param o object/array to update the field/element in
3264      * @param offset field/element offset
3265      * @param newValue new value
3266      * @return the previous value
3267      * @since 1.8
3268      */
3269     @IntrinsicCandidate
3270     public final Object getAndSetReference(Object o, long offset, Object newValue) {
3271         Object v;
3272         do {
3273             v = getReferenceVolatile(o, offset);
3274         } while (!weakCompareAndSetReference(o, offset, v, newValue));
3275         return v;
3276     }
3277 
3278     @ForceInline
3279     public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3280         Object v;
3281         do {
3282             v = getReferenceVolatile(o, offset);
3283         } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3284         return v;
3285     }
3286 
3287     @ForceInline
3288     public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3289         Object v;
3290         do {
3291             v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3292         } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3293         return v;
3294     }
3295 
3296     @ForceInline
3297     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3298         Object v;
3299         do {
3300             v = getReference(o, offset);
3301         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3302         return v;
3303     }
3304 
3305     @ForceInline
3306     public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3307         return getAndSetReference(o, offset, valueType, newValue);
3308     }
3309 
3310     @ForceInline
3311     public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3312         return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3313     }
3314 
3315     @ForceInline
3316     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3317         Object v;
3318         do {
3319             v = getReferenceAcquire(o, offset);
3320         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3321         return v;
3322     }
3323 
3324     @ForceInline
3325     public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3326         return getAndSetReference(o, offset, valueType, newValue);
3327     }
3328 
3329     @ForceInline
3330     public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3331         return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3332     }
3333 
3334     @IntrinsicCandidate
3335     public final byte getAndSetByte(Object o, long offset, byte newValue) {
3336         byte v;
3337         do {
3338             v = getByteVolatile(o, offset);
3339         } while (!weakCompareAndSetByte(o, offset, v, newValue));
3340         return v;
3341     }
3342 
3343     @ForceInline
3344     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3345         byte v;
3346         do {
3347             v = getByte(o, offset);
3348         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3349         return v;
3350     }
3351 
3352     @ForceInline
3353     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

4371     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
4372 
4373 
4374 
4375     private native long allocateMemory0(long bytes);
4376     private native long reallocateMemory0(long address, long bytes);
4377     private native void freeMemory0(long address);
4378     @IntrinsicCandidate
4379     private native void setMemory0(Object o, long offset, long bytes, byte value);
4380     @IntrinsicCandidate
4381     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4382     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4383     private native long objectFieldOffset0(Field f);
4384     private native long objectFieldOffset1(Class<?> c, String name);
4385     private native long staticFieldOffset0(Field f);
4386     private native Object staticFieldBase0(Field f);
4387     private native boolean shouldBeInitialized0(Class<?> c);
4388     private native void ensureClassInitialized0(Class<?> c);
4389     private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4390     private native int arrayIndexScale0(Class<?> arrayClass);
4391     private native long getObjectSize0(Object o);
4392     private native int getLoadAverage0(double[] loadavg, int nelems);
4393 
4394 
4395     /**
4396      * Invokes the given direct byte buffer's cleaner, if any.
4397      *
4398      * @param directBuffer a direct byte buffer
4399      * @throws NullPointerException     if {@code directBuffer} is null
4400      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4401      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
4402      *                                  {@link java.nio.Buffer#duplicate duplicate}
4403      */
4404     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4405         if (!directBuffer.isDirect())
4406             throw new IllegalArgumentException("buffer is non-direct");
4407 
4408         DirectBuffer db = (DirectBuffer) directBuffer;
4409         if (db.attachment() != null)
4410             throw new IllegalArgumentException("duplicate or slice");
4411 
< prev index next >