Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:14:59

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