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