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 HCCL_REFERENCED_H
12 : #define HCCL_REFERENCED_H
13 :
14 : namespace Hccl {
15 :
16 : class Referenced {
17 : public:
18 : // 初始化这个类,引用计数设为1,并且将p指向传入的地址
19 14 : Referenced(): refCount(0) {}
20 :
21 : // 引用计数加1
22 20 : int Ref()
23 : {
24 20 : return ++refCount;
25 : }
26 :
27 : // 引用计数减1
28 5 : int Unref()
29 : {
30 5 : return --refCount;
31 : }
32 :
33 : // 返回引用计数
34 26 : int Count() const
35 : {
36 26 : return refCount;
37 : }
38 :
39 : int Clear()
40 : {
41 : refCount = 0;
42 : return refCount;
43 : }
44 : bool IsZero() const
45 : {
46 : return refCount == 0;
47 : }
48 41 : ~Referenced() {}
49 : private:
50 : int refCount; // 引用计数,表示有多少个变量引用这块内存
51 : };
52 :
53 : } // namespace Hccl
54 :
55 : #endif // HCCL_REFERENCED_H
|