From: Yedidya Feldblum Date: Sun, 29 Oct 2017 10:33:14 +0000 (-0700) Subject: Move folly/Launder.h X-Git-Tag: v2017.10.30.00~3 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=8034b69f9c27a6acf87bb5d77c2a850365b15d0d;p=folly.git Move folly/Launder.h Summary: [Folly] Move `folly/Launder.h` to `folly/lang/`. Reviewed By: luciang Differential Revision: D6182882 fbshipit-source-id: 97e46bd4e4d6f212d8234209ee90a41e7850ecb9 --- diff --git a/folly/Launder.h b/folly/Launder.h deleted file mode 100644 index fb0e5f7a..00000000 --- a/folly/Launder.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -namespace folly { - -/** - * Approximate backport from C++17 of std::launder. It should be `constexpr` - * but that can't be done without specific support from the compiler. - */ -template -FOLLY_NODISCARD inline T* launder(T* in) noexcept { -#if FOLLY_HAS_BUILTIN(__builtin_launder) || __GNUC__ >= 7 - // The builtin has no unwanted side-effects. - return __builtin_launder(in); -#elif __GNUC__ - // This inline assembler block declares that `in` is an input and an output, - // so the compiler has to assume that it has been changed inside the block. - __asm__("" : "+r"(in)); - return in; -#elif defined(_WIN32) - // MSVC does not currently have optimizations around const members of structs. - // _ReadWriteBarrier() will prevent compiler reordering memory accesses. - _ReadWriteBarrier(); - return in; -#else - static_assert( - false, "folly::launder is not implemented for this environment"); -#endif -} - -/* The standard explicitly forbids laundering these */ -void launder(void*) = delete; -void launder(void const*) = delete; -void launder(void volatile*) = delete; -void launder(void const volatile*) = delete; -template -void launder(T (*)(Args...)) = delete; -} // namespace folly diff --git a/folly/Makefile.am b/folly/Makefile.am index a35a4a94..337b1cd6 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -313,7 +313,7 @@ nobase_follyinclude_HEADERS = \ io/async/test/Util.h \ json.h \ lang/Assume.h \ - Launder.h \ + lang/Launder.h \ Lazy.h \ LifoSem.h \ Likely.h \ diff --git a/folly/Optional.h b/folly/Optional.h index 39abcc1c..acd0b126 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -61,10 +61,10 @@ #include #include -#include #include #include #include +#include namespace folly { diff --git a/folly/Replaceable.h b/folly/Replaceable.h index b786f8e8..92d1ec6f 100644 --- a/folly/Replaceable.h +++ b/folly/Replaceable.h @@ -21,10 +21,10 @@ #include #include -#include #include #include #include +#include /** * An instance of `Replaceable` wraps an instance of `T`. diff --git a/folly/lang/Launder.h b/folly/lang/Launder.h new file mode 100644 index 00000000..fb0e5f7a --- /dev/null +++ b/folly/lang/Launder.h @@ -0,0 +1,56 @@ +/* + * Copyright 2017-present Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace folly { + +/** + * Approximate backport from C++17 of std::launder. It should be `constexpr` + * but that can't be done without specific support from the compiler. + */ +template +FOLLY_NODISCARD inline T* launder(T* in) noexcept { +#if FOLLY_HAS_BUILTIN(__builtin_launder) || __GNUC__ >= 7 + // The builtin has no unwanted side-effects. + return __builtin_launder(in); +#elif __GNUC__ + // This inline assembler block declares that `in` is an input and an output, + // so the compiler has to assume that it has been changed inside the block. + __asm__("" : "+r"(in)); + return in; +#elif defined(_WIN32) + // MSVC does not currently have optimizations around const members of structs. + // _ReadWriteBarrier() will prevent compiler reordering memory accesses. + _ReadWriteBarrier(); + return in; +#else + static_assert( + false, "folly::launder is not implemented for this environment"); +#endif +} + +/* The standard explicitly forbids laundering these */ +void launder(void*) = delete; +void launder(void const*) = delete; +void launder(void volatile*) = delete; +void launder(void const volatile*) = delete; +template +void launder(T (*)(Args...)) = delete; +} // namespace folly diff --git a/folly/lang/test/LaunderTest.cpp b/folly/lang/test/LaunderTest.cpp new file mode 100644 index 00000000..df3b555d --- /dev/null +++ b/folly/lang/test/LaunderTest.cpp @@ -0,0 +1,28 @@ +/* + * Copyright 2017-present Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using namespace ::testing; +using namespace folly; + +TEST(LaunderTest, Basics) { + int a; + int* pa = &a; + EXPECT_EQ(pa, launder(pa)); + EXPECT_TRUE(noexcept(launder(pa))); +} diff --git a/folly/test/LaunderTest.cpp b/folly/test/LaunderTest.cpp deleted file mode 100644 index fb33c98a..00000000 --- a/folly/test/LaunderTest.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2017-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -using namespace ::testing; -using namespace folly; - -TEST(LaunderTest, Basics) { - int a; - int* pa = &a; - EXPECT_EQ(pa, launder(pa)); - EXPECT_TRUE(noexcept(launder(pa))); -}