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