Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <OpenFOAM/dlLibraryTable.H>
00027 #include <OpenFOAM/debug.H>
00028
00029 #include <dlfcn.h>
00030
00031
00032
00033 Foam::dlLibraryTable Foam::dlLibraryTable::loadedLibraries;
00034
00035
00036
00037
00038 Foam::dlLibraryTable::dlLibraryTable()
00039 :
00040 HashTable<fileName, void*, Hash<void*> >()
00041 {}
00042
00043
00044 Foam::dlLibraryTable::readDlLibrary::readDlLibrary
00045 (
00046 const dictionary& dict,
00047 const word& libsEntry
00048 )
00049 {
00050 open(dict, libsEntry);
00051 }
00052
00053
00054
00055
00056 Foam::dlLibraryTable::~dlLibraryTable()
00057 {
00058
00059
00060
00061 }
00062
00063
00064
00065 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
00066 {
00067 if (functionLibName.size())
00068 {
00069 void* functionLibPtr = NULL;
00070
00071 fileNameList searchPathList;
00072
00073
00074 if (functionLibName[0] != '/' && debug::controlDict().found("LibrarySearchPaths"))
00075 {
00076 debug::controlDict().lookup("LibrarySearchPaths") >> searchPathList;
00077 }
00078
00079
00080
00081 searchPathList.setSize(searchPathList.size()+1,fileName());
00082 forAllConstIter(fileNameList, searchPathList, pathI)
00083 {
00084
00085 fileName functionLibPath;
00086 if (!pathI->empty())
00087 functionLibPath = *pathI / functionLibName;
00088 else
00089 functionLibPath = functionLibName;
00090 functionLibPtr = dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00091
00092 #ifdef darwin
00093 if(!functionLibPtr && functionLibPath.ext()=="so") {
00094 functionLibPath=functionLibPath.lessExt()+".dylib";
00095 functionLibPtr =
00096 dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00097 }
00098 #endif
00099
00100 if (functionLibPtr)
00101 {
00102 Info<< "Loaded " << functionLibName;
00103 if (pathI->empty())
00104 Info<< " from the default search path";
00105 else
00106 Info<< " from " << functionLibPath;
00107 Info<< endl;
00108 break;
00109 }
00110 }
00111 if (!functionLibPtr)
00112 {
00113 WarningIn
00114 (
00115 "dlLibraryTable::open(const fileName& functionLibName)"
00116 ) << "could not load " << dlerror()
00117 << endl;
00118
00119 return false;
00120 }
00121 else
00122 {
00123 if (!loadedLibraries.found(functionLibPtr))
00124 {
00125 loadedLibraries.insert(functionLibPtr, functionLibName);
00126 return true;
00127 }
00128 else
00129 {
00130 return false;
00131 }
00132 }
00133 }
00134 else
00135 {
00136 return false;
00137 }
00138 }
00139
00140
00141 bool Foam::dlLibraryTable::open
00142 (
00143 const dictionary& dict,
00144 const word& libsEntry
00145 )
00146 {
00147 if (dict.found(libsEntry))
00148 {
00149 fileNameList libNames(dict.lookup(libsEntry));
00150
00151 bool allOpened = (libNames.size() > 0);
00152
00153 forAll(libNames, i)
00154 {
00155 allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
00156 }
00157
00158 return allOpened;
00159 }
00160 else
00161 {
00162 return false;
00163 }
00164 }
00165
00166
00167