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