Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: base/callback_internal.cc

Issue 1129353003: Devirtualize base::BindState to save ~1% of Chrome's binary size (~1MB) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@20150507-SizesExplorations
Patch Set: fix a test Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/callback_internal.h" 5 #include "base/callback_internal.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace base { 9 namespace base {
10 namespace internal { 10 namespace internal {
11 11
12 CallbackBase::CallbackBase(const CallbackBase& c) = default; 12 void BindStateBase::AddRef() {
13 CallbackBase& CallbackBase::operator=(const CallbackBase& c) = default; 13 AtomicRefCountInc(&ref_count_);
14 }
15
16 void BindStateBase::Release() {
17 if (!AtomicRefCountDec(&ref_count_))
18 destructor_(this);
19 }
14 20
15 void CallbackBase::Reset() { 21 void CallbackBase::Reset() {
16 polymorphic_invoke_ = NULL; 22 polymorphic_invoke_ = nullptr;
17 // NULL the bind_state_ last, since it may be holding the last ref to whatever 23 // null the bind_state_ first, since it may be holding the last ref to
18 // object owns us, and we may be deleted after that. 24 // whatever object owns us, and we may be deleted after that.
19 bind_state_ = NULL; 25 if (bind_state_) {
26 BindStateBase* to_release = bind_state_;
27 bind_state_ = nullptr;
28 to_release->Release();
29 }
20 } 30 }
21 31
22 bool CallbackBase::Equals(const CallbackBase& other) const { 32 bool CallbackBase::Equals(const CallbackBase& other) const {
23 return bind_state_.get() == other.bind_state_.get() && 33 return bind_state_ == other.bind_state_ &&
24 polymorphic_invoke_ == other.polymorphic_invoke_; 34 polymorphic_invoke_ == other.polymorphic_invoke_;
25 } 35 }
26 36
37 CallbackBase::CallbackBase()
38 : bind_state_(nullptr), polymorphic_invoke_(nullptr) {
39 }
40
27 CallbackBase::CallbackBase(BindStateBase* bind_state) 41 CallbackBase::CallbackBase(BindStateBase* bind_state)
28 : bind_state_(bind_state), 42 : bind_state_(bind_state) {
29 polymorphic_invoke_(NULL) { 43 bind_state_->AddRef();
30 DCHECK(!bind_state_.get() || bind_state_->HasOneRef()); 44 DCHECK_EQ(1, bind_state_->ref_count_);
45 }
46
47 CallbackBase::CallbackBase(const CallbackBase& c)
48 : bind_state_(c.bind_state_), polymorphic_invoke_(c.polymorphic_invoke_) {
49 if (bind_state_)
50 bind_state_->AddRef();
51 }
52
53 CallbackBase& CallbackBase::operator=(const CallbackBase& c) {
54 // Increment first to handle self-assignment correctly.
55 if (c.bind_state_)
56 c.bind_state_->AddRef();
57 if (bind_state_)
58 bind_state_->Release();
59
60 bind_state_ = c.bind_state_;
61 polymorphic_invoke_ = c.polymorphic_invoke_;
62 return *this;
31 } 63 }
32 64
33 CallbackBase::~CallbackBase() { 65 CallbackBase::~CallbackBase() {
66 if (bind_state_)
67 bind_state_->Release();
34 } 68 }
35 69
36 } // namespace internal 70 } // namespace internal
37 } // namespace base 71 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698