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