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