memory_resource.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef BHO_CORE_MEMORY_RESOURCE_HPP_INCLUDED
  2. #define BHO_CORE_MEMORY_RESOURCE_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // Copyright 2023 Peter Dimov
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // https://www.boost.org/LICENSE_1_0.txt
  10. #include <asio2/bho/core/max_align.hpp>
  11. #include <asio2/bho/config.hpp>
  12. #include <asio2/bho/config/workaround.hpp>
  13. #include <cstddef>
  14. // Define our own placement new to avoid the inclusion of <new>
  15. // (~9K extra lines) at Ion Gaztanhaga's request.
  16. //
  17. // We can use our own because [intro.object] p13 says:
  18. //
  19. // Any implicit or explicit invocation of a function named `operator new`
  20. // or `operator new[]` implicitly creates objects in the returned region of
  21. // storage and returns a pointer to a suitable created object.
  22. namespace bho
  23. {
  24. namespace core
  25. {
  26. namespace detail
  27. {
  28. struct placement_new_tag {};
  29. } // namespace detail
  30. } // namespace core
  31. } // namespace bho
  32. inline void* operator new( std::size_t, void* p, bho::core::detail::placement_new_tag )
  33. {
  34. return p;
  35. }
  36. inline void operator delete( void*, void*, bho::core::detail::placement_new_tag )
  37. {
  38. }
  39. namespace bho
  40. {
  41. namespace core
  42. {
  43. class memory_resource
  44. {
  45. public:
  46. #if defined(BHO_NO_CXX11_DEFAULTED_FUNCTIONS) || BHO_WORKAROUND(BHO_GCC, < 40700)
  47. virtual ~memory_resource() {}
  48. #else
  49. virtual ~memory_resource() = default;
  50. #endif
  51. BHO_ATTRIBUTE_NODISCARD void* allocate( std::size_t bytes, std::size_t alignment = max_align )
  52. {
  53. // https://github.com/boostorg/container/issues/199
  54. // https://cplusplus.github.io/LWG/issue3471
  55. return ::operator new( bytes, do_allocate( bytes, alignment ), core::detail::placement_new_tag() );
  56. }
  57. void deallocate( void* p, std::size_t bytes, std::size_t alignment = max_align )
  58. {
  59. do_deallocate( p, bytes, alignment );
  60. }
  61. bool is_equal( memory_resource const & other ) const BHO_NOEXCEPT
  62. {
  63. return do_is_equal( other );
  64. }
  65. private:
  66. virtual void* do_allocate( std::size_t bytes, std::size_t alignment ) = 0;
  67. virtual void do_deallocate( void* p, std::size_t bytes, std::size_t alignment ) = 0;
  68. virtual bool do_is_equal( memory_resource const & other ) const BHO_NOEXCEPT = 0;
  69. };
  70. inline bool operator==( memory_resource const& a, memory_resource const& b ) BHO_NOEXCEPT
  71. {
  72. return &a == &b || a.is_equal( b );
  73. }
  74. inline bool operator!=( memory_resource const& a, memory_resource const& b ) BHO_NOEXCEPT
  75. {
  76. return !( a == b );
  77. }
  78. } // namespace core
  79. } // namespace bho
  80. #endif // #ifndef BHO_CORE_MEMORY_RESOURCE_HPP_INCLUDED