static_resource.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Copyright (c) 2020 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_STATIC_RESOURCE_HPP
  10. #define BOOST_JSON_STATIC_RESOURCE_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #include <boost/json/memory_resource.hpp>
  13. #include <cstddef>
  14. namespace boost {
  15. namespace json {
  16. #ifdef _MSC_VER
  17. #pragma warning(push)
  18. #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  19. #endif
  20. //----------------------------------------------------------
  21. /** A resource using a caller-owned buffer, with a trivial deallocate
  22. This memory resource is a special-purpose resource
  23. that releases allocated memory only when the resource
  24. is destroyed (or when @ref release is called).
  25. It has a trivial deallocate function; that is, the
  26. metafunction @ref is_deallocate_trivial returns `true`.
  27. \n
  28. The resource is constructed from a caller-owned buffer
  29. from which subsequent calls to allocate are apportioned.
  30. When a memory request cannot be satisfied from the
  31. free bytes remaining in the buffer, the allocation
  32. request fails with the exception `std::bad_alloc`.
  33. \n
  34. @par Example
  35. This parses a JSON text into a value which uses a local
  36. stack buffer, then prints the result.
  37. @code
  38. unsigned char buf[ 4000 ];
  39. static_resource mr( buf );
  40. // Parse the string, using our memory resource
  41. value const jv = parse( "[1,2,3]", &mr );
  42. // Print the JSON
  43. std::cout << jv;
  44. @endcode
  45. @par Thread Safety
  46. Members of the same instance may not be
  47. called concurrently.
  48. @see
  49. https://en.wikipedia.org/wiki/Region-based_memory_management
  50. */
  51. class BOOST_JSON_CLASS_DECL
  52. static_resource final
  53. : public memory_resource
  54. {
  55. void* p_;
  56. std::size_t n_;
  57. std::size_t size_;
  58. public:
  59. /// Copy constructor (deleted)
  60. static_resource(
  61. static_resource const&) = delete;
  62. /// Copy assignment (deleted)
  63. static_resource& operator=(
  64. static_resource const&) = delete;
  65. /** Destructor
  66. @par Complexity
  67. Constant.
  68. @par Exception Safety
  69. No-throw guarantee.
  70. */
  71. ~static_resource() noexcept;
  72. /** Constructor
  73. This constructs the resource to use the specified
  74. buffer for subsequent calls to allocate. When the
  75. buffer is exhausted, allocate will throw
  76. `std::bad_alloc`.
  77. @par Complexity
  78. Constant.
  79. @par Exception Safety
  80. No-throw guarantee.
  81. @param buffer The buffer to use.
  82. Ownership is not transferred; the caller is
  83. responsible for ensuring that the lifetime of
  84. the buffer extends until the resource is destroyed.
  85. @param size The number of valid bytes pointed
  86. to by `buffer`.
  87. */
  88. /** @{ */
  89. static_resource(
  90. unsigned char* buffer,
  91. std::size_t size) noexcept;
  92. #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
  93. static_resource(
  94. std::byte* buffer,
  95. std::size_t size) noexcept
  96. : static_resource(reinterpret_cast<
  97. unsigned char*>(buffer), size)
  98. {
  99. }
  100. #endif
  101. /** @} */
  102. /** Constructor
  103. This constructs the resource to use the specified
  104. buffer for subsequent calls to allocate. When the
  105. buffer is exhausted, allocate will throw
  106. `std::bad_alloc`.
  107. @par Complexity
  108. Constant.
  109. @par Exception Safety
  110. No-throw guarantee.
  111. @param buffer The buffer to use.
  112. Ownership is not transferred; the caller is
  113. responsible for ensuring that the lifetime of
  114. the buffer extends until the resource is destroyed.
  115. */
  116. /** @{ */
  117. template<std::size_t N>
  118. explicit
  119. static_resource(
  120. unsigned char(&buffer)[N]) noexcept
  121. : static_resource(&buffer[0], N)
  122. {
  123. }
  124. #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
  125. template<std::size_t N>
  126. explicit
  127. static_resource(
  128. std::byte(&buffer)[N]) noexcept
  129. : static_resource(&buffer[0], N)
  130. {
  131. }
  132. #endif
  133. /** @} */
  134. #ifndef BOOST_JSON_DOCS
  135. // Safety net for accidental buffer overflows
  136. template<std::size_t N>
  137. static_resource(
  138. unsigned char(&buffer)[N], std::size_t n) noexcept
  139. : static_resource(&buffer[0], n)
  140. {
  141. // If this goes off, check your parameters
  142. // closely, chances are you passed an array
  143. // thinking it was a pointer.
  144. BOOST_ASSERT(n <= N);
  145. }
  146. #ifdef __cpp_lib_byte
  147. // Safety net for accidental buffer overflows
  148. template<std::size_t N>
  149. static_resource(
  150. std::byte(&buffer)[N], std::size_t n) noexcept
  151. : static_resource(&buffer[0], n)
  152. {
  153. // If this goes off, check your parameters
  154. // closely, chances are you passed an array
  155. // thinking it was a pointer.
  156. BOOST_ASSERT(n <= N);
  157. }
  158. #endif
  159. #endif
  160. /** Release all allocated memory.
  161. This function resets the buffer provided upon
  162. construction so that all of the valid bytes are
  163. available for subsequent allocation.
  164. @par Complexity
  165. Constant
  166. @par Exception Safety
  167. No-throw guarantee.
  168. */
  169. void
  170. release() noexcept;
  171. protected:
  172. #ifndef BOOST_JSON_DOCS
  173. void*
  174. do_allocate(
  175. std::size_t n,
  176. std::size_t align) override;
  177. void
  178. do_deallocate(
  179. void* p,
  180. std::size_t n,
  181. std::size_t align) override;
  182. bool
  183. do_is_equal(
  184. memory_resource const& mr
  185. ) const noexcept override;
  186. #endif
  187. };
  188. #ifdef _MSC_VER
  189. #pragma warning(pop)
  190. #endif
  191. template<>
  192. struct is_deallocate_trivial<
  193. static_resource>
  194. {
  195. static constexpr bool value = true;
  196. };
  197. } // namespace json
  198. } // namespace boost
  199. #endif