From eaa138443a3d0bb73c32c6e00aa7d054cec56b2e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 16 Dec 2019 15:47:01 +0100 Subject: [PATCH] STYLE: library functions using std::string --- .../db/dynamicLibrary/codedBase/codedBase.C | 88 ++++++++++--------- .../db/dynamicLibrary/codedBase/codedBase.H | 16 ++-- .../dlLibraryTable/dlLibraryTable.C | 4 +- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C index 37731c909a..e9a8541643 100644 --- a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C +++ b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C @@ -45,10 +45,11 @@ namespace Foam } -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // namespace Foam { + //! \cond fileScope static inline void writeEntryIfPresent ( @@ -70,8 +71,11 @@ static inline void writeEntryIfPresent } } //! \endcond -} +} // End namespace Foam + + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // 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 ( const fileName& libPath, - const string& globalFuncName, + const std::string& funcName, const dynamicCodeContext& context ) const { // Avoid compilation by loading an existing library - void* lib = + void* handle = ( !libPath.empty() && libs().open(libPath, false) ? libs().findLibrary(libPath) : 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 - if (dlSymFound(lib, globalFuncName)) + // Manual execution of code after loading. + // This is mandatory for codedBase. + + void* rawSymbol = dlSymFind(handle, funcName); + + if (rawSymbol) { - loaderFunctionType function = - reinterpret_cast - ( - dlSym(lib, globalFuncName) - ); + loaderType fun = reinterpret_cast(rawSymbol); - if (function) + if (fun) { - (*function)(true); // force load + (*fun)(true); // force load + ok = true; } else { FatalIOErrorInFunction(context.dict()) - << "Failed looking up symbol " << globalFuncName - << nl << "from " << libPath << exit(FatalIOError); + << "Failed symbol lookup " << funcName.c_str() << nl + << "from " << libPath << nl + << exit(FatalIOError); } } else { FatalIOErrorInFunction(context.dict()) - << "Failed looking up symbol " << globalFuncName << nl - << "from " << libPath << exit(FatalIOError); + << "Failed symbol lookup " << funcName.c_str() << nl + << "from " << libPath << nl + << exit(FatalIOError); - lib = nullptr; + handle = nullptr; if (!libs().close(libPath, false)) { FatalIOErrorInFunction(context.dict()) - << "Failed unloading library " - << libPath + << "Failed unloading library " << libPath << nl << exit(FatalIOError); } } - return lib; + return handle; } void Foam::codedBase::unloadLibrary ( const fileName& libPath, - const string& globalFuncName, + const std::string& funcName, const dynamicCodeContext& context ) const { - - void* lib = + void* handle = ( !libPath.empty() && libs().open(libPath, false) ? libs().findLibrary(libPath) : nullptr ); - if (!lib) + if (!handle) { return; } - // provision for manual execution of code before unloading - if (dlSymFound(lib, globalFuncName)) - { - loaderFunctionType function = - reinterpret_cast - ( - dlSym(lib, globalFuncName) - ); + // Manual execution of code before unloading. + // This is mandatory for codedBase. - if (function) + void* rawSymbol = dlSymFind(handle, funcName); + + if (rawSymbol) + { + loaderType fun = reinterpret_cast(rawSymbol); + + if (fun) { - (*function)(false); // force unload + (*fun)(false); // force unload } else { FatalIOErrorInFunction(context.dict()) - << "Failed looking up symbol " << globalFuncName << nl - << "from " << libPath << exit(FatalIOError); + << "Failed symbol lookup " << funcName.c_str() << nl + << "from " << libPath << nl + << exit(FatalIOError); } } if (!libs().close(libPath, false)) { FatalIOErrorInFunction(context.dict()) - << "Failed unloading library " << libPath + << "Failed unloading library " << libPath << nl << exit(FatalIOError); } } diff --git a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H index e307f76d42..48343e5276 100644 --- a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H +++ b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H @@ -75,24 +75,28 @@ class codedBase mutable fileName oldLibPath_; - // Private Member Functions + // Data Types //- 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 ( const fileName& libPath, - const string& globalFuncName, + const std::string& funcName, const dynamicCodeContext& context ) const; - //- Execute globalFuncName(false) and unload specified library + //- Execute funcName(false) and unload specified library void unloadLibrary ( const fileName& libPath, - const string& globalFuncName, + const std::string& funcName, const dynamicCodeContext& context ) const; diff --git a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C index 05f23be283..da0979a6f6 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C +++ b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C @@ -143,8 +143,10 @@ void Foam::dlLibraryTable::clear(bool verbose) if (ptr == nullptr) { libNames_[i].clear(); + continue; } - else if (Foam::dlClose(ptr)) + + if (Foam::dlClose(ptr)) { DebugInFunction << "Closed [" << i << "] " << libNames_[i]