Commit baab3743 authored by Peter Griess's avatar Peter Griess Committed by Sara Golemon

Detect and use std::is_trivially_copyable<T> where possible.

Summary:
- Sadly, boost::has_trivial_copy<T> is not a suitable replacement for
std::is_trivially_copyable<T> on libc++. Fortunately, the latter is
actually supported (and works), so use it directly.

Test Plan:
- fbconfig -r folly && fbmake runtests
- ./configure && make check on Ubuntu/FC/Mac

Reviewed By: delong.j@fb.com

FB internal diff: D1008921
parent 60f2c87d
...@@ -137,4 +137,21 @@ struct MaxAlign { char c; } __attribute__((aligned)); ...@@ -137,4 +137,21 @@ struct MaxAlign { char c; } __attribute__((aligned));
#include "folly/detail/Clock.h" #include "folly/detail/Clock.h"
#endif #endif
// Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
// usage of __has_trivial_copy(), so we can't use it as a
// least-common-denominator for C++11 implementations that don't support
// std::is_trivially_copyable<T>.
//
// http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
//
// As a result, use std::is_trivially_copyable() where it exists, and fall back
// to Boost otherwise.
#if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
#include <type_traits>
#define FOLLY_IS_TRIVIALLY_COPYABLE(T) (std::is_trivially_copyable<T>::value)
#else
#include <boost/type_traits.hpp>
#define FOLLY_IS_TRIVIALLY_COPYABLE(T) (boost::has_trivial_copy<T>::value)
#endif
#endif // FOLLY_PORTABILITY_H_ #endif // FOLLY_PORTABILITY_H_
...@@ -118,6 +118,15 @@ AC_COMPILE_IFELSE( ...@@ -118,6 +118,15 @@ AC_COMPILE_IFELSE(
], ],
[AC_DEFINE([USE_LIBCPP], [1], [Define to 1 if we're using libc++.])]) [AC_DEFINE([USE_LIBCPP], [1], [Define to 1 if we're using libc++.])])
AC_COMPILE_IFELSE(
[AC_LANG_SOURCE[
#include <type_traits>
const bool val = std::is_trivially_copyable<bool>::value;]
],
[AC_DEFINE([HAVE_STD__IS_TRIVIALLY_COPYABLE], [1],
[Define to 1 if we have a usable std::is_trivially_copyable<T>
implementation.])])
# Check for clock_gettime(2). This is not in an AC_CHECK_FUNCS() because we # Check for clock_gettime(2). This is not in an AC_CHECK_FUNCS() because we
# want to link with librt if necessary. # want to link with librt if necessary.
AC_SEARCH_LIBS([clock_gettime], [rt], AC_SEARCH_LIBS([clock_gettime], [rt],
......
...@@ -116,7 +116,7 @@ namespace detail { ...@@ -116,7 +116,7 @@ namespace detail {
*/ */
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
!boost::has_trivial_copy<T>::value !FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type >::type
moveToUninitialized(T* first, T* last, T* out) { moveToUninitialized(T* first, T* last, T* out) {
auto const count = last - first; auto const count = last - first;
...@@ -138,11 +138,10 @@ namespace detail { ...@@ -138,11 +138,10 @@ namespace detail {
} }
} }
// Specialization for trivially copyable types. (TODO: change to // Specialization for trivially copyable types.
// std::is_trivially_copyable when that works.)
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
boost::has_trivial_copy<T>::value FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type >::type
moveToUninitialized(T* first, T* last, T* out) { moveToUninitialized(T* first, T* last, T* out) {
std::memmove(out, first, (last - first) * sizeof *first); std::memmove(out, first, (last - first) * sizeof *first);
...@@ -156,7 +155,7 @@ namespace detail { ...@@ -156,7 +155,7 @@ namespace detail {
*/ */
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
!boost::has_trivial_copy<T>::value !FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type >::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) { moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
if (lastConstructed == realLast) { if (lastConstructed == realLast) {
...@@ -195,7 +194,7 @@ namespace detail { ...@@ -195,7 +194,7 @@ namespace detail {
// change to std::is_trivially_copyable when that works.) // change to std::is_trivially_copyable when that works.)
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
boost::has_trivial_copy<T>::value FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type >::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) { moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
std::move_backward(first, lastConstructed, realLast); std::move_backward(first, lastConstructed, realLast);
......
...@@ -84,8 +84,8 @@ struct NontrivialType { ...@@ -84,8 +84,8 @@ struct NontrivialType {
int32_t a; int32_t a;
}; };
static_assert(!boost::has_trivial_copy<NontrivialType>::value, static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NontrivialType),
"NontrivialType isn't trivially copyable"); "NontrivialType is trivially copyable");
int NontrivialType::ctored = 0; int NontrivialType::ctored = 0;
...@@ -148,6 +148,9 @@ struct NoncopyableCounter { ...@@ -148,6 +148,9 @@ struct NoncopyableCounter {
}; };
int NoncopyableCounter::alive = 0; int NoncopyableCounter::alive = 0;
static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NoncopyableCounter),
"NoncopyableCounter is trivially copyable");
// Check that throws don't break the basic guarantee for some cases. // Check that throws don't break the basic guarantee for some cases.
// Uses the method for testing exception safety described at // Uses the method for testing exception safety described at
// http://www.boost.org/community/exception_safety.html, to force all // http://www.boost.org/community/exception_safety.html, to force all
...@@ -472,9 +475,10 @@ TEST(small_vector, Iteration) { ...@@ -472,9 +475,10 @@ TEST(small_vector, Iteration) {
} }
TEST(small_vector, NonCopyableType) { TEST(small_vector, NonCopyableType) {
folly::small_vector<std::unique_ptr<std::string>,2> vec; folly::small_vector<NontrivialType,2> vec;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
vec.emplace(vec.begin(), new std::string("asd")); vec.emplace(vec.begin(), 13);
} }
EXPECT_EQ(vec.size(), 10); EXPECT_EQ(vec.size(), 10);
auto vec2 = std::move(vec); auto vec2 = std::move(vec);
......
...@@ -179,7 +179,7 @@ void uninitializedFillDefaultOrFree(T * b, size_t n) { ...@@ -179,7 +179,7 @@ void uninitializedFillDefaultOrFree(T * b, size_t n) {
template <class T> template <class T>
void uninitializedFillOrFree(T * b, size_t n, const T& value) { void uninitializedFillOrFree(T * b, size_t n, const T& value) {
auto const e = b + n; auto const e = b + n;
if (boost::has_trivial_copy<T>::value) { if (FOLLY_IS_TRIVIALLY_COPYABLE(T)) {
auto i = b; auto i = b;
auto const e1 = b + (n & ~size_t(7)); auto const e1 = b + (n & ~size_t(7));
for (; i != e1; i += 8) { for (; i != e1; i += 8) {
...@@ -764,7 +764,7 @@ public: ...@@ -764,7 +764,7 @@ public:
memmove(const_cast<T*>(position) + n, memmove(const_cast<T*>(position) + n,
position, position,
sizeof(T) * (e_ - position)); sizeof(T) * (e_ - position));
if (boost::has_trivial_copy<T>::value) { if (FOLLY_IS_TRIVIALLY_COPYABLE(T)) {
std::uninitialized_fill(const_cast<T*>(position), std::uninitialized_fill(const_cast<T*>(position),
const_cast<T*>(position) + n, const_cast<T*>(position) + n,
x); x);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment