• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * Reports the location of a given static field, in conjunction with {@link
1262      * #staticFieldBase}.
1263      * <p>Do not expect to perform any sort of arithmetic on this offset;
1264      * it is just a cookie which is passed to the unsafe heap memory accessors.
1265      *
1266      * <p>Any given field will always have the same offset, and no two distinct
1267      * fields of the same class will ever have the same offset.
1268      *
1269      * <p>As of 1.4.1, offsets for fields are represented as long values,
1270      * although the Sun JVM does not use the most significant 32 bits.
1271      * It is hard to imagine a JVM technology which needs more than
1272      * a few bits to encode an offset within a non-array object,
1273      * However, for consistency with other methods in this class,
1274      * this method reports its result as a long value.
1275      * @see #getInt(Object, long)
1276      * @hide
1277      */
staticFieldOffset(Field f)1278     public long staticFieldOffset(Field f) {
1279         // BEGIN Android-changed: Implemented differently on Android.
1280         if (!Modifier.isStatic(f.getModifiers())) {
1281             throw new IllegalArgumentException("valid for static fields only");
1282         }
1283         return f.getOffset();
1284         /*
1285         if (f == null) {
1286             throw new NullPointerException();
1287         }
1288 
1289         return staticFieldOffset0(f);
1290          */
1291         // END Android-changed: Implemented differently on Android.
1292     }
1293 
1294     /**
1295      * Reports the location of a given static field, in conjunction with {@link
1296      * #staticFieldOffset}.
1297      * <p>Fetch the base "Object", if any, with which static fields of the
1298      * given class can be accessed via methods like {@link #getInt(Object,
1299      * long)}.  This value may be null.  This value may refer to an object
1300      * which is a "cookie", not guaranteed to be a real Object, and it should
1301      * not be used in any way except as argument to the get and put routines in
1302      * this class.
1303      *
1304      * @hide
1305      */
staticFieldBase(Field f)1306     public Object staticFieldBase(Field f) {
1307         // BEGIN Android-changed: Implemented differently on Android.
1308         if (!Modifier.isStatic(f.getModifiers())) {
1309             throw new IllegalArgumentException("valid for static fields only");
1310         }
1311         Class c = f.getDeclaringClass();
1312         return c;
1313         /*
1314         if (f == null) {
1315             throw new NullPointerException();
1316         }
1317 
1318         return staticFieldBase0(f);
1319          */
1320         // END Android-changed: Implemented differently on Android.
1321     }
1322 
1323     /**
1324      * Ensures the given class has been initialized. This is often
1325      * needed in conjunction with obtaining the static field base of a
1326      * class.
1327      */
ensureClassInitialized(Class<?> c)1328     public void ensureClassInitialized(Class<?> c) {
1329         if (c == null) {
1330             throw new NullPointerException();
1331         }
1332 
1333         // Android-changed: Implementation not yet available natively (b/202380950)
1334         // ensureClassInitialized0(c);
1335         try {
1336             Class.forName(c.getName(), true, c.getClassLoader());
1337         } catch (ClassNotFoundException e) {
1338             // The function doesn't specify that it's throwing ClassNotFoundException, so it needs
1339             // to be caught here. We could rethrow as NoClassDefFoundError, however that is not
1340             // documented for this function and the upstream implementation does not throw an
1341             // exception.
1342         }
1343     }
1344 
1345     /**
1346      * Gets the offset from the start of an array object's memory to
1347      * the memory used to store its initial (zeroeth) element.
1348      *
1349      * @param clazz non-{@code null}; class in question; must be an array class
1350      * @return the offset to the initial element
1351      */
arrayBaseOffset(Class clazz)1352     public int arrayBaseOffset(Class clazz) {
1353         Class<?> component = clazz.getComponentType();
1354         if (component == null) {
1355             throw new IllegalArgumentException("Valid for array classes only: " + clazz);
1356         }
1357         return getArrayBaseOffsetForComponentType(component);
1358     }
1359 
1360     /** The value of {@code arrayBaseOffset(boolean[].class)} */
1361     public static final int ARRAY_BOOLEAN_BASE_OFFSET
1362             = theUnsafe.arrayBaseOffset(boolean[].class);
1363 
1364     /** The value of {@code arrayBaseOffset(byte[].class)} */
1365     public static final int ARRAY_BYTE_BASE_OFFSET
1366             = theUnsafe.arrayBaseOffset(byte[].class);
1367 
1368     /** The value of {@code arrayBaseOffset(short[].class)} */
1369     public static final int ARRAY_SHORT_BASE_OFFSET
1370             = theUnsafe.arrayBaseOffset(short[].class);
1371 
1372     /** The value of {@code arrayBaseOffset(char[].class)} */
1373     public static final int ARRAY_CHAR_BASE_OFFSET
1374             = theUnsafe.arrayBaseOffset(char[].class);
1375 
1376     /** The value of {@code arrayBaseOffset(int[].class)} */
1377     public static final int ARRAY_INT_BASE_OFFSET
1378             = theUnsafe.arrayBaseOffset(int[].class);
1379 
1380     /** The value of {@code arrayBaseOffset(long[].class)} */
1381     public static final int ARRAY_LONG_BASE_OFFSET
1382             = theUnsafe.arrayBaseOffset(long[].class);
1383 
1384     /** The value of {@code arrayBaseOffset(float[].class)} */
1385     public static final int ARRAY_FLOAT_BASE_OFFSET
1386             = theUnsafe.arrayBaseOffset(float[].class);
1387 
1388     /** The value of {@code arrayBaseOffset(double[].class)} */
1389     public static final int ARRAY_DOUBLE_BASE_OFFSET
1390             = theUnsafe.arrayBaseOffset(double[].class);
1391 
1392     /** The value of {@code arrayBaseOffset(Object[].class)} */
1393     public static final int ARRAY_OBJECT_BASE_OFFSET
1394             = theUnsafe.arrayBaseOffset(Object[].class);
1395 
1396     /**
1397      * Gets the size of each element of the given array class.
1398      *
1399      * @param clazz non-{@code null}; class in question; must be an array class
1400      * @return &gt; 0; the size of each element of the array
1401      */
arrayIndexScale(Class clazz)1402     public int arrayIndexScale(Class clazz) {
1403       Class<?> component = clazz.getComponentType();
1404       if (component == null) {
1405           throw new IllegalArgumentException("Valid for array classes only: " + clazz);
1406       }
1407       return getArrayIndexScaleForComponentType(component);
1408     }
1409 
1410     /** The value of {@code arrayIndexScale(boolean[].class)} */
1411     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1412             = theUnsafe.arrayIndexScale(boolean[].class);
1413 
1414     /** The value of {@code arrayIndexScale(byte[].class)} */
1415     public static final int ARRAY_BYTE_INDEX_SCALE
1416             = theUnsafe.arrayIndexScale(byte[].class);
1417 
1418     /** The value of {@code arrayIndexScale(short[].class)} */
1419     public static final int ARRAY_SHORT_INDEX_SCALE
1420             = theUnsafe.arrayIndexScale(short[].class);
1421 
1422     /** The value of {@code arrayIndexScale(char[].class)} */
1423     public static final int ARRAY_CHAR_INDEX_SCALE
1424             = theUnsafe.arrayIndexScale(char[].class);
1425 
1426     /** The value of {@code arrayIndexScale(int[].class)} */
1427     public static final int ARRAY_INT_INDEX_SCALE
1428             = theUnsafe.arrayIndexScale(int[].class);
1429 
1430     /** The value of {@code arrayIndexScale(long[].class)} */
1431     public static final int ARRAY_LONG_INDEX_SCALE
1432             = theUnsafe.arrayIndexScale(long[].class);
1433 
1434     /** The value of {@code arrayIndexScale(float[].class)} */
1435     public static final int ARRAY_FLOAT_INDEX_SCALE
1436             = theUnsafe.arrayIndexScale(float[].class);
1437 
1438     /** The value of {@code arrayIndexScale(double[].class)} */
1439     public static final int ARRAY_DOUBLE_INDEX_SCALE
1440             = theUnsafe.arrayIndexScale(double[].class);
1441 
1442     /** The value of {@code arrayIndexScale(Object[].class)} */
1443     public static final int ARRAY_OBJECT_INDEX_SCALE
1444             = theUnsafe.arrayIndexScale(Object[].class);
1445 
1446     /**
1447      * Gets the size of the address value, in bytes.
1448      *
1449      * @return the size of the address, in bytes
1450      */
1451     @FastNative
addressSize()1452     public native int addressSize();
1453 
1454     /** The value of {@code addressSize()} */
1455     // Android-changed: Use different source for the address size.
1456     // public static final int ADDRESS_SIZE = ADDRESS_SIZE0;
1457     public static final int ADDRESS_SIZE = theUnsafe.addressSize();
1458 
1459     /**
1460      * Gets the size of the memory page, in bytes.
1461      *
1462      * @return the size of the page
1463      */
1464     // Android-changed: Implemented as native call.
1465     // public int pageSize() { return PAGE_SIZE; }
1466     @FastNative
pageSize()1467     public native int pageSize();
1468 
1469     // BEGIN Android-removed: Not used in Android.
1470     /*
1471     /**
1472      * Reports the size in bytes of a data cache line written back by
1473      * the hardware cache line flush operation available to the JVM or
1474      * 0 if data cache line flushing is not enabled.
1475      * /
1476     public int dataCacheLineFlushSize() { return DATA_CACHE_LINE_FLUSH_SIZE; }
1477 
1478     /**
1479      * Rounds down address to a data cache line boundary as
1480      * determined by {@link #dataCacheLineFlushSize}
1481      * @return the rounded down address
1482      * /
1483     public long dataCacheLineAlignDown(long address) {
1484         return (address & ~(DATA_CACHE_LINE_FLUSH_SIZE - 1));
1485     }
1486 
1487     /**
1488      * Returns true if data cache line writeback
1489      * /
1490     public static boolean isWritebackEnabled() { return DATA_CACHE_LINE_FLUSH_SIZE != 0; }
1491 
1492     /// random trusted operations from JNI:
1493 
1494     /**
1495      * Tells the VM to define a class, without security checks.  By default, the
1496      * class loader and protection domain come from the caller's class.
1497      * /
1498     public Class<?> defineClass(String name, byte[] b, int off, int len,
1499                                 ClassLoader loader,
1500                                 ProtectionDomain protectionDomain) {
1501         if (b == null) {
1502             throw new NullPointerException();
1503         }
1504         if (len < 0) {
1505             throw new ArrayIndexOutOfBoundsException();
1506         }
1507 
1508         return defineClass0(name, b, off, len, loader, protectionDomain);
1509     }
1510 
1511     public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1512                                         ClassLoader loader,
1513                                         ProtectionDomain protectionDomain);
1514 
1515     /**
1516      * Allocates an instance but does not run any constructor.
1517      * Initializes the class if it has not yet been.
1518      * /
1519     @IntrinsicCandidate
1520     public native Object allocateInstance(Class<?> cls)
1521         throws InstantiationException;
1522 
1523     /**
1524      * Allocates an array of a given type, but does not do zeroing.
1525      * <p>
1526      * This method should only be used in the very rare cases where a high-performance code
1527      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1528      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1529      * <p>
1530      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1531      * before allowing untrusted code, or code in other threads, to observe the reference
1532      * to the newly allocated array. In addition, the publication of the array reference must be
1533      * safe according to the Java Memory Model requirements.
1534      * <p>
1535      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1536      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1537      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1538      * or issuing a {@link #storeFence} before publishing the reference.
1539      * <p>
1540      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1541      * elements that could break heap integrity.
1542      *
1543      * @param componentType array component type to allocate
1544      * @param length array size to allocate
1545      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1546      *                                  or the length is negative
1547      * /
1548     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1549        if (componentType == null) {
1550            throw new IllegalArgumentException("Component type is null");
1551        }
1552        if (!componentType.isPrimitive()) {
1553            throw new IllegalArgumentException("Component type is not primitive");
1554        }
1555        if (length < 0) {
1556            throw new IllegalArgumentException("Negative length");
1557        }
1558        return allocateUninitializedArray0(componentType, length);
1559     }
1560      */
1561     // END Android-removed: Not used in Android.
1562 
1563 
1564     /**
1565      * Allocates an instance of the given class without running the constructor.
1566      * The class' <clinit> will be run, if necessary.
1567      */
1568     @IntrinsicCandidate
allocateInstance(Class<?> cls)1569     public native Object allocateInstance(Class<?> cls);
1570     // Android-changed: No throw specification
1571     //     throws InstantiationException;
1572 
1573     // BEGIN Android-removed: Not used in Android.
1574     /*
1575     /**
1576      * Allocates an array of a given type, but does not do zeroing.
1577      * <p>
1578      * This method should only be used in the very rare cases where a high-performance code
1579      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1580      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1581      * <p>
1582      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1583      * before allowing untrusted code, or code in other threads, to observe the reference
1584      * to the newly allocated array. In addition, the publication of the array reference must be
1585      * safe according to the Java Memory Model requirements.
1586      * <p>
1587      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1588      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1589      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1590      * or issuing a {@link #storeFence} before publishing the reference.
1591      * <p>
1592      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1593      * elements that could break heap integrity.
1594      *
1595      * @param componentType array component type to allocate
1596      * @param length array size to allocate
1597      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1598      *                                  or the length is negative
1599      * /
1600     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1601        if (componentType == null) {
1602            throw new IllegalArgumentException("Component type is null");
1603        }
1604        if (!componentType.isPrimitive()) {
1605            throw new IllegalArgumentException("Component type is not primitive");
1606        }
1607        if (length < 0) {
1608            throw new IllegalArgumentException("Negative length");
1609        }
1610        return allocateUninitializedArray0(componentType, length);
1611     }
1612 
1613     @IntrinsicCandidate
1614     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1615        // These fallbacks provide zeroed arrays, but intrinsic is not required to
1616        // return the zeroed arrays.
1617        if (componentType == byte.class)    return new byte[length];
1618        if (componentType == boolean.class) return new boolean[length];
1619        if (componentType == short.class)   return new short[length];
1620        if (componentType == char.class)    return new char[length];
1621        if (componentType == int.class)     return new int[length];
1622        if (componentType == float.class)   return new float[length];
1623        if (componentType == long.class)    return new long[length];
1624        if (componentType == double.class)  return new double[length];
1625        return null;
1626     }
1627 
1628     /** Throws the exception without telling the verifier. * /
1629     public native void throwException(Throwable ee);
1630 
1631      */
1632     // END Android-removed: Not used in Android.
1633 
1634     /**
1635      * Atomically updates Java variable to {@code x} if it is currently
1636      * holding {@code expected}.
1637      *
1638      * <p>This operation has memory semantics of a {@code volatile} read
1639      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1640      *
1641      * @return {@code true} if successful
1642      */
1643     // Android-added: FastNative annotation.
1644     @FastNative
1645     @IntrinsicCandidate
compareAndSetReference(Object o, long offset, Object expected, Object x)1646     public final native boolean compareAndSetReference(Object o, long offset,
1647                                                        Object expected,
1648                                                        Object x);
1649 
1650     // BEGIN Android-removed: Not used in Android.
1651     /*
1652     @IntrinsicCandidate
1653     public final native Object compareAndExchangeReference(Object o, long offset,
1654                                                            Object expected,
1655                                                            Object x);
1656 
1657     @IntrinsicCandidate
1658     public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1659                                                            Object expected,
1660                                                            Object x) {
1661         return compareAndExchangeReference(o, offset, expected, x);
1662     }
1663 
1664     @IntrinsicCandidate
1665     public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1666                                                            Object expected,
1667                                                            Object x) {
1668         return compareAndExchangeReference(o, offset, expected, x);
1669     }
1670 
1671     @IntrinsicCandidate
1672     public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1673                                                          Object expected,
1674                                                          Object x) {
1675         return compareAndSetReference(o, offset, expected, x);
1676     }
1677 
1678     @IntrinsicCandidate
1679     public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1680                                                            Object expected,
1681                                                            Object x) {
1682         return compareAndSetReference(o, offset, expected, x);
1683     }
1684 
1685     @IntrinsicCandidate
1686     public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1687                                                            Object expected,
1688                                                            Object x) {
1689         return compareAndSetReference(o, offset, expected, x);
1690     }
1691      */
1692     // END Android-removed: Not used in Android.
1693 
1694     @IntrinsicCandidate
weakCompareAndSetReference(Object o, long offset, Object expected, Object x)1695     public final boolean weakCompareAndSetReference(Object o, long offset,
1696                                                     Object expected,
1697                                                     Object x) {
1698         return compareAndSetReference(o, offset, expected, x);
1699     }
1700 
1701     /**
1702      * Atomically updates Java variable to {@code x} if it is currently
1703      * holding {@code expected}.
1704      *
1705      * <p>This operation has memory semantics of a {@code volatile} read
1706      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1707      *
1708      * @return {@code true} if successful
1709      */
1710     // Android-added: FastNative annotation.
1711     @FastNative
1712     @IntrinsicCandidate
compareAndSetInt(Object o, long offset, int expected, int x)1713     public final native boolean compareAndSetInt(Object o, long offset,
1714                                                  int expected,
1715                                                  int x);
1716 
1717     // BEGIN Android-removed: Not used in Android.
1718     /*
1719     @IntrinsicCandidate
1720     public final native int compareAndExchangeInt(Object o, long offset,
1721                                                   int expected,
1722                                                   int x);
1723 
1724     @IntrinsicCandidate
1725     public final int compareAndExchangeIntAcquire(Object o, long offset,
1726                                                          int expected,
1727                                                          int x) {
1728         return compareAndExchangeInt(o, offset, expected, x);
1729     }
1730 
1731     @IntrinsicCandidate
1732     public final int compareAndExchangeIntRelease(Object o, long offset,
1733                                                          int expected,
1734                                                          int x) {
1735         return compareAndExchangeInt(o, offset, expected, x);
1736     }
1737 
1738     @IntrinsicCandidate
1739     public final boolean weakCompareAndSetIntPlain(Object o, long offset,
1740                                                    int expected,
1741                                                    int x) {
1742         return compareAndSetInt(o, offset, expected, x);
1743     }
1744 
1745     @IntrinsicCandidate
1746     public final boolean weakCompareAndSetIntAcquire(Object o, long offset,
1747                                                      int expected,
1748                                                      int x) {
1749         return compareAndSetInt(o, offset, expected, x);
1750     }
1751 
1752     @IntrinsicCandidate
1753     public final boolean weakCompareAndSetIntRelease(Object o, long offset,
1754                                                      int expected,
1755                                                      int x) {
1756         return compareAndSetInt(o, offset, expected, x);
1757     }
1758      */
1759     // END Android-removed: Not used in Android.
1760 
1761     @IntrinsicCandidate
weakCompareAndSetInt(Object o, long offset, int expected, int x)1762     public final boolean weakCompareAndSetInt(Object o, long offset,
1763                                               int expected,
1764                                               int x) {
1765         return compareAndSetInt(o, offset, expected, x);
1766     }
1767 
1768     // BEGIN Android-removed: Not used in Android.
1769     /*
1770     @IntrinsicCandidate
1771     public final byte compareAndExchangeByte(Object o, long offset,
1772                                              byte expected,
1773                                              byte x) {
1774         long wordOffset = offset & ~3;
1775         int shift = (int) (offset & 3) << 3;
1776         if (BIG_ENDIAN) {
1777             shift = 24 - shift;
1778         }
1779         int mask           = 0xFF << shift;
1780         int maskedExpected = (expected & 0xFF) << shift;
1781         int maskedX        = (x & 0xFF) << shift;
1782         int fullWord;
1783         do {
1784             fullWord = getIntVolatile(o, wordOffset);
1785             if ((fullWord & mask) != maskedExpected)
1786                 return (byte) ((fullWord & mask) >> shift);
1787         } while (!weakCompareAndSetInt(o, wordOffset,
1788                                                 fullWord, (fullWord & ~mask) | maskedX));
1789         return expected;
1790     }
1791 
1792     @IntrinsicCandidate
1793     public final boolean compareAndSetByte(Object o, long offset,
1794                                            byte expected,
1795                                            byte x) {
1796         return compareAndExchangeByte(o, offset, expected, x) == expected;
1797     }
1798 
1799     @IntrinsicCandidate
1800     public final boolean weakCompareAndSetByte(Object o, long offset,
1801                                                byte expected,
1802                                                byte x) {
1803         return compareAndSetByte(o, offset, expected, x);
1804     }
1805 
1806     @IntrinsicCandidate
1807     public final boolean weakCompareAndSetByteAcquire(Object o, long offset,
1808                                                       byte expected,
1809                                                       byte x) {
1810         return weakCompareAndSetByte(o, offset, expected, x);
1811     }
1812 
1813     @IntrinsicCandidate
1814     public final boolean weakCompareAndSetByteRelease(Object o, long offset,
1815                                                       byte expected,
1816                                                       byte x) {
1817         return weakCompareAndSetByte(o, offset, expected, x);
1818     }
1819 
1820     @IntrinsicCandidate
1821     public final boolean weakCompareAndSetBytePlain(Object o, long offset,
1822                                                     byte expected,
1823                                                     byte x) {
1824         return weakCompareAndSetByte(o, offset, expected, x);
1825     }
1826 
1827     @IntrinsicCandidate
1828     public final byte compareAndExchangeByteAcquire(Object o, long offset,
1829                                                     byte expected,
1830                                                     byte x) {
1831         return compareAndExchangeByte(o, offset, expected, x);
1832     }
1833 
1834     @IntrinsicCandidate
1835     public final byte compareAndExchangeByteRelease(Object o, long offset,
1836                                                     byte expected,
1837                                                     byte x) {
1838         return compareAndExchangeByte(o, offset, expected, x);
1839     }
1840 
1841     @IntrinsicCandidate
1842     public final short compareAndExchangeShort(Object o, long offset,
1843                                                short expected,
1844                                                short x) {
1845         if ((offset & 3) == 3) {
1846             throw new IllegalArgumentException("Update spans the word, not supported");
1847         }
1848         long wordOffset = offset & ~3;
1849         int shift = (int) (offset & 3) << 3;
1850         if (BIG_ENDIAN) {
1851             shift = 16 - shift;
1852         }
1853         int mask           = 0xFFFF << shift;
1854         int maskedExpected = (expected & 0xFFFF) << shift;
1855         int maskedX        = (x & 0xFFFF) << shift;
1856         int fullWord;
1857         do {
1858             fullWord = getIntVolatile(o, wordOffset);
1859             if ((fullWord & mask) != maskedExpected) {
1860                 return (short) ((fullWord & mask) >> shift);
1861             }
1862         } while (!weakCompareAndSetInt(o, wordOffset,
1863                                                 fullWord, (fullWord & ~mask) | maskedX));
1864         return expected;
1865     }
1866 
1867     @IntrinsicCandidate
1868     public final boolean compareAndSetShort(Object o, long offset,
1869                                             short expected,
1870                                             short x) {
1871         return compareAndExchangeShort(o, offset, expected, x) == expected;
1872     }
1873 
1874     @IntrinsicCandidate
1875     public final boolean weakCompareAndSetShort(Object o, long offset,
1876                                                 short expected,
1877                                                 short x) {
1878         return compareAndSetShort(o, offset, expected, x);
1879     }
1880 
1881     @IntrinsicCandidate
1882     public final boolean weakCompareAndSetShortAcquire(Object o, long offset,
1883                                                        short expected,
1884                                                        short x) {
1885         return weakCompareAndSetShort(o, offset, expected, x);
1886     }
1887 
1888     @IntrinsicCandidate
1889     public final boolean weakCompareAndSetShortRelease(Object o, long offset,
1890                                                        short expected,
1891                                                        short x) {
1892         return weakCompareAndSetShort(o, offset, expected, x);
1893     }
1894 
1895     @IntrinsicCandidate
1896     public final boolean weakCompareAndSetShortPlain(Object o, long offset,
1897                                                      short expected,
1898                                                      short x) {
1899         return weakCompareAndSetShort(o, offset, expected, x);
1900     }
1901 
1902 
1903     @IntrinsicCandidate
1904     public final short compareAndExchangeShortAcquire(Object o, long offset,
1905                                                      short expected,
1906                                                      short x) {
1907         return compareAndExchangeShort(o, offset, expected, x);
1908     }
1909 
1910     @IntrinsicCandidate
1911     public final short compareAndExchangeShortRelease(Object o, long offset,
1912                                                     short expected,
1913                                                     short x) {
1914         return compareAndExchangeShort(o, offset, expected, x);
1915     }
1916 
1917     @ForceInline
1918     private char s2c(short s) {
1919         return (char) s;
1920     }
1921 
1922     @ForceInline
1923     private short c2s(char s) {
1924         return (short) s;
1925     }
1926 
1927     @ForceInline
1928     public final boolean compareAndSetChar(Object o, long offset,
1929                                            char expected,
1930                                            char x) {
1931         return compareAndSetShort(o, offset, c2s(expected), c2s(x));
1932     }
1933 
1934     @ForceInline
1935     public final char compareAndExchangeChar(Object o, long offset,
1936                                              char expected,
1937                                              char x) {
1938         return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x)));
1939     }
1940 
1941     @ForceInline
1942     public final char compareAndExchangeCharAcquire(Object o, long offset,
1943                                             char expected,
1944                                             char x) {
1945         return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x)));
1946     }
1947 
1948     @ForceInline
1949     public final char compareAndExchangeCharRelease(Object o, long offset,
1950                                             char expected,
1951                                             char x) {
1952         return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x)));
1953     }
1954 
1955     @ForceInline
1956     public final boolean weakCompareAndSetChar(Object o, long offset,
1957                                                char expected,
1958                                                char x) {
1959         return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x));
1960     }
1961 
1962     @ForceInline
1963     public final boolean weakCompareAndSetCharAcquire(Object o, long offset,
1964                                                       char expected,
1965                                                       char x) {
1966         return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x));
1967     }
1968 
1969     @ForceInline
1970     public final boolean weakCompareAndSetCharRelease(Object o, long offset,
1971                                                       char expected,
1972                                                       char x) {
1973         return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x));
1974     }
1975 
1976     @ForceInline
1977     public final boolean weakCompareAndSetCharPlain(Object o, long offset,
1978                                                     char expected,
1979                                                     char x) {
1980         return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x));
1981     }
1982 
1983     /**
1984      * The JVM converts integral values to boolean values using two
1985      * different conventions, byte testing against zero and truncation
1986      * to least-significant bit.
1987      *
1988      * <p>The JNI documents specify that, at least for returning
1989      * values from native methods, a Java boolean value is converted
1990      * to the value-set 0..1 by first truncating to a byte (0..255 or
1991      * maybe -128..127) and then testing against zero. Thus, Java
1992      * booleans in non-Java data structures are by convention
1993      * represented as 8-bit containers containing either zero (for
1994      * false) or any non-zero value (for true).
1995      *
1996      * <p>Java booleans in the heap are also stored in bytes, but are
1997      * strongly normalized to the value-set 0..1 (i.e., they are
1998      * truncated to the least-significant bit).
1999      *
2000      * <p>The main reason for having different conventions for
2001      * conversion is performance: Truncation to the least-significant
2002      * bit can be usually implemented with fewer (machine)
2003      * instructions than byte testing against zero.
2004      *
2005      * <p>A number of Unsafe methods load boolean values from the heap
2006      * as bytes. Unsafe converts those values according to the JNI
2007      * rules (i.e, using the "testing against zero" convention). The
2008      * method {@code byte2bool} implements that conversion.
2009      *
2010      * @param b the byte to be converted to boolean
2011      * @return the result of the conversion
2012      * /
2013     @ForceInline
2014     private boolean byte2bool(byte b) {
2015         return b != 0;
2016     }
2017 
2018     /**
2019      * Convert a boolean value to a byte. The return value is strongly
2020      * normalized to the value-set 0..1 (i.e., the value is truncated
2021      * to the least-significant bit). See {@link #byte2bool(byte)} for
2022      * more details on conversion conventions.
2023      *
2024      * @param b the boolean to be converted to byte (and then normalized)
2025      * @return the result of the conversion
2026      * /
2027     @ForceInline
2028     private byte bool2byte(boolean b) {
2029         return b ? (byte)1 : (byte)0;
2030     }
2031 
2032     @ForceInline
2033     public final boolean compareAndSetBoolean(Object o, long offset,
2034                                               boolean expected,
2035                                               boolean x) {
2036         return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2037     }
2038 
2039     @ForceInline
2040     public final boolean compareAndExchangeBoolean(Object o, long offset,
2041                                                    boolean expected,
2042                                                    boolean x) {
2043         return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x)));
2044     }
2045 
2046     @ForceInline
2047     public final boolean compareAndExchangeBooleanAcquire(Object o, long offset,
2048                                                     boolean expected,
2049                                                     boolean x) {
2050         return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x)));
2051     }
2052 
2053     @ForceInline
2054     public final boolean compareAndExchangeBooleanRelease(Object o, long offset,
2055                                                        boolean expected,
2056                                                        boolean x) {
2057         return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x)));
2058     }
2059 
2060     @ForceInline
2061     public final boolean weakCompareAndSetBoolean(Object o, long offset,
2062                                                   boolean expected,
2063                                                   boolean x) {
2064         return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x));
2065     }
2066 
2067     @ForceInline
2068     public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset,
2069                                                          boolean expected,
2070                                                          boolean x) {
2071         return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x));
2072     }
2073 
2074     @ForceInline
2075     public final boolean weakCompareAndSetBooleanRelease(Object o, long offset,
2076                                                          boolean expected,
2077                                                          boolean x) {
2078         return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x));
2079     }
2080 
2081     @ForceInline
2082     public final boolean weakCompareAndSetBooleanPlain(Object o, long offset,
2083                                                        boolean expected,
2084                                                        boolean x) {
2085         return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x));
2086     }
2087 
2088     @ForceInline
2089     public final boolean compareAndSetFloat(Object o, long offset,
2090                                             float expected,
2091                                             float x) {
2092         return compareAndSetInt(o, offset,
2093                                  Float.floatToRawIntBits(expected),
2094                                  Float.floatToRawIntBits(x));
2095     }
2096 
2097     @ForceInline
2098     public final float compareAndExchangeFloat(Object o, long offset,
2099                                                float expected,
2100                                                float x) {
2101         int w = compareAndExchangeInt(o, offset,
2102                                       Float.floatToRawIntBits(expected),
2103                                       Float.floatToRawIntBits(x));
2104         return Float.intBitsToFloat(w);
2105     }
2106 
2107     @ForceInline
2108     public final float compareAndExchangeFloatAcquire(Object o, long offset,
2109                                                   float expected,
2110                                                   float x) {
2111         int w = compareAndExchangeIntAcquire(o, offset,
2112                                              Float.floatToRawIntBits(expected),
2113                                              Float.floatToRawIntBits(x));
2114         return Float.intBitsToFloat(w);
2115     }
2116 
2117     @ForceInline
2118     public final float compareAndExchangeFloatRelease(Object o, long offset,
2119                                                   float expected,
2120                                                   float x) {
2121         int w = compareAndExchangeIntRelease(o, offset,
2122                                              Float.floatToRawIntBits(expected),
2123                                              Float.floatToRawIntBits(x));
2124         return Float.intBitsToFloat(w);
2125     }
2126 
2127     @ForceInline
2128     public final boolean weakCompareAndSetFloatPlain(Object o, long offset,
2129                                                      float expected,
2130                                                      float x) {
2131         return weakCompareAndSetIntPlain(o, offset,
2132                                      Float.floatToRawIntBits(expected),
2133                                      Float.floatToRawIntBits(x));
2134     }
2135 
2136     @ForceInline
2137     public final boolean weakCompareAndSetFloatAcquire(Object o, long offset,
2138                                                        float expected,
2139                                                        float x) {
2140         return weakCompareAndSetIntAcquire(o, offset,
2141                                             Float.floatToRawIntBits(expected),
2142                                             Float.floatToRawIntBits(x));
2143     }
2144 
2145     @ForceInline
2146     public final boolean weakCompareAndSetFloatRelease(Object o, long offset,
2147                                                        float expected,
2148                                                        float x) {
2149         return weakCompareAndSetIntRelease(o, offset,
2150                                             Float.floatToRawIntBits(expected),
2151                                             Float.floatToRawIntBits(x));
2152     }
2153 
2154     @ForceInline
2155     public final boolean weakCompareAndSetFloat(Object o, long offset,
2156                                                 float expected,
2157                                                 float x) {
2158         return weakCompareAndSetInt(o, offset,
2159                                              Float.floatToRawIntBits(expected),
2160                                              Float.floatToRawIntBits(x));
2161     }
2162 
2163     /**
2164      * Atomically updates Java variable to {@code x} if it is currently
2165      * holding {@code expected}.
2166      *
2167      * <p>This operation has memory semantics of a {@code volatile} read
2168      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2169      *
2170      * @return {@code true} if successful
2171      * /
2172     @ForceInline
2173     public final boolean compareAndSetDouble(Object o, long offset,
2174                                              double expected,
2175                                              double x) {
2176         return compareAndSetLong(o, offset,
2177                                  Double.doubleToRawLongBits(expected),
2178                                  Double.doubleToRawLongBits(x));
2179     }
2180 
2181     @ForceInline
2182     public final double compareAndExchangeDouble(Object o, long offset,
2183                                                  double expected,
2184                                                  double x) {
2185         long w = compareAndExchangeLong(o, offset,
2186                                         Double.doubleToRawLongBits(expected),
2187                                         Double.doubleToRawLongBits(x));
2188         return Double.longBitsToDouble(w);
2189     }
2190 
2191     @ForceInline
2192     public final double compareAndExchangeDoubleAcquire(Object o, long offset,
2193                                                         double expected,
2194                                                         double x) {
2195         long w = compareAndExchangeLongAcquire(o, offset,
2196                                                Double.doubleToRawLongBits(expected),
2197                                                Double.doubleToRawLongBits(x));
2198         return Double.longBitsToDouble(w);
2199     }
2200 
2201     @ForceInline
2202     public final double compareAndExchangeDoubleRelease(Object o, long offset,
2203                                                         double expected,
2204                                                         double x) {
2205         long w = compareAndExchangeLongRelease(o, offset,
2206                                                Double.doubleToRawLongBits(expected),
2207                                                Double.doubleToRawLongBits(x));
2208         return Double.longBitsToDouble(w);
2209     }
2210 
2211     @ForceInline
2212     public final boolean weakCompareAndSetDoublePlain(Object o, long offset,
2213                                                       double expected,
2214                                                       double x) {
2215         return weakCompareAndSetLongPlain(o, offset,
2216                                      Double.doubleToRawLongBits(expected),
2217                                      Double.doubleToRawLongBits(x));
2218     }
2219 
2220     @ForceInline
2221     public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset,
2222                                                         double expected,
2223                                                         double x) {
2224         return weakCompareAndSetLongAcquire(o, offset,
2225                                              Double.doubleToRawLongBits(expected),
2226                                              Double.doubleToRawLongBits(x));
2227     }
2228 
2229     @ForceInline
2230     public final boolean weakCompareAndSetDoubleRelease(Object o, long offset,
2231                                                         double expected,
2232                                                         double x) {
2233         return weakCompareAndSetLongRelease(o, offset,
2234                                              Double.doubleToRawLongBits(expected),
2235                                              Double.doubleToRawLongBits(x));
2236     }
2237 
2238     @ForceInline
2239     public final boolean weakCompareAndSetDouble(Object o, long offset,
2240                                                  double expected,
2241                                                  double x) {
2242         return weakCompareAndSetLong(o, offset,
2243                                               Double.doubleToRawLongBits(expected),
2244                                               Double.doubleToRawLongBits(x));
2245     }
2246      */
2247     // END Android-removed: Not used in Android.
2248 
2249     /**
2250      * Atomically updates Java variable to {@code x} if it is currently
2251      * holding {@code expected}.
2252      *
2253      * <p>This operation has memory semantics of a {@code volatile} read
2254      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
2255      *
2256      * @return {@code true} if successful
2257      */
2258     // Android-added: FastNative annotation.
2259     @FastNative
2260     @IntrinsicCandidate
compareAndSetLong(Object o, long offset, long expected, long x)2261     public final native boolean compareAndSetLong(Object o, long offset,
2262                                                   long expected,
2263                                                   long x);
2264 
2265     /**
2266      * @hide
2267      */
2268     // Android-added: FastNative annotation.
2269     @FastNative
2270     @IntrinsicCandidate
compareAndExchangeLong(Object o, long offset, long expected, long x)2271     public final native long compareAndExchangeLong(Object o, long offset,
2272                                                     long expected,
2273                                                     long x);
2274 
2275     // BEGIN Android-removed: Not used in Android.
2276     /*
2277     @IntrinsicCandidate
2278     public final long compareAndExchangeLongAcquire(Object o, long offset,
2279                                                            long expected,
2280                                                            long x) {
2281         return compareAndExchangeLong(o, offset, expected, x);
2282     }
2283 
2284     @IntrinsicCandidate
2285     public final long compareAndExchangeLongRelease(Object o, long offset,
2286                                                            long expected,
2287                                                            long x) {
2288         return compareAndExchangeLong(o, offset, expected, x);
2289     }
2290 
2291     @IntrinsicCandidate
2292     public final boolean weakCompareAndSetLongPlain(Object o, long offset,
2293                                                     long expected,
2294                                                     long x) {
2295         return compareAndSetLong(o, offset, expected, x);
2296     }
2297 
2298     @IntrinsicCandidate
2299     public final boolean weakCompareAndSetLongAcquire(Object o, long offset,
2300                                                       long expected,
2301                                                       long x) {
2302         return compareAndSetLong(o, offset, expected, x);
2303     }
2304 
2305     @IntrinsicCandidate
2306     public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2307                                                       long expected,
2308                                                       long x) {
2309         return compareAndSetLong(o, offset, expected, x);
2310     }
2311 
2312     @IntrinsicCandidate
2313     public final boolean weakCompareAndSetLong(Object o, long offset,
2314                                                long expected,
2315                                                long x) {
2316         return compareAndSetLong(o, offset, expected, x);
2317     }
2318      */
2319     // END Android-removed: Not used in Android.
2320 
2321     /**
2322      * Fetches a reference value from a given Java variable, with volatile
2323      * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2324      */
2325     // Android-added: FastNative annotation.
2326     @FastNative
2327     @IntrinsicCandidate
getReferenceVolatile(Object o, long offset)2328     public native Object getReferenceVolatile(Object o, long offset);
2329 
2330     /**
2331      * Stores a reference value into a given Java variable, with
2332      * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2333      */
2334     // Android-added: FastNative annotation.
2335     @FastNative
2336     @IntrinsicCandidate
putReferenceVolatile(Object o, long offset, Object x)2337     public native void putReferenceVolatile(Object o, long offset, Object x);
2338 
2339     /**
2340      * Gets an {@code int} field from the given object,
2341      * using {@code volatile} semantics.
2342      *
2343      * @param obj non-{@code null}; object containing the field
2344      * @param offset offset to the field within {@code obj}
2345      * @return the retrieved value
2346      */
2347     // Android-added: FastNative annotation.
2348     @FastNative
2349     @IntrinsicCandidate
getIntVolatile(Object obj, long offset)2350     public native int getIntVolatile(Object obj, long offset);
2351 
2352     /**
2353      * Stores an {@code int} field into the given object,
2354      * using {@code volatile} semantics.
2355      *
2356      * @param obj non-{@code null}; object containing the field
2357      * @param offset offset to the field within {@code obj}
2358      * @param newValue the value to store
2359      */
2360     // Android-added: FastNative annotation.
2361     @FastNative
2362     @IntrinsicCandidate
putIntVolatile(Object obj, long offset, int newValue)2363     public native void putIntVolatile(Object obj, long offset, int newValue);
2364 
2365     /** Volatile version of {@link #getBoolean(Object, long)}  */
2366     // Android-added: FastNative annotation.
2367     @FastNative
2368     @IntrinsicCandidate
getBooleanVolatile(Object o, long offset)2369     public native boolean getBooleanVolatile(Object o, long offset);
2370 
2371     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2372     // Android-added: FastNative annotation.
2373     @FastNative
2374     @IntrinsicCandidate
putBooleanVolatile(Object o, long offset, boolean x)2375     public native void    putBooleanVolatile(Object o, long offset, boolean x);
2376 
2377     /** Volatile version of {@link #getByte(Object, long)}  */
2378     // Android-added: FastNative annotation.
2379     @FastNative
2380     @IntrinsicCandidate
getByteVolatile(Object o, long offset)2381     public native byte    getByteVolatile(Object o, long offset);
2382 
2383     /** Volatile version of {@link #putByte(Object, long, byte)}  */
2384     // Android-added: FastNative annotation.
2385     @FastNative
2386     @IntrinsicCandidate
putByteVolatile(Object o, long offset, byte x)2387     public native void    putByteVolatile(Object o, long offset, byte x);
2388 
2389     /** Volatile version of {@link #getShort(Object, long)}  */
2390     // Android-added: FastNative annotation.
2391     @FastNative
2392     @IntrinsicCandidate
getShortVolatile(Object o, long offset)2393     public native short   getShortVolatile(Object o, long offset);
2394 
2395     /** Volatile version of {@link #putShort(Object, long, short)}  */
2396     // Android-added: FastNative annotation.
2397     @FastNative
2398     @IntrinsicCandidate
putShortVolatile(Object o, long offset, short x)2399     public native void    putShortVolatile(Object o, long offset, short x);
2400 
2401     /** Volatile version of {@link #getChar(Object, long)}  */
2402     // Android-added: FastNative annotation.
2403     @FastNative
2404     @IntrinsicCandidate
getCharVolatile(Object o, long offset)2405     public native char    getCharVolatile(Object o, long offset);
2406 
2407     /** Volatile version of {@link #putChar(Object, long, char)}  */
2408     // Android-added: FastNative annotation.
2409     @FastNative
2410     @IntrinsicCandidate
putCharVolatile(Object o, long offset, char x)2411     public native void    putCharVolatile(Object o, long offset, char x);
2412 
2413     /**
2414      * Gets a {@code long} field from the given object,
2415      * using {@code volatile} semantics.
2416      *
2417      * @param obj non-{@code null}; object containing the field
2418      * @param offset offset to the field within {@code obj}
2419      * @return the retrieved value
2420      */
2421     // Android-added: FastNative annotation.
2422     @FastNative
2423     @IntrinsicCandidate
getLongVolatile(Object obj, long offset)2424     public native long getLongVolatile(Object obj, long offset);
2425 
2426     /**
2427      * Stores a {@code long} field into the given object,
2428      * using {@code volatile} semantics.
2429      *
2430      * @param obj non-{@code null}; object containing the field
2431      * @param offset offset to the field within {@code obj}
2432      * @param newValue the value to store
2433      */
2434     // Android-added: FastNative annotation.
2435     @FastNative
2436     @IntrinsicCandidate
putLongVolatile(Object obj, long offset, long newValue)2437     public native void putLongVolatile(Object obj, long offset, long newValue);
2438 
2439     /** Volatile version of {@link #getFloat(Object, long)}  */
2440     // Android-added: FastNative annotation.
2441     @FastNative
2442     @IntrinsicCandidate
getFloatVolatile(Object o, long offset)2443     public native float   getFloatVolatile(Object o, long offset);
2444 
2445     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2446     // Android-added: FastNative annotation.
2447     @FastNative
2448     @IntrinsicCandidate
putFloatVolatile(Object o, long offset, float x)2449     public native void    putFloatVolatile(Object o, long offset, float x);
2450 
2451     /** Volatile version of {@link #getDouble(Object, long)}  */
2452     // Android-added: FastNative annotation.
2453     @FastNative
2454     @IntrinsicCandidate
getDoubleVolatile(Object o, long offset)2455     public native double  getDoubleVolatile(Object o, long offset);
2456 
2457     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2458     // Android-added: FastNative annotation.
2459     @FastNative
2460     @IntrinsicCandidate
putDoubleVolatile(Object o, long offset, double x)2461     public native void    putDoubleVolatile(Object o, long offset, double x);
2462 
2463     /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2464     @IntrinsicCandidate
getReferenceAcquire(Object o, long offset)2465     public final Object getReferenceAcquire(Object o, long offset) {
2466         return getReferenceVolatile(o, offset);
2467     }
2468 
2469     // BEGIN Android-removed: Not used in Android.
2470     /*
2471     /** Acquire version of {@link #getBooleanVolatile(Object, long)} * /
2472     @IntrinsicCandidate
2473     public final boolean getBooleanAcquire(Object o, long offset) {
2474         return getBooleanVolatile(o, offset);
2475     }
2476 
2477     /** Acquire version of {@link #getByteVolatile(Object, long)} * /
2478     @IntrinsicCandidate
2479     public final byte getByteAcquire(Object o, long offset) {
2480         return getByteVolatile(o, offset);
2481     }
2482 
2483     /** Acquire version of {@link #getShortVolatile(Object, long)} * /
2484     @IntrinsicCandidate
2485     public final short getShortAcquire(Object o, long offset) {
2486         return getShortVolatile(o, offset);
2487     }
2488 
2489     /** Acquire version of {@link #getCharVolatile(Object, long)} * /
2490     @IntrinsicCandidate
2491     public final char getCharAcquire(Object o, long offset) {
2492         return getCharVolatile(o, offset);
2493     }
2494      */
2495     // END Android-removed: Not used in Android.
2496 
2497     /** Acquire version of {@link #getIntVolatile(Object, long)} */
2498     @IntrinsicCandidate
getIntAcquire(Object o, long offset)2499     public final int getIntAcquire(Object o, long offset) {
2500         return getIntVolatile(o, offset);
2501     }
2502 
2503     // BEGIN Android-removed: Not used in Android.
2504     /*
2505     /** Acquire version of {@link #getFloatVolatile(Object, long)} * /
2506     @IntrinsicCandidate
2507     public final float getFloatAcquire(Object o, long offset) {
2508         return getFloatVolatile(o, offset);
2509     }
2510      */
2511     // END Android-removed: Not used in Android.
2512 
2513     /** Acquire version of {@link #getLongVolatile(Object, long)} */
2514     @IntrinsicCandidate
getLongAcquire(Object o, long offset)2515     public final long getLongAcquire(Object o, long offset) {
2516         return getLongVolatile(o, offset);
2517     }
2518 
2519     // BEGIN Android-removed: Not used in Android.
2520     /*
2521     /** Acquire version of {@link #getDoubleVolatile(Object, long)} * /
2522     @IntrinsicCandidate
2523     public final double getDoubleAcquire(Object o, long offset) {
2524         return getDoubleVolatile(o, offset);
2525     }
2526 
2527     /*
2528      * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2529      * that do not guarantee immediate visibility of the store to
2530      * other threads. This method is generally only useful if the
2531      * underlying field is a Java volatile (or if an array cell, one
2532      * that is otherwise only accessed using volatile accesses).
2533      *
2534      * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2535      * /
2536      */
2537     // END Android-removed: Not used in Android.
2538 
2539     /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2540     @IntrinsicCandidate
putReferenceRelease(Object o, long offset, Object x)2541     public final void putReferenceRelease(Object o, long offset, Object x) {
2542         putReferenceVolatile(o, offset, x);
2543     }
2544 
2545     // BEGIN Android-removed: Not used in Android.
2546     /*
2547     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} * /
2548     @IntrinsicCandidate
2549     public final void putBooleanRelease(Object o, long offset, boolean x) {
2550         putBooleanVolatile(o, offset, x);
2551     }
2552 
2553     /** Release version of {@link #putByteVolatile(Object, long, byte)} * /
2554     @IntrinsicCandidate
2555     public final void putByteRelease(Object o, long offset, byte x) {
2556         putByteVolatile(o, offset, x);
2557     }
2558 
2559     /** Release version of {@link #putShortVolatile(Object, long, short)} * /
2560     @IntrinsicCandidate
2561     public final void putShortRelease(Object o, long offset, short x) {
2562         putShortVolatile(o, offset, x);
2563     }
2564 
2565     /** Release version of {@link #putCharVolatile(Object, long, char)} * /
2566     @IntrinsicCandidate
2567     public final void putCharRelease(Object o, long offset, char x) {
2568         putCharVolatile(o, offset, x);
2569     }
2570      */
2571     // END Android-removed: Not used in Android.
2572 
2573     /** Release version of {@link #putIntVolatile(Object, long, int)} */
2574     @IntrinsicCandidate
putIntRelease(Object o, long offset, int x)2575     public final void putIntRelease(Object o, long offset, int x) {
2576         putIntVolatile(o, offset, x);
2577     }
2578 
2579     // BEGIN Android-removed: Not used in Android.
2580     /*
2581     /** Release version of {@link #putFloatVolatile(Object, long, float)} * /
2582     @IntrinsicCandidate
2583     public final void putFloatRelease(Object o, long offset, float x) {
2584         putFloatVolatile(o, offset, x);
2585     }
2586       */
2587     // END Android-removed: Not used in Android.
2588 
2589     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2590     @IntrinsicCandidate
putLongRelease(Object o, long offset, long x)2591     public final void putLongRelease(Object o, long offset, long x) {
2592         putLongVolatile(o, offset, x);
2593     }
2594 
2595     // BEGIN Android-removed: Not used in Android.
2596     /*
2597     /** Release version of {@link #putDoubleVolatile(Object, long, double)} * /
2598     @IntrinsicCandidate
2599     public final void putDoubleRelease(Object o, long offset, double x) {
2600         putDoubleVolatile(o, offset, x);
2601     }
2602      */
2603     // END Android-removed: Not used in Android.
2604 
2605     // ------------------------------ Opaque --------------------------------------
2606 
2607     /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2608     @IntrinsicCandidate
getReferenceOpaque(Object o, long offset)2609     public final Object getReferenceOpaque(Object o, long offset) {
2610         return getReferenceVolatile(o, offset);
2611     }
2612 
2613     // BEGIN Android-removed: Not used in Android.
2614     /*
2615     /** Opaque version of {@link #getBooleanVolatile(Object, long)} * /
2616     @IntrinsicCandidate
2617     public final boolean getBooleanOpaque(Object o, long offset) {
2618         return getBooleanVolatile(o, offset);
2619     }
2620 
2621     /** Opaque version of {@link #getByteVolatile(Object, long)} * /
2622     @IntrinsicCandidate
2623     public final byte getByteOpaque(Object o, long offset) {
2624         return getByteVolatile(o, offset);
2625     }
2626 
2627     /** Opaque version of {@link #getShortVolatile(Object, long)} * /
2628     @IntrinsicCandidate
2629     public final short getShortOpaque(Object o, long offset) {
2630         return getShortVolatile(o, offset);
2631     }
2632 
2633     /** Opaque version of {@link #getCharVolatile(Object, long)} * /
2634     @IntrinsicCandidate
2635     public final char getCharOpaque(Object o, long offset) {
2636         return getCharVolatile(o, offset);
2637     }
2638      */
2639     // END Android-removed: Not used in Android.
2640 
2641     /** Opaque version of {@link #getIntVolatile(Object, long)} */
2642     @IntrinsicCandidate
getIntOpaque(Object o, long offset)2643     public final int getIntOpaque(Object o, long offset) {
2644         return getIntVolatile(o, offset);
2645     }
2646 
2647     // BEGIN Android-removed: Not used in Android.
2648     /*
2649     /** Opaque version of {@link #getFloatVolatile(Object, long)} * /
2650     @IntrinsicCandidate
2651     public final float getFloatOpaque(Object o, long offset) {
2652         return getFloatVolatile(o, offset);
2653     }
2654      */
2655     // END Android-removed: Not used in Android.
2656 
2657     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2658     @IntrinsicCandidate
getLongOpaque(Object o, long offset)2659     public final long getLongOpaque(Object o, long offset) {
2660         return getLongVolatile(o, offset);
2661     }
2662 
2663     // BEGIN Android-removed: Not used in Android.
2664     /*
2665     /** Opaque version of {@link #getDoubleVolatile(Object, long)} * /
2666     @IntrinsicCandidate
2667     public final double getDoubleOpaque(Object o, long offset) {
2668         return getDoubleVolatile(o, offset);
2669     }
2670      */
2671     // END Android-removed: Not used in Android.
2672 
2673     /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2674     @IntrinsicCandidate
putReferenceOpaque(Object o, long offset, Object x)2675     public final void putReferenceOpaque(Object o, long offset, Object x) {
2676         putReferenceVolatile(o, offset, x);
2677     }
2678 
2679     // BEGIN Android-removed: Not used in Android.
2680     /*
2681     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} * /
2682     @IntrinsicCandidate
2683     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2684         putBooleanVolatile(o, offset, x);
2685     }
2686 
2687     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} * /
2688     @IntrinsicCandidate
2689     public final void putByteOpaque(Object o, long offset, byte x) {
2690         putByteVolatile(o, offset, x);
2691     }
2692 
2693     /** Opaque version of {@link #putShortVolatile(Object, long, short)} * /
2694     @IntrinsicCandidate
2695     public final void putShortOpaque(Object o, long offset, short x) {
2696         putShortVolatile(o, offset, x);
2697     }
2698 
2699     /** Opaque version of {@link #putCharVolatile(Object, long, char)} * /
2700     @IntrinsicCandidate
2701     public final void putCharOpaque(Object o, long offset, char x) {
2702         putCharVolatile(o, offset, x);
2703     }
2704      */
2705     // END Android-removed: Not used in Android.
2706 
2707     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
2708     @IntrinsicCandidate
putIntOpaque(Object o, long offset, int x)2709     public final void putIntOpaque(Object o, long offset, int x) {
2710         putIntVolatile(o, offset, x);
2711     }
2712 
2713     // BEGIN Android-removed: Not used in Android.
2714     /*
2715     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} * /
2716     @IntrinsicCandidate
2717     public final void putFloatOpaque(Object o, long offset, float x) {
2718         putFloatVolatile(o, offset, x);
2719     }
2720      */
2721     // END Android-removed: Not used in Android.
2722 
2723     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2724     @IntrinsicCandidate
putLongOpaque(Object o, long offset, long x)2725     public final void putLongOpaque(Object o, long offset, long x) {
2726         putLongVolatile(o, offset, x);
2727     }
2728 
2729     // BEGIN Android-removed: Not used in Android.
2730     /*
2731     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} * /
2732     @IntrinsicCandidate
2733     public final void putDoubleOpaque(Object o, long offset, double x) {
2734         putDoubleVolatile(o, offset, x);
2735     }
2736      */
2737     // END Android-removed: Not used in Android.
2738 
2739     /**
2740      * Unparks the given object, which must be a {@link Thread}.
2741      *
2742      * <p>See {@link java.util.concurrent.locks.LockSupport} for more
2743      * in-depth information of the behavior of this method.</p>
2744      *
2745      * @param obj non-{@code null}; the object to unpark
2746      */
2747     // Android-added: FastNative annotation.
2748     @FastNative
2749     @IntrinsicCandidate
unpark(Object thread)2750     public native void unpark(Object thread);
2751 
2752     /**
2753      * Parks the calling thread for the specified amount of time,
2754      * unless the "permit" for the thread is already available (due to
2755      * a previous call to {@link #unpark}. This method may also return
2756      * spuriously (that is, without the thread being told to unpark
2757      * and without the indicated amount of time elapsing).
2758      *
2759      * <p>See {@link java.util.concurrent.locks.LockSupport} for more
2760      * in-depth information of the behavior of this method.</p>
2761      *
2762      * @param absolute whether the given time value is absolute
2763      * milliseconds-since-the-epoch ({@code true}) or relative
2764      * nanoseconds-from-now ({@code false})
2765      * @param time the (absolute millis or relative nanos) time value
2766      */
2767     @IntrinsicCandidate
park(boolean isAbsolute, long time)2768     public native void park(boolean isAbsolute, long time);
2769 
2770     /*
2771     // BEGIN Android-removed: Not used in Android.
2772     /**
2773      * Gets the load average in the system run queue assigned
2774      * to the available processors averaged over various periods of time.
2775      * This method retrieves the given {@code nelem} samples and
2776      * assigns to the elements of the given {@code loadavg} array.
2777      * The system imposes a maximum of 3 samples, representing
2778      * averages over the last 1,  5,  and  15 minutes, respectively.
2779      *
2780      * @param loadavg an array of double of size nelems
2781      * @param nelems the number of samples to be retrieved and
2782      *        must be 1 to 3.
2783      *
2784      * @return the number of samples actually retrieved; or -1
2785      *         if the load average is unobtainable.
2786      * /
2787     public int getLoadAverage(double[] loadavg, int nelems) {
2788         if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
2789             throw new ArrayIndexOutOfBoundsException();
2790         }
2791 
2792         return getLoadAverage0(loadavg, nelems);
2793     }
2794      */
2795     // END Android-removed: Not used in Android.
2796 
2797     // The following contain CAS-based Java implementations used on
2798     // platforms not supporting native instructions
2799 
2800     /**
2801      * Atomically adds the given value to the current value of a field
2802      * or array element within the given object {@code o}
2803      * at the given {@code offset}.
2804      *
2805      * @param o object/array to update the field/element in
2806      * @param offset field/element offset
2807      * @param delta the value to add
2808      * @return the previous value
2809      * @since 1.8
2810      */
2811     @IntrinsicCandidate
getAndAddInt(Object o, long offset, int delta)2812     public final int getAndAddInt(Object o, long offset, int delta) {
2813         int v;
2814         do {
2815             v = getIntVolatile(o, offset);
2816         } while (!weakCompareAndSetInt(o, offset, v, v + delta));
2817         return v;
2818     }
2819 
2820     // BEGIN Android-removed: Not used in Android.
2821     /*
2822     @ForceInline
2823     public final int getAndAddIntRelease(Object o, long offset, int delta) {
2824         int v;
2825         do {
2826             v = getInt(o, offset);
2827         } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta));
2828         return v;
2829     }
2830 
2831     @ForceInline
2832     public final int getAndAddIntAcquire(Object o, long offset, int delta) {
2833         int v;
2834         do {
2835             v = getIntAcquire(o, offset);
2836         } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta));
2837         return v;
2838     }
2839      */
2840     // END Android-removed: Not used in Android.
2841 
2842     /**
2843      * Atomically adds the given value to the current value of a field
2844      * or array element within the given object {@code o}
2845      * at the given {@code offset}.
2846      *
2847      * @param o object/array to update the field/element in
2848      * @param offset field/element offset
2849      * @param delta the value to add
2850      * @return the previous value
2851      * @since 1.8
2852      */
2853     @IntrinsicCandidate
getAndAddLong(Object o, long offset, long delta)2854     public final long getAndAddLong(Object o, long offset, long delta) {
2855         long v;
2856         do {
2857             v = getLongVolatile(o, offset);
2858         // Android-changed: weakCompareAndSetLong not available.
2859         // } while (!weakCompareAndSetLong(o, offset, v, v + delta));
2860         } while (!compareAndSwapLong(o, offset, v, v + delta));
2861         return v;
2862     }
2863 
2864     // BEGIN Android-removed: Not used in Android.
2865     /*
2866     @ForceInline
2867     public final long getAndAddLongRelease(Object o, long offset, long delta) {
2868         long v;
2869         do {
2870             v = getLong(o, offset);
2871         } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta));
2872         return v;
2873     }
2874 
2875     @ForceInline
2876     public final long getAndAddLongAcquire(Object o, long offset, long delta) {
2877         long v;
2878         do {
2879             v = getLongAcquire(o, offset);
2880         } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta));
2881         return v;
2882     }
2883 
2884     @IntrinsicCandidate
2885     public final byte getAndAddByte(Object o, long offset, byte delta) {
2886         byte v;
2887         do {
2888             v = getByteVolatile(o, offset);
2889         } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta)));
2890         return v;
2891     }
2892 
2893     @ForceInline
2894     public final byte getAndAddByteRelease(Object o, long offset, byte delta) {
2895         byte v;
2896         do {
2897             v = getByte(o, offset);
2898         } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta)));
2899         return v;
2900     }
2901 
2902     @ForceInline
2903     public final byte getAndAddByteAcquire(Object o, long offset, byte delta) {
2904         byte v;
2905         do {
2906             v = getByteAcquire(o, offset);
2907         } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta)));
2908         return v;
2909     }
2910 
2911     @IntrinsicCandidate
2912     public final short getAndAddShort(Object o, long offset, short delta) {
2913         short v;
2914         do {
2915             v = getShortVolatile(o, offset);
2916         } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta)));
2917         return v;
2918     }
2919 
2920     @ForceInline
2921     public final short getAndAddShortRelease(Object o, long offset, short delta) {
2922         short v;
2923         do {
2924             v = getShort(o, offset);
2925         } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta)));
2926         return v;
2927     }
2928 
2929     @ForceInline
2930     public final short getAndAddShortAcquire(Object o, long offset, short delta) {
2931         short v;
2932         do {
2933             v = getShortAcquire(o, offset);
2934         } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta)));
2935         return v;
2936     }
2937 
2938     @ForceInline
2939     public final char getAndAddChar(Object o, long offset, char delta) {
2940         return (char) getAndAddShort(o, offset, (short) delta);
2941     }
2942 
2943     @ForceInline
2944     public final char getAndAddCharRelease(Object o, long offset, char delta) {
2945         return (char) getAndAddShortRelease(o, offset, (short) delta);
2946     }
2947 
2948     @ForceInline
2949     public final char getAndAddCharAcquire(Object o, long offset, char delta) {
2950         return (char) getAndAddShortAcquire(o, offset, (short) delta);
2951     }
2952 
2953     @ForceInline
2954     public final float getAndAddFloat(Object o, long offset, float delta) {
2955         int expectedBits;
2956         float v;
2957         do {
2958             // Load and CAS with the raw bits to avoid issues with NaNs and
2959             // possible bit conversion from signaling NaNs to quiet NaNs that
2960             // may result in the loop not terminating.
2961             expectedBits = getIntVolatile(o, offset);
2962             v = Float.intBitsToFloat(expectedBits);
2963         } while (!weakCompareAndSetInt(o, offset,
2964                                                 expectedBits, Float.floatToRawIntBits(v + delta)));
2965         return v;
2966     }
2967 
2968     @ForceInline
2969     public final float getAndAddFloatRelease(Object o, long offset, float delta) {
2970         int expectedBits;
2971         float v;
2972         do {
2973             // Load and CAS with the raw bits to avoid issues with NaNs and
2974             // possible bit conversion from signaling NaNs to quiet NaNs that
2975             // may result in the loop not terminating.
2976             expectedBits = getInt(o, offset);
2977             v = Float.intBitsToFloat(expectedBits);
2978         } while (!weakCompareAndSetIntRelease(o, offset,
2979                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2980         return v;
2981     }
2982 
2983     @ForceInline
2984     public final float getAndAddFloatAcquire(Object o, long offset, float delta) {
2985         int expectedBits;
2986         float v;
2987         do {
2988             // Load and CAS with the raw bits to avoid issues with NaNs and
2989             // possible bit conversion from signaling NaNs to quiet NaNs that
2990             // may result in the loop not terminating.
2991             expectedBits = getIntAcquire(o, offset);
2992             v = Float.intBitsToFloat(expectedBits);
2993         } while (!weakCompareAndSetIntAcquire(o, offset,
2994                                                expectedBits, Float.floatToRawIntBits(v + delta)));
2995         return v;
2996     }
2997 
2998     @ForceInline
2999     public final double getAndAddDouble(Object o, long offset, double delta) {
3000         long expectedBits;
3001         double v;
3002         do {
3003             // Load and CAS with the raw bits to avoid issues with NaNs and
3004             // possible bit conversion from signaling NaNs to quiet NaNs that
3005             // may result in the loop not terminating.
3006             expectedBits = getLongVolatile(o, offset);
3007             v = Double.longBitsToDouble(expectedBits);
3008         } while (!weakCompareAndSetLong(o, offset,
3009                                                  expectedBits, Double.doubleToRawLongBits(v + delta)));
3010         return v;
3011     }
3012 
3013     @ForceInline
3014     public final double getAndAddDoubleRelease(Object o, long offset, double delta) {
3015         long expectedBits;
3016         double v;
3017         do {
3018             // Load and CAS with the raw bits to avoid issues with NaNs and
3019             // possible bit conversion from signaling NaNs to quiet NaNs that
3020             // may result in the loop not terminating.
3021             expectedBits = getLong(o, offset);
3022             v = Double.longBitsToDouble(expectedBits);
3023         } while (!weakCompareAndSetLongRelease(o, offset,
3024                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
3025         return v;
3026     }
3027 
3028     @ForceInline
3029     public final double getAndAddDoubleAcquire(Object o, long offset, double delta) {
3030         long expectedBits;
3031         double v;
3032         do {
3033             // Load and CAS with the raw bits to avoid issues with NaNs and
3034             // possible bit conversion from signaling NaNs to quiet NaNs that
3035             // may result in the loop not terminating.
3036             expectedBits = getLongAcquire(o, offset);
3037             v = Double.longBitsToDouble(expectedBits);
3038         } while (!weakCompareAndSetLongAcquire(o, offset,
3039                                                 expectedBits, Double.doubleToRawLongBits(v + delta)));
3040         return v;
3041     }
3042      */
3043     // END Android-removed: Not used in Android.
3044 
3045     /**
3046      * Atomically exchanges the given value with the current value of
3047      * a field or array element within the given object {@code o}
3048      * at the given {@code offset}.
3049      *
3050      * @param o object/array to update the field/element in
3051      * @param offset field/element offset
3052      * @param newValue new value
3053      * @return the previous value
3054      * @since 1.8
3055      */
3056     @IntrinsicCandidate
getAndSetInt(Object o, long offset, int newValue)3057     public final int getAndSetInt(Object o, long offset, int newValue) {
3058         int v;
3059         do {
3060             v = getIntVolatile(o, offset);
3061         } while (!weakCompareAndSetInt(o, offset, v, newValue));
3062         return v;
3063     }
3064 
3065     // BEGIN Android-removed: Not used in Android.
3066     /*
3067     @ForceInline
3068     public final int getAndSetIntRelease(Object o, long offset, int newValue) {
3069         int v;
3070         do {
3071             v = getInt(o, offset);
3072         } while (!weakCompareAndSetIntRelease(o, offset, v, newValue));
3073         return v;
3074     }
3075 
3076     @ForceInline
3077     public final int getAndSetIntAcquire(Object o, long offset, int newValue) {
3078         int v;
3079         do {
3080             v = getIntAcquire(o, offset);
3081         } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue));
3082         return v;
3083     }
3084      */
3085     // END Android-removed: Not used in Android.
3086 
3087     /**
3088      * Atomically exchanges the given value with the current value of
3089      * a field or array element within the given object {@code o}
3090      * at the given {@code offset}.
3091      *
3092      * @param o object/array to update the field/element in
3093      * @param offset field/element offset
3094      * @param newValue new value
3095      * @return the previous value
3096      * @since 1.8
3097      */
3098     @IntrinsicCandidate
getAndSetLong(Object o, long offset, long newValue)3099     public final long getAndSetLong(Object o, long offset, long newValue) {
3100         long v;
3101         do {
3102             v = getLongVolatile(o, offset);
3103         // Android-changed: weakCompareAndSetLongRelease not available.
3104         // } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
3105         } while (!compareAndSwapLong(o, offset, v, newValue));
3106         return v;
3107     }
3108 
3109     // BEGIN Android-removed: Not used in Android.
3110     /*
3111     @ForceInline
3112     public final long getAndSetLongRelease(Object o, long offset, long newValue) {
3113         long v;
3114         do {
3115             v = getLong(o, offset);
3116         } while (!weakCompareAndSetLongRelease(o, offset, v, newValue));
3117         return v;
3118     }
3119 
3120     @ForceInline
3121     public final long getAndSetLongAcquire(Object o, long offset, long newValue) {
3122         long v;
3123         do {
3124             v = getLongAcquire(o, offset);
3125         } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue));
3126         return v;
3127     }
3128     */
3129     // END Android-removed: Not used in Android.
3130 
3131     /**
3132      * Atomically exchanges the given reference value with the current
3133      * reference value of a field or array element within the given
3134      * object {@code o} at the given {@code offset}.
3135      *
3136      * @param o object/array to update the field/element in
3137      * @param offset field/element offset
3138      * @param newValue new value
3139      * @return the previous value
3140      * @since 1.8
3141      */
3142     @IntrinsicCandidate
getAndSetReference(Object o, long offset, Object newValue)3143     public final Object getAndSetReference(Object o, long offset, Object newValue) {
3144         Object v;
3145         do {
3146             v = getReferenceVolatile(o, offset);
3147         } while (!weakCompareAndSetReference(o, offset, v, newValue));
3148         return v;
3149     }
3150 
3151     // BEGIN Android-removed: Not used in Android.
3152     /*
3153     @ForceInline
3154     public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3155         Object v;
3156         do {
3157             v = getReference(o, offset);
3158         } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3159         return v;
3160     }
3161 
3162     @ForceInline
3163     public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3164         Object v;
3165         do {
3166             v = getReferenceAcquire(o, offset);
3167         } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3168         return v;
3169     }
3170 
3171     @IntrinsicCandidate
3172     public final byte getAndSetByte(Object o, long offset, byte newValue) {
3173         byte v;
3174         do {
3175             v = getByteVolatile(o, offset);
3176         } while (!weakCompareAndSetByte(o, offset, v, newValue));
3177         return v;
3178     }
3179 
3180     @ForceInline
3181     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3182         byte v;
3183         do {
3184             v = getByte(o, offset);
3185         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3186         return v;
3187     }
3188 
3189     @ForceInline
3190     public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
3191         byte v;
3192         do {
3193             v = getByteAcquire(o, offset);
3194         } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue));
3195         return v;
3196     }
3197 
3198     @ForceInline
3199     public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) {
3200         return byte2bool(getAndSetByte(o, offset, bool2byte(newValue)));
3201     }
3202 
3203     @ForceInline
3204     public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) {
3205         return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue)));
3206     }
3207 
3208     @ForceInline
3209     public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) {
3210         return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue)));
3211     }
3212 
3213     @IntrinsicCandidate
3214     public final short getAndSetShort(Object o, long offset, short newValue) {
3215         short v;
3216         do {
3217             v = getShortVolatile(o, offset);
3218         } while (!weakCompareAndSetShort(o, offset, v, newValue));
3219         return v;
3220     }
3221 
3222     @ForceInline
3223     public final short getAndSetShortRelease(Object o, long offset, short newValue) {
3224         short v;
3225         do {
3226             v = getShort(o, offset);
3227         } while (!weakCompareAndSetShortRelease(o, offset, v, newValue));
3228         return v;
3229     }
3230 
3231     @ForceInline
3232     public final short getAndSetShortAcquire(Object o, long offset, short newValue) {
3233         short v;
3234         do {
3235             v = getShortAcquire(o, offset);
3236         } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue));
3237         return v;
3238     }
3239 
3240     @ForceInline
3241     public final char getAndSetChar(Object o, long offset, char newValue) {
3242         return s2c(getAndSetShort(o, offset, c2s(newValue)));
3243     }
3244 
3245     @ForceInline
3246     public final char getAndSetCharRelease(Object o, long offset, char newValue) {
3247         return s2c(getAndSetShortRelease(o, offset, c2s(newValue)));
3248     }
3249 
3250     @ForceInline
3251     public final char getAndSetCharAcquire(Object o, long offset, char newValue) {
3252         return s2c(getAndSetShortAcquire(o, offset, c2s(newValue)));
3253     }
3254 
3255     @ForceInline
3256     public final float getAndSetFloat(Object o, long offset, float newValue) {
3257         int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue));
3258         return Float.intBitsToFloat(v);
3259     }
3260 
3261     @ForceInline
3262     public final float getAndSetFloatRelease(Object o, long offset, float newValue) {
3263         int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue));
3264         return Float.intBitsToFloat(v);
3265     }
3266 
3267     @ForceInline
3268     public final float getAndSetFloatAcquire(Object o, long offset, float newValue) {
3269         int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue));
3270         return Float.intBitsToFloat(v);
3271     }
3272 
3273     @ForceInline
3274     public final double getAndSetDouble(Object o, long offset, double newValue) {
3275         long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue));
3276         return Double.longBitsToDouble(v);
3277     }
3278 
3279     @ForceInline
3280     public final double getAndSetDoubleRelease(Object o, long offset, double newValue) {
3281         long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue));
3282         return Double.longBitsToDouble(v);
3283     }
3284 
3285     @ForceInline
3286     public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) {
3287         long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue));
3288         return Double.longBitsToDouble(v);
3289     }
3290 
3291 
3292     // The following contain CAS-based Java implementations used on
3293     // platforms not supporting native instructions
3294 
3295     @ForceInline
3296     public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) {
3297         return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask)));
3298     }
3299 
3300     @ForceInline
3301     public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) {
3302         return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask)));
3303     }
3304 
3305     @ForceInline
3306     public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) {
3307         return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask)));
3308     }
3309 
3310     @ForceInline
3311     public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) {
3312         return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask)));
3313     }
3314 
3315     @ForceInline
3316     public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) {
3317         return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask)));
3318     }
3319 
3320     @ForceInline
3321     public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) {
3322         return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask)));
3323     }
3324 
3325     @ForceInline
3326     public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) {
3327         return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask)));
3328     }
3329 
3330     @ForceInline
3331     public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) {
3332         return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask)));
3333     }
3334 
3335     @ForceInline
3336     public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) {
3337         return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask)));
3338     }
3339 
3340 
3341     @ForceInline
3342     public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) {
3343         byte current;
3344         do {
3345             current = getByteVolatile(o, offset);
3346         } while (!weakCompareAndSetByte(o, offset,
3347                                                   current, (byte) (current | mask)));
3348         return current;
3349     }
3350 
3351     @ForceInline
3352     public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) {
3353         byte current;
3354         do {
3355             current = getByte(o, offset);
3356         } while (!weakCompareAndSetByteRelease(o, offset,
3357                                                  current, (byte) (current | mask)));
3358         return current;
3359     }
3360 
3361     @ForceInline
3362     public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) {
3363         byte current;
3364         do {
3365             // Plain read, the value is a hint, the acquire CAS does the work
3366             current = getByte(o, offset);
3367         } while (!weakCompareAndSetByteAcquire(o, offset,
3368                                                  current, (byte) (current | mask)));
3369         return current;
3370     }
3371 
3372     @ForceInline
3373     public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) {
3374         byte current;
3375         do {
3376             current = getByteVolatile(o, offset);
3377         } while (!weakCompareAndSetByte(o, offset,
3378                                                   current, (byte) (current & mask)));
3379         return current;
3380     }
3381 
3382     @ForceInline
3383     public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) {
3384         byte current;
3385         do {
3386             current = getByte(o, offset);
3387         } while (!weakCompareAndSetByteRelease(o, offset,
3388                                                  current, (byte) (current & mask)));
3389         return current;
3390     }
3391 
3392     @ForceInline
3393     public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) {
3394         byte current;
3395         do {
3396             // Plain read, the value is a hint, the acquire CAS does the work
3397             current = getByte(o, offset);
3398         } while (!weakCompareAndSetByteAcquire(o, offset,
3399                                                  current, (byte) (current & mask)));
3400         return current;
3401     }
3402 
3403     @ForceInline
3404     public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) {
3405         byte current;
3406         do {
3407             current = getByteVolatile(o, offset);
3408         } while (!weakCompareAndSetByte(o, offset,
3409                                                   current, (byte) (current ^ mask)));
3410         return current;
3411     }
3412 
3413     @ForceInline
3414     public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) {
3415         byte current;
3416         do {
3417             current = getByte(o, offset);
3418         } while (!weakCompareAndSetByteRelease(o, offset,
3419                                                  current, (byte) (current ^ mask)));
3420         return current;
3421     }
3422 
3423     @ForceInline
3424     public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) {
3425         byte current;
3426         do {
3427             // Plain read, the value is a hint, the acquire CAS does the work
3428             current = getByte(o, offset);
3429         } while (!weakCompareAndSetByteAcquire(o, offset,
3430                                                  current, (byte) (current ^ mask)));
3431         return current;
3432     }
3433 
3434 
3435     @ForceInline
3436     public final char getAndBitwiseOrChar(Object o, long offset, char mask) {
3437         return s2c(getAndBitwiseOrShort(o, offset, c2s(mask)));
3438     }
3439 
3440     @ForceInline
3441     public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) {
3442         return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask)));
3443     }
3444 
3445     @ForceInline
3446     public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) {
3447         return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask)));
3448     }
3449 
3450     @ForceInline
3451     public final char getAndBitwiseAndChar(Object o, long offset, char mask) {
3452         return s2c(getAndBitwiseAndShort(o, offset, c2s(mask)));
3453     }
3454 
3455     @ForceInline
3456     public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) {
3457         return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask)));
3458     }
3459 
3460     @ForceInline
3461     public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) {
3462         return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask)));
3463     }
3464 
3465     @ForceInline
3466     public final char getAndBitwiseXorChar(Object o, long offset, char mask) {
3467         return s2c(getAndBitwiseXorShort(o, offset, c2s(mask)));
3468     }
3469 
3470     @ForceInline
3471     public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) {
3472         return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask)));
3473     }
3474 
3475     @ForceInline
3476     public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) {
3477         return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask)));
3478     }
3479 
3480 
3481     @ForceInline
3482     public final short getAndBitwiseOrShort(Object o, long offset, short mask) {
3483         short current;
3484         do {
3485             current = getShortVolatile(o, offset);
3486         } while (!weakCompareAndSetShort(o, offset,
3487                                                 current, (short) (current | mask)));
3488         return current;
3489     }
3490 
3491     @ForceInline
3492     public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) {
3493         short current;
3494         do {
3495             current = getShort(o, offset);
3496         } while (!weakCompareAndSetShortRelease(o, offset,
3497                                                current, (short) (current | mask)));
3498         return current;
3499     }
3500 
3501     @ForceInline
3502     public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) {
3503         short current;
3504         do {
3505             // Plain read, the value is a hint, the acquire CAS does the work
3506             current = getShort(o, offset);
3507         } while (!weakCompareAndSetShortAcquire(o, offset,
3508                                                current, (short) (current | mask)));
3509         return current;
3510     }
3511 
3512     @ForceInline
3513     public final short getAndBitwiseAndShort(Object o, long offset, short mask) {
3514         short current;
3515         do {
3516             current = getShortVolatile(o, offset);
3517         } while (!weakCompareAndSetShort(o, offset,
3518                                                 current, (short) (current & mask)));
3519         return current;
3520     }
3521 
3522     @ForceInline
3523     public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) {
3524         short current;
3525         do {
3526             current = getShort(o, offset);
3527         } while (!weakCompareAndSetShortRelease(o, offset,
3528                                                current, (short) (current & mask)));
3529         return current;
3530     }
3531 
3532     @ForceInline
3533     public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) {
3534         short current;
3535         do {
3536             // Plain read, the value is a hint, the acquire CAS does the work
3537             current = getShort(o, offset);
3538         } while (!weakCompareAndSetShortAcquire(o, offset,
3539                                                current, (short) (current & mask)));
3540         return current;
3541     }
3542 
3543     @ForceInline
3544     public final short getAndBitwiseXorShort(Object o, long offset, short mask) {
3545         short current;
3546         do {
3547             current = getShortVolatile(o, offset);
3548         } while (!weakCompareAndSetShort(o, offset,
3549                                                 current, (short) (current ^ mask)));
3550         return current;
3551     }
3552 
3553     @ForceInline
3554     public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) {
3555         short current;
3556         do {
3557             current = getShort(o, offset);
3558         } while (!weakCompareAndSetShortRelease(o, offset,
3559                                                current, (short) (current ^ mask)));
3560         return current;
3561     }
3562 
3563     @ForceInline
3564     public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) {
3565         short current;
3566         do {
3567             // Plain read, the value is a hint, the acquire CAS does the work
3568             current = getShort(o, offset);
3569         } while (!weakCompareAndSetShortAcquire(o, offset,
3570                                                current, (short) (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
getAndBitwiseOrInt(Object o, long offset, int mask)3578     public final int getAndBitwiseOrInt(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 getAndBitwiseOrIntRelease(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 getAndBitwiseOrIntAcquire(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     // END Android-removed: Not used in Android.
3611 
3612     /**
3613      * Atomically replaces the current value of a field or array element within
3614      * the given object with the result of bitwise AND between the current value
3615      * and mask.
3616      *
3617      * @param o object/array to update the field/element in
3618      * @param offset field/element offset
3619      * @param mask the mask value
3620      * @return the previous value
3621      * @since 9
3622      */
3623     // Android-removed: @ForceInline is an unsupported attribute.
3624     // @ForceInline
getAndBitwiseAndInt(Object o, long offset, int mask)3625     public final int getAndBitwiseAndInt(Object o, long offset, int mask) {
3626         int current;
3627         do {
3628             current = getIntVolatile(o, offset);
3629         } while (!weakCompareAndSetInt(o, offset,
3630                                                 current, current & mask));
3631         return current;
3632     }
3633 
3634     // BEGIN Android-removed: Not used in Android.
3635     /*
3636     @ForceInline
3637     public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) {
3638         int current;
3639         do {
3640             current = getInt(o, offset);
3641         } while (!weakCompareAndSetIntRelease(o, offset,
3642                                                current, current & mask));
3643         return current;
3644     }
3645 
3646     @ForceInline
3647     public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) {
3648         int current;
3649         do {
3650             // Plain read, the value is a hint, the acquire CAS does the work
3651             current = getInt(o, offset);
3652         } while (!weakCompareAndSetIntAcquire(o, offset,
3653                                                current, current & mask));
3654         return current;
3655     }
3656      */
3657     // END Android-removed: Not used in Android.
3658 
3659     // Android-removed: @ForceInline is an unsupported attribute.
3660     // @ForceInline
getAndBitwiseXorInt(Object o, long offset, int mask)3661     public final int getAndBitwiseXorInt(Object o, long offset, int mask) {
3662         int current;
3663         do {
3664             current = getIntVolatile(o, offset);
3665         } while (!weakCompareAndSetInt(o, offset,
3666                                                 current, current ^ mask));
3667         return current;
3668     }
3669 
3670     // BEGIN Android-removed: Not used in Android.
3671     /*
3672     @ForceInline
3673     public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) {
3674         int current;
3675         do {
3676             current = getInt(o, offset);
3677         } while (!weakCompareAndSetIntRelease(o, offset,
3678                                                current, current ^ mask));
3679         return current;
3680     }
3681 
3682     @ForceInline
3683     public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) {
3684         int current;
3685         do {
3686             // Plain read, the value is a hint, the acquire CAS does the work
3687             current = getInt(o, offset);
3688         } while (!weakCompareAndSetIntAcquire(o, offset,
3689                                                current, current ^ mask));
3690         return current;
3691     }
3692 
3693 
3694     @ForceInline
3695     public final long getAndBitwiseOrLong(Object o, long offset, long mask) {
3696         long current;
3697         do {
3698             current = getLongVolatile(o, offset);
3699         } while (!weakCompareAndSetLong(o, offset,
3700                                                 current, current | mask));
3701         return current;
3702     }
3703 
3704     @ForceInline
3705     public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) {
3706         long current;
3707         do {
3708             current = getLong(o, offset);
3709         } while (!weakCompareAndSetLongRelease(o, offset,
3710                                                current, current | mask));
3711         return current;
3712     }
3713 
3714     @ForceInline
3715     public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) {
3716         long current;
3717         do {
3718             // Plain read, the value is a hint, the acquire CAS does the work
3719             current = getLong(o, offset);
3720         } while (!weakCompareAndSetLongAcquire(o, offset,
3721                                                current, current | mask));
3722         return current;
3723     }
3724 
3725     @ForceInline
3726     public final long getAndBitwiseAndLong(Object o, long offset, long mask) {
3727         long current;
3728         do {
3729             current = getLongVolatile(o, offset);
3730         } while (!weakCompareAndSetLong(o, offset,
3731                                                 current, current & mask));
3732         return current;
3733     }
3734 
3735     @ForceInline
3736     public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) {
3737         long current;
3738         do {
3739             current = getLong(o, offset);
3740         } while (!weakCompareAndSetLongRelease(o, offset,
3741                                                current, current & mask));
3742         return current;
3743     }
3744 
3745     @ForceInline
3746     public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) {
3747         long current;
3748         do {
3749             // Plain read, the value is a hint, the acquire CAS does the work
3750             current = getLong(o, offset);
3751         } while (!weakCompareAndSetLongAcquire(o, offset,
3752                                                current, current & mask));
3753         return current;
3754     }
3755 
3756     @ForceInline
3757     public final long getAndBitwiseXorLong(Object o, long offset, long mask) {
3758         long current;
3759         do {
3760             current = getLongVolatile(o, offset);
3761         } while (!weakCompareAndSetLong(o, offset,
3762                                                 current, current ^ mask));
3763         return current;
3764     }
3765 
3766     @ForceInline
3767     public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) {
3768         long current;
3769         do {
3770             current = getLong(o, offset);
3771         } while (!weakCompareAndSetLongRelease(o, offset,
3772                                                current, current ^ mask));
3773         return current;
3774     }
3775 
3776     @ForceInline
3777     public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) {
3778         long current;
3779         do {
3780             // Plain read, the value is a hint, the acquire CAS does the work
3781             current = getLong(o, offset);
3782         } while (!weakCompareAndSetLongAcquire(o, offset,
3783                                                current, current ^ mask));
3784         return current;
3785     }
3786      */
3787     // END Android-removed: Not used in Android.
3788 
3789     /**
3790      * Ensures that loads before the fence will not be reordered with loads and
3791      * stores after the fence; a "LoadLoad plus LoadStore barrier".
3792      *
3793      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
3794      * (an "acquire fence").
3795      *
3796      * Provides a LoadLoad barrier followed by a LoadStore barrier.
3797      *
3798      * @since 1.8
3799      */
3800     // Android-added: FastNative annotation.
3801     @FastNative
3802     @IntrinsicCandidate
loadFence()3803     public native void loadFence();
3804 
3805     /**
3806      * Ensures that loads and stores before the fence will not be reordered with
3807      * stores after the fence; a "StoreStore plus LoadStore barrier".
3808      *
3809      * Corresponds to C11 atomic_thread_fence(memory_order_release)
3810      * (a "release fence").
3811      *
3812      * Provides a StoreStore barrier followed by a LoadStore barrier.
3813      *
3814      *
3815      * @since 1.8
3816      */
3817     // Android-added: FastNative annotation.
3818     @FastNative
3819     @IntrinsicCandidate
storeFence()3820     public native void storeFence();
3821 
3822     /**
3823      * Ensures that loads and stores before the fence will not be reordered
3824      * with loads and stores after the fence.  Implies the effects of both
3825      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
3826      * barrier.
3827      *
3828      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
3829      * @since 1.8
3830      */
3831     // Android-added: FastNative annotation.
3832     @FastNative
3833     @IntrinsicCandidate
fullFence()3834     public native void fullFence();
3835 
3836     /**
3837      * Ensures that loads before the fence will not be reordered with
3838      * loads after the fence.
3839      *
3840      * @implNote
3841      * This method is operationally equivalent to {@link #loadFence()}.
3842      *
3843      * @since 9
3844      */
loadLoadFence()3845     public final void loadLoadFence() {
3846         loadFence();
3847     }
3848 
3849     /**
3850      * Ensures that stores before the fence will not be reordered with
3851      * stores after the fence.
3852      *
3853      * @implNote
3854      * This method is operationally equivalent to {@link #storeFence()}.
3855      *
3856      * @since 9
3857      */
storeStoreFence()3858     public final void storeStoreFence() {
3859         storeFence();
3860     }
3861 
3862 
3863     // BEGIN Android-removed: Not used in Android.
3864     /*
3865     /**
3866      * Throws IllegalAccessError; for use by the VM for access control
3867      * error support.
3868      * @since 1.8
3869      * /
3870     private static void throwIllegalAccessError() {
3871         throw new IllegalAccessError();
3872     }
3873 
3874     /**
3875      * Throws NoSuchMethodError; for use by the VM for redefinition support.
3876      * @since 13
3877      * /
3878     private static void throwNoSuchMethodError() {
3879         throw new NoSuchMethodError();
3880     }
3881 
3882     /**
3883      * @return Returns true if the native byte ordering of this
3884      * platform is big-endian, false if it is little-endian.
3885      * /
3886     public final boolean isBigEndian() { return BIG_ENDIAN; }
3887 
3888     /**
3889      * @return Returns true if this platform is capable of performing
3890      * accesses at addresses which are not aligned for the type of the
3891      * primitive type being accessed, false otherwise.
3892      * /
3893     public final boolean unalignedAccess() { return UNALIGNED_ACCESS; }
3894      */
3895     // END Android-removed: Not used in Android.
3896 
3897     /**
3898      * Fetches a value at some byte offset into a given Java object.
3899      * More specifically, fetches a value within the given object
3900      * <code>o</code> at the given offset, or (if <code>o</code> is
3901      * null) from the memory address whose numerical value is the
3902      * given offset.  <p>
3903      *
3904      * The specification of this method is the same as {@link
3905      * #getLong(Object, long)} except that the offset does not need to
3906      * have been obtained from {@link #objectFieldOffset} on the
3907      * {@link java.lang.reflect.Field} of some Java field.  The value
3908      * in memory is raw data, and need not correspond to any Java
3909      * variable.  Unless <code>o</code> is null, the value accessed
3910      * must be entirely within the allocated object.  The endianness
3911      * of the value in memory is the endianness of the native platform.
3912      *
3913      * <p> The read will be atomic with respect to the largest power
3914      * of two that divides the GCD of the offset and the storage size.
3915      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
3916      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
3917      * respectively.  There are no other guarantees of atomicity.
3918      * <p>
3919      * 8-byte atomicity is only guaranteed on platforms on which
3920      * support atomic accesses to longs.
3921      *
3922      * @param o Java heap object in which the value resides, if any, else
3923      *        null
3924      * @param offset The offset in bytes from the start of the object
3925      * @return the value fetched from the indicated object
3926      * @throws RuntimeException No defined exceptions are thrown, not even
3927      *         {@link NullPointerException}
3928      * @since 9
3929      */
3930     @IntrinsicCandidate
getLongUnaligned(Object o, long offset)3931     public final long getLongUnaligned(Object o, long offset) {
3932         if ((offset & 7) == 0) {
3933             return getLong(o, offset);
3934         } else if ((offset & 3) == 0) {
3935             return makeLong(getInt(o, offset),
3936                     getInt(o, offset + 4));
3937         } else if ((offset & 1) == 0) {
3938             return makeLong(getShort(o, offset),
3939                     getShort(o, offset + 2),
3940                     getShort(o, offset + 4),
3941                     getShort(o, offset + 6));
3942         } else {
3943             return makeLong(getByte(o, offset),
3944                     getByte(o, offset + 1),
3945                     getByte(o, offset + 2),
3946                     getByte(o, offset + 3),
3947                     getByte(o, offset + 4),
3948                     getByte(o, offset + 5),
3949                     getByte(o, offset + 6),
3950                     getByte(o, offset + 7));
3951         }
3952     }
3953 
3954     /** @see #getLongUnaligned(Object, long) */
3955     @IntrinsicCandidate
getIntUnaligned(Object o, long offset)3956     public final int getIntUnaligned(Object o, long offset) {
3957         if ((offset & 3) == 0) {
3958             return getInt(o, offset);
3959         } else if ((offset & 1) == 0) {
3960             return makeInt(getShort(o, offset),
3961                     getShort(o, offset + 2));
3962         } else {
3963             return makeInt(getByte(o, offset),
3964                     getByte(o, offset + 1),
3965                     getByte(o, offset + 2),
3966                     getByte(o, offset + 3));
3967         }
3968     }
3969 
3970     // BEGIN Android-removed: Not used in Android.
3971     /*
3972     /** @see #getLongUnaligned(Object, long, boolean) * /
3973     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
3974         return convEndian(bigEndian, getIntUnaligned(o, offset));
3975     }
3976 
3977     /** @see #getLongUnaligned(Object, long) * /
3978     @IntrinsicCandidate
3979     public final short getShortUnaligned(Object o, long offset) {
3980         if ((offset & 1) == 0) {
3981             return getShort(o, offset);
3982         } else {
3983             return makeShort(getByte(o, offset),
3984                              getByte(o, offset + 1));
3985         }
3986     }
3987     /** @see #getLongUnaligned(Object, long, boolean) * /
3988     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
3989         return convEndian(bigEndian, getShortUnaligned(o, offset));
3990     }
3991 
3992     /** @see #getLongUnaligned(Object, long) * /
3993     @IntrinsicCandidate
3994     public final char getCharUnaligned(Object o, long offset) {
3995         if ((offset & 1) == 0) {
3996             return getChar(o, offset);
3997         } else {
3998             return (char)makeShort(getByte(o, offset),
3999                                    getByte(o, offset + 1));
4000         }
4001     }
4002 
4003     /** @see #getLongUnaligned(Object, long, boolean) * /
4004     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
4005         return convEndian(bigEndian, getCharUnaligned(o, offset));
4006     }
4007 
4008     /**
4009      * Stores a value at some byte offset into a given Java object.
4010      * <p>
4011      * The specification of this method is the same as {@link
4012      * #getLong(Object, long)} except that the offset does not need to
4013      * have been obtained from {@link #objectFieldOffset} on the
4014      * {@link java.lang.reflect.Field} of some Java field.  The value
4015      * in memory is raw data, and need not correspond to any Java
4016      * variable.  The endianness of the value in memory is the
4017      * endianness of the native platform.
4018      * <p>
4019      * The write will be atomic with respect to the largest power of
4020      * two that divides the GCD of the offset and the storage size.
4021      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
4022      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
4023      * respectively.  There are no other guarantees of atomicity.
4024      * <p>
4025      * 8-byte atomicity is only guaranteed on platforms on which
4026      * support atomic accesses to longs.
4027      *
4028      * @param o Java heap object in which the value resides, if any, else
4029      *        null
4030      * @param offset The offset in bytes from the start of the object
4031      * @param x the value to store
4032      * @throws RuntimeException No defined exceptions are thrown, not even
4033      *         {@link NullPointerException}
4034      * @since 9
4035      * /
4036     @IntrinsicCandidate
4037     public final void putLongUnaligned(Object o, long offset, long x) {
4038         if ((offset & 7) == 0) {
4039             putLong(o, offset, x);
4040         } else if ((offset & 3) == 0) {
4041             putLongParts(o, offset,
4042                          (int)(x >> 0),
4043                          (int)(x >>> 32));
4044         } else if ((offset & 1) == 0) {
4045             putLongParts(o, offset,
4046                          (short)(x >>> 0),
4047                          (short)(x >>> 16),
4048                          (short)(x >>> 32),
4049                          (short)(x >>> 48));
4050         } else {
4051             putLongParts(o, offset,
4052                          (byte)(x >>> 0),
4053                          (byte)(x >>> 8),
4054                          (byte)(x >>> 16),
4055                          (byte)(x >>> 24),
4056                          (byte)(x >>> 32),
4057                          (byte)(x >>> 40),
4058                          (byte)(x >>> 48),
4059                          (byte)(x >>> 56));
4060         }
4061     }
4062 
4063     /**
4064      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
4065      * argument which specifies the endianness of the value as stored in memory.
4066      * @param o Java heap object in which the value resides
4067      * @param offset The offset in bytes from the start of the object
4068      * @param x the value to store
4069      * @param bigEndian The endianness of the value
4070      * @throws RuntimeException No defined exceptions are thrown, not even
4071      *         {@link NullPointerException}
4072      * @since 9
4073      * /
4074     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
4075         putLongUnaligned(o, offset, convEndian(bigEndian, x));
4076     }
4077 
4078     /** @see #putLongUnaligned(Object, long, long) * /
4079     @IntrinsicCandidate
4080     public final void putIntUnaligned(Object o, long offset, int x) {
4081         if ((offset & 3) == 0) {
4082             putInt(o, offset, x);
4083         } else if ((offset & 1) == 0) {
4084             putIntParts(o, offset,
4085                         (short)(x >> 0),
4086                         (short)(x >>> 16));
4087         } else {
4088             putIntParts(o, offset,
4089                         (byte)(x >>> 0),
4090                         (byte)(x >>> 8),
4091                         (byte)(x >>> 16),
4092                         (byte)(x >>> 24));
4093         }
4094     }
4095     /** @see #putLongUnaligned(Object, long, long, boolean) * /
4096     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
4097         putIntUnaligned(o, offset, convEndian(bigEndian, x));
4098     }
4099 
4100     /** @see #putLongUnaligned(Object, long, long) * /
4101     @IntrinsicCandidate
4102     public final void putShortUnaligned(Object o, long offset, short x) {
4103         if ((offset & 1) == 0) {
4104             putShort(o, offset, x);
4105         } else {
4106             putShortParts(o, offset,
4107                           (byte)(x >>> 0),
4108                           (byte)(x >>> 8));
4109         }
4110     }
4111     /** @see #putLongUnaligned(Object, long, long, boolean) * /
4112     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
4113         putShortUnaligned(o, offset, convEndian(bigEndian, x));
4114     }
4115 
4116     /** @see #putLongUnaligned(Object, long, long) * /
4117     @IntrinsicCandidate
4118     public final void putCharUnaligned(Object o, long offset, char x) {
4119         putShortUnaligned(o, offset, (short)x);
4120     }
4121     /** @see #putLongUnaligned(Object, long, long, boolean) * /
4122     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
4123         putCharUnaligned(o, offset, convEndian(bigEndian, x));
4124     }
4125 
4126      */
4127     // END Android-removed: Not used in Android.
4128 
pickPos(int top, int pos)4129     private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; }
4130 
4131     // These methods construct integers from bytes.  The byte ordering
4132     // is the native endianness of this platform.
makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7)4133     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4134         return ((toUnsignedLong(i0) << pickPos(56, 0))
4135               | (toUnsignedLong(i1) << pickPos(56, 8))
4136               | (toUnsignedLong(i2) << pickPos(56, 16))
4137               | (toUnsignedLong(i3) << pickPos(56, 24))
4138               | (toUnsignedLong(i4) << pickPos(56, 32))
4139               | (toUnsignedLong(i5) << pickPos(56, 40))
4140               | (toUnsignedLong(i6) << pickPos(56, 48))
4141               | (toUnsignedLong(i7) << pickPos(56, 56)));
4142     }
makeLong(short i0, short i1, short i2, short i3)4143     private static long makeLong(short i0, short i1, short i2, short i3) {
4144         return ((toUnsignedLong(i0) << pickPos(48, 0))
4145               | (toUnsignedLong(i1) << pickPos(48, 16))
4146               | (toUnsignedLong(i2) << pickPos(48, 32))
4147               | (toUnsignedLong(i3) << pickPos(48, 48)));
4148     }
makeLong(int i0, int i1)4149     private static long makeLong(int i0, int i1) {
4150         return (toUnsignedLong(i0) << pickPos(32, 0))
4151              | (toUnsignedLong(i1) << pickPos(32, 32));
4152     }
makeInt(short i0, short i1)4153     private static int makeInt(short i0, short i1) {
4154         return (toUnsignedInt(i0) << pickPos(16, 0))
4155              | (toUnsignedInt(i1) << pickPos(16, 16));
4156     }
makeInt(byte i0, byte i1, byte i2, byte i3)4157     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
4158         return ((toUnsignedInt(i0) << pickPos(24, 0))
4159               | (toUnsignedInt(i1) << pickPos(24, 8))
4160               | (toUnsignedInt(i2) << pickPos(24, 16))
4161               | (toUnsignedInt(i3) << pickPos(24, 24)));
4162     }
makeShort(byte i0, byte i1)4163     private static short makeShort(byte i0, byte i1) {
4164         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
4165                      | (toUnsignedInt(i1) << pickPos(8, 8)));
4166     }
4167 
4168     // BEGIN Android-removed: Not used in Android.
4169     /*
4170     private static byte  pick(byte  le, byte  be) { return BIG_ENDIAN ? be : le; }
4171     private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; }
4172     private static int   pick(int   le, int   be) { return BIG_ENDIAN ? be : le; }
4173 
4174     // These methods write integers to memory from smaller parts
4175     // provided by their caller.  The ordering in which these parts
4176     // are written is the native endianness of this platform.
4177     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
4178         putByte(o, offset + 0, pick(i0, i7));
4179         putByte(o, offset + 1, pick(i1, i6));
4180         putByte(o, offset + 2, pick(i2, i5));
4181         putByte(o, offset + 3, pick(i3, i4));
4182         putByte(o, offset + 4, pick(i4, i3));
4183         putByte(o, offset + 5, pick(i5, i2));
4184         putByte(o, offset + 6, pick(i6, i1));
4185         putByte(o, offset + 7, pick(i7, i0));
4186     }
4187     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
4188         putShort(o, offset + 0, pick(i0, i3));
4189         putShort(o, offset + 2, pick(i1, i2));
4190         putShort(o, offset + 4, pick(i2, i1));
4191         putShort(o, offset + 6, pick(i3, i0));
4192     }
4193     private void putLongParts(Object o, long offset, int i0, int i1) {
4194         putInt(o, offset + 0, pick(i0, i1));
4195         putInt(o, offset + 4, pick(i1, i0));
4196     }
4197     private void putIntParts(Object o, long offset, short i0, short i1) {
4198         putShort(o, offset + 0, pick(i0, i1));
4199         putShort(o, offset + 2, pick(i1, i0));
4200     }
4201     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
4202         putByte(o, offset + 0, pick(i0, i3));
4203         putByte(o, offset + 1, pick(i1, i2));
4204         putByte(o, offset + 2, pick(i2, i1));
4205         putByte(o, offset + 3, pick(i3, i0));
4206     }
4207     private void putShortParts(Object o, long offset, byte i0, byte i1) {
4208         putByte(o, offset + 0, pick(i0, i1));
4209         putByte(o, offset + 1, pick(i1, i0));
4210     }
4211      */
4212     // END Android-removed: Not used in Android.
4213 
4214     // Zero-extend an integer
toUnsignedInt(byte n)4215     private static int toUnsignedInt(byte n)    { return n & 0xff; }
toUnsignedInt(short n)4216     private static int toUnsignedInt(short n)   { return n & 0xffff; }
toUnsignedLong(byte n)4217     private static long toUnsignedLong(byte n)  { return n & 0xffL; }
toUnsignedLong(short n)4218     private static long toUnsignedLong(short n) { return n & 0xffffL; }
toUnsignedLong(int n)4219     private static long toUnsignedLong(int n)   { return n & 0xffffffffL; }
4220 
4221     // BEGIN Android-removed: Not used in Android.
4222     /*
4223     // Maybe byte-reverse an integer
4224     private static char convEndian(boolean big, char n)   { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); }
4225     private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n)    ; }
4226     private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
4227     private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
4228 
4229 
4230 
4231     private native long allocateMemory0(long bytes);
4232     private native long reallocateMemory0(long address, long bytes);
4233     private native void freeMemory0(long address);
4234     private native void setMemory0(Object o, long offset, long bytes, byte value);
4235      */
4236     // END Android-removed: Not used in Android.
4237 
4238     // Android-added: FastNative annotation.
4239     @FastNative
4240     @IntrinsicCandidate
copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes)4241     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4242 
4243     // BEGIN Android-removed: Not used in Android.
4244     /*
4245     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4246     private native long objectFieldOffset0(Field f);
4247     private native long objectFieldOffset1(Class<?> c, String name);
4248     private native long staticFieldOffset0(Field f);
4249     private native Object staticFieldBase0(Field f);
4250     private native boolean shouldBeInitialized0(Class<?> c);
4251     private native void ensureClassInitialized0(Class<?> c);
4252     private native int arrayBaseOffset0(Class<?> arrayClass);
4253     private native int arrayIndexScale0(Class<?> arrayClass);
4254     private native int getLoadAverage0(double[] loadavg, int nelems);
4255 
4256 
4257     /**
4258      * Invokes the given direct byte buffer's cleaner, if any.
4259      *
4260      * @param directBuffer a direct byte buffer
4261      * @throws NullPointerException     if {@code directBuffer} is null
4262      * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4263      *                                  or is a {@link java.nio.Buffer#slice slice}, or is a
4264      *                                  {@link java.nio.Buffer#duplicate duplicate}
4265      * /
4266     public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4267         if (!directBuffer.isDirect())
4268             throw new IllegalArgumentException("buffer is non-direct");
4269 
4270         DirectBuffer db = (DirectBuffer) directBuffer;
4271         if (db.attachment() != null)
4272             throw new IllegalArgumentException("duplicate or slice");
4273 
4274         Cleaner cleaner = db.cleaner();
4275         if (cleaner != null) {
4276             cleaner.clean();
4277         }
4278     }
4279      */
4280     // END Android-removed: Not used in Android.
4281 
4282     @Deprecated(since="12", forRemoval=true)
getObject(Object o, long offset)4283     public final Object getObject(Object o, long offset) {
4284         return getReference(o, offset);
4285     }
4286     @Deprecated(since="12", forRemoval=true)
getObjectVolatile(Object o, long offset)4287     public final Object getObjectVolatile(Object o, long offset) {
4288         return getReferenceVolatile(o, offset);
4289     }
4290     @Deprecated(since="12", forRemoval=true)
getObjectAcquire(Object o, long offset)4291     public final Object getObjectAcquire(Object o, long offset) {
4292         return getReferenceAcquire(o, offset);
4293     }
4294 
4295     @Deprecated(since="12", forRemoval=true)
putObject(Object o, long offset, Object x)4296     public final void putObject(Object o, long offset, Object x) {
4297         putReference(o, offset, x);
4298     }
4299 
4300     @Deprecated(since="12", forRemoval=true)
putObjectVolatile(Object o, long offset, Object x)4301     public final void putObjectVolatile(Object o, long offset, Object x) {
4302         putReferenceVolatile(o, offset, x);
4303     }
4304     @Deprecated(since="12", forRemoval=true)
putObjectRelease(Object o, long offset, Object x)4305     public final void putObjectRelease(Object o, long offset, Object x) {
4306         putReferenceRelease(o, offset, x);
4307     }
4308 
4309     @Deprecated(since="12", forRemoval=true)
getAndSetObject(Object o, long offset, Object newValue)4310     public final Object getAndSetObject(Object o, long offset, Object newValue) {
4311         return getAndSetReference(o, offset, newValue);
4312     }
4313 
4314     @Deprecated(since="12", forRemoval=true)
compareAndSetObject(Object o, long offset, Object expected, Object x)4315     public final boolean compareAndSetObject(Object o, long offset, Object expected, Object x) {
4316         return compareAndSetReference(o, offset, expected, x);
4317     }
4318 
4319     // BEGIN Android-added: Methods added for the Android platform.
4320     @FastNative
getArrayBaseOffsetForComponentType(Class component_class)4321     private static native int getArrayBaseOffsetForComponentType(Class component_class);
4322     @FastNative
getArrayIndexScaleForComponentType(Class component_class)4323     private static native int getArrayIndexScaleForComponentType(Class component_class);
4324 
4325     /**
4326      * Performs a compare-and-set operation on an {@code int}
4327      * field within the given object.
4328      *
4329      * @param obj non-{@code null}; object containing the field
4330      * @param offset offset to the field within {@code obj}
4331      * @param expectedValue expected value of the field
4332      * @param newValue new value to store in the field if the contents are
4333      * as expected
4334      * @return {@code true} if the new value was in fact stored, and
4335      * {@code false} if not
4336      */
4337     @FastNative
compareAndSwapInt(Object obj, long offset, int expectedValue, int newValue)4338     public native boolean compareAndSwapInt(Object obj, long offset,
4339             int expectedValue, int newValue);
4340 
4341     /**
4342      * Performs a compare-and-set operation on a {@code long}
4343      * field within the given object.
4344      *
4345      * @param obj non-{@code null}; object containing the field
4346      * @param offset offset to the field within {@code obj}
4347      * @param expectedValue expected value of the field
4348      * @param newValue new value to store in the field if the contents are
4349      * as expected
4350      * @return {@code true} if the new value was in fact stored, and
4351      * {@code false} if not
4352      */
4353     @FastNative
compareAndSwapLong(Object obj, long offset, long expectedValue, long newValue)4354     public native boolean compareAndSwapLong(Object obj, long offset,
4355             long expectedValue, long newValue);
4356 
4357     /**
4358      * Performs a compare-and-set operation on an {@code obj}
4359      * field (that is, a reference field) within the given object.
4360      *
4361      * @param obj non-{@code null}; object containing the field
4362      * @param offset offset to the field within {@code obj}
4363      * @param expectedValue expected value of the field
4364      * @param newValue new value to store in the field if the contents are
4365      * as expected
4366      * @return {@code true} if the new value was in fact stored, and
4367      * {@code false} if not
4368      */
4369     @FastNative
compareAndSwapObject(Object obj, long offset, Object expectedValue, Object newValue)4370     public native boolean compareAndSwapObject(Object obj, long offset,
4371             Object expectedValue, Object newValue);
4372 
4373     /**
4374      * Lazy set an int field.
4375      *
4376      * @param obj non-{@code null}; object containing the field
4377      * @param offset offset to the field within {@code obj}
4378      * @param newValue the value to store
4379      */
4380     @FastNative
putOrderedInt(Object obj, long offset, int newValue)4381     public native void putOrderedInt(Object obj, long offset, int newValue);
4382 
4383     /**
4384      * Lazy set a long field.
4385      *
4386      * @param obj non-{@code null}; object containing the field
4387      * @param offset offset to the field within {@code obj}
4388      * @param newValue the value to store
4389      */
4390     @FastNative
putOrderedLong(Object obj, long offset, long newValue)4391     public native void putOrderedLong(Object obj, long offset, long newValue);
4392 
4393     /**
4394      * Lazy set an object field.
4395      *
4396      * @param obj non-{@code null}; object containing the field
4397      * @param offset offset to the field within {@code obj}
4398      * @param newValue the value to store
4399      */
4400     @FastNative
putOrderedObject(Object obj, long offset, Object newValue)4401     public native void putOrderedObject(Object obj, long offset,
4402             Object newValue);
4403 
4404     // END Android-added: Methods added for the Android platform.
4405 
4406 
4407 }
4408