1 /* 2 * Copyright (c) 2000, 2018, 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 sun.misc; 27 28 import jdk.internal.vm.annotation.ForceInline; 29 import jdk.internal.misc.VM; 30 import jdk.internal.ref.Cleaner; 31 import jdk.internal.reflect.CallerSensitive; 32 import jdk.internal.reflect.Reflection; 33 import sun.nio.ch.DirectBuffer; 34 35 import java.lang.reflect.Field; 36 import java.util.Set; 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 static { 60 Reflection.registerMethodsToFilter(Unsafe.class, Set.of("getUnsafe")); 61 } 62 63 private Unsafe() {} 64 65 private static final Unsafe theUnsafe = new Unsafe(); 66 private static final jdk.internal.misc.Unsafe theInternalUnsafe = jdk.internal.misc.Unsafe.getUnsafe(); 67 68 /** 69 * Provides the caller with the capability of performing unsafe 70 * operations. 71 * 72 * <p>The returned {@code Unsafe} object should be carefully guarded 73 * by the caller, since it can be used to read and write data at arbitrary 74 * memory addresses. It must never be passed to untrusted code. 75 * 76 * <p>Most methods in this class are very low-level, and correspond to a 77 * small number of hardware instructions (on typical machines). Compilers 78 * are encouraged to optimize these methods accordingly. 79 * 80 * <p>Here is a suggested idiom for using unsafe operations: 81 * 82 * <pre> {@code 83 * class MyTrustedClass { 84 * private static final Unsafe unsafe = Unsafe.getUnsafe(); 85 * ... 86 * private long myCountAddress = ...; 87 * public int getCount() { return unsafe.getByte(myCountAddress); } 88 * }}</pre> 89 * 90 * (It may assist compilers to make the local variable {@code final}.) 91 * 92 * @throws SecurityException if the class loader of the caller 93 * class is not in the system domain in which all permissions 94 * are granted. 95 */ 96 @CallerSensitive 97 public static Unsafe getUnsafe() { 98 Class<?> caller = Reflection.getCallerClass(); 99 if (!VM.isSystemDomainLoader(caller.getClassLoader())) 100 throw new SecurityException("Unsafe"); 101 return theUnsafe; 102 } 103 104 /// peek and poke operations 105 /// (compilers should optimize these to memory ops) 106 107 // These work on object fields in the Java heap. 108 // They will not work on elements of packed arrays. 109 110 /** 111 * Fetches a value from a given Java variable. 112 * More specifically, fetches a field or array element within the given 113 * object {@code o} at the given offset, or (if {@code o} is null) 114 * from the memory address whose numerical value is the given offset. 115 * <p> 116 * The results are undefined unless one of the following cases is true: 117 * <ul> 118 * <li>The offset was obtained from {@link #objectFieldOffset} on 119 * the {@link java.lang.reflect.Field} of some Java field and the object 120 * referred to by {@code o} is of a class compatible with that 121 * field's class. 122 * 123 * <li>The offset and object reference {@code o} (either null or 124 * non-null) were both obtained via {@link #staticFieldOffset} 125 * and {@link #staticFieldBase} (respectively) from the 126 * reflective {@link Field} representation of some Java field. 127 * 128 * <li>The object referred to by {@code o} is an array, and the offset 129 * is an integer of the form {@code B+N*S}, where {@code N} is 130 * a valid index into the array, and {@code B} and {@code S} are 131 * the values obtained by {@link #arrayBaseOffset} and {@link 132 * #arrayIndexScale} (respectively) from the array's class. The value 133 * referred to is the {@code N}<em>th</em> element of the array. 134 * 135 * </ul> 136 * <p> 137 * If one of the above cases is true, the call references a specific Java 138 * variable (field or array element). However, the results are undefined 139 * if that variable is not in fact of the type returned by this method. 140 * <p> 141 * This method refers to a variable by means of two parameters, and so 142 * it provides (in effect) a <em>double-register</em> addressing mode 143 * for Java variables. When the object reference is null, this method 144 * uses its offset as an absolute address. This is similar in operation 145 * to methods such as {@link #getInt(long)}, which provide (in effect) a 146 * <em>single-register</em> addressing mode for non-Java variables. 147 * However, because Java variables may have a different layout in memory 148 * from non-Java variables, programmers should not assume that these 149 * two addressing modes are ever equivalent. Also, programmers should 150 * remember that offsets from the double-register addressing mode cannot 151 * be portably confused with longs used in the single-register addressing 152 * mode. 153 * 154 * @param o Java heap object in which the variable resides, if any, else 155 * null 156 * @param offset indication of where the variable resides in a Java heap 157 * object, if any, else a memory address locating the variable 158 * statically 159 * @return the value fetched from the indicated Java variable 160 * @throws RuntimeException No defined exceptions are thrown, not even 161 * {@link NullPointerException} 162 */ 163 @ForceInline 164 public int getInt(Object o, long offset) { 165 return theInternalUnsafe.getInt(o, offset); 166 } 167 168 /** 169 * Stores a value into a given Java variable. 170 * <p> 171 * The first two parameters are interpreted exactly as with 172 * {@link #getInt(Object, long)} to refer to a specific 173 * Java variable (field or array element). The given value 174 * is stored into that variable. 175 * <p> 176 * The variable must be of the same type as the method 177 * parameter {@code x}. 178 * 179 * @param o Java heap object in which the variable resides, if any, else 180 * null 181 * @param offset indication of where the variable resides in a Java heap 182 * object, if any, else a memory address locating the variable 183 * statically 184 * @param x the value to store into the indicated Java variable 185 * @throws RuntimeException No defined exceptions are thrown, not even 186 * {@link NullPointerException} 187 */ 188 @ForceInline 189 public void putInt(Object o, long offset, int x) { 190 theInternalUnsafe.putInt(o, offset, x); 191 } 192 193 /** 194 * Fetches a reference value from a given Java variable. 195 * @see #getInt(Object, long) 196 */ 197 @ForceInline 198 public Object getObject(Object o, long offset) { 199 return theInternalUnsafe.getReference(o, offset); 200 } 201 202 /** 203 * Stores a reference value into a given Java variable. 204 * <p> 205 * Unless the reference {@code x} being stored is either null 206 * or matches the field type, the results are undefined. 207 * If the reference {@code o} is non-null, card marks or 208 * other store barriers for that object (if the VM requires them) 209 * are updated. 210 * @see #putInt(Object, long, int) 211 */ 212 @ForceInline 213 public void putObject(Object o, long offset, Object x) { 214 theInternalUnsafe.putReference(o, offset, x); 215 } 216 217 /** @see #getInt(Object, long) */ 218 @ForceInline 219 public boolean getBoolean(Object o, long offset) { 220 return theInternalUnsafe.getBoolean(o, offset); 221 } 222 223 /** @see #putInt(Object, long, int) */ 224 @ForceInline 225 public void putBoolean(Object o, long offset, boolean x) { 226 theInternalUnsafe.putBoolean(o, offset, x); 227 } 228 229 /** @see #getInt(Object, long) */ 230 @ForceInline 231 public byte getByte(Object o, long offset) { 232 return theInternalUnsafe.getByte(o, offset); 233 } 234 235 /** @see #putInt(Object, long, int) */ 236 @ForceInline 237 public void putByte(Object o, long offset, byte x) { 238 theInternalUnsafe.putByte(o, offset, x); 239 } 240 241 /** @see #getInt(Object, long) */ 242 @ForceInline 243 public short getShort(Object o, long offset) { 244 return theInternalUnsafe.getShort(o, offset); 245 } 246 247 /** @see #putInt(Object, long, int) */ 248 @ForceInline 249 public void putShort(Object o, long offset, short x) { 250 theInternalUnsafe.putShort(o, offset, x); 251 } 252 253 /** @see #getInt(Object, long) */ 254 @ForceInline 255 public char getChar(Object o, long offset) { 256 return theInternalUnsafe.getChar(o, offset); 257 } 258 259 /** @see #putInt(Object, long, int) */ 260 @ForceInline 261 public void putChar(Object o, long offset, char x) { 262 theInternalUnsafe.putChar(o, offset, x); 263 } 264 265 /** @see #getInt(Object, long) */ 266 @ForceInline 267 public long getLong(Object o, long offset) { 268 return theInternalUnsafe.getLong(o, offset); 269 } 270 271 /** @see #putInt(Object, long, int) */ 272 @ForceInline 273 public void putLong(Object o, long offset, long x) { 274 theInternalUnsafe.putLong(o, offset, x); 275 } 276 277 /** @see #getInt(Object, long) */ 278 @ForceInline 279 public float getFloat(Object o, long offset) { 280 return theInternalUnsafe.getFloat(o, offset); 281 } 282 283 /** @see #putInt(Object, long, int) */ 284 @ForceInline 285 public void putFloat(Object o, long offset, float x) { 286 theInternalUnsafe.putFloat(o, offset, x); 287 } 288 289 /** @see #getInt(Object, long) */ 290 @ForceInline 291 public double getDouble(Object o, long offset) { 292 return theInternalUnsafe.getDouble(o, offset); 293 } 294 295 /** @see #putInt(Object, long, int) */ 296 @ForceInline 297 public void putDouble(Object o, long offset, double x) { 298 theInternalUnsafe.putDouble(o, offset, x); 299 } 300 301 // These work on values in the C heap. 302 303 /** 304 * Fetches a value from a given memory address. If the address is zero, or 305 * does not point into a block obtained from {@link #allocateMemory}, the 306 * results are undefined. 307 * 308 * @see #allocateMemory 309 */ 310 @ForceInline 311 public byte getByte(long address) { 312 return theInternalUnsafe.getByte(address); 313 } 314 315 /** 316 * Stores a value into a given memory address. If the address is zero, or 317 * does not point into a block obtained from {@link #allocateMemory}, the 318 * results are undefined. 319 * 320 * @see #getByte(long) 321 */ 322 @ForceInline 323 public void putByte(long address, byte x) { 324 theInternalUnsafe.putByte(address, x); 325 } 326 327 /** @see #getByte(long) */ 328 @ForceInline 329 public short getShort(long address) { 330 return theInternalUnsafe.getShort(address); 331 } 332 333 /** @see #putByte(long, byte) */ 334 @ForceInline 335 public void putShort(long address, short x) { 336 theInternalUnsafe.putShort(address, x); 337 } 338 339 /** @see #getByte(long) */ 340 @ForceInline 341 public char getChar(long address) { 342 return theInternalUnsafe.getChar(address); 343 } 344 345 /** @see #putByte(long, byte) */ 346 @ForceInline 347 public void putChar(long address, char x) { 348 theInternalUnsafe.putChar(address, x); 349 } 350 351 /** @see #getByte(long) */ 352 @ForceInline 353 public int getInt(long address) { 354 return theInternalUnsafe.getInt(address); 355 } 356 357 /** @see #putByte(long, byte) */ 358 @ForceInline 359 public void putInt(long address, int x) { 360 theInternalUnsafe.putInt(address, x); 361 } 362 363 /** @see #getByte(long) */ 364 @ForceInline 365 public long getLong(long address) { 366 return theInternalUnsafe.getLong(address); 367 } 368 369 /** @see #putByte(long, byte) */ 370 @ForceInline 371 public void putLong(long address, long x) { 372 theInternalUnsafe.putLong(address, x); 373 } 374 375 /** @see #getByte(long) */ 376 @ForceInline 377 public float getFloat(long address) { 378 return theInternalUnsafe.getFloat(address); 379 } 380 381 /** @see #putByte(long, byte) */ 382 @ForceInline 383 public void putFloat(long address, float x) { 384 theInternalUnsafe.putFloat(address, x); 385 } 386 387 /** @see #getByte(long) */ 388 @ForceInline 389 public double getDouble(long address) { 390 return theInternalUnsafe.getDouble(address); 391 } 392 393 /** @see #putByte(long, byte) */ 394 @ForceInline 395 public void putDouble(long address, double x) { 396 theInternalUnsafe.putDouble(address, x); 397 } 398 399 400 /** 401 * Fetches a native pointer from a given memory address. If the address is 402 * zero, or does not point into a block obtained from {@link 403 * #allocateMemory}, the results are undefined. 404 * 405 * <p>If the native pointer is less than 64 bits wide, it is extended as 406 * an unsigned number to a Java long. The pointer may be indexed by any 407 * given byte offset, simply by adding that offset (as a simple integer) to 408 * the long representing the pointer. The number of bytes actually read 409 * from the target address may be determined by consulting {@link 410 * #addressSize}. 411 * 412 * @see #allocateMemory 413 */ 414 @ForceInline 415 public long getAddress(long address) { 416 return theInternalUnsafe.getAddress(address); 417 } 418 419 /** 420 * Stores a native pointer into a given memory address. If the address is 421 * zero, or does not point into a block obtained from {@link 422 * #allocateMemory}, the results are undefined. 423 * 424 * <p>The number of bytes actually written at the target address may be 425 * determined by consulting {@link #addressSize}. 426 * 427 * @see #getAddress(long) 428 */ 429 @ForceInline 430 public void putAddress(long address, long x) { 431 theInternalUnsafe.putAddress(address, x); 432 } 433 434 435 /// wrappers for malloc, realloc, free: 436 437 /** 438 * Allocates a new block of native memory, of the given size in bytes. The 439 * contents of the memory are uninitialized; they will generally be 440 * garbage. The resulting native pointer will never be zero, and will be 441 * aligned for all value types. Dispose of this memory by calling {@link 442 * #freeMemory}, or resize it with {@link #reallocateMemory}. 443 * 444 * <em>Note:</em> It is the resposibility of the caller to make 445 * sure arguments are checked before the methods are called. While 446 * some rudimentary checks are performed on the input, the checks 447 * are best effort and when performance is an overriding priority, 448 * as when methods of this class are optimized by the runtime 449 * compiler, some or all checks (if any) may be elided. Hence, the 450 * caller must not rely on the checks and corresponding 451 * exceptions! 452 * 453 * @throws RuntimeException if the size is negative or too large 454 * for the native size_t type 455 * 456 * @throws OutOfMemoryError if the allocation is refused by the system 457 * 458 * @see #getByte(long) 459 * @see #putByte(long, byte) 460 */ 461 @ForceInline 462 public long allocateMemory(long bytes) { 463 return theInternalUnsafe.allocateMemory(bytes); 464 } 465 466 /** 467 * Resizes a new block of native memory, to the given size in bytes. The 468 * contents of the new block past the size of the old block are 469 * uninitialized; they will generally be garbage. The resulting native 470 * pointer will be zero if and only if the requested size is zero. The 471 * resulting native pointer will be aligned for all value types. Dispose 472 * of this memory by calling {@link #freeMemory}, or resize it with {@link 473 * #reallocateMemory}. The address passed to this method may be null, in 474 * which case an allocation will be performed. 475 * 476 * <em>Note:</em> It is the resposibility of the caller to make 477 * sure arguments are checked before the methods are called. While 478 * some rudimentary checks are performed on the input, the checks 479 * are best effort and when performance is an overriding priority, 480 * as when methods of this class are optimized by the runtime 481 * compiler, some or all checks (if any) may be elided. Hence, the 482 * caller must not rely on the checks and corresponding 483 * exceptions! 484 * 485 * @throws RuntimeException if the size is negative or too large 486 * for the native size_t type 487 * 488 * @throws OutOfMemoryError if the allocation is refused by the system 489 * 490 * @see #allocateMemory 491 */ 492 @ForceInline 493 public long reallocateMemory(long address, long bytes) { 494 return theInternalUnsafe.reallocateMemory(address, bytes); 495 } 496 497 /** 498 * Sets all bytes in a given block of memory to a fixed value 499 * (usually zero). 500 * 501 * <p>This method determines a block's base address by means of two parameters, 502 * and so it provides (in effect) a <em>double-register</em> addressing mode, 503 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 504 * the offset supplies an absolute base address. 505 * 506 * <p>The stores are in coherent (atomic) units of a size determined 507 * by the address and length parameters. If the effective address and 508 * length are all even modulo 8, the stores take place in 'long' units. 509 * If the effective address and length are (resp.) even modulo 4 or 2, 510 * the stores take place in units of 'int' or 'short'. 511 * 512 * <em>Note:</em> It is the resposibility of the caller to make 513 * sure arguments are checked before the methods are called. While 514 * some rudimentary checks are performed on the input, the checks 515 * are best effort and when performance is an overriding priority, 516 * as when methods of this class are optimized by the runtime 517 * compiler, some or all checks (if any) may be elided. Hence, the 518 * caller must not rely on the checks and corresponding 519 * exceptions! 520 * 521 * @throws RuntimeException if any of the arguments is invalid 522 * 523 * @since 1.7 524 */ 525 @ForceInline 526 public void setMemory(Object o, long offset, long bytes, byte value) { 527 theInternalUnsafe.setMemory(o, offset, bytes, value); 528 } 529 530 /** 531 * Sets all bytes in a given block of memory to a fixed value 532 * (usually zero). This provides a <em>single-register</em> addressing mode, 533 * as discussed in {@link #getInt(Object,long)}. 534 * 535 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}. 536 */ 537 @ForceInline 538 public void setMemory(long address, long bytes, byte value) { 539 theInternalUnsafe.setMemory(address, bytes, value); 540 } 541 542 /** 543 * Sets all bytes in a given block of memory to a copy of another 544 * block. 545 * 546 * <p>This method determines each block's base address by means of two parameters, 547 * and so it provides (in effect) a <em>double-register</em> addressing mode, 548 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 549 * the offset supplies an absolute base address. 550 * 551 * <p>The transfers are in coherent (atomic) units of a size determined 552 * by the address and length parameters. If the effective addresses and 553 * length are all even modulo 8, the transfer takes place in 'long' units. 554 * If the effective addresses and length are (resp.) even modulo 4 or 2, 555 * the transfer takes place in units of 'int' or 'short'. 556 * 557 * <em>Note:</em> It is the resposibility of the caller to make 558 * sure arguments are checked before the methods are called. While 559 * some rudimentary checks are performed on the input, the checks 560 * are best effort and when performance is an overriding priority, 561 * as when methods of this class are optimized by the runtime 562 * compiler, some or all checks (if any) may be elided. Hence, the 563 * caller must not rely on the checks and corresponding 564 * exceptions! 565 * 566 * @throws RuntimeException if any of the arguments is invalid 567 * 568 * @since 1.7 569 */ 570 @ForceInline 571 public void copyMemory(Object srcBase, long srcOffset, 572 Object destBase, long destOffset, 573 long bytes) { 574 theInternalUnsafe.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes); 575 } 576 577 /** 578 * Sets all bytes in a given block of memory to a copy of another 579 * block. This provides a <em>single-register</em> addressing mode, 580 * as discussed in {@link #getInt(Object,long)}. 581 * 582 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}. 583 */ 584 @ForceInline 585 public void copyMemory(long srcAddress, long destAddress, long bytes) { 586 theInternalUnsafe.copyMemory(srcAddress, destAddress, bytes); 587 } 588 589 /** 590 * Disposes of a block of native memory, as obtained from {@link 591 * #allocateMemory} or {@link #reallocateMemory}. The address passed to 592 * this method may be null, in which case no action is taken. 593 * 594 * <em>Note:</em> It is the resposibility of the caller to make 595 * sure arguments are checked before the methods are called. While 596 * some rudimentary checks are performed on the input, the checks 597 * are best effort and when performance is an overriding priority, 598 * as when methods of this class are optimized by the runtime 599 * compiler, some or all checks (if any) may be elided. Hence, the 600 * caller must not rely on the checks and corresponding 601 * exceptions! 602 * 603 * @throws RuntimeException if any of the arguments is invalid 604 * 605 * @see #allocateMemory 606 */ 607 @ForceInline 608 public void freeMemory(long address) { 609 theInternalUnsafe.freeMemory(address); 610 } 611 612 /// random queries 613 614 /** 615 * This constant differs from all results that will ever be returned from 616 * {@link #staticFieldOffset}, {@link #objectFieldOffset}, 617 * or {@link #arrayBaseOffset}. 618 */ 619 public static final int INVALID_FIELD_OFFSET = jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET; 620 621 /** 622 * Reports the location of a given field in the storage allocation of its 623 * class. Do not expect to perform any sort of arithmetic on this offset; 624 * it is just a cookie which is passed to the unsafe heap memory accessors. 625 * 626 * <p>Any given field will always have the same offset and base, and no 627 * two distinct fields of the same class will ever have the same offset 628 * and base. 629 * 630 * <p>As of 1.4.1, offsets for fields are represented as long values, 631 * although the Sun JVM does not use the most significant 32 bits. 632 * However, JVM implementations which store static fields at absolute 633 * addresses can use long offsets and null base pointers to express 634 * the field locations in a form usable by {@link #getInt(Object,long)}. 635 * Therefore, code which will be ported to such JVMs on 64-bit platforms 636 * must preserve all bits of static field offsets. 637 * @see #getInt(Object, long) 638 */ 639 @ForceInline 640 public long objectFieldOffset(Field f) { 641 return theInternalUnsafe.objectFieldOffset(f); 642 } 643 644 /** 645 * Reports the location of a given static field, in conjunction with {@link 646 * #staticFieldBase}. 647 * <p>Do not expect to perform any sort of arithmetic on this offset; 648 * it is just a cookie which is passed to the unsafe heap memory accessors. 649 * 650 * <p>Any given field will always have the same offset, and no two distinct 651 * fields of the same class will ever have the same offset. 652 * 653 * <p>As of 1.4.1, offsets for fields are represented as long values, 654 * although the Sun JVM does not use the most significant 32 bits. 655 * It is hard to imagine a JVM technology which needs more than 656 * a few bits to encode an offset within a non-array object, 657 * However, for consistency with other methods in this class, 658 * this method reports its result as a long value. 659 * @see #getInt(Object, long) 660 */ 661 @ForceInline 662 public long staticFieldOffset(Field f) { 663 return theInternalUnsafe.staticFieldOffset(f); 664 } 665 666 /** 667 * Reports the location of a given static field, in conjunction with {@link 668 * #staticFieldOffset}. 669 * <p>Fetch the base "Object", if any, with which static fields of the 670 * given class can be accessed via methods like {@link #getInt(Object, 671 * long)}. This value may be null. This value may refer to an object 672 * which is a "cookie", not guaranteed to be a real Object, and it should 673 * not be used in any way except as argument to the get and put routines in 674 * this class. 675 */ 676 @ForceInline 677 public Object staticFieldBase(Field f) { 678 return theInternalUnsafe.staticFieldBase(f); 679 } 680 681 /** 682 * Detects if the given class may need to be initialized. This is often 683 * needed in conjunction with obtaining the static field base of a 684 * class. 685 * @return false only if a call to {@code ensureClassInitialized} would have no effect 686 */ 687 @ForceInline 688 public boolean shouldBeInitialized(Class<?> c) { 689 return theInternalUnsafe.shouldBeInitialized(c); 690 } 691 692 /** 693 * Ensures the given class has been initialized. This is often 694 * needed in conjunction with obtaining the static field base of a 695 * class. 696 */ 697 @ForceInline 698 public void ensureClassInitialized(Class<?> c) { 699 theInternalUnsafe.ensureClassInitialized(c); 700 } 701 702 /** 703 * Reports the offset of the first element in the storage allocation of a 704 * given array class. If {@link #arrayIndexScale} returns a non-zero value 705 * for the same class, you may use that scale factor, together with this 706 * base offset, to form new offsets to access elements of arrays of the 707 * given class. 708 * 709 * @see #getInt(Object, long) 710 * @see #putInt(Object, long, int) 711 */ 712 @ForceInline 713 public int arrayBaseOffset(Class<?> arrayClass) { 714 return theInternalUnsafe.arrayBaseOffset(arrayClass); 715 } 716 717 /** The value of {@code arrayBaseOffset(boolean[].class)} */ 718 public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET; 719 720 /** The value of {@code arrayBaseOffset(byte[].class)} */ 721 public static final int ARRAY_BYTE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; 722 723 /** The value of {@code arrayBaseOffset(short[].class)} */ 724 public static final int ARRAY_SHORT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET; 725 726 /** The value of {@code arrayBaseOffset(char[].class)} */ 727 public static final int ARRAY_CHAR_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_CHAR_BASE_OFFSET; 728 729 /** The value of {@code arrayBaseOffset(int[].class)} */ 730 public static final int ARRAY_INT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_INT_BASE_OFFSET; 731 732 /** The value of {@code arrayBaseOffset(long[].class)} */ 733 public static final int ARRAY_LONG_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_LONG_BASE_OFFSET; 734 735 /** The value of {@code arrayBaseOffset(float[].class)} */ 736 public static final int ARRAY_FLOAT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_FLOAT_BASE_OFFSET; 737 738 /** The value of {@code arrayBaseOffset(double[].class)} */ 739 public static final int ARRAY_DOUBLE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_BASE_OFFSET; 740 741 /** The value of {@code arrayBaseOffset(Object[].class)} */ 742 public static final int ARRAY_OBJECT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET; 743 744 /** 745 * Reports the scale factor for addressing elements in the storage 746 * allocation of a given array class. However, arrays of "narrow" types 747 * will generally not work properly with accessors like {@link 748 * #getByte(Object, long)}, so the scale factor for such classes is reported 749 * as zero. 750 * 751 * @see #arrayBaseOffset 752 * @see #getInt(Object, long) 753 * @see #putInt(Object, long, int) 754 */ 755 @ForceInline 756 public int arrayIndexScale(Class<?> arrayClass) { 757 return theInternalUnsafe.arrayIndexScale(arrayClass); 758 } 759 760 /** The value of {@code arrayIndexScale(boolean[].class)} */ 761 public static final int ARRAY_BOOLEAN_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE; 762 763 /** The value of {@code arrayIndexScale(byte[].class)} */ 764 public static final int ARRAY_BYTE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE; 765 766 /** The value of {@code arrayIndexScale(short[].class)} */ 767 public static final int ARRAY_SHORT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE; 768 769 /** The value of {@code arrayIndexScale(char[].class)} */ 770 public static final int ARRAY_CHAR_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_CHAR_INDEX_SCALE; 771 772 /** The value of {@code arrayIndexScale(int[].class)} */ 773 public static final int ARRAY_INT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_INT_INDEX_SCALE; 774 775 /** The value of {@code arrayIndexScale(long[].class)} */ 776 public static final int ARRAY_LONG_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_LONG_INDEX_SCALE; 777 778 /** The value of {@code arrayIndexScale(float[].class)} */ 779 public static final int ARRAY_FLOAT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE; 780 781 /** The value of {@code arrayIndexScale(double[].class)} */ 782 public static final int ARRAY_DOUBLE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE; 783 784 /** The value of {@code arrayIndexScale(Object[].class)} */ 785 public static final int ARRAY_OBJECT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE; 786 787 /** 788 * Reports the size in bytes of a native pointer, as stored via {@link 789 * #putAddress}. This value will be either 4 or 8. Note that the sizes of 790 * other primitive types (as stored in native memory blocks) is determined 791 * fully by their information content. 792 */ 793 @ForceInline 794 public int addressSize() { 795 return theInternalUnsafe.addressSize(); 796 } 797 798 /** The value of {@code addressSize()} */ 799 public static final int ADDRESS_SIZE = theInternalUnsafe.addressSize(); 800 801 /** 802 * Reports the size in bytes of a native memory page (whatever that is). 803 * This value will always be a power of two. 804 */ 805 @ForceInline 806 public int pageSize() { 807 return theInternalUnsafe.pageSize(); 808 } 809 810 811 /// random trusted operations from JNI: 812 813 /** 814 * Defines a class but does not make it known to the class loader or system dictionary. 815 * <p> 816 * For each CP entry, the corresponding CP patch must either be null or have 817 * the a format that matches its tag: 818 * <ul> 819 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang 820 * <li>Utf8: a string (must have suitable syntax if used as signature or name) 821 * <li>Class: any java.lang.Class object 822 * <li>String: any object (not just a java.lang.String) 823 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments 824 * </ul> 825 * @param hostClass context for linkage, access control, protection domain, and class loader 826 * @param data bytes of a class file 827 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data 828 */ 829 @ForceInline 830 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) { 831 return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches); 832 } 833 834 /** 835 * Allocates an instance but does not run any constructor. 836 * Initializes the class if it has not yet been. 837 */ 838 @ForceInline 839 public Object allocateInstance(Class<?> cls) 840 throws InstantiationException { 841 return theInternalUnsafe.allocateInstance(cls); 842 } 843 844 /** Throws the exception without telling the verifier. */ 845 @ForceInline 846 public void throwException(Throwable ee) { 847 theInternalUnsafe.throwException(ee); 848 } 849 850 /** 851 * Atomically updates Java variable to {@code x} if it is currently 852 * holding {@code expected}. 853 * 854 * <p>This operation has memory semantics of a {@code volatile} read 855 * and write. Corresponds to C11 atomic_compare_exchange_strong. 856 * 857 * @return {@code true} if successful 858 */ 859 @ForceInline 860 public final boolean compareAndSwapObject(Object o, long offset, 861 Object expected, 862 Object x) { 863 return theInternalUnsafe.compareAndSetReference(o, offset, expected, x); 864 } 865 866 /** 867 * Atomically updates Java variable to {@code x} if it is currently 868 * holding {@code expected}. 869 * 870 * <p>This operation has memory semantics of a {@code volatile} read 871 * and write. Corresponds to C11 atomic_compare_exchange_strong. 872 * 873 * @return {@code true} if successful 874 */ 875 @ForceInline 876 public final boolean compareAndSwapInt(Object o, long offset, 877 int expected, 878 int x) { 879 return theInternalUnsafe.compareAndSetInt(o, offset, expected, x); 880 } 881 882 /** 883 * Atomically updates Java variable to {@code x} if it is currently 884 * holding {@code expected}. 885 * 886 * <p>This operation has memory semantics of a {@code volatile} read 887 * and write. Corresponds to C11 atomic_compare_exchange_strong. 888 * 889 * @return {@code true} if successful 890 */ 891 @ForceInline 892 public final boolean compareAndSwapLong(Object o, long offset, 893 long expected, 894 long x) { 895 return theInternalUnsafe.compareAndSetLong(o, offset, expected, x); 896 } 897 898 /** 899 * Fetches a reference value from a given Java variable, with volatile 900 * load semantics. Otherwise identical to {@link #getObject(Object, long)} 901 */ 902 @ForceInline 903 public Object getObjectVolatile(Object o, long offset) { 904 return theInternalUnsafe.getReferenceVolatile(o, offset); 905 } 906 907 /** 908 * Stores a reference value into a given Java variable, with 909 * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)} 910 */ 911 @ForceInline 912 public void putObjectVolatile(Object o, long offset, Object x) { 913 theInternalUnsafe.putReferenceVolatile(o, offset, x); 914 } 915 916 /** Volatile version of {@link #getInt(Object, long)} */ 917 @ForceInline 918 public int getIntVolatile(Object o, long offset) { 919 return theInternalUnsafe.getIntVolatile(o, offset); 920 } 921 922 /** Volatile version of {@link #putInt(Object, long, int)} */ 923 @ForceInline 924 public void putIntVolatile(Object o, long offset, int x) { 925 theInternalUnsafe.putIntVolatile(o, offset, x); 926 } 927 928 /** Volatile version of {@link #getBoolean(Object, long)} */ 929 @ForceInline 930 public boolean getBooleanVolatile(Object o, long offset) { 931 return theInternalUnsafe.getBooleanVolatile(o, offset); 932 } 933 934 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */ 935 @ForceInline 936 public void putBooleanVolatile(Object o, long offset, boolean x) { 937 theInternalUnsafe.putBooleanVolatile(o, offset, x); 938 } 939 940 /** Volatile version of {@link #getByte(Object, long)} */ 941 @ForceInline 942 public byte getByteVolatile(Object o, long offset) { 943 return theInternalUnsafe.getByteVolatile(o, offset); 944 } 945 946 /** Volatile version of {@link #putByte(Object, long, byte)} */ 947 @ForceInline 948 public void putByteVolatile(Object o, long offset, byte x) { 949 theInternalUnsafe.putByteVolatile(o, offset, x); 950 } 951 952 /** Volatile version of {@link #getShort(Object, long)} */ 953 @ForceInline 954 public short getShortVolatile(Object o, long offset) { 955 return theInternalUnsafe.getShortVolatile(o, offset); 956 } 957 958 /** Volatile version of {@link #putShort(Object, long, short)} */ 959 @ForceInline 960 public void putShortVolatile(Object o, long offset, short x) { 961 theInternalUnsafe.putShortVolatile(o, offset, x); 962 } 963 964 /** Volatile version of {@link #getChar(Object, long)} */ 965 @ForceInline 966 public char getCharVolatile(Object o, long offset) { 967 return theInternalUnsafe.getCharVolatile(o, offset); 968 } 969 970 /** Volatile version of {@link #putChar(Object, long, char)} */ 971 @ForceInline 972 public void putCharVolatile(Object o, long offset, char x) { 973 theInternalUnsafe.putCharVolatile(o, offset, x); 974 } 975 976 /** Volatile version of {@link #getLong(Object, long)} */ 977 @ForceInline 978 public long getLongVolatile(Object o, long offset) { 979 return theInternalUnsafe.getLongVolatile(o, offset); 980 } 981 982 /** Volatile version of {@link #putLong(Object, long, long)} */ 983 @ForceInline 984 public void putLongVolatile(Object o, long offset, long x) { 985 theInternalUnsafe.putLongVolatile(o, offset, x); 986 } 987 988 /** Volatile version of {@link #getFloat(Object, long)} */ 989 @ForceInline 990 public float getFloatVolatile(Object o, long offset) { 991 return theInternalUnsafe.getFloatVolatile(o, offset); 992 } 993 994 /** Volatile version of {@link #putFloat(Object, long, float)} */ 995 @ForceInline 996 public void putFloatVolatile(Object o, long offset, float x) { 997 theInternalUnsafe.putFloatVolatile(o, offset, x); 998 } 999 1000 /** Volatile version of {@link #getDouble(Object, long)} */ 1001 @ForceInline 1002 public double getDoubleVolatile(Object o, long offset) { 1003 return theInternalUnsafe.getDoubleVolatile(o, offset); 1004 } 1005 1006 /** Volatile version of {@link #putDouble(Object, long, double)} */ 1007 @ForceInline 1008 public void putDoubleVolatile(Object o, long offset, double x) { 1009 theInternalUnsafe.putDoubleVolatile(o, offset, x); 1010 } 1011 1012 /** 1013 * Version of {@link #putObjectVolatile(Object, long, Object)} 1014 * that does not guarantee immediate visibility of the store to 1015 * other threads. This method is generally only useful if the 1016 * underlying field is a Java volatile (or if an array cell, one 1017 * that is otherwise only accessed using volatile accesses). 1018 * 1019 * Corresponds to C11 atomic_store_explicit(..., memory_order_release). 1020 */ 1021 @ForceInline 1022 public void putOrderedObject(Object o, long offset, Object x) { 1023 theInternalUnsafe.putReferenceRelease(o, offset, x); 1024 } 1025 1026 /** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)} */ 1027 @ForceInline 1028 public void putOrderedInt(Object o, long offset, int x) { 1029 theInternalUnsafe.putIntRelease(o, offset, x); 1030 } 1031 1032 /** Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)} */ 1033 @ForceInline 1034 public void putOrderedLong(Object o, long offset, long x) { 1035 theInternalUnsafe.putLongRelease(o, offset, x); 1036 } 1037 1038 /** 1039 * Unblocks the given thread blocked on {@code park}, or, if it is 1040 * not blocked, causes the subsequent call to {@code park} not to 1041 * block. Note: this operation is "unsafe" solely because the 1042 * caller must somehow ensure that the thread has not been 1043 * destroyed. Nothing special is usually required to ensure this 1044 * when called from Java (in which there will ordinarily be a live 1045 * reference to the thread) but this is not nearly-automatically 1046 * so when calling from native code. 1047 * 1048 * @param thread the thread to unpark. 1049 */ 1050 @ForceInline 1051 public void unpark(Object thread) { 1052 theInternalUnsafe.unpark(thread); 1053 } 1054 1055 /** 1056 * Blocks current thread, returning when a balancing 1057 * {@code unpark} occurs, or a balancing {@code unpark} has 1058 * already occurred, or the thread is interrupted, or, if not 1059 * absolute and time is not zero, the given time nanoseconds have 1060 * elapsed, or if absolute, the given deadline in milliseconds 1061 * since Epoch has passed, or spuriously (i.e., returning for no 1062 * "reason"). Note: This operation is in the Unsafe class only 1063 * because {@code unpark} is, so it would be strange to place it 1064 * elsewhere. 1065 */ 1066 @ForceInline 1067 public void park(boolean isAbsolute, long time) { 1068 theInternalUnsafe.park(isAbsolute, time); 1069 } 1070 1071 /** 1072 * Gets the load average in the system run queue assigned 1073 * to the available processors averaged over various periods of time. 1074 * This method retrieves the given {@code nelem} samples and 1075 * assigns to the elements of the given {@code loadavg} array. 1076 * The system imposes a maximum of 3 samples, representing 1077 * averages over the last 1, 5, and 15 minutes, respectively. 1078 * 1079 * @param loadavg an array of double of size nelems 1080 * @param nelems the number of samples to be retrieved and 1081 * must be 1 to 3. 1082 * 1083 * @return the number of samples actually retrieved; or -1 1084 * if the load average is unobtainable. 1085 */ 1086 @ForceInline 1087 public int getLoadAverage(double[] loadavg, int nelems) { 1088 return theInternalUnsafe.getLoadAverage(loadavg, nelems); 1089 } 1090 1091 // The following contain CAS-based Java implementations used on 1092 // platforms not supporting native instructions 1093 1094 /** 1095 * Atomically adds the given value to the current value of a field 1096 * or array element within the given object {@code o} 1097 * at the given {@code offset}. 1098 * 1099 * @param o object/array to update the field/element in 1100 * @param offset field/element offset 1101 * @param delta the value to add 1102 * @return the previous value 1103 * @since 1.8 1104 */ 1105 @ForceInline 1106 public final int getAndAddInt(Object o, long offset, int delta) { 1107 return theInternalUnsafe.getAndAddInt(o, offset, delta); 1108 } 1109 1110 /** 1111 * Atomically adds the given value to the current value of a field 1112 * or array element within the given object {@code o} 1113 * at the given {@code offset}. 1114 * 1115 * @param o object/array to update the field/element in 1116 * @param offset field/element offset 1117 * @param delta the value to add 1118 * @return the previous value 1119 * @since 1.8 1120 */ 1121 @ForceInline 1122 public final long getAndAddLong(Object o, long offset, long delta) { 1123 return theInternalUnsafe.getAndAddLong(o, offset, delta); 1124 } 1125 1126 /** 1127 * Atomically exchanges the given value with the current value of 1128 * a field or array element within the given object {@code o} 1129 * at the given {@code offset}. 1130 * 1131 * @param o object/array to update the field/element in 1132 * @param offset field/element offset 1133 * @param newValue new value 1134 * @return the previous value 1135 * @since 1.8 1136 */ 1137 @ForceInline 1138 public final int getAndSetInt(Object o, long offset, int newValue) { 1139 return theInternalUnsafe.getAndSetInt(o, offset, newValue); 1140 } 1141 1142 /** 1143 * Atomically exchanges the given value with the current value of 1144 * a field or array element within the given object {@code o} 1145 * at the given {@code offset}. 1146 * 1147 * @param o object/array to update the field/element in 1148 * @param offset field/element offset 1149 * @param newValue new value 1150 * @return the previous value 1151 * @since 1.8 1152 */ 1153 @ForceInline 1154 public final long getAndSetLong(Object o, long offset, long newValue) { 1155 return theInternalUnsafe.getAndSetLong(o, offset, newValue); 1156 } 1157 1158 /** 1159 * Atomically exchanges the given reference value with the current 1160 * reference value of a field or array element within the given 1161 * object {@code o} at the given {@code offset}. 1162 * 1163 * @param o object/array to update the field/element in 1164 * @param offset field/element offset 1165 * @param newValue new value 1166 * @return the previous value 1167 * @since 1.8 1168 */ 1169 @ForceInline 1170 public final Object getAndSetObject(Object o, long offset, Object newValue) { 1171 return theInternalUnsafe.getAndSetReference(o, offset, newValue); 1172 } 1173 1174 1175 /** 1176 * Ensures that loads before the fence will not be reordered with loads and 1177 * stores after the fence; a "LoadLoad plus LoadStore barrier". 1178 * 1179 * Corresponds to C11 atomic_thread_fence(memory_order_acquire) 1180 * (an "acquire fence"). 1181 * 1182 * A pure LoadLoad fence is not provided, since the addition of LoadStore 1183 * is almost always desired, and most current hardware instructions that 1184 * provide a LoadLoad barrier also provide a LoadStore barrier for free. 1185 * @since 1.8 1186 */ 1187 @ForceInline 1188 public void loadFence() { 1189 theInternalUnsafe.loadFence(); 1190 } 1191 1192 /** 1193 * Ensures that loads and stores before the fence will not be reordered with 1194 * stores after the fence; a "StoreStore plus LoadStore barrier". 1195 * 1196 * Corresponds to C11 atomic_thread_fence(memory_order_release) 1197 * (a "release fence"). 1198 * 1199 * A pure StoreStore fence is not provided, since the addition of LoadStore 1200 * is almost always desired, and most current hardware instructions that 1201 * provide a StoreStore barrier also provide a LoadStore barrier for free. 1202 * @since 1.8 1203 */ 1204 @ForceInline 1205 public void storeFence() { 1206 theInternalUnsafe.storeFence(); 1207 } 1208 1209 /** 1210 * Ensures that loads and stores before the fence will not be reordered 1211 * with loads and stores after the fence. Implies the effects of both 1212 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad 1213 * barrier. 1214 * 1215 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst). 1216 * @since 1.8 1217 */ 1218 @ForceInline 1219 public void fullFence() { 1220 theInternalUnsafe.fullFence(); 1221 } 1222 1223 /** 1224 * Invokes the given direct byte buffer's cleaner, if any. 1225 * 1226 * @param directBuffer a direct byte buffer 1227 * @throws NullPointerException if {@code directBuffer} is null 1228 * @throws IllegalArgumentException if {@code directBuffer} is non-direct, 1229 * or is a {@link java.nio.Buffer#slice slice}, or is a 1230 * {@link java.nio.Buffer#duplicate duplicate} 1231 * @since 9 1232 */ 1233 public void invokeCleaner(java.nio.ByteBuffer directBuffer) { 1234 if (!directBuffer.isDirect()) 1235 throw new IllegalArgumentException("buffer is non-direct"); 1236 1237 DirectBuffer db = (DirectBuffer)directBuffer; 1238 if (db.attachment() != null) 1239 throw new IllegalArgumentException("duplicate or slice"); 1240 1241 Cleaner cleaner = db.cleaner(); 1242 if (cleaner != null) { 1243 cleaner.clean(); 1244 } 1245 } 1246 }