blob: 0a2d428fd3743092f32d6c6b063a6ca8476ead10 [file] [log] [blame]
Matteo Martincighd73cecb2019-07-24 09:15:00 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matteo Martincighac60d282019-07-25 15:25:44 +01008#include "IBackendInternal.hpp"
9
Matteo Martincighd73cecb2019-07-24 09:15:00 +010010#include <armnn/Exceptions.hpp>
11
12#include <string>
13#include <dlfcn.h>
14
15#include <boost/format.hpp>
16
17namespace armnn
18{
19
20class DynamicBackendUtils
21{
22public:
23 static void* OpenHandle(const std::string& sharedObjectPath);
24 static void CloseHandle(const void* sharedObjectHandle);
25
26 template<typename EntryPointType>
27 static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName);
28
Matteo Martincighac60d282019-07-25 15:25:44 +010029 static bool IsBackendCompatible(const BackendVersion& backendVersion);
30
31protected:
32 /// Protected for testing purposes
33 static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
34
Matteo Martincighd73cecb2019-07-24 09:15:00 +010035private:
36 static std::string GetDlError();
37
38 /// This class is to hold utility functions only
39 DynamicBackendUtils() = delete;
40};
41
42template<typename EntryPointType>
43EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
44{
45 if (sharedObjectHandle == nullptr)
46 {
47 throw RuntimeException("GetEntryPoint error: invalid handle");
48 }
49
50 if (symbolName == nullptr)
51 {
52 throw RuntimeException("GetEntryPoint error: invalid symbol");
53 }
54
55 auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName));
56 if (!entryPoint)
57 {
Matteo Martincighb19d2e92019-07-25 14:04:40 +010058 throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));
Matteo Martincighd73cecb2019-07-24 09:15:00 +010059 }
60
61 return entryPoint;
62}
63
64} // namespace armnn