Free Electron
Registry.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __plugin_Registry_h__
8 #define __plugin_Registry_h__
9 
10 namespace fe
11 {
12 
13 class Catalog;
14 
15 /**************************************************************************//**
16  @brief Dynamic Library Manager
17 
18  @ingroup plugin
19 
20  Libraries can only be safely loaded and unloaded in a FILO
21  (first-in last-out) manner.
22  These restrictions are handled internally, but it may require
23  some forethought to optimize memory usage by reducing library retention.
24 *//***************************************************************************/
25 class FE_DL_EXPORT Registry:
26  public Initialize<Registry>,
27  public Handled<Registry>,
28  public ObjectSafe<Registry>
29 {
30  public:
31  Registry(void);
32  Registry(Master* pMaster);
33 virtual ~Registry(void);
34 
35 virtual void initialize(void);
36 
37  /** @brief Add a path search for managed
38  dynamic libraries.
39 
40  Currently, this only used on Windows. */
41  Result addPath(String a_absPath);
42 
43  /** @brief Register the factories in the named
44  dynamic library
45 
46  @em adaptname will append the platform-specific
47  prefix and suffix to an abbreviated @em filename,
48  such as from 'MyLib' to 'libMyLib.so',
49 
50  @em manageDependencies will automatically preload
51  any libraries that the given library says
52  it needs. */
53  Result manage(const String &filename, bool adaptname = true,
54  bool manageDependencies = true);
55 
56  /** @brief Register the factories in the Library object
57 
58  In cases where using a dso is not practical,
59  the method can add factories directly from an
60  existing Library object. */
61  Result manage(sp<Library>& a_rspLibrary);
62 
63  /** @brief (prototypical) Release interest in the
64  named library
65 
66  This doesn't necessarily unload the library
67  from memory.
68 
69  In the current system, trying to re-manage
70  a library may be unpredictable. */
71  Result unmanage(const String &filename);
72 
73  /** @brief Substitute a dynamic library
74 
75  Further create() calls will load and
76  use the new library. */
77  void substituteLibrary(const String &oldDlName,
78  const String &newDlName);
79 
80  Result registerLibrary(sp<Library> spLibrary);
81 
82  /** @brief Potentially release libraries who don't
83  have instantiated components */
84  U32 prune(void);
85 
86  /** @brief Return a list of available implementations
87  that match the string pattern */
88  I32 listAvailable(const String &pattern,
89  Array<String>& available) const;
90 
91  /** @brief Adjust the priority of matching a
92  particular implementation */
93  void prioritize(const String &a_implementation,
94  I32 a_priority);
95 
96  /** @brief Instantiate a Component of the given
97  named implementation */
98  sp<Component> create(const String &a_pattern,
99  BWORD quiet=FALSE) const;
100  sp<Component> create(const char* a_pattern,
101  BWORD quiet=FALSE) const;
102  sp<Component> create(Regex &a_regex,
103  BWORD quiet=FALSE) const;
104 
105  /** @brief Instantiate a Counted of the given
106  named implementation */
107  sp<Counted> create(String implementation,String name) const;
108 
109  /// @brief Access the Master (ptr may not be valid)
110  sp<Master> master(void) const;
111 
112  /// @brief Access the table of loadable components
113  sp<Catalog> manifest(void);
114 
115  /// @brief Access the table of loadable types
116  sp<Catalog> typeManifest(void);
117 
118  /** @brief Access the component tracker
119 
120  @internal */
121  Tracker& tracker(void) { return m_tracker; };
122 
123 const String& name(void) const { return m_name; }
124 
125  void dump(void) const;
126  void dumpLibraries(void) const;
127 
128  /** @brief reference a Counted that is released
129  just before unloading libraries
130 
131  Any number of dependents can be added. */
132  void addDependent(sp<Counted> spCounted)
133  { m_dependents.push_back(spCounted); }
134 
135  public:
136  struct FactoryLocation
137  {
138  public:
139  FactoryLocation(sp<Library> spLibrary,
140  U32 factoryIndex):
141  m_spLibrary(spLibrary),
142  m_factoryIndex(factoryIndex),
143  m_priority(0) {}
144  sp<Library> m_spLibrary;
145  U32 m_factoryIndex;
146  I32 m_priority;
147  };
148  std::map<String,FactoryLocation*>
149  &factoryMap(void) { return m_factoryMap; }
150 
151  private:
152  void clear(void);
153 
154  sp<Component> createInternal(const String* a_pattern,
155  Regex* a_pRegex,
156  BWORD quiet=FALSE) const;
157 
158  class TypeLoader:
159  public TypeMaster::TypeLoader,
160  public CastableAs<TypeLoader>
161  {
162  public:
163  virtual Result loadType(String a_name);
164  hp<Registry> m_hpRegistry;
165  };
166 
167  class LoadedLibrary
168  {
169  public:
170  LoadedLibrary(sp<Library> spLibrary):
171  m_spLibrary(spLibrary),
172  m_factoriesAccepted(0)
173  { FEASSERT(spLibrary.isValid()); }
174  ~LoadedLibrary(void);
175 
176  sp<Library> m_spLibrary;
177  U32 m_factoriesAccepted;
178  };
179 
180  class Candidate
181  {
182  public:
183  Candidate(void):
184  m_pFactoryLocation(NULL),
185  m_priority(0) {}
186  Candidate(I32):
187  m_pFactoryLocation(NULL),
188  m_priority(0) {}
189 
190  FactoryLocation* m_pFactoryLocation;
191  String m_factoryName;
192  String m_dlname;
193  I32 m_priority;
194  };
195 
196 
197  I32 findIndex(const String &filename) const;
198  void unmanage(U32 index);
199 
200  String m_name;
201 
202  //* Ordered FILO, one DL_Loader per dynamic library.
203  Array<LoadedLibrary*> m_loaderStack;
204 
205  Array< sp<Counted> > m_dependents;
206 
207  std::map<String,FactoryLocation*> m_factoryMap;
208 
209  Tracker m_tracker;
210 
211  hp<Master> m_hpMaster;
212  sp<Catalog> m_spManifest;
213  sp<Catalog> m_spTypeManifest;
214 };
215 
216 
217 } /* namespace */
218 
219 #endif /* __plugin_Registry_h__ */
Object level locking for thread safety.
Definition: Safe.h:216
kernel
Definition: namespace.dox:3
Dynamic Library Manager.
Definition: Registry.h:25
Per-class participation in the Initialized <> mechanism.
Definition: Initialized.h:117
Watches the usage of reference counted objects.
Definition: Tracker.h:25
Tracker & tracker(void)
Access the component tracker.
Definition: Registry.h:121
Automatically reference-counted string container.
Definition: String.h:128
void addDependent(sp< Counted > spCounted)
reference a Counted that is released just before unloading libraries
Definition: Registry.h:132
Wrapper for std::vector.
Definition: Array.h:21
Result
Generalized return value indicating success or failure, and why.
Definition: Result.h:24
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53
Base class providing an fe::Handle to the derived class.
Definition: Handled.h:209
Central access point for key pseudo-global objects.
Definition: Master.h:21
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192