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

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: abandoned new/delete support 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
« 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..8619d8828e7049b3c024554d15d7f7a93c83498e 100644
--- a/base/memory/singleton_unittest.cc
+++ b/base/memory/singleton_unittest.cc
@@ -110,6 +110,17 @@ struct CallbackSingletonWithStaticTrait::Trait
}
};
+template <class Type>
+class AlignedTestSingleton {
+ public:
+ static AlignedTestSingleton* GetInstance() {
+ return Singleton<AlignedTestSingleton,
+ StaticMemorySingletonTraits<AlignedTestSingleton> >::get();
+ }
+
+ Type type_;
+};
+
void SingletonNoLeak(CallbackFunc CallOnQuit) {
CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit;
@@ -250,3 +261,36 @@ TEST_F(SingletonTest, Basic) {
// The leaky singleton shouldn't leak since SingletonLeak has not been called.
VerifiesCallbacksNotCalled();
}
+
+TEST_F(SingletonTest, Alignment) {
+ using base::RawAlignedMemory;
+ AlignedTestSingleton<RawAlignedMemory<32, 32> >* singleton1_align32 =
+ AlignedTestSingleton<RawAlignedMemory<32, 32> >::GetInstance();
+ AlignedTestSingleton<int>* singleton2_align4 =
+ AlignedTestSingleton<int>::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<64, 64> >* singleton3_align64 =
+ AlignedTestSingleton<RawAlignedMemory<64, 64> >::GetInstance();
+ AlignedTestSingleton<char>* singleton4_align1 =
+ AlignedTestSingleton<char>::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<128, 128> >* singleton5_align128 =
+ AlignedTestSingleton<RawAlignedMemory<128, 128> >::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<4096, 4096> >* singleton6_align4096 =
+ AlignedTestSingleton<RawAlignedMemory<4096, 4096> >::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_align64->type_);
+ (void)singleton4_align1;
+ intptr_t singleton5_addr =
+ reinterpret_cast<intptr_t>(&singleton5_align128->type_);
+ intptr_t singleton6_addr =
+ reinterpret_cast<intptr_t>(&singleton6_align4096->type_);
+ EXPECT_EQ(intptr_t(0), singleton1_addr & 31);
Sigurður Ásgeirsson 2012/02/22 15:31:32 this test would IMHO be more readable if you'd pul
jbates 2012/02/22 19:37:15 Done.
+ EXPECT_EQ(intptr_t(0), singleton2_addr & 3);
+ EXPECT_EQ(intptr_t(0), singleton3_addr & 63);
+ // singleton4_addr is 1-byte aligned, don't care what the pointer is.
+ EXPECT_EQ(intptr_t(0), singleton5_addr & 127);
+ EXPECT_EQ(intptr_t(0), singleton6_addr & 4095);
+}
« 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