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