< prev index next >

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

Print this page




 901 
 902         if (address == 0) {
 903             return;
 904         }
 905 
 906         freeMemory0(address);
 907     }
 908 
 909     /**
 910      * Validate the arguments to freeMemory
 911      *
 912      * @throws RuntimeException if the arguments are invalid
 913      *         (<em>Note:</em> after optimization, invalid inputs may
 914      *         go undetected, which will lead to unpredictable
 915      *         behavior)
 916      */
 917     private void freeMemoryChecks(long address) {
 918         checkPointer(null, address);
 919     }
 920 



















































































 921     /// random queries
 922 
 923     /**
 924      * This constant differs from all results that will ever be returned from
 925      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 926      * or {@link #arrayBaseOffset}.
 927      */
 928     public static final int INVALID_FIELD_OFFSET = -1;
 929 
 930     /**
 931      * Reports the location of a given field in the storage allocation of its
 932      * class.  Do not expect to perform any sort of arithmetic on this offset;
 933      * it is just a cookie which is passed to the unsafe heap memory accessors.
 934      *
 935      * <p>Any given field will always have the same offset and base, and no
 936      * two distinct fields of the same class will ever have the same offset
 937      * and base.
 938      *
 939      * <p>As of 1.4.1, offsets for fields are represented as long values,
 940      * although the Sun JVM does not use the most significant 32 bits.


1155 
1156     /**
1157      * Reports the size in bytes of a native pointer, as stored via {@link
1158      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1159      * other primitive types (as stored in native memory blocks) is determined
1160      * fully by their information content.
1161      */
1162     public int addressSize() {
1163         return ADDRESS_SIZE;
1164     }
1165 
1166     /** The value of {@code addressSize()} */
1167     public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1168 
1169     /**
1170      * Reports the size in bytes of a native memory page (whatever that is).
1171      * This value will always be a power of two.
1172      */
1173     public native int pageSize();
1174 
1175 
1176     /// random trusted operations from JNI:
1177 
1178     /**
1179      * Tells the VM to define a class, without security checks.  By default, the
1180      * class loader and protection domain come from the caller's class.
1181      */
1182     public Class<?> defineClass(String name, byte[] b, int off, int len,
1183                                 ClassLoader loader,
1184                                 ProtectionDomain protectionDomain) {
1185         if (b == null) {
1186             throw new NullPointerException();
1187         }
1188         if (len < 0) {
1189             throw new ArrayIndexOutOfBoundsException();
1190         }
1191 
1192         return defineClass0(name, b, off, len, loader, protectionDomain);
1193     }
1194 
1195     public native Class<?> defineClass0(String name, byte[] b, int off, int len,


3695     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
3696 
3697 
3698 
3699     private native long allocateMemory0(long bytes);
3700     private native long reallocateMemory0(long address, long bytes);
3701     private native void freeMemory0(long address);
3702     private native void setMemory0(Object o, long offset, long bytes, byte value);
3703     @HotSpotIntrinsicCandidate
3704     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3705     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3706     private native long objectFieldOffset0(Field f);
3707     private native long objectFieldOffset1(Class<?> c, String name);
3708     private native long staticFieldOffset0(Field f);
3709     private native Object staticFieldBase0(Field f);
3710     private native boolean shouldBeInitialized0(Class<?> c);
3711     private native void ensureClassInitialized0(Class<?> c);
3712     private native int arrayBaseOffset0(Class<?> arrayClass);
3713     private native int arrayIndexScale0(Class<?> arrayClass);
3714     private native int addressSize0();

3715     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3716     private native int getLoadAverage0(double[] loadavg, int nelems);
3717     private native boolean unalignedAccess0();
3718     private native boolean isBigEndian0();
3719 }


 901 
 902         if (address == 0) {
 903             return;
 904         }
 905 
 906         freeMemory0(address);
 907     }
 908 
 909     /**
 910      * Validate the arguments to freeMemory
 911      *
 912      * @throws RuntimeException if the arguments are invalid
 913      *         (<em>Note:</em> after optimization, invalid inputs may
 914      *         go undetected, which will lead to unpredictable
 915      *         behavior)
 916      */
 917     private void freeMemoryChecks(long address) {
 918         checkPointer(null, address);
 919     }
 920 
 921     /**
 922      * ensure writeback of a specified virtual memory address range
 923      * from cache to physical memory. all bytes in the address range
 924      * are guaranteed to have been written back to physical memory on
 925      * return from this call i.e. subsequently executed store
 926      * instructions are guaranteed not to be visible before the
 927      * writeback is completed.
 928      *
 929      * @param address
 930      *        the lowest byte address that must be guaranteed written
 931      *        back to memory. bytes at lower addresses may also be
 932      *        written back.
 933      *
 934      * @param length
 935      *        the length in bytes of the region starting at address
 936      *        that must be guaranteed written back to memory.
 937      *        
 938      * @throws RuntimeException if the arguments are invalid
 939      *         (<em>Note:</em> after optimization, invalid inputs may
 940      *         go undetected, which will lead to unpredictable
 941      *         behavior)
 942      *
 943      * @since 12
 944      */
 945     
 946     public void writebackMemory(long address, long length)
 947     {
 948         checkWritebackMemory(address, length);
 949 
 950         // perform any required pre-writeback barrier
 951         writebackPreSync0();
 952 
 953         // write back one cache line at a time
 954         long line = (address & CACHE_LINE_MASK);
 955         long end = address + length;
 956         while (line < end) {
 957             writeback0(line);
 958             line += CACHE_LINE_FLUSH_SIZE;
 959         }
 960 
 961         // perform any required post-writeback barrier
 962         writebackPostSync0();
 963     }
 964 
 965     /**
 966      * Validate the arguments to writebackMemory
 967      *
 968      * @throws RuntimeException if the arguments are invalid
 969      *         (<em>Note:</em> after optimization, invalid inputs may
 970      *         go undetected, which will lead to unpredictable
 971      *         behavior)
 972      */
 973     private void checkWritebackMemory(long address, long length)
 974     {
 975         checkNativeAddress(address);
 976         checkSize(length);
 977     }
 978     
 979     /**
 980      * The size of an L1 data cache line which will be a power of two.
 981      */
 982     private static final long CACHE_LINE_FLUSH_SIZE = (long)theUnsafe.dataCacheLineFlushSize0();
 983     private static final long CACHE_LINE_MASK = ~(CACHE_LINE_FLUSH_SIZE - 1);
 984 
 985     /**
 986      * primitive operation forcing writeback of a single cache line.
 987      *
 988      * @param address
 989      *        the start address of the cache line to be written back
 990      */
 991     // native used to write back an individual cache line starting at
 992     // the supplied address
 993     @HotSpotIntrinsicCandidate
 994     private native void writeback0(long address);
 995     // native used to serialise writeback operations relative to
 996     // preceding memory writes
 997     @HotSpotIntrinsicCandidate
 998     private native void writebackPreSync0();
 999     // native used to serialise writeback operations relative to
