diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 33d9a61679e112bf7186e536ce7bbeac615d29b0..7ac75e90a452ca170e083439abcd8035bdee9303 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -1701,56 +1701,42 @@ bool Foam::dlClose(void* handle) } -void* Foam::dlSym(void* handle, const std::string& symbol) +void* Foam::dlSymFind(void* handle, const std::string& symbol, bool required) { + if (!required && (!handle || symbol.empty())) + { + return nullptr; + } + if (POSIX::debug) { std::cout - << "dlSym(void*, const std::string&)" + << "dlSymFind(void*, const std::string&, bool)" << " : dlsym of " << symbol << std::endl; } - // clear any old errors - see manpage dlopen + + // Clear any old errors - see manpage dlopen (void) ::dlerror(); - // get address of symbol + // Get address of symbol void* fun = ::dlsym(handle, symbol.c_str()); - // find error (if any) - char *error = ::dlerror(); + // Any error? + char *err = ::dlerror(); - if (error) + if (err) { - WarningInFunction - << "Cannot lookup symbol " << symbol << " : " << error - << endl; - } - - return fun; -} - - -bool Foam::dlSymFound(void* handle, const std::string& symbol) -{ - if (handle && !symbol.empty()) - { - if (POSIX::debug) + if (!required) { - std::cout - << "dlSymFound(void*, const std::string&)" - << " : dlsym of " << symbol << std::endl; + return nullptr; } - // clear any old errors - see manpage dlopen - (void) ::dlerror(); - - // get address of symbol - (void) ::dlsym(handle, symbol.c_str()); - - // symbol can be found if there was no error - return !::dlerror(); + WarningInFunction + << "Cannot lookup symbol " << symbol << " : " << err + << endl; } - return false; + return fun; } diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 4d182e698c49c0fc2969c9ad1d52bf6fdb94d404..2f568d6bab50b21a0839d525f5b37d7de8421310 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -160,7 +160,7 @@ time_t lastModified(const fileName& name, const bool followLink=true); //- Return time of last file modification // Using an empty name is a no-op and always returns 0. -double highResLastModified(const fileName&, const bool followLink = true); +double highResLastModified(const fileName&, const bool followLink=true); //- Read a directory and return the entries as a fileName List. // Using an empty directory name returns an empty list. @@ -259,13 +259,30 @@ void* dlOpen(const fileName& lib, const bool check = true); //- Close a dlopened library using handle. Return true if successful bool dlClose(void* handle); + +//- Look for symbol in a dlopened library. +// If the symbol is not 'required', using a null handle or an empty symbol +// name is a no-op and returns nullptr without error. +// +// \return the symbol or nullptr. +void* dlSymFind(void* handle, const std::string& symbol, bool required=false); + //- Lookup a symbol in a dlopened library using handle to library -void* dlSym(void* handle, const std::string& symbol); +// \return the symbol or nullptr. +inline void* dlSym(void* handle, const std::string& symbol) +{ + return dlSymFind(handle, symbol, true); +} -//- Report if symbol in a dlopened library could be found. +//- Check for symbol in a dlopened library. // Using a null handle or an empty symbol name is a no-op and always -// returns false. -bool dlSymFound(void* handle, const std::string& symbol); +// returns nullptr. +// \return the symbol or nullptr. +inline void* dlSymFound(void* handle, const std::string& symbol) +{ + return dlSymFind(handle, symbol, false); +} + //- Return all loaded libraries fileNameList dlLoaded();