NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
DynamicPluginLoader.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 #ifndef NVNEURAL_DYNAMICPLUGINLOADER_H
26 #define NVNEURAL_DYNAMICPLUGINLOADER_H
27 
28 #include <nvneural/CoreHelpers.h>
29 #include <nvneural/PluginTypes.h>
30 #include <nvneural/RefObject.h>
31 #include <string>
32 #include <vector>
33 
34 namespace nvneural {
35 
37 class DynamicPluginLoader : public refobj::RefObjectBase<refobj::Implements<IPluginLoader>>
38 {
39 public:
41  NeuralResult loadLibrary(const char* pFilename, IPlugin** ppPluginOut) noexcept override;
42 
44  NeuralResult loadDirectory(const char* pPathname, std::uint32_t* pNumberLoadedOut) noexcept override;
45 
47  NeuralResult releasePlugin(IPlugin* pPlugin) noexcept override;
49  NeuralResult releaseAllPlugins() noexcept override;
50 
52  std::size_t pluginCount() const noexcept override;
54  IPlugin* getPluginByIndex(std::size_t pluginIndex) const noexcept override;
56  NeuralResult getPluginPath(const IPlugin* pPlugin, const char** ppPluginPathOut) const noexcept override;
57 
59  void allowIncompatiblePlugins() noexcept override;
60 
61 private:
62  struct LoadedPlugin
63  {
64  IPlugin* pPlugin; // managed directly
65  void* pLibrary; // HMODULE on Windows, void* on Linux
66  std::string path;
67 
68  LoadedPlugin(IPlugin* pPlugin_, void* pLibrary_, const std::string& path_);
69  ~LoadedPlugin(); // frees library on destruction
70 
71  // Disallow copy operations because we don't want to support multiply loaded libraries
72  LoadedPlugin(const LoadedPlugin& copyFrom) = delete;
73  LoadedPlugin& operator =(const LoadedPlugin& copyFrom) = delete;
74 
75  // Explicit move operations are needed to prevent double-freeing of plugin resources
76  LoadedPlugin(LoadedPlugin&& moveFrom);
77  LoadedPlugin& operator =(LoadedPlugin&& moveFrom);
78  };
79  std::vector<LoadedPlugin> m_loadedPlugins;
80 
81  bool m_allowIncompatiblePlugins = false;
82  bool m_warnedAboutIncompatiblePlugins = false;
83 
84  bool isIncompatiblePlugin(const IPlugin* pPlugin) const noexcept;
85 
86 #if defined(_WIN32) || defined(DOXYGEN)
91  NeuralResult loadDynamicLibrary(const std::u16string& libraryPath) noexcept;
92 #endif
93 };
94 
95 } // namespace nvneural
96 
97 #endif // NVNEURAL_DYNAMICPLUGINLOADER_H
Common helper classes and template function implementations.
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Interfaces and functions that are implemented by plugin libraries.
Standard implementation for IRefObject-derived objects.
Standard IPluginLoader implementation that uses dynamic linking (dlopen/LoadLibrary) to find NvNeural...
Definition: DynamicPluginLoader.h:38
NeuralResult loadDirectory(const char *pPathname, std::uint32_t *pNumberLoadedOut) noexcept
Loads all compatible plugins from the provided directory.
Definition: DynamicPluginLoader_Linux.cpp:129
std::size_t pluginCount() const noexcept
Returns the number of plugins tracked by this object.
Definition: DynamicPluginLoader_Common.cpp:78
NeuralResult loadLibrary(const char *pFilename, IPlugin **ppPluginOut) noexcept
Loads a plugin from a specific shared library file.
Definition: DynamicPluginLoader_Linux.cpp:47
NeuralResult releasePlugin(IPlugin *pPlugin) noexcept
Releases the internal reference to a given plugin object and frees the library.
Definition: DynamicPluginLoader_Common.cpp:56
NeuralResult releaseAllPlugins() noexcept
Releases all plugins tracked by this object.
Definition: DynamicPluginLoader_Common.cpp:72
IPlugin * getPluginByIndex(std::size_t pluginIndex) const noexcept
Returns a particular IPlugin object by index, or nullptr if the index is out of range.
Definition: DynamicPluginLoader_Common.cpp:83
NeuralResult getPluginPath(const IPlugin *pPlugin, const char **ppPluginPathOut) const noexcept
Returns the file path to a particular plugin object.
Definition: DynamicPluginLoader_Common.cpp:93
void allowIncompatiblePlugins() noexcept
Override the current compatibility checks to allow the loading of out-of-date plugins.
Definition: DynamicPluginLoader_Common.cpp:109
IPlugin is the general factory interface used by NvNeural's plugin system.
Definition: PluginTypes.h:41
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336