1 /*
2 * Copyright (c) 2000, 2026, 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 jdk.internal.value.ValueClass;
33 import sun.nio.Cleaner;
34 import sun.nio.ch.DirectBuffer;
35
36 import java.lang.reflect.Field;
37 import java.security.ProtectionDomain;
38
39 import static jdk.internal.misc.UnsafeConstants.*;
40
41 /**
42 * A collection of methods for performing low-level, unsafe operations.
43 * Although the class and all methods are public, use of this class is
44 * limited because only trusted code can obtain instances of it.
45 *
46 * <h2><a id="undefined-behavior">Undefined Behavior</a></h2>
47 * For performance reasons, {@code Unsafe} is allowed to work outside the
48 * restrictions enforced by the JVM. As a result, it is the responsibility of
49 * the caller to ensure that an invocation of an {@code Unsafe} method is
50 * conformant, and failure to do so will result in undefined behavior. The
51 * runtime and the JIT compiler may assume that undefined behavior never
52 * happens, and operate accordingly. For example, the runtime assumes that each
53 * object has a header with a particular layout, and if the users use
54 * {@code Unsafe} to overwrite this header with invalid data, the behavior of
55 * the runtime becomes unpredictable. Another example is that the JIT compiler
56 * may assume that accesses on separate objects are unrelated, and schedule
57 * each of them without taking into consideration the others. If there is an
58 * {@code Unsafe} access that is out of bounds and points to object different
59 * from the declared base, the program may execute in a way that a variable
60 * seems to have multiple values at the same time. As a result, when a program
61 * exhibits undefined behavior, there is no restrictions on its behaviors. Such
62 * behaviors may include but not be limited to:
63 *
64 * <ul>
65 * <li>Working as expected.
66 * <li>Crashing the VM.
67 * <li>Corruption of the heap or JVM memory.
68 * <li>Nonsensical variable value. E.g. an {@code int} may appear to be
69 * simultaneously 0 and 1.
70 * <li>Impossible code execution. E.g. the branches of an {@code if} are
71 * both executed or both not executed.
72 * <li>Wiping out the hard drive.
73 * </ul>
74 *
75 * Undefined behavior, as described in this class, is analogous to the
76 * terminology with the same name in the C++ language.
77 * <p>
78 * Some methods (e.g. {@link #getInt}) exhibit undefined behavior if they
79 * are invoked at runtime with illegal arguments. This means that they will
80 * never exhibit undefined behavior if they are not actually reachable at
81 * runtime. On the other hands, other methods (e.g.
82 * {@link #allocateInstance(Class)}) exhibit undefined behavior if they are
83 * used incorrectly, even if the invocation may not be reachable at runtime.
84 * The analogous terminology in C++ is that such programs are ill-formed.
85 * <p>
86 * For methods exhibiting undefined behavior if they are invoked at runtime
87 * with illegal arguments, undefined behavior may time travel. That is, if a
88 * control path may eventually reach an invocation of an {@code Unsafe} method
89 * with illegal arguments, the symptoms of undefined behavior may be present
90 * even before the invocation of the {@code Unsafe} method. This is because the
91 * JIT compiler may have certain assumptions about the inputs of an
92 * {@code Unsafe} invocation, these assumptions may propagate backward to
93 * previous statements, leading to wrong executions if the assumptions are
94 * invalid.
95 *
96 * @author John R. Rose
97 * @see #getUnsafe
98 */
99 @AOTSafeClassInitializer
100 public final class Unsafe {
101
102 private static native void registerNatives();
103 static {
104 runtimeSetup();
105 }
106
107 /// BASE_OFFSET, INDEX_SCALE, and ADDRESS_SIZE fields are equivalent if the
108 /// AOT initialized heap is reused, so just register natives
109 @AOTRuntimeSetup
110 private static void runtimeSetup() {
111 registerNatives();
112 }
113
114 private Unsafe() {}
115
116 private static final Unsafe theUnsafe = new Unsafe();
117
118 /**
119 * Provides the caller with the capability of performing unsafe
120 * operations.
121 *
122 * <p>The returned {@code Unsafe} object should be carefully guarded
123 * by the caller, since it can be used to read and write data at arbitrary
124 * memory addresses. It must never be passed to untrusted code.
125 *
126 * <p>Most methods in this class are very low-level, and correspond to a
127 * small number of hardware instructions (on typical machines). Compilers
128 * are encouraged to optimize these methods accordingly.
129 *
130 * <p>Here is a suggested idiom for using unsafe operations:
131 *
132 * <pre> {@code
133 * class MyTrustedClass {
134 * private static final Unsafe unsafe = Unsafe.getUnsafe();
135 * ...
136 * private long myCountAddress = ...;
137 * public int getCount() { return unsafe.getByte(myCountAddress); }
138 * }}</pre>
139 *
140 * (It may assist compilers to make the local variable {@code final}.)
141 */
142 public static Unsafe getUnsafe() {
143 return theUnsafe;
144 }
145
146 //--- peek and poke operations
147 // (compilers should optimize these to memory ops)
148
149 // These work on object fields in the Java heap.
150 // They will not work on elements of packed arrays.
151
152 /**
153 * Fetches a value from a given Java variable.
154 * More specifically, fetches a field or array element within the given
155 * object {@code o} at the given offset, or (if {@code o} is null)
156 * from the memory address whose numerical value is the given offset.
157 * <p>
158 * The results are undefined unless one of the following cases is true:
159 * <ul>
160 * <li>The offset was obtained from {@link #objectFieldOffset} on
161 * the {@link java.lang.reflect.Field} of some Java field and the object
162 * referred to by {@code o} is of a class compatible with that
163 * field's class.
164 *
165 * <li>The offset and object reference {@code o} (either null or
166 * non-null) were both obtained via {@link #staticFieldOffset}
167 * and {@link #staticFieldBase} (respectively) from the
168 * reflective {@link Field} representation of some Java field.
169 *
170 * <li>The object referred to by {@code o} is an array, and the offset
171 * is an integer of the form {@code B+N*S}, where {@code N} is
172 * a valid index into the array, and {@code B} and {@code S} are
173 * the values obtained by {@link #arrayBaseOffset} and {@link
174 * #arrayIndexScale} (respectively) from the array's class. The value
175 * referred to is the {@code N}<em>th</em> element of the array.
176 *
177 * </ul>
178 * <p>
179 * If one of the above cases is true, the call references a specific Java
180 * variable (field or array element). However, the results are undefined
181 * if that variable is not in fact of the type returned by this method.
182 * <p>
183 * This method refers to a variable by means of two parameters, and so
184 * it provides (in effect) a <em>double-register</em> addressing mode
185 * for Java variables. When the object reference is null, this method
186 * uses its offset as an absolute address. This is similar in operation
187 * to methods such as {@link #getInt(long)}, which provide (in effect) a
188 * <em>single-register</em> addressing mode for non-Java variables.
189 * However, because Java variables may have a different layout in memory
190 * from non-Java variables, programmers should not assume that these
191 * two addressing modes are ever equivalent. Also, programmers should
192 * remember that offsets from the double-register addressing mode cannot
193 * be portably confused with longs used in the single-register addressing
194 * mode.
195 *
196 * @param o Java heap object in which the variable resides, if any, else
197 * null
198 * @param offset indication of where the variable resides in a Java heap
199 * object, if any, else a memory address locating the variable
200 * statically
201 * @return the value fetched from the indicated Java variable
202 * @throws RuntimeException No defined exceptions are thrown, not even
203 * {@link NullPointerException}
204 */
205 @IntrinsicCandidate
206 public native int getInt(Object o, long offset);
207
208 /**
209 * Stores a value into a given Java variable.
210 * <p>
211 * The first two parameters are interpreted exactly as with
212 * {@link #getInt(Object, long)} to refer to a specific
213 * Java variable (field or array element). The given value
214 * is stored into that variable.
215 * <p>
216 * The variable must be of the same type as the method
217 * parameter {@code x}.
218 *
219 * @param o Java heap object in which the variable resides, if any, else
220 * null
221 * @param offset indication of where the variable resides in a Java heap
222 * object, if any, else a memory address locating the variable
223 * statically
224 * @param x the value to store into the indicated Java variable
225 * @throws RuntimeException No defined exceptions are thrown, not even
226 * {@link NullPointerException}
227 */
228 @IntrinsicCandidate
229 public native void putInt(Object o, long offset, int x);
230
231 /**
232 * Returns true if the given field is flattened.
233 */
234 public boolean isFlatField(Field f) {
235 if (f == null) {
236 throw new NullPointerException();
237 }
238 return isFlatField0(f);
239 }
240
241 private native boolean isFlatField0(Object o);
242
243 /* Returns true if the given field has a null marker
244 * <p>
245 * Nullable flat fields are stored in a flattened representation
246 * and have an associated null marker to indicate if the the field value is
247 * null or the one stored with the flat representation
248 */
249
250 public boolean hasNullMarker(Field f) {
251 if (f == null) {
252 throw new NullPointerException();
253 }
254 return hasNullMarker0(f);
255 }
256
257 private native boolean hasNullMarker0(Object o);
258
259 /* Returns the offset of the null marker of the field,
260 * or -1 if the field doesn't have a null marker
261 */
262
263 public int nullMarkerOffset(Field f) {
264 if (f == null) {
265 throw new NullPointerException();
266 }
267 return nullMarkerOffset0(f);
268 }
269
270 private native int nullMarkerOffset0(Object o);
271
272 /**
273 * The layout value for a "reference pointer" layout. This is the only
274 * value layout values should check against; all other layout values
275 * represent some kind of flat layout opaque to Java code.
276 *
277 * @see #arrayLayout
278 * @see #fieldLayout
279 */
280 public static final int NON_FLAT_LAYOUT = 0;
281
282 /* Reports the kind of layout used for an element in the storage
283 * allocation of the given array. Do not expect to perform any logic
284 * or layout control with this value, it is just an opaque token
285 * used for performance reasons.
286 *
287 * A layout of 0 indicates this array is not flat.
288 */
289 public int arrayLayout(Object[] array) {
290 if (array == null) {
291 throw new NullPointerException();
292 }
293 return arrayLayout0(array);
294 }
295
296 @IntrinsicCandidate
297 private native int arrayLayout0(Object[] array);
298
299
300 /* Reports the kind of layout used for a given field in the storage
301 * allocation of its class. Do not expect to perform any logic
302 * or layout control with this value, it is just an opaque token
303 * used for performance reasons.
304 *
305 * A layout of 0 indicates this field is not flat.
306 */
307 public int fieldLayout(Field f) {
308 if (f == null) {
309 throw new NullPointerException();
310 }
311 return fieldLayout0(f);
312 }
313
314 private native int fieldLayout0(Object o);
315
316 public native Object[] newSpecialArray(Class<?> componentType,
317 int length, int layoutKind);
318
319 /**
320 * Fetches a reference value from a given Java variable.
321 * This method can return a reference to either an object or value
322 * or a null reference.
323 *
324 * @see #getInt(Object, long)
325 */
326 @IntrinsicCandidate
327 public native Object getReference(Object o, long offset);
328
329 /**
330 * Stores a reference value into a given Java variable.
331 * This method can store a reference to either an object or value
332 * or a null reference.
333 * <p>
334 * Unless the reference {@code x} being stored is either null
335 * or matches the field type, the results are undefined.
336 * If the reference {@code o} is non-null, card marks or
337 * other store barriers for that object (if the VM requires them)
338 * are updated.
339 * @see #putInt(Object, long, int)
340 */
341 @IntrinsicCandidate
342 public native void putReference(Object o, long offset, Object x);
343
344 /**
345 * Fetches a value of type {@code <V>} from a given Java variable.
346 * More specifically, fetches a field or array element within the given
347 * {@code o} object at the given offset, or (if {@code o} is null)
348 * from the memory address whose numerical value is the given offset.
349 *
350 * @apiNote
351 * The returned object is newly allocated into the heap, because flat
352 * values lack object headers and thus can't be used as objects directly.
353 *
354 * @param o Java heap object in which the variable resides, if any, else
355 * null
356 * @param offset indication of where the variable resides in a Java heap
357 * object, if any, else a memory address locating the variable
358 * statically
359 * @param layoutKind opaque value used by the VM to know the layout
360 * the field or array element. This value must be retrieved with
361 * {@link #fieldLayout} or {@link #arrayLayout}.
362 * @param valueType value type
363 * @param <V> the type of a value
364 * @return the value fetched from the indicated Java variable
365 * @throws RuntimeException No defined exceptions are thrown, not even
366 * {@link NullPointerException}
367 */
368 @IntrinsicCandidate
369 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
370
371 /**
372 * Stores the given value into a given Java variable.
373 *
374 * Unless the reference {@code o} being stored is either null
375 * or matches the field type, the results are undefined.
376 *
377 * @param o Java heap object in which the variable resides, if any, else
378 * null
379 * @param offset indication of where the variable resides in a Java heap
380 * object, if any, else a memory address locating the variable
381 * statically
382 * @param layoutKind opaque value used by the VM to know the layout
383 * the field or array element. This value must be retrieved with
384 * {@link #fieldLayout} or {@link #arrayLayout}.
385 * @param valueType value type
386 * @param v the value to store into the indicated Java variable
387 * @param <V> the type of a value
388 * @throws RuntimeException No defined exceptions are thrown, not even
389 * {@link NullPointerException}
390 */
391 @IntrinsicCandidate
392 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
393
394 /**
395 * Returns the header size of the given value type.
396 *
397 * @param valueType value type
398 * @return the header size of the value type
399 */
400 public native <V> long valueHeaderSize(Class<V> valueType);
401
402 /** @see #getInt(Object, long) */
403 @IntrinsicCandidate
404 public native boolean getBoolean(Object o, long offset);
405
406 /** @see #putInt(Object, long, int) */
407 @IntrinsicCandidate
408 public native void putBoolean(Object o, long offset, boolean x);
409
410 /** @see #getInt(Object, long) */
411 @IntrinsicCandidate
412 public native byte getByte(Object o, long offset);
413
414 /** @see #putInt(Object, long, int) */
415 @IntrinsicCandidate
416 public native void putByte(Object o, long offset, byte x);
417
418 /** @see #getInt(Object, long) */
419 @IntrinsicCandidate
420 public native short getShort(Object o, long offset);
421
422 /** @see #putInt(Object, long, int) */
423 @IntrinsicCandidate
424 public native void putShort(Object o, long offset, short x);
425
426 /** @see #getInt(Object, long) */
427 @IntrinsicCandidate
428 public native char getChar(Object o, long offset);
429
430 /** @see #putInt(Object, long, int) */
431 @IntrinsicCandidate
432 public native void putChar(Object o, long offset, char x);
433
434 /** @see #getInt(Object, long) */
435 @IntrinsicCandidate
436 public native long getLong(Object o, long offset);
437
438 /** @see #putInt(Object, long, int) */
439 @IntrinsicCandidate
440 public native void putLong(Object o, long offset, long x);
441
442 /** @see #getInt(Object, long) */
443 @IntrinsicCandidate
444 public native float getFloat(Object o, long offset);
445
446 /** @see #putInt(Object, long, int) */
447 @IntrinsicCandidate
448 public native void putFloat(Object o, long offset, float x);
449
450 /** @see #getInt(Object, long) */
451 @IntrinsicCandidate
452 public native double getDouble(Object o, long offset);
453
454 /** @see #putInt(Object, long, int) */
455 @IntrinsicCandidate
456 public native void putDouble(Object o, long offset, double x);
457
458 /**
459 * Fetches a native pointer from a given memory address. If the address is
460 * zero, or does not point into a block obtained from {@link
461 * #allocateMemory}, the results are undefined.
462 *
463 * <p>If the native pointer is less than 64 bits wide, it is extended as
464 * an unsigned number to a Java long. The pointer may be indexed by any
465 * given byte offset, simply by adding that offset (as a simple integer) to
466 * the long representing the pointer. The number of bytes actually read
467 * from the target address may be determined by consulting {@link
468 * #addressSize}.
469 *
470 * @see #allocateMemory
471 * @see #getInt(Object, long)
472 */
473 @ForceInline
474 public long getAddress(Object o, long offset) {
475 if (ADDRESS_SIZE == 4) {
476 return Integer.toUnsignedLong(getInt(o, offset));
477 } else {
478 return getLong(o, offset);
479 }
480 }
481
482 /**
483 * Stores a native pointer into a given memory address. If the address is
484 * zero, or does not point into a block obtained from {@link
485 * #allocateMemory}, the results are undefined.
486 *
487 * <p>The number of bytes actually written at the target address may be
488 * determined by consulting {@link #addressSize}.
489 *
490 * @see #allocateMemory
491 * @see #putInt(Object, long, int)
492 */
493 @ForceInline
494 public void putAddress(Object o, long offset, long x) {
495 if (ADDRESS_SIZE == 4) {
496 putInt(o, offset, (int)x);
497 } else {
498 putLong(o, offset, x);
499 }
500 }
501
502 // These read VM internal data.
503
504 /**
505 * Fetches an uncompressed reference value from a given native variable
506 * ignoring the VM's compressed references mode.
507 *
508 * @param address a memory address locating the variable
509 * @return the value fetched from the indicated native variable
510 */
511 public native Object getUncompressedObject(long address);
512
513 // These work on values in the C heap.
514
515 /**
516 * Fetches a value from a given memory address. If the address is zero, or
517 * does not point into a block obtained from {@link #allocateMemory}, the
518 * results are undefined.
519 *
520 * @see #allocateMemory
521 */
522 @ForceInline
523 public byte getByte(long address) {
524 return getByte(null, address);
525 }
526
527 /**
528 * Stores a value into a given memory address. If the address is zero, or
529 * does not point into a block obtained from {@link #allocateMemory}, the
530 * results are undefined.
531 *
532 * @see #getByte(long)
533 */
534 @ForceInline
535 public void putByte(long address, byte x) {
536 putByte(null, address, x);
537 }
538
539 /** @see #getByte(long) */
540 @ForceInline
541 public short getShort(long address) {
542 return getShort(null, address);
543 }
544
545 /** @see #putByte(long, byte) */
546 @ForceInline
547 public void putShort(long address, short x) {
548 putShort(null, address, x);
549 }
550
551 /** @see #getByte(long) */
552 @ForceInline
553 public char getChar(long address) {
554 return getChar(null, address);
555 }
556
557 /** @see #putByte(long, byte) */
558 @ForceInline
559 public void putChar(long address, char x) {
560 putChar(null, address, x);
561 }
562
563 /** @see #getByte(long) */
564 @ForceInline
565 public int getInt(long address) {
566 return getInt(null, address);
567 }
568
569 /** @see #putByte(long, byte) */
570 @ForceInline
571 public void putInt(long address, int x) {
572 putInt(null, address, x);
573 }
574
575 /** @see #getByte(long) */
576 @ForceInline
577 public long getLong(long address) {
578 return getLong(null, address);
579 }
580
581 /** @see #putByte(long, byte) */
582 @ForceInline
583 public void putLong(long address, long x) {
584 putLong(null, address, x);
585 }
586
587 /** @see #getByte(long) */
588 @ForceInline
589 public float getFloat(long address) {
590 return getFloat(null, address);
591 }
592
593 /** @see #putByte(long, byte) */
594 @ForceInline
595 public void putFloat(long address, float x) {
596 putFloat(null, address, x);
597 }
598
599 /** @see #getByte(long) */
600 @ForceInline
601 public double getDouble(long address) {
602 return getDouble(null, address);
603 }
604
605 /** @see #putByte(long, byte) */
606 @ForceInline
607 public void putDouble(long address, double x) {
608 putDouble(null, address, x);
609 }
610
611 /** @see #getAddress(Object, long) */
612 @ForceInline
613 public long getAddress(long address) {
614 return getAddress(null, address);
615 }
616
617 /** @see #putAddress(Object, long, long) */
618 @ForceInline
619 public void putAddress(long address, long x) {
620 putAddress(null, address, x);
621 }
622
623
624
625 //--- helper methods for validating various types of objects/values
626
627 /**
628 * Create an exception reflecting that some of the input was invalid
629 *
630 * <em>Note:</em> It is the responsibility of the caller to make
631 * sure arguments are checked before the methods are called. While
632 * some rudimentary checks are performed on the input, the checks
633 * are best effort and when performance is an overriding priority,
634 * as when methods of this class are optimized by the runtime
635 * compiler, some or all checks (if any) may be elided. Hence, the
636 * caller must not rely on the checks and corresponding
637 * exceptions!
638 *
639 * @return an exception object
640 */
641 private RuntimeException invalidInput() {
642 return new IllegalArgumentException();
643 }
644
645 /**
646 * Check if a value is 32-bit clean (32 MSB are all zero)
647 *
648 * @param value the 64-bit value to check
649 *
650 * @return true if the value is 32-bit clean
651 */
652 private boolean is32BitClean(long value) {
653 return value >>> 32 == 0;
654 }
655
656 /**
657 * Check the validity of a size (the equivalent of a size_t)
658 *
659 * @throws RuntimeException if the size is invalid
660 * (<em>Note:</em> after optimization, invalid inputs may
661 * go undetected, which will lead to unpredictable
662 * behavior)
663 */
664 private void checkSize(long size) {
665 if (ADDRESS_SIZE == 4) {
666 // Note: this will also check for negative sizes
667 if (!is32BitClean(size)) {
668 throw invalidInput();
669 }
670 } else if (size < 0) {
671 throw invalidInput();
672 }
673 }
674
675 /**
676 * Check the validity of a native address (the equivalent of void*)
677 *
678 * @throws RuntimeException if the address is invalid
679 * (<em>Note:</em> after optimization, invalid inputs may
680 * go undetected, which will lead to unpredictable
681 * behavior)
682 */
683 private void checkNativeAddress(long address) {
684 if (ADDRESS_SIZE == 4) {
685 // Accept both zero and sign extended pointers. A valid
686 // pointer will, after the +1 below, either have produced
687 // the value 0x0 or 0x1. Masking off the low bit allows
688 // for testing against 0.
689 if ((((address >> 32) + 1) & ~1) != 0) {
690 throw invalidInput();
691 }
692 }
693 }
694
695 /**
696 * Check the validity of an offset, relative to a base object
697 *
698 * @param o the base object
699 * @param offset the offset to check
700 *
701 * @throws RuntimeException if the size is invalid
702 * (<em>Note:</em> after optimization, invalid inputs may
703 * go undetected, which will lead to unpredictable
704 * behavior)
705 */
706 private void checkOffset(Object o, long offset) {
707 if (ADDRESS_SIZE == 4) {
708 // Note: this will also check for negative offsets
709 if (!is32BitClean(offset)) {
710 throw invalidInput();
711 }
712 } else if (offset < 0) {
713 throw invalidInput();
714 }
715 }
716
717 /**
718 * Check the validity of a double-register pointer
719 *
720 * Note: This code deliberately does *not* check for NPE for (at
721 * least) three reasons:
722 *
723 * 1) NPE is not just NULL/0 - there is a range of values all
724 * resulting in an NPE, which is not trivial to check for
725 *
726 * 2) It is the responsibility of the callers of Unsafe methods
727 * to verify the input, so throwing an exception here is not really
728 * useful - passing in a NULL pointer is a critical error and the
729 * must not expect an exception to be thrown anyway.
730 *
731 * 3) the actual operations will detect NULL pointers anyway by
732 * means of traps and signals (like SIGSEGV).
733 *
734 * @param o Java heap object, or null
735 * @param offset indication of where the variable resides in a Java heap
736 * object, if any, else a memory address locating the variable
737 * statically
738 *
739 * @throws RuntimeException if the pointer is invalid
740 * (<em>Note:</em> after optimization, invalid inputs may
741 * go undetected, which will lead to unpredictable
742 * behavior)
743 */
744 private void checkPointer(Object o, long offset) {
745 if (o == null) {
746 checkNativeAddress(offset);
747 } else {
748 checkOffset(o, offset);
749 }
750 }
751
752 /**
753 * Check if a type is a primitive array type
754 *
755 * @param c the type to check
756 *
757 * @return true if the type is a primitive array type
758 */
759 private void checkPrimitiveArray(Class<?> c) {
760 Class<?> componentType = c.getComponentType();
761 if (componentType == null || !componentType.isPrimitive()) {
762 throw invalidInput();
763 }
764 }
765
766 /**
767 * Check that a pointer is a valid primitive array type pointer
768 *
769 * Note: pointers off-heap are considered to be primitive arrays
770 *
771 * @throws RuntimeException if the pointer is invalid
772 * (<em>Note:</em> after optimization, invalid inputs may
773 * go undetected, which will lead to unpredictable
774 * behavior)
775 */
776 private void checkPrimitivePointer(Object o, long offset) {
777 checkPointer(o, offset);
778
779 if (o != null) {
780 // If on heap, it must be a primitive array
781 checkPrimitiveArray(o.getClass());
782 }
783 }
784
785
786 //--- wrappers for malloc, realloc, free:
787
788 /**
789 * Round up allocation size to a multiple of HeapWordSize.
790 */
791 private long alignToHeapWordSize(long bytes) {
792 if (bytes >= 0) {
793 return (bytes + ADDRESS_SIZE - 1) & ~(ADDRESS_SIZE - 1);
794 } else {
795 throw invalidInput();
796 }
797 }
798
799 /**
800 * Allocates a new block of native memory, of the given size in bytes. The
801 * contents of the memory are uninitialized; they will generally be
802 * garbage. The resulting native pointer will be zero if and only if the
803 * requested size is zero. The resulting native pointer will be aligned for
804 * all value types. Dispose of this memory by calling {@link #freeMemory}
805 * or resize it with {@link #reallocateMemory}.
806 *
807 * <em>Note:</em> It is the responsibility of the caller to make
808 * sure arguments are checked before the methods are called. While
809 * some rudimentary checks are performed on the input, the checks
810 * are best effort and when performance is an overriding priority,
811 * as when methods of this class are optimized by the runtime
812 * compiler, some or all checks (if any) may be elided. Hence, the
813 * caller must not rely on the checks and corresponding
814 * exceptions!
815 *
816 * @throws RuntimeException if the size is negative or too large
817 * for the native size_t type
818 *
819 * @throws OutOfMemoryError if the allocation is refused by the system
820 *
821 * @see #getByte(long)
822 * @see #putByte(long, byte)
823 */
824 public long allocateMemory(long bytes) {
825 bytes = alignToHeapWordSize(bytes);
826
827 allocateMemoryChecks(bytes);
828
829 if (bytes == 0) {
830 return 0;
831 }
832
833 long p = allocateMemory0(bytes);
834 if (p == 0) {
835 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes");
836 }
837
838 return p;
839 }
840
841 /**
842 * Validate the arguments to allocateMemory
843 *
844 * @throws RuntimeException if the arguments are invalid
845 * (<em>Note:</em> after optimization, invalid inputs may
846 * go undetected, which will lead to unpredictable
847 * behavior)
848 */
849 private void allocateMemoryChecks(long bytes) {
850 checkSize(bytes);
851 }
852
853 /**
854 * Resizes a new block of native memory, to the given size in bytes. The
855 * contents of the new block past the size of the old block are
856 * uninitialized; they will generally be garbage. The resulting native
857 * pointer will be zero if and only if the requested size is zero. The
858 * resulting native pointer will be aligned for all value types. Dispose
859 * of this memory by calling {@link #freeMemory}, or resize it with {@link
860 * #reallocateMemory}. The address passed to this method may be null, in
861 * which case an allocation will be performed.
862 *
863 * <em>Note:</em> It is the responsibility of the caller to make
864 * sure arguments are checked before the methods are called. While
865 * some rudimentary checks are performed on the input, the checks
866 * are best effort and when performance is an overriding priority,
867 * as when methods of this class are optimized by the runtime
868 * compiler, some or all checks (if any) may be elided. Hence, the
869 * caller must not rely on the checks and corresponding
870 * exceptions!
871 *
872 * @throws RuntimeException if the size is negative or too large
873 * for the native size_t type
874 *
875 * @throws OutOfMemoryError if the allocation is refused by the system
876 *
877 * @see #allocateMemory
878 */
879 public long reallocateMemory(long address, long bytes) {
880 bytes = alignToHeapWordSize(bytes);
881
882 reallocateMemoryChecks(address, bytes);
883
884 if (bytes == 0) {
885 freeMemory(address);
886 return 0;
887 }
888
889 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
890 if (p == 0) {
891 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes");
892 }
893
894 return p;
895 }
896
897 /**
898 * Validate the arguments to reallocateMemory
899 *
900 * @throws RuntimeException if the arguments are invalid
901 * (<em>Note:</em> after optimization, invalid inputs may
902 * go undetected, which will lead to unpredictable
903 * behavior)
904 */
905 private void reallocateMemoryChecks(long address, long bytes) {
906 checkPointer(null, address);
907 checkSize(bytes);
908 }
909
910 /**
911 * Sets all bytes in a given block of memory to a fixed value
912 * (usually zero).
913 *
914 * <p>This method determines a block's base address by means of two parameters,
915 * and so it provides (in effect) a <em>double-register</em> addressing mode,
916 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
917 * the offset supplies an absolute base address.
918 *
919 * <p>The stores are in coherent (atomic) units of a size determined
920 * by the address and length parameters. If the effective address and
921 * length are all even modulo 8, the stores take place in 'long' units.
922 * If the effective address and length are (resp.) even modulo 4 or 2,
923 * the stores take place in units of 'int' or 'short'.
924 *
925 * <em>Note:</em> It is the responsibility of the caller to make
926 * sure arguments are checked before the methods are called. While
927 * some rudimentary checks are performed on the input, the checks
928 * are best effort and when performance is an overriding priority,
929 * as when methods of this class are optimized by the runtime
930 * compiler, some or all checks (if any) may be elided. Hence, the
931 * caller must not rely on the checks and corresponding
932 * exceptions!
933 *
934 * @throws RuntimeException if any of the arguments is invalid
935 *
936 * @since 1.7
937 */
938 public void setMemory(Object o, long offset, long bytes, byte value) {
939 setMemoryChecks(o, offset, bytes, value);
940
941 if (bytes == 0) {
942 return;
943 }
944
945 setMemory0(o, offset, bytes, value);
946 }
947
948 /**
949 * Sets all bytes in a given block of memory to a fixed value
950 * (usually zero). This provides a <em>single-register</em> addressing mode,
951 * as discussed in {@link #getInt(Object,long)}.
952 *
953 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
954 */
955 public void setMemory(long address, long bytes, byte value) {
956 setMemory(null, address, bytes, value);
957 }
958
959 /**
960 * Validate the arguments to setMemory
961 *
962 * @throws RuntimeException if the arguments are invalid
963 * (<em>Note:</em> after optimization, invalid inputs may
964 * go undetected, which will lead to unpredictable
965 * behavior)
966 */
967 private void setMemoryChecks(Object o, long offset, long bytes, byte value) {
968 checkPrimitivePointer(o, offset);
969 checkSize(bytes);
970 }
971
972 /**
973 * Sets all bytes in a given block of memory to a copy of another
974 * block.
975 *
976 * <p>This method determines each block's base address by means of two parameters,
977 * and so it provides (in effect) a <em>double-register</em> addressing mode,
978 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
979 * the offset supplies an absolute base address.
980 *
981 * <p>The transfers are in coherent (atomic) units of a size determined
982 * by the address and length parameters. If the effective addresses and
983 * length are all even modulo 8, the transfer takes place in 'long' units.
984 * If the effective addresses and length are (resp.) even modulo 4 or 2,
985 * the transfer takes place in units of 'int' or 'short'.
986 *
987 * <em>Note:</em> It is the responsibility of the caller to make
988 * sure arguments are checked before the methods are called. While
989 * some rudimentary checks are performed on the input, the checks
990 * are best effort and when performance is an overriding priority,
991 * as when methods of this class are optimized by the runtime
992 * compiler, some or all checks (if any) may be elided. Hence, the
993 * caller must not rely on the checks and corresponding
994 * exceptions!
995 *
996 * @throws RuntimeException if any of the arguments is invalid
997 *
998 * @since 1.7
999 */
1000 public void copyMemory(Object srcBase, long srcOffset,
1001 Object destBase, long destOffset,
1002 long bytes) {
1003 copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
1004
1005 if (bytes == 0) {
1006 return;
1007 }
1008
1009 copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
1010 }
1011
1012 /**
1013 * Sets all bytes in a given block of memory to a copy of another
1014 * block. This provides a <em>single-register</em> addressing mode,
1015 * as discussed in {@link #getInt(Object,long)}.
1016 *
1017 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
1018 */
1019 public void copyMemory(long srcAddress, long destAddress, long bytes) {
1020 copyMemory(null, srcAddress, null, destAddress, bytes);
1021 }
1022
1023 /**
1024 * Validate the arguments to copyMemory
1025 *
1026 * @throws RuntimeException if any of the arguments is invalid
1027 * (<em>Note:</em> after optimization, invalid inputs may
1028 * go undetected, which will lead to unpredictable
1029 * behavior)
1030 */
1031 private void copyMemoryChecks(Object srcBase, long srcOffset,
1032 Object destBase, long destOffset,
1033 long bytes) {
1034 checkSize(bytes);
1035 checkPrimitivePointer(srcBase, srcOffset);
1036 checkPrimitivePointer(destBase, destOffset);
1037 }
1038
1039 /**
1040 * Copies all elements from one block of memory to another block,
1041 * *unconditionally* byte swapping the elements on the fly.
1042 *
1043 * <p>This method determines each block's base address by means of two parameters,
1044 * and so it provides (in effect) a <em>double-register</em> addressing mode,
1045 * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
1046 * the offset supplies an absolute base address.
1047 *
1048 * <em>Note:</em> It is the responsibility of the caller to make
1049 * sure arguments are checked before the methods are called. While
1050 * some rudimentary checks are performed on the input, the checks
1051 * are best effort and when performance is an overriding priority,
1052 * as when methods of this class are optimized by the runtime
1053 * compiler, some or all checks (if any) may be elided. Hence, the
1054 * caller must not rely on the checks and corresponding
1055 * exceptions!
1056 *
1057 * @throws RuntimeException if any of the arguments is invalid
1058 *
1059 * @since 9
1060 */
1061 public void copySwapMemory(Object srcBase, long srcOffset,
1062 Object destBase, long destOffset,
1063 long bytes, long elemSize) {
1064 copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
1065
1066 if (bytes == 0) {
1067 return;
1068 }
1069
1070 copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
1071 }
1072
1073 private void copySwapMemoryChecks(Object srcBase, long srcOffset,
1074 Object destBase, long destOffset,
1075 long bytes, long elemSize) {
1076 checkSize(bytes);
1077
1078 if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
1079 throw invalidInput();
1080 }
1081 if (bytes % elemSize != 0) {
1082 throw invalidInput();
1083 }
1084
1085 checkPrimitivePointer(srcBase, srcOffset);
1086 checkPrimitivePointer(destBase, destOffset);
1087 }
1088
1089 /**
1090 * Copies all elements from one block of memory to another block, byte swapping the
1091 * elements on the fly.
1092 *
1093 * This provides a <em>single-register</em> addressing mode, as
1094 * discussed in {@link #getInt(Object,long)}.
1095 *
1096 * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
1097 */
1098 public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
1099 copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
1100 }
1101
1102 /**
1103 * Disposes of a block of native memory, as obtained from {@link
1104 * #allocateMemory} or {@link #reallocateMemory}. The address passed to
1105 * this method may be null, in which case no action is taken.
1106 *
1107 * <em>Note:</em> It is the responsibility of the caller to make
1108 * sure arguments are checked before the methods are called. While
1109 * some rudimentary checks are performed on the input, the checks
1110 * are best effort and when performance is an overriding priority,
1111 * as when methods of this class are optimized by the runtime
1112 * compiler, some or all checks (if any) may be elided. Hence, the
1113 * caller must not rely on the checks and corresponding
1114 * exceptions!
1115 *
1116 * @throws RuntimeException if any of the arguments is invalid
1117 *
1118 * @see #allocateMemory
1119 */
1120 public void freeMemory(long address) {
1121 freeMemoryChecks(address);
1122
1123 if (address == 0) {
1124 return;
1125 }
1126
1127 freeMemory0(address);
1128 }
1129
1130 /**
1131 * Validate the arguments to freeMemory
1132 *
1133 * @throws RuntimeException if the arguments are invalid
1134 * (<em>Note:</em> after optimization, invalid inputs may
1135 * go undetected, which will lead to unpredictable
1136 * behavior)
1137 */
1138 private void freeMemoryChecks(long address) {
1139 checkPointer(null, address);
1140 }
1141
1142 /**
1143 * Ensure writeback of a specified virtual memory address range
1144 * from cache to physical memory. All bytes in the address range
1145 * are guaranteed to have been written back to physical memory on
1146 * return from this call i.e. subsequently executed store
1147 * instructions are guaranteed not to be visible before the
1148 * writeback is completed.
1149 *
1150 * @param address
1151 * the lowest byte address that must be guaranteed written
1152 * back to memory. bytes at lower addresses may also be
1153 * written back.
1154 *
1155 * @param length
1156 * the length in bytes of the region starting at address
1157 * that must be guaranteed written back to memory.
1158 *
1159 * @throws RuntimeException if memory writeback is not supported
1160 * on the current hardware of if the arguments are invalid.
1161 * (<em>Note:</em> after optimization, invalid inputs may
1162 * go undetected, which will lead to unpredictable
1163 * behavior)
1164 *
1165 * @since 14
1166 */
1167
1168 public void writebackMemory(long address, long length) {
1169 checkWritebackEnabled();
1170 checkWritebackMemory(address, length);
1171
1172 // perform any required pre-writeback barrier
1173 writebackPreSync0();
1174
1175 // write back one cache line at a time
1176 long line = dataCacheLineAlignDown(address);
1177 long end = address + length;
1178 while (line < end) {
1179 writeback0(line);
1180 line += dataCacheLineFlushSize();
1181 }
1182
1183 // perform any required post-writeback barrier
1184 writebackPostSync0();
1185 }
1186
1187 /**
1188 * Validate the arguments to writebackMemory
1189 *
1190 * @throws RuntimeException if the arguments are invalid
1191 * (<em>Note:</em> after optimization, invalid inputs may
1192 * go undetected, which will lead to unpredictable
1193 * behavior)
1194 */
1195 private void checkWritebackMemory(long address, long length) {
1196 checkNativeAddress(address);
1197 checkSize(length);
1198 }
1199
1200 /**
1201 * Validate that the current hardware supports memory writeback.
1202 * (<em>Note:</em> this is a belt and braces check. Clients are
1203 * expected to test whether writeback is enabled by calling
1204 * ({@link isWritebackEnabled #isWritebackEnabled} and avoid
1205 * calling method {@link writeback #writeback} if it is disabled).
1206 *
1207 *
1208 * @throws RuntimeException if memory writeback is not supported
1209 */
1210 private void checkWritebackEnabled() {
1211 if (!isWritebackEnabled()) {
1212 throw new RuntimeException("writebackMemory not enabled!");
1213 }
1214 }
1215
1216 /**
1217 * force writeback of an individual cache line.
1218 *
1219 * @param address
1220 * the start address of the cache line to be written back
1221 */
1222 @IntrinsicCandidate
1223 private native void writeback0(long address);
1224
1225 /**
1226 * Serialize writeback operations relative to preceding memory writes.
1227 */
1228 @IntrinsicCandidate
1229 private native void writebackPreSync0();
1230
1231 /**
1232 * Serialize writeback operations relative to following memory writes.
1233 */
1234 @IntrinsicCandidate
1235 private native void writebackPostSync0();
1236
1237 //--- random queries
1238
1239 /**
1240 * This constant differs from all results that will ever be returned from
1241 * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1242 * or {@link #arrayBaseOffset}.
1243 * <p>
1244 * The static type is @code long} to emphasize that long arithmetic should
1245 * always be used for offset calculations to avoid overflows.
1246 */
1247 public static final long INVALID_FIELD_OFFSET = -1;
1248
1249 /**
1250 * Reports the location of a given field in the storage allocation of its
1251 * class. Do not expect to perform any sort of arithmetic on this offset;
1252 * it is just a cookie which is passed to the unsafe heap memory accessors.
1253 *
1254 * <p>Any given field will always have the same offset and base, and no
1255 * two distinct fields of the same class will ever have the same offset
1256 * and base.
1257 *
1258 * <p>As of 1.4.1, offsets for fields are represented as long values,
1259 * although the Sun JVM does not use the most significant 32 bits.
1260 * However, JVM implementations which store static fields at absolute
1261 * addresses can use long offsets and null base pointers to express
1262 * the field locations in a form usable by {@link #getInt(Object,long)}.
1263 * Therefore, code which will be ported to such JVMs on 64-bit platforms
1264 * must preserve all bits of static field offsets.
1265 *
1266 * @throws NullPointerException if the field is {@code null}
1267 * @throws IllegalArgumentException if the field is static
1268 * @see #getInt(Object, long)
1269 */
1270 public long objectFieldOffset(Field f) {
1271 if (f == null) {
1272 throw new NullPointerException();
1273 }
1274
1275 return objectFieldOffset0(f);
1276 }
1277
1278 /**
1279 * (For compile-time known instance fields in JDK code only) Reports the
1280 * location of the field with a given name in the storage allocation of its
1281 * class.
1282 * <p>
1283 * This API is used to avoid creating reflective Objects in Java code at
1284 * startup. This should not be used to find fields in non-trusted code.
1285 * Use the {@link #objectFieldOffset(Field) Field}-accepting version for
1286 * arbitrary fields instead.
1287 *
1288 * @throws NullPointerException if any parameter is {@code null}.
1289 * @throws InternalError if the presumably known field couldn't be found
1290 *
1291 * @see #objectFieldOffset(Field)
1292 */
1293 public long objectFieldOffset(Class<?> c, String name) {
1294 if (c == null || name == null) {
1295 throw new NullPointerException();
1296 }
1297
1298 long result = knownObjectFieldOffset0(c, name);
1299 if (result < 0) {
1300 String type = switch ((int) result) {
1301 case -2 -> "a static field";
1302 case -1 -> "not found";
1303 default -> "unknown";
1304 };
1305 throw new InternalError("Field %s.%s %s".formatted(c.getTypeName(), name, type));
1306 }
1307 return result;
1308 }
1309
1310 /**
1311 * Reports the location of a given static field, in conjunction with {@link
1312 * #staticFieldBase}.
1313 * <p>Do not expect to perform any sort of arithmetic on this offset;
1314 * it is just a cookie which is passed to the unsafe heap memory accessors.
1315 *
1316 * <p>Any given field will always have the same offset, and no two distinct
1317 * fields of the same class will ever have the same offset.
1318 *
1319 * <p>As of 1.4.1, offsets for fields are represented as long values,
1320 * although the Sun JVM does not use the most significant 32 bits.
1321 * It is hard to imagine a JVM technology which needs more than
1322 * a few bits to encode an offset within a non-array object,
1323 * However, for consistency with other methods in this class,
1324 * this method reports its result as a long value.
1325 *
1326 * @throws NullPointerException if the field is {@code null}
1327 * @throws IllegalArgumentException if the field is not static
1328 * @see #getInt(Object, long)
1329 */
1330 public long staticFieldOffset(Field f) {
1331 if (f == null) {
1332 throw new NullPointerException();
1333 }
1334
1335 return staticFieldOffset0(f);
1336 }
1337
1338 /**
1339 * Reports the location of a given static field, in conjunction with {@link
1340 * #staticFieldOffset}.
1341 * <p>Fetch the base "Object", if any, with which static fields of the
1342 * given class can be accessed via methods like {@link #getInt(Object,
1343 * long)}. This value may be null. This value may refer to an object
1344 * which is a "cookie", not guaranteed to be a real Object, and it should
1345 * not be used in any way except as argument to the get and put routines in
1346 * this class.
1347 *
1348 * @throws NullPointerException if the field is {@code null}
1349 * @throws IllegalArgumentException if the field is not static
1350 */
1351 public Object staticFieldBase(Field f) {
1352 if (f == null) {
1353 throw new NullPointerException();
1354 }
1355
1356 return staticFieldBase0(f);
1357 }
1358
1359 /**
1360 * Detects if the given class is not yet fully initialized. This is often
1361 * needed in conjunction with obtaining the static field base of a
1362 * class.
1363 * @return false only if a call to {@code ensureClassInitialized} would have no effect
1364 */
1365 public boolean shouldBeInitialized(Class<?> c) {
1366 if (c == null) {
1367 throw new NullPointerException();
1368 }
1369
1370 return shouldBeInitialized0(c);
1371 }
1372
1373 /**
1374 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1375 * This is often needed in conjunction with obtaining the static field base
1376 * of a class.
1377 *
1378 * The call returns when either class {@code c} is fully initialized or
1379 * class {@code c} is being initialized and the call is performed from
1380 * the initializing thread. In the latter case a subsequent call to
1381 * {@link #shouldBeInitialized} will return {@code true}.
1382 */
1383 public void ensureClassInitialized(Class<?> c) {
1384 if (c == null) {
1385 throw new NullPointerException();
1386 }
1387
1388 ensureClassInitialized0(c);
1389 }
1390
1391 /**
1392 * The reading or writing of strict static fields may require
1393 * special processing. Notify the VM that such an event is about
1394 * to happen. The VM may respond by throwing an exception, in the
1395 * case of a read of an uninitialized field. If the VM allows the
1396 * method to return normally, no further calls are needed, with
1397 * the same arguments.
1398 */
1399 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1400 if (c == null) {
1401 throw new NullPointerException();
1402 }
1403 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1404 }
1405
1406 /**
1407 * Reports the offset of the first element in the storage allocation of a
1408 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1409 * for the same class, you may use that scale factor, together with this
1410 * base offset, to form new offsets to access elements of arrays of the
1411 * given class.
1412 * <p>
1413 * The return value is in the range of a {@code int}. The return type is
1414 * {@code long} to emphasize that long arithmetic should always be used
1415 * for offset calculations to avoid overflows.
1416 * <p>
1417 * This method doesn't support arrays with an element type that is
1418 * a value class, because this type of array can have multiple layouts.
1419 * For these arrays, {@code arrayInstanceBaseOffset(Object[] array)}
1420 * must be used instead.
1421 *
1422 * @see #getInt(Object, long)
1423 * @see #putInt(Object, long, int)
1424 */
1425 public long arrayBaseOffset(Class<?> arrayClass) {
1426 if (arrayClass == null) {
1427 throw new NullPointerException();
1428 }
1429
1430 return arrayBaseOffset0(arrayClass);
1431 }
1432
1433 public long arrayInstanceBaseOffset(Object[] array) {
1434 if (array == null) {
1435 throw new NullPointerException();
1436 }
1437
1438 return arrayInstanceBaseOffset0(array);
1439 }
1440
1441 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1442 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1443 = theUnsafe.arrayBaseOffset(boolean[].class);
1444
1445 /** The value of {@code arrayBaseOffset(byte[].class)} */
1446 public static final long ARRAY_BYTE_BASE_OFFSET
1447 = theUnsafe.arrayBaseOffset(byte[].class);
1448
1449 /** The value of {@code arrayBaseOffset(short[].class)} */
1450 public static final long ARRAY_SHORT_BASE_OFFSET
1451 = theUnsafe.arrayBaseOffset(short[].class);
1452
1453 /** The value of {@code arrayBaseOffset(char[].class)} */
1454 public static final long ARRAY_CHAR_BASE_OFFSET
1455 = theUnsafe.arrayBaseOffset(char[].class);
1456
1457 /** The value of {@code arrayBaseOffset(int[].class)} */
1458 public static final long ARRAY_INT_BASE_OFFSET
1459 = theUnsafe.arrayBaseOffset(int[].class);
1460
1461 /** The value of {@code arrayBaseOffset(long[].class)} */
1462 public static final long ARRAY_LONG_BASE_OFFSET
1463 = theUnsafe.arrayBaseOffset(long[].class);
1464
1465 /** The value of {@code arrayBaseOffset(float[].class)} */
1466 public static final long ARRAY_FLOAT_BASE_OFFSET
1467 = theUnsafe.arrayBaseOffset(float[].class);
1468
1469 /** The value of {@code arrayBaseOffset(double[].class)} */
1470 public static final long ARRAY_DOUBLE_BASE_OFFSET
1471 = theUnsafe.arrayBaseOffset(double[].class);
1472
1473 /** The value of {@code arrayBaseOffset(Object[].class)} */
1474 public static final long ARRAY_OBJECT_BASE_OFFSET
1475 = theUnsafe.arrayBaseOffset(Object[].class);
1476
1477 /**
1478 * Reports the scale factor for addressing elements in the storage
1479 * allocation of a given array class. However, arrays of "narrow" types
1480 * will generally not work properly with accessors like {@link
1481 * #getByte(Object, long)}, so the scale factor for such classes is reported
1482 * as zero.
1483 * <p>
1484 * The computation of the actual memory offset should always use {@code
1485 * long} arithmetic to avoid overflows.
1486 * <p>
1487 * This method doesn't support arrays with an element type that is
1488 * a value class, because this type of array can have multiple layouts.
1489 * For these arrays, {@code arrayInstanceIndexScale(Object[] array)}
1490 * must be used instead.
1491 *
1492 * @see #arrayBaseOffset
1493 * @see #getInt(Object, long)
1494 * @see #putInt(Object, long, int)
1495 */
1496 public int arrayIndexScale(Class<?> arrayClass) {
1497 if (arrayClass == null) {
1498 throw new NullPointerException();
1499 }
1500
1501 return arrayIndexScale0(arrayClass);
1502 }
1503
1504 public int arrayInstanceIndexScale(Object[] array) {
1505 if (array == null) {
1506 throw new NullPointerException();
1507 }
1508
1509 return arrayInstanceIndexScale0(array);
1510 }
1511
1512 /**
1513 * Returns the acmp map of this class, which must be a concrete value class.
1514 * Intended to be used by substitutability test in ValueObjectMethods only.
1515 * The format is subject to change.
1516 */
1517 public int[] getFieldMap(Class<?> c) {
1518 if (c == null) {
1519 throw new NullPointerException();
1520 }
1521 return getFieldMap0(c);
1522 }
1523
1524 /**
1525 * For a field of type {@code c}, returns true if and only if there is
1526 * a possible flat layout that contains no oop.
1527 * Required for numerical CAS safety.
1528 */
1529 public boolean isFlatPayloadBinary(Class<?> c) {
1530 int[] map = getFieldMap(c);
1531 int nbNonRef = map[0];
1532 return nbNonRef * 2 + 1 == map.length;
1533 }
1534
1535 /**
1536 * Return the size of the object in the heap.
1537 * @param o an object
1538 * @return the objects's size
1539 */
1540 public long getObjectSize(Object o) {
1541 if (o == null)
1542 throw new NullPointerException();
1543 return getObjectSize0(o);
1544 }
1545
1546 /** The value of {@code arrayIndexScale(boolean[].class)} */
1547 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1548 = theUnsafe.arrayIndexScale(boolean[].class);
1549
1550 /** The value of {@code arrayIndexScale(byte[].class)} */
1551 public static final int ARRAY_BYTE_INDEX_SCALE
1552 = theUnsafe.arrayIndexScale(byte[].class);
1553
1554 /** The value of {@code arrayIndexScale(short[].class)} */
1555 public static final int ARRAY_SHORT_INDEX_SCALE
1556 = theUnsafe.arrayIndexScale(short[].class);
1557
1558 /** The value of {@code arrayIndexScale(char[].class)} */
1559 public static final int ARRAY_CHAR_INDEX_SCALE
1560 = theUnsafe.arrayIndexScale(char[].class);
1561
1562 /** The value of {@code arrayIndexScale(int[].class)} */
1563 public static final int ARRAY_INT_INDEX_SCALE
1564 = theUnsafe.arrayIndexScale(int[].class);
1565
1566 /** The value of {@code arrayIndexScale(long[].class)} */
1567 public static final int ARRAY_LONG_INDEX_SCALE
1568 = theUnsafe.arrayIndexScale(long[].class);
1569
1570 /** The value of {@code arrayIndexScale(float[].class)} */
1571 public static final int ARRAY_FLOAT_INDEX_SCALE
1572 = theUnsafe.arrayIndexScale(float[].class);
1573
1574 /** The value of {@code arrayIndexScale(double[].class)} */
1575 public static final int ARRAY_DOUBLE_INDEX_SCALE
1576 = theUnsafe.arrayIndexScale(double[].class);
1577
1578 /** The value of {@code arrayIndexScale(Object[].class)} */
1579 public static final int ARRAY_OBJECT_INDEX_SCALE
1580 = theUnsafe.arrayIndexScale(Object[].class);
1581
1582 /**
1583 * Reports the size in bytes of a native pointer, as stored via {@link
1584 * #putAddress}. This value will be either 4 or 8. Note that the sizes of
1585 * other primitive types (as stored in native memory blocks) is determined
1586 * fully by their information content.
1587 */
1588 public int addressSize() {
1589 return ADDRESS_SIZE;
1590 }
1591
1592 /** The value of {@code addressSize()} */
1593 public static final int ADDRESS_SIZE = ADDRESS_SIZE0;
1594
1595 /**
1596 * Reports the size in bytes of a native memory page (whatever that is).
1597 * This value will always be a power of two.
1598 */
1599 public int pageSize() { return PAGE_SIZE; }
1600
1601 /**
1602 * Reports the size in bytes of a data cache line written back by
1603 * the hardware cache line flush operation available to the JVM or
1604 * 0 if data cache line flushing is not enabled.
1605 */
1606 public int dataCacheLineFlushSize() { return DATA_CACHE_LINE_FLUSH_SIZE; }
1607
1608 /**
1609 * Rounds down address to a data cache line boundary as
1610 * determined by {@link #dataCacheLineFlushSize}
1611 * @return the rounded down address
1612 */
1613 public long dataCacheLineAlignDown(long address) {
1614 return (address & ~(DATA_CACHE_LINE_FLUSH_SIZE - 1));
1615 }
1616
1617 /**
1618 * Returns true if data cache line writeback
1619 */
1620 public static boolean isWritebackEnabled() { return DATA_CACHE_LINE_FLUSH_SIZE != 0; }
1621
1622 //--- random trusted operations from JNI:
1623
1624 /**
1625 * Tells the VM to define a class, without security checks. By default, the
1626 * class loader and protection domain come from the caller's class.
1627 */
1628 public Class<?> defineClass(String name, byte[] b, int off, int len,
1629 ClassLoader loader,
1630 ProtectionDomain protectionDomain) {
1631 if (b == null) {
1632 throw new NullPointerException();
1633 }
1634 if (len < 0) {
1635 throw new ArrayIndexOutOfBoundsException();
1636 }
1637
1638 return defineClass0(name, b, off, len, loader, protectionDomain);
1639 }
1640
1641 public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1642 ClassLoader loader,
1643 ProtectionDomain protectionDomain);
1644
1645 /**
1646 * Allocates an instance but does not run any constructor.
1647 * Initializes the class if it has not yet been.
1648 * <p>
1649 * This method returns an uninitialized instance. In general, this is undefined behavior, this
1650 * method is treated specially by the JVM to allow this behavior. The returned value must be
1651 * passed into a constructor using {@link MethodHandle#linkToSpecial} before any other
1652 * operation can be performed on it. Otherwise, the program is ill-formed.
1653 */
1654 @IntrinsicCandidate
1655 public native Object allocateInstance(Class<?> cls)
1656 throws InstantiationException;
1657
1658 /**
1659 * Allocates an array of a given type, but does not do zeroing.
1660 * <p>
1661 * This method should only be used in the very rare cases where a high-performance code
1662 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1663 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1664 * <p>
1665 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1666 * before allowing untrusted code, or code in other threads, to observe the reference
1667 * to the newly allocated array. In addition, the publication of the array reference must be
1668 * safe according to the Java Memory Model requirements.
1669 * <p>
1670 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1671 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1672 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1673 * or issuing a {@link #storeFence} before publishing the reference.
1674 * <p>
1675 * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1676 * elements that could break heap integrity.
1677 *
1678 * @param componentType array component type to allocate
1679 * @param length array size to allocate
1680 * @throws IllegalArgumentException if component type is null, or not a primitive class;
1681 * or the length is negative
1682 */
1683 public Object allocateUninitializedArray(Class<?> componentType, int length) {
1684 if (componentType == null) {
1685 throw new IllegalArgumentException("Component type is null");
1686 }
1687 if (!componentType.isPrimitive()) {
1688 throw new IllegalArgumentException("Component type is not primitive");
1689 }
1690 if (length < 0) {
1691 throw new IllegalArgumentException("Negative length");
1692 }
1693 return allocateUninitializedArray0(componentType, length);
1694 }
1695
1696 @IntrinsicCandidate
1697 private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1698 // These fallbacks provide zeroed arrays, but intrinsic is not required to
1699 // return the zeroed arrays.
1700 if (componentType == byte.class) return new byte[length];
1701 if (componentType == boolean.class) return new boolean[length];
1702 if (componentType == short.class) return new short[length];
1703 if (componentType == char.class) return new char[length];
1704 if (componentType == int.class) return new int[length];
1705 if (componentType == float.class) return new float[length];
1706 if (componentType == long.class) return new long[length];
1707 if (componentType == double.class) return new double[length];
1708 return null;
1709 }
1710
1711 /** Throws the exception without telling the verifier. */
1712 public native void throwException(Throwable ee);
1713
1714 /**
1715 * Atomically updates Java variable to {@code x} if it is currently
1716 * holding {@code expected}.
1717 *
1718 * <p>This operation has memory semantics of a {@code volatile} read
1719 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1720 *
1721 * @return {@code true} if successful
1722 */
1723 @IntrinsicCandidate
1724 public final native boolean compareAndSetReference(Object o, long offset,
1725 Object expected,
1726 Object x);
1727
1728 @ForceInline
1729 private final boolean isValueObject(Class<?> valueType, Object o) {
1730 return ValueClass.isValueObjectCompatible(valueType)
1731 && o != null && o.getClass().isValue();
1732 }
1733
1734 /*
1735 * For value type, CAS should do substitutability test as opposed
1736 * to two pointers comparison.
1737 */
1738 @ForceInline
1739 public final <V> boolean compareAndSetReference(Object o, long offset,
1740 Class<?> type,
1741 V expected,
1742 V x) {
1743 if (isValueObject(type, expected)) {
1744 while (true) {
1745 Object witness = getReferenceVolatile(o, offset);
1746 if (witness != expected) {
1747 return false;
1748 }
1749 if (compareAndSetReference(o, offset, witness, x)) {
1750 return true;
1751 }
1752 }
1753 } else {
1754 return compareAndSetReference(o, offset, expected, x);
1755 }
1756 }
1757
1758 @ForceInline
1759 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1760 int layout,
1761 Class<?> valueType,
1762 V expected,
1763 V x) {
1764 Object[] array = newSpecialArray(valueType, 2, layout);
1765 return compareAndSetFlatValueAsBytes(array, o, offset, layout, valueType, expected, x);
1766 }
1767
1768 @IntrinsicCandidate
1769 public final native Object compareAndExchangeReference(Object o, long offset,
1770 Object expected,
1771 Object x);
1772
1773 @ForceInline
1774 public final <V> Object compareAndExchangeReference(Object o, long offset,
1775 Class<?> valueType,
1776 V expected,
1777 V x) {
1778 if (isValueObject(valueType, expected)) {
1779 while (true) {
1780 Object witness = getReferenceVolatile(o, offset);
1781 if (witness != expected) {
1782 return witness;
1783 }
1784 if (compareAndSetReference(o, offset, witness, x)) {
1785 return witness;
1786 }
1787 }
1788 } else {
1789 return compareAndExchangeReference(o, offset, expected, x);
1790 }
1791 }
1792
1793 @ForceInline
1794 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1795 int layout,
1796 Class<?> valueType,
1797 V expected,
1798 V x) {
1799 Object[] array = newSpecialArray(valueType, 2, layout);
1800 compareAndSetFlatValueAsBytes(array, o, offset, layout, valueType, expected, x);
1801 return array[0];
1802 }
1803
1804 @IntrinsicCandidate
1805 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1806 Object expected,
1807 Object x) {
1808 return compareAndExchangeReference(o, offset, expected, x);
1809 }
1810
1811 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1812 Class<?> valueType,
1813 V expected,
1814 V x) {
1815 return compareAndExchangeReference(o, offset, valueType, expected, x);
1816 }
1817
1818 @ForceInline
1819 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1820 int layout,
1821 Class<?> valueType,
1822 V expected,
1823 V x) {
1824 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1825 }
1826
1827 @IntrinsicCandidate
1828 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1829 Object expected,
1830 Object x) {
1831 return compareAndExchangeReference(o, offset, expected, x);
1832 }
1833
1834 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1835 Class<?> valueType,
1836 V expected,
1837 V x) {
1838 return compareAndExchangeReference(o, offset, valueType, expected, x);
1839 }
1840
1841 @ForceInline
1842 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1843 int layout,
1844 Class<?> valueType,
1845 V expected,
1846 V x) {
1847 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1848 }
1849
1850 @IntrinsicCandidate
1851 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1852 Object expected,
1853 Object x) {
1854 return compareAndSetReference(o, offset, expected, x);
1855 }
1856
1857 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1858 Class<?> valueType,
1859 V expected,
1860 V x) {
1861 if (isValueObject(valueType, expected)) {
1862 // Reusing a stronger operation for now, still compliant
1863 return compareAndSetReference(o, offset, valueType, expected, x);
1864 } else {
1865 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1866 }
1867 }
1868
1869 @ForceInline
1870 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1871 int layout,
1872 Class<?> valueType,
1873 V expected,
1874 V x) {
1875 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1876 }
1877
1878 @IntrinsicCandidate
1879 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1880 Object expected,
1881 Object x) {
1882 return compareAndSetReference(o, offset, expected, x);
1883 }
1884
1885 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1886 Class<?> valueType,
1887 V expected,
1888 V x) {
1889 if (isValueObject(valueType, expected)) {
1890 // Reusing a stronger operation for now, still compliant
1891 return compareAndSetReference(o, offset, valueType, expected, x);
1892 } else {
1893 return weakCompareAndSetReferenceAcquire(o, offset, expected, x);
1894 }
1895 }
1896
1897 @ForceInline
1898 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1899 int layout,
1900 Class<?> valueType,
1901 V expected,
1902 V x) {
1903 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1904 }
1905
1906 @IntrinsicCandidate
1907 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1908 Object expected,
1909 Object x) {
1910 return compareAndSetReference(o, offset, expected, x);
1911 }
1912
1913 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1914 Class<?> valueType,
1915 V expected,
1916 V x) {
1917 if (isValueObject(valueType, expected)) {
1918 // Reusing a stronger operation for now, still compliant
1919 return compareAndSetReference(o, offset, valueType, expected, x);
1920 } else {
1921 return weakCompareAndSetReferenceRelease(o, offset, expected, x);
1922 }
1923 }
1924
1925 @ForceInline
1926 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1927 int layout,
1928 Class<?> valueType,
1929 V expected,
1930 V x) {
1931 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1932 }
1933
1934 @IntrinsicCandidate
1935 public final boolean weakCompareAndSetReference(Object o, long offset,
1936 Object expected,
1937 Object x) {
1938 return compareAndSetReference(o, offset, expected, x);
1939 }
1940
1941 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1942 Class<?> valueType,
1943 V expected,
1944 V x) {
1945 if (isValueObject(valueType, expected)) {
1946 // Reusing a stronger operation for now, still compliant
1947 return compareAndSetReference(o, offset, valueType, expected, x);
1948 } else {
1949 return weakCompareAndSetReference(o, offset, expected, x);
1950 }
1951 }
1952
1953 @ForceInline
1954 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1955 int layout,
1956 Class<?> valueType,
1957 V expected,
1958 V x) {
1959 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1960 }
1961
1962 /**
1963 * Atomically updates Java variable to {@code x} if it is currently
1964 * holding {@code expected}.
1965 *
1966 * <p>This operation has memory semantics of a {@code volatile} read
1967 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1968 *
1969 * @return {@code true} if successful
1970 */
1971 @IntrinsicCandidate
1972 public final native boolean compareAndSetInt(Object o, long offset,
1973 int expected,
1974 int x);
1975
1976 @IntrinsicCandidate
1977 public final native int compareAndExchangeInt(Object o, long offset,
1978 int expected,
1979 int x);
1980
1981 @IntrinsicCandidate
1982 public final int compareAndExchangeIntAcquire(Object o, long offset,
1983 int expected,
1984 int x) {
1985 return compareAndExchangeInt(o, offset, expected, x);
1986 }
1987
1988 @IntrinsicCandidate
1989 public final int compareAndExchangeIntRelease(Object o, long offset,
1990 int expected,
1991 int x) {
1992 return compareAndExchangeInt(o, offset, expected, x);
1993 }
1994
1995 @IntrinsicCandidate
1996 public final boolean weakCompareAndSetIntPlain(Object o, long offset,
1997 int expected,
1998 int x) {
1999 return compareAndSetInt(o, offset, expected, x);
2000 }
2001
2002 @IntrinsicCandidate
2003 public final boolean weakCompareAndSetIntAcquire(Object o, long offset,
2004 int expected,
2005 int x) {
2006 return compareAndSetInt(o, offset, expected, x);
2007 }
2008
2009 @IntrinsicCandidate
2010 public final boolean weakCompareAndSetIntRelease(Object o, long offset,
2011 int expected,
2012 int x) {
2013 return compareAndSetInt(o, offset, expected, x);
2014 }
2015
2016 @IntrinsicCandidate
2017 public final boolean weakCompareAndSetInt(Object o, long offset,
2018 int expected,
2019 int x) {
2020 return compareAndSetInt(o, offset, expected, x);
2021 }
2022
2023 @IntrinsicCandidate
2024 public final byte compareAndExchangeByte(Object o, long offset,
2025 byte expected,
2026 byte x) {
2027 long wordOffset = offset & ~3;
2028 int shift = (int) (offset & 3) << 3;
2029 if (BIG_ENDIAN) {
2030 shift = 24 - shift;
2031 }
2032 int mask = 0xFF << shift;
2033 int maskedExpected = (expected & 0xFF) << shift;
2034 int maskedX = (x & 0xFF) << shift;
2035 int fullWord;
2036 do {
2037 fullWord = getIntVolatile(o, wordOffset);
2038 if ((fullWord & mask) != maskedExpected)
2039 return (byte) ((fullWord & mask) >> shift);
2040 } while (!weakCompareAndSetInt(o, wordOffset,
2041 fullWord, (fullWord & ~mask) | maskedX));
2042 return expected;
2043 }
2044
2045 @IntrinsicCandidate
2046 public final boolean compareAndSetByte(Object o, long offset,
2047 byte expected,
2048 byte x) {
2049 return compareAndExchangeByte(o, offset, expected, x) == expected;
2050 }
2051
2052 @IntrinsicCandidate
2053 public final boolean weakCompareAndSetByte(Object o, long offset,
2054 byte expected,
2055 byte x) {
2056 return compareAndSetByte(o, offset, expected, x);
2057 }
2058
2059 @IntrinsicCandidate
2060 public final boolean weakCompareAndSetByteAcquire(Object o, long offset,
2061 byte expected,
2062 byte x) {
2063 return weakCompareAndSetByte(o, offset, expected, x);
2064 }
2065
2066 @IntrinsicCandidate
2067 public final boolean weakCompareAndSetByteRelease(Object o, long offset,
2068 byte expected,
2069 byte x) {
2070 return weakCompareAndSetByte(o, offset, expected, x);
2071 }
2072
2073 @IntrinsicCandidate
2074 public final boolean weakCompareAndSetBytePlain(Object o, long offset,
2075 byte expected,
2076 byte x) {
2077 return weakCompareAndSetByte(o, offset, expected, x);
2078 }
2079
2080 @IntrinsicCandidate
2081 public final byte compareAndExchangeByteAcquire(Object o, long offset,
2082 byte expected,
2083 byte x) {
2084 return compareAndExchangeByte(o, offset, expected, x);
2085 }
2086
2087 @IntrinsicCandidate
2088 public final byte compareAndExchangeByteRelease(Object o, long offset,
2089 byte expected,
2090 byte x) {
2091 return compareAndExchangeByte(o, offset, expected, x);
2092 }
2093
2094 @IntrinsicCandidate
2095 public final short compareAndExchangeShort(Object o, long offset,
2096 short expected,
2097 short x) {
2098 if ((offset & 3) == 3) {
2099 throw new IllegalArgumentException("Update spans the word, not supported");
2100 }
2101 long wordOffset = offset & ~3;
2102 int shift = (int) (offset & 3) << 3;
2103 if (BIG_ENDIAN) {
2104 shift = 16 - shift;
2105 }
2106 int mask = 0xFFFF << shift;
2107 int maskedExpected = (expected & 0xFFFF) << shift;
2108 int maskedX = (x & 0xFFFF) << shift;
2109 int fullWord;
2110 do {
2111 fullWord = getIntVolatile(o, wordOffset);
2112 if ((fullWord & mask) != maskedExpected) {
2113 return (short) ((fullWord & mask) >> shift);
2114 }
2115 } while (!weakCompareAndSetInt(o, wordOffset,
2116 fullWord, (fullWord & ~mask) | maskedX));
2117 return expected;
2118 }
2119
2120 @IntrinsicCandidate
2121 public final boolean compareAndSetShort(Object o, long offset,
2122 short expected,
2123 short x) {
2124 return compareAndExchangeShort(o, offset, expected, x) == expected;
2125 }
2126
2127 @IntrinsicCandidate
2128 public final boolean weakCompareAndSetShort(Object o, long offset,
2129 short expected,
2130 short x) {
2131 return compareAndSetShort(o, offset, expected, x);
2132 }
2133
2134 @IntrinsicCandidate
2135 public final boolean weakCompareAndSetShortAcquire(Object o, long offset,
2136 short expected,
2137 short x) {
2138 return weakCompareAndSetShort(o, offset, expected, x);
2139 }
2140
2141 @IntrinsicCandidate
2142 public final boolean weakCompareAndSetShortRelease(Object o, long offset,
2143 short expected,
2144 short x) {
2145 return weakCompareAndSetShort(o, offset, expected, x);
2146 }
2147
2148 @IntrinsicCandidate
2149 public final boolean weakCompareAndSetShortPlain(Object o, long offset,
2150 short expected,
2151 short x) {
2152 return weakCompareAndSetShort(o, offset, expected, x);
2153 }
2154
2155
2156 @IntrinsicCandidate
2157 public final short compareAndExchangeShortAcquire(Object o, long offset,
2158 short expected,
2159 short x) {
2160 return compareAndExchangeShort(o, offset, expected, x);
2161 }
2162
2163 @IntrinsicCandidate
2164 public final short compareAndExchangeShortRelease(Object o, long offset,
2165 short expected,
2166 short x) {
2167 return compareAndExchangeShort(o, offset, expected, x);
2168 }
2169
2170 @ForceInline
2171 private char s2c(short s) {
2172 return (char) s;
2173 }
2174
2175 @ForceInline
2176 private short c2s(char s) {
2177 return (short) s;
2178 }
2179
2180 @ForceInline
2181 public final boolean compareAndSetChar(Object o, long offset,
2182 char expected,
2183 char x) {
2184 return compareAndSetShort(o, offset, c2s(expected), c2s(x));
2185 }
2186
2187 @ForceInline
2188 public final char compareAndExchangeChar(Object o, long offset,
2189 char expected,
2190 char x) {
2191 return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x)));
2192 }
2193
2194 @ForceInline
2195 public final char compareAndExchangeCharAcquire(Object o, long offset,
2196 char expected,
2197 char x) {
2198 return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x)));
2199 }
2200
2201 @ForceInline
2202 public final char compareAndExchangeCharRelease(Object o, long offset,
2203 char expected,
2204 char x) {
2205 return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x)));
2206 }
2207
2208 @ForceInline
2209 public final boolean weakCompareAndSetChar(Object o, long offset,
2210 char expected,
2211 char x) {
2212 return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x));
2213 }
2214
2215 @ForceInline
2216 public final boolean weakCompareAndSetCharAcquire(Object o, long offset,
2217 char expected,
2218 char x) {
2219 return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x));
2220 }
2221
2222 @ForceInline
2223 public final boolean weakCompareAndSetCharRelease(Object o, long offset,
2224 char expected,
2225 char x) {
2226 return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x));
2227 }
2228
2229 @ForceInline
2230 public final boolean weakCompareAndSetCharPlain(Object o, long offset,
2231 char expected,
2232 char x) {
2233 return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x));
2234 }
2235
2236 /**
2237 * The JVM converts integral values to boolean values using two
2238 * different conventions, byte testing against zero and truncation
2239 * to least-significant bit.
2240 *
2241 * <p>The JNI documents specify that, at least for returning
2242 * values from native methods, a Java boolean value is converted
2243 * to the value-set 0..1 by first truncating to a byte (0..255 or
2244 * maybe -128..127) and then testing against zero.
2245 *
2246 * <p>Java booleans in the heap are also stored in bytes, but are
2247 * strongly normalized to the value-set 0..1 (i.e., they are
2248 * truncated to the least-significant bit).
2249 *
2250 * <p>The main reason for having different conventions for
2251 * conversion is performance: Truncation to the least-significant
2252 * bit can be usually implemented with fewer (machine)
2253 * instructions than byte testing against zero.
2254 *
2255 * <p>A number of Unsafe methods load boolean values as bytes,
2256 * truncate them to the least-significant bit, and then test
2257 * against zero. It is uniformly performed for both Java heap
2258 * and non-Java heap accesses.
2259 *
2260 * @param b the byte to be converted to boolean
2261 * @return the result of the conversion
2262 */
2263 @ForceInline
2264 private boolean byte2bool(byte b) {
2265 return (b & 1) != 0;
2266 }
2267
2268 /**
2269 * Convert a boolean value to a byte. The return value is strongly
2270 * normalized to the value-set 0..1 (i.e., the value is truncated
2271 * to the least-significant bit). See {@link #byte2bool(byte)} for
2272 * more details on conversion conventions.
2273 *
2274 * @param b the boolean to be converted to byte (and then normalized)
2275 * @return the result of the conversion
2276 */
2277 @ForceInline
2278 private byte bool2byte(boolean b) {
2279 return b ? (byte)1 : (byte)0;
2280 }
2281
2282 @ForceInline
2283 public final boolean compareAndSetBoolean(Object o, long offset,
2284 boolean expected,
2285 boolean x) {
2286 return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2287 }
2288
2289 @ForceInline
2290 public final boolean compareAndExchangeBoolean(Object o, long offset,
2291 boolean expected,
2292 boolean x) {
2293 return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x)));
2294 }
2295
2296 @ForceInline
2297 public final boolean compareAndExchangeBooleanAcquire(Object o, long offset,
2298 boolean expected,
2299 boolean x) {
2300 return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x)));
2301 }
2302
2303 @ForceInline
2304 public final boolean compareAndExchangeBooleanRelease(Object o, long offset,
2305 boolean expected,
2306 boolean x) {
2307 return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x)));
2308 }
2309
2310 @ForceInline
2311 public final boolean weakCompareAndSetBoolean(Object o, long offset,
2312 boolean expected,
2313 boolean x) {
2314 return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2315 }
2316
2317 @ForceInline
2318 public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset,
2319 boolean expected,
2320 boolean x) {
2321 return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x));
2322 }
2323
2324 @ForceInline
2325 public final boolean weakCompareAndSetBooleanRelease(Object o, long offset,
2326 boolean expected,
2327 boolean x) {
2328 return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x));
2329 }
2330
2331 @ForceInline
2332 public final boolean weakCompareAndSetBooleanPlain(Object o, long offset,
2333 boolean expected,
2334 boolean x) {
2335 return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x));
2336 }
2337
2338 /**
2339 * Atomically updates Java variable to {@code x} if it is currently
2340 * holding {@code expected}.
2341 *
2342 * <p>This operation has memory semantics of a {@code volatile} read
2343 * and write. Corresponds to C11 atomic_compare_exchange_strong.
2344 *
2345 * @return {@code true} if successful
2346 */
2347 @ForceInline
2348 public final boolean compareAndSetFloat(Object o, long offset,
2349 float expected,
2350 float x) {
2351 return compareAndSetInt(o, offset,
2352 Float.floatToRawIntBits(expected),
2353 Float.floatToRawIntBits(x));
2354 }
2355
2356 @ForceInline
2357 public final float compareAndExchangeFloat(Object o, long offset,
2358 float expected,
2359 float x) {
2360 int w = compareAndExchangeInt(o, offset,
2361 Float.floatToRawIntBits(expected),
2362 Float.floatToRawIntBits(x));
2363 return Float.intBitsToFloat(w);
2364 }
2365
2366 @ForceInline
2367 public final float compareAndExchangeFloatAcquire(Object o, long offset,
2368 float expected,
2369 float x) {
2370 int w = compareAndExchangeIntAcquire(o, offset,
2371 Float.floatToRawIntBits(expected),
2372 Float.floatToRawIntBits(x));
2373 return Float.intBitsToFloat(w);
2374 }
2375
2376 @ForceInline
2377 public final float compareAndExchangeFloatRelease(Object o, long offset,
2378 float expected,
2379 float x) {
2380 int w = compareAndExchangeIntRelease(o, offset,
2381 Float.floatToRawIntBits(expected),
2382 Float.floatToRawIntBits(x));
2383 return Float.intBitsToFloat(w);
2384 }
2385
2386 @ForceInline
2387 public final boolean weakCompareAndSetFloatPlain(Object o, long offset,
2388 float expected,
2389 float x) {
2390 return weakCompareAndSetIntPlain(o, offset,
2391 Float.floatToRawIntBits(expected),
2392 Float.floatToRawIntBits(x));
2393 }
2394
2395 @ForceInline
2396 public final boolean weakCompareAndSetFloatAcquire(Object o, long offset,
2397 float expected,
2398 float x) {
2399 return weakCompareAndSetIntAcquire(o, offset,
2400 Float.floatToRawIntBits(expected),
2401 Float.floatToRawIntBits(x));
2402 }
2403
2404 @ForceInline
2405 public final boolean weakCompareAndSetFloatRelease(Object o, long offset,
2406 float expected,
2407 float x) {
2408 return weakCompareAndSetIntRelease(o, offset,
2409 Float.floatToRawIntBits(expected),
2410 Float.floatToRawIntBits(x));
2411 }
2412
2413 @ForceInline
2414 public final boolean weakCompareAndSetFloat(Object o, long offset,
2415 float expected,
2416 float x) {
2417 return weakCompareAndSetInt(o, offset,
2418 Float.floatToRawIntBits(expected),
2419 Float.floatToRawIntBits(x));
2420 }
2421
2422 /**
2423 * Atomically updates Java variable to {@code x} if it is currently
2424 * holding {@code expected}.
2425 *
2426 * <p>This operation has memory semantics of a {@code volatile} read
2427 * and write. Corresponds to C11 atomic_compare_exchange_strong.
2428 *
2429 * @return {@code true} if successful
2430 */
2431 @ForceInline
2432 public final boolean compareAndSetDouble(Object o, long offset,
2433 double expected,
2434 double x) {
2435 return compareAndSetLong(o, offset,
2436 Double.doubleToRawLongBits(expected),
2437 Double.doubleToRawLongBits(x));
2438 }
2439
2440 @ForceInline
2441 public final double compareAndExchangeDouble(Object o, long offset,
2442 double expected,
2443 double x) {
2444 long w = compareAndExchangeLong(o, offset,
2445 Double.doubleToRawLongBits(expected),
2446 Double.doubleToRawLongBits(x));
2447 return Double.longBitsToDouble(w);
2448 }
2449
2450 @ForceInline
2451 public final double compareAndExchangeDoubleAcquire(Object o, long offset,
2452 double expected,
2453 double x) {
2454 long w = compareAndExchangeLongAcquire(o, offset,
2455 Double.doubleToRawLongBits(expected),
2456 Double.doubleToRawLongBits(x));
2457 return Double.longBitsToDouble(w);
2458 }
2459
2460 @ForceInline
2461 public final double compareAndExchangeDoubleRelease(Object o, long offset,
2462 double expected,
2463 double x) {
2464 long w = compareAndExchangeLongRelease(o, offset,
2465 Double.doubleToRawLongBits(expected),
2466 Double.doubleToRawLongBits(x));
2467 return Double.longBitsToDouble(w);
2468 }
2469
2470 @ForceInline
2471 public final boolean weakCompareAndSetDoublePlain(Object o, long offset,
2472 double expected,
2473 double x) {
2474 return weakCompareAndSetLongPlain(o, offset,
2475 Double.doubleToRawLongBits(expected),
2476 Double.doubleToRawLongBits(x));
2477 }
2478
2479 @ForceInline
2480 public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset,
2481 double expected,
2482 double x) {
2483 return weakCompareAndSetLongAcquire(o, offset,
2484 Double.doubleToRawLongBits(expected),
2485 Double.doubleToRawLongBits(x));
2486 }
2487
2488 @ForceInline
2489 public final boolean weakCompareAndSetDoubleRelease(Object o, long offset,
2490 double expected,
2491 double x) {
2492 return weakCompareAndSetLongRelease(o, offset,
2493 Double.doubleToRawLongBits(expected),
2494 Double.doubleToRawLongBits(x));
2495 }
2496
2497 @ForceInline
2498 public final boolean weakCompareAndSetDouble(Object o, long offset,
2499 double expected,
2500 double x) {
2501 return weakCompareAndSetLong(o, offset,
2502 Double.doubleToRawLongBits(expected),
2503 Double.doubleToRawLongBits(x));
2504 }
2505
2506 /**
2507 * Atomically updates Java variable to {@code x} if it is currently
2508 * holding {@code expected}.
2509 *
2510 * <p>This operation has memory semantics of a {@code volatile} read
2511 * and write. Corresponds to C11 atomic_compare_exchange_strong.
2512 *
2513 * @return {@code true} if successful
2514 */
2515 @IntrinsicCandidate
2516 public final native boolean compareAndSetLong(Object o, long offset,
2517 long expected,
2518 long x);
2519
2520 @IntrinsicCandidate
2521 public final native long compareAndExchangeLong(Object o, long offset,
2522 long expected,
2523 long x);
2524
2525 @IntrinsicCandidate
2526 public final long compareAndExchangeLongAcquire(Object o, long offset,
2527 long expected,
2528 long x) {
2529 return compareAndExchangeLong(o, offset, expected, x);
2530 }
2531
2532 @IntrinsicCandidate
2533 public final long compareAndExchangeLongRelease(Object o, long offset,
2534 long expected,
2535 long x) {
2536 return compareAndExchangeLong(o, offset, expected, x);
2537 }
2538
2539 @IntrinsicCandidate
2540 public final boolean weakCompareAndSetLongPlain(Object o, long offset,
2541 long expected,
2542 long x) {
2543 return compareAndSetLong(o, offset, expected, x);
2544 }
2545
2546 @IntrinsicCandidate
2547 public final boolean weakCompareAndSetLongAcquire(Object o, long offset,
2548 long expected,
2549 long x) {
2550 return compareAndSetLong(o, offset, expected, x);
2551 }
2552
2553 @IntrinsicCandidate
2554 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2555 long expected,
2556 long x) {
2557 return compareAndSetLong(o, offset, expected, x);
2558 }
2559
2560 @IntrinsicCandidate
2561 public final boolean weakCompareAndSetLong(Object o, long offset,
2562 long expected,
2563 long x) {
2564 return compareAndSetLong(o, offset, expected, x);
2565 }
2566
2567 /**
2568 * Fetches a reference value from a given Java variable, with volatile
2569 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2570 */
2571 @IntrinsicCandidate
2572 public native Object getReferenceVolatile(Object o, long offset);
2573
2574 @ForceInline
2575 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2576 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2577 Object res = getFlatValue(o, offset, layout, valueType);
2578 fullFence();
2579 return res;
2580 }
2581
2582 /**
2583 * Stores a reference value into a given Java variable, with
2584 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2585 */
2586 @IntrinsicCandidate
2587 public native void putReferenceVolatile(Object o, long offset, Object x);
2588
2589 @ForceInline
2590 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2591 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2592 putFlatValueRelease(o, offset, layout, valueType, x);
2593 fullFence();
2594 }
2595
2596 /** Volatile version of {@link #getInt(Object, long)} */
2597 @IntrinsicCandidate
2598 public native int getIntVolatile(Object o, long offset);
2599
2600 /** Volatile version of {@link #putInt(Object, long, int)} */
2601 @IntrinsicCandidate
2602 public native void putIntVolatile(Object o, long offset, int x);
2603
2604 /** Volatile version of {@link #getBoolean(Object, long)} */
2605 @IntrinsicCandidate
2606 public native boolean getBooleanVolatile(Object o, long offset);
2607
2608 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2609 @IntrinsicCandidate
2610 public native void putBooleanVolatile(Object o, long offset, boolean x);
2611
2612 /** Volatile version of {@link #getByte(Object, long)} */
2613 @IntrinsicCandidate
2614 public native byte getByteVolatile(Object o, long offset);
2615
2616 /** Volatile version of {@link #putByte(Object, long, byte)} */
2617 @IntrinsicCandidate
2618 public native void putByteVolatile(Object o, long offset, byte x);
2619
2620 /** Volatile version of {@link #getShort(Object, long)} */
2621 @IntrinsicCandidate
2622 public native short getShortVolatile(Object o, long offset);
2623
2624 /** Volatile version of {@link #putShort(Object, long, short)} */
2625 @IntrinsicCandidate
2626 public native void putShortVolatile(Object o, long offset, short x);
2627
2628 /** Volatile version of {@link #getChar(Object, long)} */
2629 @IntrinsicCandidate
2630 public native char getCharVolatile(Object o, long offset);
2631
2632 /** Volatile version of {@link #putChar(Object, long, char)} */
2633 @IntrinsicCandidate
2634 public native void putCharVolatile(Object o, long offset, char x);
2635
2636 /** Volatile version of {@link #getLong(Object, long)} */
2637 @IntrinsicCandidate
2638 public native long getLongVolatile(Object o, long offset);
2639
2640 /** Volatile version of {@link #putLong(Object, long, long)} */
2641 @IntrinsicCandidate
2642 public native void putLongVolatile(Object o, long offset, long x);
2643
2644 /** Volatile version of {@link #getFloat(Object, long)} */
2645 @IntrinsicCandidate
2646 public native float getFloatVolatile(Object o, long offset);
2647
2648 /** Volatile version of {@link #putFloat(Object, long, float)} */
2649 @IntrinsicCandidate
2650 public native void putFloatVolatile(Object o, long offset, float x);
2651
2652 /** Volatile version of {@link #getDouble(Object, long)} */
2653 @IntrinsicCandidate
2654 public native double getDoubleVolatile(Object o, long offset);
2655
2656 /** Volatile version of {@link #putDouble(Object, long, double)} */
2657 @IntrinsicCandidate
2658 public native void putDoubleVolatile(Object o, long offset, double x);
2659
2660
2661
2662 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2663 @IntrinsicCandidate
2664 public final Object getReferenceAcquire(Object o, long offset) {
2665 return getReferenceVolatile(o, offset);
2666 }
2667
2668 @ForceInline
2669 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2670 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2671 Object res = getFlatValue(o, offset, layout, valueType);
2672 loadFence();
2673 return res;
2674 }
2675
2676 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2677 @IntrinsicCandidate
2678 public final boolean getBooleanAcquire(Object o, long offset) {
2679 return getBooleanVolatile(o, offset);
2680 }
2681
2682 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2683 @IntrinsicCandidate
2684 public final byte getByteAcquire(Object o, long offset) {
2685 return getByteVolatile(o, offset);
2686 }
2687
2688 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2689 @IntrinsicCandidate
2690 public final short getShortAcquire(Object o, long offset) {
2691 return getShortVolatile(o, offset);
2692 }
2693
2694 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2695 @IntrinsicCandidate
2696 public final char getCharAcquire(Object o, long offset) {
2697 return getCharVolatile(o, offset);
2698 }
2699
2700 /** Acquire version of {@link #getIntVolatile(Object, long)} */
2701 @IntrinsicCandidate
2702 public final int getIntAcquire(Object o, long offset) {
2703 return getIntVolatile(o, offset);
2704 }
2705
2706 /** Acquire version of {@link #getFloatVolatile(Object, long)} */
2707 @IntrinsicCandidate
2708 public final float getFloatAcquire(Object o, long offset) {
2709 return getFloatVolatile(o, offset);
2710 }
2711
2712 /** Acquire version of {@link #getLongVolatile(Object, long)} */
2713 @IntrinsicCandidate
2714 public final long getLongAcquire(Object o, long offset) {
2715 return getLongVolatile(o, offset);
2716 }
2717
2718 /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
2719 @IntrinsicCandidate
2720 public final double getDoubleAcquire(Object o, long offset) {
2721 return getDoubleVolatile(o, offset);
2722 }
2723
2724 /*
2725 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2726 * that do not guarantee immediate visibility of the store to
2727 * other threads. This method is generally only useful if the
2728 * underlying field is a Java volatile (or if an array cell, one
2729 * that is otherwise only accessed using volatile accesses).
2730 *
2731 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2732 */
2733
2734 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2735 @IntrinsicCandidate
2736 public final void putReferenceRelease(Object o, long offset, Object x) {
2737 putReferenceVolatile(o, offset, x);
2738 }
2739
2740 @ForceInline
2741 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2742 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2743 storeFence();
2744 putFlatValue(o, offset, layout, valueType, x);
2745 }
2746
2747 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2748 @IntrinsicCandidate
2749 public final void putBooleanRelease(Object o, long offset, boolean x) {
2750 putBooleanVolatile(o, offset, x);
2751 }
2752
2753 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2754 @IntrinsicCandidate
2755 public final void putByteRelease(Object o, long offset, byte x) {
2756 putByteVolatile(o, offset, x);
2757 }
2758
2759 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2760 @IntrinsicCandidate
2761 public final void putShortRelease(Object o, long offset, short x) {
2762 putShortVolatile(o, offset, x);
2763 }
2764
2765 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2766 @IntrinsicCandidate
2767 public final void putCharRelease(Object o, long offset, char x) {
2768 putCharVolatile(o, offset, x);
2769 }
2770
2771 /** Release version of {@link #putIntVolatile(Object, long, int)} */
2772 @IntrinsicCandidate
2773 public final void putIntRelease(Object o, long offset, int x) {
2774 putIntVolatile(o, offset, x);
2775 }
2776
2777 /** Release version of {@link #putFloatVolatile(Object, long, float)} */
2778 @IntrinsicCandidate
2779 public final void putFloatRelease(Object o, long offset, float x) {
2780 putFloatVolatile(o, offset, x);
2781 }
2782
2783 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2784 @IntrinsicCandidate
2785 public final void putLongRelease(Object o, long offset, long x) {
2786 putLongVolatile(o, offset, x);
2787 }
2788
2789 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2790 @IntrinsicCandidate
2791 public final void putDoubleRelease(Object o, long offset, double x) {
2792 putDoubleVolatile(o, offset, x);
2793 }
2794
2795 // ------------------------------ Opaque --------------------------------------
2796
2797 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2798 @IntrinsicCandidate
2799 public final Object getReferenceOpaque(Object o, long offset) {
2800 return getReferenceVolatile(o, offset);
2801 }
2802
2803 @ForceInline
2804 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2805 // this is stronger than opaque semantics
2806 return getFlatValueAcquire(o, offset, layout, valueType);
2807 }
2808
2809 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2810 @IntrinsicCandidate
2811 public final boolean getBooleanOpaque(Object o, long offset) {
2812 return getBooleanVolatile(o, offset);
2813 }
2814
2815 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2816 @IntrinsicCandidate
2817 public final byte getByteOpaque(Object o, long offset) {
2818 return getByteVolatile(o, offset);
2819 }
2820
2821 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2822 @IntrinsicCandidate
2823 public final short getShortOpaque(Object o, long offset) {
2824 return getShortVolatile(o, offset);
2825 }
2826
2827 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2828 @IntrinsicCandidate
2829 public final char getCharOpaque(Object o, long offset) {
2830 return getCharVolatile(o, offset);
2831 }
2832
2833 /** Opaque version of {@link #getIntVolatile(Object, long)} */
2834 @IntrinsicCandidate
2835 public final int getIntOpaque(Object o, long offset) {
2836 return getIntVolatile(o, offset);
2837 }
2838
2839 /** Opaque version of {@link #getFloatVolatile(Object, long)} */
2840 @IntrinsicCandidate
2841 public final float getFloatOpaque(Object o, long offset) {
2842 return getFloatVolatile(o, offset);
2843 }
2844
2845 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2846 @IntrinsicCandidate
2847 public final long getLongOpaque(Object o, long offset) {
2848 return getLongVolatile(o, offset);
2849 }
2850
2851 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2852 @IntrinsicCandidate
2853 public final double getDoubleOpaque(Object o, long offset) {
2854 return getDoubleVolatile(o, offset);
2855 }
2856
2857 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2858 @IntrinsicCandidate
2859 public final void putReferenceOpaque(Object o, long offset, Object x) {
2860 putReferenceVolatile(o, offset, x);
2861 }
2862
2863 @ForceInline
2864 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2865 // this is stronger than opaque semantics
2866 putFlatValueRelease(o, offset, layout, valueType, x);
2867 }
2868
2869 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2870 @IntrinsicCandidate
2871 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2872 putBooleanVolatile(o, offset, x);
2873 }
2874
2875 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2876 @IntrinsicCandidate
2877 public final void putByteOpaque(Object o, long offset, byte x) {
2878 putByteVolatile(o, offset, x);
2879 }
2880
2881 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2882 @IntrinsicCandidate
2883 public final void putShortOpaque(Object o, long offset, short x) {
2884 putShortVolatile(o, offset, x);
2885 }
2886
2887 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2888 @IntrinsicCandidate
2889 public final void putCharOpaque(Object o, long offset, char x) {
2890 putCharVolatile(o, offset, x);
2891 }
2892
2893 /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
2894 @IntrinsicCandidate
2895 public final void putIntOpaque(Object o, long offset, int x) {
2896 putIntVolatile(o, offset, x);
2897 }
2898
2899 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2900 @IntrinsicCandidate
2901 public final void putFloatOpaque(Object o, long offset, float x) {
2902 putFloatVolatile(o, offset, x);
2903 }
2904
2905 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2906 @IntrinsicCandidate
2907 public final void putLongOpaque(Object o, long offset, long x) {
2908 putLongVolatile(o, offset, x);
2909 }
2910
2911 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2912 @IntrinsicCandidate
2913 public final void putDoubleOpaque(Object o, long offset, double x) {
2914 putDoubleVolatile(o, offset, x);
2915 }
2916
2917 @ForceInline
2918 private boolean compareAndSetFlatValueAsBytes(Object[] array, Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2919 // We can convert between a value object and a binary value (of suitable size) using array elements.
2920 // This only works if the payload contains no oops (see VarHandles::isAtomicFlat).
2921 // Thus, we can implement the CAS with a plain numeric CAS.
2922
2923 // array[0]: witness (put as binary, get as object), at base
2924 // array[1]: x (put as object, get as binary), at base + scale
2925 // When witness == expected, the witness binary may be different from the expected binary.
2926 // This happens when compiler does not zero unused positions in the witness.
2927 // So we must obtain the witness binary and use it as expected binary for the numeric CAS.
2928 long base = arrayInstanceBaseOffset(array);
2929 int scale = arrayInstanceIndexScale(array);
2930 putFlatValue(array, base + scale, layout, valueType, x); // put x as object
2931 switch (scale) {
2932 case 1: {
2933 do {
2934 byte witnessByte = getByteVolatile(o, offset);
2935 putByte(array, base, witnessByte); // put witness as binary
2936 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2937 if (witness != expected) {
2938 return false;
2939 }
2940 byte xByte = getByte(array, base + scale); // get x as binary
2941 if (compareAndSetByte(o, offset, witnessByte, xByte)) {
2942 return true;
2943 }
2944 } while (true);
2945 }
2946 case 2: {
2947 do {
2948 short witnessShort = getShortVolatile(o, offset);
2949 putShort(array, base, witnessShort); // put witness as binary
2950 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2951 if (witness != expected) {
2952 return false;
2953 }
2954 short xShort = getShort(array, base + scale); // get x as binary
2955 if (compareAndSetShort(o, offset, witnessShort, xShort)) {
2956 return true;
2957 }
2958 } while (true);
2959 }
2960 case 4: {
2961 do {
2962 int witnessInt = getIntVolatile(o, offset);
2963 putInt(array, base, witnessInt); // put witness as binary
2964 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2965 if (witness != expected) {
2966 return false;
2967 }
2968 int xInt = getInt(array, base + scale); // get x as binary
2969 if (compareAndSetInt(o, offset, witnessInt, xInt)) {
2970 return true;
2971 }
2972 } while (true);
2973 }
2974 case 8: {
2975 do {
2976 long witnessLong = getLongVolatile(o, offset);
2977 putLong(array, base, witnessLong); // put witness as binary
2978 Object witness = getFlatValue(array, base, layout, valueType);
2979 if (witness != expected) {
2980 return false;
2981 }
2982 long xLong = getLong(array, base + scale); // get x as binary
2983 if (compareAndSetLong(o, offset, witnessLong, xLong)) {
2984 return true;
2985 }
2986 } while (true);
2987 }
2988 default: {
2989 throw new UnsupportedOperationException();
2990 }
2991 }
2992 }
2993
2994 /**
2995 * Unblocks the given thread blocked on {@code park}, or, if it is
2996 * not blocked, causes the subsequent call to {@code park} not to
2997 * block. Note: this operation is "unsafe" solely because the
2998 * caller must somehow ensure that the thread has not been
2999 * destroyed. Nothing special is usually required to ensure this
3000 * when called from Java (in which there will ordinarily be a live
3001 * reference to the thread) but this is not nearly-automatically
3002 * so when calling from native code.
3003 *
3004 * @param thread the thread to unpark.
3005 */
3006 @IntrinsicCandidate
3007 public native void unpark(Object thread);
3008
3009 /**
3010 * Blocks current thread, returning when a balancing
3011 * {@code unpark} occurs, or a balancing {@code unpark} has
3012 * already occurred, or the thread is interrupted, or, if not
3013 * absolute and time is not zero, the given time nanoseconds have
3014 * elapsed, or if absolute, the given deadline in milliseconds
3015 * since Epoch has passed, or spuriously (i.e., returning for no
3016 * "reason"). Note: This operation is in the Unsafe class only
3017 * because {@code unpark} is, so it would be strange to place it
3018 * elsewhere.
3019 */
3020 @IntrinsicCandidate
3021 public native void park(boolean isAbsolute, long time);
3022
3023 /**
3024 * Gets the load average in the system run queue assigned
3025 * to the available processors averaged over various periods of time.
3026 * This method retrieves the given {@code nelem} samples and
3027 * assigns to the elements of the given {@code loadavg} array.
3028 * The system imposes a maximum of 3 samples, representing
3029 * averages over the last 1, 5, and 15 minutes, respectively.
3030 *
3031 * @param loadavg an array of double of size nelems
3032 * @param nelems the number of samples to be retrieved and
3033 * must be 1 to 3.
3034 *
3035 * @return the number of samples actually retrieved; or -1
3036 * if the load average is unobtainable.
3037 */
3038 public int getLoadAverage(double[] loadavg, int nelems) {
3039 if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
3040 throw new ArrayIndexOutOfBoundsException();
3041 }
3042
3043 return getLoadAverage0(loadavg, nelems);
3044 }
3045
3046 // The following contain CAS-based Java implementations used on
3047 // platforms not supporting native instructions
3048
3049 /**
3050 * Atomically adds the given value to the current value of a field
3051 * or array element within the given object {@code o}
3052 * at the given {@code offset}.
3053 *
3054 * @param o object/array to update the field/element in
3055 * @param offset field/element offset
3056 * @param delta the value to add
3057 * @return the previous value
3058 * @since 1.8
3059 */
3060 @IntrinsicCandidate
3061 public final int getAndAddInt(Object o, long offset, int delta) {
3062 int v;
3063 do {
3064 v = getIntVolatile(o, offset);
3065 } while (!weakCompareAndSetInt(o, offset, v, v + delta));
3066 return v;
3067 }
3068
3069 @ForceInline
3070 public final int getAndAddIntRelease(Object o, long offset, int delta) {
3071 int v;
3072 do {
3073 v = getInt(o, offset);
3074 } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta));
3075 return v;
3076 }
3077
3078 @ForceInline
3079 public final int getAndAddIntAcquire(Object o, long offset, int delta) {
3080 int v;
3081 do {
3082 v = getIntAcquire(o, offset);
3083 } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta));
3084 return v;
3085 }
3086
3087 /**
3088 * Atomically adds the given value to the current value of a field
3089 * or array element within the given object {@code o}
3090 * at the given {@code offset}.
3091 *
3092 * @param o object/array to update the field/element in
3093 * @param offset field/element offset
3094 * @param delta the value to add
3095 * @return the previous value
3096 * @since 1.8
3097 */
3098 @IntrinsicCandidate
3099 public final long getAndAddLong(Object o, long offset, long delta) {
3100 long v;
3101 do {
3102 v = getLongVolatile(o, offset);
3103 } while (!weakCompareAndSetLong(o, offset, v, v + delta));
3104 return v;
3105 }
3106
3107 @ForceInline
3108 public final long getAndAddLongRelease(Object o, long offset, long delta) {
3109 long v;
3110 do {
3111 v = getLong(o, offset);
3112 } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta));
3113 return v;
3114 }
3115
3116 @ForceInline
3117 public final long getAndAddLongAcquire(Object o, long offset, long delta) {
3118 long v;
3119 do {
3120 v = getLongAcquire(o, offset);
3121 } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta));
3122 return v;
3123 }
3124
3125 @IntrinsicCandidate
3126 public final byte getAndAddByte(Object o, long offset, byte delta) {
3127 byte v;
3128 do {
3129 v = getByteVolatile(o, offset);
3130 } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta)));
3131 return v;
3132 }
3133
3134 @ForceInline
3135 public final byte getAndAddByteRelease(Object o, long offset, byte delta) {
3136 byte v;
3137 do {
3138 v = getByte(o, offset);
3139 } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta)));
3140 return v;
3141 }
3142
3143 @ForceInline
3144 public final byte getAndAddByteAcquire(Object o, long offset, byte delta) {
3145 byte v;
3146 do {
3147 v = getByteAcquire(o, offset);
3148 } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta)));
3149 return v;
3150 }
3151
3152 @IntrinsicCandidate
3153 public final short getAndAddShort(Object o, long offset, short delta) {
3154 short v;
3155 do {
3156 v = getShortVolatile(o, offset);
3157 } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta)));
3158 return v;
3159 }
3160
3161 @ForceInline
3162 public final short getAndAddShortRelease(Object o, long offset, short delta) {
3163 short v;
3164 do {
3165 v = getShort(o, offset);
3166 } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta)));
3167 return v;
3168 }
3169
3170 @ForceInline
3171 public final short getAndAddShortAcquire(Object o, long offset, short delta) {
3172 short v;
3173 do {
3174 v = getShortAcquire(o, offset);
3175 } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta)));
3176 return v;
3177 }
3178
3179 @ForceInline
3180 public final char getAndAddChar(Object o, long offset, char delta) {
3181 return (char) getAndAddShort(o, offset, (short) delta);
3182 }
3183
3184 @ForceInline
3185 public final char getAndAddCharRelease(Object o, long offset, char delta) {
3186 return (char) getAndAddShortRelease(o, offset, (short) delta);
3187 }
3188
3189 @ForceInline
3190 public final char getAndAddCharAcquire(Object o, long offset, char delta) {
3191 return (char) getAndAddShortAcquire(o, offset, (short) delta);
3192 }
3193
3194 @ForceInline
3195 public final float getAndAddFloat(Object o, long offset, float delta) {
3196 int expectedBits;
3197 float v;
3198 do {
3199 // Load and CAS with the raw bits to avoid issues with NaNs and
3200 // possible bit conversion from signaling NaNs to quiet NaNs that
3201 // may result in the loop not terminating.
3202 expectedBits = getIntVolatile(o, offset);
3203 v = Float.intBitsToFloat(expectedBits);
3204 } while (!weakCompareAndSetInt(o, offset,
3205 expectedBits, Float.floatToRawIntBits(v + delta)));
3206 return v;
3207 }
3208
3209 @ForceInline
3210 public final float getAndAddFloatRelease(Object o, long offset, float delta) {
3211 int expectedBits;
3212 float v;
3213 do {
3214 // Load and CAS with the raw bits to avoid issues with NaNs and
3215 // possible bit conversion from signaling NaNs to quiet NaNs that
3216 // may result in the loop not terminating.
3217 expectedBits = getInt(o, offset);
3218 v = Float.intBitsToFloat(expectedBits);
3219 } while (!weakCompareAndSetIntRelease(o, offset,
3220 expectedBits, Float.floatToRawIntBits(v + delta)));
3221 return v;
3222 }
3223
3224 @ForceInline
3225 public final float getAndAddFloatAcquire(Object o, long offset, float delta) {
3226 int expectedBits;
3227 float v;
3228 do {
3229 // Load and CAS with the raw bits to avoid issues with NaNs and
3230 // possible bit conversion from signaling NaNs to quiet NaNs that
3231 // may result in the loop not terminating.
3232 expectedBits = getIntAcquire(o, offset);
3233 v = Float.intBitsToFloat(expectedBits);
3234 } while (!weakCompareAndSetIntAcquire(o, offset,
3235 expectedBits, Float.floatToRawIntBits(v + delta)));
3236 return v;
3237 }
3238
3239 @ForceInline
3240 public final double getAndAddDouble(Object o, long offset, double delta) {
3241 long expectedBits;
3242 double v;
3243 do {
3244 // Load and CAS with the raw bits to avoid issues with NaNs and
3245 // possible bit conversion from signaling NaNs to quiet NaNs that
3246 // may result in the loop not terminating.
3247 expectedBits = getLongVolatile(o, offset);
3248 v = Double.longBitsToDouble(expectedBits);
3249 } while (!weakCompareAndSetLong(o, offset,
3250 expectedBits, Double.doubleToRawLongBits(v + delta)));
3251 return v;
3252 }
3253
3254 @ForceInline
3255 public final double getAndAddDoubleRelease(Object o, long offset, double delta) {
3256 long expectedBits;
3257 double v;
3258 do {
3259 // Load and CAS with the raw bits to avoid issues with NaNs and
3260 // possible bit conversion from signaling NaNs to quiet NaNs that
3261 // may result in the loop not terminating.
3262 expectedBits = getLong(o, offset);
3263 v = Double.longBitsToDouble(expectedBits);
3264 } while (!weakCompareAndSetLongRelease(o, offset,
3265 expectedBits, Double.doubleToRawLongBits(v + delta)));
3266 return v;
3267 }
3268
3269 @ForceInline
3270 public final double getAndAddDoubleAcquire(Object o, long offset, double delta) {
3271 long expectedBits;
3272 double v;
3273 do {
3274 // Load and CAS with the raw bits to avoid issues with NaNs and
3275 // possible bit conversion from signaling NaNs to quiet NaNs that
3276 // may result in the loop not terminating.
3277 expectedBits = getLongAcquire(o, offset);
3278 v = Double.longBitsToDouble(expectedBits);
3279 } while (!weakCompareAndSetLongAcquire(o, offset,
3280 expectedBits, Double.doubleToRawLongBits(v + delta)));
3281 return v;
3282 }
3283
3284 /**
3285 * Atomically exchanges the given value with the current value of
3286 * a field or array element within the given object {@code o}
3287 * at the given {@code offset}.
3288 *
3289 * @param o object/array to update the field/element in
3290 * @param offset field/element offset
3291 * @param newValue new value
3292 * @return the previous value
3293 * @since 1.8
3294 */
3295 @IntrinsicCandidate
3296 public final int getAndSetInt(Object o, long offset, int newValue) {
3297 int v;
3298 do {
3299 v = getIntVolatile(o, offset);
3300 } while (!weakCompareAndSetInt(o, offset, v, newValue));
3301 return v;
3302 }
3303
3304 @ForceInline
3305 public final int getAndSetIntRelease(Object o, long offset, int newValue) {
3306 int v;
3307 do {
3308 v = getInt(o, offset);
3309 } while (!weakCompareAndSetIntRelease(o, offset, v, newValue));
3310 return v;
3311 }
3312
3313 @ForceInline
3314 public final int getAndSetIntAcquire(Object o, long offset, int newValue) {
3315 int v;
3316 do {
3317 v = getIntAcquire(o, offset);
3318 } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue));
3319 return v;
3320 }
3321
3322 /**
3323 * Atomically exchanges the given value with the current value of
3324 * a field or array element within the given object {@code o}
3325 * at the given {@code offset}.
3326 *
3327 * @param o object/array to update the field/element in
3328 * @param offset field/element offset
3329 * @param newValue new value
3330 * @return the previous value
3331 * @since 1.8
3332 */
3333 @IntrinsicCandidate
3334 public final long getAndSetLong(Object o, long offset, long newValue) {
3335 long v;
3336 do {
3337 v = getLongVolatile(o, offset);
3338 } while (!weakCompareAndSetLong(o, offset, v, newValue));
3339 return v;
3340 }
3341
3342 @ForceInline
3343 public final long getAndSetLongRelease(Object o, long offset, long newValue) {
3344 long v;
3345 do {
3346 v = getLong(o, offset);
3347 } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
3348 return v;
3349 }
3350
3351 @ForceInline
3352 public final long getAndSetLongAcquire(Object o, long offset, long newValue) {
3353 long v;
3354 do {
3355 v = getLongAcquire(o, offset);
3356 } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue));
3357 return v;
3358 }
3359
3360 /**
3361 * Atomically exchanges the given reference value with the current
3362 * reference value of a field or array element within the given
3363 * object {@code o} at the given {@code offset}.
3364 *
3365 * @param o object/array to update the field/element in
3366 * @param offset field/element offset
3367 * @param newValue new value
3368 * @return the previous value
3369 * @since 1.8
3370 */
3371 @IntrinsicCandidate
3372 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3373 Object v;
3374 do {
3375 v = getReferenceVolatile(o, offset);
3376 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3377 return v;
3378 }
3379
3380 @ForceInline
3381 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3382 Object v;
3383 do {
3384 v = getReferenceVolatile(o, offset);
3385 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3386 return v;
3387 }
3388
3389 @ForceInline
3390 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3391 Object v;
3392 do {
3393 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3394 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3395 return v;
3396 }
3397
3398 @ForceInline
3399 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3400 Object v;
3401 do {
3402 v = getReference(o, offset);
3403 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3404 return v;
3405 }
3406
3407 @ForceInline
3408 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3409 return getAndSetReference(o, offset, valueType, newValue);
3410 }
3411
3412 @ForceInline
3413 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3414 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3415 }
3416
3417 @ForceInline
3418 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3419 Object v;
3420 do {
3421 v = getReferenceAcquire(o, offset);
3422 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3423 return v;
3424 }
3425
3426 @ForceInline
3427 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3428 return getAndSetReference(o, offset, valueType, newValue);
3429 }
3430
3431 @ForceInline
3432 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3433 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3434 }
3435
3436 @IntrinsicCandidate
3437 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3438 byte v;
3439 do {
3440 v = getByteVolatile(o, offset);
3441 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3442 return v;
3443 }
3444
3445 @ForceInline
3446 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3447 byte v;
3448 do {
3449 v = getByte(o, offset);
3450 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3451 return v;
3452 }
3453
3454 @ForceInline
3455 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
3456 byte v;
3457 do {
3458 v = getByteAcquire(o, offset);
3459 } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue));
3460 return v;
3461 }
3462
3463 @ForceInline
3464 public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) {
3465 return byte2bool(getAndSetByte(o, offset, bool2byte(newValue)));
3466 }
3467
3468 @ForceInline
3469 public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) {
3470 return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue)));
3471 }
3472
3473 @ForceInline
3474 public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) {
3475 return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue)));
3476 }
3477
3478 @IntrinsicCandidate
3479 public final short getAndSetShort(Object o, long offset, short newValue) {
3480 short v;
3481 do {
3482 v = getShortVolatile(o, offset);
3483 } while (!weakCompareAndSetShort(o, offset, v, newValue));
3484 return v;
3485 }
3486
3487 @ForceInline
3488 public final short getAndSetShortRelease(Object o, long offset, short newValue) {
3489 short v;
3490 do {
3491 v = getShort(o, offset);
3492 } while (!weakCompareAndSetShortRelease(o, offset, v, newValue));
3493 return v;
3494 }
3495
3496 @ForceInline
3497 public final short getAndSetShortAcquire(Object o, long offset, short newValue) {
3498 short v;
3499 do {
3500 v = getShortAcquire(o, offset);
3501 } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue));
3502 return v;
3503 }
3504
3505 @ForceInline
3506 public final char getAndSetChar(Object o, long offset, char newValue) {
3507 return s2c(getAndSetShort(o, offset, c2s(newValue)));
3508 }
3509
3510 @ForceInline
3511 public final char getAndSetCharRelease(Object o, long offset, char newValue) {
3512 return s2c(getAndSetShortRelease(o, offset, c2s(newValue)));
3513 }
3514
3515 @ForceInline
3516 public final char getAndSetCharAcquire(Object o, long offset, char newValue) {
3517 return s2c(getAndSetShortAcquire(o, offset, c2s(newValue)));
3518 }
3519
3520 @ForceInline
3521 public final float getAndSetFloat(Object o, long offset, float newValue) {
3522 int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue));
3523 return Float.intBitsToFloat(v);
3524 }
3525
3526 @ForceInline
3527 public final float getAndSetFloatRelease(Object o, long offset, float newValue) {
3528 int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue));
3529 return Float.intBitsToFloat(v);
3530 }
3531
3532 @ForceInline
3533 public final float getAndSetFloatAcquire(Object o, long offset, float newValue) {
3534 int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue));
3535 return Float.intBitsToFloat(v);
3536 }
3537
3538 @ForceInline
3539 public final double getAndSetDouble(Object o, long offset, double newValue) {
3540 long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue));
3541 return Double.longBitsToDouble(v);
3542 }
3543
3544 @ForceInline
3545 public final double getAndSetDoubleRelease(Object o, long offset, double newValue) {
3546 long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue));
3547 return Double.longBitsToDouble(v);
3548 }
3549
3550 @ForceInline
3551 public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) {
3552 long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue));
3553 return Double.longBitsToDouble(v);
3554 }
3555
3556
3557 // The following contain CAS-based Java implementations used on
3558 // platforms not supporting native instructions
3559
3560 @ForceInline
3561 public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) {
3562 return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask)));
3563 }
3564
3565 @ForceInline
3566 public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) {
3567 return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask)));
3568 }
3569
3570 @ForceInline
3571 public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) {
3572 return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask)));
3573 }
3574
3575 @ForceInline
3576 public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) {
3577 return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask)));
3578 }
3579
3580 @ForceInline
3581 public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) {
3582 return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask)));
3583 }
3584
3585 @ForceInline
3586 public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) {
3587 return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask)));
3588 }
3589
3590 @ForceInline
3591 public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) {
3592 return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask)));
3593 }
3594
3595 @ForceInline
3596 public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) {
3597 return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask)));
3598 }
3599
3600 @ForceInline
3601 public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) {
3602 return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask)));
3603 }
3604
3605
3606 @ForceInline
3607 public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) {
3608 byte current;
3609 do {
3610 current = getByteVolatile(o, offset);
3611 } while (!weakCompareAndSetByte(o, offset,
3612 current, (byte) (current | mask)));
3613 return current;
3614 }
3615
3616 @ForceInline
3617 public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) {
3618 byte current;
3619 do {
3620 current = getByte(o, offset);
3621 } while (!weakCompareAndSetByteRelease(o, offset,
3622 current, (byte) (current | mask)));
3623 return current;
3624 }
3625
3626 @ForceInline
3627 public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) {
3628 byte current;
3629 do {
3630 // Plain read, the value is a hint, the acquire CAS does the work
3631 current = getByte(o, offset);
3632 } while (!weakCompareAndSetByteAcquire(o, offset,
3633 current, (byte) (current | mask)));
3634 return current;
3635 }
3636
3637 @ForceInline
3638 public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) {
3639 byte current;
3640 do {
3641 current = getByteVolatile(o, offset);
3642 } while (!weakCompareAndSetByte(o, offset,
3643 current, (byte) (current & mask)));
3644 return current;
3645 }
3646
3647 @ForceInline
3648 public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) {
3649 byte current;
3650 do {
3651 current = getByte(o, offset);
3652 } while (!weakCompareAndSetByteRelease(o, offset,
3653 current, (byte) (current & mask)));
3654 return current;
3655 }
3656
3657 @ForceInline
3658 public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) {
3659 byte current;
3660 do {
3661 // Plain read, the value is a hint, the acquire CAS does the work
3662 current = getByte(o, offset);
3663 } while (!weakCompareAndSetByteAcquire(o, offset,
3664 current, (byte) (current & mask)));
3665 return current;
3666 }
3667
3668 @ForceInline
3669 public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) {
3670 byte current;
3671 do {
3672 current = getByteVolatile(o, offset);
3673 } while (!weakCompareAndSetByte(o, offset,
3674 current, (byte) (current ^ mask)));
3675 return current;
3676 }
3677
3678 @ForceInline
3679 public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) {
3680 byte current;
3681 do {
3682 current = getByte(o, offset);
3683 } while (!weakCompareAndSetByteRelease(o, offset,
3684 current, (byte) (current ^ mask)));
3685 return current;
3686 }
3687
3688 @ForceInline
3689 public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) {
3690 byte current;
3691 do {
3692 // Plain read, the value is a hint, the acquire CAS does the work
3693 current = getByte(o, offset);
3694 } while (!weakCompareAndSetByteAcquire(o, offset,
3695 current, (byte) (current ^ mask)));
3696 return current;
3697 }
3698
3699
3700 @ForceInline
3701 public final char getAndBitwiseOrChar(Object o, long offset, char mask) {
3702 return s2c(getAndBitwiseOrShort(o, offset, c2s(mask)));
3703 }
3704
3705 @ForceInline
3706 public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) {
3707 return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask)));
3708 }
3709
3710 @ForceInline
3711 public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) {
3712 return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask)));
3713 }
3714
3715 @ForceInline
3716 public final char getAndBitwiseAndChar(Object o, long offset, char mask) {
3717 return s2c(getAndBitwiseAndShort(o, offset, c2s(mask)));
3718 }
3719
3720 @ForceInline
3721 public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) {
3722 return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask)));
3723 }
3724
3725 @ForceInline
3726 public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) {
3727 return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask)));
3728 }
3729
3730 @ForceInline
3731 public final char getAndBitwiseXorChar(Object o, long offset, char mask) {
3732 return s2c(getAndBitwiseXorShort(o, offset, c2s(mask)));
3733 }
3734
3735 @ForceInline
3736 public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) {
3737 return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask)));
3738 }
3739
3740 @ForceInline
3741 public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) {
3742 return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask)));
3743 }
3744
3745
3746 @ForceInline
3747 public final short getAndBitwiseOrShort(Object o, long offset, short mask) {
3748 short current;
3749 do {
3750 current = getShortVolatile(o, offset);
3751 } while (!weakCompareAndSetShort(o, offset,
3752 current, (short) (current | mask)));
3753 return current;
3754 }
3755
3756 @ForceInline
3757 public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) {
3758 short current;
3759 do {
3760 current = getShort(o, offset);
3761 } while (!weakCompareAndSetShortRelease(o, offset,
3762 current, (short) (current | mask)));
3763 return current;
3764 }
3765
3766 @ForceInline
3767 public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) {
3768 short current;
3769 do {
3770 // Plain read, the value is a hint, the acquire CAS does the work
3771 current = getShort(o, offset);
3772 } while (!weakCompareAndSetShortAcquire(o, offset,
3773 current, (short) (current | mask)));
3774 return current;
3775 }
3776
3777 @ForceInline
3778 public final short getAndBitwiseAndShort(Object o, long offset, short mask) {
3779 short current;
3780 do {
3781 current = getShortVolatile(o, offset);
3782 } while (!weakCompareAndSetShort(o, offset,
3783 current, (short) (current & mask)));
3784 return current;
3785 }
3786
3787 @ForceInline
3788 public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) {
3789 short current;
3790 do {
3791 current = getShort(o, offset);
3792 } while (!weakCompareAndSetShortRelease(o, offset,
3793 current, (short) (current & mask)));
3794 return current;
3795 }
3796
3797 @ForceInline
3798 public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) {
3799 short current;
3800 do {
3801 // Plain read, the value is a hint, the acquire CAS does the work
3802 current = getShort(o, offset);
3803 } while (!weakCompareAndSetShortAcquire(o, offset,
3804 current, (short) (current & mask)));
3805 return current;
3806 }
3807
3808 @ForceInline
3809 public final short getAndBitwiseXorShort(Object o, long offset, short mask) {
3810 short current;
3811 do {
3812 current = getShortVolatile(o, offset);
3813 } while (!weakCompareAndSetShort(o, offset,
3814 current, (short) (current ^ mask)));
3815 return current;
3816 }
3817
3818 @ForceInline
3819 public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) {
3820 short current;
3821 do {
3822 current = getShort(o, offset);
3823 } while (!weakCompareAndSetShortRelease(o, offset,
3824 current, (short) (current ^ mask)));
3825 return current;
3826 }
3827
3828 @ForceInline
3829 public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) {
3830 short current;
3831 do {
3832 // Plain read, the value is a hint, the acquire CAS does the work
3833 current = getShort(o, offset);
3834 } while (!weakCompareAndSetShortAcquire(o, offset,
3835 current, (short) (current ^ mask)));
3836 return current;
3837 }
3838
3839
3840 @ForceInline
3841 public final int getAndBitwiseOrInt(Object o, long offset, int mask) {
3842 int current;
3843 do {
3844 current = getIntVolatile(o, offset);
3845 } while (!weakCompareAndSetInt(o, offset,
3846 current, current | mask));
3847 return current;
3848 }
3849
3850 @ForceInline
3851 public final int getAndBitwiseOrIntRelease(Object o, long offset, int mask) {
3852 int current;
3853 do {
3854 current = getInt(o, offset);
3855 } while (!weakCompareAndSetIntRelease(o, offset,
3856 current, current | mask));
3857 return current;
3858 }
3859
3860 @ForceInline
3861 public final int getAndBitwiseOrIntAcquire(Object o, long offset, int mask) {
3862 int current;
3863 do {
3864 // Plain read, the value is a hint, the acquire CAS does the work
3865 current = getInt(o, offset);
3866 } while (!weakCompareAndSetIntAcquire(o, offset,
3867 current, current | mask));
3868 return current;
3869 }
3870
3871 /**
3872 * Atomically replaces the current value of a field or array element within
3873 * the given object with the result of bitwise AND between the current value
3874 * and mask.
3875 *
3876 * @param o object/array to update the field/element in
3877 * @param offset field/element offset
3878 * @param mask the mask value
3879 * @return the previous value
3880 * @since 9
3881 */
3882 @ForceInline
3883 public final int getAndBitwiseAndInt(Object o, long offset, int mask) {
3884 int current;
3885 do {
3886 current = getIntVolatile(o, offset);
3887 } while (!weakCompareAndSetInt(o, offset,
3888 current, current & mask));
3889 return current;
3890 }
3891
3892 @ForceInline
3893 public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) {
3894 int current;
3895 do {
3896 current = getInt(o, offset);
3897 } while (!weakCompareAndSetIntRelease(o, offset,
3898 current, current & mask));
3899 return current;
3900 }
3901
3902 @ForceInline
3903 public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) {
3904 int current;
3905 do {
3906 // Plain read, the value is a hint, the acquire CAS does the work
3907 current = getInt(o, offset);
3908 } while (!weakCompareAndSetIntAcquire(o, offset,
3909 current, current & mask));
3910 return current;
3911 }
3912
3913 @ForceInline
3914 public final int getAndBitwiseXorInt(Object o, long offset, int mask) {
3915 int current;
3916 do {
3917 current = getIntVolatile(o, offset);
3918 } while (!weakCompareAndSetInt(o, offset,
3919 current, current ^ mask));
3920 return current;
3921 }
3922
3923 @ForceInline
3924 public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) {
3925 int current;
3926 do {
3927 current = getInt(o, offset);
3928 } while (!weakCompareAndSetIntRelease(o, offset,
3929 current, current ^ mask));
3930 return current;
3931 }
3932
3933 @ForceInline
3934 public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) {
3935 int current;
3936 do {
3937 // Plain read, the value is a hint, the acquire CAS does the work
3938 current = getInt(o, offset);
3939 } while (!weakCompareAndSetIntAcquire(o, offset,
3940 current, current ^ mask));
3941 return current;
3942 }
3943
3944
3945 @ForceInline
3946 public final long getAndBitwiseOrLong(Object o, long offset, long mask) {
3947 long current;
3948 do {
3949 current = getLongVolatile(o, offset);
3950 } while (!weakCompareAndSetLong(o, offset,
3951 current, current | mask));
3952 return current;
3953 }
3954
3955 @ForceInline
3956 public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) {
3957 long current;
3958 do {
3959 current = getLong(o, offset);
3960 } while (!weakCompareAndSetLongRelease(o, offset,
3961 current, current | mask));
3962 return current;
3963 }
3964
3965 @ForceInline
3966 public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) {
3967 long current;
3968 do {
3969 // Plain read, the value is a hint, the acquire CAS does the work
3970 current = getLong(o, offset);
3971 } while (!weakCompareAndSetLongAcquire(o, offset,
3972 current, current | mask));
3973 return current;
3974 }
3975
3976 @ForceInline
3977 public final long getAndBitwiseAndLong(Object o, long offset, long mask) {
3978 long current;
3979 do {
3980 current = getLongVolatile(o, offset);
3981 } while (!weakCompareAndSetLong(o, offset,
3982 current, current & mask));
3983 return current;
3984 }
3985
3986 @ForceInline
3987 public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) {
3988 long current;
3989 do {
3990 current = getLong(o, offset);
3991 } while (!weakCompareAndSetLongRelease(o, offset,
3992 current, current & mask));
3993 return current;
3994 }
3995
3996 @ForceInline
3997 public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) {
3998 long current;
3999 do {
4000 // Plain read, the value is a hint, the acquire CAS does the work
4001 current = getLong(o, offset);
4002 } while (!weakCompareAndSetLongAcquire(o, offset,
4003 current, current & mask));
4004 return current;
4005 }
4006
4007 @ForceInline
4008 public final long getAndBitwiseXorLong(Object o, long offset, long mask) {
4009 long current;
4010 do {
4011 current = getLongVolatile(o, offset);
4012 } while (!weakCompareAndSetLong(o, offset,
4013 current, current ^ mask));
4014 return current;
4015 }
4016
4017 @ForceInline
4018 public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) {
4019 long current;
4020 do {
4021 current = getLong(o, offset);
4022 } while (!weakCompareAndSetLongRelease(o, offset,
4023 current, current ^ mask));
4024 return current;
4025 }
4026
4027 @ForceInline
4028 public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) {
4029 long current;
4030 do {
4031 // Plain read, the value is a hint, the acquire CAS does the work
4032 current = getLong(o, offset);
4033 } while (!weakCompareAndSetLongAcquire(o, offset,
4034 current, current ^ mask));
4035 return current;
4036 }
4037
4038
4039
4040 /**
4041 * Ensures that loads before the fence will not be reordered with loads and
4042 * stores after the fence; a "LoadLoad plus LoadStore barrier".
4043 *
4044 * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
4045 * (an "acquire fence").
4046 *
4047 * Provides a LoadLoad barrier followed by a LoadStore barrier.
4048 *
4049 * @since 1.8
4050 */
4051 @IntrinsicCandidate
4052 public final void loadFence() {
4053 // If loadFence intrinsic is not available, fall back to full fence.
4054 fullFence();
4055 }
4056
4057 /**
4058 * Ensures that loads and stores before the fence will not be reordered with
4059 * stores after the fence; a "StoreStore plus LoadStore barrier".
4060 *
4061 * Corresponds to C11 atomic_thread_fence(memory_order_release)
4062 * (a "release fence").
4063 *
4064 * Provides a StoreStore barrier followed by a LoadStore barrier.
4065 *
4066 * @since 1.8
4067 */
4068 @IntrinsicCandidate
4069 public final void storeFence() {
4070 // If storeFence intrinsic is not available, fall back to full fence.
4071 fullFence();
4072 }
4073
4074 /**
4075 * Ensures that loads and stores before the fence will not be reordered
4076 * with loads and stores after the fence. Implies the effects of both
4077 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
4078 * barrier.
4079 *
4080 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
4081 * @since 1.8
4082 */
4083 @IntrinsicCandidate
4084 public native void fullFence();
4085
4086 /**
4087 * Ensures that loads before the fence will not be reordered with
4088 * loads after the fence.
4089 *
4090 * @implNote
4091 * This method is operationally equivalent to {@link #loadFence()}.
4092 *
4093 * @since 9
4094 */
4095 public final void loadLoadFence() {
4096 loadFence();
4097 }
4098
4099 /**
4100 * Ensures that stores before the fence will not be reordered with
4101 * stores after the fence.
4102 *
4103 * @since 9
4104 */
4105 @IntrinsicCandidate
4106 public final void storeStoreFence() {
4107 // If storeStoreFence intrinsic is not available, fall back to storeFence.
4108 storeFence();
4109 }
4110
4111 /**
4112 * Throws IllegalAccessError; for use by the VM for access control
4113 * error support.
4114 * @since 1.8
4115 */
4116 private static void throwIllegalAccessError() {
4117 throw new IllegalAccessError();
4118 }
4119
4120 /**
4121 * Throws NoSuchMethodError; for use by the VM for redefinition support.
4122 * @since 13
4123 */
4124 private static void throwNoSuchMethodError() {
4125 throw new NoSuchMethodError();
4126 }
4127
4128 /**
4129 * @return Returns true if the native byte ordering of this
4130 * platform is big-endian, false if it is little-endian.
4131 */
4132 public final boolean isBigEndian() { return BIG_ENDIAN; }
4133
4134 /**
4135 * @return Returns true if this platform is capable of performing
4136 * accesses at addresses which are not aligned for the type of the
4137 * primitive type being accessed, false otherwise.
4138 */
4139 public final boolean unalignedAccess() { return UNALIGNED_ACCESS; }
4140
4141 /**
4142 * Fetches a value at some byte offset into a given Java object.
4143 * More specifically, fetches a value within the given object
4144 * <code>o</code> at the given offset, or (if <code>o</code> is
4145 * null) from the memory address whose numerical value is the
4146 * given offset. <p>
4147 *
4148 * The specification of this method is the same as {@link
4149 * #getLong(Object, long)} except that the offset does not need to
4150 * have been obtained from {@link #objectFieldOffset} on the
4151 * {@link java.lang.reflect.Field} of some Java field. The value
4152 * in memory is raw data, and need not correspond to any Java
4153 * variable. Unless <code>o</code> is null, the value accessed
4154 * must be entirely within the allocated object. The endianness
4155 * of the value in memory is the endianness of the native platform.
4156 *
4157 * <p> The read will be atomic with respect to the largest power
4158 * of two that divides the GCD of the offset and the storage size.
4159 * For example, getLongUnaligned will make atomic reads of 2-, 4-,
4160 * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
4161 * respectively. There are no other guarantees of atomicity.
4162 * <p>
4163 * 8-byte atomicity is only guaranteed on platforms on which
4164 * support atomic accesses to longs.
4165 *
4166 * @param o Java heap object in which the value resides, if any, else
4167 * null
4168 * @param offset The offset in bytes from the start of the object
4169 * @return the value fetched from the indicated object
4170 * @throws RuntimeException No defined exceptions are thrown, not even
4171 * {@link NullPointerException}
4172 * @since 9
4173 */
4174 @IntrinsicCandidate
4175 public final long getLongUnaligned(Object o, long offset) {
4176 if ((offset & 7) == 0) {
4177 return getLong(o, offset);
4178 } else if ((offset & 3) == 0) {
4179 return makeLong(getInt(o, offset),
4180 getInt(o, offset + 4));
4181 } else if ((offset & 1) == 0) {
4182 return makeLong(getShort(o, offset),
4183 getShort(o, offset + 2),
4184 getShort(o, offset + 4),
4185 getShort(o, offset + 6));
4186 } else {
4187 return makeLong(getByte(o, offset),
4188 getByte(o, offset + 1),
4189 getByte(o, offset + 2),
4190 getByte(o, offset + 3),
4191 getByte(o, offset + 4),
4192 getByte(o, offset + 5),
4193 getByte(o, offset + 6),
4194 getByte(o, offset + 7));
4195 }
4196 }
4197 /**
4198 * As {@link #getLongUnaligned(Object, long)} but with an
4199 * additional argument which specifies the endianness of the value
4200 * as stored in memory.
4201 *
4202 * @param o Java heap object in which the variable resides
4203 * @param offset The offset in bytes from the start of the object
4204 * @param bigEndian The endianness of the value
4205 * @return the value fetched from the indicated object
4206 * @since 9
4207 */
4208 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
4209 return convEndian(bigEndian, getLongUnaligned(o, offset));
4210 }
4211
4212 /** @see #getLongUnaligned(Object, long) */
4213 @IntrinsicCandidate
4214 public final int getIntUnaligned(Object o, long offset) {
4215 if ((offset & 3) == 0) {
4216 return getInt(o, offset);
4217 } else if ((offset & 1) == 0) {
4218 return makeInt(getShort(o, offset),
4219 getShort(o, offset + 2));
4220 } else {
4221 return makeInt(getByte(o, offset),
4222 getByte(o, offset + 1),
4223 getByte(o, offset + 2),
4224 getByte(o, offset + 3));
4225 }
4226 }
4227 /** @see #getLongUnaligned(Object, long, boolean) */
4228 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
4229 return convEndian(bigEndian, getIntUnaligned(o, offset));
4230 }
4231
4232 /** @see #getLongUnaligned(Object, long) */
4233 @IntrinsicCandidate
4234 public final short getShortUnaligned(Object o, long offset) {
4235 if ((offset & 1) == 0) {
4236 return getShort(o, offset);
4237 } else {
4238 return makeShort(getByte(o, offset),
4239 getByte(o, offset + 1));
4240 }
4241 }
4242 /** @see #getLongUnaligned(Object, long, boolean) */
4243 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
4244 return convEndian(bigEndian, getShortUnaligned(o, offset));
4245 }
4246
4247 /** @see #getLongUnaligned(Object, long) */
4248 @IntrinsicCandidate
4249 public final char getCharUnaligned(Object o, long offset) {
4250 if ((offset & 1) == 0) {
4251 return getChar(o, offset);
4252 } else {
4253 return (char)makeShort(getByte(o, offset),
4254 getByte(o, offset + 1));
4255 }
4256 }
4257
4258 /** @see #getLongUnaligned(Object, long, boolean) */
4259 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
4260 return convEndian(bigEndian, getCharUnaligned(o, offset));
4261 }
4262
4263 /**
4264 * Stores a value at some byte offset into a given Java object.
4265 * <p>
4266 * The specification of this method is the same as {@link
4267 * #getLong(Object, long)} except that the offset does not need to
4268 * have been obtained from {@link #objectFieldOffset} on the
4269 * {@link java.lang.reflect.Field} of some Java field. The value
4270 * in memory is raw data, and need not correspond to any Java
4271 * variable. The endianness of the value in memory is the
4272 * endianness of the native platform.
4273 * <p>
4274 * The write will be atomic with respect to the largest power of
4275 * two that divides the GCD of the offset and the storage size.
4276 * For example, putLongUnaligned will make atomic writes of 2-, 4-,
4277 * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
4278 * respectively. There are no other guarantees of atomicity.
4279 * <p>
4280 * 8-byte atomicity is only guaranteed on platforms on which
4281 * support atomic accesses to longs.
4282 *
4283 * @param o Java heap object in which the value resides, if any, else
4284 * null
4285 * @param offset The offset in bytes from the start of the object
4286 * @param x the value to store
4287 * @throws RuntimeException No defined exceptions are thrown, not even
4288 * {@link NullPointerException}
4289 * @since 9
4290 */
4291 @IntrinsicCandidate
4292 public final void putLongUnaligned(Object o, long offset, long x) {
4293 if ((offset & 7) == 0) {
4294 putLong(o, offset, x);
4295 } else if ((offset & 3) == 0) {
4296 putLongParts(o, offset,
4297 (int)(x >> 0),
4298 (int)(x >>> 32));
4299 } else if ((offset & 1) == 0) {
4300 putLongParts(o, offset,
4301 (short)(x >>> 0),
4302 (short)(x >>> 16),
4303 (short)(x >>> 32),
4304 (short)(x >>> 48));
4305 } else {
4306 putLongParts(o, offset,
4307 (byte)(x >>> 0),
4308 (byte)(x >>> 8),
4309 (byte)(x >>> 16),
4310 (byte)(x >>> 24),
4311 (byte)(x >>> 32),
4312 (byte)(x >>> 40),
4313 (byte)(x >>> 48),
4314 (byte)(x >>> 56));
4315 }
4316 }
4317
4318 /**
4319 * As {@link #putLongUnaligned(Object, long, long)} but with an additional
4320 * argument which specifies the endianness of the value as stored in memory.
4321 * @param o Java heap object in which the value resides
4322 * @param offset The offset in bytes from the start of the object
4323 * @param x the value to store
4324 * @param bigEndian The endianness of the value
4325 * @throws RuntimeException No defined exceptions are thrown, not even
4326 * {@link NullPointerException}
4327 * @since 9
4328 */
4329 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
4330 putLongUnaligned(o, offset, convEndian(bigEndian, x));
4331 }
4332
4333 /** @see #putLongUnaligned(Object, long, long) */
4334 @IntrinsicCandidate
4335 public final void putIntUnaligned(Object o, long offset, int x) {
4336 if ((offset & 3) == 0) {
4337 putInt(o, offset, x);
4338 } else if ((offset & 1) == 0) {
4339 putIntParts(o, offset,
4340 (short)(x >> 0),
4341 (short)(x >>> 16));
4342 } else {
4343 putIntParts(o, offset,
4344 (byte)(x >>> 0),
4345 (byte)(x >>> 8),
4346 (byte)(x >>> 16),
4347 (byte)(x >>> 24));
4348 }
4349 }
4350 /** @see #putLongUnaligned(Object, long, long, boolean) */
4351 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
4352 putIntUnaligned(o, offset, convEndian(bigEndian, x));
4353 }
4354
4355 /** @see #putLongUnaligned(Object, long, long) */
4356 @IntrinsicCandidate
4357 public final void putShortUnaligned(Object o, long offset, short x) {
4358 if ((offset & 1) == 0) {
4359 putShort(o, offset, x);
4360 } else {
4361 putShortParts(o, offset,
4362 (byte)(x >>> 0),
4363 (byte)(x >>> 8));
4364 }
4365 }
4366 /** @see #putLongUnaligned(Object, long, long, boolean) */
4367 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
4368 putShortUnaligned(o, offset, convEndian(bigEndian, x));
4369 }
4370
4371 /** @see #putLongUnaligned(Object, long, long) */
4372 @IntrinsicCandidate
4373 public final void putCharUnaligned(Object o, long offset, char x) {
4374 putShortUnaligned(o, offset, (short)x);
4375 }
4376 /** @see #putLongUnaligned(Object, long, long, boolean) */
4377 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
4378 putCharUnaligned(o, offset, convEndian(bigEndian, x));
4379 }
4380
4381 private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; }
4382
4383 // These methods construct integers from bytes. The byte ordering
4384 // is the native endianness of this platform.
4385 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4386 return ((toUnsignedLong(i0) << pickPos(56, 0))
4387 | (toUnsignedLong(i1) << pickPos(56, 8))
4388 | (toUnsignedLong(i2) << pickPos(56, 16))
4389 | (toUnsignedLong(i3) << pickPos(56, 24))
4390 | (toUnsignedLong(i4) << pickPos(56, 32))
4391 | (toUnsignedLong(i5) << pickPos(56, 40))
4392 | (toUnsignedLong(i6) << pickPos(56, 48))
4393 | (toUnsignedLong(i7) << pickPos(56, 56)));
4394 }
4395 private static long makeLong(short i0, short i1, short i2, short i3) {
4396 return ((toUnsignedLong(i0) << pickPos(48, 0))
4397 | (toUnsignedLong(i1) << pickPos(48, 16))
4398 | (toUnsignedLong(i2) << pickPos(48, 32))
4399 | (toUnsignedLong(i3) << pickPos(48, 48)));
4400 }
4401 private static long makeLong(int i0, int i1) {
4402 return (toUnsignedLong(i0) << pickPos(32, 0))
4403 | (toUnsignedLong(i1) << pickPos(32, 32));
4404 }
4405 private static int makeInt(short i0, short i1) {
4406 return (toUnsignedInt(i0) << pickPos(16, 0))
4407 | (toUnsignedInt(i1) << pickPos(16, 16));
4408 }
4409 private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
4410 return ((toUnsignedInt(i0) << pickPos(24, 0))
4411 | (toUnsignedInt(i1) << pickPos(24, 8))
4412 | (toUnsignedInt(i2) << pickPos(24, 16))
4413 | (toUnsignedInt(i3) << pickPos(24, 24)));
4414 }
4415 private static short makeShort(byte i0, byte i1) {
4416 return (short)((toUnsignedInt(i0) << pickPos(8, 0))
4417 | (toUnsignedInt(i1) << pickPos(8, 8)));
4418 }
4419
4420 private static byte pick(byte le, byte be) { return BIG_ENDIAN ? be : le; }
4421 private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; }
4422 private static int pick(int le, int be) { return BIG_ENDIAN ? be : le; }
4423
4424 // These methods write integers to memory from smaller parts
4425 // provided by their caller. The ordering in which these parts
4426 // are written is the native endianness of this platform.
4427 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4428 putByte(o, offset + 0, pick(i0, i7));
4429 putByte(o, offset + 1, pick(i1, i6));
4430 putByte(o, offset + 2, pick(i2, i5));
4431 putByte(o, offset + 3, pick(i3, i4));
4432 putByte(o, offset + 4, pick(i4, i3));
4433 putByte(o, offset + 5, pick(i5, i2));
4434 putByte(o, offset + 6, pick(i6, i1));
4435 putByte(o, offset + 7, pick(i7, i0));
4436 }
4437 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
4438 putShort(o, offset + 0, pick(i0, i3));
4439 putShort(o, offset + 2, pick(i1, i2));
4440 putShort(o, offset + 4, pick(i2, i1));
4441 putShort(o, offset + 6, pick(i3, i0));
4442 }
4443 private void putLongParts(Object o, long offset, int i0, int i1) {
4444 putInt(o, offset + 0, pick(i0, i1));
4445 putInt(o, offset + 4, pick(i1, i0));
4446 }
4447 private void putIntParts(Object o, long offset, short i0, short i1) {
4448 putShort(o, offset + 0, pick(i0, i1));
4449 putShort(o, offset + 2, pick(i1, i0));
4450 }
4451 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
4452 putByte(o, offset + 0, pick(i0, i3));
4453 putByte(o, offset + 1, pick(i1, i2));
4454 putByte(o, offset + 2, pick(i2, i1));
4455 putByte(o, offset + 3, pick(i3, i0));
4456 }
4457 private void putShortParts(Object o, long offset, byte i0, byte i1) {
4458 putByte(o, offset + 0, pick(i0, i1));
4459 putByte(o, offset + 1, pick(i1, i0));
4460 }
4461
4462 // Zero-extend an integer
4463 private static int toUnsignedInt(byte n) { return n & 0xff; }
4464 private static int toUnsignedInt(short n) { return n & 0xffff; }
4465 private static long toUnsignedLong(byte n) { return n & 0xffl; }
4466 private static long toUnsignedLong(short n) { return n & 0xffffl; }
4467 private static long toUnsignedLong(int n) { return n & 0xffffffffl; }
4468
4469 // Maybe byte-reverse an integer
4470 private static char convEndian(boolean big, char n) { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); }
4471 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4472 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4473 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4474
4475
4476
4477 private native long allocateMemory0(long bytes);
4478 private native long reallocateMemory0(long address, long bytes);
4479 private native void freeMemory0(long address);
4480 @IntrinsicCandidate
4481 private native void setMemory0(Object o, long offset, long bytes, byte value);
4482 @IntrinsicCandidate
4483 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4484 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4485 private native long objectFieldOffset0(Field f); // throws IAE
4486 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
4487 private native long staticFieldOffset0(Field f); // throws IAE
4488 private native Object staticFieldBase0(Field f); // throws IAE
4489 private native boolean shouldBeInitialized0(Class<?> c);
4490 private native void ensureClassInitialized0(Class<?> c);
4491 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4492 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4493 @IntrinsicCandidate
4494 private native int arrayInstanceBaseOffset0(Object[] array);
4495 private native int arrayIndexScale0(Class<?> arrayClass);
4496 @IntrinsicCandidate
4497 private native int arrayInstanceIndexScale0(Object[] array);
4498 private native long getObjectSize0(Object o);
4499 private native int getLoadAverage0(double[] loadavg, int nelems);
4500 @IntrinsicCandidate
4501 private native int[] getFieldMap0(Class <?> c);
4502
4503
4504 /**
4505 * Invokes the given direct byte buffer's cleaner, if any.
4506 *
4507 * @param directBuffer a direct byte buffer
4508 * @throws NullPointerException if {@code directBuffer} is null
4509 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4510 * or is a {@link java.nio.Buffer#slice slice}, or is a
4511 * {@link java.nio.Buffer#duplicate duplicate}
4512 */
4513 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4514 if (!directBuffer.isDirect())
4515 throw new IllegalArgumentException("buffer is non-direct");
4516
4517 DirectBuffer db = (DirectBuffer) directBuffer;
4518 if (db.attachment() != null)
4519 throw new IllegalArgumentException("duplicate or slice");
4520
4521 Cleaner cleaner = db.cleaner();
4522 if (cleaner != null) {
4523 cleaner.clean();
4524 }
4525 }
4526 }