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