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