Caffe2 - C++ API
A deep learning, cross platform ML framework
dynamic_library_unix.cpp
1 #include <c10/util/Exception.h>
2 #include <torch/csrc/jit/fuser/cpu/dynamic_library.h>
3 #include <torch/csrc/utils/disallow_copy.h>
4 
5 #include <dlfcn.h>
6 #include <libgen.h>
7 
8 namespace torch {
9 namespace jit {
10 namespace fuser {
11 namespace cpu {
12 
13 static void* checkDL(void* x) {
14  if (!x) {
15  AT_ERROR("error in dlopen or dlsym: ", dlerror());
16  }
17 
18  return x;
19 }
20 DynamicLibrary::DynamicLibrary(const char* name) {
21  // NOLINTNEXTLINE(hicpp-signed-bitwise)
22  handle = checkDL(dlopen(name, RTLD_LOCAL | RTLD_NOW));
23 }
24 
25 void* DynamicLibrary::sym(const char* name) {
26  AT_ASSERT(handle);
27  return checkDL(dlsym(handle, name));
28 }
29 
30 DynamicLibrary::~DynamicLibrary() {
31  if (!handle)
32  return;
33  dlclose(handle);
34 }
35 
36 std::string DynamicLibrary::directoryOf(void* addr) {
37  Dl_info info = {};
38  if (!dladdr(addr, &info)) {
39  AT_ERROR("could not look up address: ", addr);
40  }
41  std::string name = info.dli_fname;
42  std::vector<char> path(name.begin(), name.end());
43  char* directory = dirname(path.data());
44  return directory;
45 }
46 
47 } // namespace cpu
48 } // namespace fuser
49 } // namespace jit
50 } // namespace torch
Definition: jit_type.h:17