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.vm.annotation.ForceInline; 30 31 import java.lang.reflect.Field; 32 import java.security.ProtectionDomain; 33 34 35 /** 36 * A collection of methods for performing low-level, unsafe operations. 37 * Although the class and all methods are public, use of this class is 38 * limited because only trusted code can obtain instances of it. 39 * 40 * <em>Note:</em> It is the resposibility of the caller to make sure 41 * arguments are checked before methods of this class are 42 * called. While some rudimentary checks are performed on the input, 43 * the checks are best effort and when performance is an overriding 44 * priority, as when methods of this class are optimized by the 45 * runtime compiler, some or all checks (if any) may be elided. Hence, 46 * the caller must not rely on the checks and corresponding 47 * exceptions! 48 * 49 * @author John R. Rose 50 * @see #getUnsafe 51 */ 52 53 public final class Unsafe { 54 55 private static native void registerNatives(); 56 static { 57 registerNatives(); 58 } 59 60 private Unsafe() {} 61 62 private static final Unsafe theUnsafe = new Unsafe(); 63 64 /** 65 * Provides the caller with the capability of performing unsafe 66 * operations. 67 * 68 * <p>The returned {@code Unsafe} object should be carefully guarded 69 * by the caller, since it can be used to read and write data at arbitrary 70 * memory addresses. It must never be passed to untrusted code. 71 * 72 * <p>Most methods in this class are very low-level, and correspond to a 73 * small number of hardware instructions (on typical machines). Compilers 74 * are encouraged to optimize these methods accordingly. 75 * 76 * <p>Here is a suggested idiom for using unsafe operations: 77 * 78 * <pre> {@code 79 * class MyTrustedClass { 80 * private static final Unsafe unsafe = Unsafe.getUnsafe(); 81 * ... 82 * private long myCountAddress = ...; 83 * public int getCount() { return unsafe.getByte(myCountAddress); } 84 * }}</pre> 85 * 86 * (It may assist compilers to make the local variable {@code final}.) 87 */ 88 public static Unsafe getUnsafe() { 89 return theUnsafe; 90 } 91 92 /// peek and poke operations 93 /// (compilers should optimize these to memory ops) 94 95 // These work on object fields in the Java heap. 96 // They will not work on elements of packed arrays. 97 98 /** 99 * Fetches a value from a given Java variable. 100 * More specifically, fetches a field or array element within the given 101 * object {@code o} at the given offset, or (if {@code o} is null) 102 * from the memory address whose numerical value is the given offset. 103 * <p> 104 * The results are undefined unless one of the following cases is true: 105 * <ul> 106 * <li>The offset was obtained from {@link #objectFieldOffset} on 107 * the {@link java.lang.reflect.Field} of some Java field and the object 108 * referred to by {@code o} is of a class compatible with that 109 * field's class. 110 * 111 * <li>The offset and object reference {@code o} (either null or 112 * non-null) were both obtained via {@link #staticFieldOffset} 113 * and {@link #staticFieldBase} (respectively) from the 114 * reflective {@link Field} representation of some Java field. 115 * 116 * <li>The object referred to by {@code o} is an array, and the offset 117 * is an integer of the form {@code B+N*S}, where {@code N} is 118 * a valid index into the array, and {@code B} and {@code S} are 119 * the values obtained by {@link #arrayBaseOffset} and {@link 120 * #arrayIndexScale} (respectively) from the array's class. The value 121 * referred to is the {@code N}<em>th</em> element of the array. 122 * 123 * </ul> 124 * <p> 125 * If one of the above cases is true, the call references a specific Java 126 * variable (field or array element). However, the results are undefined 127 * if that variable is not in fact of the type returned by this method. 128 * <p> 129 * This method refers to a variable by means of two parameters, and so 130 * it provides (in effect) a <em>double-register</em> addressing mode 131 * for Java variables. When the object reference is null, this method 132 * uses its offset as an absolute address. This is similar in operation 133 * to methods such as {@link #getInt(long)}, which provide (in effect) a 134 * <em>single-register</em> addressing mode for non-Java variables. 135 * However, because Java variables may have a different layout in memory 136 * from non-Java variables, programmers should not assume that these 137 * two addressing modes are ever equivalent. Also, programmers should 138 * remember that offsets from the double-register addressing mode cannot 139 * be portably confused with longs used in the single-register addressing 140 * mode. 141 * 142 * @param o Java heap object in which the variable resides, if any, else 143 * null 144 * @param offset indication of where the variable resides in a Java heap 145 * object, if any, else a memory address locating the variable 146 * statically 147 * @return the value fetched from the indicated Java variable 148 * @throws RuntimeException No defined exceptions are thrown, not even 149 * {@link NullPointerException} 150 */ 151 @HotSpotIntrinsicCandidate 152 public native int getInt(Object o, long offset); 153 154 /** 155 * Stores a value into a given Java variable. 156 * <p> 157 * The first two parameters are interpreted exactly as with 158 * {@link #getInt(Object, long)} to refer to a specific 159 * Java variable (field or array element). The given value 160 * is stored into that variable. 161 * <p> 162 * The variable must be of the same type as the method 163 * parameter {@code x}. 164 * 165 * @param o Java heap object in which the variable resides, if any, else 166 * null 167 * @param offset indication of where the variable resides in a Java heap 168 * object, if any, else a memory address locating the variable 169 * statically 170 * @param x the value to store into the indicated Java variable 171 * @throws RuntimeException No defined exceptions are thrown, not even 172 * {@link NullPointerException} 173 */ 174 @HotSpotIntrinsicCandidate 175 public native void putInt(Object o, long offset, int x); 176 177 /** 178 * Fetches a reference value from a given Java variable. 179 * @see #getInt(Object, long) 180 */ 181 @HotSpotIntrinsicCandidate 182 public native Object getObject(Object o, long offset); 183 184 /** 185 * Stores a reference value into a given Java variable. 186 * <p> 187 * Unless the reference {@code x} being stored is either null 188 * or matches the field type, the results are undefined. 189 * If the reference {@code o} is non-null, card marks or 190 * other store barriers for that object (if the VM requires them) 191 * are updated. 192 * @see #putInt(Object, long, int) 193 */ 194 @HotSpotIntrinsicCandidate 195 public native void putObject(Object o, long offset, Object x); 196 197 /** @see #getInt(Object, long) */ 198 @HotSpotIntrinsicCandidate 199 public native boolean getBoolean(Object o, long offset); 200 201 /** @see #putInt(Object, long, int) */ 202 @HotSpotIntrinsicCandidate 203 public native void putBoolean(Object o, long offset, boolean x); 204 205 /** @see #getInt(Object, long) */ 206 @HotSpotIntrinsicCandidate 207 public native byte getByte(Object o, long offset); 208 209 /** @see #putInt(Object, long, int) */ 210 @HotSpotIntrinsicCandidate 211 public native void putByte(Object o, long offset, byte x); 212 213 /** @see #getInt(Object, long) */ 214 @HotSpotIntrinsicCandidate 215 public native short getShort(Object o, long offset); 216 217 /** @see #putInt(Object, long, int) */ 218 @HotSpotIntrinsicCandidate 219 public native void putShort(Object o, long offset, short x); 220 221 /** @see #getInt(Object, long) */ 222 @HotSpotIntrinsicCandidate 223 public native char getChar(Object o, long offset); 224 225 /** @see #putInt(Object, long, int) */ 226 @HotSpotIntrinsicCandidate 227 public native void putChar(Object o, long offset, char x); 228 229 /** @see #getInt(Object, long) */ 230 @HotSpotIntrinsicCandidate 231 public native long getLong(Object o, long offset); 232 233 /** @see #putInt(Object, long, int) */ 234 @HotSpotIntrinsicCandidate 235 public native void putLong(Object o, long offset, long x); 236 237 /** @see #getInt(Object, long) */ 238 @HotSpotIntrinsicCandidate 239 public native float getFloat(Object o, long offset); 240 241 /** @see #putInt(Object, long, int) */ 242 @HotSpotIntrinsicCandidate 243 public native void putFloat(Object o, long offset, float x); 244 245 /** @see #getInt(Object, long) */ 246 @HotSpotIntrinsicCandidate 247 public native double getDouble(Object o, long offset); 248 249 /** @see #putInt(Object, long, int) */ 250 @HotSpotIntrinsicCandidate 251 public native void putDouble(Object o, long offset, double x); 252 253 /** 254 * Fetches a native pointer from a given memory address. If the address is 255 * zero, or does not point into a block obtained from {@link 256 * #allocateMemory}, the results are undefined. 257 * 258 * <p>If the native pointer is less than 64 bits wide, it is extended as 259 * an unsigned number to a Java long. The pointer may be indexed by any 260 * given byte offset, simply by adding that offset (as a simple integer) to 261 * the long representing the pointer. The number of bytes actually read 262 * from the target address may be determined by consulting {@link 263 * #addressSize}. 264 * 265 * @see #allocateMemory 266 * @see #getInt(Object, long) 267 */ 268 @ForceInline 269 public long getAddress(Object o, long offset) { 270 if (ADDRESS_SIZE == 4) { 271 return Integer.toUnsignedLong(getInt(o, offset)); 272 } else { 273 return getLong(o, offset); 274 } 275 } 276 277 /** 278 * Stores a native pointer into a given memory address. If the address is 279 * zero, or does not point into a block obtained from {@link 280 * #allocateMemory}, the results are undefined. 281 * 282 * <p>The number of bytes actually written at the target address may be 283 * determined by consulting {@link #addressSize}. 284 * 285 * @see #allocateMemory 286 * @see #putInt(Object, long, int) 287 */ 288 @ForceInline 289 public void putAddress(Object o, long offset, long x) { 290 if (ADDRESS_SIZE == 4) { 291 putInt(o, offset, (int)x); 292 } else { 293 putLong(o, offset, x); 294 } 295 } 296 297 // These read VM internal data. 298 299 /** 300 * Fetches an uncompressed reference value from a given native variable 301 * ignoring the VM's compressed references mode. 302 * 303 * @param address a memory address locating the variable 304 * @return the value fetched from the indicated native variable 305 */ 306 public native Object getUncompressedObject(long address); 307 308 // These work on values in the C heap. 309 310 /** 311 * Fetches a value from a given memory address. If the address is zero, or 312 * does not point into a block obtained from {@link #allocateMemory}, the 313 * results are undefined. 314 * 315 * @see #allocateMemory 316 */ 317 @ForceInline 318 public byte getByte(long address) { 319 return getByte(null, address); 320 } 321 322 /** 323 * Stores a value into a given memory address. If the address is zero, or 324 * does not point into a block obtained from {@link #allocateMemory}, the 325 * results are undefined. 326 * 327 * @see #getByte(long) 328 */ 329 @ForceInline 330 public void putByte(long address, byte x) { 331 putByte(null, address, x); 332 } 333 334 /** @see #getByte(long) */ 335 @ForceInline 336 public short getShort(long address) { 337 return getShort(null, address); 338 } 339 340 /** @see #putByte(long, byte) */ 341 @ForceInline 342 public void putShort(long address, short x) { 343 putShort(null, address, x); 344 } 345 346 /** @see #getByte(long) */ 347 @ForceInline 348 public char getChar(long address) { 349 return getChar(null, address); 350 } 351 352 /** @see #putByte(long, byte) */ 353 @ForceInline 354 public void putChar(long address, char x) { 355 putChar(null, address, x); 356 } 357 358 /** @see #getByte(long) */ 359 @ForceInline 360 public int getInt(long address) { 361 return getInt(null, address); 362 } 363 364 /** @see #putByte(long, byte) */ 365 @ForceInline 366 public void putInt(long address, int x) { 367 putInt(null, address, x); 368 } 369 370 /** @see #getByte(long) */ 371 @ForceInline 372 public long getLong(long address) { 373 return getLong(null, address); 374 } 375 376 /** @see #putByte(long, byte) */ 377 @ForceInline 378 public void putLong(long address, long x) { 379 putLong(null, address, x); 380 } 381 382 /** @see #getByte(long) */ 383 @ForceInline 384 public float getFloat(long address) { 385 return getFloat(null, address); 386 } 387 388 /** @see #putByte(long, byte) */ 389 @ForceInline 390 public void putFloat(long address, float x) { 391 putFloat(null, address, x); 392 } 393 394 /** @see #getByte(long) */ 395 @ForceInline 396 public double getDouble(long address) { 397 return getDouble(null, address); 398 } 399 400 /** @see #putByte(long, byte) */ 401 @ForceInline 402 public void putDouble(long address, double x) { 403 putDouble(null, address, x); 404 } 405 406 /** @see #getAddress(Object, long) */ 407 @ForceInline 408 public long getAddress(long address) { 409 return getAddress(null, address); 410 } 411 412 /** @see #putAddress(Object, long, long) */ 413 @ForceInline 414 public void putAddress(long address, long x) { 415 putAddress(null, address, x); 416 } 417 418 419 420 /// helper methods for validating various types of objects/values 421 422 /** 423 * Create an exception reflecting that some of the input was invalid 424 * 425 * <em>Note:</em> It is the resposibility of the caller to make 426 * sure arguments are checked before the methods are called. While 427 * some rudimentary checks are performed on the input, the checks 428 * are best effort and when performance is an overriding priority, 429 * as when methods of this class are optimized by the runtime 430 * compiler, some or all checks (if any) may be elided. Hence, the 431 * caller must not rely on the checks and corresponding 432 * exceptions! 433 * 434 * @return an exception object 435 */ 436 private RuntimeException invalidInput() { 437 return new IllegalArgumentException(); 438 } 439 440 /** 441 * Check if a value is 32-bit clean (32 MSB are all zero) 442 * 443 * @param value the 64-bit value to check 444 * 445 * @return true if the value is 32-bit clean 446 */ 447 private boolean is32BitClean(long value) { 448 return value >>> 32 == 0; 449 } 450 451 /** 452 * Check the validity of a size (the equivalent of a size_t) 453 * 454 * @throws RuntimeException if the size is invalid 455 * (<em>Note:</em> after optimization, invalid inputs may 456 * go undetected, which will lead to unpredictable 457 * behavior) 458 */ 459 private void checkSize(long size) { 460 if (ADDRESS_SIZE == 4) { 461 // Note: this will also check for negative sizes 462 if (!is32BitClean(size)) { 463 throw invalidInput(); 464 } 465 } else if (size < 0) { 466 throw invalidInput(); 467 } 468 } 469 470 /** 471 * Check the validity of a native address (the equivalent of void*) 472 * 473 * @throws RuntimeException if the address is invalid 474 * (<em>Note:</em> after optimization, invalid inputs may 475 * go undetected, which will lead to unpredictable 476 * behavior) 477 */ 478 private void checkNativeAddress(long address) { 479 if (ADDRESS_SIZE == 4) { 480 // Accept both zero and sign extended pointers. A valid 481 // pointer will, after the +1 below, either have produced 482 // the value 0x0 or 0x1. Masking off the low bit allows 483 // for testing against 0. 484 if ((((address >> 32) + 1) & ~1) != 0) { 485 throw invalidInput(); 486 } 487 } 488 } 489 490 /** 491 * Check the validity of an offset, relative to a base object 492 * 493 * @param o the base object 494 * @param offset the offset to check 495 * 496 * @throws RuntimeException if the size is invalid 497 * (<em>Note:</em> after optimization, invalid inputs may 498 * go undetected, which will lead to unpredictable 499 * behavior) 500 */ 501 private void checkOffset(Object o, long offset) { 502 if (ADDRESS_SIZE == 4) { 503 // Note: this will also check for negative offsets 504 if (!is32BitClean(offset)) { 505 throw invalidInput(); 506 } 507 } else if (offset < 0) { 508 throw invalidInput(); 509 } 510 } 511 512 /** 513 * Check the validity of a double-register pointer 514 * 515 * Note: This code deliberately does *not* check for NPE for (at 516 * least) three reasons: 517 * 518 * 1) NPE is not just NULL/0 - there is a range of values all 519 * resulting in an NPE, which is not trivial to check for 520 * 521 * 2) It is the responsibility of the callers of Unsafe methods 522 * to verify the input, so throwing an exception here is not really 523 * useful - passing in a NULL pointer is a critical error and the 524 * must not expect an exception to be thrown anyway. 525 * 526 * 3) the actual operations will detect NULL pointers anyway by 527 * means of traps and signals (like SIGSEGV). 528 * 529 * @param o Java heap object, or null 530 * @param offset indication of where the variable resides in a Java heap 531 * object, if any, else a memory address locating the variable 532 * statically 533 * 534 * @throws RuntimeException if the pointer is invalid 535 * (<em>Note:</em> after optimization, invalid inputs may 536 * go undetected, which will lead to unpredictable 537 * behavior) 538 */ 539 private void checkPointer(Object o, long offset) { 540 if (o == null) { 541 checkNativeAddress(offset); 542 } else { 543 checkOffset(o, offset); 544 } 545 } 546 547 /** 548 * Check if a type is a primitive array type 549 * 550 * @param c the type to check 551 * 552 * @return true if the type is a primitive array type 553 */ 554 private void checkPrimitiveArray(Class<?> c) { 555 Class<?> componentType = c.getComponentType(); 556 if (componentType == null || !componentType.isPrimitive()) { 557 throw invalidInput(); 558 } 559 } 560 561 /** 562 * Check that a pointer is a valid primitive array type pointer 563 * 564 * Note: pointers off-heap are considered to be primitive arrays 565 * 566 * @throws RuntimeException if the pointer is invalid 567 * (<em>Note:</em> after optimization, invalid inputs may 568 * go undetected, which will lead to unpredictable 569 * behavior) 570 */ 571 private void checkPrimitivePointer(Object o, long offset) { 572 checkPointer(o, offset); 573 574 if (o != null) { 575 // If on heap, it it must be a primitive array 576 checkPrimitiveArray(o.getClass()); 577 } 578 } 579 580 581 /// wrappers for malloc, realloc, free: 582 583 /** 584 * Allocates a new block of native memory, of the given size in bytes. The 585 * contents of the memory are uninitialized; they will generally be 586 * garbage. The resulting native pointer will never be zero, and will be 587 * aligned for all value types. Dispose of this memory by calling {@link 588 * #freeMemory}, or resize it with {@link #reallocateMemory}. 589 * 590 * <em>Note:</em> It is the resposibility of the caller to make 591 * sure arguments are checked before the methods are called. While 592 * some rudimentary checks are performed on the input, the checks 593 * are best effort and when performance is an overriding priority, 594 * as when methods of this class are optimized by the runtime 595 * compiler, some or all checks (if any) may be elided. Hence, the 596 * caller must not rely on the checks and corresponding 597 * exceptions! 598 * 599 * @throws RuntimeException if the size is negative or too large 600 * for the native size_t type 601 * 602 * @throws OutOfMemoryError if the allocation is refused by the system 603 * 604 * @see #getByte(long) 605 * @see #putByte(long, byte) 606 */ 607 public long allocateMemory(long bytes) { 608 allocateMemoryChecks(bytes); 609 610 if (bytes == 0) { 611 return 0; 612 } 613 614 long p = allocateMemory0(bytes); 615 if (p == 0) { 616 throw new OutOfMemoryError(); 617 } 618 619 return p; 620 } 621 622 /** 623 * Validate the arguments to allocateMemory 624 * 625 * @throws RuntimeException if the arguments are invalid 626 * (<em>Note:</em> after optimization, invalid inputs may 627 * go undetected, which will lead to unpredictable 628 * behavior) 629 */ 630 private void allocateMemoryChecks(long bytes) { 631 checkSize(bytes); 632 } 633 634 /** 635 * Resizes a new block of native memory, to the given size in bytes. The 636 * contents of the new block past the size of the old block are 637 * uninitialized; they will generally be garbage. The resulting native 638 * pointer will be zero if and only if the requested size is zero. The 639 * resulting native pointer will be aligned for all value types. Dispose 640 * of this memory by calling {@link #freeMemory}, or resize it with {@link 641 * #reallocateMemory}. The address passed to this method may be null, in 642 * which case an allocation will be performed. 643 * 644 * <em>Note:</em> It is the resposibility of the caller to make 645 * sure arguments are checked before the methods are called. While 646 * some rudimentary checks are performed on the input, the checks 647 * are best effort and when performance is an overriding priority, 648 * as when methods of this class are optimized by the runtime 649 * compiler, some or all checks (if any) may be elided. Hence, the 650 * caller must not rely on the checks and corresponding 651 * exceptions! 652 * 653 * @throws RuntimeException if the size is negative or too large 654 * for the native size_t type 655 * 656 * @throws OutOfMemoryError if the allocation is refused by the system 657 * 658 * @see #allocateMemory 659 */ 660 public long reallocateMemory(long address, long bytes) { 661 reallocateMemoryChecks(address, bytes); 662 663 if (bytes == 0) { 664 freeMemory(address); 665 return 0; 666 } 667 668 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes); 669 if (p == 0) { 670 throw new OutOfMemoryError(); 671 } 672 673 return p; 674 } 675 676 /** 677 * Validate the arguments to reallocateMemory 678 * 679 * @throws RuntimeException if the arguments are invalid 680 * (<em>Note:</em> after optimization, invalid inputs may 681 * go undetected, which will lead to unpredictable 682 * behavior) 683 */ 684 private void reallocateMemoryChecks(long address, long bytes) { 685 checkPointer(null, address); 686 checkSize(bytes); 687 } 688 689 /** 690 * Sets all bytes in a given block of memory to a fixed value 691 * (usually zero). 692 * 693 * <p>This method determines a block's base address by means of two parameters, 694 * and so it provides (in effect) a <em>double-register</em> addressing mode, 695 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 696 * the offset supplies an absolute base address. 697 * 698 * <p>The stores are in coherent (atomic) units of a size determined 699 * by the address and length parameters. If the effective address and 700 * length are all even modulo 8, the stores take place in 'long' units. 701 * If the effective address and length are (resp.) even modulo 4 or 2, 702 * the stores take place in units of 'int' or 'short'. 703 * 704 * <em>Note:</em> It is the resposibility of the caller to make 705 * sure arguments are checked before the methods are called. While 706 * some rudimentary checks are performed on the input, the checks 707 * are best effort and when performance is an overriding priority, 708 * as when methods of this class are optimized by the runtime 709 * compiler, some or all checks (if any) may be elided. Hence, the 710 * caller must not rely on the checks and corresponding 711 * exceptions! 712 * 713 * @throws RuntimeException if any of the arguments is invalid 714 * 715 * @since 1.7 716 */ 717 public void setMemory(Object o, long offset, long bytes, byte value) { 718 setMemoryChecks(o, offset, bytes, value); 719 720 if (bytes == 0) { 721 return; 722 } 723 724 setMemory0(o, offset, bytes, value); 725 } 726 727 /** 728 * Sets all bytes in a given block of memory to a fixed value 729 * (usually zero). This provides a <em>single-register</em> addressing mode, 730 * as discussed in {@link #getInt(Object,long)}. 731 * 732 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}. 733 */ 734 public void setMemory(long address, long bytes, byte value) { 735 setMemory(null, address, bytes, value); 736 } 737 738 /** 739 * Validate the arguments to setMemory 740 * 741 * @throws RuntimeException if the arguments are invalid 742 * (<em>Note:</em> after optimization, invalid inputs may 743 * go undetected, which will lead to unpredictable 744 * behavior) 745 */ 746 private void setMemoryChecks(Object o, long offset, long bytes, byte value) { 747 checkPrimitivePointer(o, offset); 748 checkSize(bytes); 749 } 750 751 /** 752 * Sets all bytes in a given block of memory to a copy of another 753 * block. 754 * 755 * <p>This method determines each block's base address by means of two parameters, 756 * and so it provides (in effect) a <em>double-register</em> addressing mode, 757 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 758 * the offset supplies an absolute base address. 759 * 760 * <p>The transfers are in coherent (atomic) units of a size determined 761 * by the address and length parameters. If the effective addresses and 762 * length are all even modulo 8, the transfer takes place in 'long' units. 763 * If the effective addresses and length are (resp.) even modulo 4 or 2, 764 * the transfer takes place in units of 'int' or 'short'. 765 * 766 * <em>Note:</em> It is the resposibility of the caller to make 767 * sure arguments are checked before the methods are called. While 768 * some rudimentary checks are performed on the input, the checks 769 * are best effort and when performance is an overriding priority, 770 * as when methods of this class are optimized by the runtime 771 * compiler, some or all checks (if any) may be elided. Hence, the 772 * caller must not rely on the checks and corresponding 773 * exceptions! 774 * 775 * @throws RuntimeException if any of the arguments is invalid 776 * 777 * @since 1.7 778 */ 779 public void copyMemory(Object srcBase, long srcOffset, 780 Object destBase, long destOffset, 781 long bytes) { 782 copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes); 783 784 if (bytes == 0) { 785 return; 786 } 787 788 copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes); 789 } 790 791 /** 792 * Sets all bytes in a given block of memory to a copy of another 793 * block. This provides a <em>single-register</em> addressing mode, 794 * as discussed in {@link #getInt(Object,long)}. 795 * 796 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}. 797 */ 798 public void copyMemory(long srcAddress, long destAddress, long bytes) { 799 copyMemory(null, srcAddress, null, destAddress, bytes); 800 } 801 802 /** 803 * Validate the arguments to copyMemory 804 * 805 * @throws RuntimeException if any of the arguments is invalid 806 * (<em>Note:</em> after optimization, invalid inputs may 807 * go undetected, which will lead to unpredictable 808 * behavior) 809 */ 810 private void copyMemoryChecks(Object srcBase, long srcOffset, 811 Object destBase, long destOffset, 812 long bytes) { 813 checkSize(bytes); 814 checkPrimitivePointer(srcBase, srcOffset); 815 checkPrimitivePointer(destBase, destOffset); 816 } 817 818 /** 819 * Copies all elements from one block of memory to another block, 820 * *unconditionally* byte swapping the elements on the fly. 821 * 822 * <p>This method determines each block's base address by means of two parameters, 823 * and so it provides (in effect) a <em>double-register</em> addressing mode, 824 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 825 * the offset supplies an absolute base address. 826 * 827 * <em>Note:</em> It is the resposibility of the caller to make 828 * sure arguments are checked before the methods are called. While 829 * some rudimentary checks are performed on the input, the checks 830 * are best effort and when performance is an overriding priority, 831 * as when methods of this class are optimized by the runtime 832 * compiler, some or all checks (if any) may be elided. Hence, the 833 * caller must not rely on the checks and corresponding 834 * exceptions! 835 * 836 * @throws RuntimeException if any of the arguments is invalid 837 * 838 * @since 9 839 */ 840 public void copySwapMemory(Object srcBase, long srcOffset, 841 Object destBase, long destOffset, 842 long bytes, long elemSize) { 843 copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 844 845 if (bytes == 0) { 846 return; 847 } 848 849 copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 850 } 851 852 private void copySwapMemoryChecks(Object srcBase, long srcOffset, 853 Object destBase, long destOffset, 854 long bytes, long elemSize) { 855 checkSize(bytes); 856 857 if (elemSize != 2 && elemSize != 4 && elemSize != 8) { 858 throw invalidInput(); 859 } 860 if (bytes % elemSize != 0) { 861 throw invalidInput(); 862 } 863 864 checkPrimitivePointer(srcBase, srcOffset); 865 checkPrimitivePointer(destBase, destOffset); 866 } 867 868 /** 869 * Copies all elements from one block of memory to another block, byte swapping the 870 * elements on the fly. 871 * 872 * This provides a <em>single-register</em> addressing mode, as 873 * discussed in {@link #getInt(Object,long)}. 874 * 875 * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}. 876 */ 877 public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) { 878 copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize); 879 } 880 881 /** 882 * Disposes of a block of native memory, as obtained from {@link 883 * #allocateMemory} or {@link #reallocateMemory}. The address passed to 884 * this method may be null, in which case no action is taken. 885 * 886 * <em>Note:</em> It is the resposibility of the caller to make 887 * sure arguments are checked before the methods are called. While 888 * some rudimentary checks are performed on the input, the checks 889 * are best effort and when performance is an overriding priority, 890 * as when methods of this class are optimized by the runtime 891 * compiler, some or all checks (if any) may be elided. Hence, the 892 * caller must not rely on the checks and corresponding 893 * exceptions! 894 * 895 * @throws RuntimeException if any of the arguments is invalid 896 * 897 * @see #allocateMemory 898 */ 899 public void freeMemory(long address) { 900 freeMemoryChecks(address); 901 902 if (address == 0) { 903 return; 904 } 905 906 freeMemory0(address); 907 } 908 909 /** 910 * Validate the arguments to freeMemory 911 * 912 * @throws RuntimeException if the arguments are invalid 913 * (<em>Note:</em> after optimization, invalid inputs may 914 * go undetected, which will lead to unpredictable 915 * behavior) 916 */ 917 private void freeMemoryChecks(long address) { 918 checkPointer(null, address); 919 } 920 921 /// random queries 922 923 /** 924 * This constant differs from all results that will ever be returned from 925 * {@link #staticFieldOffset}, {@link #objectFieldOffset}, 926 * or {@link #arrayBaseOffset}. 927 */ 928 public static final int INVALID_FIELD_OFFSET = -1; 929 930 /** 931 * Reports the location of a given field in the storage allocation of its 932 * class. Do not expect to perform any sort of arithmetic on this offset; 933 * it is just a cookie which is passed to the unsafe heap memory accessors. 934 * 935 * <p>Any given field will always have the same offset and base, and no 936 * two distinct fields of the same class will ever have the same offset 937 * and base. 938 * 939 * <p>As of 1.4.1, offsets for fields are represented as long values, 940 * although the Sun JVM does not use the most significant 32 bits. 941 * However, JVM implementations which store static fields at absolute 942 * addresses can use long offsets and null base pointers to express 943 * the field locations in a form usable by {@link #getInt(Object,long)}. 944 * Therefore, code which will be ported to such JVMs on 64-bit platforms 945 * must preserve all bits of static field offsets. 946 * @see #getInt(Object, long) 947 */ 948 public long objectFieldOffset(Field f) { 949 if (f == null) { 950 throw new NullPointerException(); 951 } 952 953 return objectFieldOffset0(f); 954 } 955 956 /** 957 * Reports the location of a given static field, in conjunction with {@link 958 * #staticFieldBase}. 959 * <p>Do not expect to perform any sort of arithmetic on this offset; 960 * it is just a cookie which is passed to the unsafe heap memory accessors. 961 * 962 * <p>Any given field will always have the same offset, and no two distinct 963 * fields of the same class will ever have the same offset. 964 * 965 * <p>As of 1.4.1, offsets for fields are represented as long values, 966 * although the Sun JVM does not use the most significant 32 bits. 967 * It is hard to imagine a JVM technology which needs more than 968 * a few bits to encode an offset within a non-array object, 969 * However, for consistency with other methods in this class, 970 * this method reports its result as a long value. 971 * @see #getInt(Object, long) 972 */ 973 public long staticFieldOffset(Field f) { 974 if (f == null) { 975 throw new NullPointerException(); 976 } 977 978 return staticFieldOffset0(f); 979 } 980 981 /** 982 * Reports the location of a given static field, in conjunction with {@link 983 * #staticFieldOffset}. 984 * <p>Fetch the base "Object", if any, with which static fields of the 985 * given class can be accessed via methods like {@link #getInt(Object, 986 * long)}. This value may be null. This value may refer to an object 987 * which is a "cookie", not guaranteed to be a real Object, and it should 988 * not be used in any way except as argument to the get and put routines in 989 * this class. 990 */ 991 public Object staticFieldBase(Field f) { 992 if (f == null) { 993 throw new NullPointerException(); 994 } 995 996 return staticFieldBase0(f); 997 } 998 999 /** 1000 * Detects if the given class may need to be initialized. This is often 1001 * needed in conjunction with obtaining the static field base of a 1002 * class. 1003 * @return false only if a call to {@code ensureClassInitialized} would have no effect 1004 */ 1005 public boolean shouldBeInitialized(Class<?> c) { 1006 if (c == null) { 1007 throw new NullPointerException(); 1008 } 1009 1010 return shouldBeInitialized0(c); 1011 } 1012 1013 /** 1014 * Ensures the given class has been initialized. This is often 1015 * needed in conjunction with obtaining the static field base of a 1016 * class. 1017 */ 1018 public void ensureClassInitialized(Class<?> c) { 1019 if (c == null) { 1020 throw new NullPointerException(); 1021 } 1022 1023 ensureClassInitialized0(c); 1024 } 1025 1026 /** 1027 * Reports the offset of the first element in the storage allocation of a 1028 * given array class. If {@link #arrayIndexScale} returns a non-zero value 1029 * for the same class, you may use that scale factor, together with this 1030 * base offset, to form new offsets to access elements of arrays of the 1031 * given class. 1032 * 1033 * @see #getInt(Object, long) 1034 * @see #putInt(Object, long, int) 1035 */ 1036 public int arrayBaseOffset(Class<?> arrayClass) { 1037 if (arrayClass == null) { 1038 throw new NullPointerException(); 1039 } 1040 1041 return arrayBaseOffset0(arrayClass); 1042 } 1043 1044 1045 /** The value of {@code arrayBaseOffset(boolean[].class)} */ 1046 public static final int ARRAY_BOOLEAN_BASE_OFFSET 1047 = theUnsafe.arrayBaseOffset(boolean[].class); 1048 1049 /** The value of {@code arrayBaseOffset(byte[].class)} */ 1050 public static final int ARRAY_BYTE_BASE_OFFSET 1051 = theUnsafe.arrayBaseOffset(byte[].class); 1052 1053 /** The value of {@code arrayBaseOffset(short[].class)} */ 1054 public static final int ARRAY_SHORT_BASE_OFFSET 1055 = theUnsafe.arrayBaseOffset(short[].class); 1056 1057 /** The value of {@code arrayBaseOffset(char[].class)} */ 1058 public static final int ARRAY_CHAR_BASE_OFFSET 1059 = theUnsafe.arrayBaseOffset(char[].class); 1060 1061 /** The value of {@code arrayBaseOffset(int[].class)} */ 1062 public static final int ARRAY_INT_BASE_OFFSET 1063 = theUnsafe.arrayBaseOffset(int[].class); 1064 1065 /** The value of {@code arrayBaseOffset(long[].class)} */ 1066 public static final int ARRAY_LONG_BASE_OFFSET 1067 = theUnsafe.arrayBaseOffset(long[].class); 1068 1069 /** The value of {@code arrayBaseOffset(float[].class)} */ 1070 public static final int ARRAY_FLOAT_BASE_OFFSET 1071 = theUnsafe.arrayBaseOffset(float[].class); 1072 1073 /** The value of {@code arrayBaseOffset(double[].class)} */ 1074 public static final int ARRAY_DOUBLE_BASE_OFFSET 1075 = theUnsafe.arrayBaseOffset(double[].class); 1076 1077 /** The value of {@code arrayBaseOffset(Object[].class)} */ 1078 public static final int ARRAY_OBJECT_BASE_OFFSET 1079 = theUnsafe.arrayBaseOffset(Object[].class); 1080 1081 /** 1082 * Reports the scale factor for addressing elements in the storage 1083 * allocation of a given array class. However, arrays of "narrow" types 1084 * will generally not work properly with accessors like {@link 1085 * #getByte(Object, long)}, so the scale factor for such classes is reported 1086 * as zero. 1087 * 1088 * @see #arrayBaseOffset 1089 * @see #getInt(Object, long) 1090 * @see #putInt(Object, long, int) 1091 */ 1092 public int arrayIndexScale(Class<?> arrayClass) { 1093 if (arrayClass == null) { 1094 throw new NullPointerException(); 1095 } 1096 1097 return arrayIndexScale0(arrayClass); 1098 } 1099 1100 1101 /** The value of {@code arrayIndexScale(boolean[].class)} */ 1102 public static final int ARRAY_BOOLEAN_INDEX_SCALE 1103 = theUnsafe.arrayIndexScale(boolean[].class); 1104 1105 /** The value of {@code arrayIndexScale(byte[].class)} */ 1106 public static final int ARRAY_BYTE_INDEX_SCALE 1107 = theUnsafe.arrayIndexScale(byte[].class); 1108 1109 /** The value of {@code arrayIndexScale(short[].class)} */ 1110 public static final int ARRAY_SHORT_INDEX_SCALE 1111 = theUnsafe.arrayIndexScale(short[].class); 1112 1113 /** The value of {@code arrayIndexScale(char[].class)} */ 1114 public static final int ARRAY_CHAR_INDEX_SCALE 1115 = theUnsafe.arrayIndexScale(char[].class); 1116 1117 /** The value of {@code arrayIndexScale(int[].class)} */ 1118 public static final int ARRAY_INT_INDEX_SCALE 1119 = theUnsafe.arrayIndexScale(int[].class); 1120 1121 /** The value of {@code arrayIndexScale(long[].class)} */ 1122 public static final int ARRAY_LONG_INDEX_SCALE 1123 = theUnsafe.arrayIndexScale(long[].class); 1124 1125 /** The value of {@code arrayIndexScale(float[].class)} */ 1126 public static final int ARRAY_FLOAT_INDEX_SCALE 1127 = theUnsafe.arrayIndexScale(float[].class); 1128 1129 /** The value of {@code arrayIndexScale(double[].class)} */ 1130 public static final int ARRAY_DOUBLE_INDEX_SCALE 1131 = theUnsafe.arrayIndexScale(double[].class); 1132 1133 /** The value of {@code arrayIndexScale(Object[].class)} */ 1134 public static final int ARRAY_OBJECT_INDEX_SCALE 1135 = theUnsafe.arrayIndexScale(Object[].class); 1136 1137 /** 1138 * Reports the size in bytes of a native pointer, as stored via {@link 1139 * #putAddress}. This value will be either 4 or 8. Note that the sizes of 1140 * other primitive types (as stored in native memory blocks) is determined 1141 * fully by their information content. 1142 */ 1143 public int addressSize() { 1144 return ADDRESS_SIZE; 1145 } 1146 1147 /** The value of {@code addressSize()} */ 1148 public static final int ADDRESS_SIZE = theUnsafe.addressSize0(); 1149 1150 /** 1151 * Reports the size in bytes of a native memory page (whatever that is). 1152 * This value will always be a power of two. 1153 */ 1154 public native int pageSize(); 1155 1156 1157 /// random trusted operations from JNI: 1158 1159 /** 1160 * Tells the VM to define a class, without security checks. By default, the 1161 * class loader and protection domain come from the caller's class. 1162 */ 1163 public Class<?> defineClass(String name, byte[] b, int off, int len, 1164 ClassLoader loader, 1165 ProtectionDomain protectionDomain) { 1166 if (b == null) { 1167 throw new NullPointerException(); 1168 } 1169 if (len < 0) { 1170 throw new ArrayIndexOutOfBoundsException(); 1171 } 1172 1173 return defineClass0(name, b, off, len, loader, protectionDomain); 1174 } 1175 1176 public native Class<?> defineClass0(String name, byte[] b, int off, int len, 1177 ClassLoader loader, 1178 ProtectionDomain protectionDomain); 1179 1180 /** 1181 * Defines a class but does not make it known to the class loader or system dictionary. 1182 * <p> 1183 * For each CP entry, the corresponding CP patch must either be null or have 1184 * the a format that matches its tag: 1185 * <ul> 1186 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang 1187 * <li>Utf8: a string (must have suitable syntax if used as signature or name) 1188 * <li>Class: any java.lang.Class object 1189 * <li>String: any object (not just a java.lang.String) 1190 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments 1191 * </ul> 1192 * @param hostClass context for linkage, access control, protection domain, and class loader 1193 * @param data bytes of a class file 1194 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data 1195 */ 1196 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) { 1197 if (hostClass == null || data == null) { 1198 throw new NullPointerException(); 1199 } 1200 1201 return defineAnonymousClass0(hostClass, data, cpPatches); 1202 } 1203 1204 /** 1205 * Allocates an instance but does not run any constructor. 1206 * Initializes the class if it has not yet been. 1207 */ 1208 @HotSpotIntrinsicCandidate 1209 public native Object allocateInstance(Class<?> cls) 1210 throws InstantiationException; 1211 1212 /** 1213 * Allocates an array of a given type, but does not do zeroing. 1214 * <p> 1215 * This method should only be used in the very rare cases where a high-performance code 1216 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination. 1217 * In an overwhelming majority of cases, a normal Java allocation should be used instead. 1218 * <p> 1219 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents 1220 * before allowing untrusted code, or code in other threads, to observe the reference 1221 * to the newly allocated array. In addition, the publication of the array reference must be 1222 * safe according to the Java Memory Model requirements. 1223 * <p> 1224 * The safest approach to deal with an uninitialized array is to keep the reference to it in local 1225 * variable at least until the initialization is complete, and then publish it <b>once</b>, either 1226 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor, 1227 * or issuing a {@link #storeFence} before publishing the reference. 1228 * <p> 1229 * @implnote This method can only allocate primitive arrays, to avoid garbage reference 1230 * elements that could break heap integrity. 1231 * 1232 * @param componentType array component type to allocate 1233 * @param length array size to allocate 1234 * @throws IllegalArgumentException if component type is null, or not a primitive class; 1235 * or the length is negative 1236 */ 1237 public Object allocateUninitializedArray(Class<?> componentType, int length) { 1238 if (componentType == null) { 1239 throw new IllegalArgumentException("Component type is null"); 1240 } 1241 if (!componentType.isPrimitive()) { 1242 throw new IllegalArgumentException("Component type is not primitive"); 1243 } 1244 if (length < 0) { 1245 throw new IllegalArgumentException("Negative length"); 1246 } 1247 return allocateUninitializedArray0(componentType, length); 1248 } 1249 1250 @HotSpotIntrinsicCandidate 1251 private Object allocateUninitializedArray0(Class<?> componentType, int length) { 1252 // These fallbacks provide zeroed arrays, but intrinsic is not required to 1253 // return the zeroed arrays. 1254 if (componentType == byte.class) return new byte[length]; 1255 if (componentType == boolean.class) return new boolean[length]; 1256 if (componentType == short.class) return new short[length]; 1257 if (componentType == char.class) return new char[length]; 1258 if (componentType == int.class) return new int[length]; 1259 if (componentType == float.class) return new float[length]; 1260 if (componentType == long.class) return new long[length]; 1261 if (componentType == double.class) return new double[length]; 1262 return null; 1263 } 1264 1265 /** Throws the exception without telling the verifier. */ 1266 public native void throwException(Throwable ee); 1267 1268 /** 1269 * Atomically updates Java variable to {@code x} if it is currently 1270 * holding {@code expected}. 1271 * 1272 * <p>This operation has memory semantics of a {@code volatile} read 1273 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1274 * 1275 * @return {@code true} if successful 1276 */ 1277 @HotSpotIntrinsicCandidate 1278 public final native boolean compareAndSwapObject(Object o, long offset, 1279 Object expected, 1280 Object x); 1281 1282 @HotSpotIntrinsicCandidate 1283 public final native Object compareAndExchangeObjectVolatile(Object o, long offset, 1284 Object expected, 1285 Object x); 1286 1287 @HotSpotIntrinsicCandidate 1288 public final Object compareAndExchangeObjectAcquire(Object o, long offset, 1289 Object expected, 1290 Object x) { 1291 return compareAndExchangeObjectVolatile(o, offset, expected, x); 1292 } 1293 1294 @HotSpotIntrinsicCandidate 1295 public final Object compareAndExchangeObjectRelease(Object o, long offset, 1296 Object expected, 1297 Object x) { 1298 return compareAndExchangeObjectVolatile(o, offset, expected, x); 1299 } 1300 1301 @HotSpotIntrinsicCandidate 1302 public final boolean weakCompareAndSwapObject(Object o, long offset, 1303 Object expected, 1304 Object x) { 1305 return compareAndSwapObject(o, offset, expected, x); 1306 } 1307 1308 @HotSpotIntrinsicCandidate 1309 public final boolean weakCompareAndSwapObjectAcquire(Object o, long offset, 1310 Object expected, 1311 Object x) { 1312 return compareAndSwapObject(o, offset, expected, x); 1313 } 1314 1315 @HotSpotIntrinsicCandidate 1316 public final boolean weakCompareAndSwapObjectRelease(Object o, long offset, 1317 Object expected, 1318 Object x) { 1319 return compareAndSwapObject(o, offset, expected, x); 1320 } 1321 1322 @HotSpotIntrinsicCandidate 1323 public final boolean weakCompareAndSwapObjectVolatile(Object o, long offset, 1324 Object expected, 1325 Object x) { 1326 return compareAndSwapObject(o, offset, expected, x); 1327 } 1328 1329 /** 1330 * Atomically updates Java variable to {@code x} if it is currently 1331 * holding {@code expected}. 1332 * 1333 * <p>This operation has memory semantics of a {@code volatile} read 1334 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1335 * 1336 * @return {@code true} if successful 1337 */ 1338 @HotSpotIntrinsicCandidate 1339 public final native boolean compareAndSwapInt(Object o, long offset, 1340 int expected, 1341 int x); 1342 1343 @HotSpotIntrinsicCandidate 1344 public final native int compareAndExchangeIntVolatile(Object o, long offset, 1345 int expected, 1346 int x); 1347 1348 @HotSpotIntrinsicCandidate 1349 public final int compareAndExchangeIntAcquire(Object o, long offset, 1350 int expected, 1351 int x) { 1352 return compareAndExchangeIntVolatile(o, offset, expected, x); 1353 } 1354 1355 @HotSpotIntrinsicCandidate 1356 public final int compareAndExchangeIntRelease(Object o, long offset, 1357 int expected, 1358 int x) { 1359 return compareAndExchangeIntVolatile(o, offset, expected, x); 1360 } 1361 1362 @HotSpotIntrinsicCandidate 1363 public final boolean weakCompareAndSwapInt(Object o, long offset, 1364 int expected, 1365 int x) { 1366 return compareAndSwapInt(o, offset, expected, x); 1367 } 1368 1369 @HotSpotIntrinsicCandidate 1370 public final boolean weakCompareAndSwapIntAcquire(Object o, long offset, 1371 int expected, 1372 int x) { 1373 return compareAndSwapInt(o, offset, expected, x); 1374 } 1375 1376 @HotSpotIntrinsicCandidate 1377 public final boolean weakCompareAndSwapIntRelease(Object o, long offset, 1378 int expected, 1379 int x) { 1380 return compareAndSwapInt(o, offset, expected, x); 1381 } 1382 1383 @HotSpotIntrinsicCandidate 1384 public final boolean weakCompareAndSwapIntVolatile(Object o, long offset, 1385 int expected, 1386 int x) { 1387 return compareAndSwapInt(o, offset, expected, x); 1388 } 1389 1390 @HotSpotIntrinsicCandidate 1391 public final byte compareAndExchangeByteVolatile(Object o, long offset, 1392 byte expected, 1393 byte x) { 1394 long wordOffset = offset & ~3; 1395 int shift = (int) (offset & 3) << 3; 1396 if (BE) { 1397 shift = 24 - shift; 1398 } 1399 int mask = 0xFF << shift; 1400 int maskedExpected = (expected & 0xFF) << shift; 1401 int maskedX = (x & 0xFF) << shift; 1402 int fullWord; 1403 do { 1404 fullWord = getIntVolatile(o, wordOffset); 1405 if ((fullWord & mask) != maskedExpected) 1406 return (byte) ((fullWord & mask) >> shift); 1407 } while (!weakCompareAndSwapIntVolatile(o, wordOffset, 1408 fullWord, (fullWord & ~mask) | maskedX)); 1409 return expected; 1410 } 1411 1412 @HotSpotIntrinsicCandidate 1413 public final boolean compareAndSwapByte(Object o, long offset, 1414 byte expected, 1415 byte x) { 1416 return compareAndExchangeByteVolatile(o, offset, expected, x) == expected; 1417 } 1418 1419 @HotSpotIntrinsicCandidate 1420 public final boolean weakCompareAndSwapByteVolatile(Object o, long offset, 1421 byte expected, 1422 byte x) { 1423 return compareAndSwapByte(o, offset, expected, x); 1424 } 1425 1426 @HotSpotIntrinsicCandidate 1427 public final boolean weakCompareAndSwapByteAcquire(Object o, long offset, 1428 byte expected, 1429 byte x) { 1430 return weakCompareAndSwapByteVolatile(o, offset, expected, x); 1431 } 1432 1433 @HotSpotIntrinsicCandidate 1434 public final boolean weakCompareAndSwapByteRelease(Object o, long offset, 1435 byte expected, 1436 byte x) { 1437 return weakCompareAndSwapByteVolatile(o, offset, expected, x); 1438 } 1439 1440 @HotSpotIntrinsicCandidate 1441 public final boolean weakCompareAndSwapByte(Object o, long offset, 1442 byte expected, 1443 byte x) { 1444 return weakCompareAndSwapByteVolatile(o, offset, expected, x); 1445 } 1446 1447 @HotSpotIntrinsicCandidate 1448 public final byte compareAndExchangeByteAcquire(Object o, long offset, 1449 byte expected, 1450 byte x) { 1451 return compareAndExchangeByteVolatile(o, offset, expected, x); 1452 } 1453 1454 @HotSpotIntrinsicCandidate 1455 public final byte compareAndExchangeByteRelease(Object o, long offset, 1456 byte expected, 1457 byte x) { 1458 return compareAndExchangeByteVolatile(o, offset, expected, x); 1459 } 1460 1461 @HotSpotIntrinsicCandidate 1462 public final short compareAndExchangeShortVolatile(Object o, long offset, 1463 short expected, 1464 short x) { 1465 if ((offset & 3) == 3) { 1466 throw new IllegalArgumentException("Update spans the word, not supported"); 1467 } 1468 long wordOffset = offset & ~3; 1469 int shift = (int) (offset & 3) << 3; 1470 if (BE) { 1471 shift = 16 - shift; 1472 } 1473 int mask = 0xFFFF << shift; 1474 int maskedExpected = (expected & 0xFFFF) << shift; 1475 int maskedX = (x & 0xFFFF) << shift; 1476 int fullWord; 1477 do { 1478 fullWord = getIntVolatile(o, wordOffset); 1479 if ((fullWord & mask) != maskedExpected) { 1480 return (short) ((fullWord & mask) >> shift); 1481 } 1482 } while (!weakCompareAndSwapIntVolatile(o, wordOffset, 1483 fullWord, (fullWord & ~mask) | maskedX)); 1484 return expected; 1485 } 1486 1487 @HotSpotIntrinsicCandidate 1488 public final boolean compareAndSwapShort(Object o, long offset, 1489 short expected, 1490 short x) { 1491 return compareAndExchangeShortVolatile(o, offset, expected, x) == expected; 1492 } 1493 1494 @HotSpotIntrinsicCandidate 1495 public final boolean weakCompareAndSwapShortVolatile(Object o, long offset, 1496 short expected, 1497 short x) { 1498 return compareAndSwapShort(o, offset, expected, x); 1499 } 1500 1501 @HotSpotIntrinsicCandidate 1502 public final boolean weakCompareAndSwapShortAcquire(Object o, long offset, 1503 short expected, 1504 short x) { 1505 return weakCompareAndSwapShortVolatile(o, offset, expected, x); 1506 } 1507 1508 @HotSpotIntrinsicCandidate 1509 public final boolean weakCompareAndSwapShortRelease(Object o, long offset, 1510 short expected, 1511 short x) { 1512 return weakCompareAndSwapShortVolatile(o, offset, expected, x); 1513 } 1514 1515 @HotSpotIntrinsicCandidate 1516 public final boolean weakCompareAndSwapShort(Object o, long offset, 1517 short expected, 1518 short x) { 1519 return weakCompareAndSwapShortVolatile(o, offset, expected, x); 1520 } 1521 1522 1523 @HotSpotIntrinsicCandidate 1524 public final short compareAndExchangeShortAcquire(Object o, long offset, 1525 short expected, 1526 short x) { 1527 return compareAndExchangeShortVolatile(o, offset, expected, x); 1528 } 1529 1530 @HotSpotIntrinsicCandidate 1531 public final short compareAndExchangeShortRelease(Object o, long offset, 1532 short expected, 1533 short x) { 1534 return compareAndExchangeShortVolatile(o, offset, expected, x); 1535 } 1536 1537 @ForceInline 1538 private char s2c(short s) { 1539 return (char) s; 1540 } 1541 1542 @ForceInline 1543 private short c2s(char s) { 1544 return (short) s; 1545 } 1546 1547 @ForceInline 1548 public final boolean compareAndSwapChar(Object o, long offset, 1549 char expected, 1550 char x) { 1551 return compareAndSwapShort(o, offset, c2s(expected), c2s(x)); 1552 } 1553 1554 @ForceInline 1555 public final char compareAndExchangeCharVolatile(Object o, long offset, 1556 char expected, 1557 char x) { 1558 return s2c(compareAndExchangeShortVolatile(o, offset, c2s(expected), c2s(x))); 1559 } 1560 1561 @ForceInline 1562 public final char compareAndExchangeCharAcquire(Object o, long offset, 1563 char expected, 1564 char x) { 1565 return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x))); 1566 } 1567 1568 @ForceInline 1569 public final char compareAndExchangeCharRelease(Object o, long offset, 1570 char expected, 1571 char x) { 1572 return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x))); 1573 } 1574 1575 @ForceInline 1576 public final boolean weakCompareAndSwapCharVolatile(Object o, long offset, 1577 char expected, 1578 char x) { 1579 return weakCompareAndSwapShortVolatile(o, offset, c2s(expected), c2s(x)); 1580 } 1581 1582 @ForceInline 1583 public final boolean weakCompareAndSwapCharAcquire(Object o, long offset, 1584 char expected, 1585 char x) { 1586 return weakCompareAndSwapShortAcquire(o, offset, c2s(expected), c2s(x)); 1587 } 1588 1589 @ForceInline 1590 public final boolean weakCompareAndSwapCharRelease(Object o, long offset, 1591 char expected, 1592 char x) { 1593 return weakCompareAndSwapShortRelease(o, offset, c2s(expected), c2s(x)); 1594 } 1595 1596 @ForceInline 1597 public final boolean weakCompareAndSwapChar(Object o, long offset, 1598 char expected, 1599 char x) { 1600 return weakCompareAndSwapShort(o, offset, c2s(expected), c2s(x)); 1601 } 1602 1603 @ForceInline 1604 private boolean byte2bool(byte b) { 1605 return b > 0; 1606 } 1607 1608 @ForceInline 1609 private byte bool2byte(boolean b) { 1610 return b ? (byte)1 : (byte)0; 1611 } 1612 1613 @ForceInline 1614 public final boolean compareAndSwapBoolean(Object o, long offset, 1615 boolean expected, 1616 boolean x) { 1617 return compareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x)); 1618 } 1619 1620 @ForceInline 1621 public final boolean compareAndExchangeBooleanVolatile(Object o, long offset, 1622 boolean expected, 1623 boolean x) { 1624 return byte2bool(compareAndExchangeByteVolatile(o, offset, bool2byte(expected), bool2byte(x))); 1625 } 1626 1627 @ForceInline 1628 public final boolean compareAndExchangeBooleanAcquire(Object o, long offset, 1629 boolean expected, 1630 boolean x) { 1631 return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x))); 1632 } 1633 1634 @ForceInline 1635 public final boolean compareAndExchangeBooleanRelease(Object o, long offset, 1636 boolean expected, 1637 boolean x) { 1638 return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x))); 1639 } 1640 1641 @ForceInline 1642 public final boolean weakCompareAndSwapBooleanVolatile(Object o, long offset, 1643 boolean expected, 1644 boolean x) { 1645 return weakCompareAndSwapByteVolatile(o, offset, bool2byte(expected), bool2byte(x)); 1646 } 1647 1648 @ForceInline 1649 public final boolean weakCompareAndSwapBooleanAcquire(Object o, long offset, 1650 boolean expected, 1651 boolean x) { 1652 return weakCompareAndSwapByteAcquire(o, offset, bool2byte(expected), bool2byte(x)); 1653 } 1654 1655 @ForceInline 1656 public final boolean weakCompareAndSwapBooleanRelease(Object o, long offset, 1657 boolean expected, 1658 boolean x) { 1659 return weakCompareAndSwapByteRelease(o, offset, bool2byte(expected), bool2byte(x)); 1660 } 1661 1662 @ForceInline 1663 public final boolean weakCompareAndSwapBoolean(Object o, long offset, 1664 boolean expected, 1665 boolean x) { 1666 return weakCompareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x)); 1667 } 1668 1669 /** 1670 * Atomically updates Java variable to {@code x} if it is currently 1671 * holding {@code expected}. 1672 * 1673 * <p>This operation has memory semantics of a {@code volatile} read 1674 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1675 * 1676 * @return {@code true} if successful 1677 */ 1678 @ForceInline 1679 public final boolean compareAndSwapFloat(Object o, long offset, 1680 float expected, 1681 float x) { 1682 return compareAndSwapInt(o, offset, 1683 Float.floatToRawIntBits(expected), 1684 Float.floatToRawIntBits(x)); 1685 } 1686 1687 @ForceInline 1688 public final float compareAndExchangeFloatVolatile(Object o, long offset, 1689 float expected, 1690 float x) { 1691 int w = compareAndExchangeIntVolatile(o, offset, 1692 Float.floatToRawIntBits(expected), 1693 Float.floatToRawIntBits(x)); 1694 return Float.intBitsToFloat(w); 1695 } 1696 1697 @ForceInline 1698 public final float compareAndExchangeFloatAcquire(Object o, long offset, 1699 float expected, 1700 float x) { 1701 int w = compareAndExchangeIntAcquire(o, offset, 1702 Float.floatToRawIntBits(expected), 1703 Float.floatToRawIntBits(x)); 1704 return Float.intBitsToFloat(w); 1705 } 1706 1707 @ForceInline 1708 public final float compareAndExchangeFloatRelease(Object o, long offset, 1709 float expected, 1710 float x) { 1711 int w = compareAndExchangeIntRelease(o, offset, 1712 Float.floatToRawIntBits(expected), 1713 Float.floatToRawIntBits(x)); 1714 return Float.intBitsToFloat(w); 1715 } 1716 1717 @ForceInline 1718 public final boolean weakCompareAndSwapFloat(Object o, long offset, 1719 float expected, 1720 float x) { 1721 return weakCompareAndSwapInt(o, offset, 1722 Float.floatToRawIntBits(expected), 1723 Float.floatToRawIntBits(x)); 1724 } 1725 1726 @ForceInline 1727 public final boolean weakCompareAndSwapFloatAcquire(Object o, long offset, 1728 float expected, 1729 float x) { 1730 return weakCompareAndSwapIntAcquire(o, offset, 1731 Float.floatToRawIntBits(expected), 1732 Float.floatToRawIntBits(x)); 1733 } 1734 1735 @ForceInline 1736 public final boolean weakCompareAndSwapFloatRelease(Object o, long offset, 1737 float expected, 1738 float x) { 1739 return weakCompareAndSwapIntRelease(o, offset, 1740 Float.floatToRawIntBits(expected), 1741 Float.floatToRawIntBits(x)); 1742 } 1743 1744 @ForceInline 1745 public final boolean weakCompareAndSwapFloatVolatile(Object o, long offset, 1746 float expected, 1747 float x) { 1748 return weakCompareAndSwapIntVolatile(o, offset, 1749 Float.floatToRawIntBits(expected), 1750 Float.floatToRawIntBits(x)); 1751 } 1752 1753 /** 1754 * Atomically updates Java variable to {@code x} if it is currently 1755 * holding {@code expected}. 1756 * 1757 * <p>This operation has memory semantics of a {@code volatile} read 1758 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1759 * 1760 * @return {@code true} if successful 1761 */ 1762 @ForceInline 1763 public final boolean compareAndSwapDouble(Object o, long offset, 1764 double expected, 1765 double x) { 1766 return compareAndSwapLong(o, offset, 1767 Double.doubleToRawLongBits(expected), 1768 Double.doubleToRawLongBits(x)); 1769 } 1770 1771 @ForceInline 1772 public final double compareAndExchangeDoubleVolatile(Object o, long offset, 1773 double expected, 1774 double x) { 1775 long w = compareAndExchangeLongVolatile(o, offset, 1776 Double.doubleToRawLongBits(expected), 1777 Double.doubleToRawLongBits(x)); 1778 return Double.longBitsToDouble(w); 1779 } 1780 1781 @ForceInline 1782 public final double compareAndExchangeDoubleAcquire(Object o, long offset, 1783 double expected, 1784 double x) { 1785 long w = compareAndExchangeLongAcquire(o, offset, 1786 Double.doubleToRawLongBits(expected), 1787 Double.doubleToRawLongBits(x)); 1788 return Double.longBitsToDouble(w); 1789 } 1790 1791 @ForceInline 1792 public final double compareAndExchangeDoubleRelease(Object o, long offset, 1793 double expected, 1794 double x) { 1795 long w = compareAndExchangeLongRelease(o, offset, 1796 Double.doubleToRawLongBits(expected), 1797 Double.doubleToRawLongBits(x)); 1798 return Double.longBitsToDouble(w); 1799 } 1800 1801 @ForceInline 1802 public final boolean weakCompareAndSwapDouble(Object o, long offset, 1803 double expected, 1804 double x) { 1805 return weakCompareAndSwapLong(o, offset, 1806 Double.doubleToRawLongBits(expected), 1807 Double.doubleToRawLongBits(x)); 1808 } 1809 1810 @ForceInline 1811 public final boolean weakCompareAndSwapDoubleAcquire(Object o, long offset, 1812 double expected, 1813 double x) { 1814 return weakCompareAndSwapLongAcquire(o, offset, 1815 Double.doubleToRawLongBits(expected), 1816 Double.doubleToRawLongBits(x)); 1817 } 1818 1819 @ForceInline 1820 public final boolean weakCompareAndSwapDoubleRelease(Object o, long offset, 1821 double expected, 1822 double x) { 1823 return weakCompareAndSwapLongRelease(o, offset, 1824 Double.doubleToRawLongBits(expected), 1825 Double.doubleToRawLongBits(x)); 1826 } 1827 1828 @ForceInline 1829 public final boolean weakCompareAndSwapDoubleVolatile(Object o, long offset, 1830 double expected, 1831 double x) { 1832 return weakCompareAndSwapLongVolatile(o, offset, 1833 Double.doubleToRawLongBits(expected), 1834 Double.doubleToRawLongBits(x)); 1835 } 1836 1837 /** 1838 * Atomically updates Java variable to {@code x} if it is currently 1839 * holding {@code expected}. 1840 * 1841 * <p>This operation has memory semantics of a {@code volatile} read 1842 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1843 * 1844 * @return {@code true} if successful 1845 */ 1846 @HotSpotIntrinsicCandidate 1847 public final native boolean compareAndSwapLong(Object o, long offset, 1848 long expected, 1849 long x); 1850 1851 @HotSpotIntrinsicCandidate 1852 public final native long compareAndExchangeLongVolatile(Object o, long offset, 1853 long expected, 1854 long x); 1855 1856 @HotSpotIntrinsicCandidate 1857 public final long compareAndExchangeLongAcquire(Object o, long offset, 1858 long expected, 1859 long x) { 1860 return compareAndExchangeLongVolatile(o, offset, expected, x); 1861 } 1862 1863 @HotSpotIntrinsicCandidate 1864 public final long compareAndExchangeLongRelease(Object o, long offset, 1865 long expected, 1866 long x) { 1867 return compareAndExchangeLongVolatile(o, offset, expected, x); 1868 } 1869 1870 @HotSpotIntrinsicCandidate 1871 public final boolean weakCompareAndSwapLong(Object o, long offset, 1872 long expected, 1873 long x) { 1874 return compareAndSwapLong(o, offset, expected, x); 1875 } 1876 1877 @HotSpotIntrinsicCandidate 1878 public final boolean weakCompareAndSwapLongAcquire(Object o, long offset, 1879 long expected, 1880 long x) { 1881 return compareAndSwapLong(o, offset, expected, x); 1882 } 1883 1884 @HotSpotIntrinsicCandidate 1885 public final boolean weakCompareAndSwapLongRelease(Object o, long offset, 1886 long expected, 1887 long x) { 1888 return compareAndSwapLong(o, offset, expected, x); 1889 } 1890 1891 @HotSpotIntrinsicCandidate 1892 public final boolean weakCompareAndSwapLongVolatile(Object o, long offset, 1893 long expected, 1894 long x) { 1895 return compareAndSwapLong(o, offset, expected, x); 1896 } 1897 1898 /** 1899 * Fetches a reference value from a given Java variable, with volatile 1900 * load semantics. Otherwise identical to {@link #getObject(Object, long)} 1901 */ 1902 @HotSpotIntrinsicCandidate 1903 public native Object getObjectVolatile(Object o, long offset); 1904 1905 /** 1906 * Stores a reference value into a given Java variable, with 1907 * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)} 1908 */ 1909 @HotSpotIntrinsicCandidate 1910 public native void putObjectVolatile(Object o, long offset, Object x); 1911 1912 /** Volatile version of {@link #getInt(Object, long)} */ 1913 @HotSpotIntrinsicCandidate 1914 public native int getIntVolatile(Object o, long offset); 1915 1916 /** Volatile version of {@link #putInt(Object, long, int)} */ 1917 @HotSpotIntrinsicCandidate 1918 public native void putIntVolatile(Object o, long offset, int x); 1919 1920 /** Volatile version of {@link #getBoolean(Object, long)} */ 1921 @HotSpotIntrinsicCandidate 1922 public native boolean getBooleanVolatile(Object o, long offset); 1923 1924 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */ 1925 @HotSpotIntrinsicCandidate 1926 public native void putBooleanVolatile(Object o, long offset, boolean x); 1927 1928 /** Volatile version of {@link #getByte(Object, long)} */ 1929 @HotSpotIntrinsicCandidate 1930 public native byte getByteVolatile(Object o, long offset); 1931 1932 /** Volatile version of {@link #putByte(Object, long, byte)} */ 1933 @HotSpotIntrinsicCandidate 1934 public native void putByteVolatile(Object o, long offset, byte x); 1935 1936 /** Volatile version of {@link #getShort(Object, long)} */ 1937 @HotSpotIntrinsicCandidate 1938 public native short getShortVolatile(Object o, long offset); 1939 1940 /** Volatile version of {@link #putShort(Object, long, short)} */ 1941 @HotSpotIntrinsicCandidate 1942 public native void putShortVolatile(Object o, long offset, short x); 1943 1944 /** Volatile version of {@link #getChar(Object, long)} */ 1945 @HotSpotIntrinsicCandidate 1946 public native char getCharVolatile(Object o, long offset); 1947 1948 /** Volatile version of {@link #putChar(Object, long, char)} */ 1949 @HotSpotIntrinsicCandidate 1950 public native void putCharVolatile(Object o, long offset, char x); 1951 1952 /** Volatile version of {@link #getLong(Object, long)} */ 1953 @HotSpotIntrinsicCandidate 1954 public native long getLongVolatile(Object o, long offset); 1955 1956 /** Volatile version of {@link #putLong(Object, long, long)} */ 1957 @HotSpotIntrinsicCandidate 1958 public native void putLongVolatile(Object o, long offset, long x); 1959 1960 /** Volatile version of {@link #getFloat(Object, long)} */ 1961 @HotSpotIntrinsicCandidate 1962 public native float getFloatVolatile(Object o, long offset); 1963 1964 /** Volatile version of {@link #putFloat(Object, long, float)} */ 1965 @HotSpotIntrinsicCandidate 1966 public native void putFloatVolatile(Object o, long offset, float x); 1967 1968 /** Volatile version of {@link #getDouble(Object, long)} */ 1969 @HotSpotIntrinsicCandidate 1970 public native double getDoubleVolatile(Object o, long offset); 1971 1972 /** Volatile version of {@link #putDouble(Object, long, double)} */ 1973 @HotSpotIntrinsicCandidate 1974 public native void putDoubleVolatile(Object o, long offset, double x); 1975 1976 1977 1978 /** Acquire version of {@link #getObjectVolatile(Object, long)} */ 1979 @HotSpotIntrinsicCandidate 1980 public final Object getObjectAcquire(Object o, long offset) { 1981 return getObjectVolatile(o, offset); 1982 } 1983 1984 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */ 1985 @HotSpotIntrinsicCandidate 1986 public final boolean getBooleanAcquire(Object o, long offset) { 1987 return getBooleanVolatile(o, offset); 1988 } 1989 1990 /** Acquire version of {@link #getByteVolatile(Object, long)} */ 1991 @HotSpotIntrinsicCandidate 1992 public final byte getByteAcquire(Object o, long offset) { 1993 return getByteVolatile(o, offset); 1994 } 1995 1996 /** Acquire version of {@link #getShortVolatile(Object, long)} */ 1997 @HotSpotIntrinsicCandidate 1998 public final short getShortAcquire(Object o, long offset) { 1999 return getShortVolatile(o, offset); 2000 } 2001 2002 /** Acquire version of {@link #getCharVolatile(Object, long)} */ 2003 @HotSpotIntrinsicCandidate 2004 public final char getCharAcquire(Object o, long offset) { 2005 return getCharVolatile(o, offset); 2006 } 2007 2008 /** Acquire version of {@link #getIntVolatile(Object, long)} */ 2009 @HotSpotIntrinsicCandidate 2010 public final int getIntAcquire(Object o, long offset) { 2011 return getIntVolatile(o, offset); 2012 } 2013 2014 /** Acquire version of {@link #getFloatVolatile(Object, long)} */ 2015 @HotSpotIntrinsicCandidate 2016 public final float getFloatAcquire(Object o, long offset) { 2017 return getFloatVolatile(o, offset); 2018 } 2019 2020 /** Acquire version of {@link #getLongVolatile(Object, long)} */ 2021 @HotSpotIntrinsicCandidate 2022 public final long getLongAcquire(Object o, long offset) { 2023 return getLongVolatile(o, offset); 2024 } 2025 2026 /** Acquire version of {@link #getDoubleVolatile(Object, long)} */ 2027 @HotSpotIntrinsicCandidate 2028 public final double getDoubleAcquire(Object o, long offset) { 2029 return getDoubleVolatile(o, offset); 2030 } 2031 2032 /* 2033 * Versions of {@link #putObjectVolatile(Object, long, Object)} 2034 * that do not guarantee immediate visibility of the store to 2035 * other threads. This method is generally only useful if the 2036 * underlying field is a Java volatile (or if an array cell, one 2037 * that is otherwise only accessed using volatile accesses). 2038 * 2039 * Corresponds to C11 atomic_store_explicit(..., memory_order_release). 2040 */ 2041 2042 /** Release version of {@link #putObjectVolatile(Object, long, Object)} */ 2043 @HotSpotIntrinsicCandidate 2044 public final void putObjectRelease(Object o, long offset, Object x) { 2045 putObjectVolatile(o, offset, x); 2046 } 2047 2048 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2049 @HotSpotIntrinsicCandidate 2050 public final void putBooleanRelease(Object o, long offset, boolean x) { 2051 putBooleanVolatile(o, offset, x); 2052 } 2053 2054 /** Release version of {@link #putByteVolatile(Object, long, byte)} */ 2055 @HotSpotIntrinsicCandidate 2056 public final void putByteRelease(Object o, long offset, byte x) { 2057 putByteVolatile(o, offset, x); 2058 } 2059 2060 /** Release version of {@link #putShortVolatile(Object, long, short)} */ 2061 @HotSpotIntrinsicCandidate 2062 public final void putShortRelease(Object o, long offset, short x) { 2063 putShortVolatile(o, offset, x); 2064 } 2065 2066 /** Release version of {@link #putCharVolatile(Object, long, char)} */ 2067 @HotSpotIntrinsicCandidate 2068 public final void putCharRelease(Object o, long offset, char x) { 2069 putCharVolatile(o, offset, x); 2070 } 2071 2072 /** Release version of {@link #putIntVolatile(Object, long, int)} */ 2073 @HotSpotIntrinsicCandidate 2074 public final void putIntRelease(Object o, long offset, int x) { 2075 putIntVolatile(o, offset, x); 2076 } 2077 2078 /** Release version of {@link #putFloatVolatile(Object, long, float)} */ 2079 @HotSpotIntrinsicCandidate 2080 public final void putFloatRelease(Object o, long offset, float x) { 2081 putFloatVolatile(o, offset, x); 2082 } 2083 2084 /** Release version of {@link #putLongVolatile(Object, long, long)} */ 2085 @HotSpotIntrinsicCandidate 2086 public final void putLongRelease(Object o, long offset, long x) { 2087 putLongVolatile(o, offset, x); 2088 } 2089 2090 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */ 2091 @HotSpotIntrinsicCandidate 2092 public final void putDoubleRelease(Object o, long offset, double x) { 2093 putDoubleVolatile(o, offset, x); 2094 } 2095 2096 // ------------------------------ Opaque -------------------------------------- 2097 2098 /** Opaque version of {@link #getObjectVolatile(Object, long)} */ 2099 @HotSpotIntrinsicCandidate 2100 public final Object getObjectOpaque(Object o, long offset) { 2101 return getObjectVolatile(o, offset); 2102 } 2103 2104 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */ 2105 @HotSpotIntrinsicCandidate 2106 public final boolean getBooleanOpaque(Object o, long offset) { 2107 return getBooleanVolatile(o, offset); 2108 } 2109 2110 /** Opaque version of {@link #getByteVolatile(Object, long)} */ 2111 @HotSpotIntrinsicCandidate 2112 public final byte getByteOpaque(Object o, long offset) { 2113 return getByteVolatile(o, offset); 2114 } 2115 2116 /** Opaque version of {@link #getShortVolatile(Object, long)} */ 2117 @HotSpotIntrinsicCandidate 2118 public final short getShortOpaque(Object o, long offset) { 2119 return getShortVolatile(o, offset); 2120 } 2121 2122 /** Opaque version of {@link #getCharVolatile(Object, long)} */ 2123 @HotSpotIntrinsicCandidate 2124 public final char getCharOpaque(Object o, long offset) { 2125 return getCharVolatile(o, offset); 2126 } 2127 2128 /** Opaque version of {@link #getIntVolatile(Object, long)} */ 2129 @HotSpotIntrinsicCandidate 2130 public final int getIntOpaque(Object o, long offset) { 2131 return getIntVolatile(o, offset); 2132 } 2133 2134 /** Opaque version of {@link #getFloatVolatile(Object, long)} */ 2135 @HotSpotIntrinsicCandidate 2136 public final float getFloatOpaque(Object o, long offset) { 2137 return getFloatVolatile(o, offset); 2138 } 2139 2140 /** Opaque version of {@link #getLongVolatile(Object, long)} */ 2141 @HotSpotIntrinsicCandidate 2142 public final long getLongOpaque(Object o, long offset) { 2143 return getLongVolatile(o, offset); 2144 } 2145 2146 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */ 2147 @HotSpotIntrinsicCandidate 2148 public final double getDoubleOpaque(Object o, long offset) { 2149 return getDoubleVolatile(o, offset); 2150 } 2151 2152 /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */ 2153 @HotSpotIntrinsicCandidate 2154 public final void putObjectOpaque(Object o, long offset, Object x) { 2155 putObjectVolatile(o, offset, x); 2156 } 2157 2158 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2159 @HotSpotIntrinsicCandidate 2160 public final void putBooleanOpaque(Object o, long offset, boolean x) { 2161 putBooleanVolatile(o, offset, x); 2162 } 2163 2164 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */ 2165 @HotSpotIntrinsicCandidate 2166 public final void putByteOpaque(Object o, long offset, byte x) { 2167 putByteVolatile(o, offset, x); 2168 } 2169 2170 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */ 2171 @HotSpotIntrinsicCandidate 2172 public final void putShortOpaque(Object o, long offset, short x) { 2173 putShortVolatile(o, offset, x); 2174 } 2175 2176 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */ 2177 @HotSpotIntrinsicCandidate 2178 public final void putCharOpaque(Object o, long offset, char x) { 2179 putCharVolatile(o, offset, x); 2180 } 2181 2182 /** Opaque version of {@link #putIntVolatile(Object, long, int)} */ 2183 @HotSpotIntrinsicCandidate 2184 public final void putIntOpaque(Object o, long offset, int x) { 2185 putIntVolatile(o, offset, x); 2186 } 2187 2188 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */ 2189 @HotSpotIntrinsicCandidate 2190 public final void putFloatOpaque(Object o, long offset, float x) { 2191 putFloatVolatile(o, offset, x); 2192 } 2193 2194 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */ 2195 @HotSpotIntrinsicCandidate 2196 public final void putLongOpaque(Object o, long offset, long x) { 2197 putLongVolatile(o, offset, x); 2198 } 2199 2200 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */ 2201 @HotSpotIntrinsicCandidate 2202 public final void putDoubleOpaque(Object o, long offset, double x) { 2203 putDoubleVolatile(o, offset, x); 2204 } 2205 2206 /** 2207 * Unblocks the given thread blocked on {@code park}, or, if it is 2208 * not blocked, causes the subsequent call to {@code park} not to 2209 * block. Note: this operation is "unsafe" solely because the 2210 * caller must somehow ensure that the thread has not been 2211 * destroyed. Nothing special is usually required to ensure this 2212 * when called from Java (in which there will ordinarily be a live 2213 * reference to the thread) but this is not nearly-automatically 2214 * so when calling from native code. 2215 * 2216 * @param thread the thread to unpark. 2217 */ 2218 @HotSpotIntrinsicCandidate 2219 public native void unpark(Object thread); 2220 2221 /** 2222 * Blocks current thread, returning when a balancing 2223 * {@code unpark} occurs, or a balancing {@code unpark} has 2224 * already occurred, or the thread is interrupted, or, if not 2225 * absolute and time is not zero, the given time nanoseconds have 2226 * elapsed, or if absolute, the given deadline in milliseconds 2227 * since Epoch has passed, or spuriously (i.e., returning for no 2228 * "reason"). Note: This operation is in the Unsafe class only 2229 * because {@code unpark} is, so it would be strange to place it 2230 * elsewhere. 2231 */ 2232 @HotSpotIntrinsicCandidate 2233 public native void park(boolean isAbsolute, long time); 2234 2235 /** 2236 * Gets the load average in the system run queue assigned 2237 * to the available processors averaged over various periods of time. 2238 * This method retrieves the given {@code nelem} samples and 2239 * assigns to the elements of the given {@code loadavg} array. 2240 * The system imposes a maximum of 3 samples, representing 2241 * averages over the last 1, 5, and 15 minutes, respectively. 2242 * 2243 * @param loadavg an array of double of size nelems 2244 * @param nelems the number of samples to be retrieved and 2245 * must be 1 to 3. 2246 * 2247 * @return the number of samples actually retrieved; or -1 2248 * if the load average is unobtainable. 2249 */ 2250 public int getLoadAverage(double[] loadavg, int nelems) { 2251 if (nelems < 0 || nelems > 3 || nelems > loadavg.length) { 2252 throw new ArrayIndexOutOfBoundsException(); 2253 } 2254 2255 return getLoadAverage0(loadavg, nelems); 2256 } 2257 2258 // The following contain CAS-based Java implementations used on 2259 // platforms not supporting native instructions 2260 2261 /** 2262 * Atomically adds the given value to the current value of a field 2263 * or array element within the given object {@code o} 2264 * at the given {@code offset}. 2265 * 2266 * @param o object/array to update the field/element in 2267 * @param offset field/element offset 2268 * @param delta the value to add 2269 * @return the previous value 2270 * @since 1.8 2271 */ 2272 @HotSpotIntrinsicCandidate 2273 public final int getAndAddInt(Object o, long offset, int delta) { 2274 int v; 2275 do { 2276 v = getIntVolatile(o, offset); 2277 } while (!weakCompareAndSwapIntVolatile(o, offset, v, v + delta)); 2278 return v; 2279 } 2280 2281 /** 2282 * Atomically adds the given value to the current value of a field 2283 * or array element within the given object {@code o} 2284 * at the given {@code offset}. 2285 * 2286 * @param o object/array to update the field/element in 2287 * @param offset field/element offset 2288 * @param delta the value to add 2289 * @return the previous value 2290 * @since 1.8 2291 */ 2292 @HotSpotIntrinsicCandidate 2293 public final long getAndAddLong(Object o, long offset, long delta) { 2294 long v; 2295 do { 2296 v = getLongVolatile(o, offset); 2297 } while (!weakCompareAndSwapLongVolatile(o, offset, v, v + delta)); 2298 return v; 2299 } 2300 2301 @HotSpotIntrinsicCandidate 2302 public final byte getAndAddByte(Object o, long offset, byte delta) { 2303 byte v; 2304 do { 2305 v = getByteVolatile(o, offset); 2306 } while (!weakCompareAndSwapByteVolatile(o, offset, v, (byte) (v + delta))); 2307 return v; 2308 } 2309 2310 @HotSpotIntrinsicCandidate 2311 public final short getAndAddShort(Object o, long offset, short delta) { 2312 short v; 2313 do { 2314 v = getShortVolatile(o, offset); 2315 } while (!weakCompareAndSwapShortVolatile(o, offset, v, (short) (v + delta))); 2316 return v; 2317 } 2318 2319 @ForceInline 2320 public final char getAndAddChar(Object o, long offset, char delta) { 2321 return (char) getAndAddShort(o, offset, (short) delta); 2322 } 2323 2324 @ForceInline 2325 public final float getAndAddFloat(Object o, long offset, float delta) { 2326 int expectedBits; 2327 float v; 2328 do { 2329 // Load and CAS with the raw bits to avoid issues with NaNs and 2330 // possible bit conversion from signaling NaNs to quiet NaNs that 2331 // may result in the loop not terminating. 2332 expectedBits = getIntVolatile(o, offset); 2333 v = Float.intBitsToFloat(expectedBits); 2334 } while (!weakCompareAndSwapIntVolatile(o, offset, 2335 expectedBits, Float.floatToRawIntBits(v + delta))); 2336 return v; 2337 } 2338 2339 @ForceInline 2340 public final double getAndAddDouble(Object o, long offset, double delta) { 2341 long expectedBits; 2342 double v; 2343 do { 2344 // Load and CAS with the raw bits to avoid issues with NaNs and 2345 // possible bit conversion from signaling NaNs to quiet NaNs that 2346 // may result in the loop not terminating. 2347 expectedBits = getLongVolatile(o, offset); 2348 v = Double.longBitsToDouble(expectedBits); 2349 } while (!weakCompareAndSwapLongVolatile(o, offset, 2350 expectedBits, Double.doubleToRawLongBits(v + delta))); 2351 return v; 2352 } 2353 2354 /** 2355 * Atomically exchanges the given value with the current value of 2356 * a field or array element within the given object {@code o} 2357 * at the given {@code offset}. 2358 * 2359 * @param o object/array to update the field/element in 2360 * @param offset field/element offset 2361 * @param newValue new value 2362 * @return the previous value 2363 * @since 1.8 2364 */ 2365 @HotSpotIntrinsicCandidate 2366 public final int getAndSetInt(Object o, long offset, int newValue) { 2367 int v; 2368 do { 2369 v = getIntVolatile(o, offset); 2370 } while (!weakCompareAndSwapIntVolatile(o, offset, v, newValue)); 2371 return v; 2372 } 2373 2374 /** 2375 * Atomically exchanges the given value with the current value of 2376 * a field or array element within the given object {@code o} 2377 * at the given {@code offset}. 2378 * 2379 * @param o object/array to update the field/element in 2380 * @param offset field/element offset 2381 * @param newValue new value 2382 * @return the previous value 2383 * @since 1.8 2384 */ 2385 @HotSpotIntrinsicCandidate 2386 public final long getAndSetLong(Object o, long offset, long newValue) { 2387 long v; 2388 do { 2389 v = getLongVolatile(o, offset); 2390 } while (!weakCompareAndSwapLongVolatile(o, offset, v, newValue)); 2391 return v; 2392 } 2393 2394 /** 2395 * Atomically exchanges the given reference value with the current 2396 * reference value of a field or array element within the given 2397 * object {@code o} at the given {@code offset}. 2398 * 2399 * @param o object/array to update the field/element in 2400 * @param offset field/element offset 2401 * @param newValue new value 2402 * @return the previous value 2403 * @since 1.8 2404 */ 2405 @HotSpotIntrinsicCandidate 2406 public final Object getAndSetObject(Object o, long offset, Object newValue) { 2407 Object v; 2408 do { 2409 v = getObjectVolatile(o, offset); 2410 } while (!weakCompareAndSwapObjectVolatile(o, offset, v, newValue)); 2411 return v; 2412 } 2413 2414 @HotSpotIntrinsicCandidate 2415 public final byte getAndSetByte(Object o, long offset, byte newValue) { 2416 byte v; 2417 do { 2418 v = getByteVolatile(o, offset); 2419 } while (!weakCompareAndSwapByteVolatile(o, offset, v, newValue)); 2420 return v; 2421 } 2422 2423 @ForceInline 2424 public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) { 2425 return byte2bool(getAndSetByte(o, offset, bool2byte(newValue))); 2426 } 2427 2428 @HotSpotIntrinsicCandidate 2429 public final short getAndSetShort(Object o, long offset, short newValue) { 2430 short v; 2431 do { 2432 v = getShortVolatile(o, offset); 2433 } while (!weakCompareAndSwapShortVolatile(o, offset, v, newValue)); 2434 return v; 2435 } 2436 2437 @ForceInline 2438 public final char getAndSetChar(Object o, long offset, char newValue) { 2439 return s2c(getAndSetShort(o, offset, c2s(newValue))); 2440 } 2441 2442 @ForceInline 2443 public final float getAndSetFloat(Object o, long offset, float newValue) { 2444 int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue)); 2445 return Float.intBitsToFloat(v); 2446 } 2447 2448 @ForceInline 2449 public final double getAndSetDouble(Object o, long offset, double newValue) { 2450 long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue)); 2451 return Double.longBitsToDouble(v); 2452 } 2453 2454 /** 2455 * Ensures that loads before the fence will not be reordered with loads and 2456 * stores after the fence; a "LoadLoad plus LoadStore barrier". 2457 * 2458 * Corresponds to C11 atomic_thread_fence(memory_order_acquire) 2459 * (an "acquire fence"). 2460 * 2461 * A pure LoadLoad fence is not provided, since the addition of LoadStore 2462 * is almost always desired, and most current hardware instructions that 2463 * provide a LoadLoad barrier also provide a LoadStore barrier for free. 2464 * @since 1.8 2465 */ 2466 @HotSpotIntrinsicCandidate 2467 public native void loadFence(); 2468 2469 /** 2470 * Ensures that loads and stores before the fence will not be reordered with 2471 * stores after the fence; a "StoreStore plus LoadStore barrier". 2472 * 2473 * Corresponds to C11 atomic_thread_fence(memory_order_release) 2474 * (a "release fence"). 2475 * 2476 * A pure StoreStore fence is not provided, since the addition of LoadStore 2477 * is almost always desired, and most current hardware instructions that 2478 * provide a StoreStore barrier also provide a LoadStore barrier for free. 2479 * @since 1.8 2480 */ 2481 @HotSpotIntrinsicCandidate 2482 public native void storeFence(); 2483 2484 /** 2485 * Ensures that loads and stores before the fence will not be reordered 2486 * with loads and stores after the fence. Implies the effects of both 2487 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad 2488 * barrier. 2489 * 2490 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst). 2491 * @since 1.8 2492 */ 2493 @HotSpotIntrinsicCandidate 2494 public native void fullFence(); 2495 2496 /** 2497 * Ensures that loads before the fence will not be reordered with 2498 * loads after the fence. 2499 */ 2500 public final void loadLoadFence() { 2501 loadFence(); 2502 } 2503 2504 /** 2505 * Ensures that stores before the fence will not be reordered with 2506 * stores after the fence. 2507 */ 2508 public final void storeStoreFence() { 2509 storeFence(); 2510 } 2511 2512 2513 /** 2514 * Throws IllegalAccessError; for use by the VM for access control 2515 * error support. 2516 * @since 1.8 2517 */ 2518 private static void throwIllegalAccessError() { 2519 throw new IllegalAccessError(); 2520 } 2521 2522 /** 2523 * @return Returns true if the native byte ordering of this 2524 * platform is big-endian, false if it is little-endian. 2525 */ 2526 public final boolean isBigEndian() { return BE; } 2527 2528 /** 2529 * @return Returns true if this platform is capable of performing 2530 * accesses at addresses which are not aligned for the type of the 2531 * primitive type being accessed, false otherwise. 2532 */ 2533 public final boolean unalignedAccess() { return unalignedAccess; } 2534 2535 /** 2536 * Fetches a value at some byte offset into a given Java object. 2537 * More specifically, fetches a value within the given object 2538 * <code>o</code> at the given offset, or (if <code>o</code> is 2539 * null) from the memory address whose numerical value is the 2540 * given offset. <p> 2541 * 2542 * The specification of this method is the same as {@link 2543 * #getLong(Object, long)} except that the offset does not need to 2544 * have been obtained from {@link #objectFieldOffset} on the 2545 * {@link java.lang.reflect.Field} of some Java field. The value 2546 * in memory is raw data, and need not correspond to any Java 2547 * variable. Unless <code>o</code> is null, the value accessed 2548 * must be entirely within the allocated object. The endianness 2549 * of the value in memory is the endianness of the native platform. 2550 * 2551 * <p> The read will be atomic with respect to the largest power 2552 * of two that divides the GCD of the offset and the storage size. 2553 * For example, getLongUnaligned will make atomic reads of 2-, 4-, 2554 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 2555 * respectively. There are no other guarantees of atomicity. 2556 * <p> 2557 * 8-byte atomicity is only guaranteed on platforms on which 2558 * support atomic accesses to longs. 2559 * 2560 * @param o Java heap object in which the value resides, if any, else 2561 * null 2562 * @param offset The offset in bytes from the start of the object 2563 * @return the value fetched from the indicated object 2564 * @throws RuntimeException No defined exceptions are thrown, not even 2565 * {@link NullPointerException} 2566 * @since 9 2567 */ 2568 @HotSpotIntrinsicCandidate 2569 public final long getLongUnaligned(Object o, long offset) { 2570 if ((offset & 7) == 0) { 2571 return getLong(o, offset); 2572 } else if ((offset & 3) == 0) { 2573 return makeLong(getInt(o, offset), 2574 getInt(o, offset + 4)); 2575 } else if ((offset & 1) == 0) { 2576 return makeLong(getShort(o, offset), 2577 getShort(o, offset + 2), 2578 getShort(o, offset + 4), 2579 getShort(o, offset + 6)); 2580 } else { 2581 return makeLong(getByte(o, offset), 2582 getByte(o, offset + 1), 2583 getByte(o, offset + 2), 2584 getByte(o, offset + 3), 2585 getByte(o, offset + 4), 2586 getByte(o, offset + 5), 2587 getByte(o, offset + 6), 2588 getByte(o, offset + 7)); 2589 } 2590 } 2591 /** 2592 * As {@link #getLongUnaligned(Object, long)} but with an 2593 * additional argument which specifies the endianness of the value 2594 * as stored in memory. 2595 * 2596 * @param o Java heap object in which the variable resides 2597 * @param offset The offset in bytes from the start of the object 2598 * @param bigEndian The endianness of the value 2599 * @return the value fetched from the indicated object 2600 * @since 9 2601 */ 2602 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) { 2603 return convEndian(bigEndian, getLongUnaligned(o, offset)); 2604 } 2605 2606 /** @see #getLongUnaligned(Object, long) */ 2607 @HotSpotIntrinsicCandidate 2608 public final int getIntUnaligned(Object o, long offset) { 2609 if ((offset & 3) == 0) { 2610 return getInt(o, offset); 2611 } else if ((offset & 1) == 0) { 2612 return makeInt(getShort(o, offset), 2613 getShort(o, offset + 2)); 2614 } else { 2615 return makeInt(getByte(o, offset), 2616 getByte(o, offset + 1), 2617 getByte(o, offset + 2), 2618 getByte(o, offset + 3)); 2619 } 2620 } 2621 /** @see #getLongUnaligned(Object, long, boolean) */ 2622 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) { 2623 return convEndian(bigEndian, getIntUnaligned(o, offset)); 2624 } 2625 2626 /** @see #getLongUnaligned(Object, long) */ 2627 @HotSpotIntrinsicCandidate 2628 public final short getShortUnaligned(Object o, long offset) { 2629 if ((offset & 1) == 0) { 2630 return getShort(o, offset); 2631 } else { 2632 return makeShort(getByte(o, offset), 2633 getByte(o, offset + 1)); 2634 } 2635 } 2636 /** @see #getLongUnaligned(Object, long, boolean) */ 2637 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) { 2638 return convEndian(bigEndian, getShortUnaligned(o, offset)); 2639 } 2640 2641 /** @see #getLongUnaligned(Object, long) */ 2642 @HotSpotIntrinsicCandidate 2643 public final char getCharUnaligned(Object o, long offset) { 2644 if ((offset & 1) == 0) { 2645 return getChar(o, offset); 2646 } else { 2647 return (char)makeShort(getByte(o, offset), 2648 getByte(o, offset + 1)); 2649 } 2650 } 2651 2652 /** @see #getLongUnaligned(Object, long, boolean) */ 2653 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) { 2654 return convEndian(bigEndian, getCharUnaligned(o, offset)); 2655 } 2656 2657 /** 2658 * Stores a value at some byte offset into a given Java object. 2659 * <p> 2660 * The specification of this method is the same as {@link 2661 * #getLong(Object, long)} except that the offset does not need to 2662 * have been obtained from {@link #objectFieldOffset} on the 2663 * {@link java.lang.reflect.Field} of some Java field. The value 2664 * in memory is raw data, and need not correspond to any Java 2665 * variable. The endianness of the value in memory is the 2666 * endianness of the native platform. 2667 * <p> 2668 * The write will be atomic with respect to the largest power of 2669 * two that divides the GCD of the offset and the storage size. 2670 * For example, putLongUnaligned will make atomic writes of 2-, 4-, 2671 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 2672 * respectively. There are no other guarantees of atomicity. 2673 * <p> 2674 * 8-byte atomicity is only guaranteed on platforms on which 2675 * support atomic accesses to longs. 2676 * 2677 * @param o Java heap object in which the value resides, if any, else 2678 * null 2679 * @param offset The offset in bytes from the start of the object 2680 * @param x the value to store 2681 * @throws RuntimeException No defined exceptions are thrown, not even 2682 * {@link NullPointerException} 2683 * @since 9 2684 */ 2685 @HotSpotIntrinsicCandidate 2686 public final void putLongUnaligned(Object o, long offset, long x) { 2687 if ((offset & 7) == 0) { 2688 putLong(o, offset, x); 2689 } else if ((offset & 3) == 0) { 2690 putLongParts(o, offset, 2691 (int)(x >> 0), 2692 (int)(x >>> 32)); 2693 } else if ((offset & 1) == 0) { 2694 putLongParts(o, offset, 2695 (short)(x >>> 0), 2696 (short)(x >>> 16), 2697 (short)(x >>> 32), 2698 (short)(x >>> 48)); 2699 } else { 2700 putLongParts(o, offset, 2701 (byte)(x >>> 0), 2702 (byte)(x >>> 8), 2703 (byte)(x >>> 16), 2704 (byte)(x >>> 24), 2705 (byte)(x >>> 32), 2706 (byte)(x >>> 40), 2707 (byte)(x >>> 48), 2708 (byte)(x >>> 56)); 2709 } 2710 } 2711 2712 /** 2713 * As {@link #putLongUnaligned(Object, long, long)} but with an additional 2714 * argument which specifies the endianness of the value as stored in memory. 2715 * @param o Java heap object in which the value resides 2716 * @param offset The offset in bytes from the start of the object 2717 * @param x the value to store 2718 * @param bigEndian The endianness of the value 2719 * @throws RuntimeException No defined exceptions are thrown, not even 2720 * {@link NullPointerException} 2721 * @since 9 2722 */ 2723 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) { 2724 putLongUnaligned(o, offset, convEndian(bigEndian, x)); 2725 } 2726 2727 /** @see #putLongUnaligned(Object, long, long) */ 2728 @HotSpotIntrinsicCandidate 2729 public final void putIntUnaligned(Object o, long offset, int x) { 2730 if ((offset & 3) == 0) { 2731 putInt(o, offset, x); 2732 } else if ((offset & 1) == 0) { 2733 putIntParts(o, offset, 2734 (short)(x >> 0), 2735 (short)(x >>> 16)); 2736 } else { 2737 putIntParts(o, offset, 2738 (byte)(x >>> 0), 2739 (byte)(x >>> 8), 2740 (byte)(x >>> 16), 2741 (byte)(x >>> 24)); 2742 } 2743 } 2744 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2745 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) { 2746 putIntUnaligned(o, offset, convEndian(bigEndian, x)); 2747 } 2748 2749 /** @see #putLongUnaligned(Object, long, long) */ 2750 @HotSpotIntrinsicCandidate 2751 public final void putShortUnaligned(Object o, long offset, short x) { 2752 if ((offset & 1) == 0) { 2753 putShort(o, offset, x); 2754 } else { 2755 putShortParts(o, offset, 2756 (byte)(x >>> 0), 2757 (byte)(x >>> 8)); 2758 } 2759 } 2760 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2761 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) { 2762 putShortUnaligned(o, offset, convEndian(bigEndian, x)); 2763 } 2764 2765 /** @see #putLongUnaligned(Object, long, long) */ 2766 @HotSpotIntrinsicCandidate 2767 public final void putCharUnaligned(Object o, long offset, char x) { 2768 putShortUnaligned(o, offset, (short)x); 2769 } 2770 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2771 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) { 2772 putCharUnaligned(o, offset, convEndian(bigEndian, x)); 2773 } 2774 2775 // JVM interface methods 2776 // BE is true iff the native endianness of this platform is big. 2777 private static final boolean BE = theUnsafe.isBigEndian0(); 2778 2779 // unalignedAccess is true iff this platform can perform unaligned accesses. 2780 private static final boolean unalignedAccess = theUnsafe.unalignedAccess0(); 2781 2782 private static int pickPos(int top, int pos) { return BE ? top - pos : pos; } 2783 2784 // These methods construct integers from bytes. The byte ordering 2785 // is the native endianness of this platform. 2786 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 2787 return ((toUnsignedLong(i0) << pickPos(56, 0)) 2788 | (toUnsignedLong(i1) << pickPos(56, 8)) 2789 | (toUnsignedLong(i2) << pickPos(56, 16)) 2790 | (toUnsignedLong(i3) << pickPos(56, 24)) 2791 | (toUnsignedLong(i4) << pickPos(56, 32)) 2792 | (toUnsignedLong(i5) << pickPos(56, 40)) 2793 | (toUnsignedLong(i6) << pickPos(56, 48)) 2794 | (toUnsignedLong(i7) << pickPos(56, 56))); 2795 } 2796 private static long makeLong(short i0, short i1, short i2, short i3) { 2797 return ((toUnsignedLong(i0) << pickPos(48, 0)) 2798 | (toUnsignedLong(i1) << pickPos(48, 16)) 2799 | (toUnsignedLong(i2) << pickPos(48, 32)) 2800 | (toUnsignedLong(i3) << pickPos(48, 48))); 2801 } 2802 private static long makeLong(int i0, int i1) { 2803 return (toUnsignedLong(i0) << pickPos(32, 0)) 2804 | (toUnsignedLong(i1) << pickPos(32, 32)); 2805 } 2806 private static int makeInt(short i0, short i1) { 2807 return (toUnsignedInt(i0) << pickPos(16, 0)) 2808 | (toUnsignedInt(i1) << pickPos(16, 16)); 2809 } 2810 private static int makeInt(byte i0, byte i1, byte i2, byte i3) { 2811 return ((toUnsignedInt(i0) << pickPos(24, 0)) 2812 | (toUnsignedInt(i1) << pickPos(24, 8)) 2813 | (toUnsignedInt(i2) << pickPos(24, 16)) 2814 | (toUnsignedInt(i3) << pickPos(24, 24))); 2815 } 2816 private static short makeShort(byte i0, byte i1) { 2817 return (short)((toUnsignedInt(i0) << pickPos(8, 0)) 2818 | (toUnsignedInt(i1) << pickPos(8, 8))); 2819 } 2820 2821 private static byte pick(byte le, byte be) { return BE ? be : le; } 2822 private static short pick(short le, short be) { return BE ? be : le; } 2823 private static int pick(int le, int be) { return BE ? be : le; } 2824 2825 // These methods write integers to memory from smaller parts 2826 // provided by their caller. The ordering in which these parts 2827 // are written is the native endianness of this platform. 2828 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 2829 putByte(o, offset + 0, pick(i0, i7)); 2830 putByte(o, offset + 1, pick(i1, i6)); 2831 putByte(o, offset + 2, pick(i2, i5)); 2832 putByte(o, offset + 3, pick(i3, i4)); 2833 putByte(o, offset + 4, pick(i4, i3)); 2834 putByte(o, offset + 5, pick(i5, i2)); 2835 putByte(o, offset + 6, pick(i6, i1)); 2836 putByte(o, offset + 7, pick(i7, i0)); 2837 } 2838 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) { 2839 putShort(o, offset + 0, pick(i0, i3)); 2840 putShort(o, offset + 2, pick(i1, i2)); 2841 putShort(o, offset + 4, pick(i2, i1)); 2842 putShort(o, offset + 6, pick(i3, i0)); 2843 } 2844 private void putLongParts(Object o, long offset, int i0, int i1) { 2845 putInt(o, offset + 0, pick(i0, i1)); 2846 putInt(o, offset + 4, pick(i1, i0)); 2847 } 2848 private void putIntParts(Object o, long offset, short i0, short i1) { 2849 putShort(o, offset + 0, pick(i0, i1)); 2850 putShort(o, offset + 2, pick(i1, i0)); 2851 } 2852 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) { 2853 putByte(o, offset + 0, pick(i0, i3)); 2854 putByte(o, offset + 1, pick(i1, i2)); 2855 putByte(o, offset + 2, pick(i2, i1)); 2856 putByte(o, offset + 3, pick(i3, i0)); 2857 } 2858 private void putShortParts(Object o, long offset, byte i0, byte i1) { 2859 putByte(o, offset + 0, pick(i0, i1)); 2860 putByte(o, offset + 1, pick(i1, i0)); 2861 } 2862 2863 // Zero-extend an integer 2864 private static int toUnsignedInt(byte n) { return n & 0xff; } 2865 private static int toUnsignedInt(short n) { return n & 0xffff; } 2866 private static long toUnsignedLong(byte n) { return n & 0xffl; } 2867 private static long toUnsignedLong(short n) { return n & 0xffffl; } 2868 private static long toUnsignedLong(int n) { return n & 0xffffffffl; } 2869 2870 // Maybe byte-reverse an integer 2871 private static char convEndian(boolean big, char n) { return big == BE ? n : Character.reverseBytes(n); } 2872 private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n) ; } 2873 private static int convEndian(boolean big, int n) { return big == BE ? n : Integer.reverseBytes(n) ; } 2874 private static long convEndian(boolean big, long n) { return big == BE ? n : Long.reverseBytes(n) ; } 2875 2876 2877 2878 private native long allocateMemory0(long bytes); 2879 private native long reallocateMemory0(long address, long bytes); 2880 private native void freeMemory0(long address); 2881 private native void setMemory0(Object o, long offset, long bytes, byte value); 2882 @HotSpotIntrinsicCandidate 2883 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes); 2884 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize); 2885 private native long objectFieldOffset0(Field f); 2886 private native long staticFieldOffset0(Field f); 2887 private native Object staticFieldBase0(Field f); 2888 private native boolean shouldBeInitialized0(Class<?> c); 2889 private native void ensureClassInitialized0(Class<?> c); 2890 private native int arrayBaseOffset0(Class<?> arrayClass); 2891 private native int arrayIndexScale0(Class<?> arrayClass); 2892 private native int addressSize0(); 2893 private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches); 2894 private native int getLoadAverage0(double[] loadavg, int nelems); 2895 private native boolean unalignedAccess0(); 2896 private native boolean isBigEndian0(); 2897 }