From 297d0e5e3659a6d4a80906e4fc8e757ababae82d Mon Sep 17 00:00:00 2001 From: "siggi@chromium.org" Date: Fri, 7 May 2010 15:24:49 +0000 Subject: Add a StaticMemorySingletonTraits class to allow constructing singletons in data segment. Change logging_win to use same. BUG=none TEST=Unittests in this change. Review URL: http://codereview.chromium.org/2023003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46686 0039d316-1c4b-4281-b951-d872f2087c98 --- base/singleton_unittest.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'base/singleton_unittest.cc') diff --git a/base/singleton_unittest.cc b/base/singleton_unittest.cc index bb46bee..acb1247 100644 --- a/base/singleton_unittest.cc +++ b/base/singleton_unittest.cc @@ -32,6 +32,15 @@ struct CallbackTrait : public DefaultSingletonTraits { } }; +struct StaticCallbackTrait : public StaticMemorySingletonTraits { + static void Delete(CallbackFunc* p) { + if (*p) + (*p)(); + StaticMemorySingletonTraits::Delete(p); + } +}; + + struct NoLeakTrait : public CallbackTrait { }; @@ -78,6 +87,14 @@ CallbackFunc* GetLeakySingleton() { return Singleton::get(); } +void SingletonStatic(CallbackFunc CallOnQuit) { + *Singleton::get() = CallOnQuit; +} + +CallbackFunc* GetStaticSingleton() { + return Singleton::get(); +} + } // namespace class SingletonTest : public testing::Test { @@ -87,21 +104,26 @@ class SingletonTest : public testing::Test { virtual void SetUp() { non_leak_called_ = false; leaky_called_ = false; + static_called_ = false; } protected: void VerifiesCallbacks() { EXPECT_TRUE(non_leak_called_); EXPECT_FALSE(leaky_called_); + EXPECT_TRUE(static_called_); non_leak_called_ = false; leaky_called_ = false; + static_called_ = false; } void VerifiesCallbacksNotCalled() { EXPECT_FALSE(non_leak_called_); EXPECT_FALSE(leaky_called_); + EXPECT_FALSE(static_called_); non_leak_called_ = false; leaky_called_ = false; + static_called_ = false; } static void CallbackNoLeak() { @@ -112,13 +134,19 @@ class SingletonTest : public testing::Test { leaky_called_ = true; } + static void CallbackStatic() { + static_called_ = true; + } + private: static bool non_leak_called_; static bool leaky_called_; + static bool static_called_; }; bool SingletonTest::non_leak_called_ = false; bool SingletonTest::leaky_called_ = false; +bool SingletonTest::static_called_ = false; TEST_F(SingletonTest, Basic) { int* singleton_int_1; @@ -127,6 +155,7 @@ TEST_F(SingletonTest, Basic) { int* singleton_int_4; int* singleton_int_5; CallbackFunc* leaky_singleton; + CallbackFunc* static_singleton; { base::ShadowingAtExitManager sem; @@ -177,6 +206,8 @@ TEST_F(SingletonTest, Basic) { SingletonNoLeak(&CallbackNoLeak); SingletonLeak(&CallbackLeak); + SingletonStatic(&CallbackStatic); + static_singleton = GetStaticSingleton(); leaky_singleton = GetLeakySingleton(); EXPECT_TRUE(leaky_singleton); } @@ -187,6 +218,9 @@ TEST_F(SingletonTest, Basic) { // *not* detect the leak when this call is commented out. :( DefaultSingletonTraits::Delete(leaky_singleton); + // The static singleton can't be acquired post-atexit. + EXPECT_EQ(NULL, GetStaticSingleton()); + { base::ShadowingAtExitManager sem; // Verifiy that the variables were reset. @@ -198,6 +232,12 @@ TEST_F(SingletonTest, Basic) { singleton_int_5 = SingletonInt5(); EXPECT_EQ(*singleton_int_5, 5); } + { + // Resurrect the static singleton, and assert that it + // still points to the same (static) memory. + StaticMemorySingletonTraits::Resurrect(); + EXPECT_EQ(GetStaticSingleton(), static_singleton); + } } // The leaky singleton shouldn't leak since SingletonLeak has not been called. VerifiesCallbacksNotCalled(); -- cgit v1.1