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