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