1 /*
2 * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.internal.misc;
27
28 import jdk.internal.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,
48 * the checks are best effort and when performance is an overriding
49 * priority, as when methods of this class are optimized by the
50 * runtime compiler, some or all checks (if any) may be elided. Hence,
51 * the caller must not rely on the checks and corresponding
52 * exceptions!
53 *
54 * @author John R. Rose
55 * @see #getUnsafe
56 */
57 @AOTSafeClassInitializer
58 public final class Unsafe {
59
60 private static native void registerNatives();
61 static {
62 runtimeSetup();
63 }
64
65 /// BASE_OFFSET, INDEX_SCALE, and ADDRESS_SIZE fields are equivalent if the
66 /// AOT initialized heap is reused, so just register natives
67 @AOTRuntimeSetup
68 private static void runtimeSetup() {
69 registerNatives();
70 }
71
72 private Unsafe() {}
73
74 private static final Unsafe theUnsafe = new Unsafe();
75
76 /**
77 * Provides the caller with the capability of performing unsafe
78 * operations.
79 *
80 * <p>The returned {@code Unsafe} object should be carefully guarded
81 * by the caller, since it can be used to read and write data at arbitrary
82 * memory addresses. It must never be passed to untrusted code.
83 *
84 * <p>Most methods in this class are very low-level, and correspond to a
85 * small number of hardware instructions (on typical machines). Compilers
86 * are encouraged to optimize these methods accordingly.
87 *
88 * <p>Here is a suggested idiom for using unsafe operations:
89 *
90 * <pre> {@code
91 * class MyTrustedClass {
92 * private static final Unsafe unsafe = Unsafe.getUnsafe();
93 * ...
94 * private long myCountAddress = ...;
95 * public int getCount() { return unsafe.getByte(myCountAddress); }
96 * }}</pre>
97 *
98 * (It may assist compilers to make the local variable {@code final}.)
99 */
100 public static Unsafe getUnsafe() {
101 return theUnsafe;
102 }
103
104 //--- peek and poke operations
105 // (compilers should optimize these to memory ops)
106
107 // These work on object fields in the Java heap.
108 // They will not work on elements of packed arrays.
109
110 /**
111 * Fetches a value from a given Java variable.
112 * More specifically, fetches a field or array element within the given
113 * object {@code o} at the given offset, or (if {@code o} is null)
114 * from the memory address whose numerical value is the given offset.
115 * <p>
116 * The results are undefined unless one of the following cases is true:
117 * <ul>
118 * <li>The offset was obtained from {@link #objectFieldOffset} on
119 * the {@link java.lang.reflect.Field} of some Java field and the object
120 * referred to by {@code o} is of a class compatible with that
121 * field's class.
122 *
123 * <li>The offset and object reference {@code o} (either null or
124 * non-null) were both obtained via {@link #staticFieldOffset}
125 * and {@link #staticFieldBase} (respectively) from the
126 * reflective {@link Field} representation of some Java field.
127 *
128 * <li>The object referred to by {@code o} is an array, and the offset
129 * is an integer of the form {@code B+N*S}, where {@code N} is
130 * a valid index into the array, and {@code B} and {@code S} are
131 * the values obtained by {@link #arrayBaseOffset} and {@link
132 * #arrayIndexScale} (respectively) from the array's class. The value
133 * referred to is the {@code N}<em>th</em> element of the array.
134 *
135 * </ul>
136 * <p>
137 * If one of the above cases is true, the call references a specific Java
138 * variable (field or array element). However, the results are undefined
139 * if that variable is not in fact of the type returned by this method.
140 * <p>
141 * This method refers to a variable by means of two parameters, and so
142 * it provides (in effect) a <em>double-register</em> addressing mode
143 * for Java variables. When the object reference is null, this method
144 * uses its offset as an absolute address. This is similar in operation
145 * to methods such as {@link #getInt(long)}, which provide (in effect) a
146 * <em>single-register</em> addressing mode for non-Java variables.
147 * However, because Java variables may have a different layout in memory
148 * from non-Java variables, programmers should not assume that these
149 * two addressing modes are ever equivalent. Also, programmers should
150 * remember that offsets from the double-register addressing mode cannot
151 * be portably confused with longs used in the single-register addressing
152 * mode.
153 *
154 * @param o Java heap object in which the variable resides, if any, else
155 * null
156 * @param offset indication of where the variable resides in a Java heap
157 * object, if any, else a memory address locating the variable
158 * statically
159 * @return the value fetched from the indicated Java variable
160 * @throws RuntimeException No defined exceptions are thrown, not even
161 * {@link NullPointerException}
162 */
163 @IntrinsicCandidate
164 public native int getInt(Object o, long offset);
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 @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
229 /** @see #putInt(Object, long, int) */
230 @IntrinsicCandidate
231 public native void putShort(Object o, long offset, short x);
232
233 /** @see #getInt(Object, long) */
234 @IntrinsicCandidate
235 public native char getChar(Object o, long offset);
236
237 /** @see #putInt(Object, long, int) */
238 @IntrinsicCandidate
239 public native void putChar(Object o, long offset, char x);
240
241 /** @see #getInt(Object, long) */
242 @IntrinsicCandidate
243 public native long getLong(Object o, long offset);
244
245 /** @see #putInt(Object, long, int) */
246 @IntrinsicCandidate
247 public native void putLong(Object o, long offset, long x);
248
249 /** @see #getInt(Object, long) */
250 @IntrinsicCandidate
251 public native float getFloat(Object o, long offset);
252
253 /** @see #putInt(Object, long, int) */
254 @IntrinsicCandidate
255 public native void putFloat(Object o, long offset, float x);
256
257 /** @see #getInt(Object, long) */
258 @IntrinsicCandidate
259 public native double getDouble(Object o, long offset);
260
261 /** @see #putInt(Object, long, int) */
262 @IntrinsicCandidate
263 public native void putDouble(Object o, long offset, double x);
264
265 /**
266 * Fetches a native pointer from a given memory address. If the address is
267 * zero, or does not point into a block obtained from {@link
268 * #allocateMemory}, the results are undefined.
269 *
270 * <p>If the native pointer is less than 64 bits wide, it is extended as
271 * an unsigned number to a Java long. The pointer may be indexed by any
272 * given byte offset, simply by adding that offset (as a simple integer) to
273 * the long representing the pointer. The number of bytes actually read
274 * from the target address may be determined by consulting {@link
275 * #addressSize}.
276 *
277 * @see #allocateMemory
278 * @see #getInt(Object, long)
279 */
280 @ForceInline
281 public long getAddress(Object o, long offset) {
282 if (ADDRESS_SIZE == 4) {
283 return Integer.toUnsignedLong(getInt(o, offset));
284 } else {
285 return getLong(o, offset);
286 }
287 }
288
289 /**
290 * Stores a native pointer into a given memory address. If the address is
291 * zero, or does not point into a block obtained from {@link
292 * #allocateMemory}, the results are undefined.
293 *
294 * <p>The number of bytes actually written at the target address may be
295 * determined by consulting {@link #addressSize}.
296 *
297 * @see #allocateMemory
298 * @see #putInt(Object, long, int)
299 */
300 @ForceInline
301 public void putAddress(Object o, long offset, long x) {
302 if (ADDRESS_SIZE == 4) {
303 putInt(o, offset, (int)x);
304 } else {
305 putLong(o, offset, x);
306 }
307 }
308
309 // These read VM internal data.
310
311 /**
312 * Fetches an uncompressed reference value from a given native variable
313 * ignoring the VM's compressed references mode.
314 *
315 * @param address a memory address locating the variable
316 * @return the value fetched from the indicated native variable
317 */
318 public native Object getUncompressedObject(long address);
319
320 // These work on values in the C heap.
321
322 /**
323 * Fetches a value from a given memory address. If the address is zero, or
324 * does not point into a block obtained from {@link #allocateMemory}, the
325 * results are undefined.
326 *
327 * @see #allocateMemory
328 */
329 @ForceInline
330 public byte getByte(long address) {
331 return getByte(null, address);
332 }
333
334 /**
335 * Stores a value into a given memory address. If the address is zero, or
336 * does not point into a block obtained from {@link #allocateMemory}, the
337 * results are undefined.
338 *
339 * @see #getByte(long)
340 */
341 @ForceInline
342 public void putByte(long address, byte x) {
343 putByte(null, address, x);
344 }
345
346 /** @see #getByte(long) */
347 @ForceInline
348 public short getShort(long address) {
349 return getShort(null, address);
350 }
351
352 /** @see #putByte(long, byte) */
353 @ForceInline
354 public void putShort(long address, short x) {
355 putShort(null, address, x);
356 }
357
358 /** @see #getByte(long) */
359 @ForceInline
360 public char getChar(long address) {
361 return getChar(null, address);
362 }
363
364 /** @see #putByte(long, byte) */
365 @ForceInline
366 public void putChar(long address, char x) {
367 putChar(null, address, x);
368 }
369
370 /** @see #getByte(long) */
371 @ForceInline
372 public int getInt(long address) {
373 return getInt(null, address);
374 }
375
376 /** @see #putByte(long, byte) */
377 @ForceInline
378 public void putInt(long address, int x) {
379 putInt(null, address, x);
380 }
381
382 /** @see #getByte(long) */
383 @ForceInline
384 public long getLong(long address) {
385 return getLong(null, address);
386 }
387
388 /** @see #putByte(long, byte) */
389 @ForceInline
390 public void putLong(long address, long x) {
391 putLong(null, address, x);
392 }
393
394 /** @see #getByte(long) */
395 @ForceInline
396 public float getFloat(long address) {
397 return getFloat(null, address);
398 }
399
400 /** @see #putByte(long, byte) */
401 @ForceInline
402 public void putFloat(long address, float x) {
403 putFloat(null, address, x);
404 }
405
406 /** @see #getByte(long) */
407 @ForceInline
408 public double getDouble(long address) {
409 return getDouble(null, address);
410 }
411
412 /** @see #putByte(long, byte) */
413 @ForceInline
414 public void putDouble(long address, double x) {
415 putDouble(null, address, x);
416 }
417
418 /** @see #getAddress(Object, long) */
419 @ForceInline
420 public long getAddress(long address) {
421 return getAddress(null, address);
422 }
423
424 /** @see #putAddress(Object, long, long) */
425 @ForceInline
426 public void putAddress(long address, long x) {
427 putAddress(null, address, x);
428 }
429
430
431
432 //--- helper methods for validating various types of objects/values
433
434 /**
435 * Create an exception reflecting that some of the input was invalid
436 *
437 * <em>Note:</em> It is the responsibility of the caller to make
438 * sure arguments are checked before the methods are called. While
439 * some rudimentary checks are performed on the input, the checks
440 * are best effort and when performance is an overriding priority,
441 * as when methods of this class are optimized by the runtime
442 * compiler, some or all checks (if any) may be elided. Hence, the
443 * caller must not rely on the checks and corresponding
444 * exceptions!
445 *
446 * @return an exception object
447 */
448 private RuntimeException invalidInput() {
449 return new IllegalArgumentException();
450 }
451
452 /**
453 * Check if a value is 32-bit clean (32 MSB are all zero)
454 *
455 * @param value the 64-bit value to check
456 *
457 * @return true if the value is 32-bit clean
458 */
459 private boolean is32BitClean(long value) {
460 return value >>> 32 == 0;
461 }
462
463 /**
464 * Check the validity of a size (the equivalent of a size_t)
465 *
466 * @throws RuntimeException if the size is invalid
467 * (<em>Note:</em> after optimization, invalid inputs may
468 * go undetected, which will lead to unpredictable
469 * behavior)
470 */
471 private void checkSize(long size) {
472 if (ADDRESS_SIZE == 4) {
473 // Note: this will also check for negative sizes
474 if (!is32BitClean(size)) {
475 throw invalidInput();
476 }
477 } else if (size < 0) {
478 throw invalidInput();
479 }
480 }
481
482 /**
483 * Check the validity of a native address (the equivalent of void*)
484 *
485 * @throws RuntimeException if the address is invalid
486 * (<em>Note:</em> after optimization, invalid inputs may
487 * go undetected, which will lead to unpredictable
488 * behavior)
489 */
490 private void checkNativeAddress(long address) {
491 if (ADDRESS_SIZE == 4) {
492 // Accept both zero and sign extended pointers. A valid
493 // pointer will, after the +1 below, either have produced
494 // the value 0x0 or 0x1. Masking off the low bit allows
495 // for testing against 0.
496 if ((((address >> 32) + 1) & ~1) != 0) {
497 throw invalidInput();
498 }
499 }
500 }
501
502 /**
503 * Check the validity of an offset, relative to a base object
504 *
505 * @param o the base object
506 * @param offset the offset to check
507 *
508 * @throws RuntimeException if the size is invalid
509 * (<em>Note:</em> after optimization, invalid inputs may
510 * go undetected, which will lead to unpredictable
511 * behavior)
512 */
513 private void checkOffset(Object o, long offset) {
514 if (ADDRESS_SIZE == 4) {
515 // Note: this will also check for negative offsets
516 if (!is32BitClean(offset)) {
517 throw invalidInput();
518 }
519 } else if (offset < 0) {
520 throw invalidInput();
521 }
522 }
523
524 /**
525 * Check the validity of a double-register pointer
526 *
527 * Note: This code deliberately does *not* check for NPE for (at
528 * least) three reasons:
529 *
530 * 1) NPE is not just NULL/0 - there is a range of values all
531 * resulting in an NPE, which is not trivial to check for
532 *
533 * 2) It is the responsibility of the callers of Unsafe methods
534 * to verify the input, so throwing an exception here is not really
535 * useful - passing in a NULL pointer is a critical error and the
536 * must not expect an exception to be thrown anyway.
537 *
538 * 3) the actual operations will detect NULL pointers anyway by
539 * means of traps and signals (like SIGSEGV).
540 *
541 * @param o Java heap object, or null
542 * @param offset indication of where the variable resides in a Java heap
543 * object, if any, else a memory address locating the variable
544 * statically
545 *
546 * @throws RuntimeException if the pointer is invalid
547 * (<em>Note:</em> after optimization, invalid inputs may
548 * go undetected, which will lead to unpredictable
549 * behavior)
550 */
551 private void checkPointer(Object o, long offset) {
552 if (o == null) {
553 checkNativeAddress(offset);
554 } else {
555 checkOffset(o, offset);
556 }
557 }
558
559 /**
560 * Check if a type is a primitive array type
561 *
562 * @param c the type to check
563 *
564 * @return true if the type is a primitive array type
565 */
566 private void checkPrimitiveArray(Class<?> c) {
567 Class<?> componentType = c.getComponentType();
568 if (componentType == null || !componentType.isPrimitive()) {
569 throw invalidInput();
570 }
571 }
572
573 /**
574 * Check that a pointer is a valid primitive array type pointer
575 *
576 * Note: pointers off-heap are considered to be primitive arrays
577 *
578 * @throws RuntimeException if the pointer is invalid
579 * (<em>Note:</em> after optimization, invalid inputs may
580 * go undetected, which will lead to unpredictable
581 * behavior)
582 */
583 private void checkPrimitivePointer(Object o, long offset) {
584 checkPointer(o, offset);
585
586 if (o != null) {
587 // If on heap, it must be a primitive array
588 checkPrimitiveArray(o.getClass());
589 }
590 }
591
592
593 //--- wrappers for malloc, realloc, free:
594
595 /**
596 * Round up allocation size to a multiple of HeapWordSize.
597 */
598 private long alignToHeapWordSize(long bytes) {
599 if (bytes >= 0) {
600 return (bytes + ADDRESS_SIZE - 1) & ~(ADDRESS_SIZE - 1);
601 } else {
602 throw invalidInput();
603 }
604 }
605
606 /**
607 * Allocates a new block of native memory, of the given size in bytes. The
608 * contents of the memory are uninitialized; they will generally be
609 * garbage. The resulting native pointer will be zero if and only if the
610 * requested size is zero. The resulting native pointer will be aligned for
611 * all value types. Dispose of this memory by calling {@link #freeMemory}
612 * or resize it with {@link #reallocateMemory}.
613 *
614 * <em>Note:</em> It is the responsibility of the caller to make
615 * sure arguments are checked before the methods are called. While
616 * some rudimentary checks are performed on the input, the checks
617 * are best effort and when performance is an overriding priority,
618 * as when methods of this class are optimized by the runtime
619 * compiler, some or all checks (if any) may be elided. Hence, the
620 * caller must not rely on the checks and corresponding
621 * exceptions!
622 *
623 * @throws RuntimeException if the size is negative or too large
624 * for the native size_t type
625 *
626 * @throws OutOfMemoryError if the allocation is refused by the system
627 *
628 * @see #getByte(long)
629 * @see #putByte(long, byte)
630 */
631 public long allocateMemory(long bytes) {
632 bytes = alignToHeapWordSize(bytes);
633
634 allocateMemoryChecks(bytes);
635
636 if (bytes == 0) {
637 return 0;
638 }
639
640 long p = allocateMemory0(bytes);
641 if (p == 0) {
642 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes");
643 }
644
645 return p;
646 }
647
648 /**
649 * Validate the arguments to allocateMemory
650 *
651 * @throws RuntimeException if the arguments are invalid
652 * (<em>Note:</em> after optimization, invalid inputs may
653 * go undetected, which will lead to unpredictable
654 * behavior)
655 */
656 private void allocateMemoryChecks(long bytes) {
657 checkSize(bytes);
658 }
659
660 /**
661 * Resizes a new block of native memory, to the given size in bytes. The
662 * contents of the new block past the size of the old block are
663 * uninitialized; they will generally be garbage. The resulting native
664 * pointer will be zero if and only if the requested size is zero. The
665 * resulting native pointer will be aligned for all value types. Dispose
666 * of this memory by calling {@link #freeMemory}, or resize it with {@link
667 * #reallocateMemory}. The address passed to this method may be null, in
668 * which case an allocation will be performed.
669 *
670 * <em>Note:</em> It is the responsibility of the caller to make
671 * sure arguments are checked before the methods are called. While
672 * some rudimentary checks are performed on the input, the checks
673 * are best effort and when performance is an overriding priority,
674 * as when methods of this class are optimized by the runtime
675 * compiler, some or all checks (if any) may be elided. Hence, the
676 * caller must not rely on the checks and corresponding
677 * exceptions!
678 *
679 * @throws RuntimeException if the size is negative or too large
680 * for the native size_t type
681 *
682 * @throws OutOfMemoryError if the allocation is refused by the system
683 *
684 * @see #allocateMemory
685 */
686 public long reallocateMemory(long address, long bytes) {
687 bytes = alignToHeapWordSize(bytes);
688
689 reallocateMemoryChecks(address, bytes);
690
691 if (bytes == 0) {
692 freeMemory(address);
693 return 0;
694 }
695
696 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
697 if (p == 0) {
698 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes");
699 }
700
701 return p;
702 }
703
704 /**
705 * Validate the arguments to reallocateMemory
706 *
707 * @throws RuntimeException if the arguments are invalid
708 * (<em>Note:</em> after optimization, invalid inputs may
709 * go undetected, which will lead to unpredictable
710 * behavior)
711 */
712 private void reallocateMemoryChecks(long address, long bytes) {
713 checkPointer(null, address);
714 checkSize(bytes);
715 }
716
717 /**
718 * Sets all bytes in a given block of memory to a fixed value
719 * (usually zero).
720 *
721 * <p>This method determines a block's base address by means of two parameters,
722 * and so it provides (in effect) a <em>double-register</em> addressing mode,
723 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
724 * the offset supplies an absolute base address.
725 *
726 * <p>The stores are in coherent (atomic) units of a size determined
727 * by the address and length parameters. If the effective address and
728 * length are all even modulo 8, the stores take place in 'long' units.
729 * If the effective address and length are (resp.) even modulo 4 or 2,
730 * the stores take place in units of 'int' or 'short'.
731 *
732 * <em>Note:</em> It is the responsibility of the caller to make
733 * sure arguments are checked before the methods are called. While
734 * some rudimentary checks are performed on the input, the checks
735 * are best effort and when performance is an overriding priority,
736 * as when methods of this class are optimized by the runtime
737 * compiler, some or all checks (if any) may be elided. Hence, the
738 * caller must not rely on the checks and corresponding
739 * exceptions!
740 *
741 * @throws RuntimeException if any of the arguments is invalid
742 *
743 * @since 1.7
744 */
745 public void setMemory(Object o, long offset, long bytes, byte value) {
746 setMemoryChecks(o, offset, bytes, value);
747
748 if (bytes == 0) {
749 return;
750 }
751
752 setMemory0(o, offset, bytes, value);
753 }
754
755 /**
756 * Sets all bytes in a given block of memory to a fixed value
757 * (usually zero). This provides a <em>single-register</em> addressing mode,
758 * as discussed in {@link #getInt(Object,long)}.
759 *
760 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
761 */
762 public void setMemory(long address, long bytes, byte value) {
763 setMemory(null, address, bytes, value);
764 }
765
766 /**
767 * Validate the arguments to setMemory
768 *
769 * @throws RuntimeException if the arguments are invalid
770 * (<em>Note:</em> after optimization, invalid inputs may
771 * go undetected, which will lead to unpredictable
772 * behavior)
773 */
774 private void setMemoryChecks(Object o, long offset, long bytes, byte value) {
775 checkPrimitivePointer(o, offset);
776 checkSize(bytes);
777 }
778
779 /**
780 * Sets all bytes in a given block of memory to a copy of another
781 * block.
782 *
783 * <p>This method determines each block's base address by means of two parameters,
784 * and so it provides (in effect) a <em>double-register</em> addressing mode,
785 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
786 * the offset supplies an absolute base address.
787 *
788 * <p>The transfers are in coherent (atomic) units of a size determined
789 * by the address and length parameters. If the effective addresses and
790 * length are all even modulo 8, the transfer takes place in 'long' units.
791 * If the effective addresses and length are (resp.) even modulo 4 or 2,
792 * the transfer takes place in units of 'int' or 'short'.
793 *
794 * <em>Note:</em> It is the responsibility of the caller to make
795 * sure arguments are checked before the methods are called. While
796 * some rudimentary checks are performed on the input, the checks
797 * are best effort and when performance is an overriding priority,
798 * as when methods of this class are optimized by the runtime
799 * compiler, some or all checks (if any) may be elided. Hence, the
800 * caller must not rely on the checks and corresponding
801 * exceptions!
802 *
803 * @throws RuntimeException if any of the arguments is invalid
804 *
805 * @since 1.7
806 */
807 public void copyMemory(Object srcBase, long srcOffset,
808 Object destBase, long destOffset,
809 long bytes) {
810 copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
811
812 if (bytes == 0) {
813 return;
814 }
815
816 copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
817 }
818
819 /**
820 * Sets all bytes in a given block of memory to a copy of another
821 * block. This provides a <em>single-register</em> addressing mode,
822 * as discussed in {@link #getInt(Object,long)}.
823 *
824 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
825 */
826 public void copyMemory(long srcAddress, long destAddress, long bytes) {
827 copyMemory(null, srcAddress, null, destAddress, bytes);
828 }
829
830 /**
831 * Validate the arguments to copyMemory
832 *
833 * @throws RuntimeException if any of the arguments is invalid
834 * (<em>Note:</em> after optimization, invalid inputs may
835 * go undetected, which will lead to unpredictable
836 * behavior)
837 */
838 private void copyMemoryChecks(Object srcBase, long srcOffset,
839 Object destBase, long destOffset,
840 long bytes) {
841 checkSize(bytes);
842 checkPrimitivePointer(srcBase, srcOffset);
843 checkPrimitivePointer(destBase, destOffset);
844 }
845
846 /**
847 * Copies all elements from one block of memory to another block,
848 * *unconditionally* byte swapping the elements on the fly.
849 *
850 * <p>This method determines each block's base address by means of two parameters,
851 * and so it provides (in effect) a <em>double-register</em> addressing mode,
852 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
853 * the offset supplies an absolute base address.
854 *
855 * <em>Note:</em> It is the responsibility of the caller to make
856 * sure arguments are checked before the methods are called. While
857 * some rudimentary checks are performed on the input, the checks
858 * are best effort and when performance is an overriding priority,
859 * as when methods of this class are optimized by the runtime
860 * compiler, some or all checks (if any) may be elided. Hence, the
861 * caller must not rely on the checks and corresponding
862 * exceptions!
863 *
864 * @throws RuntimeException if any of the arguments is invalid
865 *
866 * @since 9
867 */
868 public void copySwapMemory(Object srcBase, long srcOffset,
869 Object destBase, long destOffset,
870 long bytes, long elemSize) {
871 copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
872
873 if (bytes == 0) {
874 return;
875 }
876
877 copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
878 }
879
880 private void copySwapMemoryChecks(Object srcBase, long srcOffset,
881 Object destBase, long destOffset,
882 long bytes, long elemSize) {
883 checkSize(bytes);
884
885 if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
886 throw invalidInput();
887 }
888 if (bytes % elemSize != 0) {
889 throw invalidInput();
890 }
891
892 checkPrimitivePointer(srcBase, srcOffset);
893 checkPrimitivePointer(destBase, destOffset);
894 }
895
896 /**
897 * Copies all elements from one block of memory to another block, byte swapping the
898 * elements on the fly.
899 *
900 * This provides a <em>single-register</em> addressing mode, as
901 * discussed in {@link #getInt(Object,long)}.
902 *
903 * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
904 */
905 public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
906 copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
907 }
908
909 /**
910 * Disposes of a block of native memory, as obtained from {@link
911 * #allocateMemory} or {@link #reallocateMemory}. The address passed to
912 * this method may be null, in which case no action is taken.
913 *
914 * <em>Note:</em> It is the responsibility of the caller to make
915 * sure arguments are checked before the methods are called. While
916 * some rudimentary checks are performed on the input, the checks
917 * are best effort and when performance is an overriding priority,
918 * as when methods of this class are optimized by the runtime
919 * compiler, some or all checks (if any) may be elided. Hence, the
920 * caller must not rely on the checks and corresponding
921 * exceptions!
922 *
923 * @throws RuntimeException if any of the arguments is invalid
924 *
925 * @see #allocateMemory
926 */
927 public void freeMemory(long address) {
928 freeMemoryChecks(address);
929
930 if (address == 0) {
931 return;
932 }
933
934 freeMemory0(address);
935 }
936
937 /**
938 * Validate the arguments to freeMemory
939 *
940 * @throws RuntimeException if the arguments are invalid
941 * (<em>Note:</em> after optimization, invalid inputs may
942 * go undetected, which will lead to unpredictable
943 * behavior)
944 */
945 private void freeMemoryChecks(long address) {
946 checkPointer(null, address);
947 }
948
949 /**
950 * Ensure writeback of a specified virtual memory address range
951 * from cache to physical memory. All bytes in the address range
952 * are guaranteed to have been written back to physical memory on
953 * return from this call i.e. subsequently executed store
954 * instructions are guaranteed not to be visible before the
955 * writeback is completed.
956 *
957 * @param address
958 * the lowest byte address that must be guaranteed written
959 * back to memory. bytes at lower addresses may also be
960 * written back.
961 *
962 * @param length
963 * the length in bytes of the region starting at address
964 * that must be guaranteed written back to memory.
965 *
966 * @throws RuntimeException if memory writeback is not supported
967 * on the current hardware of if the arguments are invalid.
968 * (<em>Note:</em> after optimization, invalid inputs may
969 * go undetected, which will lead to unpredictable
970 * behavior)
971 *
972 * @since 14
973 */
974
975 public void writebackMemory(long address, long length) {
976 checkWritebackEnabled();
977 checkWritebackMemory(address, length);
978
979 // perform any required pre-writeback barrier
980 writebackPreSync0();
981
982 // write back one cache line at a time
983 long line = dataCacheLineAlignDown(address);
984 long end = address + length;
985 while (line < end) {
986 writeback0(line);
987 line += dataCacheLineFlushSize();
988 }
989
990 // perform any required post-writeback barrier
991 writebackPostSync0();
992 }
993
994 /**
995 * Validate the arguments to writebackMemory
996 *
997 * @throws RuntimeException if the arguments are invalid
998 * (<em>Note:</em> after optimization, invalid inputs may
999 * go undetected, which will lead to unpredictable
1000 * behavior)
1001 */
1002 private void checkWritebackMemory(long address, long length) {
1003 checkNativeAddress(address);
1004 checkSize(length);
1005 }
1006
1007 /**
1008 * Validate that the current hardware supports memory writeback.
1009 * (<em>Note:</em> this is a belt and braces check. Clients are
1010 * expected to test whether writeback is enabled by calling
1011 * ({@link isWritebackEnabled #isWritebackEnabled} and avoid
1012 * calling method {@link writeback #writeback} if it is disabled).
1013 *
1014 *
1015 * @throws RuntimeException if memory writeback is not supported
1016 */
1017 private void checkWritebackEnabled() {
1018 if (!isWritebackEnabled()) {
1019 throw new RuntimeException("writebackMemory not enabled!");
1020 }
1021 }
1022
1023 /**
1024 * force writeback of an individual cache line.
1025 *
1026 * @param address
1027 * the start address of the cache line to be written back
1028 */
1029 @IntrinsicCandidate
1030 private native void writeback0(long address);
1031
1032 /**
1033 * Serialize writeback operations relative to preceding memory writes.
1034 */
1035 @IntrinsicCandidate
1036 private native void writebackPreSync0();
1037
1038 /**
1039 * Serialize writeback operations relative to following memory writes.
1040 */
1041 @IntrinsicCandidate
1042 private native void writebackPostSync0();
1043
1044 //--- random queries
1045
1046 /**
1047 * This constant differs from all results that will ever be returned from
1048 * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1049 * or {@link #arrayBaseOffset}.
1050 * <p>
1051 * The static type is @code long} to emphasize that long arithmetic should
1052 * always be used for offset calculations to avoid overflows.
1053 */
1054 public static final long INVALID_FIELD_OFFSET = -1;
1055
1056 /**
1057 * Reports the location of a given field in the storage allocation of its
1058 * class. Do not expect to perform any sort of arithmetic on this offset;
1059 * it is just a cookie which is passed to the unsafe heap memory accessors.
1060 *
1061 * <p>Any given field will always have the same offset and base, and no
1062 * two distinct fields of the same class will ever have the same offset
1063 * and base.
1064 *
1065 * <p>As of 1.4.1, offsets for fields are represented as long values,
1066 * although the Sun JVM does not use the most significant 32 bits.
1067 * However, JVM implementations which store static fields at absolute
1068 * addresses can use long offsets and null base pointers to express
1069 * the field locations in a form usable by {@link #getInt(Object,long)}.
1070 * Therefore, code which will be ported to such JVMs on 64-bit platforms
1071 * must preserve all bits of static field offsets.
1072 *
1073 * @throws NullPointerException if the field is {@code null}
1074 * @throws IllegalArgumentException if the field is static
1075 * @see #getInt(Object, long)
1076 */
1077 public long objectFieldOffset(Field f) {
1078 if (f == null) {
1079 throw new NullPointerException();
1080 }
1081
1082 return objectFieldOffset0(f);
1083 }
1084
1085 /**
1086 * (For compile-time known instance fields in JDK code only) Reports the
1087 * location of the field with a given name in the storage allocation of its
1088 * class.
1089 * <p>
1090 * This API is used to avoid creating reflective Objects in Java code at
1091 * startup. This should not be used to find fields in non-trusted code.
1092 * Use the {@link #objectFieldOffset(Field) Field}-accepting version for
1093 * arbitrary fields instead.
1094 *
1095 * @throws NullPointerException if any parameter is {@code null}.
1096 * @throws InternalError if the presumably known field couldn't be found
1097 *
1098 * @see #objectFieldOffset(Field)
1099 */
1100 public long objectFieldOffset(Class<?> c, String name) {
1101 if (c == null || name == null) {
1102 throw new NullPointerException();
1103 }
1104
1105 long result = knownObjectFieldOffset0(c, name);
1106 if (result < 0) {
1107 String type = switch ((int) result) {
1108 case -2 -> "a static field";
1109 case -1 -> "not found";
1110 default -> "unknown";
1111 };
1112 throw new InternalError("Field %s.%s %s".formatted(c.getTypeName(), name, type));
1113 }
1114 return result;
1115 }
1116
1117 /**
1118 * Reports the location of a given static field, in conjunction with {@link
1119 * #staticFieldBase}.
1120 * <p>Do not expect to perform any sort of arithmetic on this offset;
1121 * it is just a cookie which is passed to the unsafe heap memory accessors.
1122 *
1123 * <p>Any given field will always have the same offset, and no two distinct
1124 * fields of the same class will ever have the same offset.
1125 *
1126 * <p>As of 1.4.1, offsets for fields are represented as long values,
1127 * although the Sun JVM does not use the most significant 32 bits.
1128 * It is hard to imagine a JVM technology which needs more than
1129 * a few bits to encode an offset within a non-array object,
1130 * However, for consistency with other methods in this class,
1131 * this method reports its result as a long value.
1132 *
1133 * @throws NullPointerException if the field is {@code null}
1134 * @throws IllegalArgumentException if the field is not static
1135 * @see #getInt(Object, long)
1136 */
1137 public long staticFieldOffset(Field f) {
1138 if (f == null) {
1139 throw new NullPointerException();
1140 }
1141
1142 return staticFieldOffset0(f);
1143 }
1144
1145 /**
1146 * Reports the location of a given static field, in conjunction with {@link
1147 * #staticFieldOffset}.
1148 * <p>Fetch the base "Object", if any, with which static fields of the
1149 * given class can be accessed via methods like {@link #getInt(Object,
1150 * long)}. This value may be null. This value may refer to an object
1151 * which is a "cookie", not guaranteed to be a real Object, and it should
1152 * not be used in any way except as argument to the get and put routines in
1153 * this class.
1154 *
1155 * @throws NullPointerException if the field is {@code null}
1156 * @throws IllegalArgumentException if the field is not static
1157 */
1158 public Object staticFieldBase(Field f) {
1159 if (f == null) {
1160 throw new NullPointerException();
1161 }
1162
1163 return staticFieldBase0(f);
1164 }
1165
1166 /**
1167 * Detects if the given class may need to be initialized. This is often
1168 * needed in conjunction with obtaining the static field base of a
1169 * class.
1170 * @return false only if a call to {@code ensureClassInitialized} would have no effect
1171 */
1172 public boolean shouldBeInitialized(Class<?> c) {
1173 if (c == null) {
1174 throw new NullPointerException();
1175 }
1176
1177 return shouldBeInitialized0(c);
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);
1240
1241 /** The value of {@code arrayBaseOffset(long[].class)} */
1242 public static final long ARRAY_LONG_BASE_OFFSET
1243 = theUnsafe.arrayBaseOffset(long[].class);
1244
1245 /** The value of {@code arrayBaseOffset(float[].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);
1299
1300 /** The value of {@code arrayIndexScale(long[].class)} */
1301 public static final int ARRAY_LONG_INDEX_SCALE
1302 = theUnsafe.arrayIndexScale(long[].class);
1303
1304 /** The value of {@code arrayIndexScale(float[].class)} */
1305 public static final int ARRAY_FLOAT_INDEX_SCALE
1306 = theUnsafe.arrayIndexScale(float[].class);
1307
1308 /** The value of {@code arrayIndexScale(double[].class)} */
1309 public static final int ARRAY_DOUBLE_INDEX_SCALE
1310 = theUnsafe.arrayIndexScale(double[].class);
1311
1312 /** The value of {@code arrayIndexScale(Object[].class)} */
1313 public static final int ARRAY_OBJECT_INDEX_SCALE
1314 = theUnsafe.arrayIndexScale(Object[].class);
1315
1316 /**
1317 * Reports the size in bytes of a native pointer, as stored via {@link
1318 * #putAddress}. This value will be either 4 or 8. Note that the sizes of
1319 * other primitive types (as stored in native memory blocks) is determined
1320 * fully by their information content.
1321 */
1322 public int addressSize() {
1323 return ADDRESS_SIZE;
1324 }
1325
1326 /** The value of {@code addressSize()} */
1327 public static final int ADDRESS_SIZE = ADDRESS_SIZE0;
1328
1329 /**
1330 * Reports the size in bytes of a native memory page (whatever that is).
1331 * This value will always be a power of two.
1332 */
1333 public int pageSize() { return PAGE_SIZE; }
1334
1335 /**
1336 * Reports the size in bytes of a data cache line written back by
1337 * the hardware cache line flush operation available to the JVM or
1338 * 0 if data cache line flushing is not enabled.
1339 */
1340 public int dataCacheLineFlushSize() { return DATA_CACHE_LINE_FLUSH_SIZE; }
1341
1342 /**
1343 * Rounds down address to a data cache line boundary as
1344 * determined by {@link #dataCacheLineFlushSize}
1345 * @return the rounded down address
1346 */
1347 public long dataCacheLineAlignDown(long address) {
1348 return (address & ~(DATA_CACHE_LINE_FLUSH_SIZE - 1));
1349 }
1350
1351 /**
1352 * Returns true if data cache line writeback
1353 */
1354 public static boolean isWritebackEnabled() { return DATA_CACHE_LINE_FLUSH_SIZE != 0; }
1355
1356 //--- random trusted operations from JNI:
1357
1358 /**
1359 * Tells the VM to define a class, without security checks. By default, the
1360 * class loader and protection domain come from the caller's class.
1361 */
1362 public Class<?> defineClass(String name, byte[] b, int off, int len,
1363 ClassLoader loader,
1364 ProtectionDomain protectionDomain) {
1365 if (b == null) {
1366 throw new NullPointerException();
1367 }
1368 if (len < 0) {
1369 throw new ArrayIndexOutOfBoundsException();
1370 }
1371
1372 return defineClass0(name, b, off, len, loader, protectionDomain);
1373 }
1374
1375 public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1376 ClassLoader loader,
1377 ProtectionDomain protectionDomain);
1378
1379 /**
1380 * Allocates an instance but does not run any constructor.
1381 * Initializes the class if it has not yet been.
1382 */
1383 @IntrinsicCandidate
1384 public native Object allocateInstance(Class<?> cls)
1385 throws InstantiationException;
1386
1387 /**
1388 * Allocates an array of a given type, but does not do zeroing.
1389 * <p>
1390 * This method should only be used in the very rare cases where a high-performance code
1391 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1392 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1393 * <p>
1394 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1395 * before allowing untrusted code, or code in other threads, to observe the reference
1396 * to the newly allocated array. In addition, the publication of the array reference must be
1397 * safe according to the Java Memory Model requirements.
1398 * <p>
1399 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1400 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1401 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1402 * or issuing a {@link #storeFence} before publishing the reference.
1403 * <p>
1404 * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1405 * elements that could break heap integrity.
1406 *
1407 * @param componentType array component type to allocate
1408 * @param length array size to allocate
1409 * @throws IllegalArgumentException if component type is null, or not a primitive class;
1410 * or the length is negative
1411 */
1412 public Object allocateUninitializedArray(Class<?> componentType, int length) {
1413 if (componentType == null) {
1414 throw new IllegalArgumentException("Component type is null");
1415 }
1416 if (!componentType.isPrimitive()) {
1417 throw new IllegalArgumentException("Component type is not primitive");
1418 }
1419 if (length < 0) {
1420 throw new IllegalArgumentException("Negative length");
1421 }
1422 return allocateUninitializedArray0(componentType, length);
1423 }
1424
1425 @IntrinsicCandidate
1426 private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1427 // These fallbacks provide zeroed arrays, but intrinsic is not required to
1428 // return the zeroed arrays.
1429 if (componentType == byte.class) return new byte[length];
1430 if (componentType == boolean.class) return new boolean[length];
1431 if (componentType == short.class) return new short[length];
1432 if (componentType == char.class) return new char[length];
1433 if (componentType == int.class) return new int[length];
1434 if (componentType == float.class) return new float[length];
1435 if (componentType == long.class) return new long[length];
1436 if (componentType == double.class) return new double[length];
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
1524 public final int compareAndExchangeIntAcquire(Object o, long offset,
1525 int expected,
1526 int x) {
1527 return compareAndExchangeInt(o, offset, expected, x);
1528 }
1529
1530 @IntrinsicCandidate
1531 public final int compareAndExchangeIntRelease(Object o, long offset,
1532 int expected,
1533 int x) {
1534 return compareAndExchangeInt(o, offset, expected, x);
1535 }
1536
1537 @IntrinsicCandidate
1538 public final boolean weakCompareAndSetIntPlain(Object o, long offset,
1539 int expected,
1540 int x) {
1541 return compareAndSetInt(o, offset, expected, x);
1542 }
1543
1544 @IntrinsicCandidate
1545 public final boolean weakCompareAndSetIntAcquire(Object o, long offset,
1546 int expected,
1547 int x) {
1548 return compareAndSetInt(o, offset, expected, x);
1549 }
1550
1551 @IntrinsicCandidate
1552 public final boolean weakCompareAndSetIntRelease(Object o, long offset,
1553 int expected,
1554 int x) {
1555 return compareAndSetInt(o, offset, expected, x);
1556 }
1557
1558 @IntrinsicCandidate
1559 public final boolean weakCompareAndSetInt(Object o, long offset,
1560 int expected,
1561 int x) {
1562 return compareAndSetInt(o, offset, expected, x);
1563 }
1564
1565 @IntrinsicCandidate
1566 public final byte compareAndExchangeByte(Object o, long offset,
1567 byte expected,
1568 byte x) {
1569 long wordOffset = offset & ~3;
1570 int shift = (int) (offset & 3) << 3;
1571 if (BIG_ENDIAN) {
1572 shift = 24 - shift;
1573 }
1574 int mask = 0xFF << shift;
1575 int maskedExpected = (expected & 0xFF) << shift;
1576 int maskedX = (x & 0xFF) << shift;
1577 int fullWord;
1578 do {
1579 fullWord = getIntVolatile(o, wordOffset);
1580 if ((fullWord & mask) != maskedExpected)
1581 return (byte) ((fullWord & mask) >> shift);
1582 } while (!weakCompareAndSetInt(o, wordOffset,
1583 fullWord, (fullWord & ~mask) | maskedX));
1584 return expected;
1585 }
1586
1587 @IntrinsicCandidate
1588 public final boolean compareAndSetByte(Object o, long offset,
1589 byte expected,
1590 byte x) {
1591 return compareAndExchangeByte(o, offset, expected, x) == expected;
1592 }
1593
1594 @IntrinsicCandidate
1595 public final boolean weakCompareAndSetByte(Object o, long offset,
1596 byte expected,
1597 byte x) {
1598 return compareAndSetByte(o, offset, expected, x);
1599 }
1600
1601 @IntrinsicCandidate
1602 public final boolean weakCompareAndSetByteAcquire(Object o, long offset,
1603 byte expected,
1604 byte x) {
1605 return weakCompareAndSetByte(o, offset, expected, x);
1606 }
1607
1608 @IntrinsicCandidate
1609 public final boolean weakCompareAndSetByteRelease(Object o, long offset,
1610 byte expected,
1611 byte x) {
1612 return weakCompareAndSetByte(o, offset, expected, x);
1613 }
1614
1615 @IntrinsicCandidate
1616 public final boolean weakCompareAndSetBytePlain(Object o, long offset,
1617 byte expected,
1618 byte x) {
1619 return weakCompareAndSetByte(o, offset, expected, x);
1620 }
1621
1622 @IntrinsicCandidate
1623 public final byte compareAndExchangeByteAcquire(Object o, long offset,
1624 byte expected,
1625 byte x) {
1626 return compareAndExchangeByte(o, offset, expected, x);
1627 }
1628
1629 @IntrinsicCandidate
1630 public final byte compareAndExchangeByteRelease(Object o, long offset,
1631 byte expected,
1632 byte x) {
1633 return compareAndExchangeByte(o, offset, expected, x);
1634 }
1635
1636 @IntrinsicCandidate
1637 public final short compareAndExchangeShort(Object o, long offset,
1638 short expected,
1639 short x) {
1640 if ((offset & 3) == 3) {
1641 throw new IllegalArgumentException("Update spans the word, not supported");
1642 }
1643 long wordOffset = offset & ~3;
1644 int shift = (int) (offset & 3) << 3;
1645 if (BIG_ENDIAN) {
1646 shift = 16 - shift;
1647 }
1648 int mask = 0xFFFF << shift;
1649 int maskedExpected = (expected & 0xFFFF) << shift;
1650 int maskedX = (x & 0xFFFF) << shift;
1651 int fullWord;
1652 do {
1653 fullWord = getIntVolatile(o, wordOffset);
1654 if ((fullWord & mask) != maskedExpected) {
1655 return (short) ((fullWord & mask) >> shift);
1656 }
1657 } while (!weakCompareAndSetInt(o, wordOffset,
1658 fullWord, (fullWord & ~mask) | maskedX));
1659 return expected;
1660 }
1661
1662 @IntrinsicCandidate
1663 public final boolean compareAndSetShort(Object o, long offset,
1664 short expected,
1665 short x) {
1666 return compareAndExchangeShort(o, offset, expected, x) == expected;
1667 }
1668
1669 @IntrinsicCandidate
1670 public final boolean weakCompareAndSetShort(Object o, long offset,
1671 short expected,
1672 short x) {
1673 return compareAndSetShort(o, offset, expected, x);
1674 }
1675
1676 @IntrinsicCandidate
1677 public final boolean weakCompareAndSetShortAcquire(Object o, long offset,
1678 short expected,
1679 short x) {
1680 return weakCompareAndSetShort(o, offset, expected, x);
1681 }
1682
1683 @IntrinsicCandidate
1684 public final boolean weakCompareAndSetShortRelease(Object o, long offset,
1685 short expected,
1686 short x) {
1687 return weakCompareAndSetShort(o, offset, expected, x);
1688 }
1689
1690 @IntrinsicCandidate
1691 public final boolean weakCompareAndSetShortPlain(Object o, long offset,
1692 short expected,
1693 short x) {
1694 return weakCompareAndSetShort(o, offset, expected, x);
1695 }
1696
1697
1698 @IntrinsicCandidate
1699 public final short compareAndExchangeShortAcquire(Object o, long offset,
1700 short expected,
1701 short x) {
1702 return compareAndExchangeShort(o, offset, expected, x);
1703 }
1704
1705 @IntrinsicCandidate
1706 public final short compareAndExchangeShortRelease(Object o, long offset,
1707 short expected,
1708 short x) {
1709 return compareAndExchangeShort(o, offset, expected, x);
1710 }
1711
1712 @ForceInline
1713 private char s2c(short s) {
1714 return (char) s;
1715 }
1716
1717 @ForceInline
1718 private short c2s(char s) {
1719 return (short) s;
1720 }
1721
1722 @ForceInline
1723 public final boolean compareAndSetChar(Object o, long offset,
1724 char expected,
1725 char x) {
1726 return compareAndSetShort(o, offset, c2s(expected), c2s(x));
1727 }
1728
1729 @ForceInline
1730 public final char compareAndExchangeChar(Object o, long offset,
1731 char expected,
1732 char x) {
1733 return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x)));
1734 }
1735
1736 @ForceInline
1737 public final char compareAndExchangeCharAcquire(Object o, long offset,
1738 char expected,
1739 char x) {
1740 return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x)));
1741 }
1742
1743 @ForceInline
1744 public final char compareAndExchangeCharRelease(Object o, long offset,
1745 char expected,
1746 char x) {
1747 return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x)));
1748 }
1749
1750 @ForceInline
1751 public final boolean weakCompareAndSetChar(Object o, long offset,
1752 char expected,
1753 char x) {
1754 return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x));
1755 }
1756
1757 @ForceInline
1758 public final boolean weakCompareAndSetCharAcquire(Object o, long offset,
1759 char expected,
1760 char x) {
1761 return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x));
1762 }
1763
1764 @ForceInline
1765 public final boolean weakCompareAndSetCharRelease(Object o, long offset,
1766 char expected,
1767 char x) {
1768 return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x));
1769 }
1770
1771 @ForceInline
1772 public final boolean weakCompareAndSetCharPlain(Object o, long offset,
1773 char expected,
1774 char x) {
1775 return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x));
1776 }
1777
1778 /**
1779 * The JVM converts integral values to boolean values using two
1780 * different conventions, byte testing against zero and truncation
1781 * to least-significant bit.
1782 *
1783 * <p>The JNI documents specify that, at least for returning
1784 * values from native methods, a Java boolean value is converted
1785 * to the value-set 0..1 by first truncating to a byte (0..255 or
1786 * maybe -128..127) and then testing against zero.
1787 *
1788 * <p>Java booleans in the heap are also stored in bytes, but are
1789 * strongly normalized to the value-set 0..1 (i.e., they are
1790 * truncated to the least-significant bit).
1791 *
1792 * <p>The main reason for having different conventions for
1793 * conversion is performance: Truncation to the least-significant
1794 * bit can be usually implemented with fewer (machine)
1795 * instructions than byte testing against zero.
1796 *
1797 * <p>A number of Unsafe methods load boolean values as bytes,
1798 * truncate them to the least-significant bit, and then test
1799 * against zero. It is uniformly performed for both Java heap
1800 * and non-Java heap accesses.
1801 *
1802 * @param b the byte to be converted to boolean
1803 * @return the result of the conversion
1804 */
1805 @ForceInline
1806 private boolean byte2bool(byte b) {
1807 return (b & 1) != 0;
1808 }
1809
1810 /**
1811 * Convert a boolean value to a byte. The return value is strongly
1812 * normalized to the value-set 0..1 (i.e., the value is truncated
1813 * to the least-significant bit). See {@link #byte2bool(byte)} for
1814 * more details on conversion conventions.
1815 *
1816 * @param b the boolean to be converted to byte (and then normalized)
1817 * @return the result of the conversion
1818 */
1819 @ForceInline
1820 private byte bool2byte(boolean b) {
1821 return b ? (byte)1 : (byte)0;
1822 }
1823
1824 @ForceInline
1825 public final boolean compareAndSetBoolean(Object o, long offset,
1826 boolean expected,
1827 boolean x) {
1828 return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
1829 }
1830
1831 @ForceInline
1832 public final boolean compareAndExchangeBoolean(Object o, long offset,
1833 boolean expected,
1834 boolean x) {
1835 return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x)));
1836 }
1837
1838 @ForceInline
1839 public final boolean compareAndExchangeBooleanAcquire(Object o, long offset,
1840 boolean expected,
1841 boolean x) {
1842 return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x)));
1843 }
1844
1845 @ForceInline
1846 public final boolean compareAndExchangeBooleanRelease(Object o, long offset,
1847 boolean expected,
1848 boolean x) {
1849 return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x)));
1850 }
1851
1852 @ForceInline
1853 public final boolean weakCompareAndSetBoolean(Object o, long offset,
1854 boolean expected,
1855 boolean x) {
1856 return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
1857 }
1858
1859 @ForceInline
1860 public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset,
1861 boolean expected,
1862 boolean x) {
1863 return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x));
1864 }
1865
1866 @ForceInline
1867 public final boolean weakCompareAndSetBooleanRelease(Object o, long offset,
1868 boolean expected,
1869 boolean x) {
1870 return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x));
1871 }
1872
1873 @ForceInline
1874 public final boolean weakCompareAndSetBooleanPlain(Object o, long offset,
1875 boolean expected,
1876 boolean x) {
1877 return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x));
1878 }
1879
1880 /**
1881 * Atomically updates Java variable to {@code x} if it is currently
1882 * holding {@code expected}.
1883 *
1884 * <p>This operation has memory semantics of a {@code volatile} read
1885 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1886 *
1887 * @return {@code true} if successful
1888 */
1889 @ForceInline
1890 public final boolean compareAndSetFloat(Object o, long offset,
1891 float expected,
1892 float x) {
1893 return compareAndSetInt(o, offset,
1894 Float.floatToRawIntBits(expected),
1895 Float.floatToRawIntBits(x));
1896 }
1897
1898 @ForceInline
1899 public final float compareAndExchangeFloat(Object o, long offset,
1900 float expected,
1901 float x) {
1902 int w = compareAndExchangeInt(o, offset,
1903 Float.floatToRawIntBits(expected),
1904 Float.floatToRawIntBits(x));
1905 return Float.intBitsToFloat(w);
1906 }
1907
1908 @ForceInline
1909 public final float compareAndExchangeFloatAcquire(Object o, long offset,
1910 float expected,
1911 float x) {
1912 int w = compareAndExchangeIntAcquire(o, offset,
1913 Float.floatToRawIntBits(expected),
1914 Float.floatToRawIntBits(x));
1915 return Float.intBitsToFloat(w);
1916 }
1917
1918 @ForceInline
1919 public final float compareAndExchangeFloatRelease(Object o, long offset,
1920 float expected,
1921 float x) {
1922 int w = compareAndExchangeIntRelease(o, offset,
1923 Float.floatToRawIntBits(expected),
1924 Float.floatToRawIntBits(x));
1925 return Float.intBitsToFloat(w);
1926 }
1927
1928 @ForceInline
1929 public final boolean weakCompareAndSetFloatPlain(Object o, long offset,
1930 float expected,
1931 float x) {
1932 return weakCompareAndSetIntPlain(o, offset,
1933 Float.floatToRawIntBits(expected),
1934 Float.floatToRawIntBits(x));
1935 }
1936
1937 @ForceInline
1938 public final boolean weakCompareAndSetFloatAcquire(Object o, long offset,
1939 float expected,
1940 float x) {
1941 return weakCompareAndSetIntAcquire(o, offset,
1942 Float.floatToRawIntBits(expected),
1943 Float.floatToRawIntBits(x));
1944 }
1945
1946 @ForceInline
1947 public final boolean weakCompareAndSetFloatRelease(Object o, long offset,
1948 float expected,
1949 float x) {
1950 return weakCompareAndSetIntRelease(o, offset,
1951 Float.floatToRawIntBits(expected),
1952 Float.floatToRawIntBits(x));
1953 }
1954
1955 @ForceInline
1956 public final boolean weakCompareAndSetFloat(Object o, long offset,
1957 float expected,
1958 float x) {
1959 return weakCompareAndSetInt(o, offset,
1960 Float.floatToRawIntBits(expected),
1961 Float.floatToRawIntBits(x));
1962 }
1963
1964 /**
1965 * Atomically updates Java variable to {@code x} if it is currently
1966 * holding {@code expected}.
1967 *
1968 * <p>This operation has memory semantics of a {@code volatile} read
1969 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1970 *
1971 * @return {@code true} if successful
1972 */
1973 @ForceInline
1974 public final boolean compareAndSetDouble(Object o, long offset,
1975 double expected,
1976 double x) {
1977 return compareAndSetLong(o, offset,
1978 Double.doubleToRawLongBits(expected),
1979 Double.doubleToRawLongBits(x));
1980 }
1981
1982 @ForceInline
1983 public final double compareAndExchangeDouble(Object o, long offset,
1984 double expected,
1985 double x) {
1986 long w = compareAndExchangeLong(o, offset,
1987 Double.doubleToRawLongBits(expected),
1988 Double.doubleToRawLongBits(x));
1989 return Double.longBitsToDouble(w);
1990 }
1991
1992 @ForceInline
1993 public final double compareAndExchangeDoubleAcquire(Object o, long offset,
1994 double expected,
1995 double x) {
1996 long w = compareAndExchangeLongAcquire(o, offset,
1997 Double.doubleToRawLongBits(expected),
1998 Double.doubleToRawLongBits(x));
1999 return Double.longBitsToDouble(w);
2000 }
2001
2002 @ForceInline
2003 public final double compareAndExchangeDoubleRelease(Object o, long offset,
2004 double expected,
2005 double x) {
2006 long w = compareAndExchangeLongRelease(o, offset,
2007 Double.doubleToRawLongBits(expected),
2008 Double.doubleToRawLongBits(x));
2009 return Double.longBitsToDouble(w);
2010 }
2011
2012 @ForceInline
2013 public final boolean weakCompareAndSetDoublePlain(Object o, long offset,
2014 double expected,
2015 double x) {
2016 return weakCompareAndSetLongPlain(o, offset,
2017 Double.doubleToRawLongBits(expected),
2018 Double.doubleToRawLongBits(x));
2019 }
2020
2021 @ForceInline
2022 public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset,
2023 double expected,
2024 double x) {
2025 return weakCompareAndSetLongAcquire(o, offset,
2026 Double.doubleToRawLongBits(expected),
2027 Double.doubleToRawLongBits(x));
2028 }
2029
2030 @ForceInline
2031 public final boolean weakCompareAndSetDoubleRelease(Object o, long offset,
2032 double expected,
2033 double x) {
2034 return weakCompareAndSetLongRelease(o, offset,
2035 Double.doubleToRawLongBits(expected),
2036 Double.doubleToRawLongBits(x));
2037 }
2038
2039 @ForceInline
2040 public final boolean weakCompareAndSetDouble(Object o, long offset,
2041 double expected,
2042 double x) {
2043 return weakCompareAndSetLong(o, offset,
2044 Double.doubleToRawLongBits(expected),
2045 Double.doubleToRawLongBits(x));
2046 }
2047
2048 /**
2049 * Atomically updates Java variable to {@code x} if it is currently
2050 * holding {@code expected}.
2051 *
2052 * <p>This operation has memory semantics of a {@code volatile} read
2053 * and write. Corresponds to C11 atomic_compare_exchange_strong.
2054 *
2055 * @return {@code true} if successful
2056 */
2057 @IntrinsicCandidate
2058 public final native boolean compareAndSetLong(Object o, long offset,
2059 long expected,
2060 long x);
2061
2062 @IntrinsicCandidate
2063 public final native long compareAndExchangeLong(Object o, long offset,
2064 long expected,
2065 long x);
2066
2067 @IntrinsicCandidate
2068 public final long compareAndExchangeLongAcquire(Object o, long offset,
2069 long expected,
2070 long x) {
2071 return compareAndExchangeLong(o, offset, expected, x);
2072 }
2073
2074 @IntrinsicCandidate
2075 public final long compareAndExchangeLongRelease(Object o, long offset,
2076 long expected,
2077 long x) {
2078 return compareAndExchangeLong(o, offset, expected, x);
2079 }
2080
2081 @IntrinsicCandidate
2082 public final boolean weakCompareAndSetLongPlain(Object o, long offset,
2083 long expected,
2084 long x) {
2085 return compareAndSetLong(o, offset, expected, x);
2086 }
2087
2088 @IntrinsicCandidate
2089 public final boolean weakCompareAndSetLongAcquire(Object o, long offset,
2090 long expected,
2091 long x) {
2092 return compareAndSetLong(o, offset, expected, x);
2093 }
2094
2095 @IntrinsicCandidate
2096 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2097 long expected,
2098 long x) {
2099 return compareAndSetLong(o, offset, expected, x);
2100 }
2101
2102 @IntrinsicCandidate
2103 public final boolean weakCompareAndSetLong(Object o, long offset,
2104 long expected,
2105 long x) {
2106 return compareAndSetLong(o, offset, expected, x);
2107 }
2108
2109 /**
2110 * Fetches a reference value from a given Java variable, with volatile
2111 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2112 */
2113 @IntrinsicCandidate
2114 public native Object getReferenceVolatile(Object o, long offset);
2115
2116 /**
2117 * Stores a reference value into a given Java variable, with
2118 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2119 */
2120 @IntrinsicCandidate
2121 public native void putReferenceVolatile(Object o, long offset, Object x);
2122
2123 /** Volatile version of {@link #getInt(Object, long)} */
2124 @IntrinsicCandidate
2125 public native int getIntVolatile(Object o, long offset);
2126
2127 /** Volatile version of {@link #putInt(Object, long, int)} */
2128 @IntrinsicCandidate
2129 public native void putIntVolatile(Object o, long offset, int x);
2130
2131 /** Volatile version of {@link #getBoolean(Object, long)} */
2132 @IntrinsicCandidate
2133 public native boolean getBooleanVolatile(Object o, long offset);
2134
2135 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2136 @IntrinsicCandidate
2137 public native void putBooleanVolatile(Object o, long offset, boolean x);
2138
2139 /** Volatile version of {@link #getByte(Object, long)} */
2140 @IntrinsicCandidate
2141 public native byte getByteVolatile(Object o, long offset);
2142
2143 /** Volatile version of {@link #putByte(Object, long, byte)} */
2144 @IntrinsicCandidate
2145 public native void putByteVolatile(Object o, long offset, byte x);
2146
2147 /** Volatile version of {@link #getShort(Object, long)} */
2148 @IntrinsicCandidate
2149 public native short getShortVolatile(Object o, long offset);
2150
2151 /** Volatile version of {@link #putShort(Object, long, short)} */
2152 @IntrinsicCandidate
2153 public native void putShortVolatile(Object o, long offset, short x);
2154
2155 /** Volatile version of {@link #getChar(Object, long)} */
2156 @IntrinsicCandidate
2157 public native char getCharVolatile(Object o, long offset);
2158
2159 /** Volatile version of {@link #putChar(Object, long, char)} */
2160 @IntrinsicCandidate
2161 public native void putCharVolatile(Object o, long offset, char x);
2162
2163 /** Volatile version of {@link #getLong(Object, long)} */
2164 @IntrinsicCandidate
2165 public native long getLongVolatile(Object o, long offset);
2166
2167 /** Volatile version of {@link #putLong(Object, long, long)} */
2168 @IntrinsicCandidate
2169 public native void putLongVolatile(Object o, long offset, long x);
2170
2171 /** Volatile version of {@link #getFloat(Object, long)} */
2172 @IntrinsicCandidate
2173 public native float getFloatVolatile(Object o, long offset);
2174
2175 /** Volatile version of {@link #putFloat(Object, long, float)} */
2176 @IntrinsicCandidate
2177 public native void putFloatVolatile(Object o, long offset, float x);
2178
2179 /** Volatile version of {@link #getDouble(Object, long)} */
2180 @IntrinsicCandidate
2181 public native double getDoubleVolatile(Object o, long offset);
2182
2183 /** Volatile version of {@link #putDouble(Object, long, double)} */
2184 @IntrinsicCandidate
2185 public native void putDoubleVolatile(Object o, long offset, double x);
2186
2187
2188
2189 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2190 @IntrinsicCandidate
2191 public final Object getReferenceAcquire(Object o, long offset) {
2192 return getReferenceVolatile(o, offset);
2193 }
2194
2195 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2196 @IntrinsicCandidate
2197 public final boolean getBooleanAcquire(Object o, long offset) {
2198 return getBooleanVolatile(o, offset);
2199 }
2200
2201 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2202 @IntrinsicCandidate
2203 public final byte getByteAcquire(Object o, long offset) {
2204 return getByteVolatile(o, offset);
2205 }
2206
2207 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2208 @IntrinsicCandidate
2209 public final short getShortAcquire(Object o, long offset) {
2210 return getShortVolatile(o, offset);
2211 }
2212
2213 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2214 @IntrinsicCandidate
2215 public final char getCharAcquire(Object o, long offset) {
2216 return getCharVolatile(o, offset);
2217 }
2218
2219 /** Acquire version of {@link #getIntVolatile(Object, long)} */
2220 @IntrinsicCandidate
2221 public final int getIntAcquire(Object o, long offset) {
2222 return getIntVolatile(o, offset);
2223 }
2224
2225 /** Acquire version of {@link #getFloatVolatile(Object, long)} */
2226 @IntrinsicCandidate
2227 public final float getFloatAcquire(Object o, long offset) {
2228 return getFloatVolatile(o, offset);
2229 }
2230
2231 /** Acquire version of {@link #getLongVolatile(Object, long)} */
2232 @IntrinsicCandidate
2233 public final long getLongAcquire(Object o, long offset) {
2234 return getLongVolatile(o, offset);
2235 }
2236
2237 /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
2238 @IntrinsicCandidate
2239 public final double getDoubleAcquire(Object o, long offset) {
2240 return getDoubleVolatile(o, offset);
2241 }
2242
2243 /*
2244 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2245 * that do not guarantee immediate visibility of the store to
2246 * other threads. This method is generally only useful if the
2247 * underlying field is a Java volatile (or if an array cell, one
2248 * that is otherwise only accessed using volatile accesses).
2249 *
2250 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2251 */
2252
2253 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2254 @IntrinsicCandidate
2255 public final void putReferenceRelease(Object o, long offset, Object x) {
2256 putReferenceVolatile(o, offset, x);
2257 }
2258
2259 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2260 @IntrinsicCandidate
2261 public final void putBooleanRelease(Object o, long offset, boolean x) {
2262 putBooleanVolatile(o, offset, x);
2263 }
2264
2265 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2266 @IntrinsicCandidate
2267 public final void putByteRelease(Object o, long offset, byte x) {
2268 putByteVolatile(o, offset, x);
2269 }
2270
2271 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2272 @IntrinsicCandidate
2273 public final void putShortRelease(Object o, long offset, short x) {
2274 putShortVolatile(o, offset, x);
2275 }
2276
2277 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2278 @IntrinsicCandidate
2279 public final void putCharRelease(Object o, long offset, char x) {
2280 putCharVolatile(o, offset, x);
2281 }
2282
2283 /** Release version of {@link #putIntVolatile(Object, long, int)} */
2284 @IntrinsicCandidate
2285 public final void putIntRelease(Object o, long offset, int x) {
2286 putIntVolatile(o, offset, x);
2287 }
2288
2289 /** Release version of {@link #putFloatVolatile(Object, long, float)} */
2290 @IntrinsicCandidate
2291 public final void putFloatRelease(Object o, long offset, float x) {
2292 putFloatVolatile(o, offset, x);
2293 }
2294
2295 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2296 @IntrinsicCandidate
2297 public final void putLongRelease(Object o, long offset, long x) {
2298 putLongVolatile(o, offset, x);
2299 }
2300
2301 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2302 @IntrinsicCandidate
2303 public final void putDoubleRelease(Object o, long offset, double x) {
2304 putDoubleVolatile(o, offset, x);
2305 }
2306
2307 // ------------------------------ Opaque --------------------------------------
2308
2309 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2310 @IntrinsicCandidate
2311 public final Object getReferenceOpaque(Object o, long offset) {
2312 return getReferenceVolatile(o, offset);
2313 }
2314
2315 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2316 @IntrinsicCandidate
2317 public final boolean getBooleanOpaque(Object o, long offset) {
2318 return getBooleanVolatile(o, offset);
2319 }
2320
2321 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2322 @IntrinsicCandidate
2323 public final byte getByteOpaque(Object o, long offset) {
2324 return getByteVolatile(o, offset);
2325 }
2326
2327 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2328 @IntrinsicCandidate
2329 public final short getShortOpaque(Object o, long offset) {
2330 return getShortVolatile(o, offset);
2331 }
2332
2333 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2334 @IntrinsicCandidate
2335 public final char getCharOpaque(Object o, long offset) {
2336 return getCharVolatile(o, offset);
2337 }
2338
2339 /** Opaque version of {@link #getIntVolatile(Object, long)} */
2340 @IntrinsicCandidate
2341 public final int getIntOpaque(Object o, long offset) {
2342 return getIntVolatile(o, offset);
2343 }
2344
2345 /** Opaque version of {@link #getFloatVolatile(Object, long)} */
2346 @IntrinsicCandidate
2347 public final float getFloatOpaque(Object o, long offset) {
2348 return getFloatVolatile(o, offset);
2349 }
2350
2351 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2352 @IntrinsicCandidate
2353 public final long getLongOpaque(Object o, long offset) {
2354 return getLongVolatile(o, offset);
2355 }
2356
2357 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2358 @IntrinsicCandidate
2359 public final double getDoubleOpaque(Object o, long offset) {
2360 return getDoubleVolatile(o, offset);
2361 }
2362
2363 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2364 @IntrinsicCandidate
2365 public final void putReferenceOpaque(Object o, long offset, Object x) {
2366 putReferenceVolatile(o, offset, x);
2367 }
2368
2369 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2370 @IntrinsicCandidate
2371 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2372 putBooleanVolatile(o, offset, x);
2373 }
2374
2375 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2376 @IntrinsicCandidate
2377 public final void putByteOpaque(Object o, long offset, byte x) {
2378 putByteVolatile(o, offset, x);
2379 }
2380
2381 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2382 @IntrinsicCandidate
2383 public final void putShortOpaque(Object o, long offset, short x) {
2384 putShortVolatile(o, offset, x);
2385 }
2386
2387 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2388 @IntrinsicCandidate
2389 public final void putCharOpaque(Object o, long offset, char x) {
2390 putCharVolatile(o, offset, x);
2391 }
2392
2393 /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
2394 @IntrinsicCandidate
2395 public final void putIntOpaque(Object o, long offset, int x) {
2396 putIntVolatile(o, offset, x);
2397 }
2398
2399 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2400 @IntrinsicCandidate
2401 public final void putFloatOpaque(Object o, long offset, float x) {
2402 putFloatVolatile(o, offset, x);
2403 }
2404
2405 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2406 @IntrinsicCandidate
2407 public final void putLongOpaque(Object o, long offset, long x) {
2408 putLongVolatile(o, offset, x);
2409 }
2410
2411 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2412 @IntrinsicCandidate
2413 public final void putDoubleOpaque(Object o, long offset, double x) {
2414 putDoubleVolatile(o, offset, x);
2415 }
2416
2417 /**
2418 * Unblocks the given thread blocked on {@code park}, or, if it is
2419 * not blocked, causes the subsequent call to {@code park} not to
2420 * block. Note: this operation is "unsafe" solely because the
2421 * caller must somehow ensure that the thread has not been
2422 * destroyed. Nothing special is usually required to ensure this
2423 * when called from Java (in which there will ordinarily be a live
2424 * reference to the thread) but this is not nearly-automatically
2425 * so when calling from native code.
2426 *
2427 * @param thread the thread to unpark.
2428 */
2429 @IntrinsicCandidate
2430 public native void unpark(Object thread);
2431
2432 /**
2433 * Blocks current thread, returning when a balancing
2434 * {@code unpark} occurs, or a balancing {@code unpark} has
2435 * already occurred, or the thread is interrupted, or, if not
2436 * absolute and time is not zero, the given time nanoseconds have
2437 * elapsed, or if absolute, the given deadline in milliseconds
2438 * since Epoch has passed, or spuriously (i.e., returning for no
2439 * "reason"). Note: This operation is in the Unsafe class only
2440 * because {@code unpark} is, so it would be strange to place it
2441 * elsewhere.
2442 */
2443 @IntrinsicCandidate
2444 public native void park(boolean isAbsolute, long time);
2445
2446 /**
2447 * Gets the load average in the system run queue assigned
2448 * to the available processors averaged over various periods of time.
2449 * This method retrieves the given {@code nelem} samples and
2450 * assigns to the elements of the given {@code loadavg} array.
2451 * The system imposes a maximum of 3 samples, representing
2452 * averages over the last 1, 5, and 15 minutes, respectively.
2453 *
2454 * @param loadavg an array of double of size nelems
2455 * @param nelems the number of samples to be retrieved and
2456 * must be 1 to 3.
2457 *
2458 * @return the number of samples actually retrieved; or -1
2459 * if the load average is unobtainable.
2460 */
2461 public int getLoadAverage(double[] loadavg, int nelems) {
2462 if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
2463 throw new ArrayIndexOutOfBoundsException();
2464 }
2465
2466 return getLoadAverage0(loadavg, nelems);
2467 }
2468
2469 // The following contain CAS-based Java implementations used on
2470 // platforms not supporting native instructions
2471
2472 /**
2473 * Atomically adds the given value to the current value of a field
2474 * or array element within the given object {@code o}
2475 * at the given {@code offset}.
2476 *
2477 * @param o object/array to update the field/element in
2478 * @param offset field/element offset
2479 * @param delta the value to add
2480 * @return the previous value
2481 * @since 1.8
2482 */
2483 @IntrinsicCandidate
2484 public final int getAndAddInt(Object o, long offset, int delta) {
2485 int v;
2486 do {
2487 v = getIntVolatile(o, offset);
2488 } while (!weakCompareAndSetInt(o, offset, v, v + delta));
2489 return v;
2490 }
2491
2492 @ForceInline
2493 public final int getAndAddIntRelease(Object o, long offset, int delta) {
2494 int v;
2495 do {
2496 v = getInt(o, offset);
2497 } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta));
2498 return v;
2499 }
2500
2501 @ForceInline
2502 public final int getAndAddIntAcquire(Object o, long offset, int delta) {
2503 int v;
2504 do {
2505 v = getIntAcquire(o, offset);
2506 } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta));
2507 return v;
2508 }
2509
2510 /**
2511 * Atomically adds the given value to the current value of a field
2512 * or array element within the given object {@code o}
2513 * at the given {@code offset}.
2514 *
2515 * @param o object/array to update the field/element in
2516 * @param offset field/element offset
2517 * @param delta the value to add
2518 * @return the previous value
2519 * @since 1.8
2520 */
2521 @IntrinsicCandidate
2522 public final long getAndAddLong(Object o, long offset, long delta) {
2523 long v;
2524 do {
2525 v = getLongVolatile(o, offset);
2526 } while (!weakCompareAndSetLong(o, offset, v, v + delta));
2527 return v;
2528 }
2529
2530 @ForceInline
2531 public final long getAndAddLongRelease(Object o, long offset, long delta) {
2532 long v;
2533 do {
2534 v = getLong(o, offset);
2535 } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta));
2536 return v;
2537 }
2538
2539 @ForceInline
2540 public final long getAndAddLongAcquire(Object o, long offset, long delta) {
2541 long v;
2542 do {
2543 v = getLongAcquire(o, offset);
2544 } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta));
2545 return v;
2546 }
2547
2548 @IntrinsicCandidate
2549 public final byte getAndAddByte(Object o, long offset, byte delta) {
2550 byte v;
2551 do {
2552 v = getByteVolatile(o, offset);
2553 } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta)));
2554 return v;
2555 }
2556
2557 @ForceInline
2558 public final byte getAndAddByteRelease(Object o, long offset, byte delta) {
2559 byte v;
2560 do {
2561 v = getByte(o, offset);
2562 } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta)));
2563 return v;
2564 }
2565
2566 @ForceInline
2567 public final byte getAndAddByteAcquire(Object o, long offset, byte delta) {
2568 byte v;
2569 do {
2570 v = getByteAcquire(o, offset);
2571 } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta)));
2572 return v;
2573 }
2574
2575 @IntrinsicCandidate
2576 public final short getAndAddShort(Object o, long offset, short delta) {
2577 short v;
2578 do {
2579 v = getShortVolatile(o, offset);
2580 } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta)));
2581 return v;
2582 }
2583
2584 @ForceInline
2585 public final short getAndAddShortRelease(Object o, long offset, short delta) {
2586 short v;
2587 do {
2588 v = getShort(o, offset);
2589 } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta)));
2590 return v;
2591 }
2592
2593 @ForceInline
2594 public final short getAndAddShortAcquire(Object o, long offset, short delta) {
2595 short v;
2596 do {
2597 v = getShortAcquire(o, offset);
2598 } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta)));
2599 return v;
2600 }
2601
2602 @ForceInline
2603 public final char getAndAddChar(Object o, long offset, char delta) {
2604 return (char) getAndAddShort(o, offset, (short) delta);
2605 }
2606
2607 @ForceInline
2608 public final char getAndAddCharRelease(Object o, long offset, char delta) {
2609 return (char) getAndAddShortRelease(o, offset, (short) delta);
2610 }
2611
2612 @ForceInline
2613 public final char getAndAddCharAcquire(Object o, long offset, char delta) {
2614 return (char) getAndAddShortAcquire(o, offset, (short) delta);
2615 }
2616
2617 @ForceInline
2618 public final float getAndAddFloat(Object o, long offset, float delta) {
2619 int expectedBits;
2620 float v;
2621 do {
2622 // Load and CAS with the raw bits to avoid issues with NaNs and
2623 // possible bit conversion from signaling NaNs to quiet NaNs that
2624 // may result in the loop not terminating.
2625 expectedBits = getIntVolatile(o, offset);
2626 v = Float.intBitsToFloat(expectedBits);
2627 } while (!weakCompareAndSetInt(o, offset,
2628 expectedBits, Float.floatToRawIntBits(v + delta)));
2629 return v;
2630 }
2631
2632 @ForceInline
2633 public final float getAndAddFloatRelease(Object o, long offset, float delta) {
2634 int expectedBits;
2635 float v;
2636 do {
2637 // Load and CAS with the raw bits to avoid issues with NaNs and
2638 // possible bit conversion from signaling NaNs to quiet NaNs that
2639 // may result in the loop not terminating.
2640 expectedBits = getInt(o, offset);
2641 v = Float.intBitsToFloat(expectedBits);
2642 } while (!weakCompareAndSetIntRelease(o, offset,
2643 expectedBits, Float.floatToRawIntBits(v + delta)));
2644 return v;
2645 }
2646
2647 @ForceInline
2648 public final float getAndAddFloatAcquire(Object o, long offset, float delta) {
2649 int expectedBits;
2650 float v;
2651 do {
2652 // Load and CAS with the raw bits to avoid issues with NaNs and
2653 // possible bit conversion from signaling NaNs to quiet NaNs that
2654 // may result in the loop not terminating.
2655 expectedBits = getIntAcquire(o, offset);
2656 v = Float.intBitsToFloat(expectedBits);
2657 } while (!weakCompareAndSetIntAcquire(o, offset,
2658 expectedBits, Float.floatToRawIntBits(v + delta)));
2659 return v;
2660 }
2661
2662 @ForceInline
2663 public final double getAndAddDouble(Object o, long offset, double delta) {
2664 long expectedBits;
2665 double v;
2666 do {
2667 // Load and CAS with the raw bits to avoid issues with NaNs and
2668 // possible bit conversion from signaling NaNs to quiet NaNs that
2669 // may result in the loop not terminating.
2670 expectedBits = getLongVolatile(o, offset);
2671 v = Double.longBitsToDouble(expectedBits);
2672 } while (!weakCompareAndSetLong(o, offset,
2673 expectedBits, Double.doubleToRawLongBits(v + delta)));
2674 return v;
2675 }
2676
2677 @ForceInline
2678 public final double getAndAddDoubleRelease(Object o, long offset, double delta) {
2679 long expectedBits;
2680 double v;
2681 do {
2682 // Load and CAS with the raw bits to avoid issues with NaNs and
2683 // possible bit conversion from signaling NaNs to quiet NaNs that
2684 // may result in the loop not terminating.
2685 expectedBits = getLong(o, offset);
2686 v = Double.longBitsToDouble(expectedBits);
2687 } while (!weakCompareAndSetLongRelease(o, offset,
2688 expectedBits, Double.doubleToRawLongBits(v + delta)));
2689 return v;
2690 }
2691
2692 @ForceInline
2693 public final double getAndAddDoubleAcquire(Object o, long offset, double delta) {
2694 long expectedBits;
2695 double v;
2696 do {
2697 // Load and CAS with the raw bits to avoid issues with NaNs and
2698 // possible bit conversion from signaling NaNs to quiet NaNs that
2699 // may result in the loop not terminating.
2700 expectedBits = getLongAcquire(o, offset);
2701 v = Double.longBitsToDouble(expectedBits);
2702 } while (!weakCompareAndSetLongAcquire(o, offset,
2703 expectedBits, Double.doubleToRawLongBits(v + delta)));
2704 return v;
2705 }
2706
2707 /**
2708 * Atomically exchanges the given value with the current value of
2709 * a field or array element within the given object {@code o}
2710 * at the given {@code offset}.
2711 *
2712 * @param o object/array to update the field/element in
2713 * @param offset field/element offset
2714 * @param newValue new value
2715 * @return the previous value
2716 * @since 1.8
2717 */
2718 @IntrinsicCandidate
2719 public final int getAndSetInt(Object o, long offset, int newValue) {
2720 int v;
2721 do {
2722 v = getIntVolatile(o, offset);
2723 } while (!weakCompareAndSetInt(o, offset, v, newValue));
2724 return v;
2725 }
2726
2727 @ForceInline
2728 public final int getAndSetIntRelease(Object o, long offset, int newValue) {
2729 int v;
2730 do {
2731 v = getInt(o, offset);
2732 } while (!weakCompareAndSetIntRelease(o, offset, v, newValue));
2733 return v;
2734 }
2735
2736 @ForceInline
2737 public final int getAndSetIntAcquire(Object o, long offset, int newValue) {
2738 int v;
2739 do {
2740 v = getIntAcquire(o, offset);
2741 } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue));
2742 return v;
2743 }
2744
2745 /**
2746 * Atomically exchanges the given value with the current value of
2747 * a field or array element within the given object {@code o}
2748 * at the given {@code offset}.
2749 *
2750 * @param o object/array to update the field/element in
2751 * @param offset field/element offset
2752 * @param newValue new value
2753 * @return the previous value
2754 * @since 1.8
2755 */
2756 @IntrinsicCandidate
2757 public final long getAndSetLong(Object o, long offset, long newValue) {
2758 long v;
2759 do {
2760 v = getLongVolatile(o, offset);
2761 } while (!weakCompareAndSetLong(o, offset, v, newValue));
2762 return v;
2763 }
2764
2765 @ForceInline
2766 public final long getAndSetLongRelease(Object o, long offset, long newValue) {
2767 long v;
2768 do {
2769 v = getLong(o, offset);
2770 } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
2771 return v;
2772 }
2773
2774 @ForceInline
2775 public final long getAndSetLongAcquire(Object o, long offset, long newValue) {
2776 long v;
2777 do {
2778 v = getLongAcquire(o, offset);
2779 } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue));
2780 return v;
2781 }
2782
2783 /**
2784 * Atomically exchanges the given reference value with the current
2785 * reference value of a field or array element within the given
2786 * object {@code o} at the given {@code offset}.
2787 *
2788 * @param o object/array to update the field/element in
2789 * @param offset field/element offset
2790 * @param newValue new value
2791 * @return the previous value
2792 * @since 1.8
2793 */
2794 @IntrinsicCandidate
2795 public final Object getAndSetReference(Object o, long offset, Object newValue) {
2796 Object v;
2797 do {
2798 v = getReferenceVolatile(o, offset);
2799 } while (!weakCompareAndSetReference(o, offset, v, newValue));
2800 return v;
2801 }
2802
2803 @ForceInline
2804 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2805 Object v;
2806 do {
2807 v = getReference(o, offset);
2808 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2809 return v;
2810 }
2811
2812 @ForceInline
2813 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2814 Object v;
2815 do {
2816 v = getReferenceAcquire(o, offset);
2817 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2818 return v;
2819 }
2820
2821 @IntrinsicCandidate
2822 public final byte getAndSetByte(Object o, long offset, byte newValue) {
2823 byte v;
2824 do {
2825 v = getByteVolatile(o, offset);
2826 } while (!weakCompareAndSetByte(o, offset, v, newValue));
2827 return v;
2828 }
2829
2830 @ForceInline
2831 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2832 byte v;
2833 do {
2834 v = getByte(o, offset);
2835 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2836 return v;
2837 }
2838
2839 @ForceInline
2840 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
2841 byte v;
2842 do {
2843 v = getByteAcquire(o, offset);
2844 } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue));
2845 return v;
2846 }
2847
2848 @ForceInline
2849 public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) {
2850 return byte2bool(getAndSetByte(o, offset, bool2byte(newValue)));
2851 }
2852
2853 @ForceInline
2854 public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) {
2855 return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue)));
2856 }
2857
2858 @ForceInline
2859 public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) {
2860 return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue)));
2861 }
2862
2863 @IntrinsicCandidate
2864 public final short getAndSetShort(Object o, long offset, short newValue) {
2865 short v;
2866 do {
2867 v = getShortVolatile(o, offset);
2868 } while (!weakCompareAndSetShort(o, offset, v, newValue));
2869 return v;
2870 }
2871
2872 @ForceInline
2873 public final short getAndSetShortRelease(Object o, long offset, short newValue) {
2874 short v;
2875 do {
2876 v = getShort(o, offset);
2877 } while (!weakCompareAndSetShortRelease(o, offset, v, newValue));
2878 return v;
2879 }
2880
2881 @ForceInline
2882 public final short getAndSetShortAcquire(Object o, long offset, short newValue) {
2883 short v;
2884 do {
2885 v = getShortAcquire(o, offset);
2886 } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue));
2887 return v;
2888 }
2889
2890 @ForceInline
2891 public final char getAndSetChar(Object o, long offset, char newValue) {
2892 return s2c(getAndSetShort(o, offset, c2s(newValue)));
2893 }
2894
2895 @ForceInline
2896 public final char getAndSetCharRelease(Object o, long offset, char newValue) {
2897 return s2c(getAndSetShortRelease(o, offset, c2s(newValue)));
2898 }
2899
2900 @ForceInline
2901 public final char getAndSetCharAcquire(Object o, long offset, char newValue) {
2902 return s2c(getAndSetShortAcquire(o, offset, c2s(newValue)));
2903 }
2904
2905 @ForceInline
2906 public final float getAndSetFloat(Object o, long offset, float newValue) {
2907 int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue));
2908 return Float.intBitsToFloat(v);
2909 }
2910
2911 @ForceInline
2912 public final float getAndSetFloatRelease(Object o, long offset, float newValue) {
2913 int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue));
2914 return Float.intBitsToFloat(v);
2915 }
2916
2917 @ForceInline
2918 public final float getAndSetFloatAcquire(Object o, long offset, float newValue) {
2919 int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue));
2920 return Float.intBitsToFloat(v);
2921 }
2922
2923 @ForceInline
2924 public final double getAndSetDouble(Object o, long offset, double newValue) {
2925 long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue));
2926 return Double.longBitsToDouble(v);
2927 }
2928
2929 @ForceInline
2930 public final double getAndSetDoubleRelease(Object o, long offset, double newValue) {
2931 long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue));
2932 return Double.longBitsToDouble(v);
2933 }
2934
2935 @ForceInline
2936 public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) {
2937 long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue));
2938 return Double.longBitsToDouble(v);
2939 }
2940
2941
2942 // The following contain CAS-based Java implementations used on
2943 // platforms not supporting native instructions
2944
2945 @ForceInline
2946 public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) {
2947 return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask)));
2948 }
2949
2950 @ForceInline
2951 public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) {
2952 return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask)));
2953 }
2954
2955 @ForceInline
2956 public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) {
2957 return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask)));
2958 }
2959
2960 @ForceInline
2961 public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) {
2962 return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask)));
2963 }
2964
2965 @ForceInline
2966 public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) {
2967 return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask)));
2968 }
2969
2970 @ForceInline
2971 public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) {
2972 return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask)));
2973 }
2974
2975 @ForceInline
2976 public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) {
2977 return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask)));
2978 }
2979
2980 @ForceInline
2981 public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) {
2982 return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask)));
2983 }
2984
2985 @ForceInline
2986 public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) {
2987 return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask)));
2988 }
2989
2990
2991 @ForceInline
2992 public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) {
2993 byte current;
2994 do {
2995 current = getByteVolatile(o, offset);
2996 } while (!weakCompareAndSetByte(o, offset,
2997 current, (byte) (current | mask)));
2998 return current;
2999 }
3000
3001 @ForceInline
3002 public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) {
3003 byte current;
3004 do {
3005 current = getByte(o, offset);
3006 } while (!weakCompareAndSetByteRelease(o, offset,
3007 current, (byte) (current | mask)));
3008 return current;
3009 }
3010
3011 @ForceInline
3012 public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) {
3013 byte current;
3014 do {
3015 // Plain read, the value is a hint, the acquire CAS does the work
3016 current = getByte(o, offset);
3017 } while (!weakCompareAndSetByteAcquire(o, offset,
3018 current, (byte) (current | mask)));
3019 return current;
3020 }
3021
3022 @ForceInline
3023 public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) {
3024 byte current;
3025 do {
3026 current = getByteVolatile(o, offset);
3027 } while (!weakCompareAndSetByte(o, offset,
3028 current, (byte) (current & mask)));
3029 return current;
3030 }
3031
3032 @ForceInline
3033 public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) {
3034 byte current;
3035 do {
3036 current = getByte(o, offset);
3037 } while (!weakCompareAndSetByteRelease(o, offset,
3038 current, (byte) (current & mask)));
3039 return current;
3040 }
3041
3042 @ForceInline
3043 public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) {
3044 byte current;
3045 do {
3046 // Plain read, the value is a hint, the acquire CAS does the work
3047 current = getByte(o, offset);
3048 } while (!weakCompareAndSetByteAcquire(o, offset,
3049 current, (byte) (current & mask)));
3050 return current;
3051 }
3052
3053 @ForceInline
3054 public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) {
3055 byte current;
3056 do {
3057 current = getByteVolatile(o, offset);
3058 } while (!weakCompareAndSetByte(o, offset,
3059 current, (byte) (current ^ mask)));
3060 return current;
3061 }
3062
3063 @ForceInline
3064 public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) {
3065 byte current;
3066 do {
3067 current = getByte(o, offset);
3068 } while (!weakCompareAndSetByteRelease(o, offset,
3069 current, (byte) (current ^ mask)));
3070 return current;
3071 }
3072
3073 @ForceInline
3074 public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) {
3075 byte current;
3076 do {
3077 // Plain read, the value is a hint, the acquire CAS does the work
3078 current = getByte(o, offset);
3079 } while (!weakCompareAndSetByteAcquire(o, offset,
3080 current, (byte) (current ^ mask)));
3081 return current;
3082 }
3083
3084
3085 @ForceInline
3086 public final char getAndBitwiseOrChar(Object o, long offset, char mask) {
3087 return s2c(getAndBitwiseOrShort(o, offset, c2s(mask)));
3088 }
3089
3090 @ForceInline
3091 public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) {
3092 return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask)));
3093 }
3094
3095 @ForceInline
3096 public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) {
3097 return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask)));
3098 }
3099
3100 @ForceInline
3101 public final char getAndBitwiseAndChar(Object o, long offset, char mask) {
3102 return s2c(getAndBitwiseAndShort(o, offset, c2s(mask)));
3103 }
3104
3105 @ForceInline
3106 public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) {
3107 return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask)));
3108 }
3109
3110 @ForceInline
3111 public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) {
3112 return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask)));
3113 }
3114
3115 @ForceInline
3116 public final char getAndBitwiseXorChar(Object o, long offset, char mask) {
3117 return s2c(getAndBitwiseXorShort(o, offset, c2s(mask)));
3118 }
3119
3120 @ForceInline
3121 public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) {
3122 return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask)));
3123 }
3124
3125 @ForceInline
3126 public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) {
3127 return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask)));
3128 }
3129
3130
3131 @ForceInline
3132 public final short getAndBitwiseOrShort(Object o, long offset, short mask) {
3133 short current;
3134 do {
3135 current = getShortVolatile(o, offset);
3136 } while (!weakCompareAndSetShort(o, offset,
3137 current, (short) (current | mask)));
3138 return current;
3139 }
3140
3141 @ForceInline
3142 public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) {
3143 short current;
3144 do {
3145 current = getShort(o, offset);
3146 } while (!weakCompareAndSetShortRelease(o, offset,
3147 current, (short) (current | mask)));
3148 return current;
3149 }
3150
3151 @ForceInline
3152 public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) {
3153 short current;
3154 do {
3155 // Plain read, the value is a hint, the acquire CAS does the work
3156 current = getShort(o, offset);
3157 } while (!weakCompareAndSetShortAcquire(o, offset,
3158 current, (short) (current | mask)));
3159 return current;
3160 }
3161
3162 @ForceInline
3163 public final short getAndBitwiseAndShort(Object o, long offset, short mask) {
3164 short current;
3165 do {
3166 current = getShortVolatile(o, offset);
3167 } while (!weakCompareAndSetShort(o, offset,
3168 current, (short) (current & mask)));
3169 return current;
3170 }
3171
3172 @ForceInline
3173 public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) {
3174 short current;
3175 do {
3176 current = getShort(o, offset);
3177 } while (!weakCompareAndSetShortRelease(o, offset,
3178 current, (short) (current & mask)));
3179 return current;
3180 }
3181
3182 @ForceInline
3183 public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) {
3184 short current;
3185 do {
3186 // Plain read, the value is a hint, the acquire CAS does the work
3187 current = getShort(o, offset);
3188 } while (!weakCompareAndSetShortAcquire(o, offset,
3189 current, (short) (current & mask)));
3190 return current;
3191 }
3192
3193 @ForceInline
3194 public final short getAndBitwiseXorShort(Object o, long offset, short mask) {
3195 short current;
3196 do {
3197 current = getShortVolatile(o, offset);
3198 } while (!weakCompareAndSetShort(o, offset,
3199 current, (short) (current ^ mask)));
3200 return current;
3201 }
3202
3203 @ForceInline
3204 public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) {
3205 short current;
3206 do {
3207 current = getShort(o, offset);
3208 } while (!weakCompareAndSetShortRelease(o, offset,
3209 current, (short) (current ^ mask)));
3210 return current;
3211 }
3212
3213 @ForceInline
3214 public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) {
3215 short current;
3216 do {
3217 // Plain read, the value is a hint, the acquire CAS does the work
3218 current = getShort(o, offset);
3219 } while (!weakCompareAndSetShortAcquire(o, offset,
3220 current, (short) (current ^ mask)));
3221 return current;
3222 }
3223
3224
3225 @ForceInline
3226 public final int getAndBitwiseOrInt(Object o, long offset, int mask) {
3227 int current;
3228 do {
3229 current = getIntVolatile(o, offset);
3230 } while (!weakCompareAndSetInt(o, offset,
3231 current, current | mask));
3232 return current;
3233 }
3234
3235 @ForceInline
3236 public final int getAndBitwiseOrIntRelease(Object o, long offset, int mask) {
3237 int current;
3238 do {
3239 current = getInt(o, offset);
3240 } while (!weakCompareAndSetIntRelease(o, offset,
3241 current, current | mask));
3242 return current;
3243 }
3244
3245 @ForceInline
3246 public final int getAndBitwiseOrIntAcquire(Object o, long offset, int mask) {
3247 int current;
3248 do {
3249 // Plain read, the value is a hint, the acquire CAS does the work
3250 current = getInt(o, offset);
3251 } while (!weakCompareAndSetIntAcquire(o, offset,
3252 current, current | mask));
3253 return current;
3254 }
3255
3256 /**
3257 * Atomically replaces the current value of a field or array element within
3258 * the given object with the result of bitwise AND between the current value
3259 * and mask.
3260 *
3261 * @param o object/array to update the field/element in
3262 * @param offset field/element offset
3263 * @param mask the mask value
3264 * @return the previous value
3265 * @since 9
3266 */
3267 @ForceInline
3268 public final int getAndBitwiseAndInt(Object o, long offset, int mask) {
3269 int current;
3270 do {
3271 current = getIntVolatile(o, offset);
3272 } while (!weakCompareAndSetInt(o, offset,
3273 current, current & mask));
3274 return current;
3275 }
3276
3277 @ForceInline
3278 public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) {
3279 int current;
3280 do {
3281 current = getInt(o, offset);
3282 } while (!weakCompareAndSetIntRelease(o, offset,
3283 current, current & mask));
3284 return current;
3285 }
3286
3287 @ForceInline
3288 public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) {
3289 int current;
3290 do {
3291 // Plain read, the value is a hint, the acquire CAS does the work
3292 current = getInt(o, offset);
3293 } while (!weakCompareAndSetIntAcquire(o, offset,
3294 current, current & mask));
3295 return current;
3296 }
3297
3298 @ForceInline
3299 public final int getAndBitwiseXorInt(Object o, long offset, int mask) {
3300 int current;
3301 do {
3302 current = getIntVolatile(o, offset);
3303 } while (!weakCompareAndSetInt(o, offset,
3304 current, current ^ mask));
3305 return current;
3306 }
3307
3308 @ForceInline
3309 public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) {
3310 int current;
3311 do {
3312 current = getInt(o, offset);
3313 } while (!weakCompareAndSetIntRelease(o, offset,
3314 current, current ^ mask));
3315 return current;
3316 }
3317
3318 @ForceInline
3319 public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) {
3320 int current;
3321 do {
3322 // Plain read, the value is a hint, the acquire CAS does the work
3323 current = getInt(o, offset);
3324 } while (!weakCompareAndSetIntAcquire(o, offset,
3325 current, current ^ mask));
3326 return current;
3327 }
3328
3329
3330 @ForceInline
3331 public final long getAndBitwiseOrLong(Object o, long offset, long mask) {
3332 long current;
3333 do {
3334 current = getLongVolatile(o, offset);
3335 } while (!weakCompareAndSetLong(o, offset,
3336 current, current | mask));
3337 return current;
3338 }
3339
3340 @ForceInline
3341 public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) {
3342 long current;
3343 do {
3344 current = getLong(o, offset);
3345 } while (!weakCompareAndSetLongRelease(o, offset,
3346 current, current | mask));
3347 return current;
3348 }
3349
3350 @ForceInline
3351 public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) {
3352 long current;
3353 do {
3354 // Plain read, the value is a hint, the acquire CAS does the work
3355 current = getLong(o, offset);
3356 } while (!weakCompareAndSetLongAcquire(o, offset,
3357 current, current | mask));
3358 return current;
3359 }
3360
3361 @ForceInline
3362 public final long getAndBitwiseAndLong(Object o, long offset, long mask) {
3363 long current;
3364 do {
3365 current = getLongVolatile(o, offset);
3366 } while (!weakCompareAndSetLong(o, offset,
3367 current, current & mask));
3368 return current;
3369 }
3370
3371 @ForceInline
3372 public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) {
3373 long current;
3374 do {
3375 current = getLong(o, offset);
3376 } while (!weakCompareAndSetLongRelease(o, offset,
3377 current, current & mask));
3378 return current;
3379 }
3380
3381 @ForceInline
3382 public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) {
3383 long current;
3384 do {
3385 // Plain read, the value is a hint, the acquire CAS does the work
3386 current = getLong(o, offset);
3387 } while (!weakCompareAndSetLongAcquire(o, offset,
3388 current, current & mask));
3389 return current;
3390 }
3391
3392 @ForceInline
3393 public final long getAndBitwiseXorLong(Object o, long offset, long mask) {
3394 long current;
3395 do {
3396 current = getLongVolatile(o, offset);
3397 } while (!weakCompareAndSetLong(o, offset,
3398 current, current ^ mask));
3399 return current;
3400 }
3401
3402 @ForceInline
3403 public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) {
3404 long current;
3405 do {
3406 current = getLong(o, offset);
3407 } while (!weakCompareAndSetLongRelease(o, offset,
3408 current, current ^ mask));
3409 return current;
3410 }
3411
3412 @ForceInline
3413 public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) {
3414 long current;
3415 do {
3416 // Plain read, the value is a hint, the acquire CAS does the work
3417 current = getLong(o, offset);
3418 } while (!weakCompareAndSetLongAcquire(o, offset,
3419 current, current ^ mask));
3420 return current;
3421 }
3422
3423
3424
3425 /**
3426 * Ensures that loads before the fence will not be reordered with loads and
3427 * stores after the fence; a "LoadLoad plus LoadStore barrier".
3428 *
3429 * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
3430 * (an "acquire fence").
3431 *
3432 * Provides a LoadLoad barrier followed by a LoadStore barrier.
3433 *
3434 * @since 1.8
3435 */
3436 @IntrinsicCandidate
3437 public final void loadFence() {
3438 // If loadFence intrinsic is not available, fall back to full fence.
3439 fullFence();
3440 }
3441
3442 /**
3443 * Ensures that loads and stores before the fence will not be reordered with
3444 * stores after the fence; a "StoreStore plus LoadStore barrier".
3445 *
3446 * Corresponds to C11 atomic_thread_fence(memory_order_release)
3447 * (a "release fence").
3448 *
3449 * Provides a StoreStore barrier followed by a LoadStore barrier.
3450 *
3451 * @since 1.8
3452 */
3453 @IntrinsicCandidate
3454 public final void storeFence() {
3455 // If storeFence intrinsic is not available, fall back to full fence.
3456 fullFence();
3457 }
3458
3459 /**
3460 * Ensures that loads and stores before the fence will not be reordered
3461 * with loads and stores after the fence. Implies the effects of both
3462 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
3463 * barrier.
3464 *
3465 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
3466 * @since 1.8
3467 */
3468 @IntrinsicCandidate
3469 public native void fullFence();
3470
3471 /**
3472 * Ensures that loads before the fence will not be reordered with
3473 * loads after the fence.
3474 *
3475 * @implNote
3476 * This method is operationally equivalent to {@link #loadFence()}.
3477 *
3478 * @since 9
3479 */
3480 public final void loadLoadFence() {
3481 loadFence();
3482 }
3483
3484 /**
3485 * Ensures that stores before the fence will not be reordered with
3486 * stores after the fence.
3487 *
3488 * @since 9
3489 */
3490 @IntrinsicCandidate
3491 public final void storeStoreFence() {
3492 // If storeStoreFence intrinsic is not available, fall back to storeFence.
3493 storeFence();
3494 }
3495
3496 /**
3497 * Throws IllegalAccessError; for use by the VM for access control
3498 * error support.
3499 * @since 1.8
3500 */
3501 private static void throwIllegalAccessError() {
3502 throw new IllegalAccessError();
3503 }
3504
3505 /**
3506 * Throws NoSuchMethodError; for use by the VM for redefinition support.
3507 * @since 13
3508 */
3509 private static void throwNoSuchMethodError() {
3510 throw new NoSuchMethodError();
3511 }
3512
3513 /**
3514 * @return Returns true if the native byte ordering of this
3515 * platform is big-endian, false if it is little-endian.
3516 */
3517 public final boolean isBigEndian() { return BIG_ENDIAN; }
3518
3519 /**
3520 * @return Returns true if this platform is capable of performing
3521 * accesses at addresses which are not aligned for the type of the
3522 * primitive type being accessed, false otherwise.
3523 */
3524 public final boolean unalignedAccess() { return UNALIGNED_ACCESS; }
3525
3526 /**
3527 * Fetches a value at some byte offset into a given Java object.
3528 * More specifically, fetches a value within the given object
3529 * <code>o</code> at the given offset, or (if <code>o</code> is
3530 * null) from the memory address whose numerical value is the
3531 * given offset. <p>
3532 *
3533 * The specification of this method is the same as {@link
3534 * #getLong(Object, long)} except that the offset does not need to
3535 * have been obtained from {@link #objectFieldOffset} on the
3536 * {@link java.lang.reflect.Field} of some Java field. The value
3537 * in memory is raw data, and need not correspond to any Java
3538 * variable. Unless <code>o</code> is null, the value accessed
3539 * must be entirely within the allocated object. The endianness
3540 * of the value in memory is the endianness of the native platform.
3541 *
3542 * <p> The read will be atomic with respect to the largest power
3543 * of two that divides the GCD of the offset and the storage size.
3544 * For example, getLongUnaligned will make atomic reads of 2-, 4-,
3545 * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3546 * respectively. There are no other guarantees of atomicity.
3547 * <p>
3548 * 8-byte atomicity is only guaranteed on platforms on which
3549 * support atomic accesses to longs.
3550 *
3551 * @param o Java heap object in which the value resides, if any, else
3552 * null
3553 * @param offset The offset in bytes from the start of the object
3554 * @return the value fetched from the indicated object
3555 * @throws RuntimeException No defined exceptions are thrown, not even
3556 * {@link NullPointerException}
3557 * @since 9
3558 */
3559 @IntrinsicCandidate
3560 public final long getLongUnaligned(Object o, long offset) {
3561 if ((offset & 7) == 0) {
3562 return getLong(o, offset);
3563 } else if ((offset & 3) == 0) {
3564 return makeLong(getInt(o, offset),
3565 getInt(o, offset + 4));
3566 } else if ((offset & 1) == 0) {
3567 return makeLong(getShort(o, offset),
3568 getShort(o, offset + 2),
3569 getShort(o, offset + 4),
3570 getShort(o, offset + 6));
3571 } else {
3572 return makeLong(getByte(o, offset),
3573 getByte(o, offset + 1),
3574 getByte(o, offset + 2),
3575 getByte(o, offset + 3),
3576 getByte(o, offset + 4),
3577 getByte(o, offset + 5),
3578 getByte(o, offset + 6),
3579 getByte(o, offset + 7));
3580 }
3581 }
3582 /**
3583 * As {@link #getLongUnaligned(Object, long)} but with an
3584 * additional argument which specifies the endianness of the value
3585 * as stored in memory.
3586 *
3587 * @param o Java heap object in which the variable resides
3588 * @param offset The offset in bytes from the start of the object
3589 * @param bigEndian The endianness of the value
3590 * @return the value fetched from the indicated object
3591 * @since 9
3592 */
3593 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
3594 return convEndian(bigEndian, getLongUnaligned(o, offset));
3595 }
3596
3597 /** @see #getLongUnaligned(Object, long) */
3598 @IntrinsicCandidate
3599 public final int getIntUnaligned(Object o, long offset) {
3600 if ((offset & 3) == 0) {
3601 return getInt(o, offset);
3602 } else if ((offset & 1) == 0) {
3603 return makeInt(getShort(o, offset),
3604 getShort(o, offset + 2));
3605 } else {
3606 return makeInt(getByte(o, offset),
3607 getByte(o, offset + 1),
3608 getByte(o, offset + 2),
3609 getByte(o, offset + 3));
3610 }
3611 }
3612 /** @see #getLongUnaligned(Object, long, boolean) */
3613 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
3614 return convEndian(bigEndian, getIntUnaligned(o, offset));
3615 }
3616
3617 /** @see #getLongUnaligned(Object, long) */
3618 @IntrinsicCandidate
3619 public final short getShortUnaligned(Object o, long offset) {
3620 if ((offset & 1) == 0) {
3621 return getShort(o, offset);
3622 } else {
3623 return makeShort(getByte(o, offset),
3624 getByte(o, offset + 1));
3625 }
3626 }
3627 /** @see #getLongUnaligned(Object, long, boolean) */
3628 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
3629 return convEndian(bigEndian, getShortUnaligned(o, offset));
3630 }
3631
3632 /** @see #getLongUnaligned(Object, long) */
3633 @IntrinsicCandidate
3634 public final char getCharUnaligned(Object o, long offset) {
3635 if ((offset & 1) == 0) {
3636 return getChar(o, offset);
3637 } else {
3638 return (char)makeShort(getByte(o, offset),
3639 getByte(o, offset + 1));
3640 }
3641 }
3642
3643 /** @see #getLongUnaligned(Object, long, boolean) */
3644 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
3645 return convEndian(bigEndian, getCharUnaligned(o, offset));
3646 }
3647
3648 /**
3649 * Stores a value at some byte offset into a given Java object.
3650 * <p>
3651 * The specification of this method is the same as {@link
3652 * #getLong(Object, long)} except that the offset does not need to
3653 * have been obtained from {@link #objectFieldOffset} on the
3654 * {@link java.lang.reflect.Field} of some Java field. The value
3655 * in memory is raw data, and need not correspond to any Java
3656 * variable. The endianness of the value in memory is the
3657 * endianness of the native platform.
3658 * <p>
3659 * The write will be atomic with respect to the largest power of
3660 * two that divides the GCD of the offset and the storage size.
3661 * For example, putLongUnaligned will make atomic writes of 2-, 4-,
3662 * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3663 * respectively. There are no other guarantees of atomicity.
3664 * <p>
3665 * 8-byte atomicity is only guaranteed on platforms on which
3666 * support atomic accesses to longs.
3667 *
3668 * @param o Java heap object in which the value resides, if any, else
3669 * null
3670 * @param offset The offset in bytes from the start of the object
3671 * @param x the value to store
3672 * @throws RuntimeException No defined exceptions are thrown, not even
3673 * {@link NullPointerException}
3674 * @since 9
3675 */
3676 @IntrinsicCandidate
3677 public final void putLongUnaligned(Object o, long offset, long x) {
3678 if ((offset & 7) == 0) {
3679 putLong(o, offset, x);
3680 } else if ((offset & 3) == 0) {
3681 putLongParts(o, offset,
3682 (int)(x >> 0),
3683 (int)(x >>> 32));
3684 } else if ((offset & 1) == 0) {
3685 putLongParts(o, offset,
3686 (short)(x >>> 0),
3687 (short)(x >>> 16),
3688 (short)(x >>> 32),
3689 (short)(x >>> 48));
3690 } else {
3691 putLongParts(o, offset,
3692 (byte)(x >>> 0),
3693 (byte)(x >>> 8),
3694 (byte)(x >>> 16),
3695 (byte)(x >>> 24),
3696 (byte)(x >>> 32),
3697 (byte)(x >>> 40),
3698 (byte)(x >>> 48),
3699 (byte)(x >>> 56));
3700 }
3701 }
3702
3703 /**
3704 * As {@link #putLongUnaligned(Object, long, long)} but with an additional
3705 * argument which specifies the endianness of the value as stored in memory.
3706 * @param o Java heap object in which the value resides
3707 * @param offset The offset in bytes from the start of the object
3708 * @param x the value to store
3709 * @param bigEndian The endianness of the value
3710 * @throws RuntimeException No defined exceptions are thrown, not even
3711 * {@link NullPointerException}
3712 * @since 9
3713 */
3714 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
3715 putLongUnaligned(o, offset, convEndian(bigEndian, x));
3716 }
3717
3718 /** @see #putLongUnaligned(Object, long, long) */
3719 @IntrinsicCandidate
3720 public final void putIntUnaligned(Object o, long offset, int x) {
3721 if ((offset & 3) == 0) {
3722 putInt(o, offset, x);
3723 } else if ((offset & 1) == 0) {
3724 putIntParts(o, offset,
3725 (short)(x >> 0),
3726 (short)(x >>> 16));
3727 } else {
3728 putIntParts(o, offset,
3729 (byte)(x >>> 0),
3730 (byte)(x >>> 8),
3731 (byte)(x >>> 16),
3732 (byte)(x >>> 24));
3733 }
3734 }
3735 /** @see #putLongUnaligned(Object, long, long, boolean) */
3736 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
3737 putIntUnaligned(o, offset, convEndian(bigEndian, x));
3738 }
3739
3740 /** @see #putLongUnaligned(Object, long, long) */
3741 @IntrinsicCandidate
3742 public final void putShortUnaligned(Object o, long offset, short x) {
3743 if ((offset & 1) == 0) {
3744 putShort(o, offset, x);
3745 } else {
3746 putShortParts(o, offset,
3747 (byte)(x >>> 0),
3748 (byte)(x >>> 8));
3749 }
3750 }
3751 /** @see #putLongUnaligned(Object, long, long, boolean) */
3752 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
3753 putShortUnaligned(o, offset, convEndian(bigEndian, x));
3754 }
3755
3756 /** @see #putLongUnaligned(Object, long, long) */
3757 @IntrinsicCandidate
3758 public final void putCharUnaligned(Object o, long offset, char x) {
3759 putShortUnaligned(o, offset, (short)x);
3760 }
3761 /** @see #putLongUnaligned(Object, long, long, boolean) */
3762 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
3763 putCharUnaligned(o, offset, convEndian(bigEndian, x));
3764 }
3765
3766 private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; }
3767
3768 // These methods construct integers from bytes. The byte ordering
3769 // is the native endianness of this platform.
3770 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
3771 return ((toUnsignedLong(i0) << pickPos(56, 0))
3772 | (toUnsignedLong(i1) << pickPos(56, 8))
3773 | (toUnsignedLong(i2) << pickPos(56, 16))
3774 | (toUnsignedLong(i3) << pickPos(56, 24))
3775 | (toUnsignedLong(i4) << pickPos(56, 32))
3776 | (toUnsignedLong(i5) << pickPos(56, 40))
3777 | (toUnsignedLong(i6) << pickPos(56, 48))
3778 | (toUnsignedLong(i7) << pickPos(56, 56)));
3779 }
3780 private static long makeLong(short i0, short i1, short i2, short i3) {
3781 return ((toUnsignedLong(i0) << pickPos(48, 0))
3782 | (toUnsignedLong(i1) << pickPos(48, 16))
3783 | (toUnsignedLong(i2) << pickPos(48, 32))
3784 | (toUnsignedLong(i3) << pickPos(48, 48)));
3785 }
3786 private static long makeLong(int i0, int i1) {
3787 return (toUnsignedLong(i0) << pickPos(32, 0))
3788 | (toUnsignedLong(i1) << pickPos(32, 32));
3789 }
3790 private static int makeInt(short i0, short i1) {
3791 return (toUnsignedInt(i0) << pickPos(16, 0))
3792 | (toUnsignedInt(i1) << pickPos(16, 16));
3793 }
3794 private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
3795 return ((toUnsignedInt(i0) << pickPos(24, 0))
3796 | (toUnsignedInt(i1) << pickPos(24, 8))
3797 | (toUnsignedInt(i2) << pickPos(24, 16))
3798 | (toUnsignedInt(i3) << pickPos(24, 24)));
3799 }
3800 private static short makeShort(byte i0, byte i1) {
3801 return (short)((toUnsignedInt(i0) << pickPos(8, 0))
3802 | (toUnsignedInt(i1) << pickPos(8, 8)));
3803 }
3804
3805 private static byte pick(byte le, byte be) { return BIG_ENDIAN ? be : le; }
3806 private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; }
3807 private static int pick(int le, int be) { return BIG_ENDIAN ? be : le; }
3808
3809 // These methods write integers to memory from smaller parts
3810 // provided by their caller. The ordering in which these parts
3811 // are written is the native endianness of this platform.
3812 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
3813 putByte(o, offset + 0, pick(i0, i7));
3814 putByte(o, offset + 1, pick(i1, i6));
3815 putByte(o, offset + 2, pick(i2, i5));
3816 putByte(o, offset + 3, pick(i3, i4));
3817 putByte(o, offset + 4, pick(i4, i3));
3818 putByte(o, offset + 5, pick(i5, i2));
3819 putByte(o, offset + 6, pick(i6, i1));
3820 putByte(o, offset + 7, pick(i7, i0));
3821 }
3822 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
3823 putShort(o, offset + 0, pick(i0, i3));
3824 putShort(o, offset + 2, pick(i1, i2));
3825 putShort(o, offset + 4, pick(i2, i1));
3826 putShort(o, offset + 6, pick(i3, i0));
3827 }
3828 private void putLongParts(Object o, long offset, int i0, int i1) {
3829 putInt(o, offset + 0, pick(i0, i1));
3830 putInt(o, offset + 4, pick(i1, i0));
3831 }
3832 private void putIntParts(Object o, long offset, short i0, short i1) {
3833 putShort(o, offset + 0, pick(i0, i1));
3834 putShort(o, offset + 2, pick(i1, i0));
3835 }
3836 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
3837 putByte(o, offset + 0, pick(i0, i3));
3838 putByte(o, offset + 1, pick(i1, i2));
3839 putByte(o, offset + 2, pick(i2, i1));
3840 putByte(o, offset + 3, pick(i3, i0));
3841 }
3842 private void putShortParts(Object o, long offset, byte i0, byte i1) {
3843 putByte(o, offset + 0, pick(i0, i1));
3844 putByte(o, offset + 1, pick(i1, i0));
3845 }
3846
3847 // Zero-extend an integer
3848 private static int toUnsignedInt(byte n) { return n & 0xff; }
3849 private static int toUnsignedInt(short n) { return n & 0xffff; }
3850 private static long toUnsignedLong(byte n) { return n & 0xffl; }
3851 private static long toUnsignedLong(short n) { return n & 0xffffl; }
3852 private static long toUnsignedLong(int n) { return n & 0xffffffffl; }
3853
3854 // Maybe byte-reverse an integer
3855 private static char convEndian(boolean big, char n) { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); }
3856 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
3857 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
3858 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
3859
3860
3861
3862 private native long allocateMemory0(long bytes);
3863 private native long reallocateMemory0(long address, long bytes);
3864 private native void freeMemory0(long address);
3865 @IntrinsicCandidate
3866 private native void setMemory0(Object o, long offset, long bytes, byte value);
3867 @IntrinsicCandidate
3868 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3869 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3870 private native long objectFieldOffset0(Field f); // throws IAE
3871 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
3872 private native long staticFieldOffset0(Field f); // throws IAE
3873 private native Object staticFieldBase0(Field f); // throws IAE
3874 private native boolean shouldBeInitialized0(Class<?> c);
3875 private native void ensureClassInitialized0(Class<?> c);
3876 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
3877 private native int arrayIndexScale0(Class<?> arrayClass);
3878 private native int getLoadAverage0(double[] loadavg, int nelems);
3879
3880
3881 /**
3882 * Invokes the given direct byte buffer's cleaner, if any.
3883 *
3884 * @param directBuffer a direct byte buffer
3885 * @throws NullPointerException if {@code directBuffer} is null
3886 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3887 * or is a {@link java.nio.Buffer#slice slice}, or is a
3888 * {@link java.nio.Buffer#duplicate duplicate}
3889 */
3890 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3891 if (!directBuffer.isDirect())
3892 throw new IllegalArgumentException("buffer is non-direct");
3893
3894 DirectBuffer db = (DirectBuffer) directBuffer;
3895 if (db.attachment() != null)
3896 throw new IllegalArgumentException("duplicate or slice");
3897
3898 Cleaner cleaner = db.cleaner();
3899 if (cleaner != null) {
3900 cleaner.clean();
3901 }
3902 }
3903 }