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