< prev index next >

src/java.base/share/classes/jdk/internal/misc/Unsafe.java

Print this page




 748      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
 749      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
 750      * <li>Class: any java.lang.Class object
 751      * <li>String: any object (not just a java.lang.String)
 752      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
 753      * </ul>
 754      * @param hostClass context for linkage, access control, protection domain, and class loader
 755      * @param data      bytes of a class file
 756      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
 757      */
 758     public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
 759 
 760     /**
 761      * Allocates an instance but does not run any constructor.
 762      * Initializes the class if it has not yet been.
 763      */
 764     @HotSpotIntrinsicCandidate
 765     public native Object allocateInstance(Class<?> cls)
 766         throws InstantiationException;
 767 





















































 768     /** Throws the exception without telling the verifier. */
 769     public native void throwException(Throwable ee);
 770 
 771     /**
 772      * Atomically updates Java variable to {@code x} if it is currently
 773      * holding {@code expected}.
 774      *
 775      * <p>This operation has memory semantics of a {@code volatile} read
 776      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
 777      *
 778      * @return {@code true} if successful
 779      */
 780     @HotSpotIntrinsicCandidate
 781     public final native boolean compareAndSwapObject(Object o, long offset,
 782                                                      Object expected,
 783                                                      Object x);
 784 
 785     @HotSpotIntrinsicCandidate
 786     public final native Object compareAndExchangeObjectVolatile(Object o, long offset,
 787                                                                 Object expected,




 748      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
 749      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
 750      * <li>Class: any java.lang.Class object
 751      * <li>String: any object (not just a java.lang.String)
 752      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
 753      * </ul>
 754      * @param hostClass context for linkage, access control, protection domain, and class loader
 755      * @param data      bytes of a class file
 756      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
 757      */
 758     public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
 759 
 760     /**
 761      * Allocates an instance but does not run any constructor.
 762      * Initializes the class if it has not yet been.
 763      */
 764     @HotSpotIntrinsicCandidate
 765     public native Object allocateInstance(Class<?> cls)
 766         throws InstantiationException;
 767 
 768     /**
 769      * Allocates an array of a given type, but does not do zeroing.
 770      * <p>
 771      * This method should only be used in the very rare cases where a high-performance code
 772      * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
 773      * In an overwhelming majority of cases, a normal Java allocation should be used instead.
 774      * <p>
 775      * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
 776      * before allowing untrusted code, or code in other threads, to observe the reference
 777      * to the newly allocated array. In addition, the publication of the array reference must be
 778      * safe according to the Java Memory Model requirements.
 779      * <p>
 780      * The safest approach to deal with an uninitialized array is to keep the reference to it in local
 781      * variable at least until the initialization is complete, and then publish it <b>once</b>, either
 782      * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
 783      * or issuing a {@link #storeFence} before publishing the reference.
 784      * <p>
 785      * @implnote This method can only allocate primitive arrays, to avoid garbage reference
 786      * elements that could break heap integrity.
 787      *
 788      * @param componentType array component type to allocate
 789      * @param length array size to allocate
 790      * @throws IllegalArgumentException if component type is null, or not a primitive class;
 791      *                                  or the length is negative
 792      */
 793     public Object allocateUninitializedArray(Class<?> componentType, int length) {
 794        if (componentType == null) {
 795            throw new IllegalArgumentException("Component type is null");
 796        }
 797        if (!componentType.isPrimitive()) {
 798            throw new IllegalArgumentException("Component type is not primitive");
 799        }
 800        if (length < 0) {
 801            throw new IllegalArgumentException("Negative length");
 802        }
 803        return allocateUninitializedArray0(componentType, length);
 804     }
 805 
 806     @HotSpotIntrinsicCandidate
 807     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
 808        // These fallbacks provide zeroed arrays, but intrinsic is not required to
 809        // return the zeroed arrays.
 810        if (componentType == byte.class)    return new byte[length];
 811        if (componentType == boolean.class) return new boolean[length];
 812        if (componentType == short.class)   return new short[length];
 813        if (componentType == char.class)    return new char[length];
 814        if (componentType == int.class)     return new int[length];
 815        if (componentType == float.class)   return new float[length];
 816        if (componentType == long.class)    return new long[length];
 817        if (componentType == double.class)  return new double[length];
 818        return null;
 819     }
 820 
 821     /** Throws the exception without telling the verifier. */
 822     public native void throwException(Throwable ee);
 823 
 824     /**
 825      * Atomically updates Java variable to {@code x} if it is currently
 826      * holding {@code expected}.
 827      *
 828      * <p>This operation has memory semantics of a {@code volatile} read
 829      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
 830      *
 831      * @return {@code true} if successful
 832      */
 833     @HotSpotIntrinsicCandidate
 834     public final native boolean compareAndSwapObject(Object o, long offset,
 835                                                      Object expected,
 836                                                      Object x);
 837 
 838     @HotSpotIntrinsicCandidate
 839     public final native Object compareAndExchangeObjectVolatile(Object o, long offset,
 840                                                                 Object expected,


< prev index next >