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