Revise allocator_access implementation

This commit is contained in:
Glen Fernandes
2020-05-25 15:00:18 -04:00
parent 65901249d5
commit 376aa7aa31
22 changed files with 562 additions and 582 deletions

View File

@@ -6,7 +6,7 @@ Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/config.hpp>
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
#include <boost/core/alloc_construct.hpp>
#include <boost/core/lightweight_test.hpp>

View File

@@ -8,53 +8,51 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
struct A1 {
typedef int* pointer;
typedef int size_type;
typedef T value_type;
typedef std::size_t size_type;
typedef T* pointer;
typedef const T* const_pointer;
template<class U>
struct rebind {
typedef A1<U> other;
};
A1()
: value() { }
pointer allocate(size_type n) {
T* allocate(std::size_t n, const void*) {
value = n;
return &value;
return 0;
}
size_type value;
std::size_t value;
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef int* pointer;
typedef int size_type;
typedef T value_type;
A2()
: value() { }
pointer allocate(size_type n) {
T* allocate(std::size_t n) {
value = n;
return &value;
return 0;
}
pointer allocate(size_type n, const void*) {
value = n + 1;
return &value;
}
size_type value;
std::size_t value;
};
#endif
int main()
{
{
A1 a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 1, 0), 1);
A1<int> a;
BOOST_TEST_NOT(boost::allocator_allocate(a, 5, 0));
BOOST_TEST_EQ(a.value, 5);
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
A2 a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 1, 0), 2);
A2<int> a;
BOOST_TEST_NOT(boost::allocator_allocate(a, 5, 0));
BOOST_TEST_EQ(a.value, 5);
}
#endif
return boost::report_errors();

View File

@@ -8,24 +8,24 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
struct A {
typedef int* pointer;
typedef int size_type;
typedef T value_type;
typedef T* pointer;
typedef std::size_t size_type;
A()
: value() { }
pointer allocate(size_type n) {
T* allocate(std::size_t n) {
value = n;
return &value;
return 0;
}
size_type value;
std::size_t value;
};
int main()
{
A a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 5), 5);
A<int> a;
BOOST_TEST_NOT(boost::allocator_allocate(a, 5));
BOOST_TEST_EQ(a.value, 5);
return boost::report_errors();
}

View File

@@ -9,22 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef char value_type;
typedef int* pointer;
typedef T value_type;
typedef int* const_pointer;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef char value_type;
typedef int* pointer;
typedef const bool* const_pointer;
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const char*,
boost::allocator_const_pointer<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const bool*,
boost::allocator_const_pointer<A2>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::allocator_const_pointer<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const int*,
boost::allocator_const_pointer<A2<int> >::type>));
#endif
return boost::report_errors();
}

View File

@@ -9,20 +9,31 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef int* pointer;
typedef T value_type;
typedef int* const_pointer;
typedef int* const_void_pointer;
template<class U>
struct rebind {
typedef A1<U> other;
};
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef int* pointer;
typedef const bool* const_void_pointer;
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::allocator_const_void_pointer<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const void*,
boost::allocator_const_void_pointer<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const bool*,
boost::allocator_const_void_pointer<A2>::type>));
boost::allocator_const_void_pointer<A2<int> >::type>));
#endif
return boost::report_errors();
}

View File

