Skip to content

Commit

Permalink
[libc++] static_assert that rebinding the allocator works as expected
Browse files Browse the repository at this point in the history
Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D133638
  • Loading branch information
philnik777 committed Oct 11, 2022
1 parent 42d80de commit d7d586e
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 59 deletions.
41 changes: 18 additions & 23 deletions libcxx/include/__functional/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
__alloc_func* __clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef
typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
_AA;
typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
_AA __a(__f_.second());
typedef __allocator_destructor<_AA> _Dp;
unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -211,8 +209,7 @@ class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>

static void __destroy_and_delete(__alloc_func* __f) {
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
_FunAlloc;
typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
_FunAlloc __a(__f->__get_allocator());
__f->destroy();
__a.deallocate(__f, 1);
Expand Down Expand Up @@ -325,7 +322,7 @@ __base<_Rp(_ArgTypes...)>*
__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.__get_allocator());
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -352,7 +349,7 @@ void
__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.__get_allocator());
__f_.destroy();
__a.deallocate(this, 1);
Expand Down Expand Up @@ -411,8 +408,7 @@ template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
_FunAlloc;
typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;

if (__function::__not_null(__f))
{
Expand Down Expand Up @@ -752,8 +748,7 @@ template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
{
typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
_FunAlloc;
typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;

if (__function::__not_null(__f))
{
Expand Down Expand Up @@ -1353,7 +1348,7 @@ __base<_Rp()>*
__func<_Fp, _Alloc, _Rp()>::__clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -1380,7 +1375,7 @@ void
__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
__f_.~__compressed_pair<_Fp, _Alloc>();
__a.deallocate(this, 1);
Expand Down Expand Up @@ -1439,7 +1434,7 @@ __base<_Rp(_A0)>*
__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -1466,7 +1461,7 @@ void
__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
__f_.~__compressed_pair<_Fp, _Alloc>();
__a.deallocate(this, 1);
Expand Down Expand Up @@ -1525,7 +1520,7 @@ __base<_Rp(_A0, _A1)>*
__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -1552,7 +1547,7 @@ void
__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
__f_.~__compressed_pair<_Fp, _Alloc>();
__a.deallocate(this, 1);
Expand Down Expand Up @@ -1611,7 +1606,7 @@ __base<_Rp(_A0, _A1, _A2)>*
__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand All @@ -1638,7 +1633,7 @@ void
__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
{
typedef allocator_traits<_Alloc> __alloc_traits;
typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
typedef __rebind_alloc<__alloc_traits, __func> _Ap;
_Ap __a(__f_.second());
__f_.~__compressed_pair<_Fp, _Alloc>();
__a.deallocate(this, 1);
Expand Down Expand Up @@ -1814,7 +1809,7 @@ function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
}
else
{
typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
typedef __rebind_alloc<__alloc_traits, _FF> _Ap;
_Ap __a(__a0);
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand Down Expand Up @@ -2092,7 +2087,7 @@ function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
}
else
{
typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
typedef __rebind_alloc<__alloc_traits, _FF> _Ap;
_Ap __a(__a0);
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand Down Expand Up @@ -2370,7 +2365,7 @@ function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
}
else
{
typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
typedef __rebind_alloc<__alloc_traits, _FF> _Ap;
_Ap __a(__a0);
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand Down Expand Up @@ -2648,7 +2643,7 @@ function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp
}
else
{
typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
typedef __rebind_alloc<__alloc_traits, _FF> _Ap;
_Ap __a(__a0);
typedef __allocator_destructor<_Ap> _Dp;
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Expand Down
7 changes: 3 additions & 4 deletions libcxx/include/__hash_table
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ public:
// Create __node

typedef typename _NodeTypes::__node_type __node;
typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
typedef allocator_traits<__node_allocator> __node_traits;
typedef typename _NodeTypes::__void_pointer __void_pointer;
typedef typename _NodeTypes::__node_pointer __node_pointer;
Expand All @@ -913,15 +913,14 @@ private:
// the pointer using 'pointer_traits'.
static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
"Allocator does not rebind pointers in a sane manner.");
typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
__node_base_allocator;
typedef __rebind_alloc<__node_traits, __first_node> __node_base_allocator;
typedef allocator_traits<__node_base_allocator> __node_base_traits;
static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
"Allocator does not rebind pointers in a sane manner.");

private:

typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
typedef __rebind_alloc<__node_traits, __next_pointer> __pointer_allocator;
typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Expand Down
9 changes: 4 additions & 5 deletions libcxx/include/__memory/allocator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,13 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits
}
};

template <class _Traits, class _Tp>
struct __rebind_alloc_helper {
#ifndef _LIBCPP_CXX03_LANG
using type _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;
template <class _Traits, class _Tp>
using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;
#else
using type = typename _Traits::template rebind_alloc<_Tp>::other;
template <class _Traits, class _Tp>
using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other;
#endif
};

// __is_default_allocator
template <class _Tp>
Expand Down
5 changes: 2 additions & 3 deletions libcxx/include/__tree
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ public:
typedef typename _NodeTypes::__parent_pointer __parent_pointer;
typedef typename _NodeTypes::__iter_pointer __iter_pointer;

typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
typedef allocator_traits<__node_allocator> __node_traits;

private:
Expand All @@ -1030,8 +1030,7 @@ private:
// the pointer using 'pointer_traits'.
static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
"Allocator does not rebind pointers in a sane manner.");
typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
__node_base_allocator;
typedef __rebind_alloc<__node_traits, __node_base> __node_base_allocator;
typedef allocator_traits<__node_base_allocator> __node_base_traits;
static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
"Allocator does not rebind pointers in a sane manner.");
Expand Down
9 changes: 5 additions & 4 deletions libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,8 @@ public:
using pointer = typename __alloc_traits::pointer;
using const_pointer = typename __alloc_traits::const_pointer;

