Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 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) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/container for documentation. |
| 8 | // |
| 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> |
| 14 | |
| 15 | #include "derived_from_memory_resource.hpp" |
| 16 | |
| 17 | #include <cstdlib> |
| 18 | #include <new> |
| 19 | |
| 20 | using namespace boost::container; |
| 21 | using namespace boost::container::pmr; |
| 22 | |
| 23 | std::size_t allocation_count = 0; |
| 24 | |
| 25 | #ifdef BOOST_MSVC |
| 26 | #pragma warning (push) |
| 27 | #pragma warning (disable : 4290) |
| 28 | #endif |
| 29 | |
| 30 | #if __cplusplus >= 201103L |
| 31 | #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER |
| 32 | #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER noexcept |
| 33 | #else |
| 34 | #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER throw(std::bad_alloc) |
| 35 | #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER throw() |
| 36 | #endif |
| 37 | |
| 38 | #if defined(BOOST_GCC) && (BOOST_GCC >= 50000) |
| 39 | #pragma GCC diagnostic ignored "-Wsized-deallocation" |
| 40 | #endif |
| 41 | |
| 42 | void* operator new[](std::size_t count) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER |
| 43 | { |
| 44 | ++allocation_count; |
| 45 | return std::malloc(count); |
| 46 | } |
| 47 | |
| 48 | void operator delete[](void *p) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER |
| 49 | { |
| 50 | --allocation_count; |
| 51 | return std::free(p); |
| 52 | } |
| 53 | |
| 54 | #ifdef BOOST_MSVC |
| 55 | #pragma warning (pop) |
| 56 | #endif |
| 57 | |
| 58 | void test_new_delete_resource() |
| 59 | { |
| 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); |
| 67 | #endif |
| 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); |
| 71 | #endif |
| 72 | mr->deallocate(addr, 16, 1); |
| 73 | BOOST_TEST(memcount == allocation_count); |
| 74 | } |
| 75 | |
| 76 | void test_null_memory_resource() |
| 77 | { |
| 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; |
| 82 | try{ |
| 83 | mr->allocate(1, 1); |
| 84 | } |
| 85 | catch(std::bad_alloc&) { |
| 86 | bad_allocexception_thrown = true; |
| 87 | } |
| 88 | catch(...) { |
| 89 | } |
| 90 | BOOST_TEST(bad_allocexception_thrown == true); |
| 91 | #else |
| 92 | BOOST_TEST(0 == mr->allocate(1, 1)); |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | void test_default_resource() |
| 97 | { |
| 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()); |
| 108 | } |
| 109 | |
| 110 | int main() |
| 111 | { |
| 112 | test_new_delete_resource(); |
| 113 | test_null_memory_resource(); |
| 114 | test_default_resource(); |
| 115 | return ::boost::report_errors(); |
| 116 | } |