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
944 public void writebackMemory(long address, long length)
945 {
946 checkWritebackMemory(address, length);
947
948 // perform any required pre-writeback barrier
949 writebackPreSync0();
950
951 // write back one cache line at a time
952 long line = (address & CACHE_LINE_MASK);
953 long end = address + length;
954 while (line < end) {
955 writeback0(line);
956 line += CACHE_LINE_FLUSH_SIZE;
957 }
958
959 // perform any required post-writeback barrier
960 writebackPostSync0();
961 }
962
963 /**
964 * Validate the arguments to writebackMemory
965 *
966 * @throws RuntimeException if the arguments are invalid
967 * (<em>Note:</em> after optimization, invalid inputs may
968 * go undetected, which will lead to unpredictable
969 * behavior)
970 */
971 private void checkWritebackMemory(long address, long length)
972 {
973 checkNativeAddress(address);
974 checkSize(length);
975 }
976
977 /**
978 * The size of an L1 data cache line which will be a power of two.
979 */
980 private static final long CACHE_LINE_FLUSH_SIZE = (long)theUnsafe.dataCacheLineFlushSize0();
981 private static final long CACHE_LINE_MASK = ~(CACHE_LINE_FLUSH_SIZE - 1);
982
983 /**
984 * primitive operation forcing writeback of a single cache line.
985 *
986 * @param address
987 * the start address of the cache line to be written back
988 */
989 // native used to write back an individual cache line starting at
990 // the supplied address
991 @HotSpotIntrinsicCandidate
992 private native void writeback0(long address);
993 // native used to serialise writeback operations relative to
994 // preceding memory writes
995 @HotSpotIntrinsicCandidate
996 private native void writebackPreSync0();
997 // native used to serialise writeback operations relative to
998 // following memory writes
999 @HotSpotIntrinsicCandidate
1000 private native void writebackPostSync0();
1001
1002 /// random queries
1003
1004 /**
1005 * This constant differs from all results that will ever be returned from
1006 * {@link #staticFieldOffset}, {@link #objectFieldOffset},
1007 * or {@link #arrayBaseOffset}.
1008 */
1009 public static final int INVALID_FIELD_OFFSET = -1;
1010
1011 /**
1012 * Reports the location of a given field in the storage allocation of its
1013 * class. Do not expect to perform any sort of arithmetic on this offset;
1014 * it is just a cookie which is passed to the unsafe heap memory accessors.
1015 *
1016 * <p>Any given field will always have the same offset and base, and no
1017 * two distinct fields of the same class will ever have the same offset
1018 * and base.
1019 *
1020 * <p>As of 1.4.1, offsets for fields are represented as long values,
1021 * although the Sun JVM does not use the most significant 32 bits.
1236
1237 /**
1238 * Reports the size in bytes of a native pointer, as stored via {@link
1239 * #putAddress}. This value will be either 4 or 8. Note that the sizes of
1240 * other primitive types (as stored in native memory blocks) is determined
1241 * fully by their information content.
1242 */
1243 public int addressSize() {
1244 return ADDRESS_SIZE;
1245 }
1246
1247 /** The value of {@code addressSize()} */
1248 public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1249
1250 /**
1251 * Reports the size in bytes of a native memory page (whatever that is).
1252 * This value will always be a power of two.
1253 */
1254 public native int pageSize();
1255
1256 /// random trusted operations from JNI:
1257
1258 /**
1259 * Tells the VM to define a class, without security checks. By default, the
1260 * class loader and protection domain come from the caller's class.
1261 */
1262 public Class<?> defineClass(String name, byte[] b, int off, int len,
1263 ClassLoader loader,
1264 ProtectionDomain protectionDomain) {
1265 if (b == null) {
1266 throw new NullPointerException();
1267 }
1268 if (len < 0) {
1269 throw new ArrayIndexOutOfBoundsException();
1270 }
1271
1272 return defineClass0(name, b, off, len, loader, protectionDomain);
1273 }
1274
1275 public native Class<?> defineClass0(String name, byte[] b, int off, int len,
3775 private static long convEndian(boolean big, long n) { return big == BE ? n : Long.reverseBytes(n) ; }
3776
3777
3778
3779 private native long allocateMemory0(long bytes);
3780 private native long reallocateMemory0(long address, long bytes);
3781 private native void freeMemory0(long address);
3782 private native void setMemory0(Object o, long offset, long bytes, byte value);
3783 @HotSpotIntrinsicCandidate
3784 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3785 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3786 private native long objectFieldOffset0(Field f);
3787 private native long objectFieldOffset1(Class<?> c, String name);
3788 private native long staticFieldOffset0(Field f);
3789 private native Object staticFieldBase0(Field f);
3790 private native boolean shouldBeInitialized0(Class<?> c);
3791 private native void ensureClassInitialized0(Class<?> c);
3792 private native int arrayBaseOffset0(Class<?> arrayClass);
3793 private native int arrayIndexScale0(Class<?> arrayClass);
3794 private native int addressSize0();
3795 private native int dataCacheLineFlushSize0();
3796 private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3797 private native int getLoadAverage0(double[] loadavg, int nelems);
3798 private native boolean unalignedAccess0();
3799 private native boolean isBigEndian0();
3800 }
|