< prev index next >

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

Print this page

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

















































































 189     /**
 190      * Fetches a reference value from a given Java variable.



 191      * @see #getInt(Object, long)
 192      */
 193     @IntrinsicCandidate
 194     public native Object getReference(Object o, long offset);
 195 
 196     /**
 197      * Stores a reference value into a given Java variable.


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














































































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

1178     }
1179 
1180     /**
1181      * Ensures the given class has been initialized (see JVMS-5.5 for details).
1182      * This is often needed in conjunction with obtaining the static field base
1183      * of a class.
1184      *
1185      * The call returns when either class {@code c} is fully initialized or
1186      * class {@code c} is being initialized and the call is performed from
1187      * the initializing thread. In the latter case a subsequent call to
1188      * {@link #shouldBeInitialized} will return {@code true}.
1189      */
1190     public void ensureClassInitialized(Class<?> c) {
1191         if (c == null) {
1192             throw new NullPointerException();
1193         }
1194 
1195         ensureClassInitialized0(c);
1196     }
1197 















1198     /**
1199      * Reports the offset of the first element in the storage allocation of a
1200      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
1201      * for the same class, you may use that scale factor, together with this
1202      * base offset, to form new offsets to access elements of arrays of the
1203      * given class.
1204      * <p>
1205      * The return value is in the range of a {@code int}.  The return type is
1206      * {@code long} to emphasize that long arithmetic should always be used
1207      * for offset calculations to avoid overflows.





1208      *
1209      * @see #getInt(Object, long)
1210      * @see #putInt(Object, long, int)
1211      */
1212     public long arrayBaseOffset(Class<?> arrayClass) {
1213         if (arrayClass == null) {
1214             throw new NullPointerException();
1215         }
1216 
1217         return arrayBaseOffset0(arrayClass);
1218     }
1219 







1220 
1221     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1222     public static final long ARRAY_BOOLEAN_BASE_OFFSET
1223             = theUnsafe.arrayBaseOffset(boolean[].class);
1224 
1225     /** The value of {@code arrayBaseOffset(byte[].class)} */
1226     public static final long ARRAY_BYTE_BASE_OFFSET
1227             = theUnsafe.arrayBaseOffset(byte[].class);
1228 
1229     /** The value of {@code arrayBaseOffset(short[].class)} */
1230     public static final long ARRAY_SHORT_BASE_OFFSET
1231             = theUnsafe.arrayBaseOffset(short[].class);
1232 
1233     /** The value of {@code arrayBaseOffset(char[].class)} */
1234     public static final long ARRAY_CHAR_BASE_OFFSET
1235             = theUnsafe.arrayBaseOffset(char[].class);
1236 
1237     /** The value of {@code arrayBaseOffset(int[].class)} */
1238     public static final long ARRAY_INT_BASE_OFFSET
1239             = theUnsafe.arrayBaseOffset(int[].class);

1246     public static final long ARRAY_FLOAT_BASE_OFFSET
1247             = theUnsafe.arrayBaseOffset(float[].class);
1248 
1249     /** The value of {@code arrayBaseOffset(double[].class)} */
1250     public static final long ARRAY_DOUBLE_BASE_OFFSET
1251             = theUnsafe.arrayBaseOffset(double[].class);
1252 
1253     /** The value of {@code arrayBaseOffset(Object[].class)} */
1254     public static final long ARRAY_OBJECT_BASE_OFFSET
1255             = theUnsafe.arrayBaseOffset(Object[].class);
1256 
1257     /**
1258      * Reports the scale factor for addressing elements in the storage
1259      * allocation of a given array class.  However, arrays of "narrow" types
1260      * will generally not work properly with accessors like {@link
1261      * #getByte(Object, long)}, so the scale factor for such classes is reported
1262      * as zero.
1263      * <p>
1264      * The computation of the actual memory offset should always use {@code
1265      * long} arithmetic to avoid overflows.





1266      *
1267      * @see #arrayBaseOffset
1268      * @see #getInt(Object, long)
1269      * @see #putInt(Object, long, int)
1270      */
1271     public int arrayIndexScale(Class<?> arrayClass) {
1272         if (arrayClass == null) {
1273             throw new NullPointerException();
1274         }
1275 
1276         return arrayIndexScale0(arrayClass);
1277     }
1278 


























