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