default_resource.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DEFAULT_RESOURCE_HPP
  10. #define BOOST_JSON_DEFAULT_RESOURCE_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #include <new>
  13. namespace boost {
  14. namespace json {
  15. namespace detail {
  16. #ifdef _MSC_VER
  17. #pragma warning(push)
  18. #pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
  19. #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  20. #endif
  21. // A simple memory resource that uses operator new and delete.
  22. class
  23. BOOST_SYMBOL_VISIBLE
  24. BOOST_JSON_DECL
  25. default_resource final
  26. : public container::pmr::memory_resource
  27. {
  28. union holder;
  29. #ifndef BOOST_JSON_WEAK_CONSTINIT
  30. # ifndef BOOST_JSON_NO_DESTROY
  31. static holder instance_;
  32. # else
  33. BOOST_JSON_NO_DESTROY
  34. static default_resource instance_;
  35. # endif
  36. #endif
  37. public:
  38. static
  39. container::pmr::memory_resource*
  40. get() noexcept
  41. {
  42. #ifdef BOOST_JSON_WEAK_CONSTINIT
  43. static default_resource instance_;
  44. #endif
  45. return reinterpret_cast<memory_resource*>(
  46. reinterpret_cast<std::uintptr_t*>(
  47. &instance_));
  48. }
  49. ~default_resource();
  50. void*
  51. do_allocate(
  52. std::size_t n,
  53. std::size_t) override;
  54. void
  55. do_deallocate(
  56. void* p,
  57. std::size_t,
  58. std::size_t) override;
  59. bool
  60. do_is_equal(
  61. memory_resource const& mr) const noexcept override;
  62. };
  63. #ifdef _MSC_VER
  64. #pragma warning(pop)
  65. #endif
  66. union default_resource::
  67. holder
  68. {
  69. #ifndef BOOST_JSON_WEAK_CONSTINIT
  70. constexpr
  71. #endif
  72. holder()
  73. : mr()
  74. {
  75. }
  76. ~holder()
  77. {
  78. }
  79. default_resource mr;
  80. };
  81. } // detail
  82. } // namespace json
  83. } // namespace boost
  84. #endif