Flutter Engine
The Flutter Engine
no_destructor.h
Go to the documentation of this file.
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 ACCESSIBILITY_BASE_NO_DESTRUCTOR_H_
6#define ACCESSIBILITY_BASE_NO_DESTRUCTOR_H_
7
8#include <new>
9#include <utility>
10
11namespace 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, '-');
24// return *s;
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;
33// }());
34// return *nonce;
35// }
36//
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>.
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.
47template <typename T>
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) {
54 new (storage_) T(std::forward<Args>(args)...);
55 }
56
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;
64
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
73 const T* get() const { return reinterpret_cast<const T*>(storage_); }
74 T* get() { return reinterpret_cast<T*>(storage_); }
75
76 private:
77 alignas(T) char storage_[sizeof(T)];
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)
94};
95
96} // namespace base
97
98#endif // ACCESSIBILITY_BASE_NO_DESTRUCTOR_H_
~NoDestructor()=default
const T * operator->() const
Definition: no_destructor.h:70
NoDestructor & operator=(const NoDestructor &)=delete
NoDestructor(const T &x)
Definition: no_destructor.h:59
const T & operator*() const
Definition: no_destructor.h:67
NoDestructor(Args &&... args)
Definition: no_destructor.h:53
const T * get() const
Definition: no_destructor.h:73
NoDestructor(const NoDestructor &)=delete
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
double x
#define T
Definition: precompiler.cc:65