LCOV - code coverage report
Current view: top level - common - bqs_so_manager.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.7 % 41 38
Test Date: 2026-08-12 11:05:07 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "bqs_so_manager.h"
      12              : 
      13              : #include <dlfcn.h>
      14              : #include "bqs_log.h"
      15              : 
      16              : namespace bqs {
      17          114 : void* SoManager::GetFuncHandle(const std::string& funcName) const
      18              : {
      19          114 :     if (soHandle_ == nullptr) {
      20           10 :         BQS_LOG_ERROR(
      21              :             "Get func handle failed by so handle is nullptr, soName=%s, funcName=%s.", soName_.c_str(),
      22              :             funcName.c_str());
      23           10 :         return nullptr;
      24              :     }
      25              : 
      26          104 :     const auto& iter = funcHandleMap_.find(funcName);
      27          104 :     if (iter == funcHandleMap_.end()) {
      28            2 :         BQS_LOG_ERROR("Func not loaded during init, soName=%s, funcName=%s.", soName_.c_str(), funcName.c_str());
      29            2 :         return nullptr;
      30              :     }
      31              : 
      32          102 :     return iter->second;
      33              : }
      34              : 
      35           15 : bool SoManager::IsSoLoad() const { return soHandle_ != nullptr; }
      36              : 
      37           14 : void SoManager::OpenSo()
      38              : {
      39           14 :     if (soName_.empty()) {
      40            2 :         BQS_LOG_ERROR("Open so failed by so name is empty.");
      41            2 :         return;
      42              :     }
      43              : 
      44           12 :     soHandle_ = dlopen(soName_.c_str(), RTLD_LAZY);
      45           12 :     if (soHandle_ == nullptr) {
      46            8 :         BQS_LOG_ERROR("Open so failed, soName=%s, reason=%s.", soName_.c_str(), dlerror());
      47              :     }
      48              : 
      49           12 :     return;
      50              : }
      51              : 
      52           14 : void SoManager::CloseSo()
      53              : {
      54           14 :     if (soHandle_ == nullptr) {
      55           11 :         return;
      56              :     }
      57              : 
      58            3 :     const int32_t ret = dlclose(soHandle_);
      59            3 :     if (ret != 0) {
      60            0 :         BQS_LOG_WARN("Close so failed, soName=%s, reason=%s.", soName_.c_str(), dlerror());
      61              :     }
      62              : 
      63            3 :     soHandle_ = nullptr;
      64            3 :     return;
      65              : }
      66              : 
      67            4 : void SoManager::BatchLoadFunc(const std::vector<std::string>& funcNames)
      68              : {
      69            4 :     if (soHandle_ == nullptr) {
      70            0 :         BQS_LOG_ERROR("Get func handle failed by so handle is nullptr, soName=%s.", soName_.c_str());
      71            0 :         return;
      72              :     }
      73              : 
      74           15 :     for (const std::string& funcName : funcNames) {
      75           11 :         void* funcPtr = GetFuncHandleFromSo(funcName);
      76           11 :         if (funcPtr != nullptr) {
      77           10 :             (void)funcHandleMap_.emplace(funcName, funcPtr);
      78              :         }
      79              :     }
      80              : 
      81            4 :     return;
      82              : }
      83              : 
      84           11 : void* SoManager::GetFuncHandleFromSo(const std::string& funcName) const
      85              : {
      86           11 :     void* funcPtr = dlsym(soHandle_, funcName.c_str());
      87           11 :     if (funcPtr == nullptr) {
      88            1 :         BQS_LOG_ERROR(
      89              :             "Get func handle from so failed, soName=%s, funcName=%s, error=%s.", soName_.c_str(), funcName.c_str(),
      90              :             dlerror());
      91            1 :         return nullptr;
      92              :     }
      93              : 
      94           10 :     return funcPtr;
      95              : }
      96              : } // namespace bqs
        

Generated by: LCOV version 2.0-1