@@ -6,43 +6,40 @@ Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/allocator_access.hpp>
#include <boost/core/default_allocator.hpp>
#include <boost/core/lightweight_test.hpp>
struct S {
S(int v)
: value(v) { }
int value;
template<class T>
struct A1 {
typedef T value_type;
A1() { }
};
struct A1 { };
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
void construct(S* p, int v) {
new(p) S(v + 1);
typedef T value_type;
A2() { }
template<class U, class V>
void construct(U* p, const V& v) {
::new((void*)p) U(v + 1);
}
};
#endif
int main()
{
boost::default_allocator<S> d;
{
S* p = d.allocate(1);
A1 a;
boost::allocator_construct(a, p, 1);
BOOST_TEST_EQ(p->value, 1);
d.deallocate(p, 1);
A1<int> a;
int i = 0;
boost::allocator_construct(a, &i, 5);
BOOST_TEST_EQ(i, 5);
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
S* p = d.allocate(1);
A2 a;
boost::allocator_construct(a, p, 1);
BOOST_TEST_EQ(p->value, 2);
d.deallocate(p, 1);
A1<int> a;
int i = 0;
boost::allocator_construct(a, &i, 5);
BOOST_TEST_EQ(i, 6);
}
#endif
return boost::report_errors();

View File

@@ -8,23 +8,22 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
struct A {
typedef int* pointer;
typedef int size_type;
typedef T value_type;
typedef T* pointer;
typedef std::size_t size_type;
A()
: value() { }
void deallocate(pointer, size_type n) {
void deallocate(T*, std::size_t n) {
value = n;
}
size_type value;
std::size_t value;
};
int main()
{
A a;
A<int> a;
boost::allocator_deallocate(a, 0, 5);
BOOST_TEST_EQ(a.value, 5);
return boost::report_errors();

View File

@@ -6,20 +6,16 @@ Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/allocator_access.hpp>
#include <boost/core/default_allocator.hpp>
#include <boost/core/lightweight_test.hpp>
struct S {
static int count;
S() {
++count;
}
S(const S&) {
++count;
}
~S() {
--count;
}
@@ -27,35 +23,39 @@ struct S {
int S::count = 0;
struct A1 { };
template<class T>
struct A1 {
typedef T value_type;
A1() { }
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
void destroy(S*) {
++S::count;
typedef T value_type;
A2() { }
template<class U>
void destroy(U* p) {
*p = U();
}
};
#endif
int main()
{
boost::default_allocator<S> d;
{
S* p = d.allocate(1);
new(p) S;
A1 a;
boost::allocator_destroy(a, p);
A1<int> a;
S s;
boost::allocator_destroy(a, &s);
BOOST_TEST_EQ(S::count, 0);
d.deallocate(p, 1);
::new((void*)&s) S();
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
S* p = d.allocate(1);
new(p) S;
A2 a;
boost::allocator_destroy(a, p);
BOOST_TEST_EQ(S::count, 2);
d.deallocate(p, 1);
A1<int> a;
int i = 5;
boost::allocator_destroy(a, &i);
BOOST_TEST_EQ(i, 0);
}
#endif
return boost::report_errors();

View File

@@ -9,20 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef char* pointer;
typedef T value_type;
typedef short difference_type;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef char* pointer;
typedef int difference_type;
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<short,
boost::allocator_difference_type<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::allocator_difference_type<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::allocator_difference_type<A2>::type>));
boost::allocator_difference_type<A2<int> >::type>));
#endif
return boost::report_errors();
}

View File

@@ -9,28 +9,39 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef T value_type;
int value;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
struct is_always_equal {
BOOST_STATIC_CONSTEXPR bool value = true;
};
int value;
typedef T value_type;
};
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
struct A3 { };
template<class T>
struct A3 {
typedef T value_type;
typedef std::false_type is_always_equal;
};
template<class T>
struct A4 {
typedef T value_type;
typedef std::true_type is_always_equal;
int value;
};
#endif
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::allocator_is_always_equal<A1>::type));
BOOST_TEST_TRAIT_TRUE((boost::allocator_is_always_equal<A2>::type));
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
BOOST_TEST_TRAIT_TRUE((boost::allocator_is_always_equal<A3>::type));
#endif
BOOST_TEST_TRAIT_FALSE((boost::allocator_is_always_equal<A1<int> >::type));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::allocator_is_always_equal<A2<int> >::type));
BOOST_TEST_TRAIT_FALSE((boost::allocator_is_always_equal<A3<int> >::type));
BOOST_TEST_TRAIT_TRUE((boost::allocator_is_always_equal<A4<int> >::type));
#endif
return boost::report_errors();
}

View File

@@ -7,29 +7,33 @@ Distributed under the Boost Software License, Version 1.0.
*/
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
#include <limits>
template<class T>
struct A1 {
typedef long value_type;
typedef T value_type;
typedef short size_type;
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
struct A2 {
typedef long value_type;
typedef short size_type;
size_type max_size() const {
A1() { }
short max_size() const {
return 1;
}
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
typedef short size_type;
A2() { }
};
#endif
int main()
{
BOOST_TEST_EQ(boost::allocator_max_size(A1()),
std::numeric_limits<A1::size_type>::max() / sizeof(A1::value_type));
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
BOOST_TEST_EQ(boost::allocator_max_size(A2()), 1);
BOOST_TEST_EQ(boost::allocator_max_size(A1<int>()), 1);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_LE(boost::allocator_max_size(A2<int>()),
(std::numeric_limits<short>::max)());
#endif
return boost::report_errors();
}

View File

@@ -9,19 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 { };
struct A2 {
struct propagate_on_container_copy_assignment {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class T>
struct A1 {
typedef T value_type;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
typedef std::true_type propagate_on_container_copy_assignment;
};
#endif
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_copy_assignment<A1>::type));
allocator_propagate_on_container_copy_assignment<A1<int> >::type));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_copy_assignment<A2>::type));
allocator_propagate_on_container_copy_assignment<A2<int> >::type));
#endif
return boost::report_errors();
}

