File indexing completed on 2026-05-03 08:13:36
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
0010 #define _LIBCPP___CXX03___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
0011
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__memory/addressof.h>
0014 #include <__cxx03/__memory_resource/memory_resource.h>
0015 #include <__cxx03/cstddef>
0016
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 # pragma GCC system_header
0019 #endif
0020
0021 #if _LIBCPP_STD_VER >= 17
0022
0023 _LIBCPP_BEGIN_NAMESPACE_STD
0024
0025 namespace pmr {
0026
0027
0028
0029 class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resource : public memory_resource {
0030 static const size_t __default_buffer_capacity = 1024;
0031 static const size_t __default_buffer_alignment = 16;
0032
0033 struct __chunk_footer {
0034 __chunk_footer* __next_;
0035 char* __start_;
0036 char* __cur_;
0037 size_t __align_;
0038 _LIBCPP_HIDE_FROM_ABI size_t __allocation_size() {
0039 return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this);
0040 }
0041 void* __try_allocate_from_chunk(size_t, size_t);
0042 };
0043
0044 struct __initial_descriptor {
0045 char* __start_;
0046 char* __cur_;
0047 union {
0048 char* __end_;
0049 size_t __size_;
0050 };
0051 void* __try_allocate_from_chunk(size_t, size_t);
0052 };
0053
0054 public:
0055 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource()
0056 : monotonic_buffer_resource(nullptr, __default_buffer_capacity, get_default_resource()) {}
0057
0058 _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(size_t __initial_size)
0059 : monotonic_buffer_resource(nullptr, __initial_size, get_default_resource()) {}
0060
0061 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(void* __buffer, size_t __buffer_size)
0062 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource()) {}
0063
0064 _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(memory_resource* __upstream)
0065 : monotonic_buffer_resource(nullptr, __default_buffer_capacity, __upstream) {}
0066
0067 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(size_t __initial_size, memory_resource* __upstream)
0068 : monotonic_buffer_resource(nullptr, __initial_size, __upstream) {}
0069
0070 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(void* __buffer, size_t __buffer_size, memory_resource* __upstream)
0071 : __res_(__upstream) {
0072 __initial_.__start_ = static_cast<char*>(__buffer);
0073 if (__buffer != nullptr) {
0074 __initial_.__cur_ = static_cast<char*>(__buffer) + __buffer_size;
0075 __initial_.__end_ = static_cast<char*>(__buffer) + __buffer_size;
0076 } else {
0077 __initial_.__cur_ = nullptr;
0078 __initial_.__size_ = __buffer_size;
0079 }
0080 __chunks_ = nullptr;
0081 }
0082
0083 monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
0084
0085 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~monotonic_buffer_resource() override { release(); }
0086
0087 monotonic_buffer_resource& operator=(const monotonic_buffer_resource&) = delete;
0088
0089 _LIBCPP_HIDE_FROM_ABI void release() {
0090 if (__initial_.__start_ != nullptr)
0091 __initial_.__cur_ = __initial_.__end_;
0092 while (__chunks_ != nullptr) {
0093 __chunk_footer* __next = __chunks_->__next_;
0094 __res_->deallocate(__chunks_->__start_, __chunks_->__allocation_size(), __chunks_->__align_);
0095 __chunks_ = __next;
0096 }
0097 }
0098
0099 _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
0100
0101 protected:
0102 void* do_allocate(size_t __bytes, size_t __alignment) override;
0103
0104 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void*, size_t, size_t) override {}
0105
0106 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
0107 return this == std::addressof(__other);
0108 }
0109
0110 private:
0111 __initial_descriptor __initial_;
0112 __chunk_footer* __chunks_;
0113 memory_resource* __res_;
0114 };
0115
0116 }
0117
0118 _LIBCPP_END_NAMESPACE_STD
0119
0120 #endif
0121
0122 #endif