1 /* 2 * Copyright (c) 2000, 2018, 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 37 /** 38 * A collection of methods for performing low-level, unsafe operations. 39 * Although the class and all methods are public, use of this class is 40 * limited because only trusted code can obtain instances of it. 41 * 42 * <em>Note:</em> It is the resposibility of the caller to make sure 43 * arguments are checked before methods of this class are 44 * called. While some rudimentary checks are performed on the input, 45 * the checks are best effort and when performance is an overriding 46 * priority, as when methods of this class are optimized by the 47 * runtime compiler, some or all checks (if any) may be elided. Hence, 48 * the caller must not rely on the checks and corresponding 49 * exceptions! 50 * 51 * @author John R. Rose 52 * @see #getUnsafe 53 */ 54 55 public final class Unsafe { 56 57 private static native void registerNatives(); 58 static { 59 registerNatives(); 60 } 61 62 private Unsafe() {} 63 64 private static final Unsafe theUnsafe = new Unsafe(); 65 66 /** 67 * Provides the caller with the capability of performing unsafe 68 * operations. 69 * 70 * <p>The returned {@code Unsafe} object should be carefully guarded 71 * by the caller, since it can be used to read and write data at arbitrary 72 * memory addresses. It must never be passed to untrusted code. 73 * 74 * <p>Most methods in this class are very low-level, and correspond to a 75 * small number of hardware instructions (on typical machines). Compilers 76 * are encouraged to optimize these methods accordingly. 77 * 78 * <p>Here is a suggested idiom for using unsafe operations: 79 * 80 * <pre> {@code 81 * class MyTrustedClass { 82 * private static final Unsafe unsafe = Unsafe.getUnsafe(); 83 * ... 84 * private long myCountAddress = ...; 85 * public int getCount() { return unsafe.getByte(myCountAddress); } 86 * }}</pre> 87 * 88 * (It may assist compilers to make the local variable {@code final}.) 89 */ 90 public static Unsafe getUnsafe() { 91 return theUnsafe; 92 } 93 94 /// peek and poke operations 95 /// (compilers should optimize these to memory ops) 96 97 // These work on object fields in the Java heap. 98 // They will not work on elements of packed arrays. 99 100 /** 101 * Fetches a value from a given Java variable. 102 * More specifically, fetches a field or array element within the given 103 * object {@code o} at the given offset, or (if {@code o} is null) 104 * from the memory address whose numerical value is the given offset. 105 * <p> 106 * The results are undefined unless one of the following cases is true: 107 * <ul> 108 * <li>The offset was obtained from {@link #objectFieldOffset} on 109 * the {@link java.lang.reflect.Field} of some Java field and the object 110 * referred to by {@code o} is of a class compatible with that 111 * field's class. 112 * 113 * <li>The offset and object reference {@code o} (either null or 114 * non-null) were both obtained via {@link #staticFieldOffset} 115 * and {@link #staticFieldBase} (respectively) from the 116 * reflective {@link Field} representation of some Java field. 117 * 118 * <li>The object referred to by {@code o} is an array, and the offset 119 * is an integer of the form {@code B+N*S}, where {@code N} is 120 * a valid index into the array, and {@code B} and {@code S} are 121 * the values obtained by {@link #arrayBaseOffset} and {@link 122 * #arrayIndexScale} (respectively) from the array's class. The value 123 * referred to is the {@code N}<em>th</em> element of the array. 124 * 125 * </ul> 126 * <p> 127 * If one of the above cases is true, the call references a specific Java 128 * variable (field or array element). However, the results are undefined 129 * if that variable is not in fact of the type returned by this method. 130 * <p> 131 * This method refers to a variable by means of two parameters, and so 132 * it provides (in effect) a <em>double-register</em> addressing mode 133 * for Java variables. When the object reference is null, this method 134 * uses its offset as an absolute address. This is similar in operation 135 * to methods such as {@link #getInt(long)}, which provide (in effect) a 136 * <em>single-register</em> addressing mode for non-Java variables. 137 * However, because Java variables may have a different layout in memory 138 * from non-Java variables, programmers should not assume that these 139 * two addressing modes are ever equivalent. Also, programmers should 140 * remember that offsets from the double-register addressing mode cannot 141 * be portably confused with longs used in the single-register addressing 142 * mode. 143 * 144 * @param o Java heap object in which the variable resides, if any, else 145 * null 146 * @param offset indication of where the variable resides in a Java heap 147 * object, if any, else a memory address locating the variable 148 * statically 149 * @return the value fetched from the indicated Java variable 150 * @throws RuntimeException No defined exceptions are thrown, not even 151 * {@link NullPointerException} 152 */ 153 @HotSpotIntrinsicCandidate 154 public native int getInt(Object o, long offset); 155 156 /** 157 * Stores a value into a given Java variable. 158 * <p> 159 * The first two parameters are interpreted exactly as with 160 * {@link #getInt(Object, long)} to refer to a specific 161 * Java variable (field or array element). The given value 162 * is stored into that variable. 163 * <p> 164 * The variable must be of the same type as the method 165 * parameter {@code x}. 166 * 167 * @param o Java heap object in which the variable resides, if any, else 168 * null 169 * @param offset indication of where the variable resides in a Java heap 170 * object, if any, else a memory address locating the variable 171 * statically 172 * @param x the value to store into the indicated Java variable 173 * @throws RuntimeException No defined exceptions are thrown, not even 174 * {@link NullPointerException} 175 */ 176 @HotSpotIntrinsicCandidate 177 public native void putInt(Object o, long offset, int x); 178 179 /** 180 * Returns true if the given class is a regular value type. 181 */ 182 public boolean isValueType(Class<?> c) { 183 return c.isValue() && c == c.asValueType(); 184 } 185 186 private static final int JVM_ACC_FLATTENED = 0x00008000; // HotSpot-specific bit 187 188 /** 189 * Returns true if the given field is flattened. 190 */ 191 public boolean isFlattened(Field f) { 192 return (f.getModifiers() & JVM_ACC_FLATTENED) == JVM_ACC_FLATTENED; 193 } 194 195 /** 196 * Returns true if the given class is a flattened array. 197 */ 198 public native boolean isFlattenedArray(Class<?> arrayClass); 199 200 /** 201 * Fetches a reference value from a given Java variable. 202 * This method can return a reference to either an object or value 203 * or a null reference. 204 * 205 * @see #getInt(Object, long) 206 */ 207 @HotSpotIntrinsicCandidate 208 public native Object getReference(Object o, long offset); 209 210 /** 211 * Stores a reference value into a given Java variable. 212 * This method can store a reference to either an object or value 213 * or a null reference. 214 * <p> 215 * Unless the reference {@code x} being stored is either null 216 * or matches the field type, the results are undefined. 217 * If the reference {@code o} is non-null, card marks or 218 * other store barriers for that object (if the VM requires them) 219 * are updated. 220 * @see #putInt(Object, long, int) 221 */ 222 @HotSpotIntrinsicCandidate 223 public native void putReference(Object o, long offset, Object x); 224 225 /** 226 * Fetches a value of type {@code <V>} from a given Java variable. 227 * More specifically, fetches a field or array element within the given 228 * {@code o} object at the given offset, or (if {@code o} is null) 229 * from the memory address whose numerical value is the given offset. 230 * 231 * @param o Java heap object in which the variable resides, if any, else 232 * null 233 * @param offset indication of where the variable resides in a Java heap 234 * object, if any, else a memory address locating the variable 235 * statically 236 * @param vc value class 237 * @param <V> the type of a value 238 * @return the value fetched from the indicated Java variable 239 * @throws RuntimeException No defined exceptions are thrown, not even 240 * {@link NullPointerException} 241 */ 242 @HotSpotIntrinsicCandidate 243 public native <V> V getValue(Object o, long offset, Class<?> vc); 244 245 /** 246 * Stores the given value into a given Java variable. 247 * 248 * Unless the reference {@code o} being stored is either null 249 * or matches the field type, the results are undefined. 250 * 251 * @param o Java heap object in which the variable resides, if any, else 252 * null 253 * @param offset indication of where the variable resides in a Java heap 254 * object, if any, else a memory address locating the variable 255 * statically 256 * @param vc value class 257 * @param v the value to store into the indicated Java variable 258 * @param <V> the type of a value 259 * @throws RuntimeException No defined exceptions are thrown, not even 260 * {@link NullPointerException} 261 */ 262 @HotSpotIntrinsicCandidate 263 public native <V> void putValue(Object o, long offset, Class<?> vc, V v); 264 265 /** 266 * Fetches a reference value of type {@code vc} from a given Java variable. 267 * This method can return a reference to a value or a null reference 268 * for a boxed value type. 269 * 270 * @param vc value class 271 */ 272 public Object getReference(Object o, long offset, Class<?> vc) { 273 Object ref = getReference(o, offset); 274 if (ref == null && isValueType(vc)) { 275 // If the type of the returned reference is a normal value type 276 // return an uninitialized default value if null 277 ref = uninitializedDefaultValue(vc); 278 } 279 return ref; 280 } 281 282 public Object getReferenceVolatile(Object o, long offset, Class<?> vc) { 283 Object ref = getReferenceVolatile(o, offset); 284 if (ref == null && isValueType(vc)) { 285 // If the type of the returned reference is a normal value type 286 // return an uninitialized default value if null 287 ref = uninitializedDefaultValue(vc); 288 } 289 return ref; 290 } 291 292 /** 293 * Returns an uninitialized default value of the given value class. 294 */ 295 public native <V> V uninitializedDefaultValue(Class<?> vc); 296 297 /** 298 * Returns an object instance with a private buffered value whose layout 299 * and contents is exactly the given value instance. The return object 300 * is in the larval state that can be updated using the unsafe put operation. 301 * 302 * @param value a value instance 303 * @param <V> the type of the given value instance 304 */ 305 @HotSpotIntrinsicCandidate 306 public native <V> V makePrivateBuffer(V value); 307 308 /** 309 * Exits the larval state and returns a value instance. 310 * 311 * @param value a value instance 312 * @param <V> the type of the given value instance 313 */ 314 @HotSpotIntrinsicCandidate 315 public native <V> V finishPrivateBuffer(V value); 316 317 /** 318 * Returns the header size of the given value class 319 * 320 * @param vc Value class 321 * @param <V> value clas 322 * @return the header size of the value class 323 */ 324 public native <V> long valueHeaderSize(Class<V> vc); 325 326 /** @see #getInt(Object, long) */ 327 @HotSpotIntrinsicCandidate 328 public native boolean getBoolean(Object o, long offset); 329 330 /** @see #putInt(Object, long, int) */ 331 @HotSpotIntrinsicCandidate 332 public native void putBoolean(Object o, long offset, boolean x); 333 334 /** @see #getInt(Object, long) */ 335 @HotSpotIntrinsicCandidate 336 public native byte getByte(Object o, long offset); 337 338 /** @see #putInt(Object, long, int) */ 339 @HotSpotIntrinsicCandidate 340 public native void putByte(Object o, long offset, byte x); 341 342 /** @see #getInt(Object, long) */ 343 @HotSpotIntrinsicCandidate 344 public native short getShort(Object o, long offset); 345 346 /** @see #putInt(Object, long, int) */ 347 @HotSpotIntrinsicCandidate 348 public native void putShort(Object o, long offset, short x); 349 350 /** @see #getInt(Object, long) */ 351 @HotSpotIntrinsicCandidate 352 public native char getChar(Object o, long offset); 353 354 /** @see #putInt(Object, long, int) */ 355 @HotSpotIntrinsicCandidate 356 public native void putChar(Object o, long offset, char x); 357 358 /** @see #getInt(Object, long) */ 359 @HotSpotIntrinsicCandidate 360 public native long getLong(Object o, long offset); 361 362 /** @see #putInt(Object, long, int) */ 363 @HotSpotIntrinsicCandidate 364 public native void putLong(Object o, long offset, long x); 365 366 /** @see #getInt(Object, long) */ 367 @HotSpotIntrinsicCandidate 368 public native float getFloat(Object o, long offset); 369 370 /** @see #putInt(Object, long, int) */ 371 @HotSpotIntrinsicCandidate 372 public native void putFloat(Object o, long offset, float x); 373 374 /** @see #getInt(Object, long) */ 375 @HotSpotIntrinsicCandidate 376 public native double getDouble(Object o, long offset); 377 378 /** @see #putInt(Object, long, int) */ 379 @HotSpotIntrinsicCandidate 380 public native void putDouble(Object o, long offset, double x); 381 382 /** 383 * Fetches a native pointer from a given memory address. If the address is 384 * zero, or does not point into a block obtained from {@link 385 * #allocateMemory}, the results are undefined. 386 * 387 * <p>If the native pointer is less than 64 bits wide, it is extended as 388 * an unsigned number to a Java long. The pointer may be indexed by any 389 * given byte offset, simply by adding that offset (as a simple integer) to 390 * the long representing the pointer. The number of bytes actually read 391 * from the target address may be determined by consulting {@link 392 * #addressSize}. 393 * 394 * @see #allocateMemory 395 * @see #getInt(Object, long) 396 */ 397 @ForceInline 398 public long getAddress(Object o, long offset) { 399 if (ADDRESS_SIZE == 4) { 400 return Integer.toUnsignedLong(getInt(o, offset)); 401 } else { 402 return getLong(o, offset); 403 } 404 } 405 406 /** 407 * Stores a native pointer into a given memory address. If the address is 408 * zero, or does not point into a block obtained from {@link 409 * #allocateMemory}, the results are undefined. 410 * 411 * <p>The number of bytes actually written at the target address may be 412 * determined by consulting {@link #addressSize}. 413 * 414 * @see #allocateMemory 415 * @see #putInt(Object, long, int) 416 */ 417 @ForceInline 418 public void putAddress(Object o, long offset, long x) { 419 if (ADDRESS_SIZE == 4) { 420 putInt(o, offset, (int)x); 421 } else { 422 putLong(o, offset, x); 423 } 424 } 425 426 // These read VM internal data. 427 428 /** 429 * Fetches an uncompressed reference value from a given native variable 430 * ignoring the VM's compressed references mode. 431 * 432 * @param address a memory address locating the variable 433 * @return the value fetched from the indicated native variable 434 */ 435 public native Object getUncompressedObject(long address); 436 437 // These work on values in the C heap. 438 439 /** 440 * Fetches a value from a given memory address. If the address is zero, or 441 * does not point into a block obtained from {@link #allocateMemory}, the 442 * results are undefined. 443 * 444 * @see #allocateMemory 445 */ 446 @ForceInline 447 public byte getByte(long address) { 448 return getByte(null, address); 449 } 450 451 /** 452 * Stores a value into a given memory address. If the address is zero, or 453 * does not point into a block obtained from {@link #allocateMemory}, the 454 * results are undefined. 455 * 456 * @see #getByte(long) 457 */ 458 @ForceInline 459 public void putByte(long address, byte x) { 460 putByte(null, address, x); 461 } 462 463 /** @see #getByte(long) */ 464 @ForceInline 465 public short getShort(long address) { 466 return getShort(null, address); 467 } 468 469 /** @see #putByte(long, byte) */ 470 @ForceInline 471 public void putShort(long address, short x) { 472 putShort(null, address, x); 473 } 474 475 /** @see #getByte(long) */ 476 @ForceInline 477 public char getChar(long address) { 478 return getChar(null, address); 479 } 480 481 /** @see #putByte(long, byte) */ 482 @ForceInline 483 public void putChar(long address, char x) { 484 putChar(null, address, x); 485 } 486 487 /** @see #getByte(long) */ 488 @ForceInline 489 public int getInt(long address) { 490 return getInt(null, address); 491 } 492 493 /** @see #putByte(long, byte) */ 494 @ForceInline 495 public void putInt(long address, int x) { 496 putInt(null, address, x); 497 } 498 499 /** @see #getByte(long) */ 500 @ForceInline 501 public long getLong(long address) { 502 return getLong(null, address); 503 } 504 505 /** @see #putByte(long, byte) */ 506 @ForceInline 507 public void putLong(long address, long x) { 508 putLong(null, address, x); 509 } 510 511 /** @see #getByte(long) */ 512 @ForceInline 513 public float getFloat(long address) { 514 return getFloat(null, address); 515 } 516 517 /** @see #putByte(long, byte) */ 518 @ForceInline 519 public void putFloat(long address, float x) { 520 putFloat(null, address, x); 521 } 522 523 /** @see #getByte(long) */ 524 @ForceInline 525 public double getDouble(long address) { 526 return getDouble(null, address); 527 } 528 529 /** @see #putByte(long, byte) */ 530 @ForceInline 531 public void putDouble(long address, double x) { 532 putDouble(null, address, x); 533 } 534 535 /** @see #getAddress(Object, long) */ 536 @ForceInline 537 public long getAddress(long address) { 538 return getAddress(null, address); 539 } 540 541 /** @see #putAddress(Object, long, long) */ 542 @ForceInline 543 public void putAddress(long address, long x) { 544 putAddress(null, address, x); 545 } 546 547 548 549 /// helper methods for validating various types of objects/values 550 551 /** 552 * Create an exception reflecting that some of the input was invalid 553 * 554 * <em>Note:</em> It is the resposibility of the caller to make 555 * sure arguments are checked before the methods are called. While 556 * some rudimentary checks are performed on the input, the checks 557 * are best effort and when performance is an overriding priority, 558 * as when methods of this class are optimized by the runtime 559 * compiler, some or all checks (if any) may be elided. Hence, the 560 * caller must not rely on the checks and corresponding 561 * exceptions! 562 * 563 * @return an exception object 564 */ 565 private RuntimeException invalidInput() { 566 return new IllegalArgumentException(); 567 } 568 569 /** 570 * Check if a value is 32-bit clean (32 MSB are all zero) 571 * 572 * @param value the 64-bit value to check 573 * 574 * @return true if the value is 32-bit clean 575 */ 576 private boolean is32BitClean(long value) { 577 return value >>> 32 == 0; 578 } 579 580 /** 581 * Check the validity of a size (the equivalent of a size_t) 582 * 583 * @throws RuntimeException if the size is invalid 584 * (<em>Note:</em> after optimization, invalid inputs may 585 * go undetected, which will lead to unpredictable 586 * behavior) 587 */ 588 private void checkSize(long size) { 589 if (ADDRESS_SIZE == 4) { 590 // Note: this will also check for negative sizes 591 if (!is32BitClean(size)) { 592 throw invalidInput(); 593 } 594 } else if (size < 0) { 595 throw invalidInput(); 596 } 597 } 598 599 /** 600 * Check the validity of a native address (the equivalent of void*) 601 * 602 * @throws RuntimeException if the address is invalid 603 * (<em>Note:</em> after optimization, invalid inputs may 604 * go undetected, which will lead to unpredictable 605 * behavior) 606 */ 607 private void checkNativeAddress(long address) { 608 if (ADDRESS_SIZE == 4) { 609 // Accept both zero and sign extended pointers. A valid 610 // pointer will, after the +1 below, either have produced 611 // the value 0x0 or 0x1. Masking off the low bit allows 612 // for testing against 0. 613 if ((((address >> 32) + 1) & ~1) != 0) { 614 throw invalidInput(); 615 } 616 } 617 } 618 619 /** 620 * Check the validity of an offset, relative to a base object 621 * 622 * @param o the base object 623 * @param offset the offset to check 624 * 625 * @throws RuntimeException if the size is invalid 626 * (<em>Note:</em> after optimization, invalid inputs may 627 * go undetected, which will lead to unpredictable 628 * behavior) 629 */ 630 private void checkOffset(Object o, long offset) { 631 if (ADDRESS_SIZE == 4) { 632 // Note: this will also check for negative offsets 633 if (!is32BitClean(offset)) { 634 throw invalidInput(); 635 } 636 } else if (offset < 0) { 637 throw invalidInput(); 638 } 639 } 640 641 /** 642 * Check the validity of a double-register pointer 643 * 644 * Note: This code deliberately does *not* check for NPE for (at 645 * least) three reasons: 646 * 647 * 1) NPE is not just NULL/0 - there is a range of values all 648 * resulting in an NPE, which is not trivial to check for 649 * 650 * 2) It is the responsibility of the callers of Unsafe methods 651 * to verify the input, so throwing an exception here is not really 652 * useful - passing in a NULL pointer is a critical error and the 653 * must not expect an exception to be thrown anyway. 654 * 655 * 3) the actual operations will detect NULL pointers anyway by 656 * means of traps and signals (like SIGSEGV). 657 * 658 * @param o Java heap object, or null 659 * @param offset indication of where the variable resides in a Java heap 660 * object, if any, else a memory address locating the variable 661 * statically 662 * 663 * @throws RuntimeException if the pointer is invalid 664 * (<em>Note:</em> after optimization, invalid inputs may 665 * go undetected, which will lead to unpredictable 666 * behavior) 667 */ 668 private void checkPointer(Object o, long offset) { 669 if (o == null) { 670 checkNativeAddress(offset); 671 } else { 672 checkOffset(o, offset); 673 } 674 } 675 676 /** 677 * Check if a type is a primitive array type 678 * 679 * @param c the type to check 680 * 681 * @return true if the type is a primitive array type 682 */ 683 private void checkPrimitiveArray(Class<?> c) { 684 Class<?> componentType = c.getComponentType(); 685 if (componentType == null || !componentType.isPrimitive()) { 686 throw invalidInput(); 687 } 688 } 689 690 /** 691 * Check that a pointer is a valid primitive array type pointer 692 * 693 * Note: pointers off-heap are considered to be primitive arrays 694 * 695 * @throws RuntimeException if the pointer is invalid 696 * (<em>Note:</em> after optimization, invalid inputs may 697 * go undetected, which will lead to unpredictable 698 * behavior) 699 */ 700 private void checkPrimitivePointer(Object o, long offset) { 701 checkPointer(o, offset); 702 703 if (o != null) { 704 // If on heap, it must be a primitive array 705 checkPrimitiveArray(o.getClass()); 706 } 707 } 708 709 710 /// wrappers for malloc, realloc, free: 711 712 /** 713 * Allocates a new block of native memory, of the given size in bytes. The 714 * contents of the memory are uninitialized; they will generally be 715 * garbage. The resulting native pointer will never be zero, and will be 716 * aligned for all value types. Dispose of this memory by calling {@link 717 * #freeMemory}, or resize it with {@link #reallocateMemory}. 718 * 719 * <em>Note:</em> It is the resposibility of the caller to make 720 * sure arguments are checked before the methods are called. While 721 * some rudimentary checks are performed on the input, the checks 722 * are best effort and when performance is an overriding priority, 723 * as when methods of this class are optimized by the runtime 724 * compiler, some or all checks (if any) may be elided. Hence, the 725 * caller must not rely on the checks and corresponding 726 * exceptions! 727 * 728 * @throws RuntimeException if the size is negative or too large 729 * for the native size_t type 730 * 731 * @throws OutOfMemoryError if the allocation is refused by the system 732 * 733 * @see #getByte(long) 734 * @see #putByte(long, byte) 735 */ 736 public long allocateMemory(long bytes) { 737 allocateMemoryChecks(bytes); 738 739 if (bytes == 0) { 740 return 0; 741 } 742 743 long p = allocateMemory0(bytes); 744 if (p == 0) { 745 throw new OutOfMemoryError(); 746 } 747 748 return p; 749 } 750 751 /** 752 * Validate the arguments to allocateMemory 753 * 754 * @throws RuntimeException if the arguments are invalid 755 * (<em>Note:</em> after optimization, invalid inputs may 756 * go undetected, which will lead to unpredictable 757 * behavior) 758 */ 759 private void allocateMemoryChecks(long bytes) { 760 checkSize(bytes); 761 } 762 763 /** 764 * Resizes a new block of native memory, to the given size in bytes. The 765 * contents of the new block past the size of the old block are 766 * uninitialized; they will generally be garbage. The resulting native 767 * pointer will be zero if and only if the requested size is zero. The 768 * resulting native pointer will be aligned for all value types. Dispose 769 * of this memory by calling {@link #freeMemory}, or resize it with {@link 770 * #reallocateMemory}. The address passed to this method may be null, in 771 * which case an allocation will be performed. 772 * 773 * <em>Note:</em> It is the resposibility of the caller to make 774 * sure arguments are checked before the methods are called. While 775 * some rudimentary checks are performed on the input, the checks 776 * are best effort and when performance is an overriding priority, 777 * as when methods of this class are optimized by the runtime 778 * compiler, some or all checks (if any) may be elided. Hence, the 779 * caller must not rely on the checks and corresponding 780 * exceptions! 781 * 782 * @throws RuntimeException if the size is negative or too large 783 * for the native size_t type 784 * 785 * @throws OutOfMemoryError if the allocation is refused by the system 786 * 787 * @see #allocateMemory 788 */ 789 public long reallocateMemory(long address, long bytes) { 790 reallocateMemoryChecks(address, bytes); 791 792 if (bytes == 0) { 793 freeMemory(address); 794 return 0; 795 } 796 797 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes); 798 if (p == 0) { 799 throw new OutOfMemoryError(); 800 } 801 802 return p; 803 } 804 805 /** 806 * Validate the arguments to reallocateMemory 807 * 808 * @throws RuntimeException if the arguments are invalid 809 * (<em>Note:</em> after optimization, invalid inputs may 810 * go undetected, which will lead to unpredictable 811 * behavior) 812 */ 813 private void reallocateMemoryChecks(long address, long bytes) { 814 checkPointer(null, address); 815 checkSize(bytes); 816 } 817 818 /** 819 * Sets all bytes in a given block of memory to a fixed value 820 * (usually zero). 821 * 822 * <p>This method determines a block's base address by means of two parameters, 823 * and so it provides (in effect) a <em>double-register</em> addressing mode, 824 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 825 * the offset supplies an absolute base address. 826 * 827 * <p>The stores are in coherent (atomic) units of a size determined 828 * by the address and length parameters. If the effective address and 829 * length are all even modulo 8, the stores take place in 'long' units. 830 * If the effective address and length are (resp.) even modulo 4 or 2, 831 * the stores take place in units of 'int' or 'short'. 832 * 833 * <em>Note:</em> It is the resposibility of the caller to make 834 * sure arguments are checked before the methods are called. While 835 * some rudimentary checks are performed on the input, the checks 836 * are best effort and when performance is an overriding priority, 837 * as when methods of this class are optimized by the runtime 838 * compiler, some or all checks (if any) may be elided. Hence, the 839 * caller must not rely on the checks and corresponding 840 * exceptions! 841 * 842 * @throws RuntimeException if any of the arguments is invalid 843 * 844 * @since 1.7 845 */ 846 public void setMemory(Object o, long offset, long bytes, byte value) { 847 setMemoryChecks(o, offset, bytes, value); 848 849 if (bytes == 0) { 850 return; 851 } 852 853 setMemory0(o, offset, bytes, value); 854 } 855 856 /** 857 * Sets all bytes in a given block of memory to a fixed value 858 * (usually zero). This provides a <em>single-register</em> addressing mode, 859 * as discussed in {@link #getInt(Object,long)}. 860 * 861 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}. 862 */ 863 public void setMemory(long address, long bytes, byte value) { 864 setMemory(null, address, bytes, value); 865 } 866 867 /** 868 * Validate the arguments to setMemory 869 * 870 * @throws RuntimeException if the arguments are invalid 871 * (<em>Note:</em> after optimization, invalid inputs may 872 * go undetected, which will lead to unpredictable 873 * behavior) 874 */ 875 private void setMemoryChecks(Object o, long offset, long bytes, byte value) { 876 checkPrimitivePointer(o, offset); 877 checkSize(bytes); 878 } 879 880 /** 881 * Sets all bytes in a given block of memory to a copy of another 882 * block. 883 * 884 * <p>This method determines each block's base address by means of two parameters, 885 * and so it provides (in effect) a <em>double-register</em> addressing mode, 886 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 887 * the offset supplies an absolute base address. 888 * 889 * <p>The transfers are in coherent (atomic) units of a size determined 890 * by the address and length parameters. If the effective addresses and 891 * length are all even modulo 8, the transfer takes place in 'long' units. 892 * If the effective addresses and length are (resp.) even modulo 4 or 2, 893 * the transfer takes place in units of 'int' or 'short'. 894 * 895 * <em>Note:</em> It is the resposibility of the caller to make 896 * sure arguments are checked before the methods are called. While 897 * some rudimentary checks are performed on the input, the checks 898 * are best effort and when performance is an overriding priority, 899 * as when methods of this class are optimized by the runtime 900 * compiler, some or all checks (if any) may be elided. Hence, the 901 * caller must not rely on the checks and corresponding 902 * exceptions! 903 * 904 * @throws RuntimeException if any of the arguments is invalid 905 * 906 * @since 1.7 907 */ 908 public void copyMemory(Object srcBase, long srcOffset, 909 Object destBase, long destOffset, 910 long bytes) { 911 copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes); 912 913 if (bytes == 0) { 914 return; 915 } 916 917 copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes); 918 } 919 920 /** 921 * Sets all bytes in a given block of memory to a copy of another 922 * block. This provides a <em>single-register</em> addressing mode, 923 * as discussed in {@link #getInt(Object,long)}. 924 * 925 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}. 926 */ 927 public void copyMemory(long srcAddress, long destAddress, long bytes) { 928 copyMemory(null, srcAddress, null, destAddress, bytes); 929 } 930 931 /** 932 * Validate the arguments to copyMemory 933 * 934 * @throws RuntimeException if any of the arguments is invalid 935 * (<em>Note:</em> after optimization, invalid inputs may 936 * go undetected, which will lead to unpredictable 937 * behavior) 938 */ 939 private void copyMemoryChecks(Object srcBase, long srcOffset, 940 Object destBase, long destOffset, 941 long bytes) { 942 checkSize(bytes); 943 checkPrimitivePointer(srcBase, srcOffset); 944 checkPrimitivePointer(destBase, destOffset); 945 } 946 947 /** 948 * Copies all elements from one block of memory to another block, 949 * *unconditionally* byte swapping the elements on the fly. 950 * 951 * <p>This method determines each block's base address by means of two parameters, 952 * and so it provides (in effect) a <em>double-register</em> addressing mode, 953 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 954 * the offset supplies an absolute base address. 955 * 956 * <em>Note:</em> It is the resposibility of the caller to make 957 * sure arguments are checked before the methods are called. While 958 * some rudimentary checks are performed on the input, the checks 959 * are best effort and when performance is an overriding priority, 960 * as when methods of this class are optimized by the runtime 961 * compiler, some or all checks (if any) may be elided. Hence, the 962 * caller must not rely on the checks and corresponding 963 * exceptions! 964 * 965 * @throws RuntimeException if any of the arguments is invalid 966 * 967 * @since 9 968 */ 969 public void copySwapMemory(Object srcBase, long srcOffset, 970 Object destBase, long destOffset, 971 long bytes, long elemSize) { 972 copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 973 974 if (bytes == 0) { 975 return; 976 } 977 978 copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 979 } 980 981 private void copySwapMemoryChecks(Object srcBase, long srcOffset, 982 Object destBase, long destOffset, 983 long bytes, long elemSize) { 984 checkSize(bytes); 985 986 if (elemSize != 2 && elemSize != 4 && elemSize != 8) { 987 throw invalidInput(); 988 } 989 if (bytes % elemSize != 0) { 990 throw invalidInput(); 991 } 992 993 checkPrimitivePointer(srcBase, srcOffset); 994 checkPrimitivePointer(destBase, destOffset); 995 } 996 997 /** 998 * Copies all elements from one block of memory to another block, byte swapping the 999 * elements on the fly. 1000 * 1001 * This provides a <em>single-register</em> addressing mode, as 1002 * discussed in {@link #getInt(Object,long)}. 1003 * 1004 * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}. 1005 */ 1006 public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) { 1007 copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize); 1008 } 1009 1010 /** 1011 * Disposes of a block of native memory, as obtained from {@link 1012 * #allocateMemory} or {@link #reallocateMemory}. The address passed to 1013 * this method may be null, in which case no action is taken. 1014 * 1015 * <em>Note:</em> It is the resposibility of the caller to make 1016 * sure arguments are checked before the methods are called. While 1017 * some rudimentary checks are performed on the input, the checks 1018 * are best effort and when performance is an overriding priority, 1019 * as when methods of this class are optimized by the runtime 1020 * compiler, some or all checks (if any) may be elided. Hence, the 1021 * caller must not rely on the checks and corresponding 1022 * exceptions! 1023 * 1024 * @throws RuntimeException if any of the arguments is invalid 1025 * 1026 * @see #allocateMemory 1027 */ 1028 public void freeMemory(long address) { 1029 freeMemoryChecks(address); 1030 1031 if (address == 0) { 1032 return; 1033 } 1034 1035 freeMemory0(address); 1036 } 1037 1038 /** 1039 * Validate the arguments to freeMemory 1040 * 1041 * @throws RuntimeException if the arguments are invalid 1042 * (<em>Note:</em> after optimization, invalid inputs may 1043 * go undetected, which will lead to unpredictable 1044 * behavior) 1045 */ 1046 private void freeMemoryChecks(long address) { 1047 checkPointer(null, address); 1048 } 1049 1050 /// random queries 1051 1052 /** 1053 * This constant differs from all results that will ever be returned from 1054 * {@link #staticFieldOffset}, {@link #objectFieldOffset}, 1055 * or {@link #arrayBaseOffset}. 1056 */ 1057 public static final int INVALID_FIELD_OFFSET = -1; 1058 1059 /** 1060 * Reports the location of a given field in the storage allocation of its 1061 * class. Do not expect to perform any sort of arithmetic on this offset; 1062 * it is just a cookie which is passed to the unsafe heap memory accessors. 1063 * 1064 * <p>Any given field will always have the same offset and base, and no 1065 * two distinct fields of the same class will ever have the same offset 1066 * and base. 1067 * 1068 * <p>As of 1.4.1, offsets for fields are represented as long values, 1069 * although the Sun JVM does not use the most significant 32 bits. 1070 * However, JVM implementations which store static fields at absolute 1071 * addresses can use long offsets and null base pointers to express 1072 * the field locations in a form usable by {@link #getInt(Object,long)}. 1073 * Therefore, code which will be ported to such JVMs on 64-bit platforms 1074 * must preserve all bits of static field offsets. 1075 * @see #getInt(Object, long) 1076 */ 1077 public long objectFieldOffset(Field f) { 1078 if (f == null) { 1079 throw new NullPointerException(); 1080 } 1081 1082 return objectFieldOffset0(f); 1083 } 1084 1085 /** 1086 * Reports the location of the field with a given name in the storage 1087 * allocation of its class. 1088 * 1089 * @throws NullPointerException if any parameter is {@code null}. 1090 * @throws InternalError if there is no field named {@code name} declared 1091 * in class {@code c}, i.e., if {@code c.getDeclaredField(name)} 1092 * would throw {@code java.lang.NoSuchFieldException}. 1093 * 1094 * @see #objectFieldOffset(Field) 1095 */ 1096 public long objectFieldOffset(Class<?> c, String name) { 1097 if (c == null || name == null) { 1098 throw new NullPointerException(); 1099 } 1100 1101 return objectFieldOffset1(c, name); 1102 } 1103 1104 /** 1105 * Reports the location of a given static field, in conjunction with {@link 1106 * #staticFieldBase}. 1107 * <p>Do not expect to perform any sort of arithmetic on this offset; 1108 * it is just a cookie which is passed to the unsafe heap memory accessors. 1109 * 1110 * <p>Any given field will always have the same offset, and no two distinct 1111 * fields of the same class will ever have the same offset. 1112 * 1113 * <p>As of 1.4.1, offsets for fields are represented as long values, 1114 * although the Sun JVM does not use the most significant 32 bits. 1115 * It is hard to imagine a JVM technology which needs more than 1116 * a few bits to encode an offset within a non-array object, 1117 * However, for consistency with other methods in this class, 1118 * this method reports its result as a long value. 1119 * @see #getInt(Object, long) 1120 */ 1121 public long staticFieldOffset(Field f) { 1122 if (f == null) { 1123 throw new NullPointerException(); 1124 } 1125 1126 return staticFieldOffset0(f); 1127 } 1128 1129 /** 1130 * Reports the location of a given static field, in conjunction with {@link 1131 * #staticFieldOffset}. 1132 * <p>Fetch the base "Object", if any, with which static fields of the 1133 * given class can be accessed via methods like {@link #getInt(Object, 1134 * long)}. This value may be null. This value may refer to an object 1135 * which is a "cookie", not guaranteed to be a real Object, and it should 1136 * not be used in any way except as argument to the get and put routines in 1137 * this class. 1138 */ 1139 public Object staticFieldBase(Field f) { 1140 if (f == null) { 1141 throw new NullPointerException(); 1142 } 1143 1144 return staticFieldBase0(f); 1145 } 1146 1147 /** 1148 * Detects if the given class may need to be initialized. This is often 1149 * needed in conjunction with obtaining the static field base of a 1150 * class. 1151 * @return false only if a call to {@code ensureClassInitialized} would have no effect 1152 */ 1153 public boolean shouldBeInitialized(Class<?> c) { 1154 if (c == null) { 1155 throw new NullPointerException(); 1156 } 1157 1158 return shouldBeInitialized0(c); 1159 } 1160 1161 /** 1162 * Ensures the given class has been initialized. This is often 1163 * needed in conjunction with obtaining the static field base of a 1164 * class. 1165 */ 1166 public void ensureClassInitialized(Class<?> c) { 1167 if (c == null) { 1168 throw new NullPointerException(); 1169 } 1170 1171 ensureClassInitialized0(c); 1172 } 1173 1174 /** 1175 * Reports the offset of the first element in the storage allocation of a 1176 * given array class. If {@link #arrayIndexScale} returns a non-zero value 1177 * for the same class, you may use that scale factor, together with this 1178 * base offset, to form new offsets to access elements of arrays of the 1179 * given class. 1180 * 1181 * @see #getInt(Object, long) 1182 * @see #putInt(Object, long, int) 1183 */ 1184 public int arrayBaseOffset(Class<?> arrayClass) { 1185 if (arrayClass == null) { 1186 throw new NullPointerException(); 1187 } 1188 1189 return arrayBaseOffset0(arrayClass); 1190 } 1191 1192 1193 /** The value of {@code arrayBaseOffset(boolean[].class)} */ 1194 public static final int ARRAY_BOOLEAN_BASE_OFFSET 1195 = theUnsafe.arrayBaseOffset(boolean[].class); 1196 1197 /** The value of {@code arrayBaseOffset(byte[].class)} */ 1198 public static final int ARRAY_BYTE_BASE_OFFSET 1199 = theUnsafe.arrayBaseOffset(byte[].class); 1200 1201 /** The value of {@code arrayBaseOffset(short[].class)} */ 1202 public static final int ARRAY_SHORT_BASE_OFFSET 1203 = theUnsafe.arrayBaseOffset(short[].class); 1204 1205 /** The value of {@code arrayBaseOffset(char[].class)} */ 1206 public static final int ARRAY_CHAR_BASE_OFFSET 1207 = theUnsafe.arrayBaseOffset(char[].class); 1208 1209 /** The value of {@code arrayBaseOffset(int[].class)} */ 1210 public static final int ARRAY_INT_BASE_OFFSET 1211 = theUnsafe.arrayBaseOffset(int[].class); 1212 1213 /** The value of {@code arrayBaseOffset(long[].class)} */ 1214 public static final int ARRAY_LONG_BASE_OFFSET 1215 = theUnsafe.arrayBaseOffset(long[].class); 1216 1217 /** The value of {@code arrayBaseOffset(float[].class)} */ 1218 public static final int ARRAY_FLOAT_BASE_OFFSET 1219 = theUnsafe.arrayBaseOffset(float[].class); 1220 1221 /** The value of {@code arrayBaseOffset(double[].class)} */ 1222 public static final int ARRAY_DOUBLE_BASE_OFFSET 1223 = theUnsafe.arrayBaseOffset(double[].class); 1224 1225 /** The value of {@code arrayBaseOffset(Object[].class)} */ 1226 public static final int ARRAY_OBJECT_BASE_OFFSET 1227 = theUnsafe.arrayBaseOffset(Object[].class); 1228 1229 /** 1230 * Reports the scale factor for addressing elements in the storage 1231 * allocation of a given array class. However, arrays of "narrow" types 1232 * will generally not work properly with accessors like {@link 1233 * #getByte(Object, long)}, so the scale factor for such classes is reported 1234 * as zero. 1235 * 1236 * @see #arrayBaseOffset 1237 * @see #getInt(Object, long) 1238 * @see #putInt(Object, long, int) 1239 */ 1240 public int arrayIndexScale(Class<?> arrayClass) { 1241 if (arrayClass == null) { 1242 throw new NullPointerException(); 1243 } 1244 1245 return arrayIndexScale0(arrayClass); 1246 } 1247 1248 1249 /** The value of {@code arrayIndexScale(boolean[].class)} */ 1250 public static final int ARRAY_BOOLEAN_INDEX_SCALE 1251 = theUnsafe.arrayIndexScale(boolean[].class); 1252 1253 /** The value of {@code arrayIndexScale(byte[].class)} */ 1254 public static final int ARRAY_BYTE_INDEX_SCALE 1255 = theUnsafe.arrayIndexScale(byte[].class); 1256 1257 /** The value of {@code arrayIndexScale(short[].class)} */ 1258 public static final int ARRAY_SHORT_INDEX_SCALE 1259 = theUnsafe.arrayIndexScale(short[].class); 1260 1261 /** The value of {@code arrayIndexScale(char[].class)} */ 1262 public static final int ARRAY_CHAR_INDEX_SCALE 1263 = theUnsafe.arrayIndexScale(char[].class); 1264 1265 /** The value of {@code arrayIndexScale(int[].class)} */ 1266 public static final int ARRAY_INT_INDEX_SCALE 1267 = theUnsafe.arrayIndexScale(int[].class); 1268 1269 /** The value of {@code arrayIndexScale(long[].class)} */ 1270 public static final int ARRAY_LONG_INDEX_SCALE 1271 = theUnsafe.arrayIndexScale(long[].class); 1272 1273 /** The value of {@code arrayIndexScale(float[].class)} */ 1274 public static final int ARRAY_FLOAT_INDEX_SCALE 1275 = theUnsafe.arrayIndexScale(float[].class); 1276 1277 /** The value of {@code arrayIndexScale(double[].class)} */ 1278 public static final int ARRAY_DOUBLE_INDEX_SCALE 1279 = theUnsafe.arrayIndexScale(double[].class); 1280 1281 /** The value of {@code arrayIndexScale(Object[].class)} */ 1282 public static final int ARRAY_OBJECT_INDEX_SCALE 1283 = theUnsafe.arrayIndexScale(Object[].class); 1284 1285 /** 1286 * Reports the size in bytes of a native pointer, as stored via {@link 1287 * #putAddress}. This value will be either 4 or 8. Note that the sizes of 1288 * other primitive types (as stored in native memory blocks) is determined 1289 * fully by their information content. 1290 */ 1291 public int addressSize() { 1292 return ADDRESS_SIZE; 1293 } 1294 1295 /** The value of {@code addressSize()} */ 1296 public static final int ADDRESS_SIZE = theUnsafe.addressSize0(); 1297 1298 /** 1299 * Reports the size in bytes of a native memory page (whatever that is). 1300 * This value will always be a power of two. 1301 */ 1302 public native int pageSize(); 1303 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 (BE) { 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 (BE) { 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 1.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 * @return Returns true if the native byte ordering of this 3606 * platform is big-endian, false if it is little-endian. 3607 */ 3608 public final boolean isBigEndian() { return BE; } 3609 3610 /** 3611 * @return Returns true if this platform is capable of performing 3612 * accesses at addresses which are not aligned for the type of the 3613 * primitive type being accessed, false otherwise. 3614 */ 3615 public final boolean unalignedAccess() { return unalignedAccess; } 3616 3617 /** 3618 * Fetches a value at some byte offset into a given Java object. 3619 * More specifically, fetches a value within the given object 3620 * <code>o</code> at the given offset, or (if <code>o</code> is 3621 * null) from the memory address whose numerical value is the 3622 * given offset. <p> 3623 * 3624 * The specification of this method is the same as {@link 3625 * #getLong(Object, long)} except that the offset does not need to 3626 * have been obtained from {@link #objectFieldOffset} on the 3627 * {@link java.lang.reflect.Field} of some Java field. The value 3628 * in memory is raw data, and need not correspond to any Java 3629 * variable. Unless <code>o</code> is null, the value accessed 3630 * must be entirely within the allocated object. The endianness 3631 * of the value in memory is the endianness of the native platform. 3632 * 3633 * <p> The read will be atomic with respect to the largest power 3634 * of two that divides the GCD of the offset and the storage size. 3635 * For example, getLongUnaligned will make atomic reads of 2-, 4-, 3636 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 3637 * respectively. There are no other guarantees of atomicity. 3638 * <p> 3639 * 8-byte atomicity is only guaranteed on platforms on which 3640 * support atomic accesses to longs. 3641 * 3642 * @param o Java heap object in which the value resides, if any, else 3643 * null 3644 * @param offset The offset in bytes from the start of the object 3645 * @return the value fetched from the indicated object 3646 * @throws RuntimeException No defined exceptions are thrown, not even 3647 * {@link NullPointerException} 3648 * @since 9 3649 */ 3650 @HotSpotIntrinsicCandidate 3651 public final long getLongUnaligned(Object o, long offset) { 3652 if ((offset & 7) == 0) { 3653 return getLong(o, offset); 3654 } else if ((offset & 3) == 0) { 3655 return makeLong(getInt(o, offset), 3656 getInt(o, offset + 4)); 3657 } else if ((offset & 1) == 0) { 3658 return makeLong(getShort(o, offset), 3659 getShort(o, offset + 2), 3660 getShort(o, offset + 4), 3661 getShort(o, offset + 6)); 3662 } else { 3663 return makeLong(getByte(o, offset), 3664 getByte(o, offset + 1), 3665 getByte(o, offset + 2), 3666 getByte(o, offset + 3), 3667 getByte(o, offset + 4), 3668 getByte(o, offset + 5), 3669 getByte(o, offset + 6), 3670 getByte(o, offset + 7)); 3671 } 3672 } 3673 /** 3674 * As {@link #getLongUnaligned(Object, long)} but with an 3675 * additional argument which specifies the endianness of the value 3676 * as stored in memory. 3677 * 3678 * @param o Java heap object in which the variable resides 3679 * @param offset The offset in bytes from the start of the object 3680 * @param bigEndian The endianness of the value 3681 * @return the value fetched from the indicated object 3682 * @since 9 3683 */ 3684 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) { 3685 return convEndian(bigEndian, getLongUnaligned(o, offset)); 3686 } 3687 3688 /** @see #getLongUnaligned(Object, long) */ 3689 @HotSpotIntrinsicCandidate 3690 public final int getIntUnaligned(Object o, long offset) { 3691 if ((offset & 3) == 0) { 3692 return getInt(o, offset); 3693 } else if ((offset & 1) == 0) { 3694 return makeInt(getShort(o, offset), 3695 getShort(o, offset + 2)); 3696 } else { 3697 return makeInt(getByte(o, offset), 3698 getByte(o, offset + 1), 3699 getByte(o, offset + 2), 3700 getByte(o, offset + 3)); 3701 } 3702 } 3703 /** @see #getLongUnaligned(Object, long, boolean) */ 3704 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) { 3705 return convEndian(bigEndian, getIntUnaligned(o, offset)); 3706 } 3707 3708 /** @see #getLongUnaligned(Object, long) */ 3709 @HotSpotIntrinsicCandidate 3710 public final short getShortUnaligned(Object o, long offset) { 3711 if ((offset & 1) == 0) { 3712 return getShort(o, offset); 3713 } else { 3714 return makeShort(getByte(o, offset), 3715 getByte(o, offset + 1)); 3716 } 3717 } 3718 /** @see #getLongUnaligned(Object, long, boolean) */ 3719 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) { 3720 return convEndian(bigEndian, getShortUnaligned(o, offset)); 3721 } 3722 3723 /** @see #getLongUnaligned(Object, long) */ 3724 @HotSpotIntrinsicCandidate 3725 public final char getCharUnaligned(Object o, long offset) { 3726 if ((offset & 1) == 0) { 3727 return getChar(o, offset); 3728 } else { 3729 return (char)makeShort(getByte(o, offset), 3730 getByte(o, offset + 1)); 3731 } 3732 } 3733 3734 /** @see #getLongUnaligned(Object, long, boolean) */ 3735 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) { 3736 return convEndian(bigEndian, getCharUnaligned(o, offset)); 3737 } 3738 3739 /** 3740 * Stores a value at some byte offset into a given Java object. 3741 * <p> 3742 * The specification of this method is the same as {@link 3743 * #getLong(Object, long)} except that the offset does not need to 3744 * have been obtained from {@link #objectFieldOffset} on the 3745 * {@link java.lang.reflect.Field} of some Java field. The value 3746 * in memory is raw data, and need not correspond to any Java 3747 * variable. The endianness of the value in memory is the 3748 * endianness of the native platform. 3749 * <p> 3750 * The write will be atomic with respect to the largest power of 3751 * two that divides the GCD of the offset and the storage size. 3752 * For example, putLongUnaligned will make atomic writes of 2-, 4-, 3753 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 3754 * respectively. There are no other guarantees of atomicity. 3755 * <p> 3756 * 8-byte atomicity is only guaranteed on platforms on which 3757 * support atomic accesses to longs. 3758 * 3759 * @param o Java heap object in which the value resides, if any, else 3760 * null 3761 * @param offset The offset in bytes from the start of the object 3762 * @param x the value to store 3763 * @throws RuntimeException No defined exceptions are thrown, not even 3764 * {@link NullPointerException} 3765 * @since 9 3766 */ 3767 @HotSpotIntrinsicCandidate 3768 public final void putLongUnaligned(Object o, long offset, long x) { 3769 if ((offset & 7) == 0) { 3770 putLong(o, offset, x); 3771 } else if ((offset & 3) == 0) { 3772 putLongParts(o, offset, 3773 (int)(x >> 0), 3774 (int)(x >>> 32)); 3775 } else if ((offset & 1) == 0) { 3776 putLongParts(o, offset, 3777 (short)(x >>> 0), 3778 (short)(x >>> 16), 3779 (short)(x >>> 32), 3780 (short)(x >>> 48)); 3781 } else { 3782 putLongParts(o, offset, 3783 (byte)(x >>> 0), 3784 (byte)(x >>> 8), 3785 (byte)(x >>> 16), 3786 (byte)(x >>> 24), 3787 (byte)(x >>> 32), 3788 (byte)(x >>> 40), 3789 (byte)(x >>> 48), 3790 (byte)(x >>> 56)); 3791 } 3792 } 3793 3794 /** 3795 * As {@link #putLongUnaligned(Object, long, long)} but with an additional 3796 * argument which specifies the endianness of the value as stored in memory. 3797 * @param o Java heap object in which the value resides 3798 * @param offset The offset in bytes from the start of the object 3799 * @param x the value to store 3800 * @param bigEndian The endianness of the value 3801 * @throws RuntimeException No defined exceptions are thrown, not even 3802 * {@link NullPointerException} 3803 * @since 9 3804 */ 3805 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) { 3806 putLongUnaligned(o, offset, convEndian(bigEndian, x)); 3807 } 3808 3809 /** @see #putLongUnaligned(Object, long, long) */ 3810 @HotSpotIntrinsicCandidate 3811 public final void putIntUnaligned(Object o, long offset, int x) { 3812 if ((offset & 3) == 0) { 3813 putInt(o, offset, x); 3814 } else if ((offset & 1) == 0) { 3815 putIntParts(o, offset, 3816 (short)(x >> 0), 3817 (short)(x >>> 16)); 3818 } else { 3819 putIntParts(o, offset, 3820 (byte)(x >>> 0), 3821 (byte)(x >>> 8), 3822 (byte)(x >>> 16), 3823 (byte)(x >>> 24)); 3824 } 3825 } 3826 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3827 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) { 3828 putIntUnaligned(o, offset, convEndian(bigEndian, x)); 3829 } 3830 3831 /** @see #putLongUnaligned(Object, long, long) */ 3832 @HotSpotIntrinsicCandidate 3833 public final void putShortUnaligned(Object o, long offset, short x) { 3834 if ((offset & 1) == 0) { 3835 putShort(o, offset, x); 3836 } else { 3837 putShortParts(o, offset, 3838 (byte)(x >>> 0), 3839 (byte)(x >>> 8)); 3840 } 3841 } 3842 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3843 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) { 3844 putShortUnaligned(o, offset, convEndian(bigEndian, x)); 3845 } 3846 3847 /** @see #putLongUnaligned(Object, long, long) */ 3848 @HotSpotIntrinsicCandidate 3849 public final void putCharUnaligned(Object o, long offset, char x) { 3850 putShortUnaligned(o, offset, (short)x); 3851 } 3852 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3853 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) { 3854 putCharUnaligned(o, offset, convEndian(bigEndian, x)); 3855 } 3856 3857 // JVM interface methods 3858 // BE is true iff the native endianness of this platform is big. 3859 private static final boolean BE = theUnsafe.isBigEndian0(); 3860 3861 // unalignedAccess is true iff this platform can perform unaligned accesses. 3862 private static final boolean unalignedAccess = theUnsafe.unalignedAccess0(); 3863 3864 private static int pickPos(int top, int pos) { return BE ? top - pos : pos; } 3865 3866 // These methods construct integers from bytes. The byte ordering 3867 // is the native endianness of this platform. 3868 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 3869 return ((toUnsignedLong(i0) << pickPos(56, 0)) 3870 | (toUnsignedLong(i1) << pickPos(56, 8)) 3871 | (toUnsignedLong(i2) << pickPos(56, 16)) 3872 | (toUnsignedLong(i3) << pickPos(56, 24)) 3873 | (toUnsignedLong(i4) << pickPos(56, 32)) 3874 | (toUnsignedLong(i5) << pickPos(56, 40)) 3875 | (toUnsignedLong(i6) << pickPos(56, 48)) 3876 | (toUnsignedLong(i7) << pickPos(56, 56))); 3877 } 3878 private static long makeLong(short i0, short i1, short i2, short i3) { 3879 return ((toUnsignedLong(i0) << pickPos(48, 0)) 3880 | (toUnsignedLong(i1) << pickPos(48, 16)) 3881 | (toUnsignedLong(i2) << pickPos(48, 32)) 3882 | (toUnsignedLong(i3) << pickPos(48, 48))); 3883 } 3884 private static long makeLong(int i0, int i1) { 3885 return (toUnsignedLong(i0) << pickPos(32, 0)) 3886 | (toUnsignedLong(i1) << pickPos(32, 32)); 3887 } 3888 private static int makeInt(short i0, short i1) { 3889 return (toUnsignedInt(i0) << pickPos(16, 0)) 3890 | (toUnsignedInt(i1) << pickPos(16, 16)); 3891 } 3892 private static int makeInt(byte i0, byte i1, byte i2, byte i3) { 3893 return ((toUnsignedInt(i0) << pickPos(24, 0)) 3894 | (toUnsignedInt(i1) << pickPos(24, 8)) 3895 | (toUnsignedInt(i2) << pickPos(24, 16)) 3896 | (toUnsignedInt(i3) << pickPos(24, 24))); 3897 } 3898 private static short makeShort(byte i0, byte i1) { 3899 return (short)((toUnsignedInt(i0) << pickPos(8, 0)) 3900 | (toUnsignedInt(i1) << pickPos(8, 8))); 3901 } 3902 3903 private static byte pick(byte le, byte be) { return BE ? be : le; } 3904 private static short pick(short le, short be) { return BE ? be : le; } 3905 private static int pick(int le, int be) { return BE ? be : le; } 3906 3907 // These methods write integers to memory from smaller parts 3908 // provided by their caller. The ordering in which these parts 3909 // are written is the native endianness of this platform. 3910 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 3911 putByte(o, offset + 0, pick(i0, i7)); 3912 putByte(o, offset + 1, pick(i1, i6)); 3913 putByte(o, offset + 2, pick(i2, i5)); 3914 putByte(o, offset + 3, pick(i3, i4)); 3915 putByte(o, offset + 4, pick(i4, i3)); 3916 putByte(o, offset + 5, pick(i5, i2)); 3917 putByte(o, offset + 6, pick(i6, i1)); 3918 putByte(o, offset + 7, pick(i7, i0)); 3919 } 3920 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) { 3921 putShort(o, offset + 0, pick(i0, i3)); 3922 putShort(o, offset + 2, pick(i1, i2)); 3923 putShort(o, offset + 4, pick(i2, i1)); 3924 putShort(o, offset + 6, pick(i3, i0)); 3925 } 3926 private void putLongParts(Object o, long offset, int i0, int i1) { 3927 putInt(o, offset + 0, pick(i0, i1)); 3928 putInt(o, offset + 4, pick(i1, i0)); 3929 } 3930 private void putIntParts(Object o, long offset, short i0, short i1) { 3931 putShort(o, offset + 0, pick(i0, i1)); 3932 putShort(o, offset + 2, pick(i1, i0)); 3933 } 3934 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) { 3935 putByte(o, offset + 0, pick(i0, i3)); 3936 putByte(o, offset + 1, pick(i1, i2)); 3937 putByte(o, offset + 2, pick(i2, i1)); 3938 putByte(o, offset + 3, pick(i3, i0)); 3939 } 3940 private void putShortParts(Object o, long offset, byte i0, byte i1) { 3941 putByte(o, offset + 0, pick(i0, i1)); 3942 putByte(o, offset + 1, pick(i1, i0)); 3943 } 3944 3945 // Zero-extend an integer 3946 private static int toUnsignedInt(byte n) { return n & 0xff; } 3947 private static int toUnsignedInt(short n) { return n & 0xffff; } 3948 private static long toUnsignedLong(byte n) { return n & 0xffl; } 3949 private static long toUnsignedLong(short n) { return n & 0xffffl; } 3950 private static long toUnsignedLong(int n) { return n & 0xffffffffl; } 3951 3952 // Maybe byte-reverse an integer 3953 private static char convEndian(boolean big, char n) { return big == BE ? n : Character.reverseBytes(n); } 3954 private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n) ; } 3955 private static int convEndian(boolean big, int n) { return big == BE ? n : Integer.reverseBytes(n) ; } 3956 private static long convEndian(boolean big, long n) { return big == BE ? n : Long.reverseBytes(n) ; } 3957 3958 3959 3960 private native long allocateMemory0(long bytes); 3961 private native long reallocateMemory0(long address, long bytes); 3962 private native void freeMemory0(long address); 3963 private native void setMemory0(Object o, long offset, long bytes, byte value); 3964 @HotSpotIntrinsicCandidate 3965 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes); 3966 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize); 3967 private native long objectFieldOffset0(Field f); 3968 private native long objectFieldOffset1(Class<?> c, String name); 3969 private native long staticFieldOffset0(Field f); 3970 private native Object staticFieldBase0(Field f); 3971 private native boolean shouldBeInitialized0(Class<?> c); 3972 private native void ensureClassInitialized0(Class<?> c); 3973 private native int arrayBaseOffset0(Class<?> arrayClass); 3974 private native int arrayIndexScale0(Class<?> arrayClass); 3975 private native int addressSize0(); 3976 private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches); 3977 private native int getLoadAverage0(double[] loadavg, int nelems); 3978 private native boolean unalignedAccess0(); 3979 private native boolean isBigEndian0(); 3980 3981 3982 /** 3983 * Invokes the given direct byte buffer's cleaner, if any. 3984 * 3985 * @param directBuffer a direct byte buffer 3986 * @throws NullPointerException if {@code directBuffer} is null 3987 * @throws IllegalArgumentException if {@code directBuffer} is non-direct, 3988 * or is a {@link java.nio.Buffer#slice slice}, or is a 3989 * {@link java.nio.Buffer#duplicate duplicate} 3990 */ 3991 public void invokeCleaner(java.nio.ByteBuffer directBuffer) { 3992 if (!directBuffer.isDirect()) 3993 throw new IllegalArgumentException("buffer is non-direct"); 3994 3995 DirectBuffer db = (DirectBuffer) directBuffer; 3996 if (db.attachment() != null) 3997 throw new IllegalArgumentException("duplicate or slice"); 3998 3999 Cleaner cleaner = db.cleaner(); 4000 if (cleaner != null) { 4001 cleaner.clean(); 4002 } 4003 } 4004 4005 // The following deprecated methods are used by JSR 166. 4006 4007 @Deprecated(since="12", forRemoval=true) 4008 public final Object getObject(Object o, long offset) { 4009 return getReference(o, offset); 4010 } 4011 @Deprecated(since="12", forRemoval=true) 4012 public final Object getObjectVolatile(Object o, long offset) { 4013 return getReferenceVolatile(o, offset); 4014 } 4015 @Deprecated(since="12", forRemoval=true) 4016 public final Object getObjectAcquire(Object o, long offset) { 4017 return getReferenceAcquire(o, offset); 4018 } 4019 @Deprecated(since="12", forRemoval=true) 4020 public final Object getObjectOpaque(Object o, long offset) { 4021 return getReferenceOpaque(o, offset); 4022 } 4023 4024 4025 @Deprecated(since="12", forRemoval=true) 4026 public final void putObject(Object o, long offset, Object x) { 4027 putReference(o, offset, x); 4028 } 4029 @Deprecated(since="12", forRemoval=true) 4030 public final void putObjectVolatile(Object o, long offset, Object x) { 4031 putReferenceVolatile(o, offset, x); 4032 } 4033 @Deprecated(since="12", forRemoval=true) 4034 public final void putObjectOpaque(Object o, long offset, Object x) { 4035 putReferenceOpaque(o, offset, x); 4036 } 4037 @Deprecated(since="12", forRemoval=true) 4038 public final void putObjectRelease(Object o, long offset, Object x) { 4039 putReferenceRelease(o, offset, x); 4040 } 4041 4042 4043 @Deprecated(since="12", forRemoval=true) 4044 public final Object getAndSetObject(Object o, long offset, Object newValue) { 4045 return getAndSetReference(o, offset, newValue); 4046 } 4047 @Deprecated(since="12", forRemoval=true) 4048 public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) { 4049 return getAndSetReferenceAcquire(o, offset, newValue); 4050 } 4051 @Deprecated(since="12", forRemoval=true) 4052 public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) { 4053 return getAndSetReferenceRelease(o, offset, newValue); 4054 } 4055 4056 4057 @Deprecated(since="12", forRemoval=true) 4058 public final boolean compareAndSetObject(Object o, long offset, Object expected, Object x) { 4059 return compareAndSetReference(o, offset, expected, x); 4060 } 4061 @Deprecated(since="12", forRemoval=true) 4062 public final Object compareAndExchangeObject(Object o, long offset, Object expected, Object x) { 4063 return compareAndExchangeReference(o, offset, expected, x); 4064 } 4065 @Deprecated(since="12", forRemoval=true) 4066 public final Object compareAndExchangeObjectAcquire(Object o, long offset, Object expected, Object x) { 4067 return compareAndExchangeReferenceAcquire(o, offset, expected, x); 4068 } 4069 @Deprecated(since="12", forRemoval=true) 4070 public final Object compareAndExchangeObjectRelease(Object o, long offset, Object expected, Object x) { 4071 return compareAndExchangeReferenceRelease(o, offset, expected, x); 4072 } 4073 4074 4075 @Deprecated(since="12", forRemoval=true) 4076 public final boolean weakCompareAndSetObject(Object o, long offset, Object expected, Object x) { 4077 return weakCompareAndSetReference(o, offset, expected, x); 4078 } 4079 @Deprecated(since="12", forRemoval=true) 4080 public final boolean weakCompareAndSetObjectAcquire(Object o, long offset, Object expected, Object x) { 4081 return weakCompareAndSetReferenceAcquire(o, offset, expected, x); 4082 } 4083 @Deprecated(since="12", forRemoval=true) 4084 public final boolean weakCompareAndSetObjectPlain(Object o, long offset, Object expected, Object x) { 4085 return weakCompareAndSetReferencePlain(o, offset, expected, x); 4086 } 4087 @Deprecated(since="12", forRemoval=true) 4088 public final boolean weakCompareAndSetObjectRelease(Object o, long offset, Object expected, Object x) { 4089 return weakCompareAndSetReferenceRelease(o, offset, expected, x); 4090 } 4091 }