8201494: Avoid early initialization of java.nio.Bits

Reviewed-by: rriggs, alanb
This commit is contained in:
Claes Redestad 2018-04-12 17:23:32 +02:00
parent d9440e4e39
commit 9ab38159e8
4 changed files with 12 additions and 17 deletions

View file

@ -65,25 +65,13 @@ class Bits { // package-private
private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final Unsafe UNSAFE = Unsafe.getUnsafe();
static Unsafe unsafe() {
return UNSAFE;
}
// -- Processor and memory-system properties -- // -- Processor and memory-system properties --
private static final ByteOrder BYTE_ORDER
= UNSAFE.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
static ByteOrder byteOrder() {
return BYTE_ORDER;
}
private static int PAGE_SIZE = -1; private static int PAGE_SIZE = -1;
static int pageSize() { static int pageSize() {
if (PAGE_SIZE == -1) if (PAGE_SIZE == -1)
PAGE_SIZE = unsafe().pageSize(); PAGE_SIZE = UNSAFE.pageSize();
return PAGE_SIZE; return PAGE_SIZE;
} }

View file

@ -183,7 +183,7 @@ import java.util.Spliterator;
public abstract class Buffer { public abstract class Buffer {
// Cached unsafe-access object // Cached unsafe-access object
static final Unsafe UNSAFE = Bits.unsafe(); static final Unsafe UNSAFE = Unsafe.getUnsafe();
/** /**
* The characteristics of Spliterators that traverse and split elements * The characteristics of Spliterators that traverse and split elements

View file

@ -25,6 +25,7 @@
package java.nio; package java.nio;
import jdk.internal.misc.Unsafe;
/** /**
* A typesafe enumeration for byte orders. * A typesafe enumeration for byte orders.
@ -57,6 +58,12 @@ public final class ByteOrder {
public static final ByteOrder LITTLE_ENDIAN public static final ByteOrder LITTLE_ENDIAN
= new ByteOrder("LITTLE_ENDIAN"); = new ByteOrder("LITTLE_ENDIAN");
// Retrieve the native byte order. It's used early during bootstrap, and
// must be initialized after BIG_ENDIAN and LITTLE_ENDIAN.
private static final ByteOrder NATIVE_ORDER
= Unsafe.getUnsafe().isBigEndian()
? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
/** /**
* Retrieves the native byte order of the underlying platform. * Retrieves the native byte order of the underlying platform.
* *
@ -69,7 +76,7 @@ public final class ByteOrder {
* virtual machine is running * virtual machine is running
*/ */
public static ByteOrder nativeOrder() { public static ByteOrder nativeOrder() {
return Bits.byteOrder(); return NATIVE_ORDER;
} }
/** /**

View file

@ -1579,7 +1579,7 @@ public abstract class $Type$Buffer
boolean bigEndian // package-private boolean bigEndian // package-private
= true; = true;
boolean nativeByteOrder // package-private boolean nativeByteOrder // package-private
= (Bits.byteOrder() == ByteOrder.BIG_ENDIAN); = (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
/** /**
* Retrieves this buffer's byte order. * Retrieves this buffer's byte order.
@ -1608,7 +1608,7 @@ public abstract class $Type$Buffer
public final $Type$Buffer order(ByteOrder bo) { public final $Type$Buffer order(ByteOrder bo) {
bigEndian = (bo == ByteOrder.BIG_ENDIAN); bigEndian = (bo == ByteOrder.BIG_ENDIAN);
nativeByteOrder = nativeByteOrder =
(bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN)); (bigEndian == (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN));
return this; return this;
} }