LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/resource/mem - mem_device.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.5 % 71 60
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 11 11

            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_device.h"
      12              : #include "adapter_rts.h"
      13              : 
      14              : namespace hccl {
      15         4651 : DeviceMem::DeviceMem(void *ptr, u64 size, bool owner) : ptr_(ptr), size_(size), owner_(owner)
      16              : {
      17         4651 : }
      18              : 
      19         8060 : DeviceMem::DeviceMem(const DeviceMem &that) : ptr_(that.ptr()), size_(that.size_), owner_(false)
      20              : {
      21         8059 : }
      22              : 
      23          357 : DeviceMem::DeviceMem(DeviceMem &&that) noexcept : ptr_(that.ptr()), size_(that.size_), owner_(that.owner_)
      24              : {
      25          357 :     that.ptr_ = nullptr;
      26          357 :     that.size_ = 0;
      27          357 :     that.owner_ = false;
      28          357 : }
      29              : 
      30        68001 : DeviceMem::~DeviceMem()
      31              : {
      32        68001 :     if (owner_ && ptr_) {
      33         3250 :         HCCL_DEBUG("ptr_[%p], size_[%llu]", ptr_, size_);
      34              : 
      35         3255 :         HcclResult ret = hrtFree(ptr_);
      36         3255 :         if (ret != HCCL_SUCCESS) {
      37            0 :             HCCL_WARNING("hrt_free error, ret[%d]", ret);
      38              :         }
      39              :     }
      40        68006 : }
      41              : 
      42          414 : DeviceMem DeviceMem::alloc(u64 size, bool level2Address)
      43              : {
      44              :     HcclResult ret;
      45          414 :     void *ptr = nullptr;
      46          414 :     ret = hrtMalloc(&ptr, size, level2Address);
      47          414 :     if (ret != HCCL_SUCCESS) {
      48            0 :         HCCL_ERROR("[DeviceMem][Alloc]rt_malloc error, ret[%d], size[%llu Byte]", ret, size);
      49              :     }
      50              : 
      51          414 :     if (ptr == nullptr) {
      52            0 :         HCCL_WARNING("DeviceMem alloc ptr null");
      53              :     }
      54              : 
      55          414 :     DeviceMem mem(ptr, size, true);
      56          414 :     return mem;
      57              : }
      58              : 
      59         3076 : HcclResult DeviceMem::alloc(DeviceMem &mem, u64 size, bool level2Address)
      60              : {
      61         3076 :     void *ptr = nullptr;
      62         3076 :     HcclResult ret = hrtMalloc(&ptr, size, level2Address);
      63         3077 :     if (ret != HCCL_SUCCESS || ptr == nullptr) {
      64            0 :         HCCL_ERROR("[DeviceMem][Alloc]rt_malloc error, ptr is nullptr, ret[%d], size[%llu Byte]", ret, size);
      65            0 :         return ret;
      66              :     }
      67         3077 :     mem = DeviceMem(ptr, size, true);
      68         3077 :     return ret;
      69              : }
      70              : 
      71        10346 : void DeviceMem::free()
      72              : {
      73        10346 :     if (ptr_) {
      74          319 :         HCCL_DEBUG("free ptr_[%p], size_[%llu Byte]", ptr_, size_);
      75          319 :         if (owner_) {
      76          100 :             HcclResult ret = hrtFree(ptr_);
      77          100 :             if (ret != HCCL_SUCCESS) {
      78            0 :                 HCCL_WARNING("hrt_free error, ret[%d]", ret);
      79              :             }
      80              :         }
      81          319 :         ptr_ = nullptr;
      82              :     }
      83        10346 : }
      84              : 
      85          915 : DeviceMem DeviceMem::create(void *ptr, u64 size)
      86              : {
      87          915 :     DeviceMem mem(ptr, size, false);
      88          915 :     return mem;
      89              : }
      90              : 
      91         1726 : DeviceMem &DeviceMem::operator=(const DeviceMem &that)
      92              : {
      93         1726 :     if (&that != this) {
      94         1726 :         ptr_ = that.ptr();
      95         1724 :         size_ = that.size_;
      96         1724 :         owner_ = false;
      97              :     }
      98              : 
      99         1724 :     return *this;
     100              : }
     101              : 
     102         6656 : DeviceMem DeviceMem::operator=(DeviceMem &&that)
     103              : {
     104         6656 :     if (&that != this) {
     105         6656 :         ptr_ = that.ptr_;
     106         6656 :         size_ = that.size_;
     107         6656 :         owner_ = that.owner_;
     108              :     }
     109              : 
     110         6656 :     that.ptr_ = nullptr;
     111         6656 :     that.size_ = 0;
     112         6656 :     that.owner_ = false;
     113              : 
     114         6656 :     return *this;
     115              : }
     116              : 
     117          174 : DeviceMem DeviceMem::range(u64 offset, u64 size) const
     118              : {
     119          174 :     DeviceMem mem;
     120          174 :     if (ptr_ == nullptr){
     121            0 :         HCCL_ERROR("DeviceMem ptr is null");
     122            0 :         return mem;
     123              :     }
     124          174 :     if ((offset + size) > size_){
     125            0 :         HCCL_ERROR("DeviceMem request range[%llu] is out of size_[%llu]", offset+size, size_);
     126            0 :         return mem;
     127              :     }
     128          174 :     mem = DeviceMem(static_cast<void *>(static_cast<s8 *>(ptr_) + offset), size, false);
     129          174 :     return mem;
     130            0 : }
     131              : }  // namespace hccl
        

Generated by: LCOV version 2.0-1