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