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

aligned_storage_for, aligned_storage_for_t

Summary: [Folly] `aligned_storage_for`, `aligned_storage_for_t`.

Reviewed By: aary

Differential Revision: D13448822

fbshipit-source-id: 59a09951e0ff75f8b0aeb201a5c1eb28850ea91d
parent 43bc1a00
......@@ -29,6 +29,7 @@
#include <folly/Conv.h>
#include <folly/Likely.h>
#include <folly/Random.h>
#include <folly/Traits.h>
#include <folly/detail/AtomicUnorderedMapUtils.h>
#include <folly/lang/Bits.h>
#include <folly/portability/SysMman.h>
......@@ -362,8 +363,7 @@ struct AtomicUnorderedInsertMap {
IndexType next_;
/// Key and Value
typename std::aligned_storage<sizeof(value_type), alignof(value_type)>::type
raw_;
aligned_storage_for_t<value_type> raw_;
~Slot() {
auto s = state();
......
......@@ -1439,7 +1439,7 @@ struct SingleElementQueue {
private:
/// Storage for a T constructed with placement new
typename std::aligned_storage<sizeof(T), alignof(T)>::type contents_;
aligned_storage_for_t<T> contents_;
/// Even turns are pushes, odd turns are pops
TurnSequencer<Atom> sequencer_;
......
......@@ -637,7 +637,7 @@ class alignas(T) Replaceable
friend struct replaceable_detail::copy_ctor_mixin<T>;
friend struct replaceable_detail::move_assignment_mixin<T>;
friend struct replaceable_detail::copy_assignment_mixin<T>;
std::aligned_storage_t<sizeof(T), alignof(T)> storage_[1];
aligned_storage_for_t<T> storage_[1];
};
#if __cplusplus > 201402L
......
......@@ -301,6 +301,10 @@ using type_t = typename traits_detail::type_t_<T, Ts...>::type;
template <class... Ts>
using void_t = type_t<void, Ts...>;
template <typename T>
using aligned_storage_for_t =
typename std::aligned_storage<sizeof(T), alignof(T)>::type;
// Older versions of libstdc++ do not provide std::is_trivially_copyable
#if defined(__clang__) && !defined(_LIBCPP_VERSION)
template <class T>
......
......@@ -24,6 +24,7 @@
#include <folly/ConstexprMath.h>
#include <folly/Optional.h>
#include <folly/Traits.h>
#include <folly/concurrency/CacheLocality.h>
#include <folly/lang/Align.h>
#include <folly/synchronization/Hazptr.h>
......@@ -757,7 +758,7 @@ class UnboundedQueue {
*/
class Entry {
Sem flag_;
typename std::aligned_storage<sizeof(T), alignof(T)>::type item_;
aligned_storage_for_t<T> item_;
public:
template <typename Arg>
......
......@@ -560,10 +560,7 @@ struct alignas(kRequiredVectorAlignment) F14Chunk {
// increases nor decreases.
uint8_t outboundOverflowCount_;
std::array<
std::aligned_storage_t<sizeof(Item), alignof(Item)>,
kAllocatedCapacity>
rawItems_;
std::array<aligned_storage_for_t<Item>, kAllocatedCapacity> rawItems_;
static F14Chunk* emptyInstance() {
auto raw = reinterpret_cast<char*>(&kEmptyTagVector);
......
......@@ -779,9 +779,7 @@ struct dynamic : private boost::operators<dynamic> {
* incomplete type right now). (Note that in contrast we know it
* is ok to do this with fbvector because we own it.)
*/
std::aligned_storage<
sizeof(F14NodeMap<int, int>),
alignof(F14NodeMap<int, int>)>::type objectBuffer;
aligned_storage_for_t<F14NodeMap<int, int>> objectBuffer;
} u_;
};
......
......@@ -1127,9 +1127,7 @@ class small_vector : public detail::small_vector_base<
}
} FOLLY_SV_PACK_ATTR;
typedef typename std::aligned_storage<
sizeof(value_type) * MaxInline,
alignof(value_type)>::type InlineStorageDataType;
typedef aligned_storage_for_t<value_type[MaxInline]> InlineStorageDataType;
typedef typename std::conditional<
sizeof(value_type) * MaxInline != 0,
......
......@@ -15,6 +15,8 @@
*/
#pragma once
#include <folly/Traits.h>
#include <folly/synchronization/Hazptr-fwd.h>
#include <folly/synchronization/HazptrDomain.h>
#include <folly/synchronization/HazptrRec.h>
......@@ -194,9 +196,7 @@ FOLLY_ALWAYS_INLINE void swap(
* Type used by hazptr_array and hazptr_local.
*/
template <template <typename> class Atom>
using aligned_hazptr_holder = typename std::aligned_storage<
sizeof(hazptr_holder<Atom>),
alignof(hazptr_holder<Atom>)>::type;
using aligned_hazptr_holder = aligned_storage_for_t<hazptr_holder<Atom>>;
/**
* hazptr_array
......
......@@ -26,6 +26,7 @@
#include <folly/CachelinePadded.h>
#include <folly/IndexedMemPool.h>
#include <folly/Likely.h>
#include <folly/Traits.h>
#include <folly/lang/SafeAssert.h>
#include <folly/synchronization/AtomicStruct.h>
#include <folly/synchronization/SaturatingSemaphore.h>
......@@ -121,7 +122,7 @@ namespace detail {
/// a large static IndexedMemPool of nodes, instead of per-type pools
template <template <typename> class Atom>
struct LifoSemRawNode {
std::aligned_storage<sizeof(void*), alignof(void*)>::type raw;
aligned_storage_for_t<void*> raw;
/// The IndexedMemPool index of the next node in this chain, or 0
/// if none. This will be set to uint32_t(-1) if the node is being
......
......@@ -268,6 +268,18 @@ TEST(Traits, type_t) {
value));
}
TEST(Traits, aligned_storage_for_t) {
struct alignas(2) Foo {
char data[4];
};
using storage = aligned_storage_for_t<Foo[4]>;
EXPECT_EQ(16, sizeof(storage));
EXPECT_EQ(2, alignof(storage));
EXPECT_TRUE(std::is_trivial<storage>::value);
EXPECT_TRUE(std::is_standard_layout<storage>::value);
EXPECT_TRUE(std::is_pod<storage>::value); // pod = trivial + standard-layout
}
TEST(Traits, remove_cvref) {
using folly::remove_cvref;
using folly::remove_cvref_t;
......
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