ENH: unify dlSym handling

- amalgamate dlSym() and dlSymFound() into a single dlSymFind() backend
  with optional 'required' argument. This makes it possible to
  query and assign at once.
This commit is contained in:
Mark Olesen
2019-04-30 14:27:12 +02:00
parent 4a603c4577
commit 08d52c6ffc
2 changed files with 42 additions and 39 deletions

View File

@ -1701,27 +1701,38 @@ 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)
{
if (!required)
{
return nullptr;
}
WarningInFunction
<< "Cannot lookup symbol " << symbol << " : " << error
<< "Cannot lookup symbol " << symbol << " : " << err
<< endl;
}
@ -1729,31 +1740,6 @@ void* Foam::dlSym(void* handle, const std::string& symbol)
}
bool Foam::dlSymFound(void* handle, const std::string& symbol)
{
if (handle && !symbol.empty())
{
if (POSIX::debug)
{
std::cout
<< "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
}
// 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();
}
return false;
}
#ifndef __APPLE__
static int collectLibsCallback
(

View File

@ -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);
//- Lookup a symbol in a dlopened library using handle to library
void* dlSym(void* handle, const std::string& symbol);
//- Report if symbol in a dlopened library could be found.
//- 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
// \return the symbol or nullptr.
inline void* dlSym(void* handle, const std::string& symbol)
{
return dlSymFind(handle, symbol, true);
}
//- 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();