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