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