View File

@@ -9,19 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 { };
struct A2 {
struct propagate_on_container_move_assignment {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class T>
struct A1 {
typedef T value_type;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
typedef std::true_type propagate_on_container_move_assignment;
};
#endif
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_move_assignment<A1>::type));
allocator_propagate_on_container_move_assignment<A1<int> >::type));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_move_assignment<A2>::type));
allocator_propagate_on_container_move_assignment<A2<int> >::type));
#endif
return boost::report_errors();
}

View File

@@ -9,19 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 { };
struct A2 {
struct propagate_on_container_swap {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class T>
struct A1 {
typedef T value_type;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
typedef std::true_type propagate_on_container_swap;
};
#endif
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_swap<A1>::type));
allocator_propagate_on_container_swap<A1<int> >::type));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_swap<A2>::type));
allocator_propagate_on_container_swap<A2<int> >::type));
#endif
return boost::report_errors();
}

View File

@@ -9,20 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef char value_type;
};
struct A2 {
typedef char value_type;
typedef T value_type;
typedef int* pointer;
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<char*,
boost::allocator_pointer<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::allocator_pointer<A2>::type>));
boost::allocator_pointer<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::allocator_pointer<A2<int> >::type>));
#endif
return boost::report_errors();
}

View File

@@ -9,22 +9,29 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class, class, class>
struct A1 { };
template<class, class U, class V>
struct A2 {
template<class T>
template<class T>
struct A1 {
typedef T value_type;
template<class>
struct rebind {
typedef A1<T, U, V> other;
typedef A1<int> other;
};
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<A1<bool, int, float>,
boost::allocator_rebind<A1<char, int, float>, bool>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<A1<bool, int, float>,
boost::allocator_rebind<A2<char, int, float>, bool>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<A1<int>,
boost::allocator_rebind<A1<char>, bool>::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<A2<int>,
boost::allocator_rebind<A2<char>, int>::type>));
#endif
return boost::report_errors();
}

View File

@@ -9,26 +9,26 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
template<class T>
struct A1 {
typedef long difference_type;
typedef T value_type;
typedef int size_type;
};
#else
struct A1 {
typedef unsigned long size_type;
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
};
#endif
struct A2 {
typedef long difference_type;
typedef unsigned short size_type;
};
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<unsigned long,
boost::allocator_size_type<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<unsigned short,
boost::allocator_size_type<A2>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::allocator_size_type<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::size_t,
boost::allocator_size_type<A2<int> >::type>));
#endif
return boost::report_errors();
}

View File

@@ -8,33 +8,34 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
struct A1 {
A1(int v)
: value(v) { }
typedef T value_type;
A1(int n)
: value(n) { }
int value;
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
A2(int v)
: value(v) { }
typedef T value_type;
A2(int n)
: value(n) { }
A2 select_on_container_copy_construction() const {
return A2(value + 1);
}
int value;
};
#endif
int main()
{
BOOST_TEST_EQ(1,
boost::allocator_select_on_container_copy_construction(A1(1)).value);
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
BOOST_TEST_EQ(2,
boost::allocator_select_on_container_copy_construction(A2(1)).value);
BOOST_TEST_EQ(1, boost::
allocator_select_on_container_copy_construction(A1<int>(1)).value);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_EQ(2, boost::
allocator_select_on_container_copy_construction(A2<int>(1)).value);
#endif
return boost::report_errors();
}

View File

@@ -9,13 +9,14 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A {
typedef int value_type;
typedef T value_type;
};
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::allocator_value_type<A>::type>));
boost::allocator_value_type<A<int> >::type>));
return boost::report_errors();
}

View File

@@ -9,20 +9,31 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct A1 {
typedef T value_type;
typedef int* pointer;
typedef int* void_pointer;
template<class U>
struct rebind {
typedef A1<U> other;
};
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef int* pointer;
typedef bool* void_pointer;
typedef T value_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::allocator_void_pointer<A1<char> >::type>));
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<void*,
boost::allocator_void_pointer<A1>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool*,
boost::allocator_void_pointer<A2>::type>));
boost::allocator_void_pointer<A2<int> >::type>));
#endif
return boost::report_errors();
}