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