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