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.
5 #ifndef BASE_NO_DESTRUCTOR_H_
6 #define BASE_NO_DESTRUCTOR_H_
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
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, '-');
27 // More complex initialization with a lambda:
28 // const std::string& GetSessionNonce() {
29 // static const base::NoDestructor<std::string> nonce([] {
31 // crypto::RandString(s.data(), s.size());
37 // NoDestructor<T> stores the object inline, so it also avoids a pointer
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:
41 // - A function scoped static T* or T& that is dynamically initialized.
42 // - A global base::LazyInstance<T>.
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.
50 // Not constexpr; just write static constexpr T x = ...; if the value should
52 template <typename... Args>
53 explicit NoDestructor(Args&&... args) {
54 new (storage_) T(std::forward<Args>(args)...);
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)); }
62 NoDestructor(const NoDestructor&) = delete;
63 NoDestructor& operator=(const NoDestructor&) = delete;
65 ~NoDestructor() = default;
67 const T& operator*() const { return *get(); }
68 T& operator*() { return *get(); }
70 const T* operator->() const { return get(); }
71 T* operator->() { return get(); }
73 const T* get() const { return reinterpret_cast<const T*>(storage_); }
74 T* get() { return reinterpret_cast<T*>(storage_); }
77 alignas(T) char storage_[sizeof(T)];
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.
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)
98 #endif // BASE_NO_DESTRUCTOR_H_