1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
10#define _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
11
12#include <__config>
13#include <__memory/addressof.h>
14#include <__memory_resource/memory_resource.h>
15#include <cstddef>
16
17#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18#  pragma GCC system_header
19#endif
20
21#if _LIBCPP_STD_VER > 14
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25namespace pmr {
26
27// [mem.res.monotonic.buffer]
28
29class _LIBCPP_TYPE_VIS monotonic_buffer_resource : public memory_resource {
30  static const size_t __default_buffer_capacity  = 1024;
31  static const size_t __default_buffer_alignment = 16;
32
33  struct __chunk_footer {
34    __chunk_footer* __next_;
35    char* __start_;
36    char* __cur_;
37    size_t __align_;
38    size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
39    void* __try_allocate_from_chunk(size_t, size_t);
40  };
41
42  struct __initial_descriptor {
43    char* __start_;
44    char* __cur_;
45    union {
46      char* __end_;
47      size_t __size_;
48    };
49    void* __try_allocate_from_chunk(size_t, size_t);
50  };
51
52public:
53  _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource()
54      : monotonic_buffer_resource(nullptr, __default_buffer_capacity, get_default_resource()) {}
55
56  _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(size_t __initial_size)
57      : monotonic_buffer_resource(nullptr, __initial_size, get_default_resource()) {}
58
59  _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(void* __buffer, size_t __buffer_size)
60      : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource()) {}
61
62  _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(memory_resource* __upstream)
63      : monotonic_buffer_resource(nullptr, __default_buffer_capacity, __upstream) {}
64
65  _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(size_t __initial_size, memory_resource* __upstream)
66      : monotonic_buffer_resource(nullptr, __initial_size, __upstream) {}
67
68  _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(void* __buffer, size_t __buffer_size, memory_resource* __upstream)
69      : __res_(__upstream) {
70    __initial_.__start_ = static_cast<char*>(__buffer);
71    if (__buffer != nullptr) {
72      __initial_.__cur_ = static_cast<char*>(__buffer) + __buffer_size;
73      __initial_.__end_ = static_cast<char*>(__buffer) + __buffer_size;
74    } else {
75      __initial_.__cur_  = nullptr;
76      __initial_.__size_ = __buffer_size;
77    }
78    __chunks_ = nullptr;
79  }
80
81  monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
82
83  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~monotonic_buffer_resource() override { release(); }
84
85  monotonic_buffer_resource& operator=(const monotonic_buffer_resource&) = delete;
86
87  _LIBCPP_HIDE_FROM_ABI void release() {
88    if (__initial_.__start_ != nullptr)
89      __initial_.__cur_ = __initial_.__end_;
90    while (__chunks_ != nullptr) {
91      __chunk_footer* __next = __chunks_->__next_;
92      __res_->deallocate(__chunks_->__start_, __chunks_->__allocation_size(), __chunks_->__align_);
93      __chunks_ = __next;
94    }
95  }
96
97  _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
98
99protected:
100  void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
101
102  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void*, size_t, size_t) override {}
103
104  _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
105    return this == std::addressof(__other);
106  }
107
108private:
109  __initial_descriptor __initial_;
110  __chunk_footer* __chunks_;
111  memory_resource* __res_;
112};
113
114} // namespace pmr
115
116_LIBCPP_END_NAMESPACE_STD
117
118#endif // _LIBCPP_STD_VER > 14
119
120#endif // _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
121