LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/resource/mem - mem_host.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.1 % 67 55
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 10 10

            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 "mem_host.h"
      12              : #include "adapter_rts.h"
      13              : 
      14              : namespace hccl {
      15         2915 : HostMem::HostMem(void *ptr, u64 size, bool owner, bool isRtsMem) : ptr_(ptr), size_(size), owner_(owner),
      16         2915 :     isRtsMem_(isRtsMem)
      17              : {
      18         2915 : }
      19              : 
      20         1308 : HostMem::HostMem(const HostMem &that) : ptr_(that.ptr()), size_(that.size_), owner_(false), isRtsMem_(that.isRtsMem_)
      21              : {
      22         1308 : }
      23              : 
      24         1598 : HostMem::HostMem(HostMem &&that) : ptr_(that.ptr_), size_(that.size_), owner_(that.owner_), isRtsMem_(that.isRtsMem_)
      25              : {
      26         1598 :     that.ptr_ = nullptr;
      27         1598 :     that.size_ = 0;
      28         1598 :     that.owner_ = false;
      29         1598 : }
      30              : 
      31        11399 : HostMem::~HostMem()
      32              : {
      33        11399 :     if (owner_ && ptr_) {
      34         2045 :         HCCL_DEBUG("size_[%llu Byte]", size_);
      35              : 
      36         2043 :         if (isRtsMem_) {
      37         2041 :             HcclResult ret = hrtFreeHost(ptr_);
      38         2043 :             if (ret != HCCL_SUCCESS) {
      39            0 :                 HCCL_WARNING("rt_free error, ret[%d]", ret);
      40              :             }
      41              :         } else {
      42            2 :             delete[] static_cast<u8 *>(ptr_);
      43              :         }
      44              :     }
      45        11399 : }
      46              : 
      47            2 : void HostMem::free()
      48              : {
      49            2 :     if (ptr_) {
      50            0 :         HCCL_DEBUG("[HostMem][free] size_[%llu Byte]", size_);
      51              : 
      52            0 :         if (isRtsMem_) {
      53            0 :             HcclResult ret = hrtFreeHost(ptr_);
      54            0 :             if (ret != HCCL_SUCCESS) {
      55            0 :                 HCCL_WARNING("[HostMem][free] rt_free error, ret[%d]", ret);
      56              :             }
      57              :         } else {
      58            0 :             delete[] static_cast<u8 *>(ptr_);
      59              :         }
      60            0 :         ptr_ = nullptr;
      61              :     }
      62            2 : }
      63              : 
      64         2039 : HostMem HostMem::alloc(u64 size, bool isRtsMem)
      65              : {
      66         2039 :     void *ptr = nullptr;
      67         2039 :     if (isRtsMem) {
      68         2039 :         HcclResult ret = hrtMallocHost(&ptr, size);
      69         2043 :         if (ret != HCCL_SUCCESS) {
      70            0 :             HCCL_ERROR("[HostMem][Alloc]rtMallocHost error, ret[%d], size[%llu Byte]", ret, size);
      71              :         }
      72              :     } else {
      73            0 :         ptr = new (std::nothrow) u8[size];
      74              :     }
      75         2045 :     if (ptr == nullptr) {
      76            0 :         HCCL_WARNING("HostMem alloc ptr null");
      77              :     }
      78         2045 :     HostMem mem(ptr, size, true, isRtsMem);
      79         2043 :     return mem;
      80              : }
      81              : 
      82          868 : HostMem HostMem::create(void *ptr, u64 size)
      83              : {
      84          868 :     HostMem mem(ptr, size, false);
      85          868 :     return mem;
      86              : }
      87              : 
      88            1 : HostMem &HostMem::operator=(const HostMem &that)
      89              : {
      90            1 :     if (&that != this) {
      91            1 :         ptr_ = that.ptr();
      92            1 :         size_ = that.size_;
      93            1 :         owner_ = false;
      94            1 :         isRtsMem_ = that.isRtsMem_;
      95              :     }
      96              : 
      97            1 :     return *this;
      98              : }
      99              : 
     100         1306 : HostMem HostMem::operator=(HostMem &&that)
     101              : {
     102         1306 :     if (&that != this) {
     103         1306 :         ptr_ = that.ptr_;
     104         1306 :         size_ = that.size_;
     105         1306 :         owner_ = that.owner_;
     106         1306 :         isRtsMem_ = that.isRtsMem_;
     107              :     }
     108              : 
     109         1306 :     that.ptr_ = nullptr;
     110         1306 :     that.size_ = 0;
     111         1306 :     that.owner_ = false;
     112              : 
     113         1306 :     return *this;
     114              : }
     115              : 
     116            5 : HostMem HostMem::range(u64 offset, u64 size) const
     117              : {
     118            5 :     HostMem mem;
     119            5 :     if (ptr_ != nullptr && (offset + size) <= size_) {
     120            3 :         mem = HostMem(static_cast<void *>(static_cast<s8 *>(ptr_) + offset), size, false);
     121              :     } else {
     122            2 :         HCCL_WARNING("HostMem range[%llu] size[%llu Byte] error or ptr null", offset + size, size_);
     123              :     }
     124            5 :     return mem;
     125            0 : }
     126              : }  // namespace hccl
        

Generated by: LCOV version 2.0-1