From 710d86d31cd278583ee3d74b36595f4148a72133 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 5 Jun 2010 00:03:49 +0200 Subject: [PATCH 1/5] Minor additions to nio/Buffers, util/IntIntHashMap and os/NativeLibrary Buffers add 'float[] getFloatArray(double[])' conversion, ready to replace all JOGL InternalBufferUtil's. NativeLibrary/DynamicLinker add global lookup method allowing Unices and OSX to lookup a symbol globally. However, this is not recommended, due to the lookup costs. Windows is not supported here. Primitive type HashMap's (IntIntHashMap): Added putAll() --- src/java/com/jogamp/common/nio/Buffers.java | 8 ++++++++ src/java/com/jogamp/common/os/DynamicLinker.java | 1 + .../jogamp/common/os/MacOSXDynamicLinkerImpl.java | 6 ++++++ src/java/com/jogamp/common/os/NativeLibrary.java | 9 +++++++++ .../com/jogamp/common/os/UnixDynamicLinkerImpl.java | 5 +++++ .../jogamp/common/os/WindowsDynamicLinkerImpl.java | 4 ++++ src/java/com/jogamp/common/util/IntIntHashMap.java | 12 ++++++++++++ 7 files changed, 45 insertions(+) diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index e3bea171..0c16b4df 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -505,6 +505,14 @@ public static ByteBuffer copyShortBufferAsByteBuffer(ShortBuffer orig) { //---------------------------------------------------------------------- // Conversion routines // + + public static float[] getFloatArray(double[] source) { + int i=source.length; + float[] dest = new float[i--]; + while(i>=0) { dest[i]=(float)source[i]; i--; } + return dest; + } + public final static FloatBuffer getFloatBuffer(DoubleBuffer source) { source.rewind(); FloatBuffer dest = newDirectFloatBuffer(source.limit()); diff --git a/src/java/com/jogamp/common/os/DynamicLinker.java b/src/java/com/jogamp/common/os/DynamicLinker.java index ef5d5dc3..d67a38f1 100755 --- a/src/java/com/jogamp/common/os/DynamicLinker.java +++ b/src/java/com/jogamp/common/os/DynamicLinker.java @@ -46,5 +46,6 @@ interface DynamicLinker { public long openLibraryGlobal(String pathname, boolean debug); public long openLibraryLocal(String pathname, boolean debug); public long lookupSymbol(long libraryHandle, String symbolName); + public long lookupSymbolGlobal(String symbolName); public void closeLibrary(long libraryHandle); } diff --git a/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java index 531bc5c8..0e71d5d3 100755 --- a/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java +++ b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java @@ -5,6 +5,8 @@ public class MacOSXDynamicLinkerImpl implements DynamicLinker { + public static final long RTLD_DEFAULT = -2; + public static final int RTLD_LAZY = 0x1; public static final int RTLD_NOW = 0x2; public static final int RTLD_LOCAL = 0x4; @@ -50,6 +52,10 @@ public long lookupSymbol(long libraryHandle, String symbolName) { return dlsym(libraryHandle, symbolName); } + public long lookupSymbolGlobal(String symbolName) { + return dlsym(RTLD_DEFAULT, symbolName); + } + public void closeLibrary(long libraryHandle) { dlclose(libraryHandle); } diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java index 2de8bc94..897e2402 100755 --- a/src/java/com/jogamp/common/os/NativeLibrary.java +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -122,6 +122,10 @@ private NativeLibrary(long libraryHandle, String libraryPath) { this.libraryPath = libraryPath; } + public String toString() { + return "NativeLibrary[" + libraryPath + ", 0x" + Long.toHexString(libraryHandle) + "]"; + } + /** Opens the given native library, assuming it has the same base name on all platforms, looking first in the system's search path, and in the context of the specified ClassLoader, which is @@ -209,6 +213,11 @@ public long dynamicLookupFunction(String funcName) { return dynLink.lookupSymbol(libraryHandle, funcName); } + /** Looks up the given function name in all loaded libraries. */ + public static long dynamicLookupFunctionGlobal(String funcName) { + return dynLink.lookupSymbolGlobal(funcName); + } + /** Retrieves the low-level library handle from this NativeLibrary object. On the Windows platform this is an HMODULE, and on Unix and Mac OS X platforms the void* result of calling dlopen(). */ diff --git a/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java index 02bc8288..d2970d6f 100755 --- a/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java +++ b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java @@ -5,6 +5,7 @@ public class UnixDynamicLinkerImpl implements DynamicLinker { + public static final long RTLD_DEFAULT = 0; public static final int RTLD_LAZY = 0x00001; public static final int RTLD_NOW = 0x00002; public static final int RTLD_NOLOAD = 0x00004; @@ -55,6 +56,10 @@ public long openLibraryGlobal(String pathname, boolean debug) { public long lookupSymbol(long libraryHandle, String symbolName) { return dlsym(libraryHandle, symbolName); } + + public long lookupSymbolGlobal(String symbolName) { + return dlsym(RTLD_DEFAULT, symbolName); + } public void closeLibrary(long libraryHandle) { dlclose(libraryHandle); diff --git a/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java index 2858f743..935f386c 100755 --- a/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java +++ b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java @@ -65,6 +65,10 @@ public long lookupSymbol(long libraryHandle, String symbolName) { return addr; } + public long lookupSymbolGlobal(String symbolName) { + throw new RuntimeException("lookupSymbolGlobal: Not supported on Windows"); + } + public void closeLibrary(long libraryHandle) { FreeLibrary(libraryHandle); } diff --git a/src/java/com/jogamp/common/util/IntIntHashMap.java b/src/java/com/jogamp/common/util/IntIntHashMap.java index aa739b4f..9e2b3bc6 100644 --- a/src/java/com/jogamp/common/util/IntIntHashMap.java +++ b/src/java/com/jogamp/common/util/IntIntHashMap.java @@ -176,6 +176,18 @@ public boolean containsKey(/*key*/int/*key*/ key) { return keyNotFoundValue; } + /** + * Copies all of the mappings from the specified map to this map. + */ +// @SuppressWarnings(value="cast") + public void putAll(/*name*/IntIntHashMap/*name*/ source) { + Iterator itr = source.iterator(); + while(itr.hasNext()) { + Entry e = (Entry) itr.next(); + put(e.key, e.value); + } + } + /** * Removes the key-value mapping from this map. * Returns the previously mapped value or {@link #getKeyNotFoundValue} if no such mapping exists. From 555091d37a9a44fb7c35479ddecfee358a559e90 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 10 Jun 2010 06:13:06 +0200 Subject: [PATCH 2/5] Adding DynamicLibraryBundle utility to bundle Tool and JNI native library loading and lookup Add JNILibLoaderBase.loadLibrary(String libname, boolean ignoreError); DynamicLibraryBundle provides Tool and JNI native library loading and lookup New classes: com.jogamp.common.os.DynamicLibraryBundle com.jogamp.common.os.DynamicLibraryBundleInfo com.jogamp.common.util.MiscUtils.java Change: DEBUG/VERBOSE properties 'gluegen' -> 'jogamp' --- .../jogamp/common/jvm/JNILibLoaderBase.java | 89 +++-- .../common/os/DynamicLibraryBundle.java | 327 ++++++++++++++++++ .../common/os/DynamicLibraryBundleInfo.java | 75 ++++ .../com/jogamp/common/os/NativeLibrary.java | 6 +- .../common/os/WindowsDynamicLinkerImpl.java | 2 +- .../com/jogamp/common/util/MiscUtils.java | 57 +++ .../gluegen/runtime/ProcAddressTable.java | 7 +- 7 files changed, 522 insertions(+), 41 deletions(-) create mode 100755 src/java/com/jogamp/common/os/DynamicLibraryBundle.java create mode 100644 src/java/com/jogamp/common/os/DynamicLibraryBundleInfo.java create mode 100644 src/java/com/jogamp/common/util/MiscUtils.java diff --git a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java index be5b1def..8d26bf4e 100644 --- a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java +++ b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java @@ -45,7 +45,6 @@ import java.lang.reflect.Method; import java.security.AccessController; import java.security.AccessControlContext; -import java.security.PrivilegedAction; import java.util.HashSet; import com.jogamp.common.impl.Debug; @@ -55,43 +54,56 @@ public class JNILibLoaderBase { public interface LoaderAction { /** - * Loads the library specified by libname. Optionally preloads the libraries specified by - * preload. The implementation should ignore, if the preload-libraries have already been - * loaded. + * Loads the library specified by libname.
+ * The implementation should ignore, if the library has been loaded already.
+ * @param libname the library to load + * @param ignoreError if true, errors during loading the library should be ignored + * @return true if library loaded successful + */ + boolean loadLibrary(String libname, boolean ignoreError); + + /** + * Loads the library specified by libname.
+ * Optionally preloads the libraries specified by preload.
+ * The implementation should ignore, if any library has been loaded already.
* @param libname the library to load * @param preload the libraries to load before loading the main library if not null - * @param preloadIgnoreError true, if errors during loading the preload-libraries should be ignored + * @param preloadIgnoreError if true, errors during loading the preload-libraries should be ignored */ - void loadLibrary(String libname, String[] preload, - boolean preloadIgnoreError); + void loadLibrary(String libname, String[] preload, boolean preloadIgnoreError); } private static class DefaultAction implements LoaderAction { - public void loadLibrary(String libname, String[] preload, boolean preloadIgnoreError) { - if (null!=preload) { - for (int i=0; i + *
    + *
  • The to-be-glued native library, eg OpenGL32.dll. From hereon this is referred as the Tool.
  • + *
  • The JNI glue-code native library, eg jogl_desktop.dll. From heron this is referred as the Glue
  • + *

+ * An instance provides a complete {@link com.jogamp.common.os.DynamicLookupHelper} + * to {@link com.jogamp.gluegen.runtime.ProcAddressTable#reset(com.jogamp.common.os.DynamicLookupHelper lookup)} + * the {@link com.jogamp.gluegen.runtime.ProcAddressTable}.
+ * At construction, it loads the Tool native library, loads Glue native library via + * {@link com.jogamp.common.os.DynamicLibraryBundleInfo#loadJNILibrary()} + * and resolves the optional Tool's
GetProcAddress
using {@link com.jogamp.common.os.DynamicLibraryBundleInfo#getToolGetProcAddressFuncNameList()}. + */ +public class DynamicLibraryBundle implements DynamicLookupHelper { + protected static final boolean DEBUG = NativeLibrary.DEBUG; + protected static final boolean DEBUG_LOOKUP = NativeLibrary.DEBUG_LOOKUP; + + private DynamicLibraryBundleInfo info; + + private List/*>*/ toolLibNames; + private boolean[] toolLibLoaded; + private int toolLibLoadedNumber; + protected List/**/ nativeLibraries; + + private List/**/ glueLibNames; + private boolean[] glueLibLoaded; + private int glueLibLoadedNumber; + + private long toolGetProcAddressHandle; + private HashSet toolGetProcAddressFuncNameSet; + private List toolGetProcAddressFuncNameList; + + public DynamicLibraryBundle(DynamicLibraryBundleInfo info) { + if(null==info) { + throw new RuntimeException("Null DynamicLibraryBundleInfo"); + } + this.info = info; + if(DEBUG) { + System.out.println("DynamicLibraryBundle.init start with: "+info.getClass().getName()); + } + nativeLibraries = new ArrayList(); + toolLibNames = info.getToolLibNames(); + glueLibNames = info.getGlueLibNames(); + loadLibraries(); + toolGetProcAddressFuncNameList = info.getToolGetProcAddressFuncNameList(); + if(null!=toolGetProcAddressFuncNameList) { + toolGetProcAddressFuncNameSet = new HashSet(toolGetProcAddressFuncNameList); + toolGetProcAddressHandle = getToolGetProcAddressHandle(); + } else { + toolGetProcAddressFuncNameSet = new HashSet(); + toolGetProcAddressHandle = 0; + } + if(DEBUG) { + System.out.println("DynamicLibraryBundle.init Summary: "+info.getClass().getName()); + System.out.println(" toolGetProcAddressFuncNameList: "+MiscUtils.toString(toolGetProcAddressFuncNameList)); + System.out.println(" Tool Lib Names : "+MiscUtils.toString(toolLibNames)); + System.out.println(" Tool Lib Loaded: "+getToolLibLoadedNumber()+"/"+getToolLibNumber()+", complete "+isToolLibComplete()); + System.out.println(" Glue Lib Names : "+MiscUtils.toString(glueLibNames)); + System.out.println(" Glue Lib Loaded: "+getGlueLibLoadedNumber()+"/"+getGlueLibNumber()+", complete "+isGlueLibComplete()); + System.out.println(" All Complete: "+isLibComplete()); + } + } + + public final boolean isLibComplete() { + return isToolLibComplete() && isGlueLibComplete() ; + } + + public final int getToolLibNumber() { + return toolLibNames.size(); + } + + public final int getToolLibLoadedNumber() { + return toolLibLoadedNumber; + } + + public final boolean isToolLibComplete() { + return getToolLibNumber() == getToolLibLoadedNumber(); + } + + public final boolean isToolLibLoaded() { + return 0 < toolLibLoadedNumber; + } + + public final boolean isToolLibLoaded(int i) { + if(0 <= i && i < toolLibLoaded.length) { + return toolLibLoaded[i]; + } + return false; + } + + public final int getGlueLibNumber() { + return glueLibNames.size(); + } + + public final int getGlueLibLoadedNumber() { + return glueLibLoadedNumber; + } + + public final boolean isGlueLibComplete() { + return getGlueLibNumber() == getGlueLibLoadedNumber(); + } + + public final boolean isGlueLibLoaded(int i) { + if(0 <= i && i < glueLibLoaded.length) { + return glueLibLoaded[i]; + } + return false; + } + + public final DynamicLibraryBundleInfo getBundleInfo() { return info; } + + protected long getToolGetProcAddressHandle() { + if(!isToolLibLoaded()) { + return 0; + } + long aptr = 0; + for(Iterator iter=toolGetProcAddressFuncNameList.iterator(); 0==aptr && iter.hasNext(); ) { + String name = (String) iter.next(); + aptr = dynamicLookupFunctionOnLibs(name); + if(DEBUG) { + System.out.println("getToolGetProcAddressHandle: "+name+" -> 0x"+Long.toHexString(aptr)); + } + } + return aptr; + } + + protected NativeLibrary loadFirstAvailable(List/**/ libNames, ClassLoader loader, boolean global) { + for (Iterator iter = libNames.iterator(); iter.hasNext(); ) { + NativeLibrary lib = NativeLibrary.open((String) iter.next(), loader, global); + if (lib != null) { + return lib; + } + } + return null; + } + + private void loadLibraries() { + if( null == toolLibNames || toolLibNames.size() == 0) { + if(DEBUG) { + System.out.println("No Tool native library names given"); + } + return; + } + + if( null == glueLibNames || glueLibNames.size() == 0 ) { + if(DEBUG) { + System.out.println("No Glue native library names given"); + } + return; + } + + toolLibLoadedNumber = 0; + int i; + toolLibLoaded = new boolean[toolLibNames.size()]; + for(i=0; i*/ libNames = null; + if(listObj instanceof List) { + libNames = (List) listObj; + } else if(listObj instanceof String) { + libNames = new ArrayList(); + libNames.add((String)listObj); + } else { + throw new RuntimeException("List element "+i+" must be either a List or String: "+MiscUtils.toString(toolLibNames)); + } + if( null != libNames && libNames.size() > 0 ) { + lib = loadFirstAvailable(libNames, loader, info.shallLinkGlobal()); + if ( null == lib ) { + if(DEBUG) { + System.out.println("Unable to load any Tool library of: "+MiscUtils.toString(libNames)); + } + } else { + nativeLibraries.add(lib); + toolLibLoaded[i]=true; + toolLibLoadedNumber++; + if(DEBUG) { + System.out.println("Loaded Tool library: "+lib); + } + } + } + } + if( !isToolLibLoaded() ) { + if(DEBUG) { + System.out.println("No Tool libraries loaded"); + } + return; + } + + glueLibLoadedNumber = 0; + i=0; + for(Iterator iter = glueLibNames.iterator(); iter.hasNext(); i++) { + String libName = (String) iter.next(); + boolean ignoreError = true; + boolean res; + try { + res = GlueJNILibLoaderBase.loadLibrary(libName, ignoreError); + if(DEBUG && !res) { + System.out.println("Info: Could not load JNI/Glue library: "+libName); + } + } catch (UnsatisfiedLinkError e) { + res = false; + if(DEBUG) { + System.out.println("Unable to load JNI/Glue library: "+libName); + e.printStackTrace(); + } + } + glueLibLoaded[i] = res; + if(res) { + glueLibLoadedNumber++; + } + } + } + + private long dynamicLookupFunctionOnLibs(String funcName) { + if(!isToolLibLoaded() || null==funcName) { + if(DEBUG_LOOKUP && !isToolLibLoaded()) { + System.err.println("Lookup-Native: <" + funcName + "> ** FAILED ** Tool native library not loaded"); + } + return 0; + } + long addr = 0; + NativeLibrary lib = null; + + if(info.shallLookupGlobal()) { + // Try a global symbol lookup first .. + addr = NativeLibrary.dynamicLookupFunctionGlobal(funcName); + } + // Look up this function name in all known libraries + for (Iterator iter = nativeLibraries.iterator(); 0==addr && iter.hasNext(); ) { + lib = (NativeLibrary) iter.next(); + addr = lib.dynamicLookupFunction(funcName); + } + if(DEBUG_LOOKUP) { + String libName = ( null == lib ) ? "GLOBAL" : lib.toString(); + if(0!=addr) { + System.err.println("Lookup-Native: <" + funcName + "> 0x" + Long.toHexString(addr) + " in lib " + libName ); + } else { + System.err.println("Lookup-Native: <" + funcName + "> ** FAILED ** in libs " + MiscUtils.toString(nativeLibraries)); + } + } + return addr; + } + + public long dynamicLookupFunction(String funcName) { + if(!isToolLibLoaded() || null==funcName) { + if(DEBUG_LOOKUP && !isToolLibLoaded()) { + System.err.println("Lookup: <" + funcName + "> ** FAILED ** Tool native library not loaded"); + } + return 0; + } + + if(toolGetProcAddressFuncNameSet.contains(funcName)) { + return toolGetProcAddressHandle; + } + + long addr = 0; + + if(0 != toolGetProcAddressHandle) { + addr = info.toolDynamicLookupFunction(toolGetProcAddressHandle, funcName); + if(DEBUG_LOOKUP) { + if(0!=addr) { + System.err.println("Lookup-Tool: <"+funcName+"> 0x"+Long.toHexString(addr)); + } + } + } + if(0==addr) { + addr = dynamicLookupFunctionOnLibs(funcName); + } + return addr; + } + + /** Inherit access */ + static class GlueJNILibLoaderBase extends JNILibLoaderBase { + protected static synchronized boolean loadLibrary(String libname, boolean ignoreError) { + return JNILibLoaderBase.loadLibrary(libname, ignoreError); + } + } +} + diff --git a/src/java/com/jogamp/common/os/DynamicLibraryBundleInfo.java b/src/java/com/jogamp/common/os/DynamicLibraryBundleInfo.java new file mode 100644 index 00000000..37ca5382 --- /dev/null +++ b/src/java/com/jogamp/common/os/DynamicLibraryBundleInfo.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2010, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Sven Gothel nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Sven Gothel BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.jogamp.common.os; + +import java.util.*; +import java.security.*; + +public interface DynamicLibraryBundleInfo { + public static final boolean DEBUG = DynamicLibraryBundle.DEBUG; + + /** @return a list of Tool library names or alternative library name lists.
+ *
    + *
  • GL/GLU example Unix: [ [ "libGL.so.1", "libGL.so", "GL" ], [ "libGLU.so", "GLU" ] ]
  • + *
  • GL/GLU example Windows: [ "OpenGL32", "GLU32" ]
  • + *
  • Cg/CgGL example: [ [ "libCg.so", "Cg" ], [ "libCgGL.so", "CgGL" ] ]
  • + * + */ + public List getToolLibNames(); + + /** @return a list of Glue library names.
    + *
      + *
    • GL: [ "nativewindow_x11", "jogl_gl2es12", "jogl_desktop" ]
    • + *
    • NEWT: [ "nativewindow_x11", "newt" ]
    • + *
    • Cg: [ "nativewindow_x11", "jogl_cg" ]
    • + *

    + * Only the last entry is crucial, ie all other are optional preload dependencies and may generate errors, + * which are ignored. + */ + public List/**/ getGlueLibNames(); + + /** May return the native libraries
    GetProcAddressFunc
    names, the first found function is being used.
    + * This could be eg:
     glXGetProcAddressARB, glXGetProcAddressARB 
    .
    + * If your Tool does not has this facility, just return null. + */ + public List getToolGetProcAddressFuncNameList() ; + + /** May implement the lookup function using the Tools facility.
    + * The actual function pointer is provided to allow proper bootstrapping of the ProcAddressTable.
    + */ + public long toolDynamicLookupFunction(long toolGetProcAddressHandle, String funcName); + + /** @return true if the native library symbols shall be made available for symbol resolution of subsequently loaded libraries. */ + public boolean shallLinkGlobal(); + + /** @return true if the dynamic symbol lookup shall happen system wide, over all loaded libraries. Otherwise only the loaded native libraries are used for lookup, which shall be the default. */ + public boolean shallLookupGlobal(); + +} + + diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java index 897e2402..97aabae6 100755 --- a/src/java/com/jogamp/common/os/NativeLibrary.java +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -62,7 +62,8 @@ public class NativeLibrary implements DynamicLookupHelper { private static final int WINDOWS = 1; private static final int UNIX = 2; private static final int MACOSX = 3; - private static boolean DEBUG; + protected static boolean DEBUG; + protected static boolean DEBUG_LOOKUP; private static int platform; private static DynamicLinker dynLink; private static String[] prefixes; @@ -81,7 +82,8 @@ public Object run() { platform = UNIX; } - DEBUG = (System.getProperty("gluegen.debug.NativeLibrary") != null); + DEBUG = (System.getProperty("jogamp.debug.NativeLibrary") != null); + DEBUG_LOOKUP = (System.getProperty("jogamp.debug.NativeLibrary.Lookup") != null); return null; } diff --git a/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java index 935f386c..7bbfe233 100755 --- a/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java +++ b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java @@ -11,7 +11,7 @@ public class WindowsDynamicLinkerImpl implements DynamicLinker { static { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - DEBUG = (System.getProperty("gluegen.debug.NativeLibrary") != null); + DEBUG = (System.getProperty("jogamp.debug.NativeLibrary") != null); return null; } }); diff --git a/src/java/com/jogamp/common/util/MiscUtils.java b/src/java/com/jogamp/common/util/MiscUtils.java new file mode 100644 index 00000000..dd32951e --- /dev/null +++ b/src/java/com/jogamp/common/util/MiscUtils.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2010, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Sven Gothel nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Sven Gothel BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.jogamp.common.util; + +import java.util.*; + +public class MiscUtils { + + public static StringBuffer append(StringBuffer sb, List list) { + sb.append("["); + if(null!=list&&list.size()>0) { + boolean comma = false; + for(Iterator iter=list.iterator(); iter.hasNext(); ) { + if(comma) { sb.append(", "); } + Object o = iter.next(); + if(o instanceof List) { + sb = append(sb, (List)o); + } else { + sb.append(o); + } + comma = true; + } + } + sb.append("]"); + return sb; + } + + public static String toString(List list) { + return append(new StringBuffer(), list).toString(); + } +} + diff --git a/src/java/com/jogamp/gluegen/runtime/ProcAddressTable.java b/src/java/com/jogamp/gluegen/runtime/ProcAddressTable.java index 9a05fce5..f0be20f9 100644 --- a/src/java/com/jogamp/gluegen/runtime/ProcAddressTable.java +++ b/src/java/com/jogamp/gluegen/runtime/ProcAddressTable.java @@ -65,9 +65,9 @@ public abstract class ProcAddressTable { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - DEBUG = (System.getProperty("gluegen.debug.ProcAddressHelper") != null); + DEBUG = (System.getProperty("jogamp.debug.ProcAddressHelper") != null); if (DEBUG) { - DEBUG_PREFIX = System.getProperty("gluegen.debug.ProcAddressHelper.prefix"); + DEBUG_PREFIX = System.getProperty("jogamp.debug.ProcAddressHelper.prefix"); } return null; } @@ -90,6 +90,9 @@ public ProcAddressTable(FunctionAddressResolver resolver) { public void reset(DynamicLookupHelper lookup) throws RuntimeException { + if(null==lookup) { + throw new RuntimeException("Passed null DynamicLookupHelper"); + } Class tableClass = getClass(); Field[] fields = tableClass.getFields(); From 57571086dabbaccebac3dcf8dc3cf41841bd4159 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 10 Jun 2010 17:12:52 +0200 Subject: [PATCH 3/5] refactored Platform. Simplified initializer. --- src/java/com/jogamp/common/os/Platform.java | 76 ++++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index b6298a70..0d8b2769 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -34,20 +34,21 @@ import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; -import java.security.*; /** * Utility class for querying platform specific properties. - * @author Michael Bien, Sven Gothel + * @author Michael Bien + * @author Sven Gothel */ public class Platform { public static final boolean JAVA_SE; public static final boolean LITTLE_ENDIAN; + public static final String OS; + public static final String ARCH; - private final static boolean is32Bit; - private final static int pointerSizeInBits; - private final static String os, arch; + private static final boolean is32Bit; + private static final int pointerSizeInBits; static { NativeLibrary.ensureNativeLibLoaded(); @@ -55,17 +56,24 @@ public class Platform { // 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"); - arch = System.getProperty("os.arch"); + OS = System.getProperty("os.name"); + ARCH = System.getProperty("os.arch"); pointerSizeInBits = getPointerSizeInBitsImpl(); + is32Bit = initAch(); + JAVA_SE = initIsJavaSE(); + LITTLE_ENDIAN = initByteOrder(); + } + + private Platform() {} - // Try to use Sun's sun.arch.data.model first .. + private static boolean initAch() throws RuntimeException { + // Try to use Sun's sun.ARCH.data.model first .. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { - is32Bit = ( 32 == pointerSizeInBits ); + return 32 == pointerSizeInBits; }else { - String os_lc = os.toLowerCase(); - String arch_lc = arch.toLowerCase(); + 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")) || @@ -79,7 +87,7 @@ public class Platform { (os_lc.startsWith("sunos_lc") && arch_lc.equals("x86")) || (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) || (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) { - is32Bit = true; + 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")) || @@ -88,44 +96,41 @@ public class Platform { (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) || (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparcv9")) || (os_lc.startsWith("sunos_lc") && arch_lc.equals("amd64"))) { - is32Bit = false; + return false; }else{ throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")"); } } + } + + private static boolean initIsJavaSE() { + + // fast path for desktop + if(System.getSecurityManager() == null && System.getProperty("java.runtime.name").indexOf("Java SE") != -1) { + return true; + } - boolean se; + // probe for classes we need on a SE environment try{ Class.forName("java.nio.LongBuffer"); Class.forName("java.nio.DoubleBuffer"); - se = true; + return true; }catch(ClassNotFoundException ex) { - se = false; + return false; } + } - if(!se) { - // no more the fast path, due to access controller .. - String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return System.getProperty("java.runtime.name"); - } - }); - se = java_runtime_name.indexOf("Java SE") != -1; - } - JAVA_SE = se; - - // byte order + 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); - LITTLE_ENDIAN = 0x0C0D == tst_s.get(0); + return 0x0C0D == tst_s.get(0); } - private Platform() {} - private static native int getPointerSizeInBitsImpl(); + /** * Returns true only if this program is running on the Java Standard Edition. */ @@ -144,14 +149,14 @@ public static boolean isLittleEndian() { * Returns the OS name. */ public static String getOS() { - return os; + return OS; } /** * Returns the CPU architecture String. */ public static String getArch() { - return arch; + return ARCH; } /** @@ -165,6 +170,9 @@ public static int getPointerSizeInBits() { return pointerSizeInBits; } -} + public static int getPointerSizeInBytes() { + return pointerSizeInBits/8; + } +} From c6748a9901ed4bb0de1d4f844637beb76477af65 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 10 Jun 2010 17:17:58 +0200 Subject: [PATCH 4/5] currently no need for a (public) MiscUtils.java. --- .../common/os/DynamicLibraryBundle.java | 14 ++--- .../com/jogamp/common/util/MiscUtils.java | 57 ------------------- 2 files changed, 6 insertions(+), 65 deletions(-) delete mode 100644 src/java/com/jogamp/common/util/MiscUtils.java diff --git a/src/java/com/jogamp/common/os/DynamicLibraryBundle.java b/src/java/com/jogamp/common/os/DynamicLibraryBundle.java index 8e138ef6..6c00afee 100755 --- a/src/java/com/jogamp/common/os/DynamicLibraryBundle.java +++ b/src/java/com/jogamp/common/os/DynamicLibraryBundle.java @@ -28,10 +28,8 @@ package com.jogamp.common.os; import java.util.*; -import java.security.*; import com.jogamp.common.jvm.JNILibLoaderBase; -import com.jogamp.common.util.MiscUtils; /** * Provides bundling of:
    @@ -87,10 +85,10 @@ public DynamicLibraryBundle(DynamicLibraryBundleInfo info) { } if(DEBUG) { System.out.println("DynamicLibraryBundle.init Summary: "+info.getClass().getName()); - System.out.println(" toolGetProcAddressFuncNameList: "+MiscUtils.toString(toolGetProcAddressFuncNameList)); - System.out.println(" Tool Lib Names : "+MiscUtils.toString(toolLibNames)); + System.out.println(" toolGetProcAddressFuncNameList: "+toolGetProcAddressFuncNameList); + System.out.println(" Tool Lib Names : "+toolLibNames); System.out.println(" Tool Lib Loaded: "+getToolLibLoadedNumber()+"/"+getToolLibNumber()+", complete "+isToolLibComplete()); - System.out.println(" Glue Lib Names : "+MiscUtils.toString(glueLibNames)); + System.out.println(" Glue Lib Names : "+glueLibNames); System.out.println(" Glue Lib Loaded: "+getGlueLibLoadedNumber()+"/"+getGlueLibNumber()+", complete "+isGlueLibComplete()); System.out.println(" All Complete: "+isLibComplete()); } @@ -209,13 +207,13 @@ private void loadLibraries() { libNames = new ArrayList(); libNames.add((String)listObj); } else { - throw new RuntimeException("List element "+i+" must be either a List or String: "+MiscUtils.toString(toolLibNames)); + throw new RuntimeException("List element "+i+" must be either a List or String: "+toolLibNames); } if( null != libNames && libNames.size() > 0 ) { lib = loadFirstAvailable(libNames, loader, info.shallLinkGlobal()); if ( null == lib ) { if(DEBUG) { - System.out.println("Unable to load any Tool library of: "+MiscUtils.toString(libNames)); + System.out.println("Unable to load any Tool library of: "+libNames); } } else { nativeLibraries.add(lib); @@ -283,7 +281,7 @@ private long dynamicLookupFunctionOnLibs(String funcName) { if(0!=addr) { System.err.println("Lookup-Native: <" + funcName + "> 0x" + Long.toHexString(addr) + " in lib " + libName ); } else { - System.err.println("Lookup-Native: <" + funcName + "> ** FAILED ** in libs " + MiscUtils.toString(nativeLibraries)); + System.err.println("Lookup-Native: <" + funcName + "> ** FAILED ** in libs " + nativeLibraries); } } return addr; diff --git a/src/java/com/jogamp/common/util/MiscUtils.java b/src/java/com/jogamp/common/util/MiscUtils.java deleted file mode 100644 index dd32951e..00000000 --- a/src/java/com/jogamp/common/util/MiscUtils.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2010, Sven Gothel - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Sven Gothel nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL Sven Gothel BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package com.jogamp.common.util; - -import java.util.*; - -public class MiscUtils { - - public static StringBuffer append(StringBuffer sb, List list) { - sb.append("["); - if(null!=list&&list.size()>0) { - boolean comma = false; - for(Iterator iter=list.iterator(); iter.hasNext(); ) { - if(comma) { sb.append(", "); } - Object o = iter.next(); - if(o instanceof List) { - sb = append(sb, (List)o); - } else { - sb.append(o); - } - comma = true; - } - } - sb.append("]"); - return sb; - } - - public static String toString(List list) { - return append(new StringBuffer(), list).toString(); - } -} - From 1d9b041200dcc44a353706377bfd6ac999a14d7e Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 10 Jun 2010 17:20:42 +0200 Subject: [PATCH 5/5] small fix in IDE target. Test ran twice in debug mode. --- nbproject/ide-file-targets.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/nbproject/ide-file-targets.xml b/nbproject/ide-file-targets.xml index 343f5274..d8793764 100644 --- a/nbproject/ide-file-targets.xml +++ b/nbproject/ide-file-targets.xml @@ -50,7 +50,6 @@ -