lin_shared_object_loader.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief POSIX compatible loader for a shared object
7  *
8  * @file lin_shared_object_loader.h
9  */
10 #pragma once
11 
12 #include <dlfcn.h>
13 
14 #include "ie_api.h"
15 #include "details/ie_exception.hpp"
17 
18 namespace InferenceEngine {
19 namespace details {
20 
21 /**
22  * @brief This class provides an OS shared module abstraction
23  */
24 class SharedObjectLoader {
25 private:
26  void* shared_object = nullptr;
27 
28 public:
29  /**
30  * @brief A shared pointer to SharedObjectLoader
31  */
32  using Ptr = std::shared_ptr<InferenceEngine::details::SharedObjectLoader>;
33 
34  /**
35  * @brief Loads a library with the name specified. The library is loaded according to
36  * the POSIX rules for dlopen
37  * @param pluginName Full or relative path to the library
38  */
39  explicit SharedObjectLoader(const char* pluginName) {
40  shared_object = dlopen(pluginName, RTLD_LAZY);
41 
42  if (shared_object == nullptr)
43  THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << dlerror();
44  }
45 
46 #ifdef ENABLE_UNICODE_PATH_SUPPORT
47  /**
48  * @brief Loads a library with the name specified. The library is loaded according to
49  * the POSIX rules for dlopen
50  * @param pluginName Full or relative path to the library
51  */
52  explicit SharedObjectLoader(const wchar_t* pluginName) : SharedObjectLoader(wStringtoMBCSstringChar(pluginName).c_str()) {
53  }
54 
55 #endif // ENABLE_UNICODE_PATH_SUPPORT
56 
57  ~SharedObjectLoader() noexcept(false) {
58  if (0 != dlclose(shared_object)) {
59  THROW_IE_EXCEPTION << "dlclose failed: " << dlerror();
60  }
61  }
62 
63  /**
64  * @brief Searches for a function symbol in the loaded module
65  * @param symbolName Name of the function to find
66  * @return A pointer to the function if found
67  * @throws InferenceEngineException if the function is not found
68  */
69  void* get_symbol(const char* symbolName) const {
70  void* procAddr = nullptr;
71 
72  procAddr = dlsym(shared_object, symbolName);
73  if (procAddr == nullptr)
74  THROW_IE_EXCEPTION << "dlSym cannot locate method '" << symbolName << "': " << dlerror();
75  return procAddr;
76  }
77 };
78 
79 } // namespace details
80 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Definition: cldnn_config.hpp:16
The macro defines a symbol import/export mechanism essential for Microsoft Windows(R) OS...
This is a header file with functions related to filesystem operations.
A header file for the main Inference Engine exception.