summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-25 19:04:48 +0000
committerjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-25 19:04:48 +0000
commit113eee0c245c31a493cfcc9397c69b9f14a95ec5 (patch)
tree50ca1601d7782ecdee1378c6151cbdd848449894 /base
parent3d2fdb0f19bd403c6e2fec6edd802ed43f2479a5 (diff)
downloadchromium_src-113eee0c245c31a493cfcc9397c69b9f14a95ec5.zip
chromium_src-113eee0c245c31a493cfcc9397c69b9f14a95ec5.tar.gz
chromium_src-113eee0c245c31a493cfcc9397c69b9f14a95ec5.tar.bz2
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
Diffstat (limited to 'base')
-rw-r--r--base/lazy_instance.h23
1 files changed, 8 insertions, 15 deletions
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 <typename Type>
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<Type*>(instance)->~Type();
+ instance->~Type();
}
};
template <typename Type>
struct LeakyLazyInstanceTraits {
+ static const bool kRegisterOnExit = false;
static const bool kAllowedToAccessOnNonjoinableThread = true;
static Type* New(void* instance) {
return DefaultLazyInstanceTraits<Type>::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 <typename Type>
-void (*LeakyLazyInstanceTraits<Type>::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<Type, Traits>* me =