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

Unified Diff: base/memory/singleton_unittest.cc

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: win compile Created 8 years, 11 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
« base/memory/singleton.h ('K') | « base/memory/singleton.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/singleton_unittest.cc
diff --git a/base/memory/singleton_unittest.cc b/base/memory/singleton_unittest.cc
index 068148ff76b75e5e25f9f02ff1ddc802e93591d8..59527774440da4ec944a7f2f311c65c6ba5a1999 100644
--- a/base/memory/singleton_unittest.cc
+++ b/base/memory/singleton_unittest.cc
@@ -110,6 +110,26 @@ struct CallbackSingletonWithStaticTrait::Trait
}
};
+template <class Type>
+class AlignedTestSingleton {
+ public:
+ static AlignedTestSingleton* GetInstance() {
+ return Singleton<AlignedTestSingleton,
+ StaticMemorySingletonTraits<AlignedTestSingleton> >::get();
+ }
+
+ Type type_;
+};
+
+class ALIGNED(32) Aligned32Type {
+ public:
+ Aligned32Type() {
+ memset(data_, 32, sizeof(data_));
+ }
+ private:
+ int32 data_[32 / 4];
+};
+
void SingletonNoLeak(CallbackFunc CallOnQuit) {
CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit;
@@ -250,3 +270,21 @@ TEST_F(SingletonTest, Basic) {
// The leaky singleton shouldn't leak since SingletonLeak has not been called.
VerifiesCallbacksNotCalled();
}
+
+TEST_F(SingletonTest, Alignment) {
+ AlignedTestSingleton<Aligned32Type>* singleton1_align32 =
+ AlignedTestSingleton<Aligned32Type>::GetInstance();
+ AlignedTestSingleton<int>* singleton2_align4 =
+ AlignedTestSingleton<int>::GetInstance();
+ AlignedTestSingleton<Aligned32Type>* singleton3_align32 =
+ AlignedTestSingleton<Aligned32Type>::GetInstance();
+ intptr_t singleton1_addr =
+ reinterpret_cast<intptr_t>(&singleton1_align32->type_);
+ intptr_t singleton2_addr =
+ reinterpret_cast<intptr_t>(&singleton2_align4->type_);
+ intptr_t singleton3_addr =
+ reinterpret_cast<intptr_t>(&singleton3_align32->type_);
+ EXPECT_EQ(intptr_t(0), singleton1_addr & 31);
+ EXPECT_EQ(intptr_t(0), singleton2_addr & 3);
+ EXPECT_EQ(intptr_t(0), singleton3_addr & 31);
Jeffrey Yasskin (google) 2012/02/15 22:27:19 Isn't a better test to check that singleton1_addr
jbates 2012/02/15 22:51:33 Yes! In fact, I had a nightmare about how broken t
+}
« base/memory/singleton.h ('K') | « base/memory/singleton.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698