/* * Copyright 2018 Google LLC * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkNoDestructor_DEFINED #define SkNoDestructor_DEFINED #include <cstddef> #include <new> #include <type_traits> // IWYU pragma: keep #include <utility> // Helper type to create a function-local static variable of type `T` when `T` // has a non-trivial destructor. Storing a `T` in a `SkNoDestructor<T>` will // prevent `~T()` from running, even when the variable goes out of scope. This // code is adapted from `base::NoDestructor<T>` in Chromium. // // Useful when a variable has static storage duration but its type has a // non-trivial destructor. Chromium (and transitively, Skia) bans global // constructors and destructors: using a function-local static variable prevents // the former, while using `SkNoDestructor<T>` prevents the latter. // // ## Caveats // // - Must not be used for locals or fields; by definition, this does not run // destructors, and this will likely lead to memory leaks and other // surprising and undesirable behaviour. // // - If `T` is not constexpr constructible, must be a function-local static // variable, since a global `NoDestructor<T>` will still generate a static // initializer. // // - If `T` is constinit constructible, may be used as a global, but mark the // global `constinit` (once C++20 is available) // // - If the data is rarely used, consider creating it on demand rather than // caching it for the lifetime of the program. Though `SkNoDestructor<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 `SkNoDestructor<T>`: // // const uint64_t GetUnstableSessionSeed() { // // No need to use `SkNoDestructor<T>` as `uint64_t` is trivially // // destructible and does not require a global destructor. // static const uint64_t kSessionSeed = GetRandUint64(); // return kSessionSeed; // } // // ## Example Usage // // const std::string& GetDefaultText() { // // Required since `static const std::string` requires a global destructor. // static const SkNoDestructor<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 SkNoDestructor<std::string> nonce([] { // std::string s(16); // GetRandString(s.data(), s.size()); // return s; // }()); // return *nonce; // } // // ## Thread safety // // Initialization 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 SkNoDestructor { … }; #endif // SkNoDestructor_DEFINED