From 58290b37f51e368db75cd3853ecbc7903a723bef Mon Sep 17 00:00:00 2001 From: Henrik Rusche Date: Wed, 6 Jun 2018 20:09:32 +0200 Subject: [PATCH] BUGFIX: Mess in dlTables and use of OSspecific --- src/OSspecific/MSWindows/MSwindows.C | 6 +- src/OSspecific/POSIX/POSIX.C | 32 +++++++ .../dlLibraryTable/dlLibraryTable.C | 88 ++++++------------- .../dlLibraryTable/dlLibraryTable.H | 10 --- 4 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/OSspecific/MSWindows/MSwindows.C b/src/OSspecific/MSWindows/MSwindows.C index 9a2568474..0afceab45 100644 --- a/src/OSspecific/MSWindows/MSwindows.C +++ b/src/OSspecific/MSWindows/MSwindows.C @@ -1277,11 +1277,9 @@ void* dlOpen(const fileName& libName, const bool check) << " : LoadLibrary of " << libName << endl; } - const char* dllExt = ".dll"; + // Replace extension with .dll + string winLibName(libName.lessExt() + ".dll"); - // Assume libName is of the form, lib.so - string winLibName(libName); - winLibName.replace(".so", dllExt); void* handle = ::LoadLibrary(winLibName.c_str()); if (NULL == handle) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 071a6723a..67ed48a4f 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -1167,6 +1167,38 @@ void* Foam::dlOpen(const fileName& lib, const bool check) } void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL); +#ifdef darwin + // If failing to load under OS X, let's try some obvious variations + // before giving up completely + fileName osxFileName(lib); + + if (!handle && lib.ext() == "so") + { + osxFileName = lib.lessExt() + ".dylib"; + handle = ::dlopen(osxFileName.c_str(), RTLD_LAZY|RTLD_GLOBAL); + } + + // If unsuccessful, which might be the case under Mac OSX 10.11 (El + // Capitan) with System Integrity Protection (SIP) enabled, let's try + // building a full path using well-known environment variables. This is + // the last resort, unless you provide the full pathname yourself. + if (!handle) + { + fileName l_LIBBIN_Name = getEnv("FOAM_LIBBIN")/osxFileName; + handle = ::dlopen(l_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); + } + if (!handle) + { + fileName l_SITE_LIBBIN_Name = getEnv("FOAM_SITE_LIBBIN")/osxFileName; + handle = ::dlopen(l_SITE_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); + } + if (!handle) + { + fileName l_USER_LIBBIN_Name = getEnv("FOAM_USER_LIBBIN")/osxFileName; + handle = ::dlopen(l_USER_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); + } +#endif + if (!handle && check) { WarningInFunction diff --git a/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C b/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C index a8938b1b6..f4e50f46d 100644 --- a/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C +++ b/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C @@ -26,7 +26,6 @@ License #include "dlLibraryTable.H" #include "OSspecific.H" #include "int.H" -#include // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -35,8 +34,6 @@ namespace Foam defineTypeNameAndDebug(dlLibraryTable, 0); } -Foam::dlLibraryTable Foam::dlLibraryTable::loadedLibraries; - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -62,6 +59,12 @@ Foam::dlLibraryTable::~dlLibraryTable() { if (libPtrs_[i]) { + if (debug) + { + InfoInFunction + << "Closing " << libNames_[i] + << " with handle " << uintptr_t(libPtrs_[i]) << endl; + } dlClose(libPtrs_[i]); } } @@ -78,75 +81,31 @@ bool Foam::dlLibraryTable::open { if (functionLibName.size()) { - void* functionLibPtr = - dlopen(functionLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL); + void* functionLibPtr = dlOpen(functionLibName, verbose); -#ifdef darwin - // If failing to load under OS X, let's try some obvious variations - // before giving up completely - fileName osxFileName(functionLibName); - - if(!functionLibPtr && functionLibName.ext()=="so") + if (debug) { - osxFileName=functionLibName.lessExt()+".dylib"; - - functionLibPtr = - dlopen(osxFileName.c_str(), RTLD_LAZY|RTLD_GLOBAL); + InfoInFunction + << "Opened " << functionLibName + << " resulting in handle " << uintptr_t(functionLibPtr) << endl; } - // If unsuccessful, which might be the case under Mac OSX 10.11 (El - // Capitan) with System Integrity Protection (SIP) enabled, let's try - // building a full path using well-known environment variables. This is - // the last resort, unless you provide the full pathname yourself. if (!functionLibPtr) { - fileName l_LIBBIN_Name = - getEnv("FOAM_LIBBIN")/osxFileName; - functionLibPtr = - dlopen(l_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); - } - if (!functionLibPtr) - { - fileName l_SITE_LIBBIN_Name = - getEnv("FOAM_SITE_LIBBIN")/osxFileName; - functionLibPtr = - dlopen(l_SITE_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); - } - if (!functionLibPtr) - { - fileName l_USER_LIBBIN_Name = - getEnv("FOAM_USER_LIBBIN")/osxFileName; - functionLibPtr = - dlopen(l_USER_LIBBIN_Name.c_str(), RTLD_LAZY|RTLD_GLOBAL); - } -#elif defined mingw - if(!functionLibPtr && functionLibName.ext()=="so") { - fileName lName=functionLibName.lessExt()+".dll"; - functionLibPtr = - dlopen(lName.c_str(), RTLD_LAZY|RTLD_GLOBAL); - } -#endif - if (!functionLibPtr) - { - WarningIn - ( - "dlLibraryTable::open(const fileName& functionLibName)" - ) << "could not load " << dlerror() - << endl; + if (verbose) + { + WarningInFunction + << "could not load " << functionLibName + << endl; + } return false; } else { - if (!loadedLibraries.found(functionLibPtr)) - { - loadedLibraries.insert(functionLibPtr, functionLibName); - return true; - } - else - { - return false; - } + libPtrs_.append(functionLibPtr); + libNames_.append(functionLibName); + return true; } } else @@ -174,6 +133,13 @@ bool Foam::dlLibraryTable::close if (index != -1) { + if (debug) + { + InfoInFunction + << "Closing " << functionLibName + << " with handle " << uintptr_t(libPtrs_[index]) << endl; + } + bool ok = dlClose(libPtrs_[index]); libPtrs_[index] = NULL; diff --git a/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H b/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H index 27076cc89..97671c5a6 100644 --- a/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H +++ b/src/foam/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H @@ -37,8 +37,6 @@ SourceFiles #include "label.H" #include "DynamicList.H" -#include "Hash.H" -#include "HashTable.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -50,8 +48,6 @@ namespace Foam \*---------------------------------------------------------------------------*/ class dlLibraryTable -: - public HashTable > { // Private Member Functions @@ -72,12 +68,6 @@ public: // Declare name of the class and its debug switch ClassName("dlLibraryTable"); - // Static data members - - //- Static data someStaticData - static dlLibraryTable loadedLibraries; - - // Constructors //- Construct null