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
;
24 #pragma warning (push)
25 #pragma warning (disable : 4290)
28 #if __cplusplus >= 201103L
29 #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
30 #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER noexcept
32 #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER throw(std::bad_alloc)
33 #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER throw()
36 #if defined(BOOST_GCC) && (BOOST_GCC >= 50000)
37 #pragma GCC diagnostic ignored "-Wsized-deallocation"
40 //ASAN does not support operator new overloading
41 #ifndef BOOST_CONTAINER_ASAN
43 std::size_t allocation_count
= 0;
45 void* operator new[](std::size_t count
) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
48 return std::malloc(count
);
51 void operator delete[](void *p
) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
57 #endif //BOOST_CONTAINER_ASAN
63 #ifndef BOOST_CONTAINER_ASAN
65 void test_new_delete_resource()
67 //Make sure new_delete_resource calls new[]/delete[]
68 std::size_t memcount
= allocation_count
;
69 memory_resource
*mr
= new_delete_resource();
70 //each time should return the same pointer
71 BOOST_TEST(mr
== new_delete_resource());
72 #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
73 BOOST_TEST(memcount
== allocation_count
);
75 void *addr
= mr
->allocate(16, 1);
76 #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
77 BOOST_TEST((allocation_count
- memcount
) == 1);
79 mr
->deallocate(addr
, 16, 1);
80 BOOST_TEST(memcount
== allocation_count
);
83 #endif //BOOST_CONTAINER_ASAN
85 void test_null_memory_resource()
87 //Make sure it throw or returns null
88 memory_resource
*mr
= null_memory_resource();
91 #if !defined(BOOST_NO_EXCEPTIONS)
92 bool bad_allocexception_thrown
= false;
97 BOOST_CATCH(std::bad_alloc
&) {
98 bad_allocexception_thrown
= true;
104 BOOST_TEST(bad_allocexception_thrown
== true);
105 #endif //BOOST_NO_EXCEPTIONS
108 void test_default_resource()
110 //Default resource must be new/delete before set_default_resource
111 BOOST_TEST(get_default_resource() == new_delete_resource());
112 //Set default resource and obtain previous
113 derived_from_memory_resource d
;
114 memory_resource
*prev_default
= set_default_resource(&d
);
115 BOOST_TEST(get_default_resource() == &d
);
116 //Set default resource with null, which should be new/delete
117 prev_default
= set_default_resource(0);
118 BOOST_TEST(prev_default
== &d
);
119 BOOST_TEST(get_default_resource() == new_delete_resource());
124 #ifndef BOOST_CONTAINER_ASAN
125 test_new_delete_resource();
127 test_null_memory_resource();
128 test_default_resource();
129 return ::boost::report_errors();