1279 
1280     /** The value of {@code arrayIndexScale(boolean[].class)} */
1281     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1282             = theUnsafe.arrayIndexScale(boolean[].class);
1283 
1284     /** The value of {@code arrayIndexScale(byte[].class)} */
1285     public static final int ARRAY_BYTE_INDEX_SCALE
1286             = theUnsafe.arrayIndexScale(byte[].class);
1287 
1288     /** The value of {@code arrayIndexScale(short[].class)} */
1289     public static final int ARRAY_SHORT_INDEX_SCALE
1290             = theUnsafe.arrayIndexScale(short[].class);
1291 
1292     /** The value of {@code arrayIndexScale(char[].class)} */
1293     public static final int ARRAY_CHAR_INDEX_SCALE
1294             = theUnsafe.arrayIndexScale(char[].class);
1295 
1296     /** The value of {@code arrayIndexScale(int[].class)} */
1297     public static final int ARRAY_INT_INDEX_SCALE
1298             = theUnsafe.arrayIndexScale(int[].class);

1437        return null;
1438     }
1439 
1440     /** Throws the exception without telling the verifier. */
1441     public native void throwException(Throwable ee);
1442 
1443     /**
1444      * Atomically updates Java variable to {@code x} if it is currently
1445      * holding {@code expected}.
1446      *
1447      * <p>This operation has memory semantics of a {@code volatile} read
1448      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1449      *
1450      * @return {@code true} if successful
1451      */
1452     @IntrinsicCandidate
1453     public final native boolean compareAndSetReference(Object o, long offset,
1454                                                        Object expected,
1455                                                        Object x);
1456 






































1457     @IntrinsicCandidate
1458     public final native Object compareAndExchangeReference(Object o, long offset,
1459                                                            Object expected,
1460                                                            Object x);
1461 































1462     @IntrinsicCandidate
1463     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1464                                                            Object expected,
1465                                                            Object x) {
1466         return compareAndExchangeReference(o, offset, expected, x);
1467     }
1468 
















1469     @IntrinsicCandidate
1470     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1471                                                            Object expected,
1472                                                            Object x) {
1473         return compareAndExchangeReference(o, offset, expected, x);
1474     }
1475 
















1476     @IntrinsicCandidate
1477     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1478                                                          Object expected,
1479                                                          Object x) {
1480         return compareAndSetReference(o, offset, expected, x);
1481     }
1482 




















1483     @IntrinsicCandidate
1484     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1485                                                            Object expected,
1486                                                            Object x) {
1487         return compareAndSetReference(o, offset, expected, x);
1488     }
1489 




















1490     @IntrinsicCandidate
1491     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1492                                                            Object expected,
1493                                                            Object x) {
1494         return compareAndSetReference(o, offset, expected, x);
1495     }
1496 




















1497     @IntrinsicCandidate
1498     public final boolean weakCompareAndSetReference(Object o, long offset,
1499                                                     Object expected,
1500                                                     Object x) {
1501         return compareAndSetReference(o, offset, expected, x);
1502     }
1503 




















1504     /**
1505      * Atomically updates Java variable to {@code x} if it is currently
1506      * holding {@code expected}.
1507      *
1508      * <p>This operation has memory semantics of a {@code volatile} read
1509      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1510      *
1511      * @return {@code true} if successful
1512      */
1513     @IntrinsicCandidate
1514     public final native boolean compareAndSetInt(Object o, long offset,
1515                                                  int expected,
1516                                                  int x);
1517 
1518     @IntrinsicCandidate
1519     public final native int compareAndExchangeInt(Object o, long offset,
1520                                                   int expected,
1521                                                   int x);
1522 
1523     @IntrinsicCandidate

2099     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2100                                                       long expected,
2101                                                       long x) {
2102         return compareAndSetLong(o, offset, expected, x);
2103     }
2104 
2105     @IntrinsicCandidate
2106     public final boolean weakCompareAndSetLong(Object o, long offset,
2107                                                long expected,
2108                                                long x) {
2109         return compareAndSetLong(o, offset, expected, x);
2110     }
2111 
2112     /**
2113      * Fetches a reference value from a given Java variable, with volatile
2114      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2115      */
2116     @IntrinsicCandidate
2117     public native Object getReferenceVolatile(Object o, long offset);
2118 








2119     /**
2120      * Stores a reference value into a given Java variable, with
2121      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2122      */
2123     @IntrinsicCandidate
2124     public native void putReferenceVolatile(Object o, long offset, Object x);
2125 







2126     /** Volatile version of {@link #getInt(Object, long)}  */
2127     @IntrinsicCandidate
2128     public native int     getIntVolatile(Object o, long offset);
2129 
2130     /** Volatile version of {@link #putInt(Object, long, int)}  */
2131     @IntrinsicCandidate
2132     public native void    putIntVolatile(Object o, long offset, int x);
2133 
2134     /** Volatile version of {@link #getBoolean(Object, long)}  */
2135     @IntrinsicCandidate
2136     public native boolean getBooleanVolatile(Object o, long offset);
2137 
2138     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2139     @IntrinsicCandidate
2140     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2141 
2142     /** Volatile version of {@link #getByte(Object, long)}  */
2143     @IntrinsicCandidate
2144     public native byte    getByteVolatile(Object o, long offset);
2145 

