memory_resource.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_MEMORY_RESOURCE_HPP
  10. #define BOOST_JSON_MEMORY_RESOURCE_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #ifdef BOOST_JSON_STANDALONE
  13. # if __has_include(<memory_resource>)
  14. # include <memory_resource>
  15. # ifndef __cpp_lib_memory_resource
  16. # error Support for std::memory_resource is required to use Boost.JSON standalone
  17. # endif
  18. # elif __has_include(<experimental/memory_resource>)
  19. # include <experimental/memory_resource>
  20. # warning Support for std::memory_resource is required to use Boost.JSON standalone, using std::experimental::memory_resource as fallback
  21. # else
  22. # error Header <memory_resource> is required to use Boost.JSON standalone
  23. # endif
  24. #else
  25. # include <boost/container/pmr/memory_resource.hpp>
  26. # include <boost/container/pmr/polymorphic_allocator.hpp>
  27. #endif
  28. BOOST_JSON_NS_BEGIN
  29. #ifdef BOOST_JSON_DOCS
  30. /** The type of memory resource used by the library.
  31. This type alias is set depending
  32. on how the library is configured:
  33. @par Use with Boost
  34. If the macro `BOOST_JSON_STANDALONE` is
  35. not defined, this type will be an alias
  36. for `boost::container::pmr::memory_resource`.
  37. Compiling a program using the library will
  38. require Boost, and a compiler conforming
  39. to C++11 or later.
  40. @par Use without Boost
  41. If the macro `BOOST_JSON_STANDALONE` is
  42. defined, this type will be an alias
  43. for `std::pmr::memory_resource`.
  44. Compiling a program using the library will
  45. require only a compiler conforming to C++17
  46. or later.
  47. @see https://en.cppreference.com/w/cpp/memory/memory_resource
  48. */
  49. class memory_resource
  50. {
  51. };
  52. /** The type of polymorphic allocator used by the library.
  53. This type alias is set depending
  54. on how the library is configured:
  55. @par Use with Boost
  56. If the macro `BOOST_JSON_STANDALONE` is
  57. not defined, this type will be an alias template
  58. for `boost::container::pmr::polymorphic_allocator`.
  59. Compiling a program using the library will
  60. require Boost, and a compiler conforming
  61. to C++11 or later.
  62. @par Use without Boost
  63. If the macro `BOOST_JSON_STANDALONE` is
  64. defined, this type will be an alias template
  65. for `std::pmr::polymorphic_allocator`.
  66. Compiling a program using the library will
  67. require only a compiler conforming to C++17
  68. or later.
  69. @see https://en.cppreference.com/w/cpp/memory/polymorphic_allocator
  70. */
  71. template<class T>
  72. class polymorphic_allocator;
  73. // VFALCO Bug: doc toolchain won't make this a ref
  74. //using memory_resource = __see_below__;
  75. #elif defined(BOOST_JSON_STANDALONE)
  76. # if __has_include(<memory_resource>)
  77. using memory_resource = std::pmr::memory_resource;
  78. template<class T>
  79. using polymorphic_allocator =
  80. std::pmr::polymorphic_allocator<T>;
  81. # else
  82. using memory_resource = std::experimental::pmr::memory_resource;
  83. template<class T>
  84. using polymorphic_allocator =
  85. std::experimental::pmr::polymorphic_allocator<T>;
  86. # endif
  87. #else
  88. using memory_resource = boost::container::pmr::memory_resource;
  89. template<class T>
  90. using polymorphic_allocator =
  91. boost::container::pmr::polymorphic_allocator<T>;
  92. #endif
  93. /** Return true if a memory resource's deallocate function has no effect.
  94. This metafunction may be specialized to indicate to
  95. the library that calls to the `deallocate` function of
  96. a @ref memory_resource have no effect. The implementation
  97. will elide such calls when it is safe to do so. By default,
  98. the implementation assumes that all memory resources
  99. require a call to `deallocate` for each memory region
  100. obtained by calling `allocate`.
  101. @par Example
  102. This example specializes the metafuction for `my_resource`,
  103. to indicate that calls to deallocate have no effect:
  104. @code
  105. // Forward-declaration for a user-defined memory resource
  106. struct my_resource;
  107. // It is necessary to specialize the template from
  108. // inside the namespace in which it is declared:
  109. namespace boost {
  110. namespace json {
  111. template<>
  112. struct is_deallocate_trivial< my_resource >
  113. {
  114. static constexpr bool value = true;
  115. };
  116. } // namespace json
  117. } // namespace boost
  118. @endcode
  119. It is usually not necessary for users to check this trait.
  120. Instead, they can call @ref storage_ptr::is_deallocate_trivial
  121. to determine if the pointed-to memory resource has a trivial
  122. deallocate function.
  123. @see
  124. @ref memory_resource,
  125. @ref storage_ptr
  126. */
  127. template<class T>
  128. struct is_deallocate_trivial
  129. {
  130. /** A bool equal to true if calls to `T::do_deallocate` have no effect.
  131. The primary template sets `value` to false.
  132. */
  133. static constexpr bool value = false;
  134. };
  135. BOOST_JSON_NS_END
  136. #endif