using __pointer_allocator = typename __rebind_alloc_helper<__alloc_traits, pointer>::type;
using __const_pointer_allocator = typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type;
using __pointer_allocator = __rebind_alloc<__alloc_traits, pointer>;
using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
using __map = __split_buffer<pointer, __pointer_allocator>;
using __map_alloc_traits = allocator_traits<__pointer_allocator>;
using __map_pointer = typename __map_alloc_traits::pointer;
Expand All @@ -972,8 +972,9 @@ public:
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

static_assert(is_same<allocator_type, typename __rebind_alloc_helper<__alloc_traits, value_type>::type>::value,
"rebinding an allocator to value_type should result in the original allocator");
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");
static_assert(is_nothrow_default_constructible<allocator_type>::value ==
is_nothrow_default_constructible<__pointer_allocator>::value,
"rebinding an allocator should not change excpetion guarantees");
Expand Down
5 changes: 2 additions & 3 deletions libcxx/include/ext/hash_map
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,7 @@ private:
typedef std::pair<key_type, mapped_type> __value_type;
typedef __hash_map_hasher<__value_type, hasher> __hasher;
typedef __hash_map_equal<__value_type, key_equal> __key_equal;
typedef typename std::__rebind_alloc_helper<
std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;

typedef std::__hash_table<__value_type, __hasher,
__key_equal, __allocator_type> __table;
Expand Down Expand Up @@ -757,7 +756,7 @@ private:
typedef std::pair<key_type, mapped_type> __value_type;
typedef __hash_map_hasher<__value_type, hasher> __hasher;
typedef __hash_map_equal<__value_type, key_equal> __key_equal;
typedef typename std::__rebind_alloc_helper<std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;

typedef std::__hash_table<__value_type, __hasher,
__key_equal, __allocator_type> __table;
Expand Down
10 changes: 6 additions & 4 deletions libcxx/include/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,22 @@ protected:
typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
typedef __forward_list_node<value_type, void_pointer> __node;
typedef __begin_node_of<value_type, void_pointer> __begin_node;
typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>, __node>::type __node_allocator;
typedef __rebind_alloc<allocator_traits<allocator_type>, __node> __node_allocator;
typedef allocator_traits<__node_allocator> __node_traits;
typedef typename __node_traits::pointer __node_pointer;

typedef typename __rebind_alloc_helper<
allocator_traits<allocator_type>, __begin_node
>::type __begin_node_allocator;
typedef __rebind_alloc<allocator_traits<allocator_type>, __begin_node> __begin_node_allocator;
typedef typename allocator_traits<__begin_node_allocator>::pointer
__begin_node_pointer;

static_assert((!is_same<allocator_type, __node_allocator>::value),
"internal allocator type must differ from user-specified "
"type; otherwise overload resolution breaks");

static_assert(is_same<allocator_type, __rebind_alloc<__node_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

__compressed_pair<__begin_node, __node_allocator> __before_begin_;

_LIBCPP_INLINE_VISIBILITY
Expand Down
8 changes: 6 additions & 2 deletions libcxx/include/list
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ protected:
typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
typedef __list_node_base<value_type, __void_pointer> __node_base;
typedef __list_node<value_type, __void_pointer> __node;
typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
typedef allocator_traits<__node_allocator> __node_alloc_traits;
typedef typename __node_alloc_traits::pointer __node_pointer;
typedef typename __node_alloc_traits::pointer __node_const_pointer;
Expand All @@ -562,7 +562,7 @@ protected:
typedef typename __alloc_traits::const_pointer const_pointer;
typedef typename __alloc_traits::difference_type difference_type;

typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator;
typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator;
typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
static_assert((!is_same<allocator_type, __node_allocator>::value),
"internal allocator type must differ from user-specified "
Expand Down Expand Up @@ -841,6 +841,10 @@ public:
typedef void __remove_return_type;
#endif

static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

_LIBCPP_INLINE_VISIBILITY
list()
_NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Expand Down
14 changes: 10 additions & 4 deletions libcxx/include/map
Original file line number Diff line number Diff line change
Expand Up @@ -999,12 +999,15 @@ private:

typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
__value_type>::type __allocator_type;
typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
typedef __tree<__value_type, __vc, __allocator_type> __base;
typedef typename __base::__node_traits __node_traits;
typedef allocator_traits<allocator_type> __alloc_traits;

static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

__base __tree_;

public:
Expand Down Expand Up @@ -1776,12 +1779,15 @@ private:

typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
__value_type>::type __allocator_type;
typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
typedef __tree<__value_type, __vc, __allocator_type> __base;
typedef typename __base::__node_traits __node_traits;
typedef allocator_traits<allocator_type> __alloc_traits;

static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

__base __tree_;

public:
Expand Down
8 changes: 8 additions & 0 deletions libcxx/include/set
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ private:
typedef __tree<value_type, value_compare, allocator_type> __base;
typedef allocator_traits<allocator_type> __alloc_traits;

static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

__base __tree_;

public:
Expand Down Expand Up @@ -1064,6 +1068,10 @@ private:
typedef __tree<value_type, value_compare, allocator_type> __base;
typedef allocator_traits<allocator_type> __alloc_traits;

static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
"original allocator");

__base __tree_;

public:
Expand Down
Loading

0 comments on commit d7d586e

Please sign in to comment.