2178     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2179     @IntrinsicCandidate
2180     public native void    putFloatVolatile(Object o, long offset, float x);
2181 
2182     /** Volatile version of {@link #getDouble(Object, long)}  */
2183     @IntrinsicCandidate
2184     public native double  getDoubleVolatile(Object o, long offset);
2185 
2186     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2187     @IntrinsicCandidate
2188     public native void    putDoubleVolatile(Object o, long offset, double x);
2189 
2190 
2191 
2192     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2193     @IntrinsicCandidate
2194     public final Object getReferenceAcquire(Object o, long offset) {
2195         return getReferenceVolatile(o, offset);
2196     }
2197 








2198     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2199     @IntrinsicCandidate
2200     public final boolean getBooleanAcquire(Object o, long offset) {
2201         return getBooleanVolatile(o, offset);
2202     }
2203 
2204     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2205     @IntrinsicCandidate
2206     public final byte getByteAcquire(Object o, long offset) {
2207         return getByteVolatile(o, offset);
2208     }
2209 
2210     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2211     @IntrinsicCandidate
2212     public final short getShortAcquire(Object o, long offset) {
2213         return getShortVolatile(o, offset);
2214     }
2215 
2216     /** Acquire version of {@link #getCharVolatile(Object, long)} */
2217     @IntrinsicCandidate

2242     public final double getDoubleAcquire(Object o, long offset) {
2243         return getDoubleVolatile(o, offset);
2244     }
2245 
2246     /*
2247      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2248      * that do not guarantee immediate visibility of the store to
2249      * other threads. This method is generally only useful if the
2250      * underlying field is a Java volatile (or if an array cell, one
2251      * that is otherwise only accessed using volatile accesses).
2252      *
2253      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2254      */
2255 
2256     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2257     @IntrinsicCandidate
2258     public final void putReferenceRelease(Object o, long offset, Object x) {
2259         putReferenceVolatile(o, offset, x);
2260     }
2261 







2262     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2263     @IntrinsicCandidate
2264     public final void putBooleanRelease(Object o, long offset, boolean x) {
2265         putBooleanVolatile(o, offset, x);
2266     }
2267 
2268     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2269     @IntrinsicCandidate
2270     public final void putByteRelease(Object o, long offset, byte x) {
2271         putByteVolatile(o, offset, x);
2272     }
2273 
2274     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2275     @IntrinsicCandidate
2276     public final void putShortRelease(Object o, long offset, short x) {
2277         putShortVolatile(o, offset, x);
2278     }
2279 
2280     /** Release version of {@link #putCharVolatile(Object, long, char)} */
2281     @IntrinsicCandidate

2298     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2299     @IntrinsicCandidate
2300     public final void putLongRelease(Object o, long offset, long x) {
2301         putLongVolatile(o, offset, x);
2302     }
2303 
2304     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2305     @IntrinsicCandidate
2306     public final void putDoubleRelease(Object o, long offset, double x) {
2307         putDoubleVolatile(o, offset, x);
2308     }
2309 
2310     // ------------------------------ Opaque --------------------------------------
2311 
2312     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2313     @IntrinsicCandidate
2314     public final Object getReferenceOpaque(Object o, long offset) {
2315         return getReferenceVolatile(o, offset);
2316     }
2317 






2318     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2319     @IntrinsicCandidate
2320     public final boolean getBooleanOpaque(Object o, long offset) {
2321         return getBooleanVolatile(o, offset);
2322     }
2323 
2324     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2325     @IntrinsicCandidate
2326     public final byte getByteOpaque(Object o, long offset) {
2327         return getByteVolatile(o, offset);
2328     }
2329 
2330     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2331     @IntrinsicCandidate
2332     public final short getShortOpaque(Object o, long offset) {
2333         return getShortVolatile(o, offset);
2334     }
2335 
2336     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2337     @IntrinsicCandidate

2352     }
2353 
2354     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2355     @IntrinsicCandidate
2356     public final long getLongOpaque(Object o, long offset) {
2357         return getLongVolatile(o, offset);
2358     }
2359 
2360     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2361     @IntrinsicCandidate
2362     public final double getDoubleOpaque(Object o, long offset) {
2363         return getDoubleVolatile(o, offset);
2364     }
2365 
2366     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2367     @IntrinsicCandidate
2368     public final void putReferenceOpaque(Object o, long offset, Object x) {
2369         putReferenceVolatile(o, offset, x);
2370     }
2371 






