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