Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4440/src/System Modified Files: SharedLib.common.cpp SharedLib.dl.cpp SharedLib.dyld.cpp SharedLib.ltdl.cpp SharedLib.shl.cpp SharedLib.win32.cpp SharedLibCache.h Log Message: - Removed SharedLib::SharedLib(const std::string&,...) - Added some trace messages Index: SharedLib.dyld.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dyld.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- SharedLib.dyld.cpp 25 Dec 2004 16:02:51 -0000 1.6 +++ SharedLib.dyld.cpp 28 Apr 2005 10:37:50 -0000 1.7 @@ -48,9 +48,7 @@ SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { -// Unicode::String realName = name; -// realName.append(".dyld"); - + //TODO: use the SharedLibCache !! int flags = BindMode2Flags(mode) | NSLINKMODULE_OPTION_RETURN_ON_ERROR; NSObjectFileImage file; @@ -63,24 +61,6 @@ _handle = (unsigned long)out; } -SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) -{ -// std::ostringstream realName; -// realName << name; -// realName << ".dyld"; - - int flags = BindMode2Flags(mode) | NSLINKMODULE_OPTION_RETURN_ON_ERROR; - - NSObjectFileImage file; - NSObjectFileImageReturnCode ret; - ret = NSCreateObjectFileImageFromFile(name.c_str(), &file); - if(ret != NSObjectFileImageSuccess) - throw SystemError(0, "Could not load shared library", P_SOURCEINFO); - - NSModule out = NSLinkModule(file, name.c_str(), flags); - _handle = (unsigned long)out; -} - SharedLib::~SharedLib() throw() { NSUnLinkModule((NSModule)_handle, NSUNLINKMODULE_OPTION_NONE); Index: SharedLib.dl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dl.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- SharedLib.dl.cpp 24 Jan 2005 01:23:32 -0000 1.10 +++ SharedLib.dl.cpp 28 Apr 2005 10:37:50 -0000 1.11 @@ -18,19 +18,18 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "pclasses/Trace.h" #include "pclasses/System/SharedLib.h" #include "SharedLibCache.h" #include <dlfcn.h> #include <errno.h> -#include <string> -#include <sstream> - namespace P { namespace System { +// cproch: why do we need this libdl_init() crap ??? void libdl_init() { dlopen( 0, RTLD_NOW | RTLD_GLOBAL ); @@ -41,6 +40,7 @@ { void operator()(unsigned long handle) { + P_TRACE(SharedLibCloser) << "closing handle=" << handle; dlclose((void*)handle); } }; @@ -66,85 +66,44 @@ SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { - Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); - CriticalSection::ScopedLock lck(cache.mutex); - - // see if we can get it from the handle cache ... - Cache::iterator i = cache.lookup(name.utf8()); - if(i == cache.end()) - { - _handle = (unsigned long)dlopen(name.utf8().c_str(), BindMode2Flags(mode)); - if(!_handle) - throw SystemError(errno, dlerror(), P_SOURCEINFO); - - // add it to the handle cache - cache.add(name.utf8(), _handle); - } - else - { - _handle = i->second; - } -} + P_TRACE(SharedLib) << "SharedLib(" << name << "," << mode << ")"; -SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) -{ Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); + std::string utf8name = name.utf8(); + // see if we can get it from the handle cache ... - Cache::iterator i = cache.lookup(name); + Cache::iterator i = cache.lookup(utf8name); if(i == cache.end()) { - _handle = (unsigned long)dlopen(name.c_str(), BindMode2Flags(mode)); + _handle = (unsigned long)dlopen(utf8name.c_str(), BindMode2Flags(mode)); if(!_handle) throw SystemError(errno, dlerror(), P_SOURCEINFO); + P_TRACE(SharedLib) << "library opened, handle=" << _handle; + // add it to the handle cache - cache.add(name, _handle); + cache.add(utf8name, _handle); } else { _handle = i->second; + P_TRACE(SharedLib) << "library opened (cached), handle=" << _handle; } } SharedLib::~SharedLib() throw() { -// dlclose((void*)_handle); -/***** -dlclose() is a BAD IDEA: - -Post-main(): - -#0 0x4001272b in strlen () from /lib/ld-linux.so.2 -#1 0x4000ce20 in _dl_signal_error () from /lib/ld-linux.so.2 -#2 0x40331947 in _dl_close () from /lib/tls/libc.so.6 -#3 0x40370d1a in dlclose_doit () from /lib/libdl.so.2 -#4 0x4000cd56 in _dl_catch_error () from /lib/ld-linux.so.2 -#5 0x40371085 in _dlerror_run () from /lib/libdl.so.2 -#6 0x40370cf0 in dlclose () from /lib/libdl.so.2 -#7 0x400398eb in ~SharedLib (this=0x8053c60) at SharedLib.dl.cpp:77 -#8 0x0804c9d8 in ~PluginCleaner (this=0x8051ec4) at Plugin.h:43 -#9 0x0804ea7c in ~PluginManager (this=0x8051ec0) at Plugin.h:106 -#10 0x0804d8cf in ~Phoenix (this=0x8051ec0) at Phoenix.h:196 -#11 0x0804bce5 in __tcf_1 () at Phoenix.h:161 -#12 0x40271820 in exit () from /lib/tls/libc.so.6 -#13 0x4025cb1a in __libc_start_main () from /lib/tls/libc.so.6 -#14 0x0804a1f1 in _start () at start.S:119 - -HOWEVER, not doing a dlclose() causes: -*** glibc detected *** double free or corruption: 0x08053c60 *** - -*****/ + // we don't leak the handle! + // the handle is owned by the SharedLibCache and will be + // freed upon application cleanup... } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) { - if( ! symbol ) - { - throw RuntimeError( "Invalid symbol (null).", P_SOURCEINFO ); - } void* addr = dlsym((void*)_handle, symbol); + P_TRACE(SharedLib) << "operator[](" << symbol << ") = " << addr; if(!addr) throw RuntimeError("Symbol not found in shared library", P_SOURCEINFO); Index: SharedLib.common.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.common.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- SharedLib.common.cpp 28 Dec 2004 15:55:16 -0000 1.2 +++ SharedLib.common.cpp 28 Apr 2005 10:37:50 -0000 1.3 @@ -75,7 +75,7 @@ } - SharedLib * openSharedLib( const std::string & path ) throw(RuntimeError) + SharedLib * openSharedLib( const Unicode::String & path ) throw(SystemError) { //CERR << "openSharedLib("<<path<<")\n"; SharedLib * sh = new SharedLib( path ); Index: SharedLib.win32.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.win32.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- SharedLib.win32.cpp 24 Jan 2005 01:21:32 -0000 1.8 +++ SharedLib.win32.cpp 28 Apr 2005 10:37:50 -0000 1.9 @@ -18,14 +18,12 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "pclasses/Trace.h" #include "pclasses/System/SharedLib.h" #include "SharedLibCache.h" #include <windows.h> -#include <string> -#include <sstream> - namespace P { namespace System { @@ -34,6 +32,7 @@ { void operator()(unsigned long handle) { + P_TRACE(SharedLibCloser) << "closing handle=" << handle; FreeLibrary((HMODULE)handle); } }; @@ -42,6 +41,8 @@ SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { + P_TRACE(SharedLib) << "SharedLib(" << name << "," << mode << ")"; + Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); @@ -54,48 +55,29 @@ if(!_handle) throw SystemError(GetLastError(), "Could not load shared library", P_SOURCEINFO); - // add the handle to the cache - cache.add(name.utf8(), _handle); - } - else - { - _handle = i->second; - } -} - -SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) -{ - Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); - CriticalSection::ScopedLock lck(cache.mutex); - - // se if we can get it from the handle cache ... - Cache::iterator i = cache.lookup(name); - if(i == cache.end()) - { - _handle = (unsigned long)LoadLibrary(name.c_str()); - if(!_handle) - throw SystemError(GetLastError(), "Could not load shared library", P_SOURCEINFO); + P_TRACE(SharedLib) << "library opened, handle=" << _handle; // add the handle to the cache - cache.add(name, _handle); + cache.add(name.utf8(), _handle); } else { _handle = i->second; + P_TRACE(SharedLib) << "library opened (cached), handle=" << _handle; } } SharedLib::~SharedLib() throw() { + // we don't leak the handle! + // the handle is owned by the SharedLibCache and will be + // freed upon application cleanup... } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) { - if( ! symbol ) - { - throw RuntimeError( "Invalid symbol (null).", P_SOURCEINFO ); - } void* addr = GetProcAddress((HMODULE)_handle, symbol); + P_TRACE(SharedLib) << "operator[](" << symbol << ") = " << addr; if(!addr) throw RuntimeError("Symbol not found in shared library", P_SOURCEINFO); Index: SharedLibCache.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLibCache.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- SharedLibCache.h 26 Dec 2004 14:54:50 -0000 1.4 +++ SharedLibCache.h 28 Apr 2005 10:37:50 -0000 1.5 @@ -21,6 +21,7 @@ #ifndef P_System_SharedLibCache_h #define P_System_SharedLibCache_h +#include "pclasses/Trace.h" #include "pclasses/Phoenix.h" #include "pclasses/System/CriticalSection.h" @@ -48,6 +49,7 @@ typename map_t::iterator it = _handles.begin(); while(_handles.end() != it) { + P_TRACE(SharedLibCache) << "destroy " << it->first; fun(it->second); ++it; } @@ -58,7 +60,10 @@ { iterator i = _handles.find(name); if(i == _handles.end()) + { + P_TRACE(SharedLibCache) << "add(" << name << "," << handle << ")"; _handles[name] = handle; + } } iterator lookup(const std::string& name) const Index: SharedLib.ltdl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.ltdl.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- SharedLib.ltdl.cpp 26 Dec 2004 14:50:52 -0000 1.8 +++ SharedLib.ltdl.cpp 28 Apr 2005 10:37:50 -0000 1.9 @@ -18,22 +18,18 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "pclasses/Trace.h" #include "pclasses/System/SharedLib.h" -#include "pclasses/Phoenix.h" #include "SharedLibCache.h" #include <ltdl.h> #include <errno.h> -#include <string> -#include <sstream> -#include <map> -#include <utility> // make_pair() - namespace P { namespace System { +// cproch: why do we need this ltdl_init() crap ??? void ltdl_init() { lt_dlinit(); @@ -41,92 +37,61 @@ } int ltdl_init_placeholder = (ltdl_init(),0); -struct ltdl_sharing_context {}; // marker class - -int BindMode2Flags(SharedLib::BindMode mode) -{ // ltdl doesn't use dlopen() flags - return 0; -} - -struct SharedLibCloser +struct SharedLibCloser { void operator()(lt_dlhandle handle) { + P_TRACE(SharedLibCloser) << "closing handle=" << (unsigned long)handle; lt_dlclose(handle); } }; typedef SharedLibCache<lt_dlhandle, SharedLibCloser> Cache; - SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) : _handle(0) { -// Please don't do this: -// Unicode::String realName = name; -// realName.append(".so"); - - //@fixme _handle = (unsigned long)dlopen(realName.utf8(), BindMode2Flags(mode)); - if(!_handle) - throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); -} - - -#ifndef CERR -#define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " -#endif - + P_TRACE(SharedLib) << "SharedLib(" << name << "," << mode << ")"; -SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) - : _handle(0) -{ Cache& cache = shared_lib_cache<lt_dlhandle, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); + std::string utf8name = name.utf8(); + // see if we can get it from the handle cache ... - Cache::iterator i = cache.lookup(name); + Cache::iterator i = cache.lookup(utf8name); if(i == cache.end()) { - lt_dlhandle h = lt_dlopen(name.c_str() /** BindMode2Flags(mode) */ ); - if( h ) - { - _handle = (unsigned long)h; - - // add it to the handle cache - cache.add(name, h); - } - else - { - // CERR << "SharedLib("<<name<<") failed.\n"; + lt_dlhandle h = lt_dlopen(utf8name.c_str() /** BindMode2Flags(mode) */ ); + if(!h) throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); - } + + _handle = (unsigned long)h; + P_TRACE(SharedLib) << "library opened, handle=" << _handle; + + // add it to the handle cache + cache.add(utf8name, h); } else { _handle = (unsigned long)i->second; + P_TRACE(SharedLib) << "library opened (cached), handle=" << _handle; } } SharedLib::~SharedLib() throw() { + // we don't leak the handle! + // the handle is owned by the SharedLibCache and will be + // freed upon application cleanup... } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) { - if( ! symbol ) - { - throw RuntimeError( "Invalid symbol (null).", P_SOURCEINFO ); - } - - lt_dlhandle lth = (lt_dlhandle)_handle; - void* addr = lt_dlsym( lth, symbol); + void* addr = lt_dlsym((lt_dlhandle)_handle, symbol); + P_TRACE(SharedLib) << "operator[](" << symbol << ") = " << addr; if(!addr) - { - std::ostringstream os; - os << "Symbol '"<<symbol<<"' not found in shared library."; - throw RuntimeError(os.str(), P_SOURCEINFO); -// throw RuntimeError("Symbol not found in SharedLib.", P_SOURCEINFO); - } + throw RuntimeError("Symbol not found in SharedLib.", P_SOURCEINFO); return addr; } Index: SharedLib.shl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.shl.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- SharedLib.shl.cpp 26 Dec 2004 14:38:09 -0000 1.7 +++ SharedLib.shl.cpp 28 Apr 2005 10:37:50 -0000 1.8 @@ -18,15 +18,13 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "pclasses/Trace.h" #include "pclasses/System/SharedLib.h" #include "SharedLibCache.h" #include <dl.h> #include <errno.h> -#include <string> -#include <sstream> - namespace P { namespace System { @@ -35,6 +33,7 @@ { void operator()(unsigned long handle) { + P_TRACE(SharedLibCloser) << "closing handle=" << handle; shl_unload((shl_t*)handle); } }; @@ -60,57 +59,46 @@ SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { -// Unicode::String realName = name; -// realName.append(".sl"); - - _handle = (unsigned long)shl_load(name.utf8(), BindMode2Flags(mode)); - - //@fixme dlerror() on hpux ?? - if(!_handle) - throw SystemError(errno, dlerror(), P_SOURCEINFO); -} + P_TRACE(SharedLib) << "SharedLib(" << name << "," << mode << ")"; -SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) -{ -// std::ostringstream realName; -// realName << name; -// realName << ".sl"; Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); + std::string utf8name = name.utf8(); + // se if we have the handle cached ... - unsigned long handle = cache.lookup(name); - if(handle == Cache::InvalidHandle) + Cache::Iterator i = cache.lookup(utf8name); + if(i == cache.end()) { - _handle = (unsigned long)shl_load(name.str().c_str(), - BindMode2Flags(mode)); - - //@fixme dlerror() on hpux ?? + _handle = (unsigned long)shl_load(utf8name.c_str(), BindMode2Flags(mode)); if(!_handle) - throw SystemError(errno, dlerror(), P_SOURCEINFO); + throw SystemError(errno, "Error loading shared library", P_SOURCEINFO); + + P_TRACE(SharedLib) << "library opened, handle=" << _handle; // add the handle to the cache - cache.add(name, _handle); + cache.add(utf8name, _handle); } else { - _handle = handle; + _handle = i->second; + P_TRACE(SharedLib) << "library opened (cached), handle=" << _handle; } } SharedLib::~SharedLib() throw() { + // we don't leak the handle! + // the handle is owned by the SharedLibCache and will be + // freed upon application cleanup... } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) { - if( ! symbol ) - { - throw RuntimeError( "Invalid symbol (null).", P_SOURCEINFO ); - } - void* address = 0; - if(shl_findsym((shl_t*)&_handle, symbol, TYPE_UNDEFINED, (void*)&address)) != 0) + int ret = shl_findsym((shl_t*)&_handle, symbol, TYPE_UNDEFINED, (void*)&address)); + P_TRACE(SharedLib) << "operator[](" << symbol << ") = " << addr; + if(ret != 0) throw RuntimeError("Symbol not found in shared library", P_SOURCEINFO); return address; |