Commit 113eee0c authored by joth@chromium.org's avatar joth@chromium.org

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
parent 3d2fdb0f
...@@ -49,6 +49,7 @@ namespace base { ...@@ -49,6 +49,7 @@ namespace base {
template <typename Type> template <typename Type>
struct DefaultLazyInstanceTraits { struct DefaultLazyInstanceTraits {
static const bool kRegisterOnExit = true;
static const bool kAllowedToAccessOnNonjoinableThread = false; static const bool kAllowedToAccessOnNonjoinableThread = false;
static Type* New(void* instance) { static Type* New(void* instance) {
...@@ -60,30 +61,24 @@ struct DefaultLazyInstanceTraits { ...@@ -60,30 +61,24 @@ struct DefaultLazyInstanceTraits {
// The parenthesis is very important here to force POD type initialization. // The parenthesis is very important here to force POD type initialization.
return new (instance) Type(); return new (instance) Type();
} }
static void Delete(void* instance) { static void Delete(Type* instance) {
// Explicitly call the destructor. // Explicitly call the destructor.
reinterpret_cast<Type*>(instance)->~Type(); instance->~Type();
} }
}; };
template <typename Type> template <typename Type>
struct LeakyLazyInstanceTraits { struct LeakyLazyInstanceTraits {
static const bool kRegisterOnExit = false;
static const bool kAllowedToAccessOnNonjoinableThread = true; static const bool kAllowedToAccessOnNonjoinableThread = true;
static Type* New(void* instance) { static Type* New(void* instance) {
return DefaultLazyInstanceTraits<Type>::New(instance); return DefaultLazyInstanceTraits<Type>::New(instance);
} }
// Rather than define an empty Delete function, we make Delete itself static void Delete(Type* instance) {
// 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);
}; };
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 // 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. // can implement the more complicated pieces out of line in the .cc file.
class BASE_EXPORT LazyInstanceHelper { class BASE_EXPORT LazyInstanceHelper {
...@@ -152,9 +147,7 @@ class LazyInstance : public LazyInstanceHelper { ...@@ -152,9 +147,7 @@ class LazyInstance : public LazyInstanceHelper {
NeedsInstance()) { NeedsInstance()) {
// Create the instance in the space provided by |buf_|. // Create the instance in the space provided by |buf_|.
instance_ = Traits::New(buf_); instance_ = Traits::New(buf_);
// Traits::Delete will be null for LeakyLazyInstanceTraits CompleteInstance(this, Traits::kRegisterOnExit ? OnExit : NULL);
void (*dtor)(void*) = Traits::Delete;
CompleteInstance(this, (dtor == NULL) ? NULL : OnExit);
} }
// This annotation helps race detectors recognize correct lock-less // This annotation helps race detectors recognize correct lock-less
...@@ -181,7 +174,7 @@ class LazyInstance : public LazyInstanceHelper { ...@@ -181,7 +174,7 @@ class LazyInstance : public LazyInstanceHelper {
private: private:
// Adapter function for use with AtExit. This should be called single // 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. // Calling OnExit while the instance is in use by other threads is a mistake.
static void OnExit(void* lazy_instance) { static void OnExit(void* lazy_instance) {
LazyInstance<Type, Traits>* me = LazyInstance<Type, Traits>* me =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment