From: Christian P. <cp...@us...> - 2004-12-26 14:38:22
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8454/src/System Modified Files: SharedLib.dl.cpp SharedLib.ltdl.cpp SharedLib.shl.cpp SharedLib.win32.cpp SharedLibCache.h Log Message: Added SharedLibCache to libltdl implementation. Added templatetized handle type to SharedLibCache. stephan: please see if this is a solution with lt_dlclose() Index: SharedLibCache.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLibCache.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- SharedLibCache.h 25 Dec 2004 07:05:59 -0000 1.2 +++ SharedLibCache.h 26 Dec 2004 14:38:09 -0000 1.3 @@ -32,13 +32,12 @@ namespace System { // Shared library handle cache -template <typename destroyF> +template <typename handle_t, typename destroyF> struct SharedLibCache { - typedef std::map<std::string, unsigned long> map_t; + typedef std::map<std::string, handle_t> map_t; + typedef typename map_t::const_iterator iterator; - enum { InvalidHandle = (unsigned long)-1 }; - SharedLibCache() { } @@ -57,18 +56,24 @@ void add(const std::string& name, unsigned long handle) { - map_t::iterator i = _handles.find(name); + iterator i = _handles.find(name); if(i == _handles.end()) _handles[name] = handle; } - unsigned long lookup(const std::string& name) const + iterator lookup(const std::string& name) const { - map_t::const_iterator i = _handles.find(name); - if(i == _handles.end()) - return InvalidHandle; + return _handles.find(name); + } - return i->second; + iterator begin() + { + return _handles.begin(); + } + + iterator end() + { + return _handles.end(); } CriticalSection mutex; @@ -80,11 +85,11 @@ /** Internal marker type. */ struct cached_libs_context {}; -template <typename destroyF> -SharedLibCache<destroyF> & shared_lib_cache() +template <typename handle_t, typename destroyF> +SharedLibCache<handle_t, destroyF> & shared_lib_cache() { return ::P::Phoenix< - SharedLibCache<destroyF>, + SharedLibCache<handle_t, destroyF>, cached_libs_context> ::instance(); } Index: SharedLib.ltdl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.ltdl.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- SharedLib.ltdl.cpp 25 Dec 2004 20:08:07 -0000 1.6 +++ SharedLib.ltdl.cpp 26 Dec 2004 14:38:09 -0000 1.7 @@ -20,6 +20,7 @@ #include "pclasses/System/SharedLib.h" #include "pclasses/Phoenix.h" +#include "SharedLibCache.h" #include <ltdl.h> #include <errno.h> @@ -33,29 +34,38 @@ namespace System { - void ltdl_init() - { - lt_dlinit(); - lt_dlopen( 0 ); - } - int ltdl_init_placeholder = (ltdl_init(),0); +void ltdl_init() +{ + lt_dlinit(); + lt_dlopen( 0 ); +} +int ltdl_init_placeholder = (ltdl_init(),0); - typedef unsigned long handle_type; - typedef std::map<handle_type,lt_dlhandle> lt_handle_map_t; +typedef unsigned long handle_type; +typedef std::map<handle_type,lt_dlhandle> lt_handle_map_t; - struct ltdl_sharing_context {}; // marker class - lt_handle_map_t & - lt_handle_map() - { - typedef ::P::Phoenix< lt_handle_map_t, ltdl_sharing_context > PHX; - return PHX::instance(); - } +struct ltdl_sharing_context {}; // marker class +lt_handle_map_t & +lt_handle_map() +{ + typedef ::P::Phoenix< lt_handle_map_t, ltdl_sharing_context > PHX; + return PHX::instance(); +} - int BindMode2Flags(SharedLib::BindMode mode) - { // ltdl doesn't use dlopen() flags - return 0; +int BindMode2Flags(SharedLib::BindMode mode) +{ // ltdl doesn't use dlopen() flags + return 0; +} + +struct SharedLibCloser +{ + void operator()(lt_dlhandle handle) + { + lt_dlclose((handle); } +}; +typedef SharedLibCache<lt_dlhandle, SharedLibCloser> Cache; SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) @@ -79,16 +89,31 @@ SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) : _handle(0) { + Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); + CriticalSection::ScopedLock lck(cache.mutex); - lt_dlhandle h = lt_dlopen(name.c_str() /** BindMode2Flags(mode) */ ); - if( h ) + // see if we can get it from the handle cache ... + Cache::iterator i = cache.lookup(name); + if(i == cache.end()) { - _handle = reinterpret_cast<handle_type>( static_cast<void *>( h ) ); - lt_handle_map().insert( std::make_pair( _handle, h ) ); - } else + lt_dlhandle h = lt_dlopen(name.c_str() /** BindMode2Flags(mode) */ ); + if( h ) + { + _handle = reinterpret_cast<handle_type>( static_cast<void *>( h ) ); + lt_handle_map().insert( std::make_pair( _handle, h ) ); + + // add it to the handle cache + cache.add(name, _handle); + } + else + { + // CERR << "SharedLib("<<name<<") failed.\n"; + throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); + } + } + else { - // CERR << "SharedLib("<<name<<") failed.\n"; - throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); + _handle = i->second; } } Index: SharedLib.dl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dl.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- SharedLib.dl.cpp 25 Dec 2004 16:02:51 -0000 1.8 +++ SharedLib.dl.cpp 26 Dec 2004 14:38:05 -0000 1.9 @@ -45,7 +45,7 @@ } }; -typedef SharedLibCache<SharedLibCloser> Cache; +typedef SharedLibCache<unsigned long, SharedLibCloser> Cache; int BindMode2Flags(SharedLib::BindMode mode) { @@ -77,12 +77,12 @@ SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) { - Cache& cache = shared_lib_cache<SharedLibCloser>(); + Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); - // se if we can get it from the handle cache ... - unsigned long handle = cache.lookup(name); - if(handle == Cache::InvalidHandle) + // see if we can get it from the handle cache ... + Cache::iterator i = cache.lookup(name); + if(i == cache.end()) { _handle = (unsigned long)dlopen(name.c_str(), BindMode2Flags(mode)); if(!_handle) @@ -93,7 +93,7 @@ } else { - _handle = handle; + _handle = i->second; } } Index: SharedLib.win32.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.win32.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- SharedLib.win32.cpp 25 Dec 2004 16:02:51 -0000 1.6 +++ SharedLib.win32.cpp 26 Dec 2004 14:38:09 -0000 1.7 @@ -38,7 +38,7 @@ } }; -typedef SharedLibCache<SharedLibCloser> Cache; +typedef SharedLibCache<unsigned long, SharedLibCloser> Cache; SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { @@ -56,7 +56,7 @@ // realName << name; // realName << ".dll"; - Cache& cache = shared_lib_cache<SharedLibCloser>(); + Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); // se if we can get it from the handle cache ... Index: SharedLib.shl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.shl.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- SharedLib.shl.cpp 25 Dec 2004 16:02:51 -0000 1.6 +++ SharedLib.shl.cpp 26 Dec 2004 14:38:09 -0000 1.7 @@ -39,7 +39,7 @@ } }; -typedef SharedLibCache<SharedLibCloser> Cache; +typedef SharedLibCache<unsigned long, SharedLibCloser> Cache; int BindMode2Flags(SharedLib::BindMode mode) { @@ -75,7 +75,7 @@ // std::ostringstream realName; // realName << name; // realName << ".sl"; - Cache& cache = shared_lib_cache<SharedLibCloser>(); + Cache& cache = shared_lib_cache<unsigned long, SharedLibCloser>(); CriticalSection::ScopedLock lck(cache.mutex); // se if we have the handle cached ... |