2372     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2373     @IntrinsicCandidate
2374     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2375         putBooleanVolatile(o, offset, x);
2376     }
2377 
2378     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2379     @IntrinsicCandidate
2380     public final void putByteOpaque(Object o, long offset, byte x) {
2381         putByteVolatile(o, offset, x);
2382     }
2383 
2384     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2385     @IntrinsicCandidate
2386     public final void putShortOpaque(Object o, long offset, short x) {
2387         putShortVolatile(o, offset, x);
2388     }
2389 
2390     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2391     @IntrinsicCandidate

2400     }
2401 
2402     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2403     @IntrinsicCandidate
2404     public final void putFloatOpaque(Object o, long offset, float x) {
2405         putFloatVolatile(o, offset, x);
2406     }
2407 
2408     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2409     @IntrinsicCandidate
2410     public final void putLongOpaque(Object o, long offset, long x) {
2411         putLongVolatile(o, offset, x);
2412     }
2413 
2414     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2415     @IntrinsicCandidate
2416     public final void putDoubleOpaque(Object o, long offset, double x) {
2417         putDoubleVolatile(o, offset, x);
2418     }
2419 













































































2420     /**
2421      * Unblocks the given thread blocked on {@code park}, or, if it is
2422      * not blocked, causes the subsequent call to {@code park} not to
2423      * block.  Note: this operation is "unsafe" solely because the
2424      * caller must somehow ensure that the thread has not been
2425      * destroyed. Nothing special is usually required to ensure this
2426      * when called from Java (in which there will ordinarily be a live
2427      * reference to the thread) but this is not nearly-automatically
2428      * so when calling from native code.
2429      *
2430      * @param thread the thread to unpark.
2431      */
2432     @IntrinsicCandidate
2433     public native void unpark(Object thread);
2434 
2435     /**
2436      * Blocks current thread, returning when a balancing
2437      * {@code unpark} occurs, or a balancing {@code unpark} has
2438      * already occurred, or the thread is interrupted, or, if not
2439      * absolute and time is not zero, the given time nanoseconds have

2786     /**
2787      * Atomically exchanges the given reference value with the current
2788      * reference value of a field or array element within the given
2789      * object {@code o} at the given {@code offset}.
2790      *
2791      * @param o object/array to update the field/element in
2792      * @param offset field/element offset
2793      * @param newValue new value
2794      * @return the previous value
2795      * @since 1.8
2796      */
2797     @IntrinsicCandidate
2798     public final Object getAndSetReference(Object o, long offset, Object newValue) {
2799         Object v;
2800         do {
2801             v = getReferenceVolatile(o, offset);
2802         } while (!weakCompareAndSetReference(o, offset, v, newValue));
2803         return v;
2804     }
2805 


















2806     @ForceInline
2807     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2808         Object v;
2809         do {
2810             v = getReference(o, offset);
2811         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2812         return v;
2813     }
2814 










2815     @ForceInline
2816     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2817         Object v;
2818         do {
2819             v = getReferenceAcquire(o, offset);
2820         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2821         return v;
2822     }
2823 










2824     @IntrinsicCandidate
2825     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2826         byte v;
2827         do {
2828             v = getByteVolatile(o, offset);
2829         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2830         return v;
2831     }
2832 
2833     @ForceInline
2834     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2835         byte v;
2836         do {
2837             v = getByte(o, offset);
2838         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2839         return v;
2840     }
2841 
2842     @ForceInline
2843     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

3859     private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n)    ; }
3860     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
3861     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
3862 
3863 
3864 
3865     private native long allocateMemory0(long bytes);
3866     private native long reallocateMemory0(long address, long bytes);
3867     private native void freeMemory0(long address);
3868     @IntrinsicCandidate
3869     private native void setMemory0(Object o, long offset, long bytes, byte value);
3870     @IntrinsicCandidate
3871     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3872     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3873     private native long objectFieldOffset0(Field f); // throws IAE
3874     private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
3875     private native long staticFieldOffset0(Field f); // throws IAE
3876     private native Object staticFieldBase0(Field f); // throws IAE
3877     private native boolean shouldBeInitialized0(Class<?> c);
3878     private native void ensureClassInitialized0(Class<?> c);

3879     private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic


3880     private native int arrayIndexScale0(Class<?> arrayClass);



3881     private native int getLoadAverage0(double[] loadavg, int nelems);


3882 
3883 
3884     /**
3885      * Invokes the given direct byte buffer's cleaner, if any.
3886      *
3887      * @param directBuffer a direct byte buffer
3888      * @throws NullPointerException     if {@code directBuffer} is null
3889      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3890      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
3891      *                                  {@link java.nio.Buffer#duplicate duplicate}
3892      */
3893     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3894         if (!directBuffer.isDirect())
3895             throw new IllegalArgumentException("buffer is non-direct");
3896 
3897         DirectBuffer db = (DirectBuffer) directBuffer;
3898         if (db.attachment() != null)
3899             throw new IllegalArgumentException("duplicate or slice");
3900 
3901         Cleaner cleaner = db.cleaner();

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

