LCOV - code coverage report
Current view: top level - legacy/ascend910/pub_inc - mem_host_pub.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 4 4
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 4 4

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

Generated by: LCOV version 2.0-1