< prev index next >

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

Print this page

        

@@ -31,11 +31,10 @@
 import sun.nio.ch.DirectBuffer;
 
 import java.lang.reflect.Field;
 import java.security.ProtectionDomain;
 
-import static jdk.internal.misc.UnsafeConstants.*;
 
 /**
  * A collection of methods for performing low-level, unsafe operations.
  * Although the class and all methods are public, use of this class is
  * limited because only trusted code can obtain instances of it.

@@ -1165,17 +1164,18 @@
     public int addressSize() {
         return ADDRESS_SIZE;
     }
 
     /** The value of {@code addressSize()} */
-    public static final int ADDRESS_SIZE = ADDRESS_SIZE0;
+    public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
 
     /**
      * Reports the size in bytes of a native memory page (whatever that is).
      * This value will always be a power of two.
      */
-    public int pageSize() { return PAGE_SIZE; }
+    public native int pageSize();
+
 
     /// random trusted operations from JNI:
 
     /**
      * Tells the VM to define a class, without security checks.  By default, the

@@ -1415,11 +1415,11 @@
     public final byte compareAndExchangeByte(Object o, long offset,
                                              byte expected,
                                              byte x) {
         long wordOffset = offset & ~3;
         int shift = (int) (offset & 3) << 3;
-        if (BIG_ENDIAN) {
+        if (BE) {
             shift = 24 - shift;
         }
         int mask           = 0xFF << shift;
         int maskedExpected = (expected & 0xFF) << shift;
         int maskedX        = (x & 0xFF) << shift;

@@ -1489,11 +1489,11 @@
         if ((offset & 3) == 3) {
             throw new IllegalArgumentException("Update spans the word, not supported");
         }
         long wordOffset = offset & ~3;
         int shift = (int) (offset & 3) << 3;
-        if (BIG_ENDIAN) {
+        if (BE) {
             shift = 16 - shift;
         }
         int mask           = 0xFFFF << shift;
         int maskedExpected = (expected & 0xFFFF) << shift;
         int maskedX        = (x & 0xFFFF) << shift;

@@ -3352,18 +3352,18 @@
 
     /**
      * @return Returns true if the native byte ordering of this
      * platform is big-endian, false if it is little-endian.
      */
-    public final boolean isBigEndian() { return BIG_ENDIAN; }
+    public final boolean isBigEndian() { return BE; }
 
     /**
      * @return Returns true if this platform is capable of performing
      * accesses at addresses which are not aligned for the type of the
      * primitive type being accessed, false otherwise.
      */
-    public final boolean unalignedAccess() { return UNALIGNED_ACCESS; }
+    public final boolean unalignedAccess() { return unalignedAccess; }
 
     /**
      * Fetches a value at some byte offset into a given Java object.
      * More specifically, fetches a value within the given object
      * <code>o</code> at the given offset, or (if <code>o</code> is

@@ -3601,11 +3601,18 @@
     /** @see #putLongUnaligned(Object, long, long, boolean) */
     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
         putCharUnaligned(o, offset, convEndian(bigEndian, x));
     }
 
-    private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; }
+    // JVM interface methods
+    // BE is true iff the native endianness of this platform is big.
+    private static final boolean BE = theUnsafe.isBigEndian0();
+
+    // unalignedAccess is true iff this platform can perform unaligned accesses.
+    private static final boolean unalignedAccess = theUnsafe.unalignedAccess0();
+
+    private static int pickPos(int top, int pos) { return BE ? top - pos : pos; }
 
     // These methods construct integers from bytes.  The byte ordering
     // is the native endianness of this platform.
     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
         return ((toUnsignedLong(i0) << pickPos(56, 0))

@@ -3640,13 +3647,13 @@
     private static short makeShort(byte i0, byte i1) {
         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
                      | (toUnsignedInt(i1) << pickPos(8, 8)));
     }
 
-    private static byte  pick(byte  le, byte  be) { return BIG_ENDIAN ? be : le; }
-    private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; }
-    private static int   pick(int   le, int   be) { return BIG_ENDIAN ? be : le; }
+    private static byte  pick(byte  le, byte  be) { return BE ? be : le; }
+    private static short pick(short le, short be) { return BE ? be : le; }
+    private static int   pick(int   le, int   be) { return BE ? be : le; }
 
     // These methods write integers to memory from smaller parts
     // provided by their caller.  The ordering in which these parts
     // are written is the native endianness of this platform.
     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {

@@ -3690,14 +3697,14 @@
     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
     private static long toUnsignedLong(short n) { return n & 0xffffl; }
     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
 
     // Maybe byte-reverse an integer
-    private static char convEndian(boolean big, char n)   { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); }
-    private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n)    ; }
-    private static int convEndian(boolean big, int n)     { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n)  ; }
-    private static long convEndian(boolean big, long n)   { return big == BIG_ENDIAN ? n : Long.reverseBytes(n)     ; }
+    private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
+    private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
+    private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
+    private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
 
 
 
     private native long allocateMemory0(long bytes);
     private native long reallocateMemory0(long address, long bytes);

@@ -3712,12 +3719,15 @@
     private native Object staticFieldBase0(Field f);
     private native boolean shouldBeInitialized0(Class<?> c);
     private native void ensureClassInitialized0(Class<?> c);
     private native int arrayBaseOffset0(Class<?> arrayClass);
     private native int arrayIndexScale0(Class<?> arrayClass);
+    private native int addressSize0();
     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
     private native int getLoadAverage0(double[] loadavg, int nelems);
+    private native boolean unalignedAccess0();
+    private native boolean isBigEndian0();
 
 
     /**
      * Invokes the given direct byte buffer's cleaner, if any.
      *
< prev index next >