LCOV - code coverage report
Current view: top level - legacy/ascend910/pub_inc - mem_device_pub.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 12 12
Test Date: 2026-07-28 12:11:00 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              : #ifndef MEM_DEVICE_PUB_H
      12              : #define MEM_DEVICE_PUB_H
      13              : 
      14              : #include "hccl_common.h"
      15              : #include "hccl/base.h"
      16              : 
      17              : namespace hccl {
      18              : class DeviceMem {
      19              : public:
      20              :     /* * 默认构造函数, 只生成无效的DeviceMem对象 */
      21        54944 :     explicit DeviceMem() : ptr_(nullptr), size_(0), owner_(false)
      22              :     {
      23        54944 :     }
      24              :     explicit DeviceMem(void *ptr, u64 size, bool owner = false);
      25              :     /* * 拷贝构造函数, 用于DeviceMem::create的返回
      26              :         新实例对源实例的ptr无所有权, 析构时不释放ptr
      27              :         源实例保留原来对ptr的所有权, 析构时释放ptr */
      28              :     DeviceMem(const DeviceMem &that);
      29              : 
      30              :     /* * 移动构造函数, 用于DeviceMem::alloc的返回
      31              :         新实例对源实例的ptr有所有权, 析构时释放ptr
      32              :         源实例放弃原来对ptr的所有权, 析构时不释放ptr */
      33              :     DeviceMem(DeviceMem &&that) noexcept;
      34              : 
      35              :     ~DeviceMem();
      36              :     void free();
      37              : 
      38              :     /**
      39              :     通过静态成员函数来创建DeviceMem对象,目的如下:
      40              :     1)
      41              :     根据入参实例化,用create
      42              :     临时申请用,用alloc
      43              :     否则先调用底层函数申请memory,再用create会造成下层实现代码上移(rt_malloc)
      44              :     造成代码维护困难
      45              :     2)
      46              :     语义上类似C语言申请内存(malloc)的方式,好理解
      47              :     */
      48              :     static DeviceMem alloc(u64 size, bool level2Address = false);
      49              :     static HcclResult alloc(DeviceMem &mem, u64 size, bool level2Address = false);
      50              :     static DeviceMem create(void *ptr, u64 size);
      51              : 
      52              :     /* * 部分操作符声明or重载, 期望达到类似memory指针操作那样来操作Mem对象 */
      53              :     /* * 重载move-assignment运算符, 用于alloc返回
      54              :         左值对象对右值对象的ptr有所有权, 析构时释放ptr
      55              :         右值对象放弃其原来对ptr的所有权, 析构时不释放ptr */
      56              :     DeviceMem operator=(DeviceMem &&that);
      57              : 
      58              :     /* * 重载copy-assignment运算符, 用于create返回和普通的DeviceMem对象拷贝
      59              :         左值对象对右值对象的ptr无所有权, 析构时释放ptr
      60              :         右值对象保留其原来对ptr的所有权, 析构时不释放ptr */
      61              :     DeviceMem &operator=(const DeviceMem &that);
      62              : 
      63              :     // "bool"运算符(可执行if(object){...}的操作判断该DeviceMem是否为空)
      64          211 :     operator bool() const
      65              :     {
      66          211 :         return ptr_ != nullptr;
      67              :     }
      68              : 
      69              :     // "=="运算符
      70           13 :     bool operator==(const DeviceMem &that) const
      71              :     {
      72           13 :         return (ptr_ == that.ptr()) && (size_ == that.size());
      73              :     }
      74              : 
      75              :     // "!="运算符
      76            3 :     bool operator!=(const DeviceMem &that) const
      77              :     {
      78            3 :         return (ptr_ != that.ptr()) || (size_ != that.size());
      79              :     }
      80              : 
      81              :     // 取地址
      82        36291 :     void *ptr() const
      83              :     {
      84        36261 :         return ptr_;
      85              :     }
      86              : 
      87              :     /* * 内联成员函数 */
      88    362177318 :     u64 size() const
      89              :     {
      90    362177318 :         return size_;
      91              :     }
      92              : 
      93              :     /* * 在当前mem实例中截取一段形成新的Mem实例 */
      94              :     DeviceMem range(u64 offset, u64 size) const;
      95              :     void *ptr_; /* * memory地址 */
      96              : protected:
      97              : private:
      98              :     explicit DeviceMem(u64 size);
      99              : 
     100              :     u64 size_;   /* * memory的size, 单位 : 字节 */
     101              :     bool owner_; /* * 类实例资源owner, 类似std::shared_ptr的做法 */
     102              : };
     103              : }  // namespace hccl
     104              : 
     105              : #endif /* MEM_DEVICE_PUB_H */
        

Generated by: LCOV version 2.0-1