summaryrefslogtreecommitdiffstats
path: root/base/lazy_instance.h
diff options
context:
space:
mode:
authorjbates@chromium.org <jbates@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-23 17:52:20 +0000
committerjbates@chromium.org <jbates@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-23 17:52:20 +0000
commitcd924d6e5efdf80f66283ac66987f2b339c381cf (patch)
tree89d2ccd8588e12669b93eb98117e8d76a766fbf2 /base/lazy_instance.h
parent2e8bb832c7b3867b3a10b81621830add0dd5bfb5 (diff)
downloadchromium_src-cd924d6e5efdf80f66283ac66987f2b339c381cf.zip
chromium_src-cd924d6e5efdf80f66283ac66987f2b339c381cf.tar.gz
chromium_src-cd924d6e5efdf80f66283ac66987f2b339c381cf.tar.bz2
Add ALIGNAS and ALIGNOF macros to ensure proper alignment of StaticMemorySingletonTraits
BUG=95006 Review URL: https://chromiumcodereview.appspot.com/9186057 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/lazy_instance.h')
-rw-r--r--base/lazy_instance.h19
1 files changed, 10 insertions, 9 deletions
diff --git a/base/lazy_instance.h b/base/lazy_instance.h
index 5ddc002..c428feb 100644
--- a/base/lazy_instance.h
+++ b/base/lazy_instance.h
@@ -42,6 +42,7 @@
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/logging.h"
+#include "base/memory/aligned_memory.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_restrictions.h"
@@ -59,7 +60,7 @@ struct DefaultLazyInstanceTraits {
static const bool kAllowedToAccessOnNonjoinableThread = false;
static Type* New(void* instance) {
- DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) % sizeof(instance), 0u)
+ DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u)
<< ": Bad boy, the buffer passed to placement new is not aligned!\n"
"This may break some stuff like SSE-based optimizations assuming the "
"<Type> objects are word aligned.";
@@ -155,7 +156,8 @@ class LazyInstance {
if (!(value & kLazyInstanceCreatedMask) &&
internal::NeedsLazyInstance(&private_instance_)) {
// Create the instance in the space provided by |private_buf_|.
- value = reinterpret_cast<subtle::AtomicWord>(Traits::New(private_buf_));
+ value = reinterpret_cast<subtle::AtomicWord>(
+ Traits::New(private_buf_.void_data()));
internal::CompleteLazyInstance(&private_instance_, value, this,
Traits::kRegisterOnExit ? OnExit : NULL);
}
@@ -174,20 +176,19 @@ class LazyInstance {
case 0:
return p == NULL;
case internal::kLazyInstanceStateCreating:
- return static_cast<int8*>(static_cast<void*>(p)) == private_buf_;
+ return static_cast<void*>(p) == private_buf_.void_data();
default:
return p == instance();
}
}
// Effectively private: member data is only public to allow the linker to
- // statically initialize it. DO NOT USE FROM OUTSIDE THIS CLASS.
+ // statically initialize it and to maintain a POD class. DO NOT USE FROM
+ // OUTSIDE THIS CLASS.
- // Note this must use AtomicWord, not Atomic32, to ensure correct alignment
- // of |private_buf_| on 64 bit architectures. (This member must be first to
- // allow the syntax used in LAZY_INSTANCE_INITIALIZER to work correctly.)
subtle::AtomicWord private_instance_;
- int8 private_buf_[sizeof(Type)]; // Preallocated space for the Type instance.
+ // Preallocated space for the Type instance.
+ base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_;
private:
Type* instance() {
@@ -201,7 +202,7 @@ class LazyInstance {
LazyInstance<Type, Traits>* me =
reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
Traits::Delete(me->instance());
- subtle::Release_Store(&me->private_instance_, 0);
+ subtle::NoBarrier_Store(&me->private_instance_, 0);
}
};