GCC Code Coverage Report


./
File: libs/__/core/runtime/detail/dl/posix.cpp
Date: 2025-01-21 16:21:04
Lines:
11/31
35.5%
Functions:
4/6
66.7%
Branches:
2/20
10.0%

Line Branch Exec Source
1 /************************************************************************
2 *
3 * Copyright (C) 2009-2023 IRCAD France
4 * Copyright (C) 2012-2020 IHU Strasbourg
5 *
6 * This file is part of Sight.
7 *
8 * Sight is free software: you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Sight is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
20 *
21 ***********************************************************************/
22
23 #if defined(__unix__)
24
25 #include "core/runtime/detail/dl/posix.hpp"
26
27 #include "core/runtime/module.hpp"
28
29 namespace sight::core::runtime::detail::dl
30 {
31
32 //------------------------------------------------------------------------------
33
34 1925 posix::posix(const std::filesystem::path& _module_path) noexcept :
35 1925 native(_module_path)
36 {
37 1925 }
38
39 //------------------------------------------------------------------------------
40
41 3850 posix::~posix() noexcept =
42 default;
43
44 //------------------------------------------------------------------------------
45
46 252 bool posix::is_loaded() const noexcept
47 {
48 252 return m_handle != nullptr;
49 }
50
51 //------------------------------------------------------------------------------
52
53 void* posix::get_symbol(const std::string& _name) const
54 {
55 void* result = nullptr;
56 if(is_loaded())
57 {
58 dlerror(); /* Clear existing error */
59 result = dlsym(m_handle, _name.c_str());
60 if(result == nullptr) /* Check for possible errors */
61 {
62 std::string message(dlerror());
63 if(!message.empty())
64 {
65 throw runtime_exception("Symbol retrieval failed. " + message);
66 }
67 }
68 }
69
70 return result;
71 }
72
73 //------------------------------------------------------------------------------
74
75 253 void posix::load()
76 {
77
1/2
✓ Branch 0 taken 253 times.
✗ Branch 1 not taken.
253 if(m_handle == nullptr)
78 {
79 // Opens the dynamic library.
80 505 m_handle = dlopen(get_full_path().string().c_str(), RTLD_LAZY | RTLD_GLOBAL);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 if(m_handle == nullptr)
82 {
83 std::string message(dlerror());
84 throw runtime_exception("Module load failed. " + message);
85 }
86 }
87 252 }
88
89 //------------------------------------------------------------------------------
90
91 void posix::unload()
92 {
93 if(m_handle != nullptr)
94 {
95 int result = 0;
96 result = dlclose(m_handle);
97 if(result != 0)
98 {
99 std::string message(dlerror());
100 throw runtime_exception("Module unload failed. " + message);
101 }
102
103 m_handle = nullptr;
104 }
105 }
106
107 //------------------------------------------------------------------------------
108
109 } // namespace sight::core::runtime::detail::dl
110
111 #endif // #if defined(__unix__)
112