123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #ifndef BOOST_JSON_MONOTONIC_RESOURCE_HPP
- #define BOOST_JSON_MONOTONIC_RESOURCE_HPP
- #include <boost/container/pmr/memory_resource.hpp>
- #include <boost/json/detail/config.hpp>
- #include <boost/json/memory_resource.hpp>
- #include <boost/json/storage_ptr.hpp>
- #include <cstddef>
- #include <utility>
- namespace boost {
- namespace json {
- #ifdef _MSC_VER
- #pragma warning(push)
- #pragma warning(disable: 4251)
- #pragma warning(disable: 4275)
- #endif
- class
- BOOST_JSON_DECL
- BOOST_SYMBOL_VISIBLE
- monotonic_resource final
- : public container::pmr::memory_resource
- {
- struct block;
- struct block_base
- {
- void* p;
- std::size_t avail;
- std::size_t size;
- block_base* next;
- };
- block_base buffer_;
- block_base* head_ = &buffer_;
- std::size_t next_size_ = 1024;
- storage_ptr upstream_;
- static constexpr std::size_t min_size_ = 1024;
- inline static constexpr std::size_t max_size();
- inline static std::size_t round_pow2(
- std::size_t n) noexcept;
- inline static std::size_t next_pow2(
- std::size_t n) noexcept;
- public:
-
- monotonic_resource(
- monotonic_resource const&) = delete;
-
- monotonic_resource& operator=(
- monotonic_resource const&) = delete;
-
- ~monotonic_resource();
-
- explicit
- monotonic_resource(
- std::size_t initial_size = 1024,
- storage_ptr upstream = {}) noexcept;
-
-
- monotonic_resource(
- unsigned char* buffer,
- std::size_t size,
- storage_ptr upstream = {}) noexcept;
- #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
- monotonic_resource(
- std::byte* buffer,
- std::size_t size,
- storage_ptr upstream) noexcept
- : monotonic_resource(reinterpret_cast<
- unsigned char*>(buffer), size,
- std::move(upstream))
- {
- }
- #endif
-
-
-
- template<std::size_t N>
- explicit
- monotonic_resource(
- unsigned char(&buffer)[N],
- storage_ptr upstream = {}) noexcept
- : monotonic_resource(&buffer[0],
- N, std::move(upstream))
- {
- }
- #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
- template<std::size_t N>
- explicit
- monotonic_resource(
- std::byte(&buffer)[N],
- storage_ptr upstream = {}) noexcept
- : monotonic_resource(&buffer[0],
- N, std::move(upstream))
- {
- }
- #endif
-
- #ifndef BOOST_JSON_DOCS
-
- template<std::size_t N>
- monotonic_resource(
- unsigned char(&buffer)[N],
- std::size_t n,
- storage_ptr upstream = {}) noexcept
- : monotonic_resource(&buffer[0],
- n, std::move(upstream))
- {
-
-
-
- BOOST_ASSERT(n <= N);
- }
- #ifdef __cpp_lib_byte
-
- template<std::size_t N>
- monotonic_resource(
- std::byte(&buffer)[N],
- std::size_t n,
- storage_ptr upstream = {}) noexcept
- : monotonic_resource(&buffer[0],
- n, std::move(upstream))
- {
-
-
-
- BOOST_ASSERT(n <= N);
- }
- #endif
- #endif
-
- void
- release() noexcept;
- protected:
- #ifndef BOOST_JSON_DOCS
- void*
- do_allocate(
- std::size_t n,
- std::size_t align) override;
- void
- do_deallocate(
- void* p,
- std::size_t n,
- std::size_t align) override;
- bool
- do_is_equal(
- memory_resource const& mr) const noexcept override;
- #endif
- };
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- template<>
- struct is_deallocate_trivial<
- monotonic_resource>
- {
- static constexpr bool value = true;
- };
- }
- }
- #endif
|