Commit e984df45 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

aligned and cacheline_aligned

Summary: [Folly] `aligned` and `cacheline_aligned`, aligned and implicitly padded value wrapper types.

Reviewed By: nbronson

Differential Revision: D17386115

fbshipit-source-id: 6da33a4d97f40ada59682e02af9d56863ccaff3d
parent 093c9442
......@@ -720,6 +720,7 @@ if (BUILD_TESTS)
TEST ssl_errors_test SOURCES SSLErrorsTest.cpp
DIRECTORY lang/test/
TEST aligned_test SOURCES AlignedTest.cpp
TEST bits_test SOURCES BitsTest.cpp
TEST cold_class_test SOURCES ColdClassTest.cpp
TEST safe_assert_test SOURCES SafeAssertTest.cpp
......
/*
* Copyright 2019-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 <cstddef>
#include <type_traits>
#include <utility>
#include <folly/Utility.h>
#include <folly/lang/Align.h>
namespace folly {
template <typename T, std::size_t Align>
class aligned {
static_assert(!(Align & (Align - 1)), "alignment not a power of two");
static_assert(alignof(T) <= Align, "alignment too small");
public:
using alignment = index_constant<Align>;
using value_type = T;
aligned() = default;
aligned(aligned const&) = default;
aligned(aligned&&) = default;
template <
typename S = T,
std::enable_if_t<std::is_copy_constructible<S>::value, int> = 0>
aligned(T const& value) noexcept(std::is_nothrow_copy_constructible<T>::value)
: value_(value) {}
template <
typename S = T,
std::enable_if_t<std::is_move_constructible<S>::value, int> = 0>
aligned(T&& value) noexcept(std::is_nothrow_move_constructible<T>::value)
: value_(static_cast<T&&>(value)) {}
template <
typename... A,
std::enable_if_t<std::is_constructible<T, A...>::value, int> = 0>
explicit aligned(in_place_t, A&&... a) noexcept(
std::is_nothrow_constructible<T, A...>::value)
: value_(static_cast<A&&>(a)...) {}
aligned& operator=(aligned const&) = default;
aligned& operator=(aligned&&) = default;
template <
typename S = T,
std::enable_if_t<std::is_copy_assignable<S>::value, int> = 0>
aligned& operator=(T const& value) noexcept(
std::is_nothrow_copy_assignable<T>::value) {
value_ = value;
return *this;
}
template <
typename S = T,
std::enable_if_t<std::is_move_assignable<S>::value, int> = 0>
aligned& operator=(T&& value) noexcept(
std::is_nothrow_move_assignable<T>::value) {
value_ = std::move(value);
return *this;
}
T* get() noexcept {
return &value_;
}
T const* get() const noexcept {
return &value_;
}
T* operator->() noexcept {
return &value_;
}
T const* operator->() const noexcept {
return &value_;
}
T& operator*() noexcept {
return value_;
}
T const& operator*() const noexcept {
return value_;
}
private:
alignas(Align) T value_;
};
template <typename T>
using cacheline_aligned = aligned<
T,
(cacheline_align_v < alignof(T) ? alignof(T) : cacheline_align_v)>;
} // namespace folly
/*
* Copyright 2019-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 <folly/lang/Aligned.h>
#include <folly/lang/Align.h>
#include <folly/portability/GTest.h>
template <typename T>
using A = folly::aligned<T, folly::max_align_v>;
class AlignedTest : public testing::Test {};
TEST_F(AlignedTest, constructors) {
{
struct T {
T() = delete;
};
EXPECT_FALSE((std::is_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>>::value));
}
{
struct T {
T() noexcept(false) {}
};
EXPECT_TRUE((std::is_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>>::value));
}
{
struct T {
T() noexcept(true) {}
};
EXPECT_TRUE((std::is_constructible<A<T>>::value));
EXPECT_TRUE((std::is_nothrow_constructible<A<T>>::value));
}
{
struct T {
T(T const&) = delete;
};
EXPECT_FALSE((std::is_copy_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_copy_constructible<A<T>>::value));
EXPECT_FALSE((std::is_constructible<A<T>, T const&>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, T const&>::value));
}
{
struct T {
T(T const&) noexcept(false) {}
};
EXPECT_TRUE((std::is_copy_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_copy_constructible<A<T>>::value));
EXPECT_TRUE((std::is_constructible<A<T>, T const&>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, T const&>::value));
}
{
struct T {
T(T const&) noexcept(true) {}
};
EXPECT_TRUE((std::is_copy_constructible<A<T>>::value));
EXPECT_TRUE((std::is_nothrow_copy_constructible<A<T>>::value));
EXPECT_TRUE((std::is_constructible<A<T>, T const&>::value));
EXPECT_TRUE((std::is_nothrow_constructible<A<T>, T const&>::value));
}
{
struct T {
T(T&&) = delete;
};
EXPECT_FALSE((std::is_move_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_move_constructible<A<T>>::value));
EXPECT_FALSE((std::is_constructible<A<T>, T&&>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, T&&>::value));
}
{
struct T {
T(T&&) noexcept(false) {}
};
EXPECT_TRUE((std::is_move_constructible<A<T>>::value));
EXPECT_FALSE((std::is_nothrow_move_constructible<A<T>>::value));
EXPECT_TRUE((std::is_constructible<A<T>, T&&>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, T&&>::value));
}
{
struct T {
T(T&&) noexcept(true) {}
};
EXPECT_TRUE((std::is_move_constructible<A<T>>::value));
EXPECT_TRUE((std::is_nothrow_move_constructible<A<T>>::value));
EXPECT_TRUE((std::is_constructible<A<T>, T&&>::value));
EXPECT_TRUE((std::is_nothrow_constructible<A<T>, T&&>::value));
}
{
using P = folly::in_place_t;
struct L {};
struct M {};
struct N {};
struct T {
explicit T(M) noexcept(false) {}
explicit T(N) noexcept(true) {}
};
EXPECT_FALSE((std::is_constructible<A<T>, P, L>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, P, L>::value));
EXPECT_TRUE((std::is_constructible<A<T>, P, M>::value));
EXPECT_FALSE((std::is_nothrow_constructible<A<T>, P, M>::value));
EXPECT_TRUE((std::is_constructible<A<T>, P, N>::value));
EXPECT_TRUE((std::is_nothrow_constructible<A<T>, P, N>::value));
}
}
// TODO ...
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