1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/container/pmr/global_resource.hpp>
11 #include <boost/container/pmr/memory_resource.hpp>
12 #include <boost/core/lightweight_test.hpp>
13 #include <boost/core/no_exceptions_support.hpp>
15 #include "derived_from_memory_resource.hpp"
20 using namespace boost::container
;
21 using namespace boost::container::pmr
;
23 std::size_t allocation_count
= 0;
26 #pragma warning (push)
27 #pragma warning (disable : 4290)
30 #if __cplusplus >= 201103L
31 #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
32 #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER noexcept
34 #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER throw(std::bad_alloc)
35 #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER throw()
38 #if defined(BOOST_GCC) && (BOOST_GCC >= 50000)
39 #pragma GCC diagnostic ignored "-Wsized-deallocation"
42 void* operator new[](std::size_t count
) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
45 return std::malloc(count
);
48 void operator delete[](void *p
) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
58 void test_new_delete_resource()
60 //Make sure new_delete_resource calls new[]/delete[]
61 std::size_t memcount
= allocation_count
;
62 memory_resource
*mr
= new_delete_resource();
63 //each time should return the same pointer
64 BOOST_TEST(mr
== new_delete_resource());
65 #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
66 BOOST_TEST(memcount
== allocation_count
);
68 void *addr
= mr
->allocate(16, 1);
69 #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
70 BOOST_TEST((allocation_count
- memcount
) == 1);
72 mr
->deallocate(addr
, 16, 1);
73 BOOST_TEST(memcount
== allocation_count
);
76 void test_null_memory_resource()
78 //Make sure it throw or returns null
79 memory_resource
*mr
= null_memory_resource();
80 #if !defined(BOOST_NO_EXCEPTIONS)
81 bool bad_allocexception_thrown
= false;
85 catch(std::bad_alloc
&) {
86 bad_allocexception_thrown
= true;
90 BOOST_TEST(bad_allocexception_thrown
== true);
92 BOOST_TEST(0 == mr
->allocate(1, 1));
96 void test_default_resource()
98 //Default resource must be new/delete before set_default_resource
99 BOOST_TEST(get_default_resource() == new_delete_resource());
100 //Set default resource and obtain previous
101 derived_from_memory_resource d
;
102 memory_resource
*prev_default
= set_default_resource(&d
);
103 BOOST_TEST(get_default_resource() == &d
);
104 //Set default resource with null, which should be new/delete
105 prev_default
= set_default_resource(0);
106 BOOST_TEST(prev_default
== &d
);
107 BOOST_TEST(get_default_resource() == new_delete_resource());
112 test_new_delete_resource();
113 test_null_memory_resource();
114 test_default_resource();
115 return ::boost::report_errors();