Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 8 | #include "IBackendInternal.hpp" |
| 9 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 10 | #include <armnn/Exceptions.hpp> |
| 11 | |
| 12 | #include <string> |
| 13 | #include <dlfcn.h> |
| 14 | |
| 15 | #include <boost/format.hpp> |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | class DynamicBackendUtils |
| 21 | { |
| 22 | public: |
| 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 Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 29 | static bool IsBackendCompatible(const BackendVersion& backendVersion); |
| 30 | |
| 31 | protected: |
| 32 | /// Protected for testing purposes |
| 33 | static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion); |
| 34 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 35 | private: |
| 36 | static std::string GetDlError(); |
| 37 | |
| 38 | /// This class is to hold utility functions only |
| 39 | DynamicBackendUtils() = delete; |
| 40 | }; |
| 41 | |
| 42 | template<typename EntryPointType> |
| 43 | EntryPointType 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 Martincigh | b19d2e9 | 2019-07-25 14:04:40 +0100 | [diff] [blame] | 58 | throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError())); |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return entryPoint; |
| 62 | } |
| 63 | |
| 64 | } // namespace armnn |