memory_resource.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <boost/container/pmr/memory_resource.hpp>
  13. #include <boost/container/pmr/polymorphic_allocator.hpp>
  14. namespace boost {
  15. namespace json {
  16. #ifdef BOOST_JSON_DOCS
  17. /** The type of memory resource used by the library.
  18. Alias for `boost::container::pmr::memory_resource`.
  19. */
  20. class memory_resource
  21. {
  22. };
  23. /** The type of polymorphic allocator used by the library.
  24. Alias template for `boost::container::pmr::polymorphic_allocator`.
  25. */
  26. template<class T>
  27. class polymorphic_allocator;
  28. // VFALCO Bug: doc toolchain won't make this a ref
  29. //using memory_resource = __see_below__;
  30. #else
  31. using memory_resource = boost::container::pmr::memory_resource;
  32. template<class T>
  33. using polymorphic_allocator =
  34. boost::container::pmr::polymorphic_allocator<T>;
  35. #endif
  36. /** Return true if a memory resource's deallocate function has no effect.
  37. This metafunction may be specialized to indicate to
  38. the library that calls to the `deallocate` function of
  39. a @ref memory_resource have no effect. The implementation
  40. will elide such calls when it is safe to do so. By default,
  41. the implementation assumes that all memory resources
  42. require a call to `deallocate` for each memory region
  43. obtained by calling `allocate`.
  44. @par Example
  45. This example specializes the metafuction for `my_resource`,
  46. to indicate that calls to deallocate have no effect:
  47. @code
  48. // Forward-declaration for a user-defined memory resource
  49. struct my_resource;
  50. // It is necessary to specialize the template from
  51. // inside the namespace in which it is declared:
  52. namespace boost {
  53. namespace json {
  54. template<>
  55. struct is_deallocate_trivial< my_resource >
  56. {
  57. static constexpr bool value = true;
  58. };
  59. } // namespace json
  60. } // namespace boost
  61. @endcode
  62. It is usually not necessary for users to check this trait.
  63. Instead, they can call @ref storage_ptr::is_deallocate_trivial
  64. to determine if the pointed-to memory resource has a trivial
  65. deallocate function.
  66. @see
  67. @ref memory_resource,
  68. @ref storage_ptr
  69. */
  70. template<class T>
  71. struct is_deallocate_trivial
  72. {
  73. /** A bool equal to true if calls to `T::do_deallocate` have no effect.
  74. The primary template sets `value` to false.
  75. */
  76. static constexpr bool value = false;
  77. };
  78. } // namespace json
  79. } // namespace boost
  80. #endif