xref: /llvm-project/libcxx/src/memory_resource.cpp (revision bfabd5be5359f482af462b587b761f7e07cc4075)
1243da90eSArthur O'Dwyer //===----------------------------------------------------------------------===//
2243da90eSArthur O'Dwyer //
3243da90eSArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4243da90eSArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5243da90eSArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6243da90eSArthur O'Dwyer //
7243da90eSArthur O'Dwyer //===----------------------------------------------------------------------===//
8243da90eSArthur O'Dwyer 
9e99c4906SNikolas Klauser #include <cstddef>
10243da90eSArthur O'Dwyer #include <memory>
11243da90eSArthur O'Dwyer #include <memory_resource>
12243da90eSArthur O'Dwyer 
13ba87515fSNikolas Klauser #if _LIBCPP_HAS_ATOMIC_HEADER
14243da90eSArthur O'Dwyer #  include <atomic>
15c6f3b7bcSNikolas Klauser #elif _LIBCPP_HAS_THREADS
16243da90eSArthur O'Dwyer #  include <mutex>
17243da90eSArthur O'Dwyer #  if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18243da90eSArthur O'Dwyer #    pragma comment(lib, "pthread")
19243da90eSArthur O'Dwyer #  endif
20243da90eSArthur O'Dwyer #endif
21243da90eSArthur O'Dwyer 
22243da90eSArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
23243da90eSArthur O'Dwyer 
24243da90eSArthur O'Dwyer namespace pmr {
25243da90eSArthur O'Dwyer 
26243da90eSArthur O'Dwyer // memory_resource
27243da90eSArthur O'Dwyer 
2857215edaSNikolas Klauser memory_resource::~memory_resource() = default;
29243da90eSArthur O'Dwyer 
30243da90eSArthur O'Dwyer // new_delete_resource()
31243da90eSArthur O'Dwyer 
32ba87515fSNikolas Klauser #if !_LIBCPP_HAS_ALIGNED_ALLOCATION
33243da90eSArthur O'Dwyer static bool is_aligned_to(void* ptr, size_t align) {
34243da90eSArthur O'Dwyer   void* p2     = ptr;
35243da90eSArthur O'Dwyer   size_t space = 1;
36243da90eSArthur O'Dwyer   void* result = std::align(align, 1, p2, space);
37243da90eSArthur O'Dwyer   return (result == ptr);
38243da90eSArthur O'Dwyer }
39243da90eSArthur O'Dwyer #endif
40243da90eSArthur O'Dwyer 
41*bfabd5beSNikolas Klauser class _LIBCPP_HIDDEN __new_delete_memory_resource_imp : public memory_resource {
42243da90eSArthur O'Dwyer   void* do_allocate(size_t bytes, size_t align) override {
43ba87515fSNikolas Klauser #if _LIBCPP_HAS_ALIGNED_ALLOCATION
44cedb44afSLouis Dionne     return std::__libcpp_allocate<std::byte>(__element_count(bytes), align);
45243da90eSArthur O'Dwyer #else
46243da90eSArthur O'Dwyer     if (bytes == 0)
47243da90eSArthur O'Dwyer       bytes = 1;
48cedb44afSLouis Dionne     std::byte* result = std::__libcpp_allocate<std::byte>(__element_count(bytes), align);
49243da90eSArthur O'Dwyer     if (!is_aligned_to(result, align)) {
50cedb44afSLouis Dionne       std::__libcpp_deallocate<std::byte>(result, __element_count(bytes), align);
51243da90eSArthur O'Dwyer       __throw_bad_alloc();
52243da90eSArthur O'Dwyer     }
53243da90eSArthur O'Dwyer     return result;
54243da90eSArthur O'Dwyer #endif
55243da90eSArthur O'Dwyer   }
56243da90eSArthur O'Dwyer 
57cedb44afSLouis Dionne   void do_deallocate(void* p, size_t bytes, size_t align) override {
58cedb44afSLouis Dionne     std::__libcpp_deallocate<std::byte>(static_cast<std::byte*>(p), __element_count(bytes), align);
59cedb44afSLouis Dionne   }
60243da90eSArthur O'Dwyer 
61243da90eSArthur O'Dwyer   bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }
62243da90eSArthur O'Dwyer };
63243da90eSArthur O'Dwyer 
64243da90eSArthur O'Dwyer // null_memory_resource()
65243da90eSArthur O'Dwyer 
66*bfabd5beSNikolas Klauser class _LIBCPP_HIDDEN __null_memory_resource_imp : public memory_resource {
67243da90eSArthur O'Dwyer   void* do_allocate(size_t, size_t) override { __throw_bad_alloc(); }
68243da90eSArthur O'Dwyer   void do_deallocate(void*, size_t, size_t) override {}
69243da90eSArthur O'Dwyer   bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }
70243da90eSArthur O'Dwyer };
71243da90eSArthur O'Dwyer 
72243da90eSArthur O'Dwyer namespace {
73243da90eSArthur O'Dwyer 
74243da90eSArthur O'Dwyer union ResourceInitHelper {
75243da90eSArthur O'Dwyer   struct {
76243da90eSArthur O'Dwyer     __new_delete_memory_resource_imp new_delete_res;
77243da90eSArthur O'Dwyer     __null_memory_resource_imp null_res;
78243da90eSArthur O'Dwyer   } resources;
79243da90eSArthur O'Dwyer   char dummy;
805a6c1ce1SLouis Dionne   constexpr ResourceInitHelper() : resources() {}
81243da90eSArthur O'Dwyer   ~ResourceInitHelper() {}
82243da90eSArthur O'Dwyer };
83243da90eSArthur O'Dwyer 
84243da90eSArthur O'Dwyer // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
85243da90eSArthur O'Dwyer // attribute with a value that's reserved for the implementation (we're the implementation).
86243da90eSArthur O'Dwyer #include "memory_resource_init_helper.h"
87243da90eSArthur O'Dwyer 
88953af0e7SLouis Dionne } // namespace
89243da90eSArthur O'Dwyer 
90243da90eSArthur O'Dwyer memory_resource* new_delete_resource() noexcept { return &res_init.resources.new_delete_res; }
91243da90eSArthur O'Dwyer 
92243da90eSArthur O'Dwyer memory_resource* null_memory_resource() noexcept { return &res_init.resources.null_res; }
93243da90eSArthur O'Dwyer 
94243da90eSArthur O'Dwyer // default_memory_resource()
95243da90eSArthur O'Dwyer 
96243da90eSArthur O'Dwyer static memory_resource* __default_memory_resource(bool set = false, memory_resource* new_res = nullptr) noexcept {
97ba87515fSNikolas Klauser #if _LIBCPP_HAS_ATOMIC_HEADER
98243da90eSArthur O'Dwyer   static constinit atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
99243da90eSArthur O'Dwyer   if (set) {
100243da90eSArthur O'Dwyer     new_res = new_res ? new_res : new_delete_resource();
101243da90eSArthur O'Dwyer     // TODO: Can a weaker ordering be used?
102243da90eSArthur O'Dwyer     return std::atomic_exchange_explicit(&__res, new_res, memory_order_acq_rel);
103243da90eSArthur O'Dwyer   } else {
104243da90eSArthur O'Dwyer     return std::atomic_load_explicit(&__res, memory_order_acquire);
105243da90eSArthur O'Dwyer   }
106c6f3b7bcSNikolas Klauser #elif _LIBCPP_HAS_THREADS
107243da90eSArthur O'Dwyer   static constinit memory_resource* res = &res_init.resources.new_delete_res;
108243da90eSArthur O'Dwyer   static mutex res_lock;
109243da90eSArthur O'Dwyer   if (set) {
110243da90eSArthur O'Dwyer     new_res = new_res ? new_res : new_delete_resource();
111243da90eSArthur O'Dwyer     lock_guard<mutex> guard(res_lock);
112243da90eSArthur O'Dwyer     memory_resource* old_res = res;
113243da90eSArthur O'Dwyer     res                      = new_res;
114243da90eSArthur O'Dwyer     return old_res;
115243da90eSArthur O'Dwyer   } else {
116243da90eSArthur O'Dwyer     lock_guard<mutex> guard(res_lock);
117243da90eSArthur O'Dwyer     return res;
118243da90eSArthur O'Dwyer   }
119243da90eSArthur O'Dwyer #else
120243da90eSArthur O'Dwyer   static constinit memory_resource* res = &res_init.resources.new_delete_res;
121243da90eSArthur O'Dwyer   if (set) {
122243da90eSArthur O'Dwyer     new_res                  = new_res ? new_res : new_delete_resource();
123243da90eSArthur O'Dwyer     memory_resource* old_res = res;
124243da90eSArthur O'Dwyer     res                      = new_res;
125243da90eSArthur O'Dwyer     return old_res;
126243da90eSArthur O'Dwyer   } else {
127243da90eSArthur O'Dwyer     return res;
128243da90eSArthur O'Dwyer   }
129243da90eSArthur O'Dwyer #endif
130243da90eSArthur O'Dwyer }
131243da90eSArthur O'Dwyer 
132243da90eSArthur O'Dwyer memory_resource* get_default_resource() noexcept { return __default_memory_resource(); }
133243da90eSArthur O'Dwyer 
134243da90eSArthur O'Dwyer memory_resource* set_default_resource(memory_resource* __new_res) noexcept {
135243da90eSArthur O'Dwyer   return __default_memory_resource(true, __new_res);
136243da90eSArthur O'Dwyer }
137243da90eSArthur O'Dwyer 
138243da90eSArthur O'Dwyer // 23.12.5, mem.res.pool
139243da90eSArthur O'Dwyer 
140243da90eSArthur O'Dwyer static size_t roundup(size_t count, size_t alignment) {
141243da90eSArthur O'Dwyer   size_t mask = alignment - 1;
142243da90eSArthur O'Dwyer   return (count + mask) & ~mask;
143243da90eSArthur O'Dwyer }
144243da90eSArthur O'Dwyer 
145243da90eSArthur O'Dwyer struct unsynchronized_pool_resource::__adhoc_pool::__chunk_footer {
146243da90eSArthur O'Dwyer   __chunk_footer* __next_;
147243da90eSArthur O'Dwyer   char* __start_;
148243da90eSArthur O'Dwyer   size_t __align_;
149243da90eSArthur O'Dwyer   size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
150243da90eSArthur O'Dwyer };
151243da90eSArthur O'Dwyer 
152243da90eSArthur O'Dwyer void unsynchronized_pool_resource::__adhoc_pool::__release_ptr(memory_resource* upstream) {
153243da90eSArthur O'Dwyer   while (__first_ != nullptr) {
154243da90eSArthur O'Dwyer     __chunk_footer* next = __first_->__next_;
155243da90eSArthur O'Dwyer     upstream->deallocate(__first_->__start_, __first_->__allocation_size(), __first_->__align_);
156243da90eSArthur O'Dwyer     __first_ = next;
157243da90eSArthur O'Dwyer   }
158243da90eSArthur O'Dwyer }
159243da90eSArthur O'Dwyer 
160243da90eSArthur O'Dwyer void* unsynchronized_pool_resource::__adhoc_pool::__do_allocate(memory_resource* upstream, size_t bytes, size_t align) {
161243da90eSArthur O'Dwyer   const size_t footer_size  = sizeof(__chunk_footer);
162243da90eSArthur O'Dwyer   const size_t footer_align = alignof(__chunk_footer);
163243da90eSArthur O'Dwyer 
164243da90eSArthur O'Dwyer   if (align < footer_align)
165243da90eSArthur O'Dwyer     align = footer_align;
166243da90eSArthur O'Dwyer 
167243da90eSArthur O'Dwyer   size_t aligned_capacity = roundup(bytes, footer_align) + footer_size;
168243da90eSArthur O'Dwyer 
169243da90eSArthur O'Dwyer   void* result = upstream->allocate(aligned_capacity, align);
170243da90eSArthur O'Dwyer 
171243da90eSArthur O'Dwyer   __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
172243da90eSArthur O'Dwyer   h->__next_        = __first_;
173243da90eSArthur O'Dwyer   h->__start_       = (char*)result;
174243da90eSArthur O'Dwyer   h->__align_       = align;
175243da90eSArthur O'Dwyer   __first_          = h;
176243da90eSArthur O'Dwyer   return result;
177243da90eSArthur O'Dwyer }
178243da90eSArthur O'Dwyer 
179243da90eSArthur O'Dwyer void unsynchronized_pool_resource::__adhoc_pool::__do_deallocate(
180243da90eSArthur O'Dwyer     memory_resource* upstream, void* p, size_t bytes, size_t align) {
181b85fdc4fSKonstantin Varlamov   _LIBCPP_ASSERT_NON_NULL(__first_ != nullptr, "deallocating a block that was not allocated with this allocator");
182243da90eSArthur O'Dwyer   if (__first_->__start_ == p) {
183243da90eSArthur O'Dwyer     __chunk_footer* next = __first_->__next_;
184243da90eSArthur O'Dwyer     upstream->deallocate(p, __first_->__allocation_size(), __first_->__align_);
185243da90eSArthur O'Dwyer     __first_ = next;
186243da90eSArthur O'Dwyer   } else {
187243da90eSArthur O'Dwyer     for (__chunk_footer* h = __first_; h->__next_ != nullptr; h = h->__next_) {
188243da90eSArthur O'Dwyer       if (h->__next_->__start_ == p) {
189243da90eSArthur O'Dwyer         __chunk_footer* next = h->__next_->__next_;
190243da90eSArthur O'Dwyer         upstream->deallocate(p, h->__next_->__allocation_size(), h->__next_->__align_);
191243da90eSArthur O'Dwyer         h->__next_ = next;
192243da90eSArthur O'Dwyer         return;
193243da90eSArthur O'Dwyer       }
194243da90eSArthur O'Dwyer     }
1956082478eSKonstantin Varlamov     // The request to deallocate memory ends up being a no-op, likely resulting in a memory leak.
1966082478eSKonstantin Varlamov     _LIBCPP_ASSERT_VALID_DEALLOCATION(false, "deallocating a block that was not allocated with this allocator");
197243da90eSArthur O'Dwyer   }
198243da90eSArthur O'Dwyer }
199243da90eSArthur O'Dwyer 
200243da90eSArthur O'Dwyer class unsynchronized_pool_resource::__fixed_pool {
201243da90eSArthur O'Dwyer   struct __chunk_footer {
202243da90eSArthur O'Dwyer     __chunk_footer* __next_;
203243da90eSArthur O'Dwyer     char* __start_;
204243da90eSArthur O'Dwyer     size_t __align_;
205243da90eSArthur O'Dwyer     size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
206243da90eSArthur O'Dwyer   };
207243da90eSArthur O'Dwyer 
208243da90eSArthur O'Dwyer   struct __vacancy_header {
209243da90eSArthur O'Dwyer     __vacancy_header* __next_vacancy_;
210243da90eSArthur O'Dwyer   };
211243da90eSArthur O'Dwyer 
212243da90eSArthur O'Dwyer   __chunk_footer* __first_chunk_     = nullptr;
213243da90eSArthur O'Dwyer   __vacancy_header* __first_vacancy_ = nullptr;
214243da90eSArthur O'Dwyer 
215243da90eSArthur O'Dwyer public:
216243da90eSArthur O'Dwyer   explicit __fixed_pool() = default;
217243da90eSArthur O'Dwyer 
218243da90eSArthur O'Dwyer   void __release_ptr(memory_resource* upstream) {
219243da90eSArthur O'Dwyer     __first_vacancy_ = nullptr;
220243da90eSArthur O'Dwyer     while (__first_chunk_ != nullptr) {
221243da90eSArthur O'Dwyer       __chunk_footer* next = __first_chunk_->__next_;
222243da90eSArthur O'Dwyer       upstream->deallocate(__first_chunk_->__start_, __first_chunk_->__allocation_size(), __first_chunk_->__align_);
223243da90eSArthur O'Dwyer       __first_chunk_ = next;
224243da90eSArthur O'Dwyer     }
225243da90eSArthur O'Dwyer   }
226243da90eSArthur O'Dwyer 
227243da90eSArthur O'Dwyer   void* __try_allocate_from_vacancies() {
228243da90eSArthur O'Dwyer     if (__first_vacancy_ != nullptr) {
229243da90eSArthur O'Dwyer       void* result     = __first_vacancy_;
230243da90eSArthur O'Dwyer       __first_vacancy_ = __first_vacancy_->__next_vacancy_;
231243da90eSArthur O'Dwyer       return result;
232243da90eSArthur O'Dwyer     }
233243da90eSArthur O'Dwyer     return nullptr;
234243da90eSArthur O'Dwyer   }
235243da90eSArthur O'Dwyer 
236243da90eSArthur O'Dwyer   void* __allocate_in_new_chunk(memory_resource* upstream, size_t block_size, size_t chunk_size) {
2374f215fddSKonstantin Varlamov     _LIBCPP_ASSERT_INTERNAL(chunk_size % block_size == 0, "");
238243da90eSArthur O'Dwyer     static_assert(__default_alignment >= alignof(std::max_align_t), "");
239243da90eSArthur O'Dwyer     static_assert(__default_alignment >= alignof(__chunk_footer), "");
240243da90eSArthur O'Dwyer     static_assert(__default_alignment >= alignof(__vacancy_header), "");
241243da90eSArthur O'Dwyer 
242243da90eSArthur O'Dwyer     const size_t footer_size  = sizeof(__chunk_footer);
243243da90eSArthur O'Dwyer     const size_t footer_align = alignof(__chunk_footer);
244243da90eSArthur O'Dwyer 
245243da90eSArthur O'Dwyer     size_t aligned_capacity = roundup(chunk_size, footer_align) + footer_size;
246243da90eSArthur O'Dwyer 
247243da90eSArthur O'Dwyer     void* result = upstream->allocate(aligned_capacity, __default_alignment);
248243da90eSArthur O'Dwyer 
249243da90eSArthur O'Dwyer     __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
250243da90eSArthur O'Dwyer     h->__next_        = __first_chunk_;
251243da90eSArthur O'Dwyer     h->__start_       = (char*)result;
252243da90eSArthur O'Dwyer     h->__align_       = __default_alignment;
253243da90eSArthur O'Dwyer     __first_chunk_    = h;
254243da90eSArthur O'Dwyer 
255243da90eSArthur O'Dwyer     if (chunk_size > block_size) {
256243da90eSArthur O'Dwyer       __vacancy_header* last_vh = this->__first_vacancy_;
257243da90eSArthur O'Dwyer       for (size_t i = block_size; i != chunk_size; i += block_size) {
258243da90eSArthur O'Dwyer         __vacancy_header* vh = (__vacancy_header*)((char*)result + i);
259243da90eSArthur O'Dwyer         vh->__next_vacancy_  = last_vh;
260243da90eSArthur O'Dwyer         last_vh              = vh;
261243da90eSArthur O'Dwyer       }
262243da90eSArthur O'Dwyer       this->__first_vacancy_ = last_vh;
263243da90eSArthur O'Dwyer     }
264243da90eSArthur O'Dwyer     return result;
265243da90eSArthur O'Dwyer   }
266243da90eSArthur O'Dwyer 
267243da90eSArthur O'Dwyer   void __evacuate(void* p) {
268243da90eSArthur O'Dwyer     __vacancy_header* vh = (__vacancy_header*)(p);
269243da90eSArthur O'Dwyer     vh->__next_vacancy_  = __first_vacancy_;
270243da90eSArthur O'Dwyer     __first_vacancy_     = vh;
271243da90eSArthur O'Dwyer   }
272243da90eSArthur O'Dwyer 
273243da90eSArthur O'Dwyer   size_t __previous_chunk_size_in_bytes() const { return __first_chunk_ ? __first_chunk_->__allocation_size() : 0; }
274243da90eSArthur O'Dwyer 
275243da90eSArthur O'Dwyer   static const size_t __default_alignment = alignof(max_align_t);
276243da90eSArthur O'Dwyer };
277243da90eSArthur O'Dwyer 
278243da90eSArthur O'Dwyer size_t unsynchronized_pool_resource::__pool_block_size(int i) const { return size_t(1) << __log2_pool_block_size(i); }
279243da90eSArthur O'Dwyer 
280243da90eSArthur O'Dwyer int unsynchronized_pool_resource::__log2_pool_block_size(int i) const { return (i + __log2_smallest_block_size); }
281243da90eSArthur O'Dwyer 
282243da90eSArthur O'Dwyer int unsynchronized_pool_resource::__pool_index(size_t bytes, size_t align) const {
283243da90eSArthur O'Dwyer   if (align > alignof(std::max_align_t) || bytes > (size_t(1) << __num_fixed_pools_))
284243da90eSArthur O'Dwyer     return __num_fixed_pools_;
285243da90eSArthur O'Dwyer   else {
286243da90eSArthur O'Dwyer     int i = 0;
287243da90eSArthur O'Dwyer     bytes = (bytes > align) ? bytes : align;
288243da90eSArthur O'Dwyer     bytes -= 1;
289243da90eSArthur O'Dwyer     bytes >>= __log2_smallest_block_size;
290243da90eSArthur O'Dwyer     while (bytes != 0) {
291243da90eSArthur O'Dwyer       bytes >>= 1;
292243da90eSArthur O'Dwyer       i += 1;
293243da90eSArthur O'Dwyer     }
294243da90eSArthur O'Dwyer     return i;
295243da90eSArthur O'Dwyer   }
296243da90eSArthur O'Dwyer }
297243da90eSArthur O'Dwyer 
298243da90eSArthur O'Dwyer unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream)
299243da90eSArthur O'Dwyer     : __res_(upstream), __fixed_pools_(nullptr) {
300243da90eSArthur O'Dwyer   size_t largest_block_size;
301243da90eSArthur O'Dwyer   if (opts.largest_required_pool_block == 0)
302243da90eSArthur O'Dwyer     largest_block_size = __default_largest_block_size;
303243da90eSArthur O'Dwyer   else if (opts.largest_required_pool_block < __smallest_block_size)
304243da90eSArthur O'Dwyer     largest_block_size = __smallest_block_size;
305243da90eSArthur O'Dwyer   else if (opts.largest_required_pool_block > __max_largest_block_size)
306243da90eSArthur O'Dwyer     largest_block_size = __max_largest_block_size;
307243da90eSArthur O'Dwyer   else
308243da90eSArthur O'Dwyer     largest_block_size = opts.largest_required_pool_block;
309243da90eSArthur O'Dwyer 
310243da90eSArthur O'Dwyer   if (opts.max_blocks_per_chunk == 0)
311243da90eSArthur O'Dwyer     __options_max_blocks_per_chunk_ = __max_blocks_per_chunk;
312243da90eSArthur O'Dwyer   else if (opts.max_blocks_per_chunk < __min_blocks_per_chunk)
313243da90eSArthur O'Dwyer     __options_max_blocks_per_chunk_ = __min_blocks_per_chunk;
314243da90eSArthur O'Dwyer   else if (opts.max_blocks_per_chunk > __max_blocks_per_chunk)
315243da90eSArthur O'Dwyer     __options_max_blocks_per_chunk_ = __max_blocks_per_chunk;
316243da90eSArthur O'Dwyer   else
317243da90eSArthur O'Dwyer     __options_max_blocks_per_chunk_ = opts.max_blocks_per_chunk;
318243da90eSArthur O'Dwyer 
319243da90eSArthur O'Dwyer   __num_fixed_pools_ = 1;
320243da90eSArthur O'Dwyer   size_t capacity    = __smallest_block_size;
321243da90eSArthur O'Dwyer   while (capacity < largest_block_size) {
322243da90eSArthur O'Dwyer     capacity <<= 1;
323243da90eSArthur O'Dwyer     __num_fixed_pools_ += 1;
324243da90eSArthur O'Dwyer   }
325243da90eSArthur O'Dwyer }
326243da90eSArthur O'Dwyer 
327243da90eSArthur O'Dwyer pool_options unsynchronized_pool_resource::options() const {
328243da90eSArthur O'Dwyer   pool_options p;
329243da90eSArthur O'Dwyer   p.max_blocks_per_chunk        = __options_max_blocks_per_chunk_;
330243da90eSArthur O'Dwyer   p.largest_required_pool_block = __pool_block_size(__num_fixed_pools_ - 1);
331243da90eSArthur O'Dwyer   return p;
332243da90eSArthur O'Dwyer }
333243da90eSArthur O'Dwyer 
334243da90eSArthur O'Dwyer void unsynchronized_pool_resource::release() {
335243da90eSArthur O'Dwyer   __adhoc_pool_.__release_ptr(__res_);
336243da90eSArthur O'Dwyer   if (__fixed_pools_ != nullptr) {
337243da90eSArthur O'Dwyer     const int n = __num_fixed_pools_;
338243da90eSArthur O'Dwyer     for (int i = 0; i < n; ++i)
339243da90eSArthur O'Dwyer       __fixed_pools_[i].__release_ptr(__res_);
340243da90eSArthur O'Dwyer     __res_->deallocate(__fixed_pools_, __num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
341243da90eSArthur O'Dwyer     __fixed_pools_ = nullptr;
342243da90eSArthur O'Dwyer   }
343243da90eSArthur O'Dwyer }
344243da90eSArthur O'Dwyer 
345243da90eSArthur O'Dwyer void* unsynchronized_pool_resource::do_allocate(size_t bytes, size_t align) {
346243da90eSArthur O'Dwyer   // A pointer to allocated storage (6.6.4.4.1) with a size of at least bytes.
347243da90eSArthur O'Dwyer   // The size and alignment of the allocated memory shall meet the requirements for
348243da90eSArthur O'Dwyer   // a class derived from memory_resource (23.12).
349243da90eSArthur O'Dwyer   // If the pool selected for a block of size bytes is unable to satisfy the memory request
350243da90eSArthur O'Dwyer   // from its own internal data structures, it will call upstream_resource()->allocate()
351243da90eSArthur O'Dwyer   // to obtain more memory. If bytes is larger than that which the largest pool can handle,
352243da90eSArthur O'Dwyer   // then memory will be allocated using upstream_resource()->allocate().
353243da90eSArthur O'Dwyer 
354243da90eSArthur O'Dwyer   int i = __pool_index(bytes, align);
355243da90eSArthur O'Dwyer   if (i == __num_fixed_pools_)
356243da90eSArthur O'Dwyer     return __adhoc_pool_.__do_allocate(__res_, bytes, align);
357243da90eSArthur O'Dwyer   else {
358243da90eSArthur O'Dwyer     if (__fixed_pools_ == nullptr) {
359243da90eSArthur O'Dwyer       __fixed_pools_ =
360243da90eSArthur O'Dwyer           (__fixed_pool*)__res_->allocate(__num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
361243da90eSArthur O'Dwyer       __fixed_pool* first = __fixed_pools_;
362243da90eSArthur O'Dwyer       __fixed_pool* last  = __fixed_pools_ + __num_fixed_pools_;
363243da90eSArthur O'Dwyer       for (__fixed_pool* pool = first; pool != last; ++pool)
364243da90eSArthur O'Dwyer         ::new ((void*)pool) __fixed_pool;
365243da90eSArthur O'Dwyer     }
366243da90eSArthur O'Dwyer     void* result = __fixed_pools_[i].__try_allocate_from_vacancies();
367243da90eSArthur O'Dwyer     if (result == nullptr) {
368243da90eSArthur O'Dwyer       auto min = [](size_t a, size_t b) { return a < b ? a : b; };
369243da90eSArthur O'Dwyer       auto max = [](size_t a, size_t b) { return a < b ? b : a; };
370243da90eSArthur O'Dwyer 
371243da90eSArthur O'Dwyer       size_t prev_chunk_size_in_bytes  = __fixed_pools_[i].__previous_chunk_size_in_bytes();
372243da90eSArthur O'Dwyer       size_t prev_chunk_size_in_blocks = prev_chunk_size_in_bytes >> __log2_pool_block_size(i);
373243da90eSArthur O'Dwyer 
374243da90eSArthur O'Dwyer       size_t chunk_size_in_blocks;
375243da90eSArthur O'Dwyer 
376243da90eSArthur O'Dwyer       if (prev_chunk_size_in_blocks == 0) {
377243da90eSArthur O'Dwyer         size_t min_blocks_per_chunk = max(__min_bytes_per_chunk >> __log2_pool_block_size(i), __min_blocks_per_chunk);
378243da90eSArthur O'Dwyer         chunk_size_in_blocks        = min_blocks_per_chunk;
379243da90eSArthur O'Dwyer       } else {
380243da90eSArthur O'Dwyer         static_assert(__max_bytes_per_chunk <= SIZE_MAX - (__max_bytes_per_chunk / 4), "unsigned overflow is possible");
381243da90eSArthur O'Dwyer         chunk_size_in_blocks = prev_chunk_size_in_blocks + (prev_chunk_size_in_blocks / 4);
382243da90eSArthur O'Dwyer       }
383243da90eSArthur O'Dwyer 
384243da90eSArthur O'Dwyer       size_t max_blocks_per_chunk =
385243da90eSArthur O'Dwyer           min((__max_bytes_per_chunk >> __log2_pool_block_size(i)),
386243da90eSArthur O'Dwyer               min(__max_blocks_per_chunk, __options_max_blocks_per_chunk_));
387243da90eSArthur O'Dwyer       if (chunk_size_in_blocks > max_blocks_per_chunk)
388243da90eSArthur O'Dwyer         chunk_size_in_blocks = max_blocks_per_chunk;
389243da90eSArthur O'Dwyer 
390243da90eSArthur O'Dwyer       size_t block_size = __pool_block_size(i);
391243da90eSArthur O'Dwyer 
392243da90eSArthur O'Dwyer       size_t chunk_size_in_bytes = (chunk_size_in_blocks << __log2_pool_block_size(i));
393243da90eSArthur O'Dwyer       result                     = __fixed_pools_[i].__allocate_in_new_chunk(__res_, block_size, chunk_size_in_bytes);
394243da90eSArthur O'Dwyer     }
395243da90eSArthur O'Dwyer     return result;
396243da90eSArthur O'Dwyer   }
397243da90eSArthur O'Dwyer }
398243da90eSArthur O'Dwyer 
399243da90eSArthur O'Dwyer void unsynchronized_pool_resource::do_deallocate(void* p, size_t bytes, size_t align) {
400243da90eSArthur O'Dwyer   // Returns the memory at p to the pool. It is unspecified if,
401243da90eSArthur O'Dwyer   // or under what circumstances, this operation will result in
402243da90eSArthur O'Dwyer   // a call to upstream_resource()->deallocate().
403243da90eSArthur O'Dwyer 
404243da90eSArthur O'Dwyer   int i = __pool_index(bytes, align);
405243da90eSArthur O'Dwyer   if (i == __num_fixed_pools_)
406243da90eSArthur O'Dwyer     return __adhoc_pool_.__do_deallocate(__res_, p, bytes, align);
407243da90eSArthur O'Dwyer   else {
408b85fdc4fSKonstantin Varlamov     _LIBCPP_ASSERT_NON_NULL(
409cd0ad421Svarconst         __fixed_pools_ != nullptr, "deallocating a block that was not allocated with this allocator");
410243da90eSArthur O'Dwyer     __fixed_pools_[i].__evacuate(p);
411243da90eSArthur O'Dwyer   }
412243da90eSArthur O'Dwyer }
413243da90eSArthur O'Dwyer 
414243da90eSArthur O'Dwyer bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const noexcept { return &other == this; }
415243da90eSArthur O'Dwyer 
416243da90eSArthur O'Dwyer // 23.12.6, mem.res.monotonic.buffer
417243da90eSArthur O'Dwyer 
4186285c46eSPeng Xie constexpr size_t __default_growth_factor = 2;
4196285c46eSPeng Xie 
420549a5fd0SNikolas Klauser static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
421549a5fd0SNikolas Klauser   if (size > space)
422549a5fd0SNikolas Klauser     return nullptr;
423549a5fd0SNikolas Klauser 
424549a5fd0SNikolas Klauser   char* p1      = static_cast<char*>(ptr);
425549a5fd0SNikolas Klauser   char* new_ptr = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 - size) & ~(align - 1));
426549a5fd0SNikolas Klauser 
427549a5fd0SNikolas Klauser   if (new_ptr < (p1 - space))
428549a5fd0SNikolas Klauser     return nullptr;
429549a5fd0SNikolas Klauser 
430549a5fd0SNikolas Klauser   ptr = new_ptr;
431549a5fd0SNikolas Klauser   space -= p1 - new_ptr;
432549a5fd0SNikolas Klauser 
433549a5fd0SNikolas Klauser   return ptr;
434549a5fd0SNikolas Klauser }
435549a5fd0SNikolas Klauser 
4366285c46eSPeng Xie template <bool is_initial, typename Chunk>
4376285c46eSPeng Xie void* __try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align) {
4386285c46eSPeng Xie   if constexpr (is_initial) {
4396285c46eSPeng Xie     // only for __initial_descriptor.
4406285c46eSPeng Xie     // if __initial_descriptor.__cur_ equals nullptr, means no available buffer given when ctor.
4416285c46eSPeng Xie     // here we just return nullptr, let the caller do the next handling.
4426285c46eSPeng Xie     if (!self.__cur_)
443243da90eSArthur O'Dwyer       return nullptr;
444243da90eSArthur O'Dwyer   }
4456285c46eSPeng Xie   void* new_ptr       = static_cast<void*>(self.__cur_);
4466285c46eSPeng Xie   size_t new_capacity = (self.__cur_ - self.__start_);
447549a5fd0SNikolas Klauser   void* aligned_ptr   = align_down(align, bytes, new_ptr, new_capacity);
448243da90eSArthur O'Dwyer   if (aligned_ptr != nullptr)
4496285c46eSPeng Xie     self.__cur_ = static_cast<char*>(new_ptr);
450243da90eSArthur O'Dwyer   return aligned_ptr;
451243da90eSArthur O'Dwyer }
452243da90eSArthur O'Dwyer 
453243da90eSArthur O'Dwyer void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
454243da90eSArthur O'Dwyer   const size_t footer_size  = sizeof(__chunk_footer);
455243da90eSArthur O'Dwyer   const size_t footer_align = alignof(__chunk_footer);
456243da90eSArthur O'Dwyer 
457243da90eSArthur O'Dwyer   auto previous_allocation_size = [&]() {
458243da90eSArthur O'Dwyer     if (__chunks_ != nullptr)
459243da90eSArthur O'Dwyer       return __chunks_->__allocation_size();
460243da90eSArthur O'Dwyer 
461243da90eSArthur O'Dwyer     size_t newsize = (__initial_.__start_ != nullptr) ? (__initial_.__end_ - __initial_.__start_) : __initial_.__size_;
462243da90eSArthur O'Dwyer 
463243da90eSArthur O'Dwyer     return roundup(newsize, footer_align) + footer_size;
464243da90eSArthur O'Dwyer   };
465243da90eSArthur O'Dwyer 
4666285c46eSPeng Xie   if (void* result = __try_allocate_from_chunk<true, __initial_descriptor>(__initial_, bytes, align))
467243da90eSArthur O'Dwyer     return result;
468243da90eSArthur O'Dwyer   if (__chunks_ != nullptr) {
4696285c46eSPeng Xie     if (void* result = __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align))
470243da90eSArthur O'Dwyer       return result;
471243da90eSArthur O'Dwyer   }
472243da90eSArthur O'Dwyer 
473243da90eSArthur O'Dwyer   // Allocate a brand-new chunk.
474243da90eSArthur O'Dwyer 
475243da90eSArthur O'Dwyer   if (align < footer_align)
476243da90eSArthur O'Dwyer     align = footer_align;
477243da90eSArthur O'Dwyer 
478243da90eSArthur O'Dwyer   size_t aligned_capacity  = roundup(bytes, footer_align) + footer_size;
479243da90eSArthur O'Dwyer   size_t previous_capacity = previous_allocation_size();
480243da90eSArthur O'Dwyer 
481243da90eSArthur O'Dwyer   if (aligned_capacity <= previous_capacity) {
4826285c46eSPeng Xie     size_t newsize   = __default_growth_factor * (previous_capacity - footer_size);
483243da90eSArthur O'Dwyer     aligned_capacity = roundup(newsize, footer_align) + footer_size;
484243da90eSArthur O'Dwyer   }
485243da90eSArthur O'Dwyer 
486243da90eSArthur O'Dwyer   char* start            = (char*)__res_->allocate(aligned_capacity, align);
487549a5fd0SNikolas Klauser   auto end               = start + aligned_capacity - footer_size;
488549a5fd0SNikolas Klauser   __chunk_footer* footer = (__chunk_footer*)(end);
489243da90eSArthur O'Dwyer   footer->__next_        = __chunks_;
490243da90eSArthur O'Dwyer   footer->__start_       = start;
491549a5fd0SNikolas Klauser   footer->__cur_         = end;
492243da90eSArthur O'Dwyer   footer->__align_       = align;
493243da90eSArthur O'Dwyer   __chunks_              = footer;
494243da90eSArthur O'Dwyer 
4956285c46eSPeng Xie   return __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align);
496243da90eSArthur O'Dwyer }
497243da90eSArthur O'Dwyer 
498243da90eSArthur O'Dwyer } // namespace pmr
499243da90eSArthur O'Dwyer 
500243da90eSArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
501