9a47654477c4cceb0a1ff002899da8e9cc29d463
[platform/upstream/armnn.git] / src / backends / backendsCommon / DynamicBackendUtils.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "DynamicBackendUtils.hpp"
7
8 namespace armnn
9 {
10
11 void* DynamicBackendUtils::OpenHandle(const std::string& sharedObjectPath)
12 {
13     if (sharedObjectPath.empty())
14     {
15         throw RuntimeException("OpenHandle error: shared object path must not be empty");
16     }
17
18     void* sharedObjectHandle = dlopen(sharedObjectPath.c_str(), RTLD_LAZY | RTLD_GLOBAL);
19     if (!sharedObjectHandle)
20     {
21         throw RuntimeException(boost::str(boost::format("OpenHandle error: %1%") % GetDlError()));
22     }
23
24     return sharedObjectHandle;
25 }
26
27 void DynamicBackendUtils::CloseHandle(const void* sharedObjectHandle)
28 {
29     if (!sharedObjectHandle)
30     {
31         return;
32     }
33
34     dlclose(const_cast<void*>(sharedObjectHandle));
35 }
36
37 std::string DynamicBackendUtils::GetDlError()
38 {
39     const char* errorMessage = dlerror();
40     if (!errorMessage)
41     {
42         return "";
43     }
44
45     return std::string(errorMessage);
46 }
47
48 } // namespace armnn