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