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

Unified Diff: base/memory/singleton.h

Issue 9186057: Add ALIGNAS and ALIGNOF macros to ensure proper alignment of StaticMemorySingletonTraits (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: willchan feedback Created 8 years, 10 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/memory/aligned_memory_unittest.cc ('k') | base/memory/singleton_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/singleton.h
diff --git a/base/memory/singleton.h b/base/memory/singleton.h
index d76070019435323869c9b2d13e6baa2f982e271e..55490124213637636fc1cc5726ae35ecdb301133 100644
--- a/base/memory/singleton.h
+++ b/base/memory/singleton.h
@@ -23,6 +23,7 @@
#include "base/at_exit.h"
#include "base/atomicops.h"
#include "base/base_export.h"
+#include "base/memory/aligned_memory.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_restrictions.h"
@@ -106,18 +107,14 @@ struct StaticMemorySingletonTraits {
// WARNING: User has to deal with get() in the singleton class
// this is traits for returning NULL.
static Type* New() {
+ // Only constructs once and returns pointer; otherwise returns NULL.
if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1))
return NULL;
- Type* ptr = reinterpret_cast<Type*>(buffer_);
- // We are protected by a memory barrier.
- new(ptr) Type();
- return ptr;
+ return new(buffer_.void_data()) Type();
}
static void Delete(Type* p) {
- base::subtle::NoBarrier_Store(&dead_, 1);
- base::subtle::MemoryBarrier();
if (p != NULL)
p->Type::~Type();
}
@@ -131,16 +128,13 @@ struct StaticMemorySingletonTraits {
}
private:
- static const size_t kBufferSize = (sizeof(Type) +
- sizeof(intptr_t) - 1) / sizeof(intptr_t);
- static intptr_t buffer_[kBufferSize];
-
+ static base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_;
// Signal the object was already deleted, so it is not revived.
static base::subtle::Atomic32 dead_;
};
-template <typename Type> intptr_t
- StaticMemorySingletonTraits<Type>::buffer_[kBufferSize];
+template <typename Type> base::AlignedMemory<sizeof(Type), ALIGNOF(Type)>
+ StaticMemorySingletonTraits<Type>::buffer_;
template <typename Type> base::subtle::Atomic32
StaticMemorySingletonTraits<Type>::dead_ = 0;
« no previous file with comments | « base/memory/aligned_memory_unittest.cc ('k') | base/memory/singleton_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698