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);
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();
|
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 @IntrinsicCandidate
249 private native int arrayLayout0(Object[] array);
250
251
252 /* Reports the kind of layout used for a given field in the storage
253 * allocation of its class. Do not expect to perform any logic
254 * or layout control with this value, it is just an opaque token
255 * used for performance reasons.
256 *
257 * A layout of 0 indicates this field is not flat.
258 */
259 public int fieldLayout(Field f) {
260 if (f == null) {
261 throw new NullPointerException();
262 }
263 return fieldLayout0(f);
264 }
265
266 private native int fieldLayout0(Object o);
267
268 public native Object[] newSpecialArray(Class<?> componentType,
269 int length, int layoutKind);
270
271 /**
272 * Fetches a reference value from a given Java variable.
273 * This method can return a reference to either an object or value
274 * or a null reference.
275 *
276 * @see #getInt(Object, long)
277 */
278 @IntrinsicCandidate
279 public native Object getReference(Object o, long offset);
280
281 /**
282 * Stores a reference value into a given Java variable.
283 * This method can store a reference to either an object or value
284 * or a null reference.
285 * <p>
286 * Unless the reference {@code x} being stored is either null
287 * or matches the field type, the results are undefined.
288 * If the reference {@code o} is non-null, card marks or
289 * other store barriers for that object (if the VM requires them)
290 * are updated.
291 * @see #putInt(Object, long, int)
292 */
293 @IntrinsicCandidate
294 public native void putReference(Object o, long offset, Object x);
295
296 /**
297 * Fetches a value of type {@code <V>} from a given Java variable.
298 * More specifically, fetches a field or array element within the given
299 * {@code o} object at the given offset, or (if {@code o} is null)
300 * from the memory address whose numerical value is the given offset.
301 *
302 * @param o Java heap object in which the variable resides, if any, else
303 * null
304 * @param offset indication of where the variable resides in a Java heap
305 * object, if any, else a memory address locating the variable
306 * statically
307 * @param valueType value type
308 * @param <V> the type of a value
309 * @return the value fetched from the indicated Java variable
310 * @throws RuntimeException No defined exceptions are thrown, not even
311 * {@link NullPointerException}
312 */
313 @IntrinsicCandidate
314 public native <V> V getValue(Object o, long offset, Class<?> valueType);
315
316 /**
317 * Fetches a value of type {@code <V>} from a given Java variable.
318 * More specifically, fetches a field or array element within the given
319 * {@code o} object at the given offset, or (if {@code o} is null)
320 * from the memory address whose numerical value is the given offset.
321 *
322 * @param o Java heap object in which the variable resides, if any, else
323 * null
324 * @param offset indication of where the variable resides in a Java heap
325 * object, if any, else a memory address locating the variable
326 * statically
327 * @param layoutKind opaque value used by the VM to know the layout
328 * the field or array element. This value must be retrieved with
329 * {@link #fieldLayout} or {@link #arrayLayout}.
330 * @param valueType value type
331 * @param <V> the type of a value
332 * @return the value fetched from the indicated Java variable
333 * @throws RuntimeException No defined exceptions are thrown, not even
334 * {@link NullPointerException}
335 */
336 @IntrinsicCandidate
337 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
338
339
340 /**
341 * Stores the given value into a given Java variable.
342 *
343 * Unless the reference {@code o} being stored is either null
344 * or matches the field type, the results are undefined.
345 *
346 * @param o Java heap object in which the variable resides, if any, else
347 * null
348 * @param offset indication of where the variable resides in a Java heap
349 * object, if any, else a memory address locating the variable
350 * statically
351 * @param valueType value type
352 * @param v the value to store into the indicated Java variable
353 * @param <V> the type of a value
354 * @throws RuntimeException No defined exceptions are thrown, not even
355 * {@link NullPointerException}
356 */
357 @IntrinsicCandidate
358 public native <V> void putValue(Object o, long offset, Class<?> valueType, V v);
359
360 /**
361 * Stores the given value into a given Java variable.
362 *
363 * Unless the reference {@code o} being stored is either null
364 * or matches the field type, the results are undefined.
365 *
366 * @param o Java heap object in which the variable resides, if any, else
367 * null
368 * @param offset indication of where the variable resides in a Java heap
369 * object, if any, else a memory address locating the variable
370 * statically
371 * @param layoutKind opaque value used by the VM to know the layout
372 * the field or array element. This value must be retrieved with
373 * {@link #fieldLayout} or {@link #arrayLayout}.
374 * @param valueType value type
375 * @param v the value to store into the indicated Java variable
376 * @param <V> the type of a value
377 * @throws RuntimeException No defined exceptions are thrown, not even
378 * {@link NullPointerException}
379 */
380 @IntrinsicCandidate
381 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
382
383 /**
384 * Returns an object instance with a private buffered value whose layout
385 * and contents is exactly the given value instance. The return object
386 * is in the larval state that can be updated using the unsafe put operation.
387 *
388 * @param value a value instance
389 * @param <V> the type of the given value instance
390 */
391 @IntrinsicCandidate
392 public native <V> V makePrivateBuffer(V value);
393
394 /**
395 * Exits the larval state and returns a value instance.
396 *
397 * @param value a value instance
398 * @param <V> the type of the given value instance
399 */
400 @IntrinsicCandidate
401 public native <V> V finishPrivateBuffer(V value);
402
403 /**
404 * Returns the header size of the given value type.
405 *
406 * @param valueType value type
407 * @return the header size of the value type
408 */
409 public native <V> long valueHeaderSize(Class<V> valueType);
410
411 /** @see #getInt(Object, long) */
412 @IntrinsicCandidate
413 public native boolean getBoolean(Object o, long offset);
414
415 /** @see #putInt(Object, long, int) */
416 @IntrinsicCandidate
417 public native void putBoolean(Object o, long offset, boolean x);
418
419 /** @see #getInt(Object, long) */
420 @IntrinsicCandidate
421 public native byte getByte(Object o, long offset);
422
423 /** @see #putInt(Object, long, int) */
424 @IntrinsicCandidate
425 public native void putByte(Object o, long offset, byte x);
426
427 /** @see #getInt(Object, long) */
428 @IntrinsicCandidate
429 public native short getShort(Object o, long offset);
430
1380 }
1381
1382 /**
1383 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1384 * This is often needed in conjunction with obtaining the static field base
1385 * of a class.
1386 *
1387 * The call returns when either class {@code c} is fully initialized or
1388 * class {@code c} is being initialized and the call is performed from
1389 * the initializing thread. In the latter case a subsequent call to
1390 * {@link #shouldBeInitialized} will return {@code true}.
1391 */
1392 public void ensureClassInitialized(Class<?> c) {
1393 if (c == null) {
1394 throw new NullPointerException();
1395 }
1396
1397 ensureClassInitialized0(c);
1398 }
1399
1400 /**
1401 * The reading or writing of strict static fields may require
1402 * special processing. Notify the VM that such an event is about
1403 * to happen. The VM may respond by throwing an exception, in the
1404 * case of a read of an uninitialized field. If the VM allows the
1405 * method to return normally, no further calls are needed, with
1406 * the same arguments.
1407 */
1408 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1409 if (c == null) {
1410 throw new NullPointerException();
1411 }
1412 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1413 }
1414
1415 /**
1416 * Reports the offset of the first element in the storage allocation of a
1417 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1418 * for the same class, you may use that scale factor, together with this
1419 * base offset, to form new offsets to access elements of arrays of the
1420 * given class.
1421 * <p>
1422 * The return value is in the range of a {@code int}. The return type is
1423 * {@code long} to emphasize that long arithmetic should always be used
1424 * for offset calculations to avoid overflows.
1425 * <p>
1426 * This method doesn't support arrays with an element type that is
1427 * a value class, because this type of array can have multiple layouts.
1428 * For these arrays, {@code arrayInstanceBaseOffset(Object[] array)}
1429 * must be used instead.
1430 *
1431 * @see #getInt(Object, long)
1432 * @see #putInt(Object, long, int)
1433 */
1434 public long arrayBaseOffset(Class<?> arrayClass) {
1435 if (arrayClass == null) {
1436 throw new NullPointerException();
1437 }
1438
1439 return arrayBaseOffset0(arrayClass);
1440 }
1441
1442 public long arrayInstanceBaseOffset(Object[] array) {
1443 if (array == null) {
1444 throw new NullPointerException();
1445 }
1446
1447 return arrayInstanceBaseOffset0(array);
1448 }
1449
1450 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1451 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1452 = theUnsafe.arrayBaseOffset(boolean[].class);
1453
1454 /** The value of {@code arrayBaseOffset(byte[].class)} */
1455 public static final long ARRAY_BYTE_BASE_OFFSET
1456 = theUnsafe.arrayBaseOffset(byte[].class);
1457
1458 /** The value of {@code arrayBaseOffset(short[].class)} */
1459 public static final long ARRAY_SHORT_BASE_OFFSET
1460 = theUnsafe.arrayBaseOffset(short[].class);
1461
1462 /** The value of {@code arrayBaseOffset(char[].class)} */
1463 public static final long ARRAY_CHAR_BASE_OFFSET
1464 = theUnsafe.arrayBaseOffset(char[].class);
1465
1466 /** The value of {@code arrayBaseOffset(int[].class)} */
1467 public static final long ARRAY_INT_BASE_OFFSET
1468 = theUnsafe.arrayBaseOffset(int[].class);
1475 public static final long ARRAY_FLOAT_BASE_OFFSET
1476 = theUnsafe.arrayBaseOffset(float[].class);
1477
1478 /** The value of {@code arrayBaseOffset(double[].class)} */
1479 public static final long ARRAY_DOUBLE_BASE_OFFSET
1480 = theUnsafe.arrayBaseOffset(double[].class);
1481
1482 /** The value of {@code arrayBaseOffset(Object[].class)} */
1483 public static final long ARRAY_OBJECT_BASE_OFFSET
1484 = theUnsafe.arrayBaseOffset(Object[].class);
1485
1486 /**
1487 * Reports the scale factor for addressing elements in the storage
1488 * allocation of a given array class. However, arrays of "narrow" types
1489 * will generally not work properly with accessors like {@link
1490 * #getByte(Object, long)}, so the scale factor for such classes is reported
1491 * as zero.
1492 * <p>
1493 * The computation of the actual memory offset should always use {@code
1494 * long} arithmetic to avoid overflows.
1495 * <p>
1496 * This method doesn't support arrays with an element type that is
1497 * a value class, because this type of array can have multiple layouts.
1498 * For these arrays, {@code arrayInstanceIndexScale(Object[] array)}
1499 * must be used instead.
1500 *
1501 * @see #arrayBaseOffset
1502 * @see #getInt(Object, long)
1503 * @see #putInt(Object, long, int)
1504 */
1505 public int arrayIndexScale(Class<?> arrayClass) {
1506 if (arrayClass == null) {
1507 throw new NullPointerException();
1508 }
1509
1510 return arrayIndexScale0(arrayClass);
1511 }
1512
1513 public int arrayInstanceIndexScale(Object[] array) {
1514 if (array == null) {
1515 throw new NullPointerException();
1516 }
1517
1518 return arrayInstanceIndexScale0(array);
1519 }
1520
1521 public int[] getFieldMap(Class<? extends Object> c) {
1522 if (c == null) {
1523 throw new NullPointerException();
1524 }
1525 return getFieldMap0(c);
1526 }
1527
1528 /**
1529 * Return the size of the object in the heap.
1530 * @param o an object
1531 * @return the objects's size
1532 * @since Valhalla
1533 */
1534 public long getObjectSize(Object o) {
1535 if (o == null)
1536 throw new NullPointerException();
1537 return getObjectSize0(o);
1538 }
1539
1540 /** The value of {@code arrayIndexScale(boolean[].class)} */
1541 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1542 = theUnsafe.arrayIndexScale(boolean[].class);
1543
1544 /** The value of {@code arrayIndexScale(byte[].class)} */
1545 public static final int ARRAY_BYTE_INDEX_SCALE
1546 = theUnsafe.arrayIndexScale(byte[].class);
1547
1548 /** The value of {@code arrayIndexScale(short[].class)} */
1549 public static final int ARRAY_SHORT_INDEX_SCALE
1550 = theUnsafe.arrayIndexScale(short[].class);
1551
1552 /** The value of {@code arrayIndexScale(char[].class)} */
1553 public static final int ARRAY_CHAR_INDEX_SCALE
1554 = theUnsafe.arrayIndexScale(char[].class);
1555
1556 /** The value of {@code arrayIndexScale(int[].class)} */
1557 public static final int ARRAY_INT_INDEX_SCALE
1558 = theUnsafe.arrayIndexScale(int[].class);
1697 return null;
1698 }
1699
1700 /** Throws the exception without telling the verifier. */
1701 public native void throwException(Throwable ee);
1702
1703 /**
1704 * Atomically updates Java variable to {@code x} if it is currently
1705 * holding {@code expected}.
1706 *
1707 * <p>This operation has memory semantics of a {@code volatile} read
1708 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1709 *
1710 * @return {@code true} if successful
1711 */
1712 @IntrinsicCandidate
1713 public final native boolean compareAndSetReference(Object o, long offset,
1714 Object expected,
1715 Object x);
1716
1717 private final boolean isValueObject(Object o) {
1718 return o != null && o.getClass().isValue();
1719 }
1720
1721 /*
1722 * For value type, CAS should do substitutability test as opposed
1723 * to two pointers comparison.
1724 */
1725 @ForceInline
1726 public final <V> boolean compareAndSetReference(Object o, long offset,
1727 Class<?> type,
1728 V expected,
1729 V x) {
1730 if (type.isValue() || isValueObject(expected)) {
1731 while (true) {
1732 Object witness = getReferenceVolatile(o, offset);
1733 if (witness != expected) {
1734 return false;
1735 }
1736 if (compareAndSetReference(o, offset, witness, x)) {
1737 return true;
1738 }
1739 }
1740 } else {
1741 return compareAndSetReference(o, offset, expected, x);
1742 }
1743 }
1744
1745 @ForceInline
1746 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1747 int layout,
1748 Class<?> valueType,
1749 V expected,
1750 V x) {
1751 while (true) {
1752 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1753 if (witness != expected) {
1754 return false;
1755 }
1756 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1757 return true;
1758 }
1759 }
1760 }
1761
1762 @IntrinsicCandidate
1763 public final native Object compareAndExchangeReference(Object o, long offset,
1764 Object expected,
1765 Object x);
1766
1767 @ForceInline
1768 public final <V> Object compareAndExchangeReference(Object o, long offset,
1769 Class<?> valueType,
1770 V expected,
1771 V x) {
1772 if (valueType.isValue() || isValueObject(expected)) {
1773 while (true) {
1774 Object witness = getReferenceVolatile(o, offset);
1775 if (witness != expected) {
1776 return witness;
1777 }
1778 if (compareAndSetReference(o, offset, witness, x)) {
1779 return witness;
1780 }
1781 }
1782 } else {
1783 return compareAndExchangeReference(o, offset, expected, x);
1784 }
1785 }
1786
1787 @ForceInline
1788 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1789 int layout,
1790 Class<?> valueType,
1791 V expected,
1792 V x) {
1793 while (true) {
1794 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1795 if (witness != expected) {
1796 return witness;
1797 }
1798 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1799 return witness;
1800 }
1801 }
1802 }
1803
1804 @IntrinsicCandidate
1805 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1806 Object expected,
1807 Object x) {
1808 return compareAndExchangeReference(o, offset, expected, x);
1809 }
1810
1811 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1812 Class<?> valueType,
1813 V expected,
1814 V x) {
1815 return compareAndExchangeReference(o, offset, valueType, expected, x);
1816 }
1817
1818 @ForceInline
1819 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1820 int layout,
1821 Class<?> valueType,
1822 V expected,
1823 V x) {
1824 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1825 }
1826
1827 @IntrinsicCandidate
1828 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1829 Object expected,
1830 Object x) {
1831 return compareAndExchangeReference(o, offset, expected, x);
1832 }
1833
1834 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1835 Class<?> valueType,
1836 V expected,
1837 V x) {
1838 return compareAndExchangeReference(o, offset, valueType, expected, x);
1839 }
1840
1841 @ForceInline
1842 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1843 int layout,
1844 Class<?> valueType,
1845 V expected,
1846 V x) {
1847 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1848 }
1849
1850 @IntrinsicCandidate
1851 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1852 Object expected,
1853 Object x) {
1854 return compareAndSetReference(o, offset, expected, x);
1855 }
1856
1857 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1858 Class<?> valueType,
1859 V expected,
1860 V x) {
1861 if (valueType.isValue() || isValueObject(expected)) {
1862 return compareAndSetReference(o, offset, valueType, expected, x);
1863 } else {
1864 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1865 }
1866 }
1867
1868 @ForceInline
1869 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1870 int layout,
1871 Class<?> valueType,
1872 V expected,
1873 V x) {
1874 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1875 }
1876
1877 @IntrinsicCandidate
1878 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1879 Object expected,
1880 Object x) {
1881 return compareAndSetReference(o, offset, expected, x);
1882 }
1883
1884 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1885 Class<?> valueType,
1886 V expected,
1887 V x) {
1888 if (valueType.isValue() || isValueObject(expected)) {
1889 return compareAndSetReference(o, offset, valueType, expected, x);
1890 } else {
1891 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1892 }
1893 }
1894
1895 @ForceInline
1896 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1897 int layout,
1898 Class<?> valueType,
1899 V expected,
1900 V x) {
1901 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1902 }
1903
1904 @IntrinsicCandidate
1905 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1906 Object expected,
1907 Object x) {
1908 return compareAndSetReference(o, offset, expected, x);
1909 }
1910
1911 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1912 Class<?> valueType,
1913 V expected,
1914 V x) {
1915 if (valueType.isValue() || isValueObject(expected)) {
1916 return compareAndSetReference(o, offset, valueType, expected, x);
1917 } else {
1918 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1919 }
1920 }
1921
1922 @ForceInline
1923 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1924 int layout,
1925 Class<?> valueType,
1926 V expected,
1927 V x) {
1928 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1929 }
1930
1931 @IntrinsicCandidate
1932 public final boolean weakCompareAndSetReference(Object o, long offset,
1933 Object expected,
1934 Object x) {
1935 return compareAndSetReference(o, offset, expected, x);
1936 }
1937
1938 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1939 Class<?> valueType,
1940 V expected,
1941 V x) {
1942 if (valueType.isValue() || isValueObject(expected)) {
1943 return compareAndSetReference(o, offset, valueType, expected, x);
1944 } else {
1945 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1946 }
1947 }
1948
1949 @ForceInline
1950 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1951 int layout,
1952 Class<?> valueType,
1953 V expected,
1954 V x) {
1955 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1956 }
1957
1958 /**
1959 * Atomically updates Java variable to {@code x} if it is currently
1960 * holding {@code expected}.
1961 *
1962 * <p>This operation has memory semantics of a {@code volatile} read
1963 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1964 *
1965 * @return {@code true} if successful
1966 */
1967 @IntrinsicCandidate
1968 public final native boolean compareAndSetInt(Object o, long offset,
1969 int expected,
1970 int x);
1971
1972 @IntrinsicCandidate
1973 public final native int compareAndExchangeInt(Object o, long offset,
1974 int expected,
1975 int x);
1976
1977 @IntrinsicCandidate
2553 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2554 long expected,
2555 long x) {
2556 return compareAndSetLong(o, offset, expected, x);
2557 }
2558
2559 @IntrinsicCandidate
2560 public final boolean weakCompareAndSetLong(Object o, long offset,
2561 long expected,
2562 long x) {
2563 return compareAndSetLong(o, offset, expected, x);
2564 }
2565
2566 /**
2567 * Fetches a reference value from a given Java variable, with volatile
2568 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2569 */
2570 @IntrinsicCandidate
2571 public native Object getReferenceVolatile(Object o, long offset);
2572
2573 @ForceInline
2574 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2575 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2576 Object res = getFlatValue(o, offset, layout, valueType);
2577 fullFence();
2578 return res;
2579 }
2580
2581 /**
2582 * Stores a reference value into a given Java variable, with
2583 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2584 */
2585 @IntrinsicCandidate
2586 public native void putReferenceVolatile(Object o, long offset, Object x);
2587
2588 @ForceInline
2589 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2590 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2591 putFlatValueRelease(o, offset, layout, valueType, x);
2592 fullFence();
2593 }
2594
2595 /** Volatile version of {@link #getInt(Object, long)} */
2596 @IntrinsicCandidate
2597 public native int getIntVolatile(Object o, long offset);
2598
2599 /** Volatile version of {@link #putInt(Object, long, int)} */
2600 @IntrinsicCandidate
2601 public native void putIntVolatile(Object o, long offset, int x);
2602
2603 /** Volatile version of {@link #getBoolean(Object, long)} */
2604 @IntrinsicCandidate
2605 public native boolean getBooleanVolatile(Object o, long offset);
2606
2607 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2608 @IntrinsicCandidate
2609 public native void putBooleanVolatile(Object o, long offset, boolean x);
2610
2611 /** Volatile version of {@link #getByte(Object, long)} */
2612 @IntrinsicCandidate
2613 public native byte getByteVolatile(Object o, long offset);
2614
2647 /** Volatile version of {@link #putFloat(Object, long, float)} */
2648 @IntrinsicCandidate
2649 public native void putFloatVolatile(Object o, long offset, float x);
2650
2651 /** Volatile version of {@link #getDouble(Object, long)} */
2652 @IntrinsicCandidate
2653 public native double getDoubleVolatile(Object o, long offset);
2654
2655 /** Volatile version of {@link #putDouble(Object, long, double)} */
2656 @IntrinsicCandidate
2657 public native void putDoubleVolatile(Object o, long offset, double x);
2658
2659
2660
2661 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2662 @IntrinsicCandidate
2663 public final Object getReferenceAcquire(Object o, long offset) {
2664 return getReferenceVolatile(o, offset);
2665 }
2666
2667 @ForceInline
2668 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2669 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2670 Object res = getFlatValue(o, offset, layout, valueType);
2671 loadFence();
2672 return res;
2673 }
2674
2675 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2676 @IntrinsicCandidate
2677 public final boolean getBooleanAcquire(Object o, long offset) {
2678 return getBooleanVolatile(o, offset);
2679 }
2680
2681 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2682 @IntrinsicCandidate
2683 public final byte getByteAcquire(Object o, long offset) {
2684 return getByteVolatile(o, offset);
2685 }
2686
2687 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2688 @IntrinsicCandidate
2689 public final short getShortAcquire(Object o, long offset) {
2690 return getShortVolatile(o, offset);
2691 }
2692
2693 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2694 @IntrinsicCandidate
2719 public final double getDoubleAcquire(Object o, long offset) {
2720 return getDoubleVolatile(o, offset);
2721 }
2722
2723 /*
2724 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2725 * that do not guarantee immediate visibility of the store to
2726 * other threads. This method is generally only useful if the
2727 * underlying field is a Java volatile (or if an array cell, one
2728 * that is otherwise only accessed using volatile accesses).
2729 *
2730 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2731 */
2732
2733 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2734 @IntrinsicCandidate
2735 public final void putReferenceRelease(Object o, long offset, Object x) {
2736 putReferenceVolatile(o, offset, x);
2737 }
2738
2739 @ForceInline
2740 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2741 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2742 storeFence();
2743 putFlatValue(o, offset, layout, valueType, x);
2744 }
2745
2746 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2747 @IntrinsicCandidate
2748 public final void putBooleanRelease(Object o, long offset, boolean x) {
2749 putBooleanVolatile(o, offset, x);
2750 }
2751
2752 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2753 @IntrinsicCandidate
2754 public final void putByteRelease(Object o, long offset, byte x) {
2755 putByteVolatile(o, offset, x);
2756 }
2757
2758 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2759 @IntrinsicCandidate
2760 public final void putShortRelease(Object o, long offset, short x) {
2761 putShortVolatile(o, offset, x);
2762 }
2763
2764 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2765 @IntrinsicCandidate
2782 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2783 @IntrinsicCandidate
2784 public final void putLongRelease(Object o, long offset, long x) {
2785 putLongVolatile(o, offset, x);
2786 }
2787
2788 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2789 @IntrinsicCandidate
2790 public final void putDoubleRelease(Object o, long offset, double x) {
2791 putDoubleVolatile(o, offset, x);
2792 }
2793
2794 // ------------------------------ Opaque --------------------------------------
2795
2796 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2797 @IntrinsicCandidate
2798 public final Object getReferenceOpaque(Object o, long offset) {
2799 return getReferenceVolatile(o, offset);
2800 }
2801
2802 @ForceInline
2803 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2804 // this is stronger than opaque semantics
2805 return getFlatValueAcquire(o, offset, layout, valueType);
2806 }
2807
2808 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2809 @IntrinsicCandidate
2810 public final boolean getBooleanOpaque(Object o, long offset) {
2811 return getBooleanVolatile(o, offset);
2812 }
2813
2814 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2815 @IntrinsicCandidate
2816 public final byte getByteOpaque(Object o, long offset) {
2817 return getByteVolatile(o, offset);
2818 }
2819
2820 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2821 @IntrinsicCandidate
2822 public final short getShortOpaque(Object o, long offset) {
2823 return getShortVolatile(o, offset);
2824 }
2825
2826 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2827 @IntrinsicCandidate
2842 }
2843
2844 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2845 @IntrinsicCandidate
2846 public final long getLongOpaque(Object o, long offset) {
2847 return getLongVolatile(o, offset);
2848 }
2849
2850 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2851 @IntrinsicCandidate
2852 public final double getDoubleOpaque(Object o, long offset) {
2853 return getDoubleVolatile(o, offset);
2854 }
2855
2856 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2857 @IntrinsicCandidate
2858 public final void putReferenceOpaque(Object o, long offset, Object x) {
2859 putReferenceVolatile(o, offset, x);
2860 }
2861
2862 @ForceInline
2863 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2864 // this is stronger than opaque semantics
2865 putFlatValueRelease(o, offset, layout, valueType, x);
2866 }
2867
2868 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2869 @IntrinsicCandidate
2870 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2871 putBooleanVolatile(o, offset, x);
2872 }
2873
2874 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2875 @IntrinsicCandidate
2876 public final void putByteOpaque(Object o, long offset, byte x) {
2877 putByteVolatile(o, offset, x);
2878 }
2879
2880 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2881 @IntrinsicCandidate
2882 public final void putShortOpaque(Object o, long offset, short x) {
2883 putShortVolatile(o, offset, x);
2884 }
2885
2886 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2887 @IntrinsicCandidate
2896 }
2897
2898 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2899 @IntrinsicCandidate
2900 public final void putFloatOpaque(Object o, long offset, float x) {
2901 putFloatVolatile(o, offset, x);
2902 }
2903
2904 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2905 @IntrinsicCandidate
2906 public final void putLongOpaque(Object o, long offset, long x) {
2907 putLongVolatile(o, offset, x);
2908 }
2909
2910 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2911 @IntrinsicCandidate
2912 public final void putDoubleOpaque(Object o, long offset, double x) {
2913 putDoubleVolatile(o, offset, x);
2914 }
2915
2916 @ForceInline
2917 private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2918 // We turn the payload of an atomic value into a numeric value (of suitable type)
2919 // by storing the value into an array element (of matching layout) and by reading
2920 // back the array element as an integral value. After which we can implement the CAS
2921 // as a plain numeric CAS. Note: this only works if the payload contains no oops
2922 // (see VarHandles::isAtomicFlat).
2923 Object[] expectedArray = newSpecialArray(valueType, 1, layout);
2924 Object xArray = newSpecialArray(valueType, 1, layout);
2925 long base = arrayInstanceBaseOffset(expectedArray);
2926 int scale = arrayInstanceIndexScale(expectedArray);
2927 putFlatValue(expectedArray, base, layout, valueType, expected);
2928 putFlatValue(xArray, base, layout, valueType, x);
2929 switch (scale) {
2930 case 1: {
2931 byte expectedByte = getByte(expectedArray, base);
2932 byte xByte = getByte(xArray, base);
2933 return compareAndSetByte(o, offset, expectedByte, xByte);
2934 }
2935 case 2: {
2936 short expectedShort = getShort(expectedArray, base);
2937 short xShort = getShort(xArray, base);
2938 return compareAndSetShort(o, offset, expectedShort, xShort);
2939 }
2940 case 4: {
2941 int expectedInt = getInt(expectedArray, base);
2942 int xInt = getInt(xArray, base);
2943 return compareAndSetInt(o, offset, expectedInt, xInt);
2944 }
2945 case 8: {
2946 long expectedLong = getLong(expectedArray, base);
2947 long xLong = getLong(xArray, base);
2948 return compareAndSetLong(o, offset, expectedLong, xLong);
2949 }
2950 default: {
2951 throw new UnsupportedOperationException();
2952 }
2953 }
2954 }
2955
2956 /**
2957 * Unblocks the given thread blocked on {@code park}, or, if it is
2958 * not blocked, causes the subsequent call to {@code park} not to
2959 * block. Note: this operation is "unsafe" solely because the
2960 * caller must somehow ensure that the thread has not been
2961 * destroyed. Nothing special is usually required to ensure this
2962 * when called from Java (in which there will ordinarily be a live
2963 * reference to the thread) but this is not nearly-automatically
2964 * so when calling from native code.
2965 *
2966 * @param thread the thread to unpark.
2967 */
2968 @IntrinsicCandidate
2969 public native void unpark(Object thread);
2970
2971 /**
2972 * Blocks current thread, returning when a balancing
2973 * {@code unpark} occurs, or a balancing {@code unpark} has
2974 * already occurred, or the thread is interrupted, or, if not
2975 * absolute and time is not zero, the given time nanoseconds have
3322 /**
3323 * Atomically exchanges the given reference value with the current
3324 * reference value of a field or array element within the given
3325 * object {@code o} at the given {@code offset}.
3326 *
3327 * @param o object/array to update the field/element in
3328 * @param offset field/element offset
3329 * @param newValue new value
3330 * @return the previous value
3331 * @since 1.8
3332 */
3333 @IntrinsicCandidate
3334 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3335 Object v;
3336 do {
3337 v = getReferenceVolatile(o, offset);
3338 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3339 return v;
3340 }
3341
3342 @ForceInline
3343 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3344 Object v;
3345 do {
3346 v = getReferenceVolatile(o, offset);
3347 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3348 return v;
3349 }
3350
3351 @ForceInline
3352 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3353 Object v;
3354 do {
3355 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3356 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3357 return v;
3358 }
3359
3360 @ForceInline
3361 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3362 Object v;
3363 do {
3364 v = getReference(o, offset);
3365 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3366 return v;
3367 }
3368
3369 @ForceInline
3370 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3371 return getAndSetReference(o, offset, valueType, newValue);
3372 }
3373
3374 @ForceInline
3375 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3376 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3377 }
3378
3379 @ForceInline
3380 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3381 Object v;
3382 do {
3383 v = getReferenceAcquire(o, offset);
3384 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3385 return v;
3386 }
3387
3388 @ForceInline
3389 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3390 return getAndSetReference(o, offset, valueType, newValue);
3391 }
3392
3393 @ForceInline
3394 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3395 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3396 }
3397
3398 @IntrinsicCandidate
3399 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3400 byte v;
3401 do {
3402 v = getByteVolatile(o, offset);
3403 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3404 return v;
3405 }
3406
3407 @ForceInline
3408 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3409 byte v;
3410 do {
3411 v = getByte(o, offset);
3412 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3413 return v;
3414 }
3415
3416 @ForceInline
3417 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4433 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4434 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4435 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4436
4437
4438
4439 private native long allocateMemory0(long bytes);
4440 private native long reallocateMemory0(long address, long bytes);
4441 private native void freeMemory0(long address);
4442 @IntrinsicCandidate
4443 private native void setMemory0(Object o, long offset, long bytes, byte value);
4444 @IntrinsicCandidate
4445 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4446 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4447 private native long objectFieldOffset0(Field f); // throws IAE
4448 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
4449 private native long staticFieldOffset0(Field f); // throws IAE
4450 private native Object staticFieldBase0(Field f); // throws IAE
4451 private native boolean shouldBeInitialized0(Class<?> c);
4452 private native void ensureClassInitialized0(Class<?> c);
4453 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4454 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4455 @IntrinsicCandidate
4456 private native int arrayInstanceBaseOffset0(Object[] array);
4457 private native int arrayIndexScale0(Class<?> arrayClass);
4458 @IntrinsicCandidate
4459 private native int arrayInstanceIndexScale0(Object[] array);
4460 private native long getObjectSize0(Object o);
4461 private native int getLoadAverage0(double[] loadavg, int nelems);
4462 private native int[] getFieldMap0(Class <?> c);
4463
4464
4465 /**
4466 * Invokes the given direct byte buffer's cleaner, if any.
4467 *
4468 * @param directBuffer a direct byte buffer
4469 * @throws NullPointerException if {@code directBuffer} is null
4470 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4471 * or is a {@link java.nio.Buffer#slice slice}, or is a
4472 * {@link java.nio.Buffer#duplicate duplicate}
4473 */
4474 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4475 if (!directBuffer.isDirect())
4476 throw new IllegalArgumentException("buffer is non-direct");
4477
4478 DirectBuffer db = (DirectBuffer) directBuffer;
4479 if (db.attachment() != null)
4480 throw new IllegalArgumentException("duplicate or slice");
4481
4482 Cleaner cleaner = db.cleaner();
|