diff options
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/shared_copy_on_write.h | 32 | ||||
-rw-r--r-- | core/fxcrt/shared_copy_on_write_unittest.cpp | 5 |
2 files changed, 6 insertions, 31 deletions
diff --git a/core/fxcrt/shared_copy_on_write.h b/core/fxcrt/shared_copy_on_write.h index c04730d5e0..f7d7a2afdb 100644 --- a/core/fxcrt/shared_copy_on_write.h +++ b/core/fxcrt/shared_copy_on_write.h @@ -24,7 +24,7 @@ class SharedCopyOnWrite { template <typename... Args> ObjClass* Emplace(Args... params) { - m_pObject.Reset(new CountedObj(params...)); + m_pObject.Reset(new ObjClass(params...)); return m_pObject.Get(); } @@ -42,7 +42,7 @@ class SharedCopyOnWrite { if (!m_pObject) return Emplace(params...); if (!m_pObject->HasOneRef()) - m_pObject.Reset(new CountedObj(*m_pObject)); + m_pObject.Reset(new ObjClass(*m_pObject)); return m_pObject.Get(); } @@ -55,33 +55,7 @@ class SharedCopyOnWrite { explicit operator bool() const { return !!m_pObject; } private: - class CountedObj : public ObjClass { - public: - template <typename... Args> - // NOLINTNEXTLINE(runtime/explicit) - CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} - - CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} - ~CountedObj() { m_RefCount = 0; } - - bool HasOneRef() const { return m_RefCount == 1; } - void Retain() { m_RefCount++; } - void Release() { - ASSERT(m_RefCount); - if (--m_RefCount == 0) - delete this; - } - - private: - // To ensure ref counts do not overflow, consider the worst possible case: - // the entire address space contains nothing but pointers to this object. - // Since the count increments with each new pointer, the largest value is - // the number of pointers that can fit into the address space. The size of - // the address space itself is a good upper bound on it. - intptr_t m_RefCount; - }; - - RetainPtr<CountedObj> m_pObject; + RetainPtr<ObjClass> m_pObject; }; } // namespace fxcrt diff --git a/core/fxcrt/shared_copy_on_write_unittest.cpp b/core/fxcrt/shared_copy_on_write_unittest.cpp index 01cc09468c..57e33d1019 100644 --- a/core/fxcrt/shared_copy_on_write_unittest.cpp +++ b/core/fxcrt/shared_copy_on_write_unittest.cpp @@ -7,6 +7,7 @@ #include <map> #include <string> +#include "core/fxcrt/retain_ptr.h" #include "testing/gtest/include/gtest/gtest.h" namespace fxcrt { @@ -28,7 +29,7 @@ class Observer { std::map<std::string, int> destruction_counts_; }; -class Object { +class Object : public Retainable { public: Object(Observer* observer, const std::string& name) : name_(name), observer_(observer) { @@ -37,7 +38,7 @@ class Object { Object(const Object& that) : name_(that.name_), observer_(that.observer_) { observer_->OnConstruct(name_); } - ~Object() { observer_->OnDestruct(name_); } + ~Object() override { observer_->OnDestruct(name_); } private: std::string name_; |