1342     }
1343 
1344     /**
1345      * Ensures the given class has been initialized (see JVMS-5.5 for details).
1346      * This is often needed in conjunction with obtaining the static field base
1347      * of a class.
1348      *
1349      * The call returns when either class {@code c} is fully initialized or
1350      * class {@code c} is being initialized and the call is performed from
1351      * the initializing thread. In the latter case a subsequent call to
1352      * {@link #shouldBeInitialized} will return {@code true}.
1353      */
1354     public void ensureClassInitialized(Class<?> c) {
1355         if (c == null) {
1356             throw new NullPointerException();
1357         }
1358 
1359         ensureClassInitialized0(c);
1360     }
1361 
1362     /**
1363      * The reading or writing of strict static fields may require
1364      * special processing.  Notify the VM that such an event is about
1365      * to happen.  The VM may respond by throwing an exception, in the
1366      * case of a read of an uninitialized field.  If the VM allows the
1367      * method to return normally, no further calls are needed, with
1368      * the same arguments.
1369      */
1370     public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1371         if (c == null) {
1372             throw new NullPointerException();
1373         }
1374         notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1375     }
1376 
1377     /**
1378      * Reports the offset of the first element in the storage allocation of a
1379      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
1380      * for the same class, you may use that scale factor, together with this
1381      * base offset, to form new offsets to access elements of arrays of the
1382      * given class.
1383      * <p>
1384      * The return value is in the range of a {@code int}.  The return type is
1385      * {@code long} to emphasize that long arithmetic should always be used
1386      * for offset calculations to avoid overflows.
1387      * <p>
1388      * This method doesn't support arrays with an element type that is
1389      * a value class, because this type of array can have multiple layouts.
1390      * For these arrays, {@code arrayInstanceBaseOffset(Object[] array)}
1391      * must be used instead.
1392      *
1393      * @see #getInt(Object, long)
1394      * @see #putInt(Object, long, int)
1395      */
1396     public long arrayBaseOffset(Class<?> arrayClass) {
1397         if (arrayClass == null) {
1398             throw new NullPointerException();
1399         }
1400 
1401         return arrayBaseOffset0(arrayClass);
1402     }
1403 
1404     public long arrayInstanceBaseOffset(Object[] array) {
1405         if (array == null) {
1406             throw new NullPointerException();
1407         }
1408 
1409         return arrayInstanceBaseOffset0(array);
1410     }
1411 
1412     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1413     public static final long ARRAY_BOOLEAN_BASE_OFFSET
1414             = theUnsafe.arrayBaseOffset(boolean[].class);
1415 
1416     /** The value of {@code arrayBaseOffset(byte[].class)} */
1417     public static final long ARRAY_BYTE_BASE_OFFSET
1418             = theUnsafe.arrayBaseOffset(byte[].class);
1419 
1420     /** The value of {@code arrayBaseOffset(short[].class)} */
1421     public static final long ARRAY_SHORT_BASE_OFFSET
1422             = theUnsafe.arrayBaseOffset(short[].class);
1423 
1424     /** The value of {@code arrayBaseOffset(char[].class)} */
1425     public static final long ARRAY_CHAR_BASE_OFFSET
1426             = theUnsafe.arrayBaseOffset(char[].class);
1427 
1428     /** The value of {@code arrayBaseOffset(int[].class)} */
1429     public static final long ARRAY_INT_BASE_OFFSET
1430             = theUnsafe.arrayBaseOffset(int[].class);

