[jffi-next] 41/49: Do not rely on environment variables for cpu detection - call down to the stub lib

Tim Potter tpot-guest at moszumanska.debian.org
Wed Mar 4 04:51:16 UTC 2015


This is an automated email from the git hooks/post-receive script.

tpot-guest pushed a commit to annotated tag upstream/1.0.10
in repository jffi-next.

commit c41e2a15e3d78ebd9e466c5d8554483d283dd4bc
Author: Wayne Meissner <wmeissner at gmail.com>
Date:   Thu Apr 28 01:35:11 2011 +1000

    Do not rely on environment variables for cpu detection - call down to the stub lib
---
 jni/jffi/Foreign.c               |  51 +++++++++++
 src/com/kenai/jffi/Foreign.java  |   1 +
 src/com/kenai/jffi/Platform.java | 184 ++++++++++++++++++++++-----------------
 3 files changed, 156 insertions(+), 80 deletions(-)

diff --git a/jni/jffi/Foreign.c b/jni/jffi/Foreign.c
index e3e160f..d6593a0 100644
--- a/jni/jffi/Foreign.c
+++ b/jni/jffi/Foreign.c
@@ -194,3 +194,54 @@ Java_com_kenai_jffi_Foreign_unregisterNatives(JNIEnv *env, jobject self, jclass
 {
     return (*env)->UnregisterNatives(env, clazz);
 }
+
+/*
+ * Determine the cpu type at compile time - useful for MacOSX where the jvm
+ * reports os.arch as 'universal'
+ */
+#if defined(__i386__) || defined(__i386)
+# define CPU "i386"
+
+#elif defined(__x86_64__) || defined(__x86_64) || defined(__amd64)
+# define CPU "x86_64"
+
+#elif defined(__ppc64__) || defined(__powerpc64__)
+# define CPU "ppc64"
+
+#elif defined(__ppc__) || defined(__powerpc__) || defined(__powerpc)
+# define CPU "ppc"
+
+/* Need to check for __sparcv9 first, because __sparc will be defined either way
+. */
+#elif defined(__sparcv9__) || defined(__sparcv9)
+# define CPU "sparcv9"
+
+#elif defined(__sparc__) || defined(__sparc)
+# define CPU "sparc"
+
+#elif defined(__arm__) || defined(__arm)
+# define CPU "arm"
+
+#elif defined(__ia64__) || defined(__ia64)
+# define CPU "ia64"
+
+#elif defined(__mips__) || defined(__mips)
+# define CPU "mips"
+
+#elif defined(__s390__)
+# define CPU "s390"
+
+#else
+# define CPU "unknown"
+#endif
+
+/*
+ * Class:     com_kenai_jffi_Foreign
+ * Method:    getArch
+ * Signature: ()Ljava/lang/String;
+ */
+JNIEXPORT jstring JNICALL 
+Java_com_kenai_jffi_Foreign_getArch(JNIEnv *env, jobject self)
+{
+    return (*env)->NewStringUTF(env, CPU);
+}
\ No newline at end of file
diff --git a/src/com/kenai/jffi/Foreign.java b/src/com/kenai/jffi/Foreign.java
index 7cf1c00..532757b 100644
--- a/src/com/kenai/jffi/Foreign.java
+++ b/src/com/kenai/jffi/Foreign.java
@@ -1423,5 +1423,6 @@ final class Foreign {
     final native int registerNatives(Class clazz, long methods,  int methodCount);
     final native int unregisterNatives(Class clazz);
     
+    final native String getArch();
     
 }
diff --git a/src/com/kenai/jffi/Platform.java b/src/com/kenai/jffi/Platform.java
index b518ed6..b8107cb 100644
--- a/src/com/kenai/jffi/Platform.java
+++ b/src/com/kenai/jffi/Platform.java
@@ -37,10 +37,6 @@ package com.kenai.jffi;
  */
 public abstract class Platform {
     private final OS os;
-    private final CPU cpu;
-    private final int addressSize;
-    private final long addressMask;
-    private final int longSize;
     private final int javaVersionMajor;
 
     /**
@@ -84,22 +80,29 @@ public abstract class Platform {
      */
     public enum CPU {
         /** Intel ia32 */
-        I386,
+        I386(32),
         /** AMD 64 bit (aka EM64T/X64) */
-        X86_64,
+        X86_64(64),
         /** Power PC 32 bit */
-        PPC,
+        PPC(32),
         /** Power PC 64 bit */
-        PPC64,
+        PPC64(64),
         /** Sun sparc 32 bit */
-        SPARC,
+        SPARC(32),
         /** Sun sparc 64 bit */
-        SPARCV9,
+        SPARCV9(64),
         /** IBM zSeries S/390 64 bit */
-        S390X,
+        S390X(64),
         /** Unknown CPU */
-        UNKNOWN;
+        UNKNOWN(64);
+        
+        CPU(int dataModel) {
+            this.dataModel = dataModel;
+            this.addressMask = dataModel == 32 ? 0xffffffffL : 0xffffffffffffffffL;
+        }
 
+        public final int dataModel;
+        public final long addressMask;
         @Override
         public String toString() { return name().toLowerCase(); }
     }
@@ -120,20 +123,27 @@ public abstract class Platform {
         String osName = System.getProperty("os.name").split(" ")[0].toLowerCase();
         if (osName.startsWith("mac") || osName.startsWith("darwin")) {
             return OS.DARWIN;
+        
         } else if (osName.startsWith("linux")) {
             return OS.LINUX;
+        
         } else if (osName.startsWith("sunos") || osName.startsWith("solaris")) {
             return OS.SOLARIS;
+        
         } else if (osName.startsWith("aix")) {
             return OS.AIX; 
+        
         } else if (osName.startsWith("openbsd")) {
             return OS.OPENBSD;
+        
         } else if (osName.startsWith("freebsd")) {
             return OS.FREEBSD;
+        
         } else if (osName.startsWith("windows")) {
             return OS.WINDOWS;
+        
         } else {
-            throw new ExceptionInInitializerError("Unsupported operating system");
+            return OS.UNKNOWN;
         }
     }
 
@@ -146,40 +156,73 @@ public abstract class Platform {
     private static final Platform determinePlatform(OS os) {
         switch (os) {
             case DARWIN:
-                return new Darwin();
+                return newDarwinPlatform();
+            
             case WINDOWS:
-                return new Windows();
-            case UNKNOWN:
-                throw new ExceptionInInitializerError("Unsupported operating system");
+                return newWindowsPlatform();
+            
             default:
-                return new Default(os);
+                return newDefaultPlatform(os);
         }
     }
+    
+    private static Platform newDarwinPlatform() {
+        return new Darwin();
+    }
+    
+    private static Platform newWindowsPlatform() {
+        return new Windows();
+    }
+    
+    private static Platform newDefaultPlatform(OS os) {
+        return new Default(os);
+    }
+    
+    
+    private static final class ArchHolder {
+        public static final CPU CPU = determineCPU();
+        
+        /**
+         * Determines the CPU architecture the JVM is running on.
+         *
+         * This normalizes all the variations that are equivalent (e.g. i386, x86, i86pc)
+         * into a common cpu type.
+         *
+         * @return A member of the <tt>CPU</tt> enum.
+         */
+        private static CPU determineCPU() {
+            String archString = null;
+            try {
+                archString = Foreign.getInstance().getArch().toLowerCase();
+            } catch (UnsatisfiedLinkError ex) {}
+            
+            if (archString == null || "unknown".equals(archString)) {
+                archString = System.getProperty("os.arch", "unknown").toLowerCase();
+            }
 
-    /**
-     * Determines the CPU architecture the JVM is running on.
-     *
-     * This normalizes all the variations that are equivalent (e.g. i386, x86, i86pc)
-     * into a common cpu type.
-     *
-     * @return A member of the <tt>CPU</tt> enum.
-     */
-    private static final CPU determineCPU() {
-        String archString = System.getProperty("os.arch", "unknown").toLowerCase();
-        if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
-            return CPU.I386;
-        } else if ("x86_64".equals(archString) || "amd64".equals(archString)) {
-            return CPU.X86_64;
-        } else if ("ppc".equals(archString) || "powerpc".equals(archString)) {
-            return CPU.PPC;
-        } else if ("powerpc64".equals(archString)) {
-            return CPU.PPC64;
-        }
-        // Try to find by lookup up in the CPU list
-        try {
-            return CPU.valueOf(archString.toUpperCase());
-        } catch (IllegalArgumentException ex) {
-            throw new ExceptionInInitializerError("Unsupported CPU architecture: " + archString);
+            if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
+                return CPU.I386;
+
+            } else if ("x86_64".equals(archString) || "amd64".equals(archString)) {
+                return CPU.X86_64;
+
+            } else if ("ppc".equals(archString) || "powerpc".equals(archString)) {
+                return CPU.PPC;
+
+            } else if ("ppc64".equals(archString) || "powerpc64".equals(archString)) {
+                return CPU.PPC64;
+            
+            } else if ("s390".equals(archString) || "s390x".equals(archString)) {
+                return CPU.S390X;
+            }
+
+            // Try to find by lookup up in the CPU list
+            try {
+                return CPU.valueOf(archString.toUpperCase());
+
+            } catch (IllegalArgumentException ex) {
+                return CPU.UNKNOWN;
+            }
         }
     }
 
@@ -190,36 +233,6 @@ public abstract class Platform {
      */
     private Platform(OS os) {
         this.os = os;
-        this.cpu = determineCPU();
-
-        
-        int dataModel = Integer.getInteger("sun.arch.data.model", 0);
-
-        //
-        // If we're running on a broken JVM that doesn't support the sun.arch.data.model property
-        // try to deduce the data model using the CPU type.
-        //
-        if (dataModel != 32 && dataModel != 64) {
-            switch (cpu) {
-                case I386:
-                case PPC:
-                case SPARC:
-                    dataModel = 32;
-                    break;
-                case X86_64:
-                case PPC64:
-                case SPARCV9:
-                case S390X:
-                    dataModel = 64;
-                    break;
-                default:
-                    throw new ExceptionInInitializerError("Cannot determine cpu address size");
-            }
-        }
-
-        addressSize = dataModel;
-        addressMask = addressSize == 32 ? 0xffffffffL : 0xffffffffffffffffL;
-        longSize = os == OS.WINDOWS ? 32 : addressSize;
 
         int version = 5;
         try {
@@ -229,8 +242,10 @@ public abstract class Platform {
                 version = Integer.valueOf(v[1]);
             }
         } catch (Exception ex) {
-            throw new ExceptionInInitializerError("Could not determine java version");
+            // Assume version 5 or above.
+            version = 5;
         }
+
         javaVersionMajor = version;
     }
     
@@ -258,7 +273,7 @@ public abstract class Platform {
      * @return A <tt>CPU</tt> value representing the current processor architecture.
      */
     public final CPU getCPU() {
-        return cpu;
+        return ArchHolder.CPU;
     }
     
     /**
@@ -275,9 +290,7 @@ public abstract class Platform {
      *
      * @return the size of a long in bits
      */
-    public final int longSize() {
-        return longSize;
-    }
+    public abstract int longSize();
 
     /**
      * Gets the size of a C address/pointer on the native platform.
@@ -285,7 +298,7 @@ public abstract class Platform {
      * @return the size of a pointer in bits
      */
     public final int addressSize() {
-        return addressSize;
+        return getCPU().dataModel;
     }
 
     /**
@@ -294,7 +307,7 @@ public abstract class Platform {
      * @return the size of a pointer in bits
      */
     public final long addressMask() {
-        return addressMask;
+        return getCPU().addressMask;
     }
 
     /**
@@ -355,6 +368,9 @@ public abstract class Platform {
             super(os);
         }
         
+        public final int longSize() {
+            return getCPU().dataModel;
+        }        
     }
     /**
      * A {@link Platform} subclass representing the MacOS system.
@@ -384,13 +400,17 @@ public abstract class Platform {
         public String getName() {
             return "Darwin";
         }
+        
+        public final int longSize() {
+            return getCPU().dataModel;
+        }
 
     }
 
     /**
      * A {@link Platform} subclass representing the Windows system.
      */
-    private static class Windows extends Platform {
+    private static final class Windows extends Platform {
 
         public Windows() {
             super(OS.WINDOWS);
@@ -400,6 +420,10 @@ public abstract class Platform {
         public String getLibraryNamePattern() {
             return ".*\\.dll$";
         }
+        
+        public final int longSize() {
+            return 32;
+        }
     }
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jffi-next.git



More information about the pkg-java-commits mailing list