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 /** 1604 * The JVM converts ints to booleans using two different 1605 * conventions, byte testing against zero and truncation to 1606 * least-significant bit. 1607 * 1608 * <p>The JNI documents specify that, at least for returning 1609 * values from native methods, a Java boolean value is converted 1610 * to the value-set 0..1 by first truncating to a byte (0..255 or 1611 * maybe -128..127) and then testing against zero. Thus, Java 1612 * booleans in non-Java data structures are by convention 1613 * represented as 8-bit containers containing either zero (for 1614 * false) or any non-zero value (for true). 1615 * 1616 * <p>Java booleans in the heap are also stored in bytes, but are 1617 * strongly normalized to the value-set 0..1 (i.e., they are 1618 * truncated to the least-significant bit). 1619 * 1620 * <p>The main reason for having different conventions for 1621 * conversion is performance: Truncation to the least-significant 1622 * bit can be usually implemented with fewer (machine) 1623 * instructions than byte testing against zero. 1624 * 1625 * <p>A number of Unsafe methods load boolean values from the heap 1626 * as bytes. Unsafe converts those values according to the JNI 1627 * rules (i.e, using the "testing against zero" convention). The 1628 * method {@code byte2bool} implements that conversion. 1629 * 1630 * @param b the byte to be converted to boolean 1631 * @return the result of the conversion 1632 */ 1633 @ForceInline 1634 private boolean byte2bool(byte b) { 1635 return b != 0; 1636 } 1637 1638 /** 1639 * Convert a boolean value to a byte. The return value is strongly 1640 * normalized to the value-set 0..1 (i.e., the value is truncated 1641 * to the least-significant bit). See {@link #byte2bool(byte)} for 1642 * more details on conversion conventions. 1643 * 1644 * @param b the boolean to be converted to byte (and then normalized) 1645 * @return the result of the conversion 1646 */ 1647 @ForceInline 1648 private byte bool2byte(boolean b) { 1649 return b ? (byte)1 : (byte)0; 1650 } 1651 1652 @ForceInline 1653 public final boolean compareAndSwapBoolean(Object o, long offset, 1654 boolean expected, 1655 boolean x) { 1656 return compareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x)); 1657 } 1658 1659 @ForceInline 1660 public final boolean compareAndExchangeBooleanVolatile(Object o, long offset, 1661 boolean expected, 1662 boolean x) { 1663 return byte2bool(compareAndExchangeByteVolatile(o, offset, bool2byte(expected), bool2byte(x))); 1664 } 1665 1666 @ForceInline 1667 public final boolean compareAndExchangeBooleanAcquire(Object o, long offset, 1668 boolean expected, 1669 boolean x) { 1670 return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x))); 1671 } 1672 1673 @ForceInline 1674 public final boolean compareAndExchangeBooleanRelease(Object o, long offset, 1675 boolean expected, 1676 boolean x) { 1677 return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x))); 1678 } 1679 1680 @ForceInline 1681 public final boolean weakCompareAndSwapBooleanVolatile(Object o, long offset, 1682 boolean expected, 1683 boolean x) { 1684 return weakCompareAndSwapByteVolatile(o, offset, bool2byte(expected), bool2byte(x)); 1685 } 1686 1687 @ForceInline 1688 public final boolean weakCompareAndSwapBooleanAcquire(Object o, long offset, 1689 boolean expected, 1690 boolean x) { 1691 return weakCompareAndSwapByteAcquire(o, offset, bool2byte(expected), bool2byte(x)); 1692 } 1693 1694 @ForceInline 1695 public final boolean weakCompareAndSwapBooleanRelease(Object o, long offset, 1696 boolean expected, 1697 boolean x) { 1698 return weakCompareAndSwapByteRelease(o, offset, bool2byte(expected), bool2byte(x)); 1699 } 1700 1701 @ForceInline 1702 public final boolean weakCompareAndSwapBoolean(Object o, long offset, 1703 boolean expected, 1704 boolean x) { 1705 return weakCompareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x)); 1706 } 1707 1708 /** 1709 * Atomically updates Java variable to {@code x} if it is currently 1710 * holding {@code expected}. 1711 * 1712 * <p>This operation has memory semantics of a {@code volatile} read 1713 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1714 * 1715 * @return {@code true} if successful 1716 */ 1717 @ForceInline 1718 public final boolean compareAndSwapFloat(Object o, long offset, 1719 float expected, 1720 float x) { 1721 return compareAndSwapInt(o, offset, 1722 Float.floatToRawIntBits(expected), 1723 Float.floatToRawIntBits(x)); 1724 } 1725 1726 @ForceInline 1727 public final float compareAndExchangeFloatVolatile(Object o, long offset, 1728 float expected, 1729 float x) { 1730 int w = compareAndExchangeIntVolatile(o, offset, 1731 Float.floatToRawIntBits(expected), 1732 Float.floatToRawIntBits(x)); 1733 return Float.intBitsToFloat(w); 1734 } 1735 1736 @ForceInline 1737 public final float compareAndExchangeFloatAcquire(Object o, long offset, 1738 float expected, 1739 float x) { 1740 int w = compareAndExchangeIntAcquire(o, offset, 1741 Float.floatToRawIntBits(expected), 1742 Float.floatToRawIntBits(x)); 1743 return Float.intBitsToFloat(w); 1744 } 1745 1746 @ForceInline 1747 public final float compareAndExchangeFloatRelease(Object o, long offset, 1748 float expected, 1749 float x) { 1750 int w = compareAndExchangeIntRelease(o, offset, 1751 Float.floatToRawIntBits(expected), 1752 Float.floatToRawIntBits(x)); 1753 return Float.intBitsToFloat(w); 1754 } 1755 1756 @ForceInline 1757 public final boolean weakCompareAndSwapFloat(Object o, long offset, 1758 float expected, 1759 float x) { 1760 return weakCompareAndSwapInt(o, offset, 1761 Float.floatToRawIntBits(expected), 1762 Float.floatToRawIntBits(x)); 1763 } 1764 1765 @ForceInline 1766 public final boolean weakCompareAndSwapFloatAcquire(Object o, long offset, 1767 float expected, 1768 float x) { 1769 return weakCompareAndSwapIntAcquire(o, offset, 1770 Float.floatToRawIntBits(expected), 1771 Float.floatToRawIntBits(x)); 1772 } 1773 1774 @ForceInline 1775 public final boolean weakCompareAndSwapFloatRelease(Object o, long offset, 1776 float expected, 1777 float x) { 1778 return weakCompareAndSwapIntRelease(o, offset, 1779 Float.floatToRawIntBits(expected), 1780 Float.floatToRawIntBits(x)); 1781 } 1782 1783 @ForceInline 1784 public final boolean weakCompareAndSwapFloatVolatile(Object o, long offset, 1785 float expected, 1786 float x) { 1787 return weakCompareAndSwapIntVolatile(o, offset, 1788 Float.floatToRawIntBits(expected), 1789 Float.floatToRawIntBits(x)); 1790 } 1791 1792 /** 1793 * Atomically updates Java variable to {@code x} if it is currently 1794 * holding {@code expected}. 1795 * 1796 * <p>This operation has memory semantics of a {@code volatile} read 1797 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1798 * 1799 * @return {@code true} if successful 1800 */ 1801 @ForceInline 1802 public final boolean compareAndSwapDouble(Object o, long offset, 1803 double expected, 1804 double x) { 1805 return compareAndSwapLong(o, offset, 1806 Double.doubleToRawLongBits(expected), 1807 Double.doubleToRawLongBits(x)); 1808 } 1809 1810 @ForceInline 1811 public final double compareAndExchangeDoubleVolatile(Object o, long offset, 1812 double expected, 1813 double x) { 1814 long w = compareAndExchangeLongVolatile(o, offset, 1815 Double.doubleToRawLongBits(expected), 1816 Double.doubleToRawLongBits(x)); 1817 return Double.longBitsToDouble(w); 1818 } 1819 1820 @ForceInline 1821 public final double compareAndExchangeDoubleAcquire(Object o, long offset, 1822 double expected, 1823 double x) { 1824 long w = compareAndExchangeLongAcquire(o, offset, 1825 Double.doubleToRawLongBits(expected), 1826 Double.doubleToRawLongBits(x)); 1827 return Double.longBitsToDouble(w); 1828 } 1829 1830 @ForceInline 1831 public final double compareAndExchangeDoubleRelease(Object o, long offset, 1832 double expected, 1833 double x) { 1834 long w = compareAndExchangeLongRelease(o, offset, 1835 Double.doubleToRawLongBits(expected), 1836 Double.doubleToRawLongBits(x)); 1837 return Double.longBitsToDouble(w); 1838 } 1839 1840 @ForceInline 1841 public final boolean weakCompareAndSwapDouble(Object o, long offset, 1842 double expected, 1843 double x) { 1844 return weakCompareAndSwapLong(o, offset, 1845 Double.doubleToRawLongBits(expected), 1846 Double.doubleToRawLongBits(x)); 1847 } 1848 1849 @ForceInline 1850 public final boolean weakCompareAndSwapDoubleAcquire(Object o, long offset, 1851 double expected, 1852 double x) { 1853 return weakCompareAndSwapLongAcquire(o, offset, 1854 Double.doubleToRawLongBits(expected), 1855 Double.doubleToRawLongBits(x)); 1856 } 1857 1858 @ForceInline 1859 public final boolean weakCompareAndSwapDoubleRelease(Object o, long offset, 1860 double expected, 1861 double x) { 1862 return weakCompareAndSwapLongRelease(o, offset, 1863 Double.doubleToRawLongBits(expected), 1864 Double.doubleToRawLongBits(x)); 1865 } 1866 1867 @ForceInline 1868 public final boolean weakCompareAndSwapDoubleVolatile(Object o, long offset, 1869 double expected, 1870 double x) { 1871 return weakCompareAndSwapLongVolatile(o, offset, 1872 Double.doubleToRawLongBits(expected), 1873 Double.doubleToRawLongBits(x)); 1874 } 1875 1876 /** 1877 * Atomically updates Java variable to {@code x} if it is currently 1878 * holding {@code expected}. 1879 * 1880 * <p>This operation has memory semantics of a {@code volatile} read 1881 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1882 * 1883 * @return {@code true} if successful 1884 */ 1885 @HotSpotIntrinsicCandidate 1886 public final native boolean compareAndSwapLong(Object o, long offset, 1887 long expected, 1888 long x); 1889 1890 @HotSpotIntrinsicCandidate 1891 public final native long compareAndExchangeLongVolatile(Object o, long offset, 1892 long expected, 1893 long x); 1894 1895 @HotSpotIntrinsicCandidate 1896 public final long compareAndExchangeLongAcquire(Object o, long offset, 1897 long expected, 1898 long x) { 1899 return compareAndExchangeLongVolatile(o, offset, expected, x); 1900 } 1901 1902 @HotSpotIntrinsicCandidate 1903 public final long compareAndExchangeLongRelease(Object o, long offset, 1904 long expected, 1905 long x) { 1906 return compareAndExchangeLongVolatile(o, offset, expected, x); 1907 } 1908 1909 @HotSpotIntrinsicCandidate 1910 public final boolean weakCompareAndSwapLong(Object o, long offset, 1911 long expected, 1912 long x) { 1913 return compareAndSwapLong(o, offset, expected, x); 1914 } 1915 1916 @HotSpotIntrinsicCandidate 1917 public final boolean weakCompareAndSwapLongAcquire(Object o, long offset, 1918 long expected, 1919 long x) { 1920 return compareAndSwapLong(o, offset, expected, x); 1921 } 1922 1923 @HotSpotIntrinsicCandidate 1924 public final boolean weakCompareAndSwapLongRelease(Object o, long offset, 1925 long expected, 1926 long x) { 1927 return compareAndSwapLong(o, offset, expected, x); 1928 } 1929 1930 @HotSpotIntrinsicCandidate 1931 public final boolean weakCompareAndSwapLongVolatile(Object o, long offset, 1932 long expected, 1933 long x) { 1934 return compareAndSwapLong(o, offset, expected, x); 1935 } 1936 1937 /** 1938 * Fetches a reference value from a given Java variable, with volatile 1939 * load semantics. Otherwise identical to {@link #getObject(Object, long)} 1940 */ 1941 @HotSpotIntrinsicCandidate 1942 public native Object getObjectVolatile(Object o, long offset); 1943 1944 /** 1945 * Stores a reference value into a given Java variable, with 1946 * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)} 1947 */ 1948 @HotSpotIntrinsicCandidate 1949 public native void putObjectVolatile(Object o, long offset, Object x); 1950 1951 /** Volatile version of {@link #getInt(Object, long)} */ 1952 @HotSpotIntrinsicCandidate 1953 public native int getIntVolatile(Object o, long offset); 1954 1955 /** Volatile version of {@link #putInt(Object, long, int)} */ 1956 @HotSpotIntrinsicCandidate 1957 public native void putIntVolatile(Object o, long offset, int x); 1958 1959 /** Volatile version of {@link #getBoolean(Object, long)} */ 1960 @HotSpotIntrinsicCandidate 1961 public native boolean getBooleanVolatile(Object o, long offset); 1962 1963 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */ 1964 @HotSpotIntrinsicCandidate 1965 public native void putBooleanVolatile(Object o, long offset, boolean x); 1966 1967 /** Volatile version of {@link #getByte(Object, long)} */ 1968 @HotSpotIntrinsicCandidate 1969 public native byte getByteVolatile(Object o, long offset); 1970 1971 /** Volatile version of {@link #putByte(Object, long, byte)} */ 1972 @HotSpotIntrinsicCandidate 1973 public native void putByteVolatile(Object o, long offset, byte x); 1974 1975 /** Volatile version of {@link #getShort(Object, long)} */ 1976 @HotSpotIntrinsicCandidate 1977 public native short getShortVolatile(Object o, long offset); 1978 1979 /** Volatile version of {@link #putShort(Object, long, short)} */ 1980 @HotSpotIntrinsicCandidate 1981 public native void putShortVolatile(Object o, long offset, short x); 1982 1983 /** Volatile version of {@link #getChar(Object, long)} */ 1984 @HotSpotIntrinsicCandidate 1985 public native char getCharVolatile(Object o, long offset); 1986 1987 /** Volatile version of {@link #putChar(Object, long, char)} */ 1988 @HotSpotIntrinsicCandidate 1989 public native void putCharVolatile(Object o, long offset, char x); 1990 1991 /** Volatile version of {@link #getLong(Object, long)} */ 1992 @HotSpotIntrinsicCandidate 1993 public native long getLongVolatile(Object o, long offset); 1994 1995 /** Volatile version of {@link #putLong(Object, long, long)} */ 1996 @HotSpotIntrinsicCandidate 1997 public native void putLongVolatile(Object o, long offset, long x); 1998 1999 /** Volatile version of {@link #getFloat(Object, long)} */ 2000 @HotSpotIntrinsicCandidate 2001 public native float getFloatVolatile(Object o, long offset); 2002 2003 /** Volatile version of {@link #putFloat(Object, long, float)} */ 2004 @HotSpotIntrinsicCandidate 2005 public native void putFloatVolatile(Object o, long offset, float x); 2006 2007 /** Volatile version of {@link #getDouble(Object, long)} */ 2008 @HotSpotIntrinsicCandidate 2009 public native double getDoubleVolatile(Object o, long offset); 2010 2011 /** Volatile version of {@link #putDouble(Object, long, double)} */ 2012 @HotSpotIntrinsicCandidate 2013 public native void putDoubleVolatile(Object o, long offset, double x); 2014 2015 2016 2017 /** Acquire version of {@link #getObjectVolatile(Object, long)} */ 2018 @HotSpotIntrinsicCandidate 2019 public final Object getObjectAcquire(Object o, long offset) { 2020 return getObjectVolatile(o, offset); 2021 } 2022 2023 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */ 2024 @HotSpotIntrinsicCandidate 2025 public final boolean getBooleanAcquire(Object o, long offset) { 2026 return getBooleanVolatile(o, offset); 2027 } 2028 2029 /** Acquire version of {@link #getByteVolatile(Object, long)} */ 2030 @HotSpotIntrinsicCandidate 2031 public final byte getByteAcquire(Object o, long offset) { 2032 return getByteVolatile(o, offset); 2033 } 2034 2035 /** Acquire version of {@link #getShortVolatile(Object, long)} */ 2036 @HotSpotIntrinsicCandidate 2037 public final short getShortAcquire(Object o, long offset) { 2038 return getShortVolatile(o, offset); 2039 } 2040 2041 /** Acquire version of {@link #getCharVolatile(Object, long)} */ 2042 @HotSpotIntrinsicCandidate 2043 public final char getCharAcquire(Object o, long offset) { 2044 return getCharVolatile(o, offset); 2045 } 2046 2047 /** Acquire version of {@link #getIntVolatile(Object, long)} */ 2048 @HotSpotIntrinsicCandidate 2049 public final int getIntAcquire(Object o, long offset) { 2050 return getIntVolatile(o, offset); 2051 } 2052 2053 /** Acquire version of {@link #getFloatVolatile(Object, long)} */ 2054 @HotSpotIntrinsicCandidate 2055 public final float getFloatAcquire(Object o, long offset) { 2056 return getFloatVolatile(o, offset); 2057 } 2058 2059 /** Acquire version of {@link #getLongVolatile(Object, long)} */ 2060 @HotSpotIntrinsicCandidate 2061 public final long getLongAcquire(Object o, long offset) { 2062 return getLongVolatile(o, offset); 2063 } 2064 2065 /** Acquire version of {@link #getDoubleVolatile(Object, long)} */ 2066 @HotSpotIntrinsicCandidate 2067 public final double getDoubleAcquire(Object o, long offset) { 2068 return getDoubleVolatile(o, offset); 2069 } 2070 2071 /* 2072 * Versions of {@link #putObjectVolatile(Object, long, Object)} 2073 * that do not guarantee immediate visibility of the store to 2074 * other threads. This method is generally only useful if the 2075 * underlying field is a Java volatile (or if an array cell, one 2076 * that is otherwise only accessed using volatile accesses). 2077 * 2078 * Corresponds to C11 atomic_store_explicit(..., memory_order_release). 2079 */ 2080 2081 /** Release version of {@link #putObjectVolatile(Object, long, Object)} */ 2082 @HotSpotIntrinsicCandidate 2083 public final void putObjectRelease(Object o, long offset, Object x) { 2084 putObjectVolatile(o, offset, x); 2085 } 2086 2087 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2088 @HotSpotIntrinsicCandidate 2089 public final void putBooleanRelease(Object o, long offset, boolean x) { 2090 putBooleanVolatile(o, offset, x); 2091 } 2092 2093 /** Release version of {@link #putByteVolatile(Object, long, byte)} */ 2094 @HotSpotIntrinsicCandidate 2095 public final void putByteRelease(Object o, long offset, byte x) { 2096 putByteVolatile(o, offset, x); 2097 } 2098 2099 /** Release version of {@link #putShortVolatile(Object, long, short)} */ 2100 @HotSpotIntrinsicCandidate 2101 public final void putShortRelease(Object o, long offset, short x) { 2102 putShortVolatile(o, offset, x); 2103 } 2104 2105 /** Release version of {@link #putCharVolatile(Object, long, char)} */ 2106 @HotSpotIntrinsicCandidate 2107 public final void putCharRelease(Object o, long offset, char x) { 2108 putCharVolatile(o, offset, x); 2109 } 2110 2111 /** Release version of {@link #putIntVolatile(Object, long, int)} */ 2112 @HotSpotIntrinsicCandidate 2113 public final void putIntRelease(Object o, long offset, int x) { 2114 putIntVolatile(o, offset, x); 2115 } 2116 2117 /** Release version of {@link #putFloatVolatile(Object, long, float)} */ 2118 @HotSpotIntrinsicCandidate 2119 public final void putFloatRelease(Object o, long offset, float x) { 2120 putFloatVolatile(o, offset, x); 2121 } 2122 2123 /** Release version of {@link #putLongVolatile(Object, long, long)} */ 2124 @HotSpotIntrinsicCandidate 2125 public final void putLongRelease(Object o, long offset, long x) { 2126 putLongVolatile(o, offset, x); 2127 } 2128 2129 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */ 2130 @HotSpotIntrinsicCandidate 2131 public final void putDoubleRelease(Object o, long offset, double x) { 2132 putDoubleVolatile(o, offset, x); 2133 } 2134 2135 // ------------------------------ Opaque -------------------------------------- 2136 2137 /** Opaque version of {@link #getObjectVolatile(Object, long)} */ 2138 @HotSpotIntrinsicCandidate 2139 public final Object getObjectOpaque(Object o, long offset) { 2140 return getObjectVolatile(o, offset); 2141 } 2142 2143 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */ 2144 @HotSpotIntrinsicCandidate 2145 public final boolean getBooleanOpaque(Object o, long offset) { 2146 return getBooleanVolatile(o, offset); 2147 } 2148 2149 /** Opaque version of {@link #getByteVolatile(Object, long)} */ 2150 @HotSpotIntrinsicCandidate 2151 public final byte getByteOpaque(Object o, long offset) { 2152 return getByteVolatile(o, offset); 2153 } 2154 2155 /** Opaque version of {@link #getShortVolatile(Object, long)} */ 2156 @HotSpotIntrinsicCandidate 2157 public final short getShortOpaque(Object o, long offset) { 2158 return getShortVolatile(o, offset); 2159 } 2160 2161 /** Opaque version of {@link #getCharVolatile(Object, long)} */ 2162 @HotSpotIntrinsicCandidate 2163 public final char getCharOpaque(Object o, long offset) { 2164 return getCharVolatile(o, offset); 2165 } 2166 2167 /** Opaque version of {@link #getIntVolatile(Object, long)} */ 2168 @HotSpotIntrinsicCandidate 2169 public final int getIntOpaque(Object o, long offset) { 2170 return getIntVolatile(o, offset); 2171 } 2172 2173 /** Opaque version of {@link #getFloatVolatile(Object, long)} */ 2174 @HotSpotIntrinsicCandidate 2175 public final float getFloatOpaque(Object o, long offset) { 2176 return getFloatVolatile(o, offset); 2177 } 2178 2179 /** Opaque version of {@link #getLongVolatile(Object, long)} */ 2180 @HotSpotIntrinsicCandidate 2181 public final long getLongOpaque(Object o, long offset) { 2182 return getLongVolatile(o, offset); 2183 } 2184 2185 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */ 2186 @HotSpotIntrinsicCandidate 2187 public final double getDoubleOpaque(Object o, long offset) { 2188 return getDoubleVolatile(o, offset); 2189 } 2190 2191 /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */ 2192 @HotSpotIntrinsicCandidate 2193 public final void putObjectOpaque(Object o, long offset, Object x) { 2194 putObjectVolatile(o, offset, x); 2195 } 2196 2197 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2198 @HotSpotIntrinsicCandidate 2199 public final void putBooleanOpaque(Object o, long offset, boolean x) { 2200 putBooleanVolatile(o, offset, x); 2201 } 2202 2203 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */ 2204 @HotSpotIntrinsicCandidate 2205 public final void putByteOpaque(Object o, long offset, byte x) { 2206 putByteVolatile(o, offset, x); 2207 } 2208 2209 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */ 2210 @HotSpotIntrinsicCandidate 2211 public final void putShortOpaque(Object o, long offset, short x) { 2212 putShortVolatile(o, offset, x); 2213 } 2214 2215 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */ 2216 @HotSpotIntrinsicCandidate 2217 public final void putCharOpaque(Object o, long offset, char x) { 2218 putCharVolatile(o, offset, x); 2219 } 2220 2221 /** Opaque version of {@link #putIntVolatile(Object, long, int)} */ 2222 @HotSpotIntrinsicCandidate 2223 public final void putIntOpaque(Object o, long offset, int x) { 2224 putIntVolatile(o, offset, x); 2225 } 2226 2227 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */ 2228 @HotSpotIntrinsicCandidate 2229 public final void putFloatOpaque(Object o, long offset, float x) { 2230 putFloatVolatile(o, offset, x); 2231 } 2232 2233 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */ 2234 @HotSpotIntrinsicCandidate 2235 public final void putLongOpaque(Object o, long offset, long x) { 2236 putLongVolatile(o, offset, x); 2237 } 2238 2239 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */ 2240 @HotSpotIntrinsicCandidate 2241 public final void putDoubleOpaque(Object o, long offset, double x) { 2242 putDoubleVolatile(o, offset, x); 2243 } 2244 2245 /** 2246 * Unblocks the given thread blocked on {@code park}, or, if it is 2247 * not blocked, causes the subsequent call to {@code park} not to 2248 * block. Note: this operation is "unsafe" solely because the 2249 * caller must somehow ensure that the thread has not been 2250 * destroyed. Nothing special is usually required to ensure this 2251 * when called from Java (in which there will ordinarily be a live 2252 * reference to the thread) but this is not nearly-automatically 2253 * so when calling from native code. 2254 * 2255 * @param thread the thread to unpark. 2256 */ 2257 @HotSpotIntrinsicCandidate 2258 public native void unpark(Object thread); 2259 2260 /** 2261 * Blocks current thread, returning when a balancing 2262 * {@code unpark} occurs, or a balancing {@code unpark} has 2263 * already occurred, or the thread is interrupted, or, if not 2264 * absolute and time is not zero, the given time nanoseconds have 2265 * elapsed, or if absolute, the given deadline in milliseconds 2266 * since Epoch has passed, or spuriously (i.e., returning for no 2267 * "reason"). Note: This operation is in the Unsafe class only 2268 * because {@code unpark} is, so it would be strange to place it 2269 * elsewhere. 2270 */ 2271 @HotSpotIntrinsicCandidate 2272 public native void park(boolean isAbsolute, long time); 2273 2274 /** 2275 * Gets the load average in the system run queue assigned 2276 * to the available processors averaged over various periods of time. 2277 * This method retrieves the given {@code nelem} samples and 2278 * assigns to the elements of the given {@code loadavg} array. 2279 * The system imposes a maximum of 3 samples, representing 2280 * averages over the last 1, 5, and 15 minutes, respectively. 2281 * 2282 * @param loadavg an array of double of size nelems 2283 * @param nelems the number of samples to be retrieved and 2284 * must be 1 to 3. 2285 * 2286 * @return the number of samples actually retrieved; or -1 2287 * if the load average is unobtainable. 2288 */ 2289 public int getLoadAverage(double[] loadavg, int nelems) { 2290 if (nelems < 0 || nelems > 3 || nelems > loadavg.length) { 2291 throw new ArrayIndexOutOfBoundsException(); 2292 } 2293 2294 return getLoadAverage0(loadavg, nelems); 2295 } 2296 2297 // The following contain CAS-based Java implementations used on 2298 // platforms not supporting native instructions 2299 2300 /** 2301 * Atomically adds the given value to the current value of a field 2302 * or array element within the given object {@code o} 2303 * at the given {@code offset}. 2304 * 2305 * @param o object/array to update the field/element in 2306 * @param offset field/element offset 2307 * @param delta the value to add 2308 * @return the previous value 2309 * @since 1.8 2310 */ 2311 @HotSpotIntrinsicCandidate 2312 public final int getAndAddInt(Object o, long offset, int delta) { 2313 int v; 2314 do { 2315 v = getIntVolatile(o, offset); 2316 } while (!weakCompareAndSwapIntVolatile(o, offset, v, v + delta)); 2317 return v; 2318 } 2319 2320 /** 2321 * Atomically adds the given value to the current value of a field 2322 * or array element within the given object {@code o} 2323 * at the given {@code offset}. 2324 * 2325 * @param o object/array to update the field/element in 2326 * @param offset field/element offset 2327 * @param delta the value to add 2328 * @return the previous value 2329 * @since 1.8 2330 */ 2331 @HotSpotIntrinsicCandidate 2332 public final long getAndAddLong(Object o, long offset, long delta) { 2333 long v; 2334 do { 2335 v = getLongVolatile(o, offset); 2336 } while (!weakCompareAndSwapLongVolatile(o, offset, v, v + delta)); 2337 return v; 2338 } 2339 2340 @HotSpotIntrinsicCandidate 2341 public final byte getAndAddByte(Object o, long offset, byte delta) { 2342 byte v; 2343 do { 2344 v = getByteVolatile(o, offset); 2345 } while (!weakCompareAndSwapByteVolatile(o, offset, v, (byte) (v + delta))); 2346 return v; 2347 } 2348 2349 @HotSpotIntrinsicCandidate 2350 public final short getAndAddShort(Object o, long offset, short delta) { 2351 short v; 2352 do { 2353 v = getShortVolatile(o, offset); 2354 } while (!weakCompareAndSwapShortVolatile(o, offset, v, (short) (v + delta))); 2355 return v; 2356 } 2357 2358 @ForceInline 2359 public final char getAndAddChar(Object o, long offset, char delta) { 2360 return (char) getAndAddShort(o, offset, (short) delta); 2361 } 2362 2363 @ForceInline 2364 public final float getAndAddFloat(Object o, long offset, float delta) { 2365 int expectedBits; 2366 float v; 2367 do { 2368 // Load and CAS with the raw bits to avoid issues with NaNs and 2369 // possible bit conversion from signaling NaNs to quiet NaNs that 2370 // may result in the loop not terminating. 2371 expectedBits = getIntVolatile(o, offset); 2372 v = Float.intBitsToFloat(expectedBits); 2373 } while (!weakCompareAndSwapIntVolatile(o, offset, 2374 expectedBits, Float.floatToRawIntBits(v + delta))); 2375 return v; 2376 } 2377 2378 @ForceInline 2379 public final double getAndAddDouble(Object o, long offset, double delta) { 2380 long expectedBits; 2381 double v; 2382 do { 2383 // Load and CAS with the raw bits to avoid issues with NaNs and 2384 // possible bit conversion from signaling NaNs to quiet NaNs that 2385 // may result in the loop not terminating. 2386 expectedBits = getLongVolatile(o, offset); 2387 v = Double.longBitsToDouble(expectedBits); 2388 } while (!weakCompareAndSwapLongVolatile(o, offset, 2389 expectedBits, Double.doubleToRawLongBits(v + delta))); 2390 return v; 2391 } 2392 2393 /** 2394 * Atomically exchanges the given value with the current value of 2395 * a field or array element within the given object {@code o} 2396 * at the given {@code offset}. 2397 * 2398 * @param o object/array to update the field/element in 2399 * @param offset field/element offset 2400 * @param newValue new value 2401 * @return the previous value 2402 * @since 1.8 2403 */ 2404 @HotSpotIntrinsicCandidate 2405 public final int getAndSetInt(Object o, long offset, int newValue) { 2406 int v; 2407 do { 2408 v = getIntVolatile(o, offset); 2409 } while (!weakCompareAndSwapIntVolatile(o, offset, v, newValue)); 2410 return v; 2411 } 2412 2413 /** 2414 * Atomically exchanges the given value with the current value of 2415 * a field or array element within the given object {@code o} 2416 * at the given {@code offset}. 2417 * 2418 * @param o object/array to update the field/element in 2419 * @param offset field/element offset 2420 * @param newValue new value 2421 * @return the previous value 2422 * @since 1.8 2423 */ 2424 @HotSpotIntrinsicCandidate 2425 public final long getAndSetLong(Object o, long offset, long newValue) { 2426 long v; 2427 do { 2428 v = getLongVolatile(o, offset); 2429 } while (!weakCompareAndSwapLongVolatile(o, offset, v, newValue)); 2430 return v; 2431 } 2432 2433 /** 2434 * Atomically exchanges the given reference value with the current 2435 * reference value of a field or array element within the given 2436 * object {@code o} at the given {@code offset}. 2437 * 2438 * @param o object/array to update the field/element in 2439 * @param offset field/element offset 2440 * @param newValue new value 2441 * @return the previous value 2442 * @since 1.8 2443 */ 2444 @HotSpotIntrinsicCandidate 2445 public final Object getAndSetObject(Object o, long offset, Object newValue) { 2446 Object v; 2447 do { 2448 v = getObjectVolatile(o, offset); 2449 } while (!weakCompareAndSwapObjectVolatile(o, offset, v, newValue)); 2450 return v; 2451 } 2452 2453 @HotSpotIntrinsicCandidate 2454 public final byte getAndSetByte(Object o, long offset, byte newValue) { 2455 byte v; 2456 do { 2457 v = getByteVolatile(o, offset); 2458 } while (!weakCompareAndSwapByteVolatile(o, offset, v, newValue)); 2459 return v; 2460 } 2461 2462 @ForceInline 2463 public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) { 2464 return byte2bool(getAndSetByte(o, offset, bool2byte(newValue))); 2465 } 2466 2467 @HotSpotIntrinsicCandidate 2468 public final short getAndSetShort(Object o, long offset, short newValue) { 2469 short v; 2470 do { 2471 v = getShortVolatile(o, offset); 2472 } while (!weakCompareAndSwapShortVolatile(o, offset, v, newValue)); 2473 return v; 2474 } 2475 2476 @ForceInline 2477 public final char getAndSetChar(Object o, long offset, char newValue) { 2478 return s2c(getAndSetShort(o, offset, c2s(newValue))); 2479 } 2480 2481 @ForceInline 2482 public final float getAndSetFloat(Object o, long offset, float newValue) { 2483 int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue)); 2484 return Float.intBitsToFloat(v); 2485 } 2486 2487 @ForceInline 2488 public final double getAndSetDouble(Object o, long offset, double newValue) { 2489 long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue)); 2490 return Double.longBitsToDouble(v); 2491 } 2492 2493 /** 2494 * Ensures that loads before the fence will not be reordered with loads and 2495 * stores after the fence; a "LoadLoad plus LoadStore barrier". 2496 * 2497 * Corresponds to C11 atomic_thread_fence(memory_order_acquire) 2498 * (an "acquire fence"). 2499 * 2500 * A pure LoadLoad fence is not provided, since the addition of LoadStore 2501 * is almost always desired, and most current hardware instructions that 2502 * provide a LoadLoad barrier also provide a LoadStore barrier for free. 2503 * @since 1.8 2504 */ 2505 @HotSpotIntrinsicCandidate 2506 public native void loadFence(); 2507 2508 /** 2509 * Ensures that loads and stores before the fence will not be reordered with 2510 * stores after the fence; a "StoreStore plus LoadStore barrier". 2511 * 2512 * Corresponds to C11 atomic_thread_fence(memory_order_release) 2513 * (a "release fence"). 2514 * 2515 * A pure StoreStore fence is not provided, since the addition of LoadStore 2516 * is almost always desired, and most current hardware instructions that 2517 * provide a StoreStore barrier also provide a LoadStore barrier for free. 2518 * @since 1.8 2519 */ 2520 @HotSpotIntrinsicCandidate 2521 public native void storeFence(); 2522 2523 /** 2524 * Ensures that loads and stores before the fence will not be reordered 2525 * with loads and stores after the fence. Implies the effects of both 2526 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad 2527 * barrier. 2528 * 2529 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst). 2530 * @since 1.8 2531 */ 2532 @HotSpotIntrinsicCandidate 2533 public native void fullFence(); 2534 2535 /** 2536 * Ensures that loads before the fence will not be reordered with 2537 * loads after the fence. 2538 */ 2539 public final void loadLoadFence() { 2540 loadFence(); 2541 } 2542 2543 /** 2544 * Ensures that stores before the fence will not be reordered with 2545 * stores after the fence. 2546 */ 2547 public final void storeStoreFence() { 2548 storeFence(); 2549 } 2550 2551 2552 /** 2553 * Throws IllegalAccessError; for use by the VM for access control 2554 * error support. 2555 * @since 1.8 2556 */ 2557 private static void throwIllegalAccessError() { 2558 throw new IllegalAccessError(); 2559 } 2560 2561 /** 2562 * @return Returns true if the native byte ordering of this 2563 * platform is big-endian, false if it is little-endian. 2564 */ 2565 public final boolean isBigEndian() { return BE; } 2566 2567 /** 2568 * @return Returns true if this platform is capable of performing 2569 * accesses at addresses which are not aligned for the type of the 2570 * primitive type being accessed, false otherwise. 2571 */ 2572 public final boolean unalignedAccess() { return unalignedAccess; } 2573 2574 /** 2575 * Fetches a value at some byte offset into a given Java object. 2576 * More specifically, fetches a value within the given object 2577 * <code>o</code> at the given offset, or (if <code>o</code> is 2578 * null) from the memory address whose numerical value is the 2579 * given offset. <p> 2580 * 2581 * The specification of this method is the same as {@link 2582 * #getLong(Object, long)} except that the offset does not need to 2583 * have been obtained from {@link #objectFieldOffset} on the 2584 * {@link java.lang.reflect.Field} of some Java field. The value 2585 * in memory is raw data, and need not correspond to any Java 2586 * variable. Unless <code>o</code> is null, the value accessed 2587 * must be entirely within the allocated object. The endianness 2588 * of the value in memory is the endianness of the native platform. 2589 * 2590 * <p> The read will be atomic with respect to the largest power 2591 * of two that divides the GCD of the offset and the storage size. 2592 * For example, getLongUnaligned will make atomic reads of 2-, 4-, 2593 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 2594 * respectively. There are no other guarantees of atomicity. 2595 * <p> 2596 * 8-byte atomicity is only guaranteed on platforms on which 2597 * support atomic accesses to longs. 2598 * 2599 * @param o Java heap object in which the value resides, if any, else 2600 * null 2601 * @param offset The offset in bytes from the start of the object 2602 * @return the value fetched from the indicated object 2603 * @throws RuntimeException No defined exceptions are thrown, not even 2604 * {@link NullPointerException} 2605 * @since 9 2606 */ 2607 @HotSpotIntrinsicCandidate 2608 public final long getLongUnaligned(Object o, long offset) { 2609 if ((offset & 7) == 0) { 2610 return getLong(o, offset); 2611 } else if ((offset & 3) == 0) { 2612 return makeLong(getInt(o, offset), 2613 getInt(o, offset + 4)); 2614 } else if ((offset & 1) == 0) { 2615 return makeLong(getShort(o, offset), 2616 getShort(o, offset + 2), 2617 getShort(o, offset + 4), 2618 getShort(o, offset + 6)); 2619 } else { 2620 return makeLong(getByte(o, offset), 2621 getByte(o, offset + 1), 2622 getByte(o, offset + 2), 2623 getByte(o, offset + 3), 2624 getByte(o, offset + 4), 2625 getByte(o, offset + 5), 2626 getByte(o, offset + 6), 2627 getByte(o, offset + 7)); 2628 } 2629 } 2630 /** 2631 * As {@link #getLongUnaligned(Object, long)} but with an 2632 * additional argument which specifies the endianness of the value 2633 * as stored in memory. 2634 * 2635 * @param o Java heap object in which the variable resides 2636 * @param offset The offset in bytes from the start of the object 2637 * @param bigEndian The endianness of the value 2638 * @return the value fetched from the indicated object 2639 * @since 9 2640 */ 2641 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) { 2642 return convEndian(bigEndian, getLongUnaligned(o, offset)); 2643 } 2644 2645 /** @see #getLongUnaligned(Object, long) */ 2646 @HotSpotIntrinsicCandidate 2647 public final int getIntUnaligned(Object o, long offset) { 2648 if ((offset & 3) == 0) { 2649 return getInt(o, offset); 2650 } else if ((offset & 1) == 0) { 2651 return makeInt(getShort(o, offset), 2652 getShort(o, offset + 2)); 2653 } else { 2654 return makeInt(getByte(o, offset), 2655 getByte(o, offset + 1), 2656 getByte(o, offset + 2), 2657 getByte(o, offset + 3)); 2658 } 2659 } 2660 /** @see #getLongUnaligned(Object, long, boolean) */ 2661 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) { 2662 return convEndian(bigEndian, getIntUnaligned(o, offset)); 2663 } 2664 2665 /** @see #getLongUnaligned(Object, long) */ 2666 @HotSpotIntrinsicCandidate 2667 public final short getShortUnaligned(Object o, long offset) { 2668 if ((offset & 1) == 0) { 2669 return getShort(o, offset); 2670 } else { 2671 return makeShort(getByte(o, offset), 2672 getByte(o, offset + 1)); 2673 } 2674 } 2675 /** @see #getLongUnaligned(Object, long, boolean) */ 2676 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) { 2677 return convEndian(bigEndian, getShortUnaligned(o, offset)); 2678 } 2679 2680 /** @see #getLongUnaligned(Object, long) */ 2681 @HotSpotIntrinsicCandidate 2682 public final char getCharUnaligned(Object o, long offset) { 2683 if ((offset & 1) == 0) { 2684 return getChar(o, offset); 2685 } else { 2686 return (char)makeShort(getByte(o, offset), 2687 getByte(o, offset + 1)); 2688 } 2689 } 2690 2691 /** @see #getLongUnaligned(Object, long, boolean) */ 2692 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) { 2693 return convEndian(bigEndian, getCharUnaligned(o, offset)); 2694 } 2695 2696 /** 2697 * Stores a value at some byte offset into a given Java object. 2698 * <p> 2699 * The specification of this method is the same as {@link 2700 * #getLong(Object, long)} except that the offset does not need to 2701 * have been obtained from {@link #objectFieldOffset} on the 2702 * {@link java.lang.reflect.Field} of some Java field. The value 2703 * in memory is raw data, and need not correspond to any Java 2704 * variable. The endianness of the value in memory is the 2705 * endianness of the native platform. 2706 * <p> 2707 * The write will be atomic with respect to the largest power of 2708 * two that divides the GCD of the offset and the storage size. 2709 * For example, putLongUnaligned will make atomic writes of 2-, 4-, 2710 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 2711 * respectively. There are no other guarantees of atomicity. 2712 * <p> 2713 * 8-byte atomicity is only guaranteed on platforms on which 2714 * support atomic accesses to longs. 2715 * 2716 * @param o Java heap object in which the value resides, if any, else 2717 * null 2718 * @param offset The offset in bytes from the start of the object 2719 * @param x the value to store 2720 * @throws RuntimeException No defined exceptions are thrown, not even 2721 * {@link NullPointerException} 2722 * @since 9 2723 */ 2724 @HotSpotIntrinsicCandidate 2725 public final void putLongUnaligned(Object o, long offset, long x) { 2726 if ((offset & 7) == 0) { 2727 putLong(o, offset, x); 2728 } else if ((offset & 3) == 0) { 2729 putLongParts(o, offset, 2730 (int)(x >> 0), 2731 (int)(x >>> 32)); 2732 } else if ((offset & 1) == 0) { 2733 putLongParts(o, offset, 2734 (short)(x >>> 0), 2735 (short)(x >>> 16), 2736 (short)(x >>> 32), 2737 (short)(x >>> 48)); 2738 } else { 2739 putLongParts(o, offset, 2740 (byte)(x >>> 0), 2741 (byte)(x >>> 8), 2742 (byte)(x >>> 16), 2743 (byte)(x >>> 24), 2744 (byte)(x >>> 32), 2745 (byte)(x >>> 40), 2746 (byte)(x >>> 48), 2747 (byte)(x >>> 56)); 2748 } 2749 } 2750 2751 /** 2752 * As {@link #putLongUnaligned(Object, long, long)} but with an additional 2753 * argument which specifies the endianness of the value as stored in memory. 2754 * @param o Java heap object in which the value resides 2755 * @param offset The offset in bytes from the start of the object 2756 * @param x the value to store 2757 * @param bigEndian The endianness of the value 2758 * @throws RuntimeException No defined exceptions are thrown, not even 2759 * {@link NullPointerException} 2760 * @since 9 2761 */ 2762 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) { 2763 putLongUnaligned(o, offset, convEndian(bigEndian, x)); 2764 } 2765 2766 /** @see #putLongUnaligned(Object, long, long) */ 2767 @HotSpotIntrinsicCandidate 2768 public final void putIntUnaligned(Object o, long offset, int x) { 2769 if ((offset & 3) == 0) { 2770 putInt(o, offset, x); 2771 } else if ((offset & 1) == 0) { 2772 putIntParts(o, offset, 2773 (short)(x >> 0), 2774 (short)(x >>> 16)); 2775 } else { 2776 putIntParts(o, offset, 2777 (byte)(x >>> 0), 2778 (byte)(x >>> 8), 2779 (byte)(x >>> 16), 2780 (byte)(x >>> 24)); 2781 } 2782 } 2783 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2784 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) { 2785 putIntUnaligned(o, offset, convEndian(bigEndian, x)); 2786 } 2787 2788 /** @see #putLongUnaligned(Object, long, long) */ 2789 @HotSpotIntrinsicCandidate 2790 public final void putShortUnaligned(Object o, long offset, short x) { 2791 if ((offset & 1) == 0) { 2792 putShort(o, offset, x); 2793 } else { 2794 putShortParts(o, offset, 2795 (byte)(x >>> 0), 2796 (byte)(x >>> 8)); 2797 } 2798 } 2799 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2800 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) { 2801 putShortUnaligned(o, offset, convEndian(bigEndian, x)); 2802 } 2803 2804 /** @see #putLongUnaligned(Object, long, long) */ 2805 @HotSpotIntrinsicCandidate 2806 public final void putCharUnaligned(Object o, long offset, char x) { 2807 putShortUnaligned(o, offset, (short)x); 2808 } 2809 /** @see #putLongUnaligned(Object, long, long, boolean) */ 2810 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) { 2811 putCharUnaligned(o, offset, convEndian(bigEndian, x)); 2812 } 2813 2814 // JVM interface methods 2815 // BE is true iff the native endianness of this platform is big. 2816 private static final boolean BE = theUnsafe.isBigEndian0(); 2817 2818 // unalignedAccess is true iff this platform can perform unaligned accesses. 2819 private static final boolean unalignedAccess = theUnsafe.unalignedAccess0(); 2820 2821 private static int pickPos(int top, int pos) { return BE ? top - pos : pos; } 2822 2823 // These methods construct integers from bytes. The byte ordering 2824 // is the native endianness of this platform. 2825 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 2826 return ((toUnsignedLong(i0) << pickPos(56, 0)) 2827 | (toUnsignedLong(i1) << pickPos(56, 8)) 2828 | (toUnsignedLong(i2) << pickPos(56, 16)) 2829 | (toUnsignedLong(i3) << pickPos(56, 24)) 2830 | (toUnsignedLong(i4) << pickPos(56, 32)) 2831 | (toUnsignedLong(i5) << pickPos(56, 40)) 2832 | (toUnsignedLong(i6) << pickPos(56, 48)) 2833 | (toUnsignedLong(i7) << pickPos(56, 56))); 2834 } 2835 private static long makeLong(short i0, short i1, short i2, short i3) { 2836 return ((toUnsignedLong(i0) << pickPos(48, 0)) 2837 | (toUnsignedLong(i1) << pickPos(48, 16)) 2838 | (toUnsignedLong(i2) << pickPos(48, 32)) 2839 | (toUnsignedLong(i3) << pickPos(48, 48))); 2840 } 2841 private static long makeLong(int i0, int i1) { 2842 return (toUnsignedLong(i0) << pickPos(32, 0)) 2843 | (toUnsignedLong(i1) << pickPos(32, 32)); 2844 } 2845 private static int makeInt(short i0, short i1) { 2846 return (toUnsignedInt(i0) << pickPos(16, 0)) 2847 | (toUnsignedInt(i1) << pickPos(16, 16)); 2848 } 2849 private static int makeInt(byte i0, byte i1, byte i2, byte i3) { 2850 return ((toUnsignedInt(i0) << pickPos(24, 0)) 2851 | (toUnsignedInt(i1) << pickPos(24, 8)) 2852 | (toUnsignedInt(i2) << pickPos(24, 16)) 2853 | (toUnsignedInt(i3) << pickPos(24, 24))); 2854 } 2855 private static short makeShort(byte i0, byte i1) { 2856 return (short)((toUnsignedInt(i0) << pickPos(8, 0)) 2857 | (toUnsignedInt(i1) << pickPos(8, 8))); 2858 } 2859 2860 private static byte pick(byte le, byte be) { return BE ? be : le; } 2861 private static short pick(short le, short be) { return BE ? be : le; } 2862 private static int pick(int le, int be) { return BE ? be : le; } 2863 2864 // These methods write integers to memory from smaller parts 2865 // provided by their caller. The ordering in which these parts 2866 // are written is the native endianness of this platform. 2867 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 2868 putByte(o, offset + 0, pick(i0, i7)); 2869 putByte(o, offset + 1, pick(i1, i6)); 2870 putByte(o, offset + 2, pick(i2, i5)); 2871 putByte(o, offset + 3, pick(i3, i4)); 2872 putByte(o, offset + 4, pick(i4, i3)); 2873 putByte(o, offset + 5, pick(i5, i2)); 2874 putByte(o, offset + 6, pick(i6, i1)); 2875 putByte(o, offset + 7, pick(i7, i0)); 2876 } 2877 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) { 2878 putShort(o, offset + 0, pick(i0, i3)); 2879 putShort(o, offset + 2, pick(i1, i2)); 2880 putShort(o, offset + 4, pick(i2, i1)); 2881 putShort(o, offset + 6, pick(i3, i0)); 2882 } 2883 private void putLongParts(Object o, long offset, int i0, int i1) { 2884 putInt(o, offset + 0, pick(i0, i1)); 2885 putInt(o, offset + 4, pick(i1, i0)); 2886 } 2887 private void putIntParts(Object o, long offset, short i0, short i1) { 2888 putShort(o, offset + 0, pick(i0, i1)); 2889 putShort(o, offset + 2, pick(i1, i0)); 2890 } 2891 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) { 2892 putByte(o, offset + 0, pick(i0, i3)); 2893 putByte(o, offset + 1, pick(i1, i2)); 2894 putByte(o, offset + 2, pick(i2, i1)); 2895 putByte(o, offset + 3, pick(i3, i0)); 2896 } 2897 private void putShortParts(Object o, long offset, byte i0, byte i1) { 2898 putByte(o, offset + 0, pick(i0, i1)); 2899 putByte(o, offset + 1, pick(i1, i0)); 2900 } 2901 2902 // Zero-extend an integer 2903 private static int toUnsignedInt(byte n) { return n & 0xff; } 2904 private static int toUnsignedInt(short n) { return n & 0xffff; } 2905 private static long toUnsignedLong(byte n) { return n & 0xffl; } 2906 private static long toUnsignedLong(short n) { return n & 0xffffl; } 2907 private static long toUnsignedLong(int n) { return n & 0xffffffffl; } 2908 2909 // Maybe byte-reverse an integer 2910 private static char convEndian(boolean big, char n) { return big == BE ? n : Character.reverseBytes(n); } 2911 private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n) ; } 2912 private static int convEndian(boolean big, int n) { return big == BE ? n : Integer.reverseBytes(n) ; } 2913 private static long convEndian(boolean big, long n) { return big == BE ? n : Long.reverseBytes(n) ; } 2914 2915 2916 2917 private native long allocateMemory0(long bytes); 2918 private native long reallocateMemory0(long address, long bytes); 2919 private native void freeMemory0(long address); 2920 private native void setMemory0(Object o, long offset, long bytes, byte value); 2921 @HotSpotIntrinsicCandidate 2922 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes); 2923 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize); 2924 private native long objectFieldOffset0(Field f); 2925 private native long staticFieldOffset0(Field f); 2926 private native Object staticFieldBase0(Field f); 2927 private native boolean shouldBeInitialized0(Class<?> c); 2928 private native void ensureClassInitialized0(Class<?> c); 2929 private native int arrayBaseOffset0(Class<?> arrayClass); 2930 private native int arrayIndexScale0(Class<?> arrayClass); 2931 private native int addressSize0(); 2932 private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches); 2933 private native int getLoadAverage0(double[] loadavg, int nelems); 2934 private native boolean unalignedAccess0(); 2935 private native boolean isBigEndian0(); 2936 }