diff options
Diffstat (limited to 'src/java/com/jogamp/common/os/Platform.java')
-rw-r--r-- | src/java/com/jogamp/common/os/Platform.java | 256 |
1 files changed, 152 insertions, 104 deletions
diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 0775f37..680d5be 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -26,18 +26,13 @@ * or implied, of JogAmp Community. */ -/* - * Created on Sunday, March 28 2010 14:43 - */ package com.jogamp.common.os; -import com.jogamp.common.nio.Buffers; -import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; import java.security.AccessController; import java.security.PrivilegedAction; +import jogamp.common.os.MachineDescriptionRuntime; + /** * Utility class for querying platform specific properties. * @author Michael Bien @@ -46,97 +41,157 @@ import java.security.PrivilegedAction; public class Platform { public static final boolean JAVA_SE; - public static final boolean LITTLE_ENDIAN; public static final String OS; + public static final String OS_lower; public static final String OS_VERSION; public static final String ARCH; + public static final String ARCH_lower; public static final String JAVA_VENDOR; public static final String JAVA_VENDOR_URL; public static final String JAVA_VERSION; public static final String NEWLINE; + public enum OSType { + LINUX(0), FREEBSD(1), DALVIK(2), MACOS(3), SUNOS(4), HPUX(5), WINDOWS(6), OPENKODE(7); + + public final int id; + + OSType(int id){ + this.id = id; + } + } + public static final OSType OS_TYPE; + + public enum CPUType { + X86(0), IA(1), ARM(2), SPARC(3), PA_RISC(4), PPC(5); + + public final int id; + + CPUType(int id){ + this.id = id; + } + } + public static final CPUType CPU_TYPE; + + public enum CPUArch { + X86_32(0), X86_64(1), IA64(2), ARM_32(3), SPARC_32(4), SPARCV9_64(5), PA_RISC2_0(6), PPC(7); + + public final int id; + + CPUArch(int id){ + this.id = id; + } + } + public static final CPUArch CPU_ARCH; + private static final boolean is32Bit; - private static final int pointerSizeInBits; - private static final int pageSize; + private static final MachineDescription machineDescription; + static { // We don't seem to need an AccessController.doPrivileged() block // here as these system properties are visible even to unsigned // applets OS = System.getProperty("os.name"); + OS_lower = OS.toLowerCase(); OS_VERSION = System.getProperty("os.version"); ARCH = System.getProperty("os.arch"); + ARCH_lower = ARCH.toLowerCase(); JAVA_VENDOR = System.getProperty("java.vendor"); JAVA_VENDOR_URL = System.getProperty("java.vendor.url"); JAVA_VERSION = System.getProperty("java.version"); NEWLINE = System.getProperty("line.separator"); JAVA_SE = initIsJavaSE(); - LITTLE_ENDIAN = initByteOrder(); - boolean libsLoaded = true; - try{ - NativeLibrary.ensureNativeLibLoaded(); - }catch (UnsatisfiedLinkError err){ - libsLoaded = false; - } + if( ARCH_lower.equals("x86") || + ARCH_lower.equals("i386") || + ARCH_lower.equals("i486") || + ARCH_lower.equals("i586") || + ARCH_lower.equals("i686") ) { + CPU_ARCH = CPUArch.X86_32; + CPU_TYPE = CPUType.X86; + } else if( ARCH_lower.equals("x86_64") || + ARCH_lower.equals("amd64") ) { + CPU_ARCH = CPUArch.X86_64; + CPU_TYPE = CPUType.X86; + } else if( ARCH_lower.equals("ia64") ) { + CPU_ARCH = CPUArch.IA64; + CPU_TYPE = CPUType.IA; + } else if( ARCH_lower.equals("arm") ) { + CPU_ARCH = CPUArch.ARM_32; + CPU_TYPE = CPUType.ARM; + } else if( ARCH_lower.equals("sparc") ) { + CPU_ARCH = CPUArch.SPARC_32; + CPU_TYPE = CPUType.SPARC; + } else if( ARCH_lower.equals("sparcv9") ) { + CPU_ARCH = CPUArch.SPARCV9_64; + CPU_TYPE = CPUType.SPARC; + } else if( ARCH_lower.equals("pa_risc2.0") ) { + CPU_ARCH = CPUArch.PA_RISC2_0; + CPU_TYPE = CPUType.PA_RISC; + } else if( ARCH_lower.equals("ppc") ) { + CPU_ARCH = CPUArch.PPC; + CPU_TYPE = CPUType.PPC; + } else { + throw new RuntimeException("Please port CPU detection to your platform (" + OS_lower + "/" + ARCH_lower + ")"); + } + OS_TYPE = getOSTypeImpl(); - if(libsLoaded) { - pointerSizeInBits = getPointerSizeInBitsImpl(); - final long pageSizeL = getPageSizeImpl(); - if(Integer.MAX_VALUE < pageSizeL) { - throw new InternalError("PageSize exceeds integer value: " + pageSizeL); - } - pageSize = (int) pageSizeL ; - }else{ - pointerSizeInBits = -1; - pageSize = -1; - } - - is32Bit = initArch(); - + machineDescription = MachineDescriptionRuntime.getMachineDescription(getIs32BitByCPUArchImpl()); + is32Bit = machineDescription.is32Bit(); } private Platform() {} - private static boolean initArch() throws RuntimeException { - if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { - return 32 == pointerSizeInBits; - }else { - String os_lc = OS.toLowerCase(); - String arch_lc = ARCH.toLowerCase(); - - if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) || - (os_lc.startsWith("windows") && arch_lc.equals("arm")) || - (os_lc.startsWith("linux") && arch_lc.equals("i386")) || - (os_lc.startsWith("linux") && arch_lc.equals("x86")) || - (os_lc.startsWith("mac os") && arch_lc.equals("ppc")) || - (os_lc.startsWith("mac os") && arch_lc.equals("i386")) || - (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) || - (os_lc.startsWith("darwin") && arch_lc.equals("i386")) || - (os_lc.startsWith("sunos") && arch_lc.equals("sparc")) || - (os_lc.startsWith("sunos") && arch_lc.equals("x86")) || - (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) || - (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) { + private static boolean getIs32BitByCPUArchImpl() throws RuntimeException { + switch( CPU_ARCH ) { + case X86_32: + case ARM_32: + case SPARC_32: + case PPC: return true; - } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) || - (os_lc.startsWith("linux") && arch_lc.equals("amd64")) || - (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("linux") && arch_lc.equals("ia64")) || - (os_lc.startsWith("mac os") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("sunos") && arch_lc.equals("sparcv9")) || - (os_lc.startsWith("sunos") && arch_lc.equals("amd64"))) { + case X86_64: + case IA64: + case SPARCV9_64: + case PA_RISC2_0: return false; - }else{ - throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")"); - } + default: + throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + Platform.OS_lower + "/" + Platform.ARCH_lower + "("+Platform.CPU_ARCH+"))"); } } - + + private static OSType getOSTypeImpl() throws RuntimeException { + if ( OS_lower.startsWith("linux") ) { + return OSType.LINUX; + } + if ( OS_lower.startsWith("freebsd") ) { + return OSType.FREEBSD; + } + if ( OS_lower.startsWith("dalvik") ) { + return OSType.DALVIK; + } + if ( OS_lower.startsWith("mac os x") || + OS_lower.startsWith("darwin") ) { + return OSType.MACOS; + } + if ( OS_lower.startsWith("sunos") ) { + return OSType.SUNOS; + } + if ( OS_lower.startsWith("hp-ux") ) { + return OSType.HPUX; + } + if ( OS_lower.startsWith("windows") ) { + return OSType.WINDOWS; + } + if ( OS_lower.startsWith("kd") ) { + return OSType.OPENKODE; + } + throw new RuntimeException("Please port OS detection to your platform (" + OS_lower + "/" + ARCH_lower + ")"); + } + private static boolean initIsJavaSE() { - // the fast path, check property Java SE instead of traversing through the ClassLoader String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { @@ -159,18 +214,6 @@ public class Platform { return false; } - private static boolean initByteOrder() { - ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order - IntBuffer tst_i = tst_b.asIntBuffer(); - ShortBuffer tst_s = tst_b.asShortBuffer(); - tst_i.put(0, 0x0A0B0C0D); - return 0x0C0D == tst_s.get(0); - } - - private static native int getPointerSizeInBitsImpl(); - private static native long getPageSizeImpl(); - - /** * Returns true only if this program is running on the Java Standard Edition. */ @@ -179,13 +222,6 @@ public class Platform { } /** - * Returns true only if this system uses little endian byte ordering. - */ - public static boolean isLittleEndian() { - return LITTLE_ENDIAN; - } - - /** * Returns the OS name. */ public static String getOS() { @@ -208,6 +244,27 @@ public class Platform { } /** + * Returns the OS type. + */ + public static OSType getOSType() { + return OS_TYPE; + } + + /** + * Returns the CPU type. + */ + public static CPUType getCPUType() { + return CPU_TYPE; + } + + /** + * Returns the CPU architecture. + */ + public static CPUArch getCPUArch() { + return CPU_ARCH; + } + + /** * Returns the JAVA. */ public static String getJavaVendor() { @@ -236,37 +293,28 @@ public class Platform { } /** - * Returns true if this JVM is a 32bit JVM. + * Returns true if this JVM/ARCH is 32bit. + * <p>Shortcut to {@link #getMachineDescription()}.{@link MachineDescription#is32Bit() is32Bit()}</p> */ public static boolean is32Bit() { - return is32Bit; + // return Platform.machineDescription.is32Bit(); + return Platform.is32Bit; // used very often } /** - * Returns true if this JVM is a 64bit JVM. + * Returns true if this JVM/ARCH is 64bit. + * <p>Shortcut to {@link #getMachineDescription()}.{@link MachineDescription#is32Bit() is64Bit()}</p> */ public static boolean is64Bit() { - return !is32Bit; - } - - public static int getPointerSizeInBits() { - return pointerSizeInBits; + // return Platform.machineDescription.is64Bit(); + return !Platform.is32Bit; // used very often } - public static int getPointerSizeInBytes() { - return pointerSizeInBits/8; - } - - public static int getPageSize() { - return pageSize; - } - - public static int getPageNumber(int size) { - return ( size + ( pageSize - 1) ) / pageSize ; // integer arithmetic + /** + * Returns the MachineDescription of the running machine. + */ + public static MachineDescription getMachineDescription() { + return machineDescription; } - - public static int getPageAlignedSize(int size) { - return getPageNumber(size) * pageSize; - } } |