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