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