mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00
Make WeakPtr slightly cheaper to allocate and use
This changes Flag to not inherit from a virtual interface. Also fixing iwyu and build dependencies. Bug: none Change-Id: Iba6e095ec771d8975a32059041185270d32e51be Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/348940 Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/main@{#42162}
This commit is contained in:
parent
56b1799389
commit
af3dfd8c35
3 changed files with 19 additions and 18 deletions
|
@ -788,6 +788,8 @@ rtc_library("weak_ptr") {
|
||||||
"weak_ptr.h",
|
"weak_ptr.h",
|
||||||
]
|
]
|
||||||
deps = [
|
deps = [
|
||||||
|
":checks",
|
||||||
|
":macromagic",
|
||||||
":refcount",
|
":refcount",
|
||||||
"../api:scoped_refptr",
|
"../api:scoped_refptr",
|
||||||
"../api:sequence_checker",
|
"../api:sequence_checker",
|
||||||
|
|
|
@ -16,25 +16,19 @@
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
WeakReference::Flag::Flag() : is_valid_(true) {}
|
|
||||||
|
|
||||||
void WeakReference::Flag::Invalidate() {
|
void WeakReference::Flag::Invalidate() {
|
||||||
RTC_DCHECK(checker_.IsCurrent())
|
RTC_DCHECK_RUN_ON(&checker_);
|
||||||
<< "WeakPtrs must be invalidated on the same sequence.";
|
|
||||||
is_valid_ = false;
|
is_valid_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WeakReference::Flag::IsValid() const {
|
bool WeakReference::Flag::IsValid() const {
|
||||||
RTC_DCHECK(checker_.IsCurrent())
|
RTC_DCHECK_RUN_ON(&checker_);
|
||||||
<< "WeakPtrs must be checked on the same sequence.";
|
|
||||||
return is_valid_;
|
return is_valid_;
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference::Flag::~Flag() {}
|
|
||||||
|
|
||||||
WeakReference::WeakReference() {}
|
WeakReference::WeakReference() {}
|
||||||
|
|
||||||
WeakReference::WeakReference(const Flag* flag) : flag_(flag) {}
|
WeakReference::WeakReference(const RefCountedFlag* flag) : flag_(flag) {}
|
||||||
|
|
||||||
WeakReference::~WeakReference() {}
|
WeakReference::~WeakReference() {}
|
||||||
|
|
||||||
|
@ -55,7 +49,7 @@ WeakReferenceOwner::~WeakReferenceOwner() {
|
||||||
WeakReference WeakReferenceOwner::GetRef() const {
|
WeakReference WeakReferenceOwner::GetRef() const {
|
||||||
// If we hold the last reference to the Flag then create a new one.
|
// If we hold the last reference to the Flag then create a new one.
|
||||||
if (!HasRefs())
|
if (!HasRefs())
|
||||||
flag_ = new RefCountedObject<WeakReference::Flag>();
|
flag_ = new WeakReference::RefCountedFlag();
|
||||||
|
|
||||||
return WeakReference(flag_.get());
|
return WeakReference(flag_.get());
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,11 @@
|
||||||
|
|
||||||
#include "api/scoped_refptr.h"
|
#include "api/scoped_refptr.h"
|
||||||
#include "api/sequence_checker.h"
|
#include "api/sequence_checker.h"
|
||||||
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/ref_count.h"
|
#include "rtc_base/ref_count.h"
|
||||||
#include "rtc_base/ref_counted_object.h"
|
#include "rtc_base/ref_counted_object.h"
|
||||||
#include "rtc_base/system/no_unique_address.h"
|
#include "rtc_base/system/no_unique_address.h"
|
||||||
|
#include "rtc_base/thread_annotations.h"
|
||||||
|
|
||||||
// The implementation is borrowed from chromium except that it does not
|
// The implementation is borrowed from chromium except that it does not
|
||||||
// implement SupportsWeakPtr.
|
// implement SupportsWeakPtr.
|
||||||
|
@ -92,25 +94,28 @@ class WeakReference {
|
||||||
public:
|
public:
|
||||||
// Although Flag is bound to a specific sequence, it may be
|
// Although Flag is bound to a specific sequence, it may be
|
||||||
// deleted from another via base::WeakPtr::~WeakPtr().
|
// deleted from another via base::WeakPtr::~WeakPtr().
|
||||||
class Flag : public RefCountInterface {
|
class Flag {
|
||||||
public:
|
public:
|
||||||
Flag();
|
Flag() = default;
|
||||||
|
|
||||||
void Invalidate();
|
void Invalidate();
|
||||||
bool IsValid() const;
|
bool IsValid() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class RefCountedObject<Flag>;
|
friend class FinalRefCountedObject<Flag>;
|
||||||
|
|
||||||
~Flag() override;
|
~Flag() = default;
|
||||||
|
|
||||||
RTC_NO_UNIQUE_ADDRESS ::webrtc::SequenceChecker checker_{
|
RTC_NO_UNIQUE_ADDRESS ::webrtc::SequenceChecker checker_{
|
||||||
webrtc::SequenceChecker::kDetached};
|
webrtc::SequenceChecker::kDetached};
|
||||||
bool is_valid_;
|
bool is_valid_ RTC_GUARDED_BY(checker_) = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// `RefCountedFlag` is the reference counted (shared), non-virtual, flag type.
|
||||||
|
using RefCountedFlag = FinalRefCountedObject<Flag>;
|
||||||
|
|
||||||
WeakReference();
|
WeakReference();
|
||||||
explicit WeakReference(const Flag* flag);
|
explicit WeakReference(const RefCountedFlag* flag);
|
||||||
~WeakReference();
|
~WeakReference();
|
||||||
|
|
||||||
WeakReference(WeakReference&& other);
|
WeakReference(WeakReference&& other);
|
||||||
|
@ -121,7 +126,7 @@ class WeakReference {
|
||||||
bool is_valid() const;
|
bool is_valid() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
scoped_refptr<const Flag> flag_;
|
scoped_refptr<const RefCountedFlag> flag_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WeakReferenceOwner {
|
class WeakReferenceOwner {
|
||||||
|
@ -136,7 +141,7 @@ class WeakReferenceOwner {
|
||||||
void Invalidate();
|
void Invalidate();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable scoped_refptr<RefCountedObject<WeakReference::Flag>> flag_;
|
mutable scoped_refptr<WeakReference::RefCountedFlag> flag_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This class simplifies the implementation of WeakPtr's type conversion
|
// This class simplifies the implementation of WeakPtr's type conversion
|
||||||
|
|
Loading…
Reference in a new issue