From 113eee0c245c31a493cfcc9397c69b9f14a95ec5 Mon Sep 17 00:00:00 2001 From: "joth@chromium.org" Date: Tue, 25 Oct 2011 19:04:48 +0000 Subject: Remove static function pointer This save ~1720B from the size of debug chrome build. Also make the traits Delete method typesafe. BUG=None TEST=lazy_instance unittests Review URL: http://codereview.chromium.org/8393002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107170 0039d316-1c4b-4281-b951-d872f2087c98 --- base/lazy_instance.h | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'base/lazy_instance.h') diff --git a/base/lazy_instance.h b/base/lazy_instance.h index 1471335..496fc1f 100644 --- a/base/lazy_instance.h +++ b/base/lazy_instance.h @@ -49,6 +49,7 @@ namespace base { template struct DefaultLazyInstanceTraits { + static const bool kRegisterOnExit = true; static const bool kAllowedToAccessOnNonjoinableThread = false; static Type* New(void* instance) { @@ -60,30 +61,24 @@ struct DefaultLazyInstanceTraits { // The parenthesis is very important here to force POD type initialization. return new (instance) Type(); } - static void Delete(void* instance) { + static void Delete(Type* instance) { // Explicitly call the destructor. - reinterpret_cast(instance)->~Type(); + instance->~Type(); } }; template struct LeakyLazyInstanceTraits { + static const bool kRegisterOnExit = false; static const bool kAllowedToAccessOnNonjoinableThread = true; static Type* New(void* instance) { return DefaultLazyInstanceTraits::New(instance); } - // Rather than define an empty Delete function, we make Delete itself - // a null pointer. This allows us to completely sidestep registering - // this object with an AtExitManager, which allows you to use - // LeakyLazyInstanceTraits in contexts where you don't have an - // AtExitManager. - static void (*Delete)(void* instance); + static void Delete(Type* instance) { + } }; -template -void (*LeakyLazyInstanceTraits::Delete)(void* instance) = NULL; - // We pull out some of the functionality into a non-templated base, so that we // can implement the more complicated pieces out of line in the .cc file. class BASE_EXPORT LazyInstanceHelper { @@ -152,9 +147,7 @@ class LazyInstance : public LazyInstanceHelper { NeedsInstance()) { // Create the instance in the space provided by |buf_|. instance_ = Traits::New(buf_); - // Traits::Delete will be null for LeakyLazyInstanceTraits - void (*dtor)(void*) = Traits::Delete; - CompleteInstance(this, (dtor == NULL) ? NULL : OnExit); + CompleteInstance(this, Traits::kRegisterOnExit ? OnExit : NULL); } // This annotation helps race detectors recognize correct lock-less @@ -181,7 +174,7 @@ class LazyInstance : public LazyInstanceHelper { private: // Adapter function for use with AtExit. This should be called single - // threaded, so don't use atomic operations. + // threaded, so don't synchronize across threads. // Calling OnExit while the instance is in use by other threads is a mistake. static void OnExit(void* lazy_instance) { LazyInstance* me = -- cgit v1.1