From f92d871f4f319fb727911de5a0ae155557c2c361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Mon, 23 Oct 2017 14:26:10 +0200 Subject: [PATCH] Let ChanneOwner use scoped_refptr and RefCountedBase. Eliminates manual reference counting logic, and one of the few remaining uses of the Atomic32 type. Bug: webrtc:8270 Change-Id: Ibbbf227c710f7e8f68a98bac46d3e24b7cb5ee2f Reviewed-on: https://webrtc-review.googlesource.com/14600 Commit-Queue: Niels Moller Reviewed-by: Fredrik Solenberg Cr-Commit-Position: refs/heads/master@{#20393} --- voice_engine/BUILD.gn | 1 + voice_engine/channel_manager.cc | 25 +------------------------ voice_engine/channel_manager.h | 13 ++++++------- 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/voice_engine/BUILD.gn b/voice_engine/BUILD.gn index 2e18810fc3..f4bbd96b2a 100644 --- a/voice_engine/BUILD.gn +++ b/voice_engine/BUILD.gn @@ -58,6 +58,7 @@ rtc_static_library("voice_engine") { "../api:call_api", "../api:libjingle_peerconnection_api", "../api:optional", + "../api:refcountedbase", "../api:transport_api", "../api/audio_codecs:audio_codecs_api", "../api/audio_codecs:builtin_audio_decoder_factory", diff --git a/voice_engine/channel_manager.cc b/voice_engine/channel_manager.cc index f841a6e200..9a82d2445f 100644 --- a/voice_engine/channel_manager.cc +++ b/voice_engine/channel_manager.cc @@ -19,31 +19,8 @@ namespace voe { ChannelOwner::ChannelOwner(class Channel* channel) : channel_ref_(new ChannelRef(channel)) {} -ChannelOwner::ChannelOwner(const ChannelOwner& channel_owner) - : channel_ref_(channel_owner.channel_ref_) { - ++channel_ref_->ref_count; -} - -ChannelOwner::~ChannelOwner() { - if (--channel_ref_->ref_count == 0) - delete channel_ref_; -} - -ChannelOwner& ChannelOwner::operator=(const ChannelOwner& other) { - if (other.channel_ref_ == channel_ref_) - return *this; - - if (--channel_ref_->ref_count == 0) - delete channel_ref_; - - channel_ref_ = other.channel_ref_; - ++channel_ref_->ref_count; - - return *this; -} - ChannelOwner::ChannelRef::ChannelRef(class Channel* channel) - : channel(channel), ref_count(1) {} + : channel(channel) {} ChannelManager::ChannelManager(uint32_t instance_id) : instance_id_(instance_id), diff --git a/voice_engine/channel_manager.h b/voice_engine/channel_manager.h index 7dfdc32a03..f7cf5f4dac 100644 --- a/voice_engine/channel_manager.h +++ b/voice_engine/channel_manager.h @@ -14,6 +14,7 @@ #include #include +#include "api/refcountedbase.h" #include "rtc_base/constructormagic.h" #include "rtc_base/criticalsection.h" #include "rtc_base/random.h" @@ -49,26 +50,24 @@ class Channel; class ChannelOwner { public: explicit ChannelOwner(Channel* channel); - ChannelOwner(const ChannelOwner& channel_owner); + ChannelOwner(const ChannelOwner& channel_owner) = default; - ~ChannelOwner(); + ~ChannelOwner() = default; - ChannelOwner& operator=(const ChannelOwner& other); + ChannelOwner& operator=(const ChannelOwner& other) = default; Channel* channel() const { return channel_ref_->channel.get(); } bool IsValid() { return channel_ref_->channel.get() != NULL; } - int use_count() const { return channel_ref_->ref_count.Value(); } private: // Shared instance of a Channel. Copying ChannelOwners increase the reference // count and destroying ChannelOwners decrease references. Channels are // deleted when no references to them are held. - struct ChannelRef { + struct ChannelRef : public rtc::RefCountedBase { ChannelRef(Channel* channel); const std::unique_ptr channel; - Atomic32 ref_count; }; - ChannelRef* channel_ref_; + rtc::scoped_refptr channel_ref_; }; class ChannelManager {