Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2045)

Unified Diff: base/lazy_instance.h

Issue 2932053002: Use C++11 alignment primitives (Closed)
Patch Set: Fix merge Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/containers/stack_container_unittest.cc ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/lazy_instance.h
diff --git a/base/lazy_instance.h b/base/lazy_instance.h
index aaad3f7b2f2941ba64ec1aa617eeec201c86333f..99a000ed7d8eb135ae074ad1d0758e3f58101c54 100644
--- a/base/lazy_instance.h
+++ b/base/lazy_instance.h
@@ -41,7 +41,6 @@
#include "base/base_export.h"
#include "base/debug/leak_annotations.h"
#include "base/logging.h"
-#include "base/memory/aligned_memory.h"
#include "base/threading/thread_restrictions.h"
// LazyInstance uses its own struct initializer-list style static
@@ -55,7 +54,7 @@ namespace base {
template <typename Type>
struct LazyInstanceTraitsBase {
static Type* New(void* instance) {
- DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u);
+ DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (alignof(Type) - 1), 0u);
// 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();
@@ -195,7 +194,7 @@ class LazyInstance {
#endif
return static_cast<Type*>(internal::GetOrCreateLazyPointer(
&private_instance_,
- [this]() { return Traits::New(private_buf_.void_data()); },
+ [this]() { return Traits::New(private_buf_); },
Traits::kRegisterOnExit ? OnExit : nullptr, this));
}
@@ -204,19 +203,31 @@ class LazyInstance {
case 0:
return p == NULL;
case internal::kLazyInstanceStateCreating:
- return static_cast<void*>(p) == private_buf_.void_data();
+ return static_cast<void*>(p) == private_buf_;
default:
return p == instance();
}
}
+ // MSVC gives a warning that the alignment expands the size of the
+ // LazyInstance struct to make the size a multiple of the alignment. This
+ // is expected in this case.
+#if defined(OS_WIN)
+#pragma warning(push)
+#pragma warning(disable: 4324)
+#endif
+
// Effectively private: member data is only public to allow the linker to
// statically initialize it and to maintain a POD class. DO NOT USE FROM
// OUTSIDE THIS CLASS.
-
subtle::AtomicWord private_instance_;
+
// Preallocated space for the Type instance.
- base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_;
+ alignas(Type) char private_buf_[sizeof(Type)];
+
+#if defined(OS_WIN)
+#pragma warning(pop)
+#endif
private:
Type* instance() {
« no previous file with comments | « base/containers/stack_container_unittest.cc ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698