summaryrefslogtreecommitdiff
path: root/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/no_destructor.h
blob: 2daa62ceefb935daa4e5274f9370759a2b92484f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PARTITION_ALLOC_PARTITION_ALLOC_BASE_NO_DESTRUCTOR_H_
#define PARTITION_ALLOC_PARTITION_ALLOC_BASE_NO_DESTRUCTOR_H_

#include <new>
#include <type_traits>
#include <utility>

namespace partition_alloc::internal::base {

// Helper type to create a function-local static variable of type `T` when `T`
// has a non-trivial destructor. Storing a `T` in a `base::NoDestructor<T>` will
// prevent `~T()` from running, even when the variable goes out of scope.
//
// Useful when a variable has static storage duration but its type has a
// non-trivial destructor. Chromium bans global constructors and destructors:
// using a function-local static variable prevents the former, while using
// `base::NoDestructor<T>` prevents the latter.
//
// ## Caveats
//
// - Must only be used as a function-local static variable. Declaring a global
//   variable of type `base::NoDestructor<T>` will still generate a global
//   constructor; declaring a local or member variable will lead to memory leaks
//   or other surprising and undesirable behaviour.
//
// - If the data is rarely used, consider creating it on demand rather than
//   caching it for the lifetime of the program. Though `base::NoDestructor<T>`
//   does not heap allocate, the compiler still reserves space in bss for
//   storing `T`, which costs memory at runtime.
//
// - If `T` is trivially destructible, do not use `base::NoDestructor<T>`:
//
//     const uint64_t GetUnstableSessionSeed() {
//       // No need to use `base::NoDestructor<T>` as `uint64_t` is trivially
//       // destructible and does not require a global destructor.
//       static const uint64_t kSessionSeed = base::RandUint64();
//       return kSessionSeed;
//     }
//
// ## Example Usage
//
// const std::string& GetDefaultText() {
//   // Required since `static const std::string` requires a global destructor.
//   static const base::NoDestructor<std::string> s("Hello world!");
//   return *s;
// }
//
// More complex initialization using a lambda:
//
// const std::string& GetRandomNonce() {
//   // `nonce` is initialized with random data the first time this function is
//   // called, but its value is fixed thereafter.
//   static const base::NoDestructor<std::string> nonce([] {
//     std::string s(16);
//     crypto::RandString(s.data(), s.size());
//     return s;
//   }());
//   return *nonce;
// }
//
// ## Thread safety
//
// Initialisation of function-local static variables is thread-safe since C++11.
// The standard guarantees that:
//
// - function-local static variables will be initialised the first time
//   execution passes through the declaration.
//
// - if another thread's execution concurrently passes through the declaration
//   in the middle of initialisation, that thread will wait for the in-progress
//   initialisation to complete.
template <typename T>
class NoDestructor {
 public:
  template <typename... Args>
  constexpr explicit NoDestructor(Args&&... args)
      : storage_(std::forward<Args>(args)...) {}

  // Allows copy and move construction of the contained type, to allow
  // construction from an initializer list, e.g. for std::vector.
  explicit NoDestructor(const T& x) : storage_(x) {}
  explicit NoDestructor(T&& x) : storage_(std::move(x)) {}

  NoDestructor(const NoDestructor&) = delete;
  NoDestructor& operator=(const NoDestructor&) = delete;

  ~NoDestructor() = default;

  const T& operator*() const { return *storage_.get()(); }
  T& operator*() { return *storage_.get(); }

  const T* operator->() const { return storage_.get()(); }
  T* operator->() { return storage_.get(); }

  const T* get() const { return storage_.get(); }
  T* get() { return storage_.get(); }

 private:
  // Do not friend this. This is an implementation detail.
  class DirectStorage {
   public:
    template <typename... Args>
    constexpr explicit DirectStorage(Args&&... args)
        : storage_(std::forward<Args>(args)...) {}

    const T* get() const { return &storage_; }
    T* get() { return &storage_; }

   private:
    T storage_;
  };

  // Do not friend this. This is an implementation detail.
  class PlacementStorage {
   public:
    template <typename... Args>
    explicit PlacementStorage(Args&&... args) {
      new (storage_) T(std::forward<Args>(args)...);
    }

    const T* get() const { return const_cast<PlacementStorage*>(this)->get(); }
    T* get() { return reinterpret_cast<T*>(storage_); }

   private:
    alignas(T) char storage_[sizeof(T)];
  };

  // C++20 provides a constexpr `std::construct_at`, so in theory, both branches
  // could use PlacementStorage. There are some advantages to providing
  // `DirectStorage` though; it can avoid the need to have runtime once-init
  // tracking at all.
  std::conditional_t<std::is_trivially_destructible_v<T>,
                     DirectStorage,
                     PlacementStorage>
      storage_;
};

}  // namespace partition_alloc::internal::base

#endif  // PARTITION_ALLOC_PARTITION_ALLOC_BASE_NO_DESTRUCTOR_H_