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

Generated by: LCOV version 2.0-1