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