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