Implement allocator access utilities

This commit is contained in:
Glen Fernandes
2020-04-13 01:43:43 -04:00
parent 690514e87c
commit c31e23b362
23 changed files with 1461 additions and 0 deletions

View File

@@ -161,6 +161,24 @@ run alloc_construct_cxx11_test.cpp ;
run nvp_test.cpp ;
run allocator_value_type_test.cpp ;
run allocator_pointer_test.cpp ;
run allocator_const_pointer_test.cpp ;
run allocator_void_pointer_test.cpp ;
run allocator_const_void_pointer_test.cpp ;
run allocator_difference_type_test.cpp ;
run allocator_size_type_test.cpp ;
run allocator_pocca_test.cpp ;
run allocator_pocma_test.cpp ;
run allocator_pocs_test.cpp ;
run allocator_is_always_equal_test.cpp ;
run allocator_rebind_test.cpp ;
run allocator_allocate_test.cpp ;
run allocator_allocate_hint_test.cpp ;
run allocator_deallocate_test.cpp ;
run allocator_max_size_test.cpp ;
run allocator_soccc_test.cpp ;
lib lib_typeid : lib_typeid.cpp : <link>shared:<define>LIB_TYPEID_DYN_LINK=1 ;
run test_lib_typeid.cpp lib_typeid : : : <link>shared : test_lib_typeid_shared ;

View File

@@ -0,0 +1,61 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/lightweight_test.hpp>
struct A1 {
typedef int* pointer;
typedef int size_type;
A1()
: value() { }
pointer allocate(size_type n) {
value = n;
return &value;
}
size_type value;
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
struct A2 {
typedef int* pointer;
typedef int size_type;
A2()
: value() { }
pointer allocate(size_type n) {
value = n;
return &value;
}
pointer allocate(size_type n, const void*) {
value = n + 1;
return &value;
}
size_type value;
};
#endif
int main()
{
{
A1 a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 1, 0), 1);
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
{
A2 a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 1, 0), 2);
}
#endif
return boost::report_errors();
}

View File

@@ -0,0 +1,31 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/lightweight_test.hpp>
struct A {
typedef int* pointer;
typedef int size_type;
A()
: value() { }
pointer allocate(size_type n) {
value = n;
return &value;
}
size_type value;
};
int main()
{
A a;
BOOST_TEST_EQ(*boost::allocator_allocate(a, 5), 5);
return boost::report_errors();
}

View File

@@ -0,0 +1,30 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
typedef char value_type;
typedef int* pointer;
};
struct A2 {
typedef char value_type;
typedef int* pointer;
typedef const bool* const_pointer;
};
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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,28 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
typedef int* pointer;
};
struct A2 {
typedef int* pointer;
typedef const bool* const_void_pointer;
};
int main()
{
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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,49 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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;
};
struct A1 { };
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
struct A2 {
void construct(S* p, int v) {
new(p) S(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);
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
{
S* p = d.allocate(1);
A2 a;
boost::allocator_construct(a, p, 1);
BOOST_TEST_EQ(p->value, 2);
d.deallocate(p, 1);
}
#endif
return boost::report_errors();
}

View File

@@ -0,0 +1,31 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/lightweight_test.hpp>
struct A {
typedef int* pointer;
typedef int size_type;
A()
: value() { }
void deallocate(pointer, size_type n) {
value = n;
}
size_type value;
};
int main()
{
A a;
boost::allocator_deallocate(a, 0, 5);
BOOST_TEST_EQ(a.value, 5);
return boost::report_errors();
}

View File

@@ -0,0 +1,62 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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;
}
};
int S::count = 0;
struct A1 { };
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
struct A2 {
void destroy(S*) {
++S::count;
}
};
#endif
int main()
{
boost::default_allocator<S> d;
{
S* p = d.allocate(1);
new(p) S;
A1 a;
boost::allocator_destroy(a, p);
BOOST_TEST_EQ(S::count, 0);
d.deallocate(p, 1);
}
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
{
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);
}
#endif
return boost::report_errors();
}

View File

@@ -0,0 +1,28 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
typedef char* pointer;
};
struct A2 {
typedef char* pointer;
typedef int difference_type;
};
int main()
{
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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,36 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
int value;
};
struct A2 {
struct is_always_equal {
BOOST_STATIC_CONSTEXPR bool value = true;
};
int value;
};
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
struct A3 { };
#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
return boost::report_errors();
}

View File

@@ -0,0 +1,35 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/lightweight_test.hpp>
struct A1 {
typedef long 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 {
return 1;
}
};
#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);
#endif
return boost::report_errors();
}

View File

@@ -0,0 +1,27 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/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;
};
};
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_copy_assignment<A1>::type));
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_copy_assignment<A2>::type));
return boost::report_errors();
}

View File

@@ -0,0 +1,27 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/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;
};
};
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_move_assignment<A1>::type));
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_move_assignment<A2>::type));
return boost::report_errors();
}

View File

@@ -0,0 +1,27 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/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;
};
};
int main()
{
BOOST_TEST_TRAIT_FALSE((boost::
allocator_propagate_on_container_swap<A1>::type));
BOOST_TEST_TRAIT_TRUE((boost::
allocator_propagate_on_container_swap<A2>::type));
return boost::report_errors();
}

View File

@@ -0,0 +1,28 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
typedef char value_type;
};
struct A2 {
typedef char value_type;
typedef int* pointer;
};
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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,30 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/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>
struct rebind {
typedef A1<T, U, V> other;
};
};
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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,34 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
struct A1 {
typedef long difference_type;
};
#else
struct A1 {
typedef unsigned long size_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>));
return boost::report_errors();
}

View File

@@ -0,0 +1,40 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/lightweight_test.hpp>
struct A1 {
A1(int v)
: value(v) { }
int value;
};
#if defined(BOOST_CORE_ALLOCATOR_DETECTION)
struct A2 {
A2(int v)
: value(v) { }
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);
#endif
return boost::report_errors();
}

View File

@@ -0,0 +1,21 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A {
typedef int value_type;
};
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::allocator_value_type<A>::type>));
return boost::report_errors();
}

View File

@@ -0,0 +1,28 @@
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)
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/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
struct A1 {
typedef int* pointer;
};
struct A2 {
typedef int* pointer;
typedef bool* void_pointer;
};
int main()
{
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>));
return boost::report_errors();
}