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.vm.annotation.AOTRuntimeSetup;
29 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
30 import jdk.internal.vm.annotation.ForceInline;
31 import jdk.internal.vm.annotation.IntrinsicCandidate;
32 import sun.nio.Cleaner;
33 import sun.nio.ch.DirectBuffer;
34
35 import java.lang.reflect.Field;
36 import java.security.ProtectionDomain;
37
38 import static jdk.internal.misc.UnsafeConstants.*;
39
40 /**
41 * A collection of methods for performing low-level, unsafe operations.
42 * Although the class and all methods are public, use of this class is
43 * limited because only trusted code can obtain instances of it.
44 *
45 * <em>Note:</em> It is the responsibility of the caller to make sure
46 * arguments are checked before methods of this class are
47 * called. While some rudimentary checks are performed on the input,
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);
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
|
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.value.ValueClass;
29 import jdk.internal.vm.annotation.AOTRuntimeSetup;
30 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
31 import jdk.internal.vm.annotation.ForceInline;
32 import jdk.internal.vm.annotation.IntrinsicCandidate;
33 import sun.nio.Cleaner;
34 import sun.nio.ch.DirectBuffer;
35
36 import java.lang.reflect.Field;
37 import java.security.ProtectionDomain;
38
39 import static jdk.internal.misc.UnsafeConstants.*;
40
41 /**
42 * A collection of methods for performing low-level, unsafe operations.
43 * Although the class and all methods are public, use of this class is
44 * limited because only trusted code can obtain instances of it.
45 *
46 * <em>Note:</em> It is the responsibility of the caller to make sure
47 * arguments are checked before methods of this class are
48 * called. While some rudimentary checks are performed on the input,
170 * The first two parameters are interpreted exactly as with
171 * {@link #getInt(Object, long)} to refer to a specific
172 * Java variable (field or array element). The given value
173 * is stored into that variable.
174 * <p>
175 * The variable must be of the same type as the method
176 * parameter {@code x}.
177 *
178 * @param o Java heap object in which the variable resides, if any, else
179 * null
180 * @param offset indication of where the variable resides in a Java heap
181 * object, if any, else a memory address locating the variable
182 * statically
183 * @param x the value to store into the indicated Java variable
184 * @throws RuntimeException No defined exceptions are thrown, not even
185 * {@link NullPointerException}
186 */
187 @IntrinsicCandidate
188 public native void putInt(Object o, long offset, int x);
189
190
191 /**
192 * Returns true if the given field is flattened.
193 */
194 public boolean isFlatField(Field f) {
195 if (f == null) {
196 throw new NullPointerException();
197 }
198 return isFlatField0(f);
199 }
200
201 private native boolean isFlatField0(Object o);
202
203 /* Returns true if the given field has a null marker
204 * <p>
205 * Nullable flat fields are stored in a flattened representation
206 * and have an associated null marker to indicate if the the field value is
207 * null or the one stored with the flat representation
208 */
209
210 public boolean hasNullMarker(Field f) {
211 if (f == null) {
212 throw new NullPointerException();
213 }
214 return hasNullMarker0(f);
215 }
216
217 private native boolean hasNullMarker0(Object o);
218
219 /* Returns the offset of the null marker of the field,
220 * or -1 if the field doesn't have a null marker
221 */
222
223 public int nullMarkerOffset(Field f) {
224 if (f == null) {
225 throw new NullPointerException();
226 }
227 return nullMarkerOffset0(f);
228 }
229
230 private native int nullMarkerOffset0(Object o);
231
232 public static final int NON_FLAT_LAYOUT = 0;
233
234 /* Reports the kind of layout used for an element in the storage
235 * allocation of the given array. Do not expect to perform any logic
236 * or layout control with this value, it is just an opaque token
237 * used for performance reasons.
238 *
239 * A layout of 0 indicates this array is not flat.
240 */
241 public int arrayLayout(Object[] array) {
242 if (array == null) {
243 throw new NullPointerException();
244 }
245 return arrayLayout0(array);
246 }
247
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 * @param o Java heap object in which the variable resides, if any, else
302 * null
303 * @param offset indication of where the variable resides in a Java heap
304 * object, if any, else a memory address locating the variable
305 * statically
306 * @param valueType value type
307 * @param <V> the type of a value
308 * @return the value fetched from the indicated Java variable
309 * @throws RuntimeException No defined exceptions are thrown, not even
310 * {@link NullPointerException}
311 */
312 @IntrinsicCandidate
313 public native <V> V getValue(Object o, long offset, Class<?> valueType);
314
315 /**
316 * Fetches a value of type {@code <V>} from a given Java variable.
317 * More specifically, fetches a field or array element within the given
318 * {@code o} object at the given offset, or (if {@code o} is null)
319 * from the memory address whose numerical value is the given offset.
320 *
321 * @param o Java heap object in which the variable resides, if any, else
322 * null
323 * @param offset indication of where the variable resides in a Java heap
324 * object, if any, else a memory address locating the variable
325 * statically
326 * @param layoutKind opaque value used by the VM to know the layout
327 * the field or array element. This value must be retrieved with
328 * {@link #fieldLayout} or {@link #arrayLayout}.
329 * @param valueType value type
330 * @param <V> the type of a value
331 * @return the value fetched from the indicated Java variable
332 * @throws RuntimeException No defined exceptions are thrown, not even
333 * {@link NullPointerException}
334 */
335 @IntrinsicCandidate
336 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
337
338
339 /**
340 * Stores the given value into a given Java variable.
341 *
342 * Unless the reference {@code o} being stored is either null
343 * or matches the field type, the results are undefined.
344 *
345 * @param o Java heap object in which the variable resides, if any, else
346 * null
347 * @param offset indication of where the variable resides in a Java heap
348 * object, if any, else a memory address locating the variable
349 * statically
350 * @param valueType value type
351 * @param v the value to store into the indicated Java variable
352 * @param <V> the type of a value
353 * @throws RuntimeException No defined exceptions are thrown, not even
354 * {@link NullPointerException}
355 */
356 @IntrinsicCandidate
357 public native <V> void putValue(Object o, long offset, Class<?> valueType, V v);
358
359 /**
360 * Stores the given value into a given Java variable.
361 *
362 * Unless the reference {@code o} being stored is either null
363 * or matches the field type, the results are undefined.
364 *
365 * @param o Java heap object in which the variable resides, if any, else
366 * null
367 * @param offset indication of where the variable resides in a Java heap
368 * object, if any, else a memory address locating the variable
369 * statically
370 * @param layoutKind opaque value used by the VM to know the layout
371 * the field or array element. This value must be retrieved with
372 * {@link #fieldLayout} or {@link #arrayLayout}.
373 * @param valueType value type
374 * @param v the value to store into the indicated Java variable
375 * @param <V> the type of a value
376 * @throws RuntimeException No defined exceptions are thrown, not even
377 * {@link NullPointerException}
378 */
379 @IntrinsicCandidate
380 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
381
382 /**
383 * Returns an object instance with a private buffered value whose layout
384 * and contents is exactly the given value instance. The return object
385 * is in the larval state that can be updated using the unsafe put operation.
386 *
387 * @param value a value instance
388 * @param <V> the type of the given value instance
389 */
390 @IntrinsicCandidate
391 public native <V> V makePrivateBuffer(V value);
392
393 /**
394 * Exits the larval state and returns a value instance.
395 *
396 * @param value a value instance
397 * @param <V> the type of the given value instance
398 */
399 @IntrinsicCandidate
400 public native <V> V finishPrivateBuffer(V value);
401
402 /**
403 * Returns the header size of the given value type.
404 *
405 * @param valueType value type
406 * @return the header size of the value type
407 */
408 public native <V> long valueHeaderSize(Class<V> valueType);
409
410 /** @see #getInt(Object, long) */
411 @IntrinsicCandidate
412 public native boolean getBoolean(Object o, long offset);
413
414 /** @see #putInt(Object, long, int) */
415 @IntrinsicCandidate
416 public native void putBoolean(Object o, long offset, boolean x);
417
418 /** @see #getInt(Object, long) */
419 @IntrinsicCandidate
420 public native byte getByte(Object o, long offset);
421
422 /** @see #putInt(Object, long, int) */
423 @IntrinsicCandidate
424 public native void putByte(Object o, long offset, byte x);
425
426 /** @see #getInt(Object, long) */
427 @IntrinsicCandidate
428 public native short getShort(Object o, long offset);
429
1379 }
1380
1381 /**
1382 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1383 * This is often needed in conjunction with obtaining the static field base
1384 * of a class.
1385 *
1386 * The call returns when either class {@code c} is fully initialized or
1387 * class {@code c} is being initialized and the call is performed from
1388 * the initializing thread. In the latter case a subsequent call to
1389 * {@link #shouldBeInitialized} will return {@code true}.
1390 */
1391 public void ensureClassInitialized(Class<?> c) {
1392 if (c == null) {
1393 throw new NullPointerException();
1394 }
1395
1396 ensureClassInitialized0(c);
1397 }
1398
1399 /**
1400 * The reading or writing of strict static fields may require
1401 * special processing. Notify the VM that such an event is about
1402 * to happen. The VM may respond by throwing an exception, in the
1403 * case of a read of an uninitialized field. If the VM allows the
1404 * method to return normally, no further calls are needed, with
1405 * the same arguments.
1406 */
1407 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1408 if (c == null) {
1409 throw new NullPointerException();
1410 }
1411 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1412 }
1413
1414 /**
1415 * Reports the offset of the first element in the storage allocation of a
1416 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1417 * for the same class, you may use that scale factor, together with this
1418 * base offset, to form new offsets to access elements of arrays of the
1419 * given class.
1420 * <p>
1421 * The return value is in the range of a {@code int}. The return type is
1422 * {@code long} to emphasize that long arithmetic should always be used
1423 * for offset calculations to avoid overflows.
1424 *
1425 * @see #getInt(Object, long)
1426 * @see #putInt(Object, long, int)
1427 */
1428 public long arrayBaseOffset(Class<?> arrayClass) {
1429 if (arrayClass == null) {
1430 throw new NullPointerException();
1431 }
1432
1433 return arrayBaseOffset0(arrayClass);
1434 }
1435
1436 public long arrayBaseOffset(Object[] array) {
1437 if (array == null) {
1438 throw new NullPointerException();
1439 }
1440
1441 return arrayBaseOffset1(array);
1442 }
1443
1444 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1445 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1446 = theUnsafe.arrayBaseOffset(boolean[].class);
1447
1448 /** The value of {@code arrayBaseOffset(byte[].class)} */
1449 public static final long ARRAY_BYTE_BASE_OFFSET
1450 = theUnsafe.arrayBaseOffset(byte[].class);
1451
1452 /** The value of {@code arrayBaseOffset(short[].class)} */
1453 public static final long ARRAY_SHORT_BASE_OFFSET
1454 = theUnsafe.arrayBaseOffset(short[].class);
1455
1456 /** The value of {@code arrayBaseOffset(char[].class)} */
1457 public static final long ARRAY_CHAR_BASE_OFFSET
1458 = theUnsafe.arrayBaseOffset(char[].class);
1459
1460 /** The value of {@code arrayBaseOffset(int[].class)} */
1461 public static final long ARRAY_INT_BASE_OFFSET
1462 = theUnsafe.arrayBaseOffset(int[].class);
1482 * allocation of a given array class. However, arrays of "narrow" types
1483 * will generally not work properly with accessors like {@link
1484 * #getByte(Object, long)}, so the scale factor for such classes is reported
1485 * as zero.
1486 * <p>
1487 * The computation of the actual memory offset should always use {@code
1488 * long} arithmetic to avoid overflows.
1489 *
1490 * @see #arrayBaseOffset
1491 * @see #getInt(Object, long)
1492 * @see #putInt(Object, long, int)
1493 */
1494 public int arrayIndexScale(Class<?> arrayClass) {
1495 if (arrayClass == null) {
1496 throw new NullPointerException();
1497 }
1498
1499 return arrayIndexScale0(arrayClass);
1500 }
1501
1502 public int arrayIndexScale(Object[] array) {
1503 if (array == null) {
1504 throw new NullPointerException();
1505 }
1506
1507 return arrayIndexScale1(array);
1508 }
1509
1510 /**
1511 * Return the size of the object in the heap.
1512 * @param o an object
1513 * @return the objects's size
1514 * @since Valhalla
1515 */
1516 public long getObjectSize(Object o) {
1517 if (o == null)
1518 throw new NullPointerException();
1519 return getObjectSize0(o);
1520 }
1521
1522 /** The value of {@code arrayIndexScale(boolean[].class)} */
1523 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1524 = theUnsafe.arrayIndexScale(boolean[].class);
1525
1526 /** The value of {@code arrayIndexScale(byte[].class)} */
1527 public static final int ARRAY_BYTE_INDEX_SCALE
1528 = theUnsafe.arrayIndexScale(byte[].class);
1529
1530 /** The value of {@code arrayIndexScale(short[].class)} */
1531 public static final int ARRAY_SHORT_INDEX_SCALE
1532 = theUnsafe.arrayIndexScale(short[].class);
1533
1534 /** The value of {@code arrayIndexScale(char[].class)} */
1535 public static final int ARRAY_CHAR_INDEX_SCALE
1536 = theUnsafe.arrayIndexScale(char[].class);
1537
1538 /** The value of {@code arrayIndexScale(int[].class)} */
1539 public static final int ARRAY_INT_INDEX_SCALE
1540 = theUnsafe.arrayIndexScale(int[].class);
1679 return null;
1680 }
1681
1682 /** Throws the exception without telling the verifier. */
1683 public native void throwException(Throwable ee);
1684
1685 /**
1686 * Atomically updates Java variable to {@code x} if it is currently
1687 * holding {@code expected}.
1688 *
1689 * <p>This operation has memory semantics of a {@code volatile} read
1690 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1691 *
1692 * @return {@code true} if successful
1693 */
1694 @IntrinsicCandidate
1695 public final native boolean compareAndSetReference(Object o, long offset,
1696 Object expected,
1697 Object x);
1698
1699 private final boolean isValueObject(Object o) {
1700 return o != null && o.getClass().isValue();
1701 }
1702
1703 /*
1704 * For value type, CAS should do substitutability test as opposed
1705 * to two pointers comparison.
1706 */
1707 @ForceInline
1708 public final <V> boolean compareAndSetReference(Object o, long offset,
1709 Class<?> type,
1710 V expected,
1711 V x) {
1712 if (type.isValue() || isValueObject(expected)) {
1713 while (true) {
1714 Object witness = getReferenceVolatile(o, offset);
1715 if (witness != expected) {
1716 return false;
1717 }
1718 if (compareAndSetReference(o, offset, witness, x)) {
1719 return true;
1720 }
1721 }
1722 } else {
1723 return compareAndSetReference(o, offset, expected, x);
1724 }
1725 }
1726
1727 @ForceInline
1728 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1729 int layout,
1730 Class<?> valueType,
1731 V expected,
1732 V x) {
1733 while (true) {
1734 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1735 if (witness != expected) {
1736 return false;
1737 }
1738 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1739 return true;
1740 }
1741 }
1742 }
1743
1744 @IntrinsicCandidate
1745 public final native Object compareAndExchangeReference(Object o, long offset,
1746 Object expected,
1747 Object x);
1748
1749 @ForceInline
1750 public final <V> Object compareAndExchangeReference(Object o, long offset,
1751 Class<?> valueType,
1752 V expected,
1753 V x) {
1754 if (valueType.isValue() || isValueObject(expected)) {
1755 while (true) {
1756 Object witness = getReferenceVolatile(o, offset);
1757 if (witness != expected) {
1758 return witness;
1759 }
1760 if (compareAndSetReference(o, offset, witness, x)) {
1761 return witness;
1762 }
1763 }
1764 } else {
1765 return compareAndExchangeReference(o, offset, expected, x);
1766 }
1767 }
1768
1769 @ForceInline
1770 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1771 int layout,
1772 Class<?> valueType,
1773 V expected,
1774 V x) {
1775 while (true) {
1776 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1777 if (witness != expected) {
1778 return witness;
1779 }
1780 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1781 return witness;
1782 }
1783 }
1784 }
1785
1786 @IntrinsicCandidate
1787 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1788 Object expected,
1789 Object x) {
1790 return compareAndExchangeReference(o, offset, expected, x);
1791 }
1792
1793 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1794 Class<?> valueType,
1795 V expected,
1796 V x) {
1797 return compareAndExchangeReference(o, offset, valueType, expected, x);
1798 }
1799
1800 @ForceInline
1801 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1802 int layout,
1803 Class<?> valueType,
1804 V expected,
1805 V x) {
1806 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1807 }
1808
1809 @IntrinsicCandidate
1810 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1811 Object expected,
1812 Object x) {
1813 return compareAndExchangeReference(o, offset, expected, x);
1814 }
1815
1816 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1817 Class<?> valueType,
1818 V expected,
1819 V x) {
1820 return compareAndExchangeReference(o, offset, valueType, expected, x);
1821 }
1822
1823 @ForceInline
1824 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1825 int layout,
1826 Class<?> valueType,
1827 V expected,
1828 V x) {
1829 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1830 }
1831
1832 @IntrinsicCandidate
1833 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1834 Object expected,
1835 Object x) {
1836 return compareAndSetReference(o, offset, expected, x);
1837 }
1838
1839 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1840 Class<?> valueType,
1841 V expected,
1842 V x) {
1843 if (valueType.isValue() || isValueObject(expected)) {
1844 return compareAndSetReference(o, offset, valueType, expected, x);
1845 } else {
1846 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1847 }
1848 }
1849
1850 @ForceInline
1851 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1852 int layout,
1853 Class<?> valueType,
1854 V expected,
1855 V x) {
1856 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1857 }
1858
1859 @IntrinsicCandidate
1860 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1861 Object expected,
1862 Object x) {
1863 return compareAndSetReference(o, offset, expected, x);
1864 }
1865
1866 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1867 Class<?> valueType,
1868 V expected,
1869 V x) {
1870 if (valueType.isValue() || isValueObject(expected)) {
1871 return compareAndSetReference(o, offset, valueType, expected, x);
1872 } else {
1873 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1874 }
1875 }
1876
1877 @ForceInline
1878 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1879 int layout,
1880 Class<?> valueType,
1881 V expected,
1882 V x) {
1883 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1884 }
1885
1886 @IntrinsicCandidate
1887 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1888 Object expected,
1889 Object x) {
1890 return compareAndSetReference(o, offset, expected, x);
1891 }
1892
1893 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1894 Class<?> valueType,
1895 V expected,
1896 V x) {
1897 if (valueType.isValue() || isValueObject(expected)) {
1898 return compareAndSetReference(o, offset, valueType, expected, x);
1899 } else {
1900 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1901 }
1902 }
1903
1904 @ForceInline
1905 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1906 int layout,
1907 Class<?> valueType,
1908 V expected,
1909 V x) {
1910 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1911 }
1912
1913 @IntrinsicCandidate
1914 public final boolean weakCompareAndSetReference(Object o, long offset,
1915 Object expected,
1916 Object x) {
1917 return compareAndSetReference(o, offset, expected, x);
1918 }
1919
1920 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1921 Class<?> valueType,
1922 V expected,
1923 V x) {
1924 if (valueType.isValue() || isValueObject(expected)) {
1925 return compareAndSetReference(o, offset, valueType, expected, x);
1926 } else {
1927 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1928 }
1929 }
1930
1931 @ForceInline
1932 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1933 int layout,
1934 Class<?> valueType,
1935 V expected,
1936 V x) {
1937 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1938 }
1939
1940 /**
1941 * Atomically updates Java variable to {@code x} if it is currently
1942 * holding {@code expected}.
1943 *
1944 * <p>This operation has memory semantics of a {@code volatile} read
1945 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1946 *
1947 * @return {@code true} if successful
1948 */
1949 @IntrinsicCandidate
1950 public final native boolean compareAndSetInt(Object o, long offset,
1951 int expected,
1952 int x);
1953
1954 @IntrinsicCandidate
1955 public final native int compareAndExchangeInt(Object o, long offset,
1956 int expected,
1957 int x);
1958
1959 @IntrinsicCandidate
2535 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2536 long expected,
2537 long x) {
2538 return compareAndSetLong(o, offset, expected, x);
2539 }
2540
2541 @IntrinsicCandidate
2542 public final boolean weakCompareAndSetLong(Object o, long offset,
2543 long expected,
2544 long x) {
2545 return compareAndSetLong(o, offset, expected, x);
2546 }
2547
2548 /**
2549 * Fetches a reference value from a given Java variable, with volatile
2550 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2551 */
2552 @IntrinsicCandidate
2553 public native Object getReferenceVolatile(Object o, long offset);
2554
2555 @ForceInline
2556 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2557 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2558 Object res = getFlatValue(o, offset, layout, valueType);
2559 fullFence();
2560 return res;
2561 }
2562
2563 /**
2564 * Stores a reference value into a given Java variable, with
2565 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2566 */
2567 @IntrinsicCandidate
2568 public native void putReferenceVolatile(Object o, long offset, Object x);
2569
2570 @ForceInline
2571 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2572 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2573 putFlatValueRelease(o, offset, layout, valueType, x);
2574 fullFence();
2575 }
2576
2577 /** Volatile version of {@link #getInt(Object, long)} */
2578 @IntrinsicCandidate
2579 public native int getIntVolatile(Object o, long offset);
2580
2581 /** Volatile version of {@link #putInt(Object, long, int)} */
2582 @IntrinsicCandidate
2583 public native void putIntVolatile(Object o, long offset, int x);
2584
2585 /** Volatile version of {@link #getBoolean(Object, long)} */
2586 @IntrinsicCandidate
2587 public native boolean getBooleanVolatile(Object o, long offset);
2588
2589 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2590 @IntrinsicCandidate
2591 public native void putBooleanVolatile(Object o, long offset, boolean x);
2592
2593 /** Volatile version of {@link #getByte(Object, long)} */
2594 @IntrinsicCandidate
2595 public native byte getByteVolatile(Object o, long offset);
2596
2629 /** Volatile version of {@link #putFloat(Object, long, float)} */
2630 @IntrinsicCandidate
2631 public native void putFloatVolatile(Object o, long offset, float x);
2632
2633 /** Volatile version of {@link #getDouble(Object, long)} */
2634 @IntrinsicCandidate
2635 public native double getDoubleVolatile(Object o, long offset);
2636
2637 /** Volatile version of {@link #putDouble(Object, long, double)} */
2638 @IntrinsicCandidate
2639 public native void putDoubleVolatile(Object o, long offset, double x);
2640
2641
2642
2643 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2644 @IntrinsicCandidate
2645 public final Object getReferenceAcquire(Object o, long offset) {
2646 return getReferenceVolatile(o, offset);
2647 }
2648
2649 @ForceInline
2650 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2651 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2652 Object res = getFlatValue(o, offset, layout, valueType);
2653 loadFence();
2654 return res;
2655 }
2656
2657 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2658 @IntrinsicCandidate
2659 public final boolean getBooleanAcquire(Object o, long offset) {
2660 return getBooleanVolatile(o, offset);
2661 }
2662
2663 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2664 @IntrinsicCandidate
2665 public final byte getByteAcquire(Object o, long offset) {
2666 return getByteVolatile(o, offset);
2667 }
2668
2669 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2670 @IntrinsicCandidate
2671 public final short getShortAcquire(Object o, long offset) {
2672 return getShortVolatile(o, offset);
2673 }
2674
2675 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2676 @IntrinsicCandidate
2701 public final double getDoubleAcquire(Object o, long offset) {
2702 return getDoubleVolatile(o, offset);
2703 }
2704
2705 /*
2706 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2707 * that do not guarantee immediate visibility of the store to
2708 * other threads. This method is generally only useful if the
2709 * underlying field is a Java volatile (or if an array cell, one
2710 * that is otherwise only accessed using volatile accesses).
2711 *
2712 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2713 */
2714
2715 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2716 @IntrinsicCandidate
2717 public final void putReferenceRelease(Object o, long offset, Object x) {
2718 putReferenceVolatile(o, offset, x);
2719 }
2720
2721 @ForceInline
2722 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2723 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2724 storeFence();
2725 putFlatValue(o, offset, layout, valueType, x);
2726 }
2727
2728 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2729 @IntrinsicCandidate
2730 public final void putBooleanRelease(Object o, long offset, boolean x) {
2731 putBooleanVolatile(o, offset, x);
2732 }
2733
2734 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2735 @IntrinsicCandidate
2736 public final void putByteRelease(Object o, long offset, byte x) {
2737 putByteVolatile(o, offset, x);
2738 }
2739
2740 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2741 @IntrinsicCandidate
2742 public final void putShortRelease(Object o, long offset, short x) {
2743 putShortVolatile(o, offset, x);
2744 }
2745
2746 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2747 @IntrinsicCandidate
2764 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2765 @IntrinsicCandidate
2766 public final void putLongRelease(Object o, long offset, long x) {
2767 putLongVolatile(o, offset, x);
2768 }
2769
2770 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2771 @IntrinsicCandidate
2772 public final void putDoubleRelease(Object o, long offset, double x) {
2773 putDoubleVolatile(o, offset, x);
2774 }
2775
2776 // ------------------------------ Opaque --------------------------------------
2777
2778 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2779 @IntrinsicCandidate
2780 public final Object getReferenceOpaque(Object o, long offset) {
2781 return getReferenceVolatile(o, offset);
2782 }
2783
2784 @ForceInline
2785 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2786 // this is stronger than opaque semantics
2787 return getFlatValueAcquire(o, offset, layout, valueType);
2788 }
2789
2790 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2791 @IntrinsicCandidate
2792 public final boolean getBooleanOpaque(Object o, long offset) {
2793 return getBooleanVolatile(o, offset);
2794 }
2795
2796 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2797 @IntrinsicCandidate
2798 public final byte getByteOpaque(Object o, long offset) {
2799 return getByteVolatile(o, offset);
2800 }
2801
2802 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2803 @IntrinsicCandidate
2804 public final short getShortOpaque(Object o, long offset) {
2805 return getShortVolatile(o, offset);
2806 }
2807
2808 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2809 @IntrinsicCandidate
2824 }
2825
2826 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2827 @IntrinsicCandidate
2828 public final long getLongOpaque(Object o, long offset) {
2829 return getLongVolatile(o, offset);
2830 }
2831
2832 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2833 @IntrinsicCandidate
2834 public final double getDoubleOpaque(Object o, long offset) {
2835 return getDoubleVolatile(o, offset);
2836 }
2837
2838 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2839 @IntrinsicCandidate
2840 public final void putReferenceOpaque(Object o, long offset, Object x) {
2841 putReferenceVolatile(o, offset, x);
2842 }
2843
2844 @ForceInline
2845 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2846 // this is stronger than opaque semantics
2847 putFlatValueRelease(o, offset, layout, valueType, x);
2848 }
2849
2850 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2851 @IntrinsicCandidate
2852 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2853 putBooleanVolatile(o, offset, x);
2854 }
2855
2856 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2857 @IntrinsicCandidate
2858 public final void putByteOpaque(Object o, long offset, byte x) {
2859 putByteVolatile(o, offset, x);
2860 }
2861
2862 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2863 @IntrinsicCandidate
2864 public final void putShortOpaque(Object o, long offset, short x) {
2865 putShortVolatile(o, offset, x);
2866 }
2867
2868 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2869 @IntrinsicCandidate
2878 }
2879
2880 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2881 @IntrinsicCandidate
2882 public final void putFloatOpaque(Object o, long offset, float x) {
2883 putFloatVolatile(o, offset, x);
2884 }
2885
2886 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2887 @IntrinsicCandidate
2888 public final void putLongOpaque(Object o, long offset, long x) {
2889 putLongVolatile(o, offset, x);
2890 }
2891
2892 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2893 @IntrinsicCandidate
2894 public final void putDoubleOpaque(Object o, long offset, double x) {
2895 putDoubleVolatile(o, offset, x);
2896 }
2897
2898 @ForceInline
2899 private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2900 // We turn the payload of an atomic value into a numeric value (of suitable type)
2901 // by storing the value into an array element (of matching layout) and by reading
2902 // back the array element as an integral value. After which we can implement the CAS
2903 // as a plain numeric CAS. Note: this only works if the payload contains no oops
2904 // (see VarHandles::isAtomicFlat).
2905 Object[] expectedArray = newSpecialArray(valueType, 1, layout);
2906 Object xArray = newSpecialArray(valueType, 1, layout);
2907 long base = arrayBaseOffset(expectedArray);
2908 int scale = arrayIndexScale(expectedArray);
2909 putFlatValue(expectedArray, base, layout, valueType, expected);
2910 putFlatValue(xArray, base, layout, valueType, x);
2911 switch (scale) {
2912 case 1: {
2913 byte expectedByte = getByte(expectedArray, base);
2914 byte xByte = getByte(xArray, base);
2915 return compareAndSetByte(o, offset, expectedByte, xByte);
2916 }
2917 case 2: {
2918 short expectedShort = getShort(expectedArray, base);
2919 short xShort = getShort(xArray, base);
2920 return compareAndSetShort(o, offset, expectedShort, xShort);
2921 }
2922 case 4: {
2923 int expectedInt = getInt(expectedArray, base);
2924 int xInt = getInt(xArray, base);
2925 return compareAndSetInt(o, offset, expectedInt, xInt);
2926 }
2927 case 8: {
2928 long expectedLong = getLong(expectedArray, base);
2929 long xLong = getLong(xArray, base);
2930 return compareAndSetLong(o, offset, expectedLong, xLong);
2931 }
2932 default: {
2933 throw new UnsupportedOperationException();
2934 }
2935 }
2936 }
2937
2938 /**
2939 * Unblocks the given thread blocked on {@code park}, or, if it is
2940 * not blocked, causes the subsequent call to {@code park} not to
2941 * block. Note: this operation is "unsafe" solely because the
2942 * caller must somehow ensure that the thread has not been
2943 * destroyed. Nothing special is usually required to ensure this
2944 * when called from Java (in which there will ordinarily be a live
2945 * reference to the thread) but this is not nearly-automatically
2946 * so when calling from native code.
2947 *
2948 * @param thread the thread to unpark.
2949 */
2950 @IntrinsicCandidate
2951 public native void unpark(Object thread);
2952
2953 /**
2954 * Blocks current thread, returning when a balancing
2955 * {@code unpark} occurs, or a balancing {@code unpark} has
2956 * already occurred, or the thread is interrupted, or, if not
2957 * absolute and time is not zero, the given time nanoseconds have
3304 /**
3305 * Atomically exchanges the given reference value with the current
3306 * reference value of a field or array element within the given
3307 * object {@code o} at the given {@code offset}.
3308 *
3309 * @param o object/array to update the field/element in
3310 * @param offset field/element offset
3311 * @param newValue new value
3312 * @return the previous value
3313 * @since 1.8
3314 */
3315 @IntrinsicCandidate
3316 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3317 Object v;
3318 do {
3319 v = getReferenceVolatile(o, offset);
3320 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3321 return v;
3322 }
3323
3324 @ForceInline
3325 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3326 Object v;
3327 do {
3328 v = getReferenceVolatile(o, offset);
3329 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3330 return v;
3331 }
3332
3333 @ForceInline
3334 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3335 Object v;
3336 do {
3337 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3338 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3339 return v;
3340 }
3341
3342 @ForceInline
3343 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3344 Object v;
3345 do {
3346 v = getReference(o, offset);
3347 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3348 return v;
3349 }
3350
3351 @ForceInline
3352 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3353 return getAndSetReference(o, offset, valueType, newValue);
3354 }
3355
3356 @ForceInline
3357 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3358 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3359 }
3360
3361 @ForceInline
3362 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3363 Object v;
3364 do {
3365 v = getReferenceAcquire(o, offset);
3366 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3367 return v;
3368 }
3369
3370 @ForceInline
3371 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3372 return getAndSetReference(o, offset, valueType, newValue);
3373 }
3374
3375 @ForceInline
3376 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3377 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3378 }
3379
3380 @IntrinsicCandidate
3381 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3382 byte v;
3383 do {
3384 v = getByteVolatile(o, offset);
3385 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3386 return v;
3387 }
3388
3389 @ForceInline
3390 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3391 byte v;
3392 do {
3393 v = getByte(o, offset);
3394 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3395 return v;
3396 }
3397
3398 @ForceInline
3399 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4415 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4416 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4417 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4418
4419
4420
4421 private native long allocateMemory0(long bytes);
4422 private native long reallocateMemory0(long address, long bytes);
4423 private native void freeMemory0(long address);
4424 @IntrinsicCandidate
4425 private native void setMemory0(Object o, long offset, long bytes, byte value);
4426 @IntrinsicCandidate
4427 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4428 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4429 private native long objectFieldOffset0(Field f); // throws IAE
4430 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
4431 private native long staticFieldOffset0(Field f); // throws IAE
4432 private native Object staticFieldBase0(Field f); // throws IAE
4433 private native boolean shouldBeInitialized0(Class<?> c);
4434 private native void ensureClassInitialized0(Class<?> c);
4435 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4436 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4437 private native int arrayBaseOffset1(Object[] array);
4438 private native int arrayIndexScale0(Class<?> arrayClass);
4439 private native int arrayIndexScale1(Object[] array);
4440 private native long getObjectSize0(Object o);
4441 private native int getLoadAverage0(double[] loadavg, int nelems);
4442
4443
4444 /**
4445 * Invokes the given direct byte buffer's cleaner, if any.
4446 *
4447 * @param directBuffer a direct byte buffer
4448 * @throws NullPointerException if {@code directBuffer} is null
4449 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4450 * or is a {@link java.nio.Buffer#slice slice}, or is a
4451 * {@link java.nio.Buffer#duplicate duplicate}
4452 */
4453 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4454 if (!directBuffer.isDirect())
4455 throw new IllegalArgumentException("buffer is non-direct");
4456
4457 DirectBuffer db = (DirectBuffer) directBuffer;
4458 if (db.attachment() != null)
4459 throw new IllegalArgumentException("duplicate or slice");
4460
|