Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:36:26

0001 //
0002 // Copyright (c) 2020 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/json
0008 //
0009 
0010 #ifndef BOOST_JSON_STATIC_RESOURCE_HPP
0011 #define BOOST_JSON_STATIC_RESOURCE_HPP
0012 
0013 #include <boost/container/pmr/memory_resource.hpp>
0014 #include <boost/json/detail/config.hpp>
0015 #include <boost/json/is_deallocate_trivial.hpp>
0016 #include <boost/json/memory_resource.hpp>
0017 #include <cstddef>
0018 
0019 namespace boost {
0020 namespace json {
0021 
0022 #ifdef _MSC_VER
0023 #pragma warning(push)
0024 #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
0025 #endif
0026 
0027 //----------------------------------------------------------
0028 
0029 /** A resource using a caller-owned buffer, with a trivial deallocate
0030 
0031     This memory resource is a special-purpose resource
0032     that releases allocated memory only when the resource
0033     is destroyed (or when @ref release is called).
0034     It has a trivial deallocate function; that is, the
0035     metafunction @ref is_deallocate_trivial returns `true`.
0036 \n
0037     The resource is constructed from a caller-owned buffer
0038     from which subsequent calls to allocate are apportioned.
0039     When a memory request cannot be satisfied from the
0040     free bytes remaining in the buffer, the allocation
0041     request fails with the exception `std::bad_alloc`.
0042 \n
0043     @par Example
0044 
0045     This parses a JSON text into a value which uses a local
0046     stack buffer, then prints the result.
0047 
0048     @code
0049 
0050     unsigned char buf[ 4000 ];
0051     static_resource mr( buf );
0052 
0053     // Parse the string, using our memory resource
0054     value const jv = parse( "[1,2,3]", &mr );
0055 
0056     // Print the JSON
0057     std::cout << jv;
0058 
0059     @endcode
0060 
0061     @par Thread Safety
0062     Members of the same instance may not be
0063     called concurrently.
0064 
0065     @see
0066         https://en.wikipedia.org/wiki/Region-based_memory_management
0067 */
0068 class
0069     BOOST_JSON_DECL
0070     BOOST_SYMBOL_VISIBLE
0071 static_resource final
0072     : public container::pmr::memory_resource
0073 {
0074     void* p_;
0075     std::size_t n_;
0076     std::size_t size_;
0077 
0078 public:
0079     /// Copy constructor (deleted)
0080     static_resource(
0081         static_resource const&) = delete;
0082 
0083     /// Copy assignment (deleted)
0084     static_resource& operator=(
0085         static_resource const&) = delete;
0086 
0087     /** Constructor
0088 
0089         This constructs the resource to use the specified
0090         buffer for subsequent calls to allocate. When the
0091         buffer is exhausted, allocate will throw
0092         `std::bad_alloc`.
0093 
0094         @par Complexity
0095         Constant.
0096 
0097         @par Exception Safety
0098         No-throw guarantee.
0099 
0100         @param buffer The buffer to use.
0101         Ownership is not transferred; the caller is
0102         responsible for ensuring that the lifetime of
0103         the buffer extends until the resource is destroyed.
0104 
0105         @param size The number of valid bytes pointed
0106         to by `buffer`.
0107     */
0108     /** @{ */
0109     static_resource(
0110         unsigned char* buffer,
0111         std::size_t size) noexcept;
0112 
0113 #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
0114     static_resource(
0115         std::byte* buffer,
0116         std::size_t size) noexcept
0117         : static_resource(reinterpret_cast<
0118             unsigned char*>(buffer), size)
0119     {
0120     }
0121 #endif
0122     /** @} */
0123 
0124     /** Constructor
0125 
0126         This constructs the resource to use the specified
0127         buffer for subsequent calls to allocate. When the
0128         buffer is exhausted, allocate will throw
0129         `std::bad_alloc`.
0130 
0131         @par Complexity
0132         Constant.
0133 
0134         @par Exception Safety
0135         No-throw guarantee.
0136 
0137         @param buffer The buffer to use.
0138         Ownership is not transferred; the caller is
0139         responsible for ensuring that the lifetime of
0140         the buffer extends until the resource is destroyed.
0141     */
0142     /** @{ */
0143     template<std::size_t N>
0144     explicit
0145     static_resource(
0146         unsigned char(&buffer)[N]) noexcept
0147         : static_resource(&buffer[0], N)
0148     {
0149     }
0150 
0151 #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
0152     template<std::size_t N>
0153     explicit
0154     static_resource(
0155         std::byte(&buffer)[N]) noexcept
0156         : static_resource(&buffer[0], N)
0157     {
0158     }
0159 #endif
0160     /** @} */
0161 
0162 #ifndef BOOST_JSON_DOCS
0163     // Safety net for accidental buffer overflows
0164     template<std::size_t N>
0165     static_resource(
0166         unsigned char(&buffer)[N], std::size_t n) noexcept
0167         : static_resource(&buffer[0], n)
0168     {
0169         // If this goes off, check your parameters
0170         // closely, chances are you passed an array
0171         // thinking it was a pointer.
0172         BOOST_ASSERT(n <= N);
0173     }
0174 
0175 #ifdef __cpp_lib_byte
0176     // Safety net for accidental buffer overflows
0177     template<std::size_t N>
0178     static_resource(
0179         std::byte(&buffer)[N], std::size_t n) noexcept
0180         : static_resource(&buffer[0], n)
0181     {
0182         // If this goes off, check your parameters
0183         // closely, chances are you passed an array
0184         // thinking it was a pointer.
0185         BOOST_ASSERT(n <= N);
0186     }
0187 #endif
0188 #endif
0189 
0190     /** Release all allocated memory.
0191 
0192         This function resets the buffer provided upon
0193         construction so that all of the valid bytes are
0194         available for subsequent allocation.
0195 
0196         @par Complexity
0197         Constant
0198 
0199         @par Exception Safety
0200         No-throw guarantee.
0201     */
0202     void
0203     release() noexcept;
0204 
0205 protected:
0206 #ifndef BOOST_JSON_DOCS
0207     void*
0208     do_allocate(
0209         std::size_t n,
0210         std::size_t align) override;
0211 
0212     void
0213     do_deallocate(
0214         void* p,
0215         std::size_t n,
0216         std::size_t align) override;
0217 
0218     bool
0219     do_is_equal(
0220         memory_resource const& mr
0221             ) const noexcept override;
0222 #endif
0223 };
0224 
0225 #ifdef _MSC_VER
0226 #pragma warning(pop)
0227 #endif
0228 
0229 template<>
0230 struct is_deallocate_trivial<
0231     static_resource>
0232 {
0233     static constexpr bool value = true;
0234 };
0235 
0236 } // namespace json
0237 } // namespace boost
0238 
0239 #endif