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 sun.misc;
27
28 import jdk.internal.HotSpotIntrinsicCandidate;
29 import jdk.internal.misc.VM;
30 import sun.reflect.CallerSensitive;
31 import sun.reflect.Reflection;
32
33 import java.lang.reflect.Field;
34 import java.security.ProtectionDomain;
35
36
37 /**
38 * A collection of methods for performing low-level, unsafe operations.
39 * Although the class and all methods are public, use of this class is
40 * limited because only trusted code can obtain instances of it.
41 *
42 * @author John R. Rose
43 * @see #getUnsafe
44 */
45
46 public final class Unsafe {
47
48 private static native void registerNatives();
49 static {
50 registerNatives();
51 sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
52 }
53
54 private Unsafe() {}
55
56 private static final Unsafe theUnsafe = new Unsafe();
57
58 /**
59 * Provides the caller with the capability of performing unsafe
60 * operations.
61 *
62 * <p>The returned {@code Unsafe} object should be carefully guarded
63 * by the caller, since it can be used to read and write data at arbitrary
64 * memory addresses. It must never be passed to untrusted code.
65 *
66 * <p>Most methods in this class are very low-level, and correspond to a
67 * small number of hardware instructions (on typical machines). Compilers
68 * are encouraged to optimize these methods accordingly.
69 *
70 * <p>Here is a suggested idiom for using unsafe operations:
71 *
72 * <pre> {@code
73 * class MyTrustedClass {
74 * private static final Unsafe unsafe = Unsafe.getUnsafe();
75 * ...
76 * private long myCountAddress = ...;
133 * for Java variables. When the object reference is null, this method
134 * uses its offset as an absolute address. This is similar in operation
135 * to methods such as {@link #getInt(long)}, which provide (in effect) a
136 * <em>single-register</em> addressing mode for non-Java variables.
137 * However, because Java variables may have a different layout in memory
138 * from non-Java variables, programmers should not assume that these
139 * two addressing modes are ever equivalent. Also, programmers should
140 * remember that offsets from the double-register addressing mode cannot
141 * be portably confused with longs used in the single-register addressing
142 * mode.
143 *
144 * @param o Java heap object in which the variable resides, if any, else
145 * null
146 * @param offset indication of where the variable resides in a Java heap
147 * object, if any, else a memory address locating the variable
148 * statically
149 * @return the value fetched from the indicated Java variable
150 * @throws RuntimeException No defined exceptions are thrown, not even
151 * {@link NullPointerException}
152 */
153 @HotSpotIntrinsicCandidate
154 public native int getInt(Object o, long offset);
155
156 /**
157 * Stores a value into a given Java variable.
158 * <p>
159 * The first two parameters are interpreted exactly as with
160 * {@link #getInt(Object, long)} to refer to a specific
161 * Java variable (field or array element). The given value
162 * is stored into that variable.
163 * <p>
164 * The variable must be of the same type as the method
165 * parameter {@code x}.
166 *
167 * @param o Java heap object in which the variable resides, if any, else
168 * null
169 * @param offset indication of where the variable resides in a Java heap
170 * object, if any, else a memory address locating the variable
171 * statically
172 * @param x the value to store into the indicated Java variable
173 * @throws RuntimeException No defined exceptions are thrown, not even
174 * {@link NullPointerException}
175 */
176 @HotSpotIntrinsicCandidate
177 public native void putInt(Object o, long offset, int x);
178
179 /**
180 * Fetches a reference value from a given Java variable.
181 * @see #getInt(Object, long)
182 */
183 @HotSpotIntrinsicCandidate
184 public native Object getObject(Object o, long offset);
185
186 /**
187 * Stores a reference value into a given Java variable.
188 * <p>
189 * Unless the reference {@code x} being stored is either null
190 * or matches the field type, the results are undefined.
191 * If the reference {@code o} is non-null, card marks or
192 * other store barriers for that object (if the VM requires them)
193 * are updated.
194 * @see #putInt(Object, long, int)
195 */
196 @HotSpotIntrinsicCandidate
197 public native void putObject(Object o, long offset, Object x);
198
199 /** @see #getInt(Object, long) */
200 @HotSpotIntrinsicCandidate
201 public native boolean getBoolean(Object o, long offset);
202 /** @see #putInt(Object, long, int) */
203 @HotSpotIntrinsicCandidate
204 public native void putBoolean(Object o, long offset, boolean x);
205 /** @see #getInt(Object, long) */
206 @HotSpotIntrinsicCandidate
207 public native byte getByte(Object o, long offset);
208 /** @see #putInt(Object, long, int) */
209 @HotSpotIntrinsicCandidate
210 public native void putByte(Object o, long offset, byte x);
211 /** @see #getInt(Object, long) */
212 @HotSpotIntrinsicCandidate
213 public native short getShort(Object o, long offset);
214 /** @see #putInt(Object, long, int) */
215 @HotSpotIntrinsicCandidate
216 public native void putShort(Object o, long offset, short x);
217 /** @see #getInt(Object, long) */
218 @HotSpotIntrinsicCandidate
219 public native char getChar(Object o, long offset);
220 /** @see #putInt(Object, long, int) */
221 @HotSpotIntrinsicCandidate
222 public native void putChar(Object o, long offset, char x);
223 /** @see #getInt(Object, long) */
224 @HotSpotIntrinsicCandidate
225 public native long getLong(Object o, long offset);
226 /** @see #putInt(Object, long, int) */
227 @HotSpotIntrinsicCandidate
228 public native void putLong(Object o, long offset, long x);
229 /** @see #getInt(Object, long) */
230 @HotSpotIntrinsicCandidate
231 public native float getFloat(Object o, long offset);
232 /** @see #putInt(Object, long, int) */
233 @HotSpotIntrinsicCandidate
234 public native void putFloat(Object o, long offset, float x);
235 /** @see #getInt(Object, long) */
236 @HotSpotIntrinsicCandidate
237 public native double getDouble(Object o, long offset);
238 /** @see #putInt(Object, long, int) */
239 @HotSpotIntrinsicCandidate
240 public native void putDouble(Object o, long offset, double x);
241
242 // These read VM internal data.
243
244 /**
245 * Fetches an uncompressed reference value from a given native variable
246 * ignoring the VM's compressed references mode.
247 *
248 * @param address a memory address locating the variable
249 * @return the value fetched from the indicated native variable
250 */
251 public native Object getUncompressedObject(long address);
252
253 /**
254 * Fetches the {@link java.lang.Class} Java mirror for the given native
255 * metaspace {@code Klass} pointer.
256 *
257 * @param metaspaceKlass a native metaspace {@code Klass} pointer
258 * @return the {@link java.lang.Class} Java mirror
259 */
260 public native Class<?> getJavaMirror(long metaspaceKlass);
261
262 /**
263 * Fetches a native metaspace {@code Klass} pointer for the given Java
264 * object.
265 *
266 * @param o Java heap object for which to fetch the class pointer
267 * @return a native metaspace {@code Klass} pointer
268 */
269 public native long getKlassPointer(Object o);
270
271 // These work on values in the C heap.
272
273 /**
274 * Fetches a value from a given memory address. If the address is zero, or
275 * does not point into a block obtained from {@link #allocateMemory}, the
276 * results are undefined.
277 *
278 * @see #allocateMemory
279 */
280 @HotSpotIntrinsicCandidate
281 public native byte getByte(long address);
282
283 /**
284 * Stores a value into a given memory address. If the address is zero, or
285 * does not point into a block obtained from {@link #allocateMemory}, the
286 * results are undefined.
287 *
288 * @see #getByte(long)
289 */
290 @HotSpotIntrinsicCandidate
291 public native void putByte(long address, byte x);
292
293 /** @see #getByte(long) */
294 @HotSpotIntrinsicCandidate
295 public native short getShort(long address);
296 /** @see #putByte(long, byte) */
297 @HotSpotIntrinsicCandidate
298 public native void putShort(long address, short x);
299 /** @see #getByte(long) */
300 @HotSpotIntrinsicCandidate
301 public native char getChar(long address);
302 /** @see #putByte(long, byte) */
303 @HotSpotIntrinsicCandidate
304 public native void putChar(long address, char x);
305 /** @see #getByte(long) */
306 @HotSpotIntrinsicCandidate
307 public native int getInt(long address);
308 /** @see #putByte(long, byte) */
309 @HotSpotIntrinsicCandidate
310 public native void putInt(long address, int x);
311 /** @see #getByte(long) */
312 @HotSpotIntrinsicCandidate
313 public native long getLong(long address);
314 /** @see #putByte(long, byte) */
315 @HotSpotIntrinsicCandidate
316 public native void putLong(long address, long x);
317 /** @see #getByte(long) */
318 @HotSpotIntrinsicCandidate
319 public native float getFloat(long address);
320 /** @see #putByte(long, byte) */
321 @HotSpotIntrinsicCandidate
322 public native void putFloat(long address, float x);
323 /** @see #getByte(long) */
324 @HotSpotIntrinsicCandidate
325 public native double getDouble(long address);
326 /** @see #putByte(long, byte) */
327 @HotSpotIntrinsicCandidate
328 public native void putDouble(long address, double x);
329
330 /**
331 * Fetches a native pointer from a given memory address. If the address is
332 * zero, or does not point into a block obtained from {@link
333 * #allocateMemory}, the results are undefined.
334 *
335 * <p>If the native pointer is less than 64 bits wide, it is extended as
336 * an unsigned number to a Java long. The pointer may be indexed by any
337 * given byte offset, simply by adding that offset (as a simple integer) to
338 * the long representing the pointer. The number of bytes actually read
339 * from the target address may be determined by consulting {@link
340 * #addressSize}.
341 *
342 * @see #allocateMemory
343 */
344 @HotSpotIntrinsicCandidate
345 public native long getAddress(long address);
346
347 /**
348 * Stores a native pointer into a given memory address. If the address is
349 * zero, or does not point into a block obtained from {@link
350 * #allocateMemory}, the results are undefined.
351 *
352 * <p>The number of bytes actually written at the target address may be
353 * determined by consulting {@link #addressSize}.
354 *
355 * @see #getAddress(long)
356 */
357 @HotSpotIntrinsicCandidate
358 public native void putAddress(long address, long x);
359
360 /// wrappers for malloc, realloc, free:
361
362 /**
363 * Allocates a new block of native memory, of the given size in bytes. The
364 * contents of the memory are uninitialized; they will generally be
365 * garbage. The resulting native pointer will never be zero, and will be
366 * aligned for all value types. Dispose of this memory by calling {@link
367 * #freeMemory}, or resize it with {@link #reallocateMemory}.
368 *
369 * @throws IllegalArgumentException if the size is negative or too large
370 * for the native size_t type
371 *
372 * @throws OutOfMemoryError if the allocation is refused by the system
373 *
374 * @see #getByte(long)
375 * @see #putByte(long, byte)
376 */
377 public native long allocateMemory(long bytes);
378
379 /**
380 * Resizes a new block of native memory, to the given size in bytes. The
381 * contents of the new block past the size of the old block are
382 * uninitialized; they will generally be garbage. The resulting native
383 * pointer will be zero if and only if the requested size is zero. The
384 * resulting native pointer will be aligned for all value types. Dispose
385 * of this memory by calling {@link #freeMemory}, or resize it with {@link
386 * #reallocateMemory}. The address passed to this method may be null, in
387 * which case an allocation will be performed.
388 *
389 * @throws IllegalArgumentException if the size is negative or too large
390 * for the native size_t type
391 *
392 * @throws OutOfMemoryError if the allocation is refused by the system
393 *
394 * @see #allocateMemory
395 */
396 public native long reallocateMemory(long address, long bytes);
397
398 /**
399 * Sets all bytes in a given block of memory to a fixed value
400 * (usually zero).
401 *
402 * <p>This method determines a block's base address by means of two parameters,
403 * and so it provides (in effect) a <em>double-register</em> addressing mode,
404 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
405 * the offset supplies an absolute base address.
406 *
407 * <p>The stores are in coherent (atomic) units of a size determined
408 * by the address and length parameters. If the effective address and
409 * length are all even modulo 8, the stores take place in 'long' units.
410 * If the effective address and length are (resp.) even modulo 4 or 2,
411 * the stores take place in units of 'int' or 'short'.
412 *
413 * @since 1.7
414 */
415 public native void setMemory(Object o, long offset, long bytes, byte value);
416
417 /**
418 * Sets all bytes in a given block of memory to a fixed value
419 * (usually zero). This provides a <em>single-register</em> addressing mode,
420 * as discussed in {@link #getInt(Object,long)}.
421 *
422 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
423 */
424 public void setMemory(long address, long bytes, byte value) {
425 setMemory(null, address, bytes, value);
426 }
427
428 /**
429 * Sets all bytes in a given block of memory to a copy of another
430 * block.
431 *
432 * <p>This method determines each block's base address by means of two parameters,
433 * and so it provides (in effect) a <em>double-register</em> addressing mode,
434 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
435 * the offset supplies an absolute base address.
436 *
437 * <p>The transfers are in coherent (atomic) units of a size determined
438 * by the address and length parameters. If the effective addresses and
439 * length are all even modulo 8, the transfer takes place in 'long' units.
440 * If the effective addresses and length are (resp.) even modulo 4 or 2,
441 * the transfer takes place in units of 'int' or 'short'.
442 *
443 * @since 1.7
444 */
445 @HotSpotIntrinsicCandidate
446 public native void copyMemory(Object srcBase, long srcOffset,
447 Object destBase, long destOffset,
448 long bytes);
449 /**
450 * Sets all bytes in a given block of memory to a copy of another
451 * block. This provides a <em>single-register</em> addressing mode,
452 * as discussed in {@link #getInt(Object,long)}.
453 *
454 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
455 */
456 public void copyMemory(long srcAddress, long destAddress, long bytes) {
457 copyMemory(null, srcAddress, null, destAddress, bytes);
458 }
459
460 /**
461 * Disposes of a block of native memory, as obtained from {@link
462 * #allocateMemory} or {@link #reallocateMemory}. The address passed to
463 * this method may be null, in which case no action is taken.
464 *
465 * @see #allocateMemory
466 */
467 public native void freeMemory(long address);
468
469 /// random queries
470
471 /**
472 * This constant differs from all results that will ever be returned from
473 * {@link #staticFieldOffset}, {@link #objectFieldOffset},
474 * or {@link #arrayBaseOffset}.
475 */
476 public static final int INVALID_FIELD_OFFSET = -1;
477
478 /**
479 * Reports the location of a given field in the storage allocation of its
480 * class. Do not expect to perform any sort of arithmetic on this offset;
481 * it is just a cookie which is passed to the unsafe heap memory accessors.
482 *
483 * <p>Any given field will always have the same offset and base, and no
484 * two distinct fields of the same class will ever have the same offset
485 * and base.
486 *
487 * <p>As of 1.4.1, offsets for fields are represented as long values,
488 * although the Sun JVM does not use the most significant 32 bits.
489 * However, JVM implementations which store static fields at absolute
490 * addresses can use long offsets and null base pointers to express
491 * the field locations in a form usable by {@link #getInt(Object,long)}.
492 * Therefore, code which will be ported to such JVMs on 64-bit platforms
493 * must preserve all bits of static field offsets.
494 * @see #getInt(Object, long)
495 */
496 public native long objectFieldOffset(Field f);
497
498 /**
499 * Reports the location of a given static field, in conjunction with {@link
500 * #staticFieldBase}.
501 * <p>Do not expect to perform any sort of arithmetic on this offset;
502 * it is just a cookie which is passed to the unsafe heap memory accessors.
503 *
504 * <p>Any given field will always have the same offset, and no two distinct
505 * fields of the same class will ever have the same offset.
506 *
507 * <p>As of 1.4.1, offsets for fields are represented as long values,
508 * although the Sun JVM does not use the most significant 32 bits.
509 * It is hard to imagine a JVM technology which needs more than
510 * a few bits to encode an offset within a non-array object,
511 * However, for consistency with other methods in this class,
512 * this method reports its result as a long value.
513 * @see #getInt(Object, long)
514 */
515 public native long staticFieldOffset(Field f);
516
517 /**
518 * Reports the location of a given static field, in conjunction with {@link
519 * #staticFieldOffset}.
520 * <p>Fetch the base "Object", if any, with which static fields of the
521 * given class can be accessed via methods like {@link #getInt(Object,
522 * long)}. This value may be null. This value may refer to an object
523 * which is a "cookie", not guaranteed to be a real Object, and it should
524 * not be used in any way except as argument to the get and put routines in
525 * this class.
526 */
527 public native Object staticFieldBase(Field f);
528
529 /**
530 * Detects if the given class may need to be initialized. This is often
531 * needed in conjunction with obtaining the static field base of a
532 * class.
533 * @return false only if a call to {@code ensureClassInitialized} would have no effect
534 */
535 public native boolean shouldBeInitialized(Class<?> c);
536
537 /**
538 * Ensures the given class has been initialized. This is often
539 * needed in conjunction with obtaining the static field base of a
540 * class.
541 */
542 public native void ensureClassInitialized(Class<?> c);
543
544 /**
545 * Reports the offset of the first element in the storage allocation of a
546 * given array class. If {@link #arrayIndexScale} returns a non-zero value
547 * for the same class, you may use that scale factor, together with this
548 * base offset, to form new offsets to access elements of arrays of the
549 * given class.
550 *
551 * @see #getInt(Object, long)
552 * @see #putInt(Object, long, int)
553 */
554 public native int arrayBaseOffset(Class<?> arrayClass);
555
556 /** The value of {@code arrayBaseOffset(boolean[].class)} */
557 public static final int ARRAY_BOOLEAN_BASE_OFFSET
558 = theUnsafe.arrayBaseOffset(boolean[].class);
559
560 /** The value of {@code arrayBaseOffset(byte[].class)} */
561 public static final int ARRAY_BYTE_BASE_OFFSET
562 = theUnsafe.arrayBaseOffset(byte[].class);
563
564 /** The value of {@code arrayBaseOffset(short[].class)} */
565 public static final int ARRAY_SHORT_BASE_OFFSET
566 = theUnsafe.arrayBaseOffset(short[].class);
567
568 /** The value of {@code arrayBaseOffset(char[].class)} */
569 public static final int ARRAY_CHAR_BASE_OFFSET
570 = theUnsafe.arrayBaseOffset(char[].class);
571
572 /** The value of {@code arrayBaseOffset(int[].class)} */
573 public static final int ARRAY_INT_BASE_OFFSET
574 = theUnsafe.arrayBaseOffset(int[].class);
575
576 /** The value of {@code arrayBaseOffset(long[].class)} */
577 public static final int ARRAY_LONG_BASE_OFFSET
578 = theUnsafe.arrayBaseOffset(long[].class);
579
580 /** The value of {@code arrayBaseOffset(float[].class)} */
581 public static final int ARRAY_FLOAT_BASE_OFFSET
582 = theUnsafe.arrayBaseOffset(float[].class);
583
584 /** The value of {@code arrayBaseOffset(double[].class)} */
585 public static final int ARRAY_DOUBLE_BASE_OFFSET
586 = theUnsafe.arrayBaseOffset(double[].class);
587
588 /** The value of {@code arrayBaseOffset(Object[].class)} */
589 public static final int ARRAY_OBJECT_BASE_OFFSET
590 = theUnsafe.arrayBaseOffset(Object[].class);
591
592 /**
593 * Reports the scale factor for addressing elements in the storage
594 * allocation of a given array class. However, arrays of "narrow" types
595 * will generally not work properly with accessors like {@link
596 * #getByte(Object, long)}, so the scale factor for such classes is reported
597 * as zero.
598 *
599 * @see #arrayBaseOffset
600 * @see #getInt(Object, long)
601 * @see #putInt(Object, long, int)
602 */
603 public native int arrayIndexScale(Class<?> arrayClass);
604
605 /** The value of {@code arrayIndexScale(boolean[].class)} */
606 public static final int ARRAY_BOOLEAN_INDEX_SCALE
607 = theUnsafe.arrayIndexScale(boolean[].class);
608
609 /** The value of {@code arrayIndexScale(byte[].class)} */
610 public static final int ARRAY_BYTE_INDEX_SCALE
611 = theUnsafe.arrayIndexScale(byte[].class);
612
613 /** The value of {@code arrayIndexScale(short[].class)} */
614 public static final int ARRAY_SHORT_INDEX_SCALE
615 = theUnsafe.arrayIndexScale(short[].class);
616
617 /** The value of {@code arrayIndexScale(char[].class)} */
618 public static final int ARRAY_CHAR_INDEX_SCALE
619 = theUnsafe.arrayIndexScale(char[].class);
620
621 /** The value of {@code arrayIndexScale(int[].class)} */
622 public static final int ARRAY_INT_INDEX_SCALE
623 = theUnsafe.arrayIndexScale(int[].class);
624
625 /** The value of {@code arrayIndexScale(long[].class)} */
626 public static final int ARRAY_LONG_INDEX_SCALE
627 = theUnsafe.arrayIndexScale(long[].class);
628
629 /** The value of {@code arrayIndexScale(float[].class)} */
630 public static final int ARRAY_FLOAT_INDEX_SCALE
631 = theUnsafe.arrayIndexScale(float[].class);
632
633 /** The value of {@code arrayIndexScale(double[].class)} */
634 public static final int ARRAY_DOUBLE_INDEX_SCALE
635 = theUnsafe.arrayIndexScale(double[].class);
636
637 /** The value of {@code arrayIndexScale(Object[].class)} */
638 public static final int ARRAY_OBJECT_INDEX_SCALE
639 = theUnsafe.arrayIndexScale(Object[].class);
640
641 /**
642 * Reports the size in bytes of a native pointer, as stored via {@link
643 * #putAddress}. This value will be either 4 or 8. Note that the sizes of
644 * other primitive types (as stored in native memory blocks) is determined
645 * fully by their information content.
646 */
647 public native int addressSize();
648
649 /** The value of {@code addressSize()} */
650 public static final int ADDRESS_SIZE = theUnsafe.addressSize();
651
652 /**
653 * Reports the size in bytes of a native memory page (whatever that is).
654 * This value will always be a power of two.
655 */
656 public native int pageSize();
657
658
659 /// random trusted operations from JNI:
660
661 /**
662 * Tells the VM to define a class, without security checks. By default, the
663 * class loader and protection domain come from the caller's class.
664 */
665 public native Class<?> defineClass(String name, byte[] b, int off, int len,
666 ClassLoader loader,
667 ProtectionDomain protectionDomain);
668
669 /**
670 * Defines a class but does not make it known to the class loader or system dictionary.
671 * <p>
672 * For each CP entry, the corresponding CP patch must either be null or have
673 * the a format that matches its tag:
674 * <ul>
675 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
676 * <li>Utf8: a string (must have suitable syntax if used as signature or name)
677 * <li>Class: any java.lang.Class object
678 * <li>String: any object (not just a java.lang.String)
679 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
680 * </ul>
681 * @param hostClass context for linkage, access control, protection domain, and class loader
682 * @param data bytes of a class file
683 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
684 */
685 public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
686
687 /**
688 * Allocates an instance but does not run any constructor.
689 * Initializes the class if it has not yet been.
690 */
691 @HotSpotIntrinsicCandidate
692 public native Object allocateInstance(Class<?> cls)
693 throws InstantiationException;
694
695 /** Throws the exception without telling the verifier. */
696 public native void throwException(Throwable ee);
697
698 /**
699 * Atomically updates Java variable to {@code x} if it is currently
700 * holding {@code expected}.
701 *
702 * <p>This operation has memory semantics of a {@code volatile} read
703 * and write. Corresponds to C11 atomic_compare_exchange_strong.
704 *
705 * @return {@code true} if successful
706 */
707 @HotSpotIntrinsicCandidate
708 public final native boolean compareAndSwapObject(Object o, long offset,
709 Object expected,
710 Object x);
711
712 /**
713 * Atomically updates Java variable to {@code x} if it is currently
714 * holding {@code expected}.
715 *
716 * <p>This operation has memory semantics of a {@code volatile} read
717 * and write. Corresponds to C11 atomic_compare_exchange_strong.
718 *
719 * @return {@code true} if successful
720 */
721 @HotSpotIntrinsicCandidate
722 public final native boolean compareAndSwapInt(Object o, long offset,
723 int expected,
724 int x);
725
726 /**
727 * Atomically updates Java variable to {@code x} if it is currently
728 * holding {@code expected}.
729 *
730 * <p>This operation has memory semantics of a {@code volatile} read
731 * and write. Corresponds to C11 atomic_compare_exchange_strong.
732 *
733 * @return {@code true} if successful
734 */
735 @HotSpotIntrinsicCandidate
736 public final native boolean compareAndSwapLong(Object o, long offset,
737 long expected,
738 long x);
739
740 /**
741 * Fetches a reference value from a given Java variable, with volatile
742 * load semantics. Otherwise identical to {@link #getObject(Object, long)}
743 */
744 @HotSpotIntrinsicCandidate
745 public native Object getObjectVolatile(Object o, long offset);
746
747 /**
748 * Stores a reference value into a given Java variable, with
749 * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
750 */
751 @HotSpotIntrinsicCandidate
752 public native void putObjectVolatile(Object o, long offset, Object x);
753
754 /** Volatile version of {@link #getInt(Object, long)} */
755 @HotSpotIntrinsicCandidate
756 public native int getIntVolatile(Object o, long offset);
757
758 /** Volatile version of {@link #putInt(Object, long, int)} */
759 @HotSpotIntrinsicCandidate
760 public native void putIntVolatile(Object o, long offset, int x);
761
762 /** Volatile version of {@link #getBoolean(Object, long)} */
763 @HotSpotIntrinsicCandidate
764 public native boolean getBooleanVolatile(Object o, long offset);
765
766 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
767 @HotSpotIntrinsicCandidate
768 public native void putBooleanVolatile(Object o, long offset, boolean x);
769
770 /** Volatile version of {@link #getByte(Object, long)} */
771 @HotSpotIntrinsicCandidate
772 public native byte getByteVolatile(Object o, long offset);
773
774 /** Volatile version of {@link #putByte(Object, long, byte)} */
775 @HotSpotIntrinsicCandidate
776 public native void putByteVolatile(Object o, long offset, byte x);
777
778 /** Volatile version of {@link #getShort(Object, long)} */
779 @HotSpotIntrinsicCandidate
780 public native short getShortVolatile(Object o, long offset);
781
782 /** Volatile version of {@link #putShort(Object, long, short)} */
783 @HotSpotIntrinsicCandidate
784 public native void putShortVolatile(Object o, long offset, short x);
785
786 /** Volatile version of {@link #getChar(Object, long)} */
787 @HotSpotIntrinsicCandidate
788 public native char getCharVolatile(Object o, long offset);
789
790 /** Volatile version of {@link #putChar(Object, long, char)} */
791 @HotSpotIntrinsicCandidate
792 public native void putCharVolatile(Object o, long offset, char x);
793
794 /** Volatile version of {@link #getLong(Object, long)} */
795 @HotSpotIntrinsicCandidate
796 public native long getLongVolatile(Object o, long offset);
797
798 /** Volatile version of {@link #putLong(Object, long, long)} */
799 @HotSpotIntrinsicCandidate
800 public native void putLongVolatile(Object o, long offset, long x);
801
802 /** Volatile version of {@link #getFloat(Object, long)} */
803 @HotSpotIntrinsicCandidate
804 public native float getFloatVolatile(Object o, long offset);
805
806 /** Volatile version of {@link #putFloat(Object, long, float)} */
807 @HotSpotIntrinsicCandidate
808 public native void putFloatVolatile(Object o, long offset, float x);
809
810 /** Volatile version of {@link #getDouble(Object, long)} */
811 @HotSpotIntrinsicCandidate
812 public native double getDoubleVolatile(Object o, long offset);
813
814 /** Volatile version of {@link #putDouble(Object, long, double)} */
815 @HotSpotIntrinsicCandidate
816 public native void putDoubleVolatile(Object o, long offset, double x);
817
818 /**
819 * Version of {@link #putObjectVolatile(Object, long, Object)}
820 * that does not guarantee immediate visibility of the store to
821 * other threads. This method is generally only useful if the
822 * underlying field is a Java volatile (or if an array cell, one
823 * that is otherwise only accessed using volatile accesses).
824 *
825 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
826 */
827 @HotSpotIntrinsicCandidate
828 public native void putOrderedObject(Object o, long offset, Object x);
829
830 /** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)} */
831 @HotSpotIntrinsicCandidate
832 public native void putOrderedInt(Object o, long offset, int x);
833
834 /** Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)} */
835 @HotSpotIntrinsicCandidate
836 public native void putOrderedLong(Object o, long offset, long x);
837
838 /**
839 * Unblocks the given thread blocked on {@code park}, or, if it is
840 * not blocked, causes the subsequent call to {@code park} not to
841 * block. Note: this operation is "unsafe" solely because the
842 * caller must somehow ensure that the thread has not been
843 * destroyed. Nothing special is usually required to ensure this
844 * when called from Java (in which there will ordinarily be a live
845 * reference to the thread) but this is not nearly-automatically
846 * so when calling from native code.
847 *
848 * @param thread the thread to unpark.
849 */
850 @HotSpotIntrinsicCandidate
851 public native void unpark(Object thread);
852
853 /**
854 * Blocks current thread, returning when a balancing
855 * {@code unpark} occurs, or a balancing {@code unpark} has
856 * already occurred, or the thread is interrupted, or, if not
857 * absolute and time is not zero, the given time nanoseconds have
858 * elapsed, or if absolute, the given deadline in milliseconds
859 * since Epoch has passed, or spuriously (i.e., returning for no
860 * "reason"). Note: This operation is in the Unsafe class only
861 * because {@code unpark} is, so it would be strange to place it
862 * elsewhere.
863 */
864 @HotSpotIntrinsicCandidate
865 public native void park(boolean isAbsolute, long time);
866
867 /**
868 * Gets the load average in the system run queue assigned
869 * to the available processors averaged over various periods of time.
870 * This method retrieves the given {@code nelem} samples and
871 * assigns to the elements of the given {@code loadavg} array.
872 * The system imposes a maximum of 3 samples, representing
873 * averages over the last 1, 5, and 15 minutes, respectively.
874 *
875 * @param loadavg an array of double of size nelems
876 * @param nelems the number of samples to be retrieved and
877 * must be 1 to 3.
878 *
879 * @return the number of samples actually retrieved; or -1
880 * if the load average is unobtainable.
881 */
882 public native int getLoadAverage(double[] loadavg, int nelems);
883
884 // The following contain CAS-based Java implementations used on
885 // platforms not supporting native instructions
886
887 /**
888 * Atomically adds the given value to the current value of a field
889 * or array element within the given object {@code o}
890 * at the given {@code offset}.
891 *
892 * @param o object/array to update the field/element in
893 * @param offset field/element offset
894 * @param delta the value to add
895 * @return the previous value
896 * @since 1.8
897 */
898 @HotSpotIntrinsicCandidate
899 public final int getAndAddInt(Object o, long offset, int delta) {
900 int v;
901 do {
902 v = getIntVolatile(o, offset);
903 } while (!compareAndSwapInt(o, offset, v, v + delta));
904 return v;
905 }
906
907 /**
908 * Atomically adds the given value to the current value of a field
909 * or array element within the given object {@code o}
910 * at the given {@code offset}.
911 *
912 * @param o object/array to update the field/element in
913 * @param offset field/element offset
914 * @param delta the value to add
915 * @return the previous value
916 * @since 1.8
917 */
918 @HotSpotIntrinsicCandidate
919 public final long getAndAddLong(Object o, long offset, long delta) {
920 long v;
921 do {
922 v = getLongVolatile(o, offset);
923 } while (!compareAndSwapLong(o, offset, v, v + delta));
924 return v;
925 }
926
927 /**
928 * Atomically exchanges the given value with the current value of
929 * a field or array element within the given object {@code o}
930 * at the given {@code offset}.
931 *
932 * @param o object/array to update the field/element in
933 * @param offset field/element offset
934 * @param newValue new value
935 * @return the previous value
936 * @since 1.8
937 */
938 @HotSpotIntrinsicCandidate
939 public final int getAndSetInt(Object o, long offset, int newValue) {
940 int v;
941 do {
942 v = getIntVolatile(o, offset);
943 } while (!compareAndSwapInt(o, offset, v, newValue));
944 return v;
945 }
946
947 /**
948 * Atomically exchanges the given value with the current value of
949 * a field or array element within the given object {@code o}
950 * at the given {@code offset}.
951 *
952 * @param o object/array to update the field/element in
953 * @param offset field/element offset
954 * @param newValue new value
955 * @return the previous value
956 * @since 1.8
957 */
958 @HotSpotIntrinsicCandidate
959 public final long getAndSetLong(Object o, long offset, long newValue) {
960 long v;
961 do {
962 v = getLongVolatile(o, offset);
963 } while (!compareAndSwapLong(o, offset, v, newValue));
964 return v;
965 }
966
967 /**
968 * Atomically exchanges the given reference value with the current
969 * reference value of a field or array element within the given
970 * object {@code o} at the given {@code offset}.
971 *
972 * @param o object/array to update the field/element in
973 * @param offset field/element offset
974 * @param newValue new value
975 * @return the previous value
976 * @since 1.8
977 */
978 @HotSpotIntrinsicCandidate
979 public final Object getAndSetObject(Object o, long offset, Object newValue) {
980 Object v;
981 do {
982 v = getObjectVolatile(o, offset);
983 } while (!compareAndSwapObject(o, offset, v, newValue));
984 return v;
985 }
986
987
988 /**
989 * Ensures that loads before the fence will not be reordered with loads and
990 * stores after the fence; a "LoadLoad plus LoadStore barrier".
991 *
992 * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
993 * (an "acquire fence").
994 *
995 * A pure LoadLoad fence is not provided, since the addition of LoadStore
996 * is almost always desired, and most current hardware instructions that
997 * provide a LoadLoad barrier also provide a LoadStore barrier for free.
998 * @since 1.8
999 */
1000 @HotSpotIntrinsicCandidate
1001 public native void loadFence();
1002
1003 /**
1004 * Ensures that loads and stores before the fence will not be reordered with
1005 * stores after the fence; a "StoreStore plus LoadStore barrier".
1006 *
1007 * Corresponds to C11 atomic_thread_fence(memory_order_release)
1008 * (a "release fence").
1009 *
1010 * A pure StoreStore fence is not provided, since the addition of LoadStore
1011 * is almost always desired, and most current hardware instructions that
1012 * provide a StoreStore barrier also provide a LoadStore barrier for free.
1013 * @since 1.8
1014 */
1015 @HotSpotIntrinsicCandidate
1016 public native void storeFence();
1017
1018 /**
1019 * Ensures that loads and stores before the fence will not be reordered
1020 * with loads and stores after the fence. Implies the effects of both
1021 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
1022 * barrier.
1023 *
1024 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
1025 * @since 1.8
1026 */
1027 @HotSpotIntrinsicCandidate
1028 public native void fullFence();
1029
1030 /**
1031 * Throws IllegalAccessError; for use by the VM for access control
1032 * error support.
1033 * @since 1.8
1034 */
1035 private static void throwIllegalAccessError() {
1036 throw new IllegalAccessError();
1037 }
1038 }
|
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 sun.misc;
27
28 import jdk.internal.vm.annotation.ForceInline;
29 import jdk.internal.misc.VM;
30 import sun.reflect.CallerSensitive;
31 import sun.reflect.Reflection;
32
33 import java.lang.reflect.Field;
34 import java.security.ProtectionDomain;
35
36
37 /**
38 * A collection of methods for performing low-level, unsafe operations.
39 * Although the class and all methods are public, use of this class is
40 * limited because only trusted code can obtain instances of it.
41 *
42 * <em>Note:</em> It is the resposibility of the caller to make sure
43 * arguments are checked before methods of this class are
44 * called. While some rudimentary checks are performed on the input,
45 * the checks are best effort and when performance is an overriding
46 * priority, as when methods of this class are optimized by the
47 * runtime compiler, some or all checks (if any) may be elided. Hence,
48 * the caller must not rely on the checks and corresponding
49 * exceptions!
50 *
51 * @author John R. Rose
52 * @see #getUnsafe
53 */
54
55 public final class Unsafe {
56
57 static {
58 sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
59 }
60
61 private Unsafe() {}
62
63 private static final Unsafe theUnsafe = new Unsafe();
64 private static final jdk.internal.misc.Unsafe theInternalUnsafe = jdk.internal.misc.Unsafe.getUnsafe();
65
66 /**
67 * Provides the caller with the capability of performing unsafe
68 * operations.
69 *
70 * <p>The returned {@code Unsafe} object should be carefully guarded
71 * by the caller, since it can be used to read and write data at arbitrary
72 * memory addresses. It must never be passed to untrusted code.
73 *
74 * <p>Most methods in this class are very low-level, and correspond to a
75 * small number of hardware instructions (on typical machines). Compilers
76 * are encouraged to optimize these methods accordingly.
77 *
78 * <p>Here is a suggested idiom for using unsafe operations:
79 *
80 * <pre> {@code
81 * class MyTrustedClass {
82 * private static final Unsafe unsafe = Unsafe.getUnsafe();
83 * ...
84 * private long myCountAddress = ...;
141 * for Java variables. When the object reference is null, this method
142 * uses its offset as an absolute address. This is similar in operation
143 * to methods such as {@link #getInt(long)}, which provide (in effect) a
144 * <em>single-register</em> addressing mode for non-Java variables.
145 * However, because Java variables may have a different layout in memory
146 * from non-Java variables, programmers should not assume that these
147 * two addressing modes are ever equivalent. Also, programmers should
148 * remember that offsets from the double-register addressing mode cannot
149 * be portably confused with longs used in the single-register addressing
150 * mode.
151 *
152 * @param o Java heap object in which the variable resides, if any, else
153 * null
154 * @param offset indication of where the variable resides in a Java heap
155 * object, if any, else a memory address locating the variable
156 * statically
157 * @return the value fetched from the indicated Java variable
158 * @throws RuntimeException No defined exceptions are thrown, not even
159 * {@link NullPointerException}
160 */
161 @ForceInline
162 public int getInt(Object o, long offset) {
163 return theInternalUnsafe.getInt(o, offset);
164 }
165
166 /**
167 * Stores a value into a given Java variable.
168 * <p>
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 @ForceInline
187 public void putInt(Object o, long offset, int x) {
188 theInternalUnsafe.putInt(o, offset, x);
189 }
190
191 /**
192 * Fetches a reference value from a given Java variable.
193 * @see #getInt(Object, long)
194 */
195 @ForceInline
196 public Object getObject(Object o, long offset) {
197 return theInternalUnsafe.getObject(o, offset);
198 }
199
200 /**
201 * Stores a reference value into a given Java variable.
202 * <p>
203 * Unless the reference {@code x} being stored is either null
204 * or matches the field type, the results are undefined.
205 * If the reference {@code o} is non-null, card marks or
206 * other store barriers for that object (if the VM requires them)
207 * are updated.
208 * @see #putInt(Object, long, int)
209 */
210 @ForceInline
211 public void putObject(Object o, long offset, Object x) {
212 theInternalUnsafe.putObject(o, offset, x);
213 }
214
215 /** @see #getInt(Object, long) */
216 @ForceInline
217 public boolean getBoolean(Object o, long offset) {
218 return theInternalUnsafe.getBoolean(o, offset);
219 }
220
221 /** @see #putInt(Object, long, int) */
222 @ForceInline
223 public void putBoolean(Object o, long offset, boolean x) {
224 theInternalUnsafe.putBoolean(o, offset, x);
225 }
226
227 /** @see #getInt(Object, long) */
228 @ForceInline
229 public byte getByte(Object o, long offset) {
230 return theInternalUnsafe.getByte(o, offset);
231 }
232
233 /** @see #putInt(Object, long, int) */
234 @ForceInline
235 public void putByte(Object o, long offset, byte x) {
236 theInternalUnsafe.putByte(o, offset, x);
237 }
238
239 /** @see #getInt(Object, long) */
240 @ForceInline
241 public short getShort(Object o, long offset) {
242 return theInternalUnsafe.getShort(o, offset);
243 }
244
245 /** @see #putInt(Object, long, int) */
246 @ForceInline
247 public void putShort(Object o, long offset, short x) {
248 theInternalUnsafe.putShort(o, offset, x);
249 }
250
251 /** @see #getInt(Object, long) */
252 @ForceInline
253 public char getChar(Object o, long offset) {
254 return theInternalUnsafe.getChar(o, offset);
255 }
256
257 /** @see #putInt(Object, long, int) */
258 @ForceInline
259 public void putChar(Object o, long offset, char x) {
260 theInternalUnsafe.putChar(o, offset, x);
261 }
262
263 /** @see #getInt(Object, long) */
264 @ForceInline
265 public long getLong(Object o, long offset) {
266 return theInternalUnsafe.getLong(o, offset);
267 }
268
269 /** @see #putInt(Object, long, int) */
270 @ForceInline
271 public void putLong(Object o, long offset, long x) {
272 theInternalUnsafe.putLong(o, offset, x);
273 }
274
275 /** @see #getInt(Object, long) */
276 @ForceInline
277 public float getFloat(Object o, long offset) {
278 return theInternalUnsafe.getFloat(o, offset);
279 }
280
281 /** @see #putInt(Object, long, int) */
282 @ForceInline
283 public void putFloat(Object o, long offset, float x) {
284 theInternalUnsafe.putFloat(o, offset, x);
285 }
286
287 /** @see #getInt(Object, long) */
288 @ForceInline
289 public double getDouble(Object o, long offset) {
290 return theInternalUnsafe.getDouble(o, offset);
291 }
292
293 /** @see #putInt(Object, long, int) */
294 @ForceInline
295 public void putDouble(Object o, long offset, double x) {
296 theInternalUnsafe.putDouble(o, offset, x);
297 }
298
299
300 // These read VM internal data.
301
302 /**
303 * Fetches an uncompressed reference value from a given native variable
304 * ignoring the VM's compressed references mode.
305 *
306 * @param address a memory address locating the variable
307 * @return the value fetched from the indicated native variable
308 */
309 @ForceInline
310 public Object getUncompressedObject(long address) {
311 return theInternalUnsafe.getUncompressedObject(address);
312 }
313
314 /**
315 * Fetches the {@link java.lang.Class} Java mirror for the given native
316 * metaspace {@code Klass} pointer.
317 *
318 * @param metaspaceKlass a native metaspace {@code Klass} pointer
319 * @return the {@link java.lang.Class} Java mirror
320 */
321 @ForceInline
322 public Class<?> getJavaMirror(long metaspaceKlass) {
323 return theInternalUnsafe.getJavaMirror(metaspaceKlass);
324 }
325
326 /**
327 * Fetches a native metaspace {@code Klass} pointer for the given Java
328 * object.
329 *
330 * @param o Java heap object for which to fetch the class pointer
331 * @return a native metaspace {@code Klass} pointer
332 */
333 @ForceInline
334 public long getKlassPointer(Object o) {
335 return theInternalUnsafe.getKlassPointer(o);
336 }
337
338 // These work on values in the C heap.
339
340 /**
341 * Fetches a value from a given memory address. If the address is zero, or
342 * does not point into a block obtained from {@link #allocateMemory}, the
343 * results are undefined.
344 *
345 * @see #allocateMemory
346 */
347 @ForceInline
348 public byte getByte(long address) {
349 return theInternalUnsafe.getByte(address);
350 }
351
352 /**
353 * Stores a value into a given memory address. If the address is zero, or
354 * does not point into a block obtained from {@link #allocateMemory}, the
355 * results are undefined.
356 *
357 * @see #getByte(long)
358 */
359 @ForceInline
360 public void putByte(long address, byte x) {
361 theInternalUnsafe.putByte(address, x);
362 }
363
364 /** @see #getByte(long) */
365 @ForceInline
366 public short getShort(long address) {
367 return theInternalUnsafe.getShort(address);
368 }
369
370 /** @see #putByte(long, byte) */
371 @ForceInline
372 public void putShort(long address, short x) {
373 theInternalUnsafe.putShort(address, x);
374 }
375
376 /** @see #getByte(long) */
377 @ForceInline
378 public char getChar(long address) {
379 return theInternalUnsafe.getChar(address);
380 }
381
382 /** @see #putByte(long, byte) */
383 @ForceInline
384 public void putChar(long address, char x) {
385 theInternalUnsafe.putChar(address, x);
386 }
387
388 /** @see #getByte(long) */
389 @ForceInline
390 public int getInt(long address) {
391 return theInternalUnsafe.getInt(address);
392 }
393
394 /** @see #putByte(long, byte) */
395 @ForceInline
396 public void putInt(long address, int x) {
397 theInternalUnsafe.putInt(address, x);
398 }
399
400 /** @see #getByte(long) */
401 @ForceInline
402 public long getLong(long address) {
403 return theInternalUnsafe.getLong(address);
404 }
405
406 /** @see #putByte(long, byte) */
407 @ForceInline
408 public void putLong(long address, long x) {
409 theInternalUnsafe.putLong(address, x);
410 }
411
412 /** @see #getByte(long) */
413 @ForceInline
414 public float getFloat(long address) {
415 return theInternalUnsafe.getFloat(address);
416 }
417
418 /** @see #putByte(long, byte) */
419 @ForceInline
420 public void putFloat(long address, float x) {
421 theInternalUnsafe.putFloat(address, x);
422 }
423
424 /** @see #getByte(long) */
425 @ForceInline
426 public double getDouble(long address) {
427 return theInternalUnsafe.getDouble(address);
428 }
429
430 /** @see #putByte(long, byte) */
431 @ForceInline
432 public void putDouble(long address, double x) {
433 theInternalUnsafe.putDouble(address, x);
434 }
435
436
437 /**
438 * Fetches a native pointer from a given memory address. If the address is
439 * zero, or does not point into a block obtained from {@link
440 * #allocateMemory}, the results are undefined.
441 *
442 * <p>If the native pointer is less than 64 bits wide, it is extended as
443 * an unsigned number to a Java long. The pointer may be indexed by any
444 * given byte offset, simply by adding that offset (as a simple integer) to
445 * the long representing the pointer. The number of bytes actually read
446 * from the target address may be determined by consulting {@link
447 * #addressSize}.
448 *
449 * @see #allocateMemory
450 */
451 @ForceInline
452 public long getAddress(long address) {
453 return theInternalUnsafe.getAddress(address);
454 }
455
456 /**
457 * Stores a native pointer into a given memory address. If the address is
458 * zero, or does not point into a block obtained from {@link
459 * #allocateMemory}, the results are undefined.
460 *
461 * <p>The number of bytes actually written at the target address may be
462 * determined by consulting {@link #addressSize}.
463 *
464 * @see #getAddress(long)
465 */
466 @ForceInline
467 public void putAddress(long address, long x) {
468 theInternalUnsafe.putAddress(address, x);
469 }
470
471
472 /// wrappers for malloc, realloc, free:
473
474 /**
475 * Allocates a new block of native memory, of the given size in bytes. The
476 * contents of the memory are uninitialized; they will generally be
477 * garbage. The resulting native pointer will never be zero, and will be
478 * aligned for all value types. Dispose of this memory by calling {@link
479 * #freeMemory}, or resize it with {@link #reallocateMemory}.
480 *
481 * <em>Note:</em> It is the resposibility of the caller to make
482 * sure arguments are checked before the methods are called. While
483 * some rudimentary checks are performed on the input, the checks
484 * are best effort and when performance is an overriding priority,
485 * as when methods of this class are optimized by the runtime
486 * compiler, some or all checks (if any) may be elided. Hence, the
487 * caller must not rely on the checks and corresponding
488 * exceptions!
489 *
490 * @throws RuntimeException if the size is negative or too large
491 * for the native size_t type
492 *
493 * @throws OutOfMemoryError if the allocation is refused by the system
494 *
495 * @see #getByte(long)
496 * @see #putByte(long, byte)
497 */
498 @ForceInline
499 public long allocateMemory(long bytes) {
500 return theInternalUnsafe.allocateMemory(bytes);
501 }
502
503 /**
504 * Resizes a new block of native memory, to the given size in bytes. The
505 * contents of the new block past the size of the old block are
506 * uninitialized; they will generally be garbage. The resulting native
507 * pointer will be zero if and only if the requested size is zero. The
508 * resulting native pointer will be aligned for all value types. Dispose
509 * of this memory by calling {@link #freeMemory}, or resize it with {@link
510 * #reallocateMemory}. The address passed to this method may be null, in
511 * which case an allocation will be performed.
512 *
513 * <em>Note:</em> It is the resposibility of the caller to make
514 * sure arguments are checked before the methods are called. While
515 * some rudimentary checks are performed on the input, the checks
516 * are best effort and when performance is an overriding priority,
517 * as when methods of this class are optimized by the runtime
518 * compiler, some or all checks (if any) may be elided. Hence, the
519 * caller must not rely on the checks and corresponding
520 * exceptions!
521 *
522 * @throws RuntimeException if the size is negative or too large
523 * for the native size_t type
524 *
525 * @throws OutOfMemoryError if the allocation is refused by the system
526 *
527 * @see #allocateMemory
528 */
529 @ForceInline
530 public long reallocateMemory(long address, long bytes) {
531 return theInternalUnsafe.reallocateMemory(address, bytes);
532 }
533
534 /**
535 * Sets all bytes in a given block of memory to a fixed value
536 * (usually zero).
537 *
538 * <p>This method determines a block's base address by means of two parameters,
539 * and so it provides (in effect) a <em>double-register</em> addressing mode,
540 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
541 * the offset supplies an absolute base address.
542 *
543 * <p>The stores are in coherent (atomic) units of a size determined
544 * by the address and length parameters. If the effective address and
545 * length are all even modulo 8, the stores take place in 'long' units.
546 * If the effective address and length are (resp.) even modulo 4 or 2,
547 * the stores take place in units of 'int' or 'short'.
548 *
549 * <em>Note:</em> It is the resposibility of the caller to make
550 * sure arguments are checked before the methods are called. While
551 * some rudimentary checks are performed on the input, the checks
552 * are best effort and when performance is an overriding priority,
553 * as when methods of this class are optimized by the runtime
554 * compiler, some or all checks (if any) may be elided. Hence, the
555 * caller must not rely on the checks and corresponding
556 * exceptions!
557 *
558 * @throws RuntimeException if any of the arguments is invalid
559 *
560 * @since 1.7
561 */
562 @ForceInline
563 public void setMemory(Object o, long offset, long bytes, byte value) {
564 theInternalUnsafe.setMemory(o, offset, bytes, value);
565 }
566
567 /**
568 * Sets all bytes in a given block of memory to a fixed value
569 * (usually zero). This provides a <em>single-register</em> addressing mode,
570 * as discussed in {@link #getInt(Object,long)}.
571 *
572 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
573 */
574 @ForceInline
575 public void setMemory(long address, long bytes, byte value) {
576 theInternalUnsafe.setMemory(address, bytes, value);
577 }
578
579 /**
580 * Sets all bytes in a given block of memory to a copy of another
581 * block.
582 *
583 * <p>This method determines each block's base address by means of two parameters,
584 * and so it provides (in effect) a <em>double-register</em> addressing mode,
585 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
586 * the offset supplies an absolute base address.
587 *
588 * <p>The transfers are in coherent (atomic) units of a size determined
589 * by the address and length parameters. If the effective addresses and
590 * length are all even modulo 8, the transfer takes place in 'long' units.
591 * If the effective addresses and length are (resp.) even modulo 4 or 2,
592 * the transfer takes place in units of 'int' or 'short'.
593 *
594 * <em>Note:</em> It is the resposibility of the caller to make
595 * sure arguments are checked before the methods are called. While
596 * some rudimentary checks are performed on the input, the checks
597 * are best effort and when performance is an overriding priority,
598 * as when methods of this class are optimized by the runtime
599 * compiler, some or all checks (if any) may be elided. Hence, the
600 * caller must not rely on the checks and corresponding
601 * exceptions!
602 *
603 * @throws RuntimeException if any of the arguments is invalid
604 *
605 * @since 1.7
606 */
607 @ForceInline
608 public void copyMemory(Object srcBase, long srcOffset,
609 Object destBase, long destOffset,
610 long bytes) {
611 theInternalUnsafe.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes);
612 }
613
614 /**
615 * Sets all bytes in a given block of memory to a copy of another
616 * block. This provides a <em>single-register</em> addressing mode,
617 * as discussed in {@link #getInt(Object,long)}.
618 *
619 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
620 */
621 @ForceInline
622 public void copyMemory(long srcAddress, long destAddress, long bytes) {
623 theInternalUnsafe.copyMemory(srcAddress, destAddress, bytes);
624 }
625
626 /**
627 * Disposes of a block of native memory, as obtained from {@link
628 * #allocateMemory} or {@link #reallocateMemory}. The address passed to
629 * this method may be null, in which case no action is taken.
630 *
631 * <em>Note:</em> It is the resposibility of the caller to make
632 * sure arguments are checked before the methods are called. While
633 * some rudimentary checks are performed on the input, the checks
634 * are best effort and when performance is an overriding priority,
635 * as when methods of this class are optimized by the runtime
636 * compiler, some or all checks (if any) may be elided. Hence, the
637 * caller must not rely on the checks and corresponding
638 * exceptions!
639 *
640 * @throws RuntimeException if any of the arguments is invalid
641 *
642 * @see #allocateMemory
643 */
644 @ForceInline
645 public void freeMemory(long address) {
646 theInternalUnsafe.freeMemory(address);
647 }
648
649 /// random queries
650
651 /**
652 * This constant differs from all results that will ever be returned from
653 * {@link #staticFieldOffset}, {@link #objectFieldOffset},
654 * or {@link #arrayBaseOffset}.
655 */
656 public static final int INVALID_FIELD_OFFSET = jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
657
658 /**
659 * Reports the location of a given field in the storage allocation of its
660 * class. Do not expect to perform any sort of arithmetic on this offset;
661 * it is just a cookie which is passed to the unsafe heap memory accessors.
662 *
663 * <p>Any given field will always have the same offset and base, and no
664 * two distinct fields of the same class will ever have the same offset
665 * and base.
666 *
667 * <p>As of 1.4.1, offsets for fields are represented as long values,
668 * although the Sun JVM does not use the most significant 32 bits.
669 * However, JVM implementations which store static fields at absolute
670 * addresses can use long offsets and null base pointers to express
671 * the field locations in a form usable by {@link #getInt(Object,long)}.
672 * Therefore, code which will be ported to such JVMs on 64-bit platforms
673 * must preserve all bits of static field offsets.
674 * @see #getInt(Object, long)
675 */
676 @ForceInline
677 public long objectFieldOffset(Field f) {
678 return theInternalUnsafe.objectFieldOffset(f);
679 }
680
681 /**
682 * Reports the location of a given static field, in conjunction with {@link
683 * #staticFieldBase}.
684 * <p>Do not expect to perform any sort of arithmetic on this offset;
685 * it is just a cookie which is passed to the unsafe heap memory accessors.
686 *
687 * <p>Any given field will always have the same offset, and no two distinct
688 * fields of the same class will ever have the same offset.
689 *
690 * <p>As of 1.4.1, offsets for fields are represented as long values,
691 * although the Sun JVM does not use the most significant 32 bits.
692 * It is hard to imagine a JVM technology which needs more than
693 * a few bits to encode an offset within a non-array object,
694 * However, for consistency with other methods in this class,
695 * this method reports its result as a long value.
696 * @see #getInt(Object, long)
697 */
698 @ForceInline
699 public long staticFieldOffset(Field f) {
700 return theInternalUnsafe.staticFieldOffset(f);
701 }
702
703 /**
704 * Reports the location of a given static field, in conjunction with {@link
705 * #staticFieldOffset}.
706 * <p>Fetch the base "Object", if any, with which static fields of the
707 * given class can be accessed via methods like {@link #getInt(Object,
708 * long)}. This value may be null. This value may refer to an object
709 * which is a "cookie", not guaranteed to be a real Object, and it should
710 * not be used in any way except as argument to the get and put routines in
711 * this class.
712 */
713 @ForceInline
714 public Object staticFieldBase(Field f) {
715 return theInternalUnsafe.staticFieldBase(f);
716 }
717
718 /**
719 * Detects if the given class may need to be initialized. This is often
720 * needed in conjunction with obtaining the static field base of a
721 * class.
722 * @return false only if a call to {@code ensureClassInitialized} would have no effect
723 */
724 @ForceInline
725 public boolean shouldBeInitialized(Class<?> c) {
726 return theInternalUnsafe.shouldBeInitialized(c);
727 }
728
729 /**
730 * Ensures the given class has been initialized. This is often
731 * needed in conjunction with obtaining the static field base of a
732 * class.
733 */
734 @ForceInline
735 public void ensureClassInitialized(Class<?> c) {
736 theInternalUnsafe.ensureClassInitialized(c);
737 }
738
739 /**
740 * Reports the offset of the first element in the storage allocation of a
741 * given array class. If {@link #arrayIndexScale} returns a non-zero value
742 * for the same class, you may use that scale factor, together with this
743 * base offset, to form new offsets to access elements of arrays of the
744 * given class.
745 *
746 * @see #getInt(Object, long)
747 * @see #putInt(Object, long, int)
748 */
749 @ForceInline
750 public int arrayBaseOffset(Class<?> arrayClass) {
751 return theInternalUnsafe.arrayBaseOffset(arrayClass);
752 }
753
754 /** The value of {@code arrayBaseOffset(boolean[].class)} */
755 public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
756
757 /** The value of {@code arrayBaseOffset(byte[].class)} */
758 public static final int ARRAY_BYTE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
759
760 /** The value of {@code arrayBaseOffset(short[].class)} */
761 public static final int ARRAY_SHORT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET;
762
763 /** The value of {@code arrayBaseOffset(char[].class)} */
764 public static final int ARRAY_CHAR_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_CHAR_BASE_OFFSET;
765
766 /** The value of {@code arrayBaseOffset(int[].class)} */
767 public static final int ARRAY_INT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_INT_BASE_OFFSET;
768
769 /** The value of {@code arrayBaseOffset(long[].class)} */
770 public static final int ARRAY_LONG_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_LONG_BASE_OFFSET;
771
772 /** The value of {@code arrayBaseOffset(float[].class)} */
773 public static final int ARRAY_FLOAT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_FLOAT_BASE_OFFSET;
774
775 /** The value of {@code arrayBaseOffset(double[].class)} */
776 public static final int ARRAY_DOUBLE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
777
778 /** The value of {@code arrayBaseOffset(Object[].class)} */
779 public static final int ARRAY_OBJECT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET;
780
781 /**
782 * Reports the scale factor for addressing elements in the storage
783 * allocation of a given array class. However, arrays of "narrow" types
784 * will generally not work properly with accessors like {@link
785 * #getByte(Object, long)}, so the scale factor for such classes is reported
786 * as zero.
787 *
788 * @see #arrayBaseOffset
789 * @see #getInt(Object, long)
790 * @see #putInt(Object, long, int)
791 */
792 @ForceInline
793 public int arrayIndexScale(Class<?> arrayClass) {
794 return theInternalUnsafe.arrayIndexScale(arrayClass);
795 }
796
797 /** The value of {@code arrayIndexScale(boolean[].class)} */
798 public static final int ARRAY_BOOLEAN_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
799
800 /** The value of {@code arrayIndexScale(byte[].class)} */
801 public static final int ARRAY_BYTE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE;
802
803 /** The value of {@code arrayIndexScale(short[].class)} */
804 public static final int ARRAY_SHORT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
805
806 /** The value of {@code arrayIndexScale(char[].class)} */
807 public static final int ARRAY_CHAR_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_CHAR_INDEX_SCALE;
808
809 /** The value of {@code arrayIndexScale(int[].class)} */
810 public static final int ARRAY_INT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
811
812 /** The value of {@code arrayIndexScale(long[].class)} */
813 public static final int ARRAY_LONG_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
814
815 /** The value of {@code arrayIndexScale(float[].class)} */
816 public static final int ARRAY_FLOAT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
817
818 /** The value of {@code arrayIndexScale(double[].class)} */
819 public static final int ARRAY_DOUBLE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
820
821 /** The value of {@code arrayIndexScale(Object[].class)} */
822 public static final int ARRAY_OBJECT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
823
824 /**
825 * Reports the size in bytes of a native pointer, as stored via {@link
826 * #putAddress}. This value will be either 4 or 8. Note that the sizes of
827 * other primitive types (as stored in native memory blocks) is determined
828 * fully by their information content.
829 */
830 @ForceInline
831 public int addressSize() {
832 return theInternalUnsafe.addressSize();
833 }
834
835 /** The value of {@code addressSize()} */
836 public static final int ADDRESS_SIZE = theInternalUnsafe.addressSize();
837
838 /**
839 * Reports the size in bytes of a native memory page (whatever that is).
840 * This value will always be a power of two.
841 */
842 @ForceInline
843 public int pageSize() {
844 return theInternalUnsafe.pageSize();
845 }
846
847
848 /// random trusted operations from JNI:
849
850 /**
851 * Tells the VM to define a class, without security checks. By default, the
852 * class loader and protection domain come from the caller's class.
853 */
854 @ForceInline
855 public Class<?> defineClass(String name, byte[] b, int off, int len,
856 ClassLoader loader,
857 ProtectionDomain protectionDomain) {
858 return theInternalUnsafe.defineClass(name, b, off, len, loader, protectionDomain);
859 }
860
861 /**
862 * Defines a class but does not make it known to the class loader or system dictionary.
863 * <p>
864 * For each CP entry, the corresponding CP patch must either be null or have
865 * the a format that matches its tag:
866 * <ul>
867 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
868 * <li>Utf8: a string (must have suitable syntax if used as signature or name)
869 * <li>Class: any java.lang.Class object
870 * <li>String: any object (not just a java.lang.String)
871 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
872 * </ul>
873 * @param hostClass context for linkage, access control, protection domain, and class loader
874 * @param data bytes of a class file
875 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
876 */
877 @ForceInline
878 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
879 return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches);
880 }
881
882 /**
883 * Allocates an instance but does not run any constructor.
884 * Initializes the class if it has not yet been.
885 */
886 @ForceInline
887 public Object allocateInstance(Class<?> cls)
888 throws InstantiationException {
889 return theInternalUnsafe.allocateInstance(cls);
890 }
891
892 /** Throws the exception without telling the verifier. */
893 @ForceInline
894 public void throwException(Throwable ee) {
895 theInternalUnsafe.throwException(ee);
896 }
897
898 /**
899 * Atomically updates Java variable to {@code x} if it is currently
900 * holding {@code expected}.
901 *
902 * <p>This operation has memory semantics of a {@code volatile} read
903 * and write. Corresponds to C11 atomic_compare_exchange_strong.
904 *
905 * @return {@code true} if successful
906 */
907 @ForceInline
908 public final boolean compareAndSwapObject(Object o, long offset,
909 Object expected,
910 Object x) {
911 return theInternalUnsafe.compareAndSwapObject(o, offset, expected, x);
912 }
913
914 /**
915 * Atomically updates Java variable to {@code x} if it is currently
916 * holding {@code expected}.
917 *
918 * <p>This operation has memory semantics of a {@code volatile} read
919 * and write. Corresponds to C11 atomic_compare_exchange_strong.
920 *
921 * @return {@code true} if successful
922 */
923 @ForceInline
924 public final boolean compareAndSwapInt(Object o, long offset,
925 int expected,
926 int x) {
927 return theInternalUnsafe.compareAndSwapInt(o, offset, expected, x);
928 }
929
930 /**
931 * Atomically updates Java variable to {@code x} if it is currently
932 * holding {@code expected}.
933 *
934 * <p>This operation has memory semantics of a {@code volatile} read
935 * and write. Corresponds to C11 atomic_compare_exchange_strong.
936 *
937 * @return {@code true} if successful
938 */
939 @ForceInline
940 public final boolean compareAndSwapLong(Object o, long offset,
941 long expected,
942 long x) {
943 return theInternalUnsafe.compareAndSwapLong(o, offset, expected, x);
944 }
945
946 /**
947 * Fetches a reference value from a given Java variable, with volatile
948 * load semantics. Otherwise identical to {@link #getObject(Object, long)}
949 */
950 @ForceInline
951 public Object getObjectVolatile(Object o, long offset) {
952 return theInternalUnsafe.getObjectVolatile(o, offset);
953 }
954
955 /**
956 * Stores a reference value into a given Java variable, with
957 * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
958 */
959 @ForceInline
960 public void putObjectVolatile(Object o, long offset, Object x) {
961 theInternalUnsafe.putObjectVolatile(o, offset, x);
962 }
963
964 /** Volatile version of {@link #getInt(Object, long)} */
965 @ForceInline
966 public int getIntVolatile(Object o, long offset) {
967 return theInternalUnsafe.getIntVolatile(o, offset);
968 }
969
970 /** Volatile version of {@link #putInt(Object, long, int)} */
971 @ForceInline
972 public void putIntVolatile(Object o, long offset, int x) {
973 theInternalUnsafe.putIntVolatile(o, offset, x);
974 }
975
976 /** Volatile version of {@link #getBoolean(Object, long)} */
977 @ForceInline
978 public boolean getBooleanVolatile(Object o, long offset) {
979 return theInternalUnsafe.getBooleanVolatile(o, offset);
980 }
981
982 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
983 @ForceInline
984 public void putBooleanVolatile(Object o, long offset, boolean x) {
985 theInternalUnsafe.putBooleanVolatile(o, offset, x);
986 }
987
988 /** Volatile version of {@link #getByte(Object, long)} */
989 @ForceInline
990 public byte getByteVolatile(Object o, long offset) {
991 return theInternalUnsafe.getByteVolatile(o, offset);
992 }
993
994 /** Volatile version of {@link #putByte(Object, long, byte)} */
995 @ForceInline
996 public void putByteVolatile(Object o, long offset, byte x) {
997 theInternalUnsafe.putByteVolatile(o, offset, x);
998 }
999
1000 /** Volatile version of {@link #getShort(Object, long)} */
1001 @ForceInline
1002 public short getShortVolatile(Object o, long offset) {
1003 return theInternalUnsafe.getShortVolatile(o, offset);
1004 }
1005
1006 /** Volatile version of {@link #putShort(Object, long, short)} */
1007 @ForceInline
1008 public void putShortVolatile(Object o, long offset, short x) {
1009 theInternalUnsafe.putShortVolatile(o, offset, x);
1010 }
1011
1012 /** Volatile version of {@link #getChar(Object, long)} */
1013 @ForceInline
1014 public char getCharVolatile(Object o, long offset) {
1015 return theInternalUnsafe.getCharVolatile(o, offset);
1016 }
1017
1018 /** Volatile version of {@link #putChar(Object, long, char)} */
1019 @ForceInline
1020 public void putCharVolatile(Object o, long offset, char x) {
1021 theInternalUnsafe.putCharVolatile(o, offset, x);
1022 }
1023
1024 /** Volatile version of {@link #getLong(Object, long)} */
1025 @ForceInline
1026 public long getLongVolatile(Object o, long offset) {
1027 return theInternalUnsafe.getLongVolatile(o, offset);
1028 }
1029
1030 /** Volatile version of {@link #putLong(Object, long, long)} */
1031 @ForceInline
1032 public void putLongVolatile(Object o, long offset, long x) {
1033 theInternalUnsafe.putLongVolatile(o, offset, x);
1034 }
1035
1036 /** Volatile version of {@link #getFloat(Object, long)} */
1037 @ForceInline
1038 public float getFloatVolatile(Object o, long offset) {
1039 return theInternalUnsafe.getFloatVolatile(o, offset);
1040 }
1041
1042 /** Volatile version of {@link #putFloat(Object, long, float)} */
1043 @ForceInline
1044 public void putFloatVolatile(Object o, long offset, float x) {
1045 theInternalUnsafe.putFloatVolatile(o, offset, x);
1046 }
1047
1048 /** Volatile version of {@link #getDouble(Object, long)} */
1049 @ForceInline
1050 public double getDoubleVolatile(Object o, long offset) {
1051 return theInternalUnsafe.getDoubleVolatile(o, offset);
1052 }
1053
1054 /** Volatile version of {@link #putDouble(Object, long, double)} */
1055 @ForceInline
1056 public void putDoubleVolatile(Object o, long offset, double x) {
1057 theInternalUnsafe.putDoubleVolatile(o, offset, x);
1058 }
1059
1060 /**
1061 * Version of {@link #putObjectVolatile(Object, long, Object)}
1062 * that does not guarantee immediate visibility of the store to
1063 * other threads. This method is generally only useful if the
1064 * underlying field is a Java volatile (or if an array cell, one
1065 * that is otherwise only accessed using volatile accesses).
1066 *
1067 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
1068 */
1069 @ForceInline
1070 public void putOrderedObject(Object o, long offset, Object x) {
1071 theInternalUnsafe.putOrderedObject(o, offset, x);
1072 }
1073
1074 /** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)} */
1075 @ForceInline
1076 public void putOrderedInt(Object o, long offset, int x) {
1077 theInternalUnsafe.putOrderedInt(o, offset, x);
1078 }
1079
1080 /** Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)} */
1081 @ForceInline
1082 public void putOrderedLong(Object o, long offset, long x) {
1083 theInternalUnsafe.putOrderedLong(o, offset, x);
1084 }
1085
1086 /**
1087 * Unblocks the given thread blocked on {@code park}, or, if it is
1088 * not blocked, causes the subsequent call to {@code park} not to
1089 * block. Note: this operation is "unsafe" solely because the
1090 * caller must somehow ensure that the thread has not been
1091 * destroyed. Nothing special is usually required to ensure this
1092 * when called from Java (in which there will ordinarily be a live
1093 * reference to the thread) but this is not nearly-automatically
1094 * so when calling from native code.
1095 *
1096 * @param thread the thread to unpark.
1097 */
1098 @ForceInline
1099 public void unpark(Object thread) {
1100 theInternalUnsafe.unpark(thread);
1101 }
1102
1103 /**
1104 * Blocks current thread, returning when a balancing
1105 * {@code unpark} occurs, or a balancing {@code unpark} has
1106 * already occurred, or the thread is interrupted, or, if not
1107 * absolute and time is not zero, the given time nanoseconds have
1108 * elapsed, or if absolute, the given deadline in milliseconds
1109 * since Epoch has passed, or spuriously (i.e., returning for no
1110 * "reason"). Note: This operation is in the Unsafe class only
1111 * because {@code unpark} is, so it would be strange to place it
1112 * elsewhere.
1113 */
1114 @ForceInline
1115 public void park(boolean isAbsolute, long time) {
1116 theInternalUnsafe.park(isAbsolute, time);
1117 }
1118
1119 /**
1120 * Gets the load average in the system run queue assigned
1121 * to the available processors averaged over various periods of time.
1122 * This method retrieves the given {@code nelem} samples and
1123 * assigns to the elements of the given {@code loadavg} array.
1124 * The system imposes a maximum of 3 samples, representing
1125 * averages over the last 1, 5, and 15 minutes, respectively.
1126 *
1127 * @param loadavg an array of double of size nelems
1128 * @param nelems the number of samples to be retrieved and
1129 * must be 1 to 3.
1130 *
1131 * @return the number of samples actually retrieved; or -1
1132 * if the load average is unobtainable.
1133 */
1134 @ForceInline
1135 public int getLoadAverage(double[] loadavg, int nelems) {
1136 return theInternalUnsafe.getLoadAverage(loadavg, nelems);
1137 }
1138
1139 // The following contain CAS-based Java implementations used on
1140 // platforms not supporting native instructions
1141
1142 /**
1143 * Atomically adds the given value to the current value of a field
1144 * or array element within the given object {@code o}
1145 * at the given {@code offset}.
1146 *
1147 * @param o object/array to update the field/element in
1148 * @param offset field/element offset
1149 * @param delta the value to add
1150 * @return the previous value
1151 * @since 1.8
1152 */
1153 @ForceInline
1154 public final int getAndAddInt(Object o, long offset, int delta) {
1155 return theInternalUnsafe.getAndAddInt(o, offset, delta);
1156 }
1157
1158 /**
1159 * Atomically adds the given value to the current value of a field
1160 * or array element within the given object {@code o}
1161 * at the given {@code offset}.
1162 *
1163 * @param o object/array to update the field/element in
1164 * @param offset field/element offset
1165 * @param delta the value to add
1166 * @return the previous value
1167 * @since 1.8
1168 */
1169 @ForceInline
1170 public final long getAndAddLong(Object o, long offset, long delta) {
1171 return theInternalUnsafe.getAndAddLong(o, offset, delta);
1172 }
1173
1174 /**
1175 * Atomically exchanges the given value with the current value of
1176 * a field or array element within the given object {@code o}
1177 * at the given {@code offset}.
1178 *
1179 * @param o object/array to update the field/element in
1180 * @param offset field/element offset
1181 * @param newValue new value
1182 * @return the previous value
1183 * @since 1.8
1184 */
1185 @ForceInline
1186 public final int getAndSetInt(Object o, long offset, int newValue) {
1187 return theInternalUnsafe.getAndSetInt(o, offset, newValue);
1188 }
1189
1190 /**
1191 * Atomically exchanges the given value with the current value of
1192 * a field or array element within the given object {@code o}
1193 * at the given {@code offset}.
1194 *
1195 * @param o object/array to update the field/element in
1196 * @param offset field/element offset
1197 * @param newValue new value
1198 * @return the previous value
1199 * @since 1.8
1200 */
1201 @ForceInline
1202 public final long getAndSetLong(Object o, long offset, long newValue) {
1203 return theInternalUnsafe.getAndSetLong(o, offset, newValue);
1204 }
1205
1206 /**
1207 * Atomically exchanges the given reference value with the current
1208 * reference value of a field or array element within the given
1209 * object {@code o} at the given {@code offset}.
1210 *
1211 * @param o object/array to update the field/element in
1212 * @param offset field/element offset
1213 * @param newValue new value
1214 * @return the previous value
1215 * @since 1.8
1216 */
1217 @ForceInline
1218 public final Object getAndSetObject(Object o, long offset, Object newValue) {
1219 return theInternalUnsafe.getAndSetObject(o, offset, newValue);
1220 }
1221
1222
1223 /**
1224 * Ensures that loads before the fence will not be reordered with loads and
1225 * stores after the fence; a "LoadLoad plus LoadStore barrier".
1226 *
1227 * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
1228 * (an "acquire fence").
1229 *
1230 * A pure LoadLoad fence is not provided, since the addition of LoadStore
1231 * is almost always desired, and most current hardware instructions that
1232 * provide a LoadLoad barrier also provide a LoadStore barrier for free.
1233 * @since 1.8
1234 */
1235 @ForceInline
1236 public void loadFence() {
1237 theInternalUnsafe.loadFence();
1238 }
1239
1240 /**
1241 * Ensures that loads and stores before the fence will not be reordered with
1242 * stores after the fence; a "StoreStore plus LoadStore barrier".
1243 *
1244 * Corresponds to C11 atomic_thread_fence(memory_order_release)
1245 * (a "release fence").
1246 *
1247 * A pure StoreStore fence is not provided, since the addition of LoadStore
1248 * is almost always desired, and most current hardware instructions that
1249 * provide a StoreStore barrier also provide a LoadStore barrier for free.
1250 * @since 1.8
1251 */
1252 @ForceInline
1253 public void storeFence() {
1254 theInternalUnsafe.storeFence();
1255 }
1256
1257 /**
1258 * Ensures that loads and stores before the fence will not be reordered
1259 * with loads and stores after the fence. Implies the effects of both
1260 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
1261 * barrier.
1262 *
1263 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
1264 * @since 1.8
1265 */
1266 @ForceInline
1267 public void fullFence() {
1268 theInternalUnsafe.fullFence();
1269 }
1270 }
|