1437     public static final long ARRAY_FLOAT_BASE_OFFSET
1438             = theUnsafe.arrayBaseOffset(float[].class);
1439 
1440     /** The value of {@code arrayBaseOffset(double[].class)} */
1441     public static final long ARRAY_DOUBLE_BASE_OFFSET
1442             = theUnsafe.arrayBaseOffset(double[].class);
1443 
1444     /** The value of {@code arrayBaseOffset(Object[].class)} */
1445     public static final long ARRAY_OBJECT_BASE_OFFSET
1446             = theUnsafe.arrayBaseOffset(Object[].class);
1447 
1448     /**
1449      * Reports the scale factor for addressing elements in the storage
1450      * allocation of a given array class.  However, arrays of "narrow" types
1451      * will generally not work properly with accessors like {@link
1452      * #getByte(Object, long)}, so the scale factor for such classes is reported
1453      * as zero.
1454      * <p>
1455      * The computation of the actual memory offset should always use {@code
1456      * long} arithmetic to avoid overflows.
1457      * <p>
1458      * This method doesn't support arrays with an element type that is
1459      * a value class, because this type of array can have multiple layouts.
1460      * For these arrays, {@code arrayInstanceIndexScale(Object[] array)}
1461      * must be used instead.
1462      *
1463      * @see #arrayBaseOffset
1464      * @see #getInt(Object, long)
1465      * @see #putInt(Object, long, int)
1466      */
1467     public int arrayIndexScale(Class<?> arrayClass) {
1468         if (arrayClass == null) {
1469             throw new NullPointerException();
1470         }
1471 
1472         return arrayIndexScale0(arrayClass);
1473     }
1474 
1475     public int arrayInstanceIndexScale(Object[] array) {
1476         if (array == null) {
1477             throw new NullPointerException();
1478         }
1479 
1480         return arrayInstanceIndexScale0(array);
1481     }
1482 
1483     public int[] getFieldMap(Class<? extends Object> c) {
1484       if (c == null) {
1485         throw new NullPointerException();
1486       }
1487       return getFieldMap0(c);
1488     }
1489 
1490     /**
1491      * Return the size of the object in the heap.
1492      * @param o an object
1493      * @return the objects's size
1494      * @since Valhalla
1495      */
1496     public long getObjectSize(Object o) {
1497         if (o == null)
1498             throw new NullPointerException();
1499         return getObjectSize0(o);
1500     }
1501 
1502     /** The value of {@code arrayIndexScale(boolean[].class)} */
1503     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1504             = theUnsafe.arrayIndexScale(boolean[].class);
1505 
1506     /** The value of {@code arrayIndexScale(byte[].class)} */
1507     public static final int ARRAY_BYTE_INDEX_SCALE
1508             = theUnsafe.arrayIndexScale(byte[].class);
1509 
1510     /** The value of {@code arrayIndexScale(short[].class)} */
1511     public static final int ARRAY_SHORT_INDEX_SCALE
1512             = theUnsafe.arrayIndexScale(short[].class);
1513 
1514     /** The value of {@code arrayIndexScale(char[].class)} */
1515     public static final int ARRAY_CHAR_INDEX_SCALE
1516             = theUnsafe.arrayIndexScale(char[].class);
1517 
1518     /** The value of {@code arrayIndexScale(int[].class)} */
1519     public static final int ARRAY_INT_INDEX_SCALE
1520             = theUnsafe.arrayIndexScale(int[].class);

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

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

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

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

2731     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2732     @IntrinsicCandidate
2733     public final void putLongRelease(Object o, long offset, long x) {
2734         putLongVolatile(o, offset, x);
2735     }
2736 
2737     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2738     @IntrinsicCandidate
2739     public final void putDoubleRelease(Object o, long offset, double x) {
2740         putDoubleVolatile(o, offset, x);
2741     }
2742 
2743     // ------------------------------ Opaque --------------------------------------
2744 
2745     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2746     @IntrinsicCandidate
2747     public final Object getReferenceOpaque(Object o, long offset) {
2748         return getReferenceVolatile(o, offset);
2749     }
2750 
2751     @ForceInline
2752     public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2753         // this is stronger than opaque semantics
2754         return getFlatValueAcquire(o, offset, layout, valueType);
2755     }
2756 
2757     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2758     @IntrinsicCandidate
2759     public final boolean getBooleanOpaque(Object o, long offset) {
2760         return getBooleanVolatile(o, offset);
2761     }
2762 
2763     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2764     @IntrinsicCandidate
2765     public final byte getByteOpaque(Object o, long offset) {
2766         return getByteVolatile(o, offset);
2767     }
2768 
2769     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2770     @IntrinsicCandidate
2771     public final short getShortOpaque(Object o, long offset) {
2772         return getShortVolatile(o, offset);
2773     }
2774 
2775     /** Opaque version of {@link #getCharVolatile(Object, long)} */
2776     @IntrinsicCandidate

2791     }
2792 
2793     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2794     @IntrinsicCandidate
2795     public final long getLongOpaque(Object o, long offset) {
2796         return getLongVolatile(o, offset);
2797     }
2798 
2799     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2800     @IntrinsicCandidate
2801     public final double getDoubleOpaque(Object o, long offset) {
2802         return getDoubleVolatile(o, offset);
2803     }
2804 
2805     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2806     @IntrinsicCandidate
2807     public final void putReferenceOpaque(Object o, long offset, Object x) {
2808         putReferenceVolatile(o, offset, x);
2809     }
2810 
2811     @ForceInline
2812     public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2813         // this is stronger than opaque semantics
2814         putFlatValueRelease(o, offset, layout, valueType, x);
2815     }
2816 
2817     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2818     @IntrinsicCandidate
2819     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2820         putBooleanVolatile(o, offset, x);
2821     }
2822 
2823     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2824     @IntrinsicCandidate
2825     public final void putByteOpaque(Object o, long offset, byte x) {
2826         putByteVolatile(o, offset, x);
2827     }
2828 
2829     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2830     @IntrinsicCandidate
2831     public final void putShortOpaque(Object o, long offset, short x) {
2832         putShortVolatile(o, offset, x);
2833     }
2834 
2835     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2836     @IntrinsicCandidate

