2 * Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 #include <vist/dynamic-loader.hpp>
21 DynamicLoader::DynamicLoader(const std::string& path, int flag)
22 : scopedHandle(init(path, flag))
26 DynamicLoader::ScopedHandle DynamicLoader::init(const std::string& path, int flag)
28 auto open = [&]() -> void* {
31 auto handle = ::dlopen(path.c_str(), flag);
32 if (handle == nullptr) {
33 if (auto error = ::dlerror(); error != nullptr)
34 ERROR(VIST) << "Failed to open library: " << error;
36 THROW(ErrCode::RuntimeError) << "Failed to open library: " << path;
42 auto close = [&](void* /*handle*/) {
45 // Calling symbol & code after dlclose() makes SEGFAULT.
46 // TODO: Sync dynamic loading's life-cycle with program.
48 // if (::dlclose(handle) != 0)
49 // THROW(ErrCode::RuntimeError) << "Failed to close library: " << ::dlerror();
52 return ScopedHandle(open(), close);