[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
avi | 9beac25 | 2015-12-24 08:44:47 | [diff] [blame^] | 5 | #include <stdint.h> |
| 6 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 7 | #include "base/at_exit.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/singleton.h" |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 11 | namespace base { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 12 | namespace { |
| 13 | |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 14 | static_assert(DefaultSingletonTraits<int>::kRegisterAtExit == true, |
| 15 | "object must be deleted on process exit"); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 16 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 17 | typedef void (*CallbackFunc)(); |
| 18 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 19 | class IntSingleton { |
| 20 | public: |
| 21 | static IntSingleton* GetInstance() { |
| 22 | return Singleton<IntSingleton>::get(); |
| 23 | } |
| 24 | |
| 25 | int value_; |
| 26 | }; |
| 27 | |
| 28 | class Init5Singleton { |
| 29 | public: |
| 30 | struct Trait; |
| 31 | |
| 32 | static Init5Singleton* GetInstance() { |
| 33 | return Singleton<Init5Singleton, Trait>::get(); |
| 34 | } |
| 35 | |
| 36 | int value_; |
| 37 | }; |
| 38 | |
| 39 | struct Init5Singleton::Trait : public DefaultSingletonTraits<Init5Singleton> { |
| 40 | static Init5Singleton* New() { |
| 41 | Init5Singleton* instance = new Init5Singleton(); |
| 42 | instance->value_ = 5; |
| 43 | return instance; |
[email protected] | 5820d2f0 | 2010-12-11 10:23:37 | [diff] [blame] | 44 | } |
| 45 | }; |
[email protected] | 49ab556 | 2010-12-11 09:13:54 | [diff] [blame] | 46 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 47 | int* SingletonInt() { |
| 48 | return &IntSingleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int* SingletonInt5() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 52 | return &Init5Singleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 55 | template <typename Type> |
| 56 | struct CallbackTrait : public DefaultSingletonTraits<Type> { |
| 57 | static void Delete(Type* instance) { |
| 58 | if (instance->callback_) |
| 59 | (instance->callback_)(); |
| 60 | DefaultSingletonTraits<Type>::Delete(instance); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class CallbackSingleton { |
| 65 | public: |
| 66 | CallbackSingleton() : callback_(NULL) { } |
| 67 | CallbackFunc callback_; |
| 68 | }; |
| 69 | |
| 70 | class CallbackSingletonWithNoLeakTrait : public CallbackSingleton { |
| 71 | public: |
| 72 | struct Trait : public CallbackTrait<CallbackSingletonWithNoLeakTrait> { }; |
| 73 | |
| 74 | CallbackSingletonWithNoLeakTrait() : CallbackSingleton() { } |
| 75 | |
| 76 | static CallbackSingletonWithNoLeakTrait* GetInstance() { |
| 77 | return Singleton<CallbackSingletonWithNoLeakTrait, Trait>::get(); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | class CallbackSingletonWithLeakTrait : public CallbackSingleton { |
| 82 | public: |
| 83 | struct Trait : public CallbackTrait<CallbackSingletonWithLeakTrait> { |
| 84 | static const bool kRegisterAtExit = false; |
| 85 | }; |
| 86 | |
| 87 | CallbackSingletonWithLeakTrait() : CallbackSingleton() { } |
| 88 | |
| 89 | static CallbackSingletonWithLeakTrait* GetInstance() { |
| 90 | return Singleton<CallbackSingletonWithLeakTrait, Trait>::get(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class CallbackSingletonWithStaticTrait : public CallbackSingleton { |
| 95 | public: |
| 96 | struct Trait; |
| 97 | |
| 98 | CallbackSingletonWithStaticTrait() : CallbackSingleton() { } |
| 99 | |
| 100 | static CallbackSingletonWithStaticTrait* GetInstance() { |
| 101 | return Singleton<CallbackSingletonWithStaticTrait, Trait>::get(); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | struct CallbackSingletonWithStaticTrait::Trait |
| 106 | : public StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait> { |
| 107 | static void Delete(CallbackSingletonWithStaticTrait* instance) { |
| 108 | if (instance->callback_) |
| 109 | (instance->callback_)(); |
| 110 | StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait>::Delete( |
| 111 | instance); |
| 112 | } |
| 113 | }; |
| 114 | |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 115 | template <class Type> |
| 116 | class AlignedTestSingleton { |
| 117 | public: |
| 118 | AlignedTestSingleton() {} |
| 119 | ~AlignedTestSingleton() {} |
| 120 | static AlignedTestSingleton* GetInstance() { |
| 121 | return Singleton<AlignedTestSingleton, |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 122 | StaticMemorySingletonTraits<AlignedTestSingleton>>::get(); |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Type type_; |
| 126 | }; |
| 127 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 128 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 129 | void SingletonNoLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 130 | CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void SingletonLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 134 | CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | CallbackFunc* GetLeakySingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 138 | return &CallbackSingletonWithLeakTrait::GetInstance()->callback_; |
| 139 | } |
| 140 | |
| 141 | void DeleteLeakySingleton() { |
| 142 | DefaultSingletonTraits<CallbackSingletonWithLeakTrait>::Delete( |
| 143 | CallbackSingletonWithLeakTrait::GetInstance()); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 146 | void SingletonStatic(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 147 | CallbackSingletonWithStaticTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | CallbackFunc* GetStaticSingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 151 | return &CallbackSingletonWithStaticTrait::GetInstance()->callback_; |
| 152 | } |
| 153 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | |
| 155 | class SingletonTest : public testing::Test { |
| 156 | public: |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 157 | SingletonTest() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 159 | void SetUp() override { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | non_leak_called_ = false; |
| 161 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 162 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | } |
| 164 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | protected: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | void VerifiesCallbacks() { |
| 167 | EXPECT_TRUE(non_leak_called_); |
| 168 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 169 | EXPECT_TRUE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | non_leak_called_ = false; |
| 171 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 172 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void VerifiesCallbacksNotCalled() { |
| 176 | EXPECT_FALSE(non_leak_called_); |
| 177 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 178 | EXPECT_FALSE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | non_leak_called_ = false; |
| 180 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 181 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 184 | static void CallbackNoLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 185 | non_leak_called_ = true; |
| 186 | } |
| 187 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 188 | static void CallbackLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 189 | leaky_called_ = true; |
| 190 | } |
| 191 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 192 | static void CallbackStatic() { |
| 193 | static_called_ = true; |
| 194 | } |
| 195 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 196 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | static bool non_leak_called_; |
| 198 | static bool leaky_called_; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 199 | static bool static_called_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | bool SingletonTest::non_leak_called_ = false; |
| 203 | bool SingletonTest::leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 204 | bool SingletonTest::static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | TEST_F(SingletonTest, Basic) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 207 | int* singleton_int; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | int* singleton_int_5; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 209 | CallbackFunc* leaky_singleton; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 210 | CallbackFunc* static_singleton; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 212 | { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 213 | ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 214 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 215 | singleton_int = SingletonInt(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | } |
| 217 | // Ensure POD type initialization. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 218 | EXPECT_EQ(*singleton_int, 0); |
| 219 | *singleton_int = 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 221 | EXPECT_EQ(singleton_int, SingletonInt()); |
| 222 | EXPECT_EQ(*singleton_int, 1); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 223 | |
| 224 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 225 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | } |
| 227 | // Is default initialized to 5. |
| 228 | EXPECT_EQ(*singleton_int_5, 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 229 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 230 | SingletonNoLeak(&CallbackNoLeak); |
| 231 | SingletonLeak(&CallbackLeak); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 232 | SingletonStatic(&CallbackStatic); |
| 233 | static_singleton = GetStaticSingleton(); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 234 | leaky_singleton = GetLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | EXPECT_TRUE(leaky_singleton); |
| 236 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 237 | |
| 238 | // Verify that only the expected callback has been called. |
| 239 | VerifiesCallbacks(); |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 240 | // Delete the leaky singleton. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 241 | DeleteLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 243 | // The static singleton can't be acquired post-atexit. |
| 244 | EXPECT_EQ(NULL, GetStaticSingleton()); |
| 245 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 246 | { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 247 | ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | // Verifiy that the variables were reset. |
| 249 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 250 | singleton_int = SingletonInt(); |
| 251 | EXPECT_EQ(*singleton_int, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | } |
| 253 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 254 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | EXPECT_EQ(*singleton_int_5, 5); |
| 256 | } |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 257 | { |
| 258 | // Resurrect the static singleton, and assert that it |
| 259 | // still points to the same (static) memory. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 260 | CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 261 | EXPECT_EQ(GetStaticSingleton(), static_singleton); |
| 262 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 263 | } |
| 264 | // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 265 | VerifiesCallbacksNotCalled(); |
| 266 | } |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 267 | |
| 268 | #define EXPECT_ALIGNED(ptr, align) \ |
| 269 | EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 270 | |
| 271 | TEST_F(SingletonTest, Alignment) { |
| 272 | using base::AlignedMemory; |
| 273 | |
| 274 | // Create some static singletons with increasing sizes and alignment |
| 275 | // requirements. By ordering this way, the linker will need to do some work to |
| 276 | // ensure proper alignment of the static data. |
avi | 9beac25 | 2015-12-24 08:44:47 | [diff] [blame^] | 277 | AlignedTestSingleton<int32_t>* align4 = |
| 278 | AlignedTestSingleton<int32_t>::GetInstance(); |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 279 | AlignedTestSingleton<AlignedMemory<32, 32> >* align32 = |
| 280 | AlignedTestSingleton<AlignedMemory<32, 32> >::GetInstance(); |
| 281 | AlignedTestSingleton<AlignedMemory<128, 128> >* align128 = |
| 282 | AlignedTestSingleton<AlignedMemory<128, 128> >::GetInstance(); |
| 283 | AlignedTestSingleton<AlignedMemory<4096, 4096> >* align4096 = |
| 284 | AlignedTestSingleton<AlignedMemory<4096, 4096> >::GetInstance(); |
| 285 | |
| 286 | EXPECT_ALIGNED(align4, 4); |
| 287 | EXPECT_ALIGNED(align32, 32); |
| 288 | EXPECT_ALIGNED(align128, 128); |
| 289 | EXPECT_ALIGNED(align4096, 4096); |
| 290 | } |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 291 | |
| 292 | } // namespace |
| 293 | } // namespace base |