2845     }
2846 
2847     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2848     @IntrinsicCandidate
2849     public final void putFloatOpaque(Object o, long offset, float x) {
2850         putFloatVolatile(o, offset, x);
2851     }
2852 
2853     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2854     @IntrinsicCandidate
2855     public final void putLongOpaque(Object o, long offset, long x) {
2856         putLongVolatile(o, offset, x);
2857     }
2858 
2859     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2860     @IntrinsicCandidate
2861     public final void putDoubleOpaque(Object o, long offset, double x) {
2862         putDoubleVolatile(o, offset, x);
2863     }
2864 
2865     @ForceInline
2866     private boolean compareAndSetFlatValueAsBytes(Object[] array, Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2867         // We can convert between a value object and a binary value (of suitable size) using array elements.
2868         // This only works if the payload contains no oops (see VarHandles::isAtomicFlat).
2869         // Thus, we can implement the CAS with a plain numeric CAS.
2870 
2871         // array[0]: witness (put as binary, get as object), at base
2872         // array[1]: x (put as object, get as binary), at base + scale
2873         // When witness == expected, the witness binary may be different from the expected binary.
2874         // This happens when compiler does not zero unused positions in the witness.
2875         // So we must obtain the witness binary and use it as expected binary for the numeric CAS.
2876         long base = arrayInstanceBaseOffset(array);
2877         int scale = arrayInstanceIndexScale(array);
2878         putFlatValue(array, base + scale, layout, valueType, x); // put x as object
2879         switch (scale) {
2880             case 1: {
2881                 do {
2882                     byte witnessByte = getByteVolatile(o, offset);
2883                     putByte(array, base, witnessByte); // put witness as binary
2884                     Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2885                     if (witness != expected) {
2886                         return false;
2887                     }
2888                     byte xByte = getByte(array, base + scale); // get x as binary
2889                     if (compareAndSetByte(o, offset, witnessByte, xByte)) {
2890                         return true;
2891                     }
2892                 } while (true);
2893             }
2894             case 2: {
2895                 do {
2896                     short witnessShort = getShortVolatile(o, offset);
2897                     putShort(array, base, witnessShort); // put witness as binary
2898                     Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2899                     if (witness != expected) {
2900                         return false;
2901                     }
2902                     short xShort = getShort(array, base + scale); // get x as binary
2903                     if (compareAndSetShort(o, offset, witnessShort, xShort)) {
2904                         return true;
2905                     }
2906                 } while (true);
2907             }
2908             case 4: {
2909                 do {
2910                     int witnessInt = getIntVolatile(o, offset);
2911                     putInt(array, base, witnessInt); // put witness as binary
2912                     Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2913                     if (witness != expected) {
2914                         return false;
2915                     }
2916                     int xInt = getInt(array, base + scale); // get x as binary
2917                     if (compareAndSetInt(o, offset, witnessInt, xInt)) {
2918                         return true;
2919                     }
2920                 } while (true);
2921             }
2922             case 8: {
2923                 do {
2924                     long witnessLong = getLongVolatile(o, offset);
2925                     putLong(array, base, witnessLong); // put witness as binary
2926                     Object witness = getFlatValue(array, base, layout, valueType);
2927                     if (witness != expected) {
2928                         return false;
2929                     }
2930                     long xLong = getLong(array, base + scale); // get x as binary
2931                     if (compareAndSetLong(o, offset, witnessLong, xLong)) {
2932                         return true;
2933                     }
2934                 } while (true);
2935             }
2936             default: {
2937                 throw new UnsupportedOperationException();
2938             }
2939         }
2940     }
2941 
2942     /**
2943      * Unblocks the given thread blocked on {@code park}, or, if it is
2944      * not blocked, causes the subsequent call to {@code park} not to
2945      * block.  Note: this operation is "unsafe" solely because the
2946      * caller must somehow ensure that the thread has not been
2947      * destroyed. Nothing special is usually required to ensure this
2948      * when called from Java (in which there will ordinarily be a live
2949      * reference to the thread) but this is not nearly-automatically
2950      * so when calling from native code.
2951      *
2952      * @param thread the thread to unpark.
2953      */
2954     @IntrinsicCandidate
2955     public native void unpark(Object thread);
2956 
2957     /**
2958      * Blocks current thread, returning when a balancing
2959      * {@code unpark} occurs, or a balancing {@code unpark} has
2960      * already occurred, or the thread is interrupted, or, if not
2961      * absolute and time is not zero, the given time nanoseconds have

3308     /**
3309      * Atomically exchanges the given reference value with the current
3310      * reference value of a field or array element within the given
3311      * object {@code o} at the given {@code offset}.
3312      *
3313      * @param o object/array to update the field/element in
3314      * @param offset field/element offset
3315      * @param newValue new value
3316      * @return the previous value
3317      * @since 1.8
3318      */
3319     @IntrinsicCandidate
3320     public final Object getAndSetReference(Object o, long offset, Object newValue) {
3321         Object v;
3322         do {
3323             v = getReferenceVolatile(o, offset);
3324         } while (!weakCompareAndSetReference(o, offset, v, newValue));
3325         return v;
3326     }
3327 
3328     @ForceInline
3329     public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3330         Object v;
3331         do {
3332             v = getReferenceVolatile(o, offset);
3333         } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3334         return v;
3335     }
3336 
3337     @ForceInline
3338     public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3339         Object v;
3340         do {
3341             v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3342         } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3343         return v;
3344     }
3345 
3346     @ForceInline
3347     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3348         Object v;
3349         do {
3350             v = getReference(o, offset);
3351         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3352         return v;
3353     }
3354 
3355     @ForceInline
3356     public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3357         return getAndSetReference(o, offset, valueType, newValue);
3358     }
3359 
3360     @ForceInline
3361     public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3362         return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3363     }
3364 
3365     @ForceInline
3366     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3367         Object v;
3368         do {
3369             v = getReferenceAcquire(o, offset);
3370         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3371         return v;
3372     }
3373 
3374     @ForceInline
3375     public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3376         return getAndSetReference(o, offset, valueType, newValue);
3377     }
3378 
3379     @ForceInline
3380     public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3381         return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3382     }
3383 
3384     @IntrinsicCandidate
3385     public final byte getAndSetByte(Object o, long offset, byte newValue) {
3386         byte v;
3387         do {
3388             v = getByteVolatile(o, offset);
3389         } while (!weakCompareAndSetByte(o, offset, v, newValue));
3390         return v;
3391     }
3392 
3393     @ForceInline
3394     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3395         byte v;
3396         do {
3397             v = getByte(o, offset);
3398         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3399         return v;
3400     }
3401 
3402     @ForceInline
3403     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {

4419     private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n)    ; }
4420     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
4421     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
4422 
4423 
4424 
4425     private native long allocateMemory0(long bytes);
4426     private native long reallocateMemory0(long address, long bytes);
4427     private native void freeMemory0(long address);
4428     @IntrinsicCandidate
4429     private native void setMemory0(Object o, long offset, long bytes, byte value);
4430     @IntrinsicCandidate
4431     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4432     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4433     private native long objectFieldOffset0(Field f); // throws IAE
4434     private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
4435     private native long staticFieldOffset0(Field f); // throws IAE
4436     private native Object staticFieldBase0(Field f); // throws IAE
4437     private native boolean shouldBeInitialized0(Class<?> c);
4438     private native void ensureClassInitialized0(Class<?> c);
4439     private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4440     private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4441     @IntrinsicCandidate
4442     private native int arrayInstanceBaseOffset0(Object[] array);
4443     private native int arrayIndexScale0(Class<?> arrayClass);
4444     @IntrinsicCandidate
4445     private native int arrayInstanceIndexScale0(Object[] array);
4446     private native long getObjectSize0(Object o);
4447     private native int getLoadAverage0(double[] loadavg, int nelems);
4448     @IntrinsicCandidate
4449     private native int[] getFieldMap0(Class <?> c);
4450 
4451 
4452     /**
4453      * Invokes the given direct byte buffer's cleaner, if any.
4454      *
4455      * @param directBuffer a direct byte buffer
4456      * @throws NullPointerException     if {@code directBuffer} is null
4457      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4458      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
4459      *                                  {@link java.nio.Buffer#duplicate duplicate}
4460      */
4461     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4462         if (!directBuffer.isDirect())
4463             throw new IllegalArgumentException("buffer is non-direct");
4464 
4465         DirectBuffer db = (DirectBuffer) directBuffer;
4466         if (db.attachment() != null)
4467             throw new IllegalArgumentException("duplicate or slice");
4468 
4469         Cleaner cleaner = db.cleaner();
< prev index next >