Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_NO_DESTRUCTOR_H_ |
| 6 | #define BASE_NO_DESTRUCTOR_H_ |
| 7 | |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 8 | #include <new> |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | // A wrapper that makes it easy to create an object of type T with static |
| 14 | // storage duration that: |
| 15 | // - is only constructed on first access |
| 16 | // - never invokes the destructor |
| 17 | // in order to satisfy the styleguide ban on global constructors and |
| 18 | // destructors. |
| 19 | // |
| 20 | // Runtime constant example: |
| 21 | // const std::string& GetLineSeparator() { |
| 22 | // // Forwards to std::string(size_t, char, const Allocator&) constructor. |
| 23 | // static const base::NoDestructor<std::string> s(5, '-'); |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 24 | // return *s; |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 25 | // } |
| 26 | // |
| 27 | // More complex initialization with a lambda: |
| 28 | // const std::string& GetSessionNonce() { |
| 29 | // static const base::NoDestructor<std::string> nonce([] { |
| 30 | // std::string s(16); |
| 31 | // crypto::RandString(s.data(), s.size()); |
| 32 | // return s; |
Peter Kasting | 602defc | 2018-02-16 03:21:54 | [diff] [blame] | 33 | // }()); |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 34 | // return *nonce; |
| 35 | // } |
| 36 | // |
| 37 | // NoDestructor<T> stores the object inline, so it also avoids a pointer |
Alexander Khaustov | fd6d802 | 2018-02-19 19:18:49 | [diff] [blame] | 38 | // indirection and a malloc. Also note that since C++11 static local variable |
| 39 | // initialization is thread-safe and so is this pattern. Code should prefer to |
| 40 | // use NoDestructor<T> over: |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 41 | // - A function scoped static T* or T& that is dynamically initialized. |
| 42 | // - A global base::LazyInstance<T>. |
| 43 | // |
| 44 | // Note that since the destructor is never run, this *will* leak memory if used |
| 45 | // as a stack or member variable. Furthermore, a NoDestructor<T> should never |
| 46 | // have global scope as that may require a static initializer. |
| 47 | template <typename T> |
| 48 | class NoDestructor { |
| 49 | public: |
| 50 | // Not constexpr; just write static constexpr T x = ...; if the value should |
| 51 | // be a constexpr. |
| 52 | template <typename... Args> |
| 53 | explicit NoDestructor(Args&&... args) { |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 54 | new (storage_) T(std::forward<Args>(args)...); |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 57 | // Allows copy and move construction of the contained type, to allow |
| 58 | // construction from an initializer list, e.g. for std::vector. |
| 59 | explicit NoDestructor(const T& x) { new (storage_) T(x); } |
| 60 | explicit NoDestructor(T&& x) { new (storage_) T(std::move(x)); } |
| 61 | |
| 62 | NoDestructor(const NoDestructor&) = delete; |
| 63 | NoDestructor& operator=(const NoDestructor&) = delete; |
| 64 | |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 65 | ~NoDestructor() = default; |
| 66 | |
| 67 | const T& operator*() const { return *get(); } |
| 68 | T& operator*() { return *get(); } |
| 69 | |
| 70 | const T* operator->() const { return get(); } |
| 71 | T* operator->() { return get(); } |
| 72 | |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 73 | const T* get() const { return reinterpret_cast<const T*>(storage_); } |
| 74 | T* get() { return reinterpret_cast<T*>(storage_); } |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | alignas(T) char storage_[sizeof(T)]; |
Daniel Cheng | bae2473 | 2018-02-14 19:42:14 | [diff] [blame] | 78 | |
| 79 | #if defined(LEAK_SANITIZER) |
| 80 | // TODO(https://crbug.com/812277): This is a hack to work around the fact |
| 81 | // that LSan doesn't seem to treat NoDestructor as a root for reachability |
| 82 | // analysis. This means that code like this: |
| 83 | // static base::NoDestructor<std::vector<int>> v({1, 2, 3}); |
| 84 | // is considered a leak. Using the standard leak sanitizer annotations to |
| 85 | // suppress leaks doesn't work: std::vector is implicitly constructed before |
| 86 | // calling the base::NoDestructor constructor. |
| 87 | // |
| 88 | // Unfortunately, I haven't been able to demonstrate this issue in simpler |
| 89 | // reproductions: until that's resolved, hold an explicit pointer to the |
| 90 | // placement-new'd object in leak sanitizer mode to help LSan realize that |
| 91 | // objects allocated by the contained type are still reachable. |
| 92 | T* storage_ptr_ = reinterpret_cast<T*>(storage_); |
| 93 | #endif // defined(LEAK_SANITIZER) |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace base |
| 97 | |
| 98 | #endif // BASE_NO_DESTRUCTOR_H_ |