IVGCVSW-3563 + IVGCVSW-3555 Create new utility functions for dynamic backends
authorMatteo Martincigh <matteo.martincigh@arm.com>
Wed, 24 Jul 2019 08:15:00 +0000 (09:15 +0100)
committerNikhil Raj Arm <nikhil.raj@arm.com>
Wed, 24 Jul 2019 14:19:58 +0000 (14:19 +0000)
 * Created new DynamicBackendUtils class
 * Added OpenHandle, CloseHandle, GetEntryPoint and GetDlError methods to it

Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Change-Id: I88c683b2c5d37968a9ebdf335be932ae2d9061e5

src/backends/backendsCommon/CMakeLists.txt
src/backends/backendsCommon/DynamicBackendUtils.cpp [new file with mode: 0644]
src/backends/backendsCommon/DynamicBackendUtils.hpp [new file with mode: 0644]

index bc1c15b..bb31ce3 100644 (file)
@@ -9,6 +9,8 @@ list(APPEND armnnBackendsCommon_sources
     CpuTensorHandle.cpp
     CpuTensorHandleFwd.hpp
     CpuTensorHandle.hpp
+    DynamicBackendUtils.cpp
+    DynamicBackendUtils.hpp
     IBackendInternal.hpp
     IBackendContext.hpp
     ITensorHandleFactory.cpp
diff --git a/src/backends/backendsCommon/DynamicBackendUtils.cpp b/src/backends/backendsCommon/DynamicBackendUtils.cpp
new file mode 100644 (file)
index 0000000..f5ee12c
--- /dev/null
@@ -0,0 +1,48 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "DynamicBackendUtils.hpp"
+
+namespace armnn
+{
+
+void* DynamicBackendUtils::OpenHandle(const std::string& sharedObjectPath)
+{
+    if (sharedObjectPath.empty())
+    {
+        throw RuntimeException("OpenHandle error: shared object path must not be empty");
+    }
+
+    void* sharedObjectHandle = dlopen(sharedObjectPath.c_str(), RTLD_LAZY | RTLD_GLOBAL);
+    if (!sharedObjectHandle)
+    {
+        throw RuntimeException(boost::str(boost::format("OpenHandle error: %1") % GetDlError()));
+    }
+
+    return sharedObjectHandle;
+}
+
+void DynamicBackendUtils::CloseHandle(const void* sharedObjectHandle)
+{
+    if (!sharedObjectHandle)
+    {
+        return;
+    }
+
+    dlclose(const_cast<void*>(sharedObjectHandle));
+}
+
+std::string DynamicBackendUtils::GetDlError()
+{
+    const char* errorMessage = dlerror();
+    if (!errorMessage)
+    {
+        return "";
+    }
+
+    return std::string(errorMessage);
+}
+
+} // namespace armnn
diff --git a/src/backends/backendsCommon/DynamicBackendUtils.hpp b/src/backends/backendsCommon/DynamicBackendUtils.hpp
new file mode 100644 (file)
index 0000000..6bedec4
--- /dev/null
@@ -0,0 +1,56 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <armnn/Exceptions.hpp>
+
+#include <string>
+#include <dlfcn.h>
+
+#include <boost/format.hpp>
+
+namespace armnn
+{
+
+class DynamicBackendUtils
+{
+public:
+    static void* OpenHandle(const std::string& sharedObjectPath);
+    static void CloseHandle(const void* sharedObjectHandle);
+
+    template<typename EntryPointType>
+    static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName);
+
+private:
+    static std::string GetDlError();
+
+    /// This class is to hold utility functions only
+    DynamicBackendUtils() = delete;
+};
+
+template<typename EntryPointType>
+EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
+{
+    if (sharedObjectHandle == nullptr)
+    {
+        throw RuntimeException("GetEntryPoint error: invalid handle");
+    }
+
+    if (symbolName == nullptr)
+    {
+        throw RuntimeException("GetEntryPoint error: invalid symbol");
+    }
+
+    auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName));
+    if (!entryPoint)
+    {
+        throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1") % GetDlError()));
+    }
+
+    return entryPoint;
+}
+
+} // namespace armnn