35 #define EXT_SO "dylib" 55 std::unique_ptr<Foam::dlLibraryTable> Foam::dlLibraryTable::global_(
nullptr);
84 global_.reset(
new dlLibraryTable{});
91 bool Foam::dlLibraryTable::functionHook
95 const std::string& funcName,
97 const std::string& context
100 if (!handle || funcName.empty())
114 loaderType fun =
reinterpret_cast<loaderType
>(symbol);
129 <<
"Failed symbol lookup " << funcName.c_str() <<
nl;
131 if (!context.empty())
133 err <<
"from " << context.c_str() <<
nl;
144 const std::string& funcName,
146 const std::string& context
149 return functionHook(
true, handle, funcName, verbose, context);
156 const std::string& funcName,
158 const std::string& context
161 return functionHook(
false, handle, funcName, verbose, context);
167 void* Foam::dlLibraryTable::openLibrary
169 const fileName& libName,
182 <<
"Opened " << libName
189 os <<
"Could not load " << libName <<
nl << msg.c_str() <<
endl;
210 std::initializer_list<fileName> libNames,
220 const word& libsEntry,
234 const word& libsEntry,
258 for (
const void* ptr : libPtrs_)
274 for (
const void* ptr : libPtrs_)
301 void* ptr = libPtrs_[i];
302 const fileName& libName = libNames_[i];
304 if (ptr !=
nullptr && !libName.empty())
306 list[nLoaded] = libName;
323 void* ptr = libPtrs_[i];
327 libNames_[i].clear();
334 <<
"Closed [" << i <<
"] " << libNames_[i]
337 libPtrs_[i] =
nullptr;
338 libNames_[i].clear();
347 <<
"Failed closing " << libNames_[i]
355 if (nLoaded && nLoaded != libPtrs_.size())
361 if (libPtrs_[i] !=
nullptr)
365 libPtrs_[nLoaded] = libPtrs_[i];
366 libNames_[nLoaded] = std::move(libNames_[i]);
374 libPtrs_.resize(nLoaded);
375 libNames_.resize(nLoaded);
381 if (libName.empty() || libNames_.contains(libName))
386 libPtrs_.push_back(
nullptr);
387 libNames_.push_back(libName);
397 for (
const fileName& libName : libNames)
399 if (push_back(libName))
416 void* ptr = libPtrs_[i];
417 const fileName& libName = libNames_[i];
419 if (ptr ==
nullptr && !libName.empty())
423 ptr = openLibrary(libName, verbose);
432 libNames_[i].clear();
437 return nOpen && nOpen == nCand;
443 const fileName& libName,
448 void* ptr = openLibrary(libName, verbose);
452 libPtrs_.push_back(ptr);
453 libNames_.push_back(libName);
462 const UList<fileName>& libNames,
466 decltype(libNames.size()) nOpen = 0;
468 for (
const fileName& libName : libNames)
470 const label index = libNames_.find(libName);
472 if (index >= 0 && libPtrs_[index] !=
nullptr)
483 return nOpen && nOpen == libNames.size();
489 std::initializer_list<fileName> libNames,
493 decltype(libNames.size()) nOpen = 0;
495 for (
const fileName& libName : libNames)
497 const label index = libNames_.find(libName);
499 if (index >= 0 && libPtrs_[index] !=
nullptr)
510 return nOpen && nOpen == libNames.size();
516 const fileName& libName,
520 const label index = libNames_.rfind(libName);
522 if (index < 0 || libName.empty())
527 void* ptr = libPtrs_[index];
531 libNames_[index].clear();
536 <<
"Closing " << libName
541 libPtrs_[index] =
nullptr;
542 libNames_[index].clear();
553 for (label idx = 0; (idx = libPtrs_.find(ptr, idx)) >= 0; ++idx)
556 libPtrs_[idx] =
nullptr;
557 libNames_[idx].clear();
563 <<
"Could not close " << libName <<
endl;
572 const label index = libNames_.rfind(libName);
574 if (index < 0 || libName.empty())
579 return libPtrs_[index];
585 const word& libsEntry,
586 const dictionary&
dict,
590 List<fileName> libNames;
602 const word& libsEntry
614 const InfoProxy<dlLibraryTable>& iproxy
617 const auto& tbl = *iproxy;
624 const void* ptr = tbl.pointers()[i];
625 const fileName& libName = tbl.names()[i];
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
A class for handling file names.
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
void resize(const label len)
Adjust allocated size of list.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
constexpr char nl
The newline '\n' character (0x0a)
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
void clear()
Clears the table, without attempting to close the libraries.
Begin list [isseparator].
List< fileName > loaded() const
Names of the libraries in use.
~dlLibraryTable()
Destructor. Closes all libraries loaded by the table.
static dlLibraryTable & libs()
Table of global libraries.
static std::string stem(const std::string &str)
Return the basename, without extension.
void close(bool verbose=true)
Close all libraries loaded by the table and remove the closed functions from the table.
#define forAll(list, i)
Loop across all elements in list.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
word ext() const
Return file name extension (part after last .)
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
static bool loadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Low-level interface to execute global "void funcName(true)" from the library, typically for additiona...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
static word basename(const fileName &libPath)
Library basename without leading 'lib' or trailing '.so'.
A class for handling words, derived from Foam::string.
#define DebugInFunction
Report an information message using Foam::Info.
bool push_back(const fileName &libName)
Add to the list of names, but do not yet open.
static word fullname(word libName)
Library fullname, prefix with 'lib', suffix with '.so'.
dlLibraryTable()=default
Default construct.
A table of dynamically loaded libraries.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
An Ostream is an abstract base class for all output systems (streams, files, token lists...
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
bool empty() const noexcept
True if there are no libraries loaded by the table.
static bool unloadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Low-level interface to execute global "void funcName(false)" from the library, typically for unloadin...
#define WarningInFunction
Report a warning using Foam::Warning.
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
void * findLibrary(const fileName &libName)
Find the handle of the named library.
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
bool open(bool verbose=true)
Open named, but unopened libraries. These names will normally have been added with push_back()...
static int dlcloseOnTerminate
Use dlclose() when clearing the dlLibraryTable.
label size() const noexcept
The number of libraries loaded by the table.
string expand(const std::string &s, const HashTable< string > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping and return the expanded string.