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