1000     // following memory writes
1001     @HotSpotIntrinsicCandidate
1002     private native void writebackPostSync0();
1003     
1004     /// random queries
1005 
1006     /**
1007      * This constant differs from all results that will ever be returned from
1008      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1009      * or {@link #arrayBaseOffset}.
1010      */
1011     public static final int INVALID_FIELD_OFFSET = -1;
1012 
1013     /**
1014      * Reports the location of a given field in the storage allocation of its
1015      * class.  Do not expect to perform any sort of arithmetic on this offset;
1016      * it is just a cookie which is passed to the unsafe heap memory accessors.
1017      *
1018      * <p>Any given field will always have the same offset and base, and no
1019      * two distinct fields of the same class will ever have the same offset
1020      * and base.
1021      *
1022      * <p>As of 1.4.1, offsets for fields are represented as long values,
1023      * although the Sun JVM does not use the most significant 32 bits.


1238 
1239     /**
1240      * Reports the size in bytes of a native pointer, as stored via {@link
1241      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1242      * other primitive types (as stored in native memory blocks) is determined
1243      * fully by their information content.
1244      */
1245     public int addressSize() {
1246         return ADDRESS_SIZE;
1247     }
1248 
1249     /** The value of {@code addressSize()} */
1250     public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1251 
1252     /**
1253      * Reports the size in bytes of a native memory page (whatever that is).
1254      * This value will always be a power of two.
1255      */
1256     public native int pageSize();
1257 

1258     /// random trusted operations from JNI:
1259 
1260     /**
1261      * Tells the VM to define a class, without security checks.  By default, the
1262      * class loader and protection domain come from the caller's class.
1263      */
1264     public Class<?> defineClass(String name, byte[] b, int off, int len,
1265                                 ClassLoader loader,
1266                                 ProtectionDomain protectionDomain) {
1267         if (b == null) {
1268             throw new NullPointerException();
1269         }
1270         if (len < 0) {
1271             throw new ArrayIndexOutOfBoundsException();
1272         }
1273 
1274         return defineClass0(name, b, off, len, loader, protectionDomain);
1275     }
1276 
1277     public native Class<?> defineClass0(String name, byte[] b, int off, int len,


3777     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
3778 
3779 
3780 
3781     private native long allocateMemory0(long bytes);
3782     private native long reallocateMemory0(long address, long bytes);
3783     private native void freeMemory0(long address);
3784     private native void setMemory0(Object o, long offset, long bytes, byte value);
3785     @HotSpotIntrinsicCandidate
3786     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3787     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3788     private native long objectFieldOffset0(Field f);
3789     private native long objectFieldOffset1(Class<?> c, String name);
3790     private native long staticFieldOffset0(Field f);
3791     private native Object staticFieldBase0(Field f);
3792     private native boolean shouldBeInitialized0(Class<?> c);
3793     private native void ensureClassInitialized0(Class<?> c);
3794     private native int arrayBaseOffset0(Class<?> arrayClass);
3795     private native int arrayIndexScale0(Class<?> arrayClass);
3796     private native int addressSize0();
3797     private native int dataCacheLineFlushSize0();
3798     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3799     private native int getLoadAverage0(double[] loadavg, int nelems);
3800     private native boolean unalignedAccess0();
3801     private native boolean isBigEndian0();
3802 }
< prev index next >