1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MOCK_NATIVE_INCLUDE_REFBASE_H_ 17 #define MOCK_NATIVE_INCLUDE_REFBASE_H_ 18 19 #include <atomic> 20 #include <functional> 21 22 namespace OHOS { 23 #define INITIAL_PRIMARY_VALUE (1 << 28) 24 25 class RefBase; 26 27 class RefCounter { 28 public: 29 using RefPtrCallback = std::function<void()>; 30 friend class RefBase; 31 32 RefCounter(); 33 34 explicit RefCounter(RefCounter *counter); 35 36 RefCounter &operator=(const RefCounter &counter); 37 38 virtual ~RefCounter(); 39 40 void SetCallback(const RefPtrCallback& callback); 41 42 void RemoveCallback(); 43 44 int GetRefCount(); 45 46 void IncRefCount(); 47 48 void DecRefCount(); 49 50 bool IsRefPtrValid(); 51 52 int IncStrongRefCount(const void *objectId); 53 54 int DecStrongRefCount(const void *objectId); 55 56 int GetStrongRefCount(); 57 58 int IncWeakRefCount(const void *objectId); 59 60 int DecWeakRefCount(const void *objectId); 61 62 int GetWeakRefCount(); 63 64 void SetAttemptAcquire(); 65 66 bool IsAttemptAcquireSet(); 67 68 void ClearAttemptAcquire(); 69 70 bool AttemptIncStrongRef(const void *objectId, int &outCount); 71 72 // Only for IPC use. 73 bool AttemptIncStrong(const void *objectId); 74 75 bool IsLifeTimeExtended(); 76 77 void ExtendObjectLifetime(); 78 79 private: 80 std::atomic<int> atomicStrong_; 81 std::atomic<int> atomicWeak_; 82 std::atomic<int> atomicRefCount_; 83 std::atomic<unsigned int> atomicFlags_; 84 std::atomic<int> atomicAttempt_; 85 RefPtrCallback callback_ = nullptr; 86 static constexpr unsigned int FLAG_EXTEND_LIFE_TIME = 0x00000002; 87 #ifdef DEBUG_REFBASE 88 RefTracker* refTracker = nullptr; 89 std::mutex trackerMutex; // To ensure refTracker be thread-safe 90 void GetNewTrace(const void* object); 91 void PrintTracker(); 92 #endif 93 }; 94 95 class WeakRefCounter { 96 public: 97 WeakRefCounter(RefCounter *base, void *cookie); 98 99 virtual ~WeakRefCounter(); 100 101 void *GetRefPtr(); 102 103 void IncWeakRefCount(const void *objectId); 104 105 void DecWeakRefCount(const void *objectId); 106 107 bool AttemptIncStrongRef(const void *objectId); 108 109 private: 110 std::atomic<int> atomicWeak_; 111 RefCounter *refCounter_ = nullptr; 112 void *cookie_ = nullptr; 113 }; 114 115 class RefBase { 116 public: 117 RefBase(); 118 119 RefBase(const RefBase &refbase); 120 121 RefBase &operator=(const RefBase &refbase); 122 123 RefBase(RefBase &&refbase) noexcept; 124 125 RefBase &operator=(RefBase &&refbase) noexcept; 126 127 virtual ~RefBase(); 128 129 virtual void RefPtrCallback(); 130 131 void ExtendObjectLifetime(); 132 133 void IncStrongRef(const void *objectId); 134 135 void DecStrongRef(const void *objectId); 136 137 int GetSptrRefCount(); 138 139 WeakRefCounter *CreateWeakRef(void *cookie); 140 141 void IncWeakRef(const void *objectId); 142 143 void DecWeakRef(const void *objectId); 144 145 int GetWptrRefCount(); 146 147 bool AttemptAcquire(const void *objectId); 148 149 bool AttemptIncStrongRef(const void *objectId); 150 151 // Only for IPC use. 152 bool AttemptIncStrong(const void *objectId); 153 154 bool IsAttemptAcquireSet(); 155 156 bool IsExtendLifeTimeSet(); 157 158 virtual void OnFirstStrongRef(const void *objectId); 159 160 virtual void OnLastStrongRef(const void *objectId); 161 162 virtual void OnLastWeakRef(const void *objectId); 163 164 virtual bool OnAttemptPromoted(const void *objectId); 165 166 private: 167 RefCounter *refs_ = nullptr; 168 }; 169 } // namespace OHOS 170 171 #endif // MOCK_NATIVE_INCLUDE_REFBASE_H_ 172