]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under both the GPLv2 (found in the | |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
5 | ||
6 | #pragma once | |
7 | ||
8 | #include <new> | |
9 | ||
10 | #include <folly/Portability.h> | |
11 | ||
12 | /*** | |
13 | * include or backport: | |
14 | * * std::launder | |
15 | */ | |
16 | ||
17 | namespace folly { | |
18 | ||
19 | /** | |
20 | * Approximate backport from C++17 of std::launder. It should be `constexpr` | |
21 | * but that can't be done without specific support from the compiler. | |
22 | */ | |
23 | template <typename T> | |
24 | FOLLY_NODISCARD inline T* launder(T* in) noexcept { | |
25 | #if FOLLY_HAS_BUILTIN(__builtin_launder) || __GNUC__ >= 7 | |
26 | // The builtin has no unwanted side-effects. | |
27 | return __builtin_launder(in); | |
28 | #elif __GNUC__ | |
29 | // This inline assembler block declares that `in` is an input and an output, | |
30 | // so the compiler has to assume that it has been changed inside the block. | |
31 | __asm__("" : "+r"(in)); | |
32 | return in; | |
33 | #elif defined(_WIN32) | |
34 | // MSVC does not currently have optimizations around const members of structs. | |
35 | // _ReadWriteBarrier() will prevent compiler reordering memory accesses. | |
36 | _ReadWriteBarrier(); | |
37 | return in; | |
38 | #else | |
39 | static_assert( | |
40 | false, "folly::launder is not implemented for this environment"); | |
41 | #endif | |
42 | } | |
43 | ||
44 | /* The standard explicitly forbids laundering these */ | |
45 | void launder(void*) = delete; | |
46 | void launder(void const*) = delete; | |
47 | void launder(void volatile*) = delete; | |
48 | void launder(void const volatile*) = delete; | |
49 | template <typename T, typename... Args> | |
50 | void launder(T (*)(Args...)) = delete; | |
51 | } // namespace folly |