PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/com/jogamp/common/os/Platform.java

https://github.com/carlo-salinari/gluegen
Java | 272 lines | 158 code | 40 blank | 74 comment | 33 complexity | 7750a1a1c0066f085256acf87ee2bd09 MD5 | raw file
  1. /**
  2. * Copyright 2010 JogAmp Community. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification, are
  5. * permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of
  8. * conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  11. * of conditions and the following disclaimer in the documentation and/or other materials
  12. * provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * The views and conclusions contained in the software and documentation are those of the
  25. * authors and should not be interpreted as representing official policies, either expressed
  26. * or implied, of JogAmp Community.
  27. */
  28. /*
  29. * Created on Sunday, March 28 2010 14:43
  30. */
  31. package com.jogamp.common.os;
  32. import com.jogamp.common.nio.Buffers;
  33. import java.nio.ByteBuffer;
  34. import java.nio.IntBuffer;
  35. import java.nio.ShortBuffer;
  36. import java.security.AccessController;
  37. import java.security.PrivilegedAction;
  38. /**
  39. * Utility class for querying platform specific properties.
  40. * @author Michael Bien
  41. * @author Sven Gothel
  42. */
  43. public class Platform {
  44. public static final boolean JAVA_SE;
  45. public static final boolean LITTLE_ENDIAN;
  46. public static final String OS;
  47. public static final String OS_VERSION;
  48. public static final String ARCH;
  49. public static final String JAVA_VENDOR;
  50. public static final String JAVA_VENDOR_URL;
  51. public static final String JAVA_VERSION;
  52. public static final String NEWLINE;
  53. private static final boolean is32Bit;
  54. private static final int pointerSizeInBits;
  55. private static final int pageSize;
  56. static {
  57. // We don't seem to need an AccessController.doPrivileged() block
  58. // here as these system properties are visible even to unsigned
  59. // applets
  60. OS = System.getProperty("os.name");
  61. OS_VERSION = System.getProperty("os.version");
  62. ARCH = System.getProperty("os.arch");
  63. JAVA_VENDOR = System.getProperty("java.vendor");
  64. JAVA_VENDOR_URL = System.getProperty("java.vendor.url");
  65. JAVA_VERSION = System.getProperty("java.version");
  66. NEWLINE = System.getProperty("line.separator");
  67. JAVA_SE = initIsJavaSE();
  68. LITTLE_ENDIAN = initByteOrder();
  69. boolean libsLoaded = true;
  70. try{
  71. NativeLibrary.ensureNativeLibLoaded();
  72. }catch (UnsatisfiedLinkError err){
  73. libsLoaded = false;
  74. }
  75. if(libsLoaded) {
  76. pointerSizeInBits = getPointerSizeInBitsImpl();
  77. final long pageSizeL = getPageSizeImpl();
  78. if(Integer.MAX_VALUE < pageSizeL) {
  79. throw new InternalError("PageSize exceeds integer value: " + pageSizeL);
  80. }
  81. pageSize = (int) pageSizeL ;
  82. }else{
  83. pointerSizeInBits = -1;
  84. pageSize = -1;
  85. }
  86. is32Bit = initArch();
  87. }
  88. private Platform() {}
  89. private static boolean initArch() throws RuntimeException {
  90. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) {
  91. return 32 == pointerSizeInBits;
  92. }else {
  93. String os_lc = OS.toLowerCase();
  94. String arch_lc = ARCH.toLowerCase();
  95. if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) ||
  96. (os_lc.startsWith("windows") && arch_lc.equals("arm")) ||
  97. (os_lc.startsWith("linux") && arch_lc.equals("i386")) ||
  98. (os_lc.startsWith("linux") && arch_lc.equals("x86")) ||
  99. (os_lc.startsWith("mac os") && arch_lc.equals("ppc")) ||
  100. (os_lc.startsWith("mac os") && arch_lc.equals("i386")) ||
  101. (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) ||
  102. (os_lc.startsWith("darwin") && arch_lc.equals("i386")) ||
  103. (os_lc.startsWith("sunos") && arch_lc.equals("sparc")) ||
  104. (os_lc.startsWith("sunos") && arch_lc.equals("x86")) ||
  105. (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) ||
  106. (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) {
  107. return true;
  108. } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) ||
  109. (os_lc.startsWith("linux") && arch_lc.equals("amd64")) ||
  110. (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) ||
  111. (os_lc.startsWith("linux") && arch_lc.equals("ia64")) ||
  112. (os_lc.startsWith("mac os") && arch_lc.equals("x86_64")) ||
  113. (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) ||
  114. (os_lc.startsWith("sunos") && arch_lc.equals("sparcv9")) ||
  115. (os_lc.startsWith("sunos") && arch_lc.equals("amd64"))) {
  116. return false;
  117. }else{
  118. throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")");
  119. }
  120. }
  121. }
  122. private static boolean initIsJavaSE() {
  123. // the fast path, check property Java SE instead of traversing through the ClassLoader
  124. String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() {
  125. public Object run() {
  126. return System.getProperty("java.runtime.name");
  127. }
  128. });
  129. if(java_runtime_name.indexOf("Java SE") != -1) {
  130. return true;
  131. }
  132. // probe for classes we need on a SE environment
  133. try {
  134. Class.forName("java.nio.LongBuffer");
  135. Class.forName("java.nio.DoubleBuffer");
  136. return true;
  137. } catch(ClassNotFoundException ex) {
  138. // continue with Java SE check
  139. }
  140. return false;
  141. }
  142. private static boolean initByteOrder() {
  143. ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
  144. IntBuffer tst_i = tst_b.asIntBuffer();
  145. ShortBuffer tst_s = tst_b.asShortBuffer();
  146. tst_i.put(0, 0x0A0B0C0D);
  147. return 0x0C0D == tst_s.get(0);
  148. }
  149. private static native int getPointerSizeInBitsImpl();
  150. private static native long getPageSizeImpl();
  151. /**
  152. * Returns true only if this program is running on the Java Standard Edition.
  153. */
  154. public static boolean isJavaSE() {
  155. return JAVA_SE;
  156. }
  157. /**
  158. * Returns true only if this system uses little endian byte ordering.
  159. */
  160. public static boolean isLittleEndian() {
  161. return LITTLE_ENDIAN;
  162. }
  163. /**
  164. * Returns the OS name.
  165. */
  166. public static String getOS() {
  167. return OS;
  168. }
  169. /**
  170. * Returns the OS version.
  171. */
  172. public static String getOSVersion() {
  173. return OS_VERSION;
  174. }
  175. /**
  176. * Returns the CPU architecture String.
  177. */
  178. public static String getArch() {
  179. return ARCH;
  180. }
  181. /**
  182. * Returns the JAVA.
  183. */
  184. public static String getJavaVendor() {
  185. return JAVA_VENDOR;
  186. }
  187. /**
  188. * Returns the JAVA vendor url.
  189. */
  190. public static String getJavaVendorURL() {
  191. return JAVA_VENDOR_URL;
  192. }
  193. /**
  194. * Returns the JAVA vendor.
  195. */
  196. public static String getJavaVersion() {
  197. return JAVA_VERSION;
  198. }
  199. /**
  200. * Returns the JAVA vendor.
  201. */
  202. public static String getNewline() {
  203. return NEWLINE;
  204. }
  205. /**
  206. * Returns true if this JVM is a 32bit JVM.
  207. */
  208. public static boolean is32Bit() {
  209. return is32Bit;
  210. }
  211. /**
  212. * Returns true if this JVM is a 64bit JVM.
  213. */
  214. public static boolean is64Bit() {
  215. return !is32Bit;
  216. }
  217. public static int getPointerSizeInBits() {
  218. return pointerSizeInBits;
  219. }
  220. public static int getPointerSizeInBytes() {
  221. return pointerSizeInBits/8;
  222. }
  223. public static int getPageSize() {
  224. return pageSize;
  225. }
  226. public static int getPageNumber(int size) {
  227. return ( size + ( pageSize - 1) ) / pageSize ; // integer arithmetic
  228. }
  229. public static int getPageAlignedSize(int size) {
  230. return getPageNumber(size) * pageSize;
  231. }
  232. }