STYLE: library functions using std::string

This commit is contained in:
Mark Olesen
2019-12-16 15:47:01 +01:00
parent 83e0501a77
commit eaa138443a
3 changed files with 60 additions and 48 deletions

View File

@ -45,10 +45,11 @@ namespace Foam
} }
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam namespace Foam
{ {
//! \cond fileScope //! \cond fileScope
static inline void writeEntryIfPresent static inline void writeEntryIfPresent
( (
@ -70,8 +71,11 @@ static inline void writeEntryIfPresent
} }
} }
//! \endcond //! \endcond
}
} // End namespace Foam
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::codedBase::writeCodeDict(Ostream& os, const dictionary& dict) void Foam::codedBase::writeCodeDict(Ostream& os, const dictionary& dict)
{ {
@ -88,111 +92,113 @@ void Foam::codedBase::writeCodeDict(Ostream& os, const dictionary& dict)
void* Foam::codedBase::loadLibrary void* Foam::codedBase::loadLibrary
( (
const fileName& libPath, const fileName& libPath,
const string& globalFuncName, const std::string& funcName,
const dynamicCodeContext& context const dynamicCodeContext& context
) const ) const
{ {
// Avoid compilation by loading an existing library // Avoid compilation by loading an existing library
void* lib = void* handle =
( (
!libPath.empty() && libs().open(libPath, false) !libPath.empty() && libs().open(libPath, false)
? libs().findLibrary(libPath) ? libs().findLibrary(libPath)
: nullptr : nullptr
); );
if (!lib) if (!handle)
{ {
return lib; return handle;
} }
// verify the loaded version and unload if needed // Verify the loaded version and unload if needed
// provision for manual execution of code after loading // Manual execution of code after loading.
if (dlSymFound(lib, globalFuncName)) // This is mandatory for codedBase.
void* rawSymbol = dlSymFind(handle, funcName);
if (rawSymbol)
{ {
loaderFunctionType function = loaderType fun = reinterpret_cast<loaderType>(rawSymbol);
reinterpret_cast<loaderFunctionType>
(
dlSym(lib, globalFuncName)
);
if (function) if (fun)
{ {
(*function)(true); // force load (*fun)(true); // force load
ok = true;
} }
else else
{ {
FatalIOErrorInFunction(context.dict()) FatalIOErrorInFunction(context.dict())
<< "Failed looking up symbol " << globalFuncName << "Failed symbol lookup " << funcName.c_str() << nl
<< nl << "from " << libPath << exit(FatalIOError); << "from " << libPath << nl
<< exit(FatalIOError);
} }
} }
else else
{ {
FatalIOErrorInFunction(context.dict()) FatalIOErrorInFunction(context.dict())
<< "Failed looking up symbol " << globalFuncName << nl << "Failed symbol lookup " << funcName.c_str() << nl
<< "from " << libPath << exit(FatalIOError); << "from " << libPath << nl
<< exit(FatalIOError);
lib = nullptr; handle = nullptr;
if (!libs().close(libPath, false)) if (!libs().close(libPath, false))
{ {
FatalIOErrorInFunction(context.dict()) FatalIOErrorInFunction(context.dict())
<< "Failed unloading library " << "Failed unloading library " << libPath << nl
<< libPath
<< exit(FatalIOError); << exit(FatalIOError);
} }
} }
return lib; return handle;
} }
void Foam::codedBase::unloadLibrary void Foam::codedBase::unloadLibrary
( (
const fileName& libPath, const fileName& libPath,
const string& globalFuncName, const std::string& funcName,
const dynamicCodeContext& context const dynamicCodeContext& context
) const ) const
{ {
void* handle =
void* lib =
( (
!libPath.empty() && libs().open(libPath, false) !libPath.empty() && libs().open(libPath, false)
? libs().findLibrary(libPath) ? libs().findLibrary(libPath)
: nullptr : nullptr
); );
if (!lib) if (!handle)
{ {
return; return;
} }
// provision for manual execution of code before unloading // Manual execution of code before unloading.
if (dlSymFound(lib, globalFuncName)) // This is mandatory for codedBase.
{
loaderFunctionType function =
reinterpret_cast<loaderFunctionType>
(
dlSym(lib, globalFuncName)
);
if (function) void* rawSymbol = dlSymFind(handle, funcName);
if (rawSymbol)
{
loaderType fun = reinterpret_cast<loaderType>(rawSymbol);
if (fun)
{ {
(*function)(false); // force unload (*fun)(false); // force unload
} }
else else
{ {
FatalIOErrorInFunction(context.dict()) FatalIOErrorInFunction(context.dict())
<< "Failed looking up symbol " << globalFuncName << nl << "Failed symbol lookup " << funcName.c_str() << nl
<< "from " << libPath << exit(FatalIOError); << "from " << libPath << nl
<< exit(FatalIOError);
} }
} }
if (!libs().close(libPath, false)) if (!libs().close(libPath, false))
{ {
FatalIOErrorInFunction(context.dict()) FatalIOErrorInFunction(context.dict())
<< "Failed unloading library " << libPath << "Failed unloading library " << libPath << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
} }

View File

@ -75,24 +75,28 @@ class codedBase
mutable fileName oldLibPath_; mutable fileName oldLibPath_;
// Private Member Functions // Data Types
//- Global loader/unloader function type //- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool); // Called with true on load, false on unload.
typedef void (*loaderType)(bool);
//- Load specified library and execute globalFuncName(true)
// Private Member Functions
//- Load specified library and execute funcName(true)
void* loadLibrary void* loadLibrary
( (
const fileName& libPath, const fileName& libPath,
const string& globalFuncName, const std::string& funcName,
const dynamicCodeContext& context const dynamicCodeContext& context
) const; ) const;
//- Execute globalFuncName(false) and unload specified library //- Execute funcName(false) and unload specified library
void unloadLibrary void unloadLibrary
( (
const fileName& libPath, const fileName& libPath,
const string& globalFuncName, const std::string& funcName,
const dynamicCodeContext& context const dynamicCodeContext& context
) const; ) const;

View File

@ -143,8 +143,10 @@ void Foam::dlLibraryTable::clear(bool verbose)
if (ptr == nullptr) if (ptr == nullptr)
{ {
libNames_[i].clear(); libNames_[i].clear();
continue;
} }
else if (Foam::dlClose(ptr))
if (Foam::dlClose(ptr))
{ {
DebugInFunction DebugInFunction
<< "Closed [" << i << "] " << libNames_[i] << "Closed [" << i << "] " << libNames_[i]