dlLibraryTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dlLibraryTable.H"
27 #include "OSspecific.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
34 }
35 
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42 {}
43 
44 
46 (
47  const dictionary& dict,
48  const word& libsEntry
49 )
50 {
51  open(dict, libsEntry);
52 }
53 
54 
55 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
56 
58 {
59  forAllReverse(libPtrs_, i)
60  {
61  if (libPtrs_[i])
62  {
63  if (debug)
64  {
66  << "Closing " << libNames_[i]
67  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
68  }
69  if (!dlClose(libPtrs_[i]))
70  {
71  WarningInFunction<< "Failed closing " << libNames_[i]
72  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
73  }
74  }
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 (
83  const fileName& libName,
84  const bool verbose
85 )
86 {
87  if (libName.size())
88  {
89  void* libPtr = dlOpen
90  (
91  fileName(libName).expand(),
92  verbose
93  );
94 
95  if (debug)
96  {
98  << "Opened " << libName
99  << " resulting in handle " << uintptr_t(libPtr) << endl;
100  }
101 
102  if (!libPtr)
103  {
104  if (verbose)
105  {
107  << "could not load " << libName
108  << endl;
109  }
110 
111  return false;
112  }
113  else
114  {
115  libPtrs_.append(libPtr);
116  libNames_.append(libName);
117  return true;
118  }
119  }
120  else
121  {
122  return false;
123  }
124 }
125 
126 
128 (
129  const fileNameList& libNames,
130  const bool verbose
131 )
132 {
133  bool allOpened = !libNames.empty();
134 
135  forAll(libNames, i)
136  {
137  allOpened = open(libNames[i], verbose) && allOpened;
138  }
139 
140  return allOpened;
141 }
142 
143 
145 (
146  const dictionary& dict,
147  const word& libsEntry
148 )
149 {
150  if (dict.found(libsEntry))
151  {
152  fileNameList libNames(dict.lookup(libsEntry));
153 
154  bool allOpened = !libNames.empty();
155 
156  forAll(libNames, i)
157  {
158  allOpened = open(libNames[i]) && allOpened;
159  }
160 
161  return allOpened;
162  }
163  else
164  {
165  return false;
166  }
167 }
168 
169 
171 (
172  const regExp& pattern,
173  const bool verbose
174 )
175 {
176  DynamicList<fileName> libNames;
177  libNames.append(readDir(getEnv("FOAM_LIBBIN")));
178  libNames.append(readDir(getEnv("FOAM_SITE_LIBBIN")));
179  libNames.append(readDir(getEnv("FOAM_USER_LIBBIN")));
180 
181  bool allOpened = !libNames.empty();
182 
183  forAll(libNames, i)
184  {
185  if (pattern.match(libNames[i]))
186  {
187  allOpened = open(libNames[i], verbose) && allOpened;
188  }
189  }
190 
191  return allOpened;
192 }
193 
194 
196 (
197  const fileName& libName,
198  const bool verbose
199 )
200 {
201  label index = -1;
202  forAllReverse(libNames_, i)
203  {
204  if (libNames_[i] == libName)
205  {
206  index = i;
207  break;
208  }
209  }
210 
211  if (index != -1)
212  {
213  if (debug)
214  {
216  << "Closing " << libName
217  << " with handle " << uintptr_t(libPtrs_[index]) << endl;
218  }
219 
220  bool ok = dlClose(libPtrs_[index]);
221 
222  libPtrs_[index] = nullptr;
223  libNames_[index] = fileName::null;
224 
225  if (!ok)
226  {
227  if (verbose)
228  {
230  << "could not close " << libName
231  << endl;
232  }
233 
234  return false;
235  }
236 
237  return true;
238  }
239  return false;
240 }
241 
242 
244 {
245  label index = -1;
246  forAllReverse(libNames_, i)
247  {
248  if (libNames_[i] == libName)
249  {
250  index = i;
251  break;
252  }
253  }
254 
255  if (index != -1)
256  {
257  return libPtrs_[index];
258  }
259  return nullptr;
260 }
261 
262 
263 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:461
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:669
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
A table of dynamically loaded libraries.
bool close(const fileName &name, const bool verbose=true)
Close the named library, optionally with warnings if problems occur.
~dlLibraryTable()
Destructor.
void * findLibrary(const fileName &libName)
Find the handle of the named library.
dlLibraryTable()
Construct null.
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
bool openPattern(const regExp &pattern, const bool verbose=true)
Open the named libraries, optionally with warnings if problems occur.
A class for handling file names.
Definition: fileName.H:82
static const fileName null
An empty fileName.
Definition: fileName.H:97
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:62
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:201
A class for handling words, derived from string.
Definition: word.H:63
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
Namespace for OpenFOAM.
dlLibraryTable libs
Table of loaded dynamic libraries.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1236
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1264
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
fileNameList readDir(const fileName &, const fileType=fileType::file, const bool filterVariants=true, const bool followLink=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:662
string expand(const string &s, string::size_type &index, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty)
Definition: stringOps.C:146
dictionary dict