global_resource_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "derived_from_memory_resource.hpp"
  15. #include <cstdlib>
  16. #include <new>
  17. using namespace boost::container;
  18. using namespace boost::container::pmr;
  19. #ifdef BOOST_MSVC
  20. #pragma warning (push)
  21. #pragma warning (disable : 4290)
  22. #endif
  23. #if __cplusplus >= 201103L
  24. #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
  25. #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER noexcept
  26. #else
  27. #define BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER throw(std::bad_alloc)
  28. #define BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER throw()
  29. #endif
  30. #if defined(BOOST_GCC) && (BOOST_GCC >= 50000)
  31. #pragma GCC diagnostic ignored "-Wsized-deallocation"
  32. #endif
  33. //ASAN does not support operator new overloading
  34. #ifndef BOOST_CONTAINER_ASAN
  35. std::size_t allocation_count = 0;
  36. void* operator new[](std::size_t count) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
  37. {
  38. ++allocation_count;
  39. return std::malloc(count);
  40. }
  41. void operator delete[](void *p) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
  42. {
  43. --allocation_count;
  44. return std::free(p);
  45. }
  46. #endif //BOOST_CONTAINER_ASAN
  47. #ifdef BOOST_MSVC
  48. #pragma warning (pop)
  49. #endif
  50. #ifndef BOOST_CONTAINER_ASAN
  51. void test_new_delete_resource()
  52. {
  53. //Make sure new_delete_resource calls new[]/delete[]
  54. std::size_t memcount = allocation_count;
  55. memory_resource *mr = new_delete_resource();
  56. //each time should return the same pointer
  57. BOOST_TEST(mr == new_delete_resource());
  58. #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
  59. BOOST_TEST(memcount == allocation_count);
  60. #endif
  61. void *addr = mr->allocate(16, 1);
  62. #if !defined(BOOST_CONTAINER_DYNAMIC_LINKING) //No new delete replacement possible new_delete is a DLL
  63. BOOST_TEST((allocation_count - memcount) == 1);
  64. #endif
  65. mr->deallocate(addr, 16, 1);
  66. BOOST_TEST(memcount == allocation_count);
  67. }
  68. #endif //BOOST_CONTAINER_ASAN
  69. void test_null_memory_resource()
  70. {
  71. //Make sure it throw or returns null
  72. memory_resource *mr = null_memory_resource();
  73. #if !defined(BOOST_NO_EXCEPTIONS)
  74. bool bad_allocexception_thrown = false;
  75. try{
  76. mr->allocate(1, 1);
  77. }
  78. catch(std::bad_alloc&) {
  79. bad_allocexception_thrown = true;
  80. }
  81. catch(...) {
  82. }
  83. BOOST_TEST(bad_allocexception_thrown == true);
  84. #else
  85. BOOST_TEST(0 == mr->allocate(1, 1));
  86. #endif
  87. }
  88. void test_default_resource()
  89. {
  90. //Default resource must be new/delete before set_default_resource
  91. BOOST_TEST(get_default_resource() == new_delete_resource());
  92. //Set default resource and obtain previous
  93. derived_from_memory_resource d;
  94. memory_resource *prev_default = set_default_resource(&d);
  95. BOOST_TEST(get_default_resource() == &d);
  96. //Set default resource with null, which should be new/delete
  97. prev_default = set_default_resource(0);
  98. BOOST_TEST(prev_default == &d);
  99. BOOST_TEST(get_default_resource() == new_delete_resource());
  100. }
  101. int main()
  102. {
  103. #ifndef BOOST_CONTAINER_ASAN
  104. test_new_delete_resource();
  105. #endif
  106. test_null_memory_resource();
  107. test_default_resource();
  108. return ::boost::report_errors();
  109. }