default_resource.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  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. BOOST_JSON_NS_BEGIN
  14. namespace detail {
  15. #ifdef _MSC_VER
  16. #pragma warning(push)
  17. #pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
  18. #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  19. #endif
  20. // A simple memory resource that uses operator new and delete.
  21. class
  22. BOOST_SYMBOL_VISIBLE
  23. BOOST_JSON_CLASS_DECL
  24. default_resource final
  25. : public memory_resource
  26. {
  27. union holder;
  28. #ifndef BOOST_JSON_WEAK_CONSTINIT
  29. # ifndef BOOST_JSON_NO_DESTROY
  30. static holder instance_;
  31. # else
  32. BOOST_JSON_NO_DESTROY
  33. static default_resource instance_;
  34. # endif
  35. #endif
  36. public:
  37. static
  38. memory_resource*
  39. get() noexcept
  40. {
  41. #ifdef BOOST_JSON_WEAK_CONSTINIT
  42. static default_resource instance_;
  43. #endif
  44. return reinterpret_cast<memory_resource*>(
  45. reinterpret_cast<std::uintptr_t*>(
  46. &instance_));
  47. }
  48. ~default_resource();
  49. void*
  50. do_allocate(
  51. std::size_t n,
  52. std::size_t) override;
  53. void
  54. do_deallocate(
  55. void* p,
  56. std::size_t,
  57. std::size_t) override;
  58. bool
  59. do_is_equal(
  60. memory_resource const& mr) const noexcept override;
  61. };
  62. #ifdef _MSC_VER
  63. #pragma warning(pop)
  64. #endif
  65. union default_resource::
  66. holder
  67. {
  68. #ifndef BOOST_JSON_WEAK_CONSTINIT
  69. constexpr
  70. #endif
  71. holder()
  72. : mr()
  73. {
  74. }
  75. ~holder()
  76. {
  77. }
  78. default_resource mr;
  79. };
  80. } // detail
  81. BOOST_JSON_NS_END
  82. #endif