[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libcxx / include / __memory_resource / monotonic_buffer_resource.h
blob0c83f1ebc8db43719b5cfef7a45219b9b56fa468
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
10 #define _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
12 #include <__availability>
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory_resource/memory_resource.h>
16 #include <cstddef>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
20 #endif
22 #if _LIBCPP_STD_VER >= 17
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 namespace pmr {
28 // [mem.res.monotonic.buffer]
30 class _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;
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);
42 void* __try_allocate_from_chunk(size_t, size_t);
45 struct __initial_descriptor {
46 char* __start_;
47 char* __cur_;
48 union {
49 char* __end_;
50 size_t __size_;
52 void* __try_allocate_from_chunk(size_t, size_t);
55 public:
56 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource()
57 : monotonic_buffer_resource(nullptr, __default_buffer_capacity, get_default_resource()) {}
59 _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(size_t __initial_size)
60 : monotonic_buffer_resource(nullptr, __initial_size, get_default_resource()) {}
62 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(void* __buffer, size_t __buffer_size)
63 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource()) {}
65 _LIBCPP_HIDE_FROM_ABI explicit monotonic_buffer_resource(memory_resource* __upstream)
66 : monotonic_buffer_resource(nullptr, __default_buffer_capacity, __upstream) {}
68 _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource(size_t __initial_size, memory_resource* __upstream)
69 : monotonic_buffer_resource(nullptr, __initial_size, __upstream) {}
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;
81 __chunks_ = nullptr;
84 monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
86 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~monotonic_buffer_resource() override { release(); }
88 monotonic_buffer_resource& operator=(const monotonic_buffer_resource&) = delete;
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;
100 _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
102 protected:
103 void* do_allocate(size_t __bytes, size_t __alignment) override; // key function
105 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void*, size_t, size_t) override {}
107 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
108 return this == std::addressof(__other);
111 private:
112 __initial_descriptor __initial_;
113 __chunk_footer* __chunks_;
114 memory_resource* __res_;
117 } // namespace pmr
119 _LIBCPP_END_NAMESPACE_STD
121 #endif // _LIBCPP_STD_VER >= 17
123 #endif // _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H