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

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: siggi, jyasskin 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
« base/memory/aligned_memory_unittest.cc ('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..30580483188b66799e5ba3e0350505f41f8fbdf0 100644
--- a/base/memory/singleton_unittest.cc
+++ b/base/memory/singleton_unittest.cc
@@ -110,6 +110,19 @@ struct CallbackSingletonWithStaticTrait::Trait
}
};
+template <class Type>
+class AlignedTestSingleton {
+ public:
+ AlignedTestSingleton() {}
+ ~AlignedTestSingleton() {}
+ static AlignedTestSingleton* GetInstance() {
+ return Singleton<AlignedTestSingleton,
+ StaticMemorySingletonTraits<AlignedTestSingleton> >::get();
+ }
+
+ Type type_;
+};
+
void SingletonNoLeak(CallbackFunc CallOnQuit) {
CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit;
@@ -250,3 +263,27 @@ TEST_F(SingletonTest, Basic) {
// The leaky singleton shouldn't leak since SingletonLeak has not been called.
VerifiesCallbacksNotCalled();
}
+
+#define EXPECT_ALIGNED(ptr, align) \
+ EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
+
+TEST_F(SingletonTest, Alignment) {
+ using base::RawAlignedMemory;
+
+ // Create some static singletons with increasing sizes and alignment
+ // requirements. By ordering this way, the linker will need to do some work to
+ // ensure proper alignment of the static data.
+ AlignedTestSingleton<int32>* align4 =
+ AlignedTestSingleton<int32>::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<32, 32> >* align32 =
+ AlignedTestSingleton<RawAlignedMemory<32, 32> >::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<128, 128> >* align128 =
+ AlignedTestSingleton<RawAlignedMemory<128, 128> >::GetInstance();
+ AlignedTestSingleton<RawAlignedMemory<4096, 4096> >* align4096 =
+ AlignedTestSingleton<RawAlignedMemory<4096, 4096> >::GetInstance();
+
+ EXPECT_ALIGNED(align4, 4);
willchan no longer on Chromium 2012/02/23 01:18:41 Again it's reversed here.
jbates 2012/02/23 02:23:53 The macro is defined above to call EXPECT_EQ prope
+ EXPECT_ALIGNED(align32, 32);
+ EXPECT_ALIGNED(align128, 128);
+ EXPECT_ALIGNED(align4096, 4096);
+}
« base/memory/aligned_memory_unittest.cc ('K') | « base/memory/singleton.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698