123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- #ifndef BOOST_JSON_STORAGE_PTR_HPP
- #define BOOST_JSON_STORAGE_PTR_HPP
- #include <boost/container/pmr/polymorphic_allocator.hpp>
- #include <boost/json/detail/config.hpp>
- #include <boost/json/memory_resource.hpp>
- #include <boost/json/detail/shared_resource.hpp>
- #include <boost/json/detail/default_resource.hpp>
- #include <boost/json/is_deallocate_trivial.hpp>
- #include <cstddef>
- #include <new>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace json {
- class storage_ptr
- {
- #ifndef BOOST_JSON_DOCS
-
- friend struct detail::shared_resource;
- #endif
- using shared_resource =
- detail::shared_resource;
- using default_resource =
- detail::default_resource;
- std::uintptr_t i_;
- shared_resource*
- get_shared() const noexcept
- {
- return static_cast<shared_resource*>(
- reinterpret_cast<container::pmr::memory_resource*>(
- i_ & ~3));
- }
- void
- addref() const noexcept
- {
- if(is_shared())
- get_shared()->refs.fetch_add(
- 1, std::memory_order_relaxed);
- }
- void
- release() const noexcept
- {
- if(is_shared())
- {
- auto const p = get_shared();
- if(p->refs.fetch_sub(1,
- std::memory_order_acq_rel) == 1)
- delete p;
- }
- }
- template<class T>
- storage_ptr(
- detail::shared_resource_impl<T>* p) noexcept
- : i_(reinterpret_cast<std::uintptr_t>(
- static_cast<container::pmr::memory_resource*>(p)) + 1 +
- (json::is_deallocate_trivial<T>::value ? 2 : 0))
- {
- BOOST_ASSERT(p);
- }
- public:
-
- ~storage_ptr() noexcept
- {
- release();
- }
-
- storage_ptr() noexcept
- : i_(0)
- {
- }
-
- template<class T
- #ifndef BOOST_JSON_DOCS
- , class = typename std::enable_if<
- std::is_convertible<T*,
- container::pmr::memory_resource*>::value>::type
- #endif
- >
- storage_ptr(T* r) noexcept
- : i_(reinterpret_cast<std::uintptr_t>(
- static_cast<container::pmr::memory_resource *>(r)) +
- (json::is_deallocate_trivial<T>::value ? 2 : 0))
- {
- BOOST_ASSERT(r);
- }
-
- template<class T>
- storage_ptr(
- container::pmr::polymorphic_allocator<T> const& alloc) noexcept
- : i_(reinterpret_cast<std::uintptr_t>(
- alloc.resource()))
- {
- }
-
- storage_ptr(
- storage_ptr&& other) noexcept
- : i_(detail::exchange(other.i_, 0))
- {
- }
-
- storage_ptr(
- storage_ptr const& other) noexcept
- : i_(other.i_)
- {
- addref();
- }
-
- storage_ptr&
- operator=(
- storage_ptr&& other) noexcept
- {
- release();
- i_ = detail::exchange(other.i_, 0);
- return *this;
- }
-
- storage_ptr&
- operator=(
- storage_ptr const& other) noexcept
- {
- other.addref();
- release();
- i_ = other.i_;
- return *this;
- }
-
- bool
- is_shared() const noexcept
- {
- return (i_ & 1) != 0;
- }
-
- bool
- is_deallocate_trivial() const noexcept
- {
- return (i_ & 2) != 0;
- }
-
- bool
- is_not_shared_and_deallocate_is_trivial() const noexcept
- {
- return (i_ & 3) == 2;
- }
-
- container::pmr::memory_resource*
- get() const noexcept
- {
- if(i_ != 0)
- return reinterpret_cast<
- container::pmr::memory_resource*>(i_ & ~3);
- return default_resource::get();
- }
-
- container::pmr::memory_resource*
- operator->() const noexcept
- {
- return get();
- }
-
- container::pmr::memory_resource&
- operator*() const noexcept
- {
- return *get();
- }
- template<class U, class... Args>
- friend
- storage_ptr
- make_shared_resource(Args&&... args);
- };
- #if defined(_MSC_VER)
- # pragma warning( push )
- # if !defined(__clang__) && _MSC_VER <= 1900
- # pragma warning( disable : 4702 )
- # endif
- #endif
- template<class U, class... Args>
- storage_ptr
- make_shared_resource(Args&&... args)
- {
-
-
- BOOST_STATIC_ASSERT(
- std::is_base_of<
- container::pmr::memory_resource, U>::value);
- return storage_ptr(new
- detail::shared_resource_impl<U>(
- std::forward<Args>(args)...));
- }
- #if defined(_MSC_VER)
- # pragma warning( pop )
- #endif
- inline
- bool
- operator==(
- storage_ptr const& lhs,
- storage_ptr const& rhs) noexcept
- {
- return lhs.get() == rhs.get();
- }
- inline
- bool
- operator!=(
- storage_ptr const& lhs,
- storage_ptr const& rhs) noexcept
- {
- return lhs.get() != rhs.get();
- }
- }
- }
- #endif
|