From e4a638f76ada2aaacd46f0adb2037e28dffc3648 Mon Sep 17 00:00:00 2001 From: "timurrrr@chromium.org" Date: Fri, 21 Oct 2011 19:46:00 +0000 Subject: Make the placement-new buffer in LazyInstance aligned. Before, the LazyInstance::buf_ was 4-byte aligned on x64, which is wrong. WHY?! I thought buf_ is the first member of LazyInstance?! NO! LazyInstance inherits LazyInstanceHelper, sizeof(LIH) = 4. Then, buf_ is given to placement new. As a result, the LazyInstance instances are all 4-byte aligned on x64. This may break some stuff like SSE-based optimizations assuming the instance is 8-bytes aligned (fair assumption). Also, if Type contains a bunch of std::vector/hash_map's, their pointers occupy two half-words and Valgrind doesn't traverse to their data, reporting a false leak. BUG=64930 Review URL: http://codereview.chromium.org/8366041 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106763 0039d316-1c4b-4281-b951-d872f2087c98 --- base/lazy_instance.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'base') diff --git a/base/lazy_instance.h b/base/lazy_instance.h index c6785f3..1471335 100644 --- a/base/lazy_instance.h +++ b/base/lazy_instance.h @@ -41,6 +41,7 @@ #include "base/atomicops.h" #include "base/base_export.h" #include "base/basictypes.h" +#include "base/logging.h" #include "base/third_party/dynamic_annotations/dynamic_annotations.h" #include "base/threading/thread_restrictions.h" @@ -51,6 +52,10 @@ struct DefaultLazyInstanceTraits { static const bool kAllowedToAccessOnNonjoinableThread = false; static Type* New(void* instance) { + DCHECK_EQ(reinterpret_cast(instance) % sizeof(instance), 0u) + << ": Bad boy, the buffer passed to placement new is not aligned!\n" + "This may break some stuff like SSE-based optimizations assuming the " + " objects are word aligned."; // Use placement new to initialize our instance in our preallocated space. // The parenthesis is very important here to force POD type initialization. return new (instance) Type(); @@ -186,8 +191,8 @@ class LazyInstance : public LazyInstanceHelper { base::subtle::Release_Store(&me->state_, STATE_EMPTY); } - int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. Type *instance_; + int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. DISALLOW_COPY_AND_ASSIGN(LazyInstance); }; -- cgit v1.1