1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric 
9bdd1243dSDimitry Andric #include <memory>
10bdd1243dSDimitry Andric #include <memory_resource>
11bdd1243dSDimitry Andric 
12bdd1243dSDimitry Andric #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
13bdd1243dSDimitry Andric #  include <atomic>
14bdd1243dSDimitry Andric #elif !defined(_LIBCPP_HAS_NO_THREADS)
15bdd1243dSDimitry Andric #  include <mutex>
16bdd1243dSDimitry Andric #  if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
17bdd1243dSDimitry Andric #    pragma comment(lib, "pthread")
18bdd1243dSDimitry Andric #  endif
19bdd1243dSDimitry Andric #endif
20bdd1243dSDimitry Andric 
21bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
22bdd1243dSDimitry Andric 
23bdd1243dSDimitry Andric namespace pmr {
24bdd1243dSDimitry Andric 
25bdd1243dSDimitry Andric // memory_resource
26bdd1243dSDimitry Andric 
27bdd1243dSDimitry Andric memory_resource::~memory_resource() = default;
28bdd1243dSDimitry Andric 
29bdd1243dSDimitry Andric // new_delete_resource()
30bdd1243dSDimitry Andric 
31bdd1243dSDimitry Andric #ifdef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
is_aligned_to(void * ptr,size_t align)32bdd1243dSDimitry Andric static bool is_aligned_to(void* ptr, size_t align) {
33bdd1243dSDimitry Andric   void* p2     = ptr;
34bdd1243dSDimitry Andric   size_t space = 1;
35bdd1243dSDimitry Andric   void* result = std::align(align, 1, p2, space);
36bdd1243dSDimitry Andric   return (result == ptr);
37bdd1243dSDimitry Andric }
38bdd1243dSDimitry Andric #endif
39bdd1243dSDimitry Andric 
4006c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __new_delete_memory_resource_imp : public memory_resource {
do_allocate(size_t bytes,size_t align)41bdd1243dSDimitry Andric   void* do_allocate(size_t bytes, size_t align) override {
42bdd1243dSDimitry Andric #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
43bdd1243dSDimitry Andric     return std::__libcpp_allocate(bytes, align);
44bdd1243dSDimitry Andric #else
45bdd1243dSDimitry Andric     if (bytes == 0)
46bdd1243dSDimitry Andric       bytes = 1;
47bdd1243dSDimitry Andric     void* result = std::__libcpp_allocate(bytes, align);
48bdd1243dSDimitry Andric     if (!is_aligned_to(result, align)) {
49bdd1243dSDimitry Andric       std::__libcpp_deallocate(result, bytes, align);
50bdd1243dSDimitry Andric       __throw_bad_alloc();
51bdd1243dSDimitry Andric     }
52bdd1243dSDimitry Andric     return result;
53bdd1243dSDimitry Andric #endif
54bdd1243dSDimitry Andric   }
55bdd1243dSDimitry Andric 
do_deallocate(void * p,size_t bytes,size_t align)56bdd1243dSDimitry Andric   void do_deallocate(void* p, size_t bytes, size_t align) override { std::__libcpp_deallocate(p, bytes, align); }
57bdd1243dSDimitry Andric 
do_is_equal(const memory_resource & other) const58bdd1243dSDimitry Andric   bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }
59bdd1243dSDimitry Andric };
60bdd1243dSDimitry Andric 
61bdd1243dSDimitry Andric // null_memory_resource()
62bdd1243dSDimitry Andric 
6306c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __null_memory_resource_imp : public memory_resource {
do_allocate(size_t,size_t)64bdd1243dSDimitry Andric   void* do_allocate(size_t, size_t) override { __throw_bad_alloc(); }
do_deallocate(void *,size_t,size_t)65bdd1243dSDimitry Andric   void do_deallocate(void*, size_t, size_t) override {}
do_is_equal(const memory_resource & other) const66bdd1243dSDimitry Andric   bool do_is_equal(const memory_resource& other) const noexcept override { return &other == this; }
67bdd1243dSDimitry Andric };
68bdd1243dSDimitry Andric 
69bdd1243dSDimitry Andric namespace {
70bdd1243dSDimitry Andric 
71bdd1243dSDimitry Andric union ResourceInitHelper {
72bdd1243dSDimitry Andric   struct {
73bdd1243dSDimitry Andric     __new_delete_memory_resource_imp new_delete_res;
74bdd1243dSDimitry Andric     __null_memory_resource_imp null_res;
75bdd1243dSDimitry Andric   } resources;
76bdd1243dSDimitry Andric   char dummy;
ResourceInitHelper()775f757f3fSDimitry Andric   constexpr ResourceInitHelper() : resources() {}
~ResourceInitHelper()78bdd1243dSDimitry Andric   ~ResourceInitHelper() {}
79bdd1243dSDimitry Andric };
80bdd1243dSDimitry Andric 
81bdd1243dSDimitry Andric // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
82bdd1243dSDimitry Andric // attribute with a value that's reserved for the implementation (we're the implementation).
83bdd1243dSDimitry Andric #include "memory_resource_init_helper.h"
84bdd1243dSDimitry Andric 
85bdd1243dSDimitry Andric } // end namespace
86bdd1243dSDimitry Andric 
new_delete_resource()87bdd1243dSDimitry Andric memory_resource* new_delete_resource() noexcept { return &res_init.resources.new_delete_res; }
88bdd1243dSDimitry Andric 
null_memory_resource()89bdd1243dSDimitry Andric memory_resource* null_memory_resource() noexcept { return &res_init.resources.null_res; }
90bdd1243dSDimitry Andric 
91bdd1243dSDimitry Andric // default_memory_resource()
92bdd1243dSDimitry Andric 
__default_memory_resource(bool set=false,memory_resource * new_res=nullptr)93bdd1243dSDimitry Andric static memory_resource* __default_memory_resource(bool set = false, memory_resource* new_res = nullptr) noexcept {
94bdd1243dSDimitry Andric #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
95bdd1243dSDimitry Andric   static constinit atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
96bdd1243dSDimitry Andric   if (set) {
97bdd1243dSDimitry Andric     new_res = new_res ? new_res : new_delete_resource();
98bdd1243dSDimitry Andric     // TODO: Can a weaker ordering be used?
99bdd1243dSDimitry Andric     return std::atomic_exchange_explicit(&__res, new_res, memory_order_acq_rel);
100bdd1243dSDimitry Andric   } else {
101bdd1243dSDimitry Andric     return std::atomic_load_explicit(&__res, memory_order_acquire);
102bdd1243dSDimitry Andric   }
103bdd1243dSDimitry Andric #elif !defined(_LIBCPP_HAS_NO_THREADS)
104bdd1243dSDimitry Andric   static constinit memory_resource* res = &res_init.resources.new_delete_res;
105bdd1243dSDimitry Andric   static mutex res_lock;
106bdd1243dSDimitry Andric   if (set) {
107bdd1243dSDimitry Andric     new_res = new_res ? new_res : new_delete_resource();
108bdd1243dSDimitry Andric     lock_guard<mutex> guard(res_lock);
109bdd1243dSDimitry Andric     memory_resource* old_res = res;
110bdd1243dSDimitry Andric     res                      = new_res;
111bdd1243dSDimitry Andric     return old_res;
112bdd1243dSDimitry Andric   } else {
113bdd1243dSDimitry Andric     lock_guard<mutex> guard(res_lock);
114bdd1243dSDimitry Andric     return res;
115bdd1243dSDimitry Andric   }
116bdd1243dSDimitry Andric #else
117bdd1243dSDimitry Andric   static constinit memory_resource* res = &res_init.resources.new_delete_res;
118bdd1243dSDimitry Andric   if (set) {
119bdd1243dSDimitry Andric     new_res                  = new_res ? new_res : new_delete_resource();
120bdd1243dSDimitry Andric     memory_resource* old_res = res;
121bdd1243dSDimitry Andric     res                      = new_res;
122bdd1243dSDimitry Andric     return old_res;
123bdd1243dSDimitry Andric   } else {
124bdd1243dSDimitry Andric     return res;
125bdd1243dSDimitry Andric   }
126bdd1243dSDimitry Andric #endif
127bdd1243dSDimitry Andric }
128bdd1243dSDimitry Andric 
get_default_resource()129bdd1243dSDimitry Andric memory_resource* get_default_resource() noexcept { return __default_memory_resource(); }
130bdd1243dSDimitry Andric 
set_default_resource(memory_resource * __new_res)131bdd1243dSDimitry Andric memory_resource* set_default_resource(memory_resource* __new_res) noexcept {
132bdd1243dSDimitry Andric   return __default_memory_resource(true, __new_res);
133bdd1243dSDimitry Andric }
134bdd1243dSDimitry Andric 
135bdd1243dSDimitry Andric // 23.12.5, mem.res.pool
136bdd1243dSDimitry Andric 
roundup(size_t count,size_t alignment)137bdd1243dSDimitry Andric static size_t roundup(size_t count, size_t alignment) {
138bdd1243dSDimitry Andric   size_t mask = alignment - 1;
139bdd1243dSDimitry Andric   return (count + mask) & ~mask;
140bdd1243dSDimitry Andric }
141bdd1243dSDimitry Andric 
142bdd1243dSDimitry Andric struct unsynchronized_pool_resource::__adhoc_pool::__chunk_footer {
143bdd1243dSDimitry Andric   __chunk_footer* __next_;
144bdd1243dSDimitry Andric   char* __start_;
145bdd1243dSDimitry Andric   size_t __align_;
__allocation_sizepmr::unsynchronized_pool_resource::__adhoc_pool::__chunk_footer146bdd1243dSDimitry Andric   size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
147bdd1243dSDimitry Andric };
148bdd1243dSDimitry Andric 
__release_ptr(memory_resource * upstream)149bdd1243dSDimitry Andric void unsynchronized_pool_resource::__adhoc_pool::__release_ptr(memory_resource* upstream) {
150bdd1243dSDimitry Andric   while (__first_ != nullptr) {
151bdd1243dSDimitry Andric     __chunk_footer* next = __first_->__next_;
152bdd1243dSDimitry Andric     upstream->deallocate(__first_->__start_, __first_->__allocation_size(), __first_->__align_);
153bdd1243dSDimitry Andric     __first_ = next;
154bdd1243dSDimitry Andric   }
155bdd1243dSDimitry Andric }
156bdd1243dSDimitry Andric 
__do_allocate(memory_resource * upstream,size_t bytes,size_t align)157bdd1243dSDimitry Andric void* unsynchronized_pool_resource::__adhoc_pool::__do_allocate(memory_resource* upstream, size_t bytes, size_t align) {
158bdd1243dSDimitry Andric   const size_t footer_size  = sizeof(__chunk_footer);
159bdd1243dSDimitry Andric   const size_t footer_align = alignof(__chunk_footer);
160bdd1243dSDimitry Andric 
161bdd1243dSDimitry Andric   if (align < footer_align)
162bdd1243dSDimitry Andric     align = footer_align;
163bdd1243dSDimitry Andric 
164bdd1243dSDimitry Andric   size_t aligned_capacity = roundup(bytes, footer_align) + footer_size;
165bdd1243dSDimitry Andric 
166bdd1243dSDimitry Andric   void* result = upstream->allocate(aligned_capacity, align);
167bdd1243dSDimitry Andric 
168bdd1243dSDimitry Andric   __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
169bdd1243dSDimitry Andric   h->__next_        = __first_;
170bdd1243dSDimitry Andric   h->__start_       = (char*)result;
171bdd1243dSDimitry Andric   h->__align_       = align;
172bdd1243dSDimitry Andric   __first_          = h;
173bdd1243dSDimitry Andric   return result;
174bdd1243dSDimitry Andric }
175bdd1243dSDimitry Andric 
__do_deallocate(memory_resource * upstream,void * p,size_t bytes,size_t align)176bdd1243dSDimitry Andric void unsynchronized_pool_resource::__adhoc_pool::__do_deallocate(
177bdd1243dSDimitry Andric     memory_resource* upstream, void* p, size_t bytes, size_t align) {
1785f757f3fSDimitry Andric   _LIBCPP_ASSERT_NON_NULL(__first_ != nullptr, "deallocating a block that was not allocated with this allocator");
179bdd1243dSDimitry Andric   if (__first_->__start_ == p) {
180bdd1243dSDimitry Andric     __chunk_footer* next = __first_->__next_;
181bdd1243dSDimitry Andric     upstream->deallocate(p, __first_->__allocation_size(), __first_->__align_);
182bdd1243dSDimitry Andric     __first_ = next;
183bdd1243dSDimitry Andric   } else {
184bdd1243dSDimitry Andric     for (__chunk_footer* h = __first_; h->__next_ != nullptr; h = h->__next_) {
185bdd1243dSDimitry Andric       if (h->__next_->__start_ == p) {
186bdd1243dSDimitry Andric         __chunk_footer* next = h->__next_->__next_;
187bdd1243dSDimitry Andric         upstream->deallocate(p, h->__next_->__allocation_size(), h->__next_->__align_);
188bdd1243dSDimitry Andric         h->__next_ = next;
189bdd1243dSDimitry Andric         return;
190bdd1243dSDimitry Andric       }
191bdd1243dSDimitry Andric     }
192*7a6dacacSDimitry Andric     // The request to deallocate memory ends up being a no-op, likely resulting in a memory leak.
193*7a6dacacSDimitry Andric     _LIBCPP_ASSERT_VALID_DEALLOCATION(false, "deallocating a block that was not allocated with this allocator");
194bdd1243dSDimitry Andric   }
195bdd1243dSDimitry Andric }
196bdd1243dSDimitry Andric 
197bdd1243dSDimitry Andric class unsynchronized_pool_resource::__fixed_pool {
198bdd1243dSDimitry Andric   struct __chunk_footer {
199bdd1243dSDimitry Andric     __chunk_footer* __next_;
200bdd1243dSDimitry Andric     char* __start_;
201bdd1243dSDimitry Andric     size_t __align_;
__allocation_sizepmr::unsynchronized_pool_resource::__fixed_pool::__chunk_footer202bdd1243dSDimitry Andric     size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
203bdd1243dSDimitry Andric   };
204bdd1243dSDimitry Andric 
205bdd1243dSDimitry Andric   struct __vacancy_header {
206bdd1243dSDimitry Andric     __vacancy_header* __next_vacancy_;
207bdd1243dSDimitry Andric   };
208bdd1243dSDimitry Andric 
209bdd1243dSDimitry Andric   __chunk_footer* __first_chunk_     = nullptr;
210bdd1243dSDimitry Andric   __vacancy_header* __first_vacancy_ = nullptr;
211bdd1243dSDimitry Andric 
212bdd1243dSDimitry Andric public:
213bdd1243dSDimitry Andric   explicit __fixed_pool() = default;
214bdd1243dSDimitry Andric 
__release_ptr(memory_resource * upstream)215bdd1243dSDimitry Andric   void __release_ptr(memory_resource* upstream) {
216bdd1243dSDimitry Andric     __first_vacancy_ = nullptr;
217bdd1243dSDimitry Andric     while (__first_chunk_ != nullptr) {
218bdd1243dSDimitry Andric       __chunk_footer* next = __first_chunk_->__next_;
219bdd1243dSDimitry Andric       upstream->deallocate(__first_chunk_->__start_, __first_chunk_->__allocation_size(), __first_chunk_->__align_);
220bdd1243dSDimitry Andric       __first_chunk_ = next;
221bdd1243dSDimitry Andric     }
222bdd1243dSDimitry Andric   }
223bdd1243dSDimitry Andric 
__try_allocate_from_vacancies()224bdd1243dSDimitry Andric   void* __try_allocate_from_vacancies() {
225bdd1243dSDimitry Andric     if (__first_vacancy_ != nullptr) {
226bdd1243dSDimitry Andric       void* result     = __first_vacancy_;
227bdd1243dSDimitry Andric       __first_vacancy_ = __first_vacancy_->__next_vacancy_;
228bdd1243dSDimitry Andric       return result;
229bdd1243dSDimitry Andric     }
230bdd1243dSDimitry Andric     return nullptr;
231bdd1243dSDimitry Andric   }
232bdd1243dSDimitry Andric 
__allocate_in_new_chunk(memory_resource * upstream,size_t block_size,size_t chunk_size)233bdd1243dSDimitry Andric   void* __allocate_in_new_chunk(memory_resource* upstream, size_t block_size, size_t chunk_size) {
2341db9f3b2SDimitry Andric     _LIBCPP_ASSERT_INTERNAL(chunk_size % block_size == 0, "");
235bdd1243dSDimitry Andric     static_assert(__default_alignment >= alignof(std::max_align_t), "");
236bdd1243dSDimitry Andric     static_assert(__default_alignment >= alignof(__chunk_footer), "");
237bdd1243dSDimitry Andric     static_assert(__default_alignment >= alignof(__vacancy_header), "");
238bdd1243dSDimitry Andric 
239bdd1243dSDimitry Andric     const size_t footer_size  = sizeof(__chunk_footer);
240bdd1243dSDimitry Andric     const size_t footer_align = alignof(__chunk_footer);
241bdd1243dSDimitry Andric 
242bdd1243dSDimitry Andric     size_t aligned_capacity = roundup(chunk_size, footer_align) + footer_size;
243bdd1243dSDimitry Andric 
244bdd1243dSDimitry Andric     void* result = upstream->allocate(aligned_capacity, __default_alignment);
245bdd1243dSDimitry Andric 
246bdd1243dSDimitry Andric     __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
247bdd1243dSDimitry Andric     h->__next_        = __first_chunk_;
248bdd1243dSDimitry Andric     h->__start_       = (char*)result;
249bdd1243dSDimitry Andric     h->__align_       = __default_alignment;
250bdd1243dSDimitry Andric     __first_chunk_    = h;
251bdd1243dSDimitry Andric 
252bdd1243dSDimitry Andric     if (chunk_size > block_size) {
253bdd1243dSDimitry Andric       __vacancy_header* last_vh = this->__first_vacancy_;
254bdd1243dSDimitry Andric       for (size_t i = block_size; i != chunk_size; i += block_size) {
255bdd1243dSDimitry Andric         __vacancy_header* vh = (__vacancy_header*)((char*)result + i);
256bdd1243dSDimitry Andric         vh->__next_vacancy_  = last_vh;
257bdd1243dSDimitry Andric         last_vh              = vh;
258bdd1243dSDimitry Andric       }
259bdd1243dSDimitry Andric       this->__first_vacancy_ = last_vh;
260bdd1243dSDimitry Andric     }
261bdd1243dSDimitry Andric     return result;
262bdd1243dSDimitry Andric   }
263bdd1243dSDimitry Andric 
__evacuate(void * p)264bdd1243dSDimitry Andric   void __evacuate(void* p) {
265bdd1243dSDimitry Andric     __vacancy_header* vh = (__vacancy_header*)(p);
266bdd1243dSDimitry Andric     vh->__next_vacancy_  = __first_vacancy_;
267bdd1243dSDimitry Andric     __first_vacancy_     = vh;
268bdd1243dSDimitry Andric   }
269bdd1243dSDimitry Andric 
__previous_chunk_size_in_bytes() const270bdd1243dSDimitry Andric   size_t __previous_chunk_size_in_bytes() const { return __first_chunk_ ? __first_chunk_->__allocation_size() : 0; }
271bdd1243dSDimitry Andric 
272bdd1243dSDimitry Andric   static const size_t __default_alignment = alignof(max_align_t);
273bdd1243dSDimitry Andric };
274bdd1243dSDimitry Andric 
__pool_block_size(int i) const275bdd1243dSDimitry Andric size_t unsynchronized_pool_resource::__pool_block_size(int i) const { return size_t(1) << __log2_pool_block_size(i); }
276bdd1243dSDimitry Andric 
__log2_pool_block_size(int i) const277bdd1243dSDimitry Andric int unsynchronized_pool_resource::__log2_pool_block_size(int i) const { return (i + __log2_smallest_block_size); }
278bdd1243dSDimitry Andric 
__pool_index(size_t bytes,size_t align) const279bdd1243dSDimitry Andric int unsynchronized_pool_resource::__pool_index(size_t bytes, size_t align) const {
280bdd1243dSDimitry Andric   if (align > alignof(std::max_align_t) || bytes > (size_t(1) << __num_fixed_pools_))
281bdd1243dSDimitry Andric     return __num_fixed_pools_;
282bdd1243dSDimitry Andric   else {
283bdd1243dSDimitry Andric     int i = 0;
284bdd1243dSDimitry Andric     bytes = (bytes > align) ? bytes : align;
285bdd1243dSDimitry Andric     bytes -= 1;
286bdd1243dSDimitry Andric     bytes >>= __log2_smallest_block_size;
287bdd1243dSDimitry Andric     while (bytes != 0) {
288bdd1243dSDimitry Andric       bytes >>= 1;
289bdd1243dSDimitry Andric       i += 1;
290bdd1243dSDimitry Andric     }
291bdd1243dSDimitry Andric     return i;
292bdd1243dSDimitry Andric   }
293bdd1243dSDimitry Andric }
294bdd1243dSDimitry Andric 
unsynchronized_pool_resource(const pool_options & opts,memory_resource * upstream)295bdd1243dSDimitry Andric unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream)
296bdd1243dSDimitry Andric     : __res_(upstream), __fixed_pools_(nullptr) {
297bdd1243dSDimitry Andric   size_t largest_block_size;
298bdd1243dSDimitry Andric   if (opts.largest_required_pool_block == 0)
299bdd1243dSDimitry Andric     largest_block_size = __default_largest_block_size;
300bdd1243dSDimitry Andric   else if (opts.largest_required_pool_block < __smallest_block_size)
301bdd1243dSDimitry Andric     largest_block_size = __smallest_block_size;
302bdd1243dSDimitry Andric   else if (opts.largest_required_pool_block > __max_largest_block_size)
303bdd1243dSDimitry Andric     largest_block_size = __max_largest_block_size;
304bdd1243dSDimitry Andric   else
305bdd1243dSDimitry Andric     largest_block_size = opts.largest_required_pool_block;
306bdd1243dSDimitry Andric 
307bdd1243dSDimitry Andric   if (opts.max_blocks_per_chunk == 0)
308bdd1243dSDimitry Andric     __options_max_blocks_per_chunk_ = __max_blocks_per_chunk;
309bdd1243dSDimitry Andric   else if (opts.max_blocks_per_chunk < __min_blocks_per_chunk)
310bdd1243dSDimitry Andric     __options_max_blocks_per_chunk_ = __min_blocks_per_chunk;
311bdd1243dSDimitry Andric   else if (opts.max_blocks_per_chunk > __max_blocks_per_chunk)
312bdd1243dSDimitry Andric     __options_max_blocks_per_chunk_ = __max_blocks_per_chunk;
313bdd1243dSDimitry Andric   else
314bdd1243dSDimitry Andric     __options_max_blocks_per_chunk_ = opts.max_blocks_per_chunk;
315bdd1243dSDimitry Andric 
316bdd1243dSDimitry Andric   __num_fixed_pools_ = 1;
317bdd1243dSDimitry Andric   size_t capacity    = __smallest_block_size;
318bdd1243dSDimitry Andric   while (capacity < largest_block_size) {
319bdd1243dSDimitry Andric     capacity <<= 1;
320bdd1243dSDimitry Andric     __num_fixed_pools_ += 1;
321bdd1243dSDimitry Andric   }
322bdd1243dSDimitry Andric }
323bdd1243dSDimitry Andric 
options() const324bdd1243dSDimitry Andric pool_options unsynchronized_pool_resource::options() const {
325bdd1243dSDimitry Andric   pool_options p;
326bdd1243dSDimitry Andric   p.max_blocks_per_chunk        = __options_max_blocks_per_chunk_;
327bdd1243dSDimitry Andric   p.largest_required_pool_block = __pool_block_size(__num_fixed_pools_ - 1);
328bdd1243dSDimitry Andric   return p;
329bdd1243dSDimitry Andric }
330bdd1243dSDimitry Andric 
release()331bdd1243dSDimitry Andric void unsynchronized_pool_resource::release() {
332bdd1243dSDimitry Andric   __adhoc_pool_.__release_ptr(__res_);
333bdd1243dSDimitry Andric   if (__fixed_pools_ != nullptr) {
334bdd1243dSDimitry Andric     const int n = __num_fixed_pools_;
335bdd1243dSDimitry Andric     for (int i = 0; i < n; ++i)
336bdd1243dSDimitry Andric       __fixed_pools_[i].__release_ptr(__res_);
337bdd1243dSDimitry Andric     __res_->deallocate(__fixed_pools_, __num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
338bdd1243dSDimitry Andric     __fixed_pools_ = nullptr;
339bdd1243dSDimitry Andric   }
340bdd1243dSDimitry Andric }
341bdd1243dSDimitry Andric 
do_allocate(size_t bytes,size_t align)342bdd1243dSDimitry Andric void* unsynchronized_pool_resource::do_allocate(size_t bytes, size_t align) {
343bdd1243dSDimitry Andric   // A pointer to allocated storage (6.6.4.4.1) with a size of at least bytes.
344bdd1243dSDimitry Andric   // The size and alignment of the allocated memory shall meet the requirements for
345bdd1243dSDimitry Andric   // a class derived from memory_resource (23.12).
346bdd1243dSDimitry Andric   // If the pool selected for a block of size bytes is unable to satisfy the memory request
347bdd1243dSDimitry Andric   // from its own internal data structures, it will call upstream_resource()->allocate()
348bdd1243dSDimitry Andric   // to obtain more memory. If bytes is larger than that which the largest pool can handle,
349bdd1243dSDimitry Andric   // then memory will be allocated using upstream_resource()->allocate().
350bdd1243dSDimitry Andric 
351bdd1243dSDimitry Andric   int i = __pool_index(bytes, align);
352bdd1243dSDimitry Andric   if (i == __num_fixed_pools_)
353bdd1243dSDimitry Andric     return __adhoc_pool_.__do_allocate(__res_, bytes, align);
354bdd1243dSDimitry Andric   else {
355bdd1243dSDimitry Andric     if (__fixed_pools_ == nullptr) {
356bdd1243dSDimitry Andric       __fixed_pools_ =
357bdd1243dSDimitry Andric           (__fixed_pool*)__res_->allocate(__num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
358bdd1243dSDimitry Andric       __fixed_pool* first = __fixed_pools_;
359bdd1243dSDimitry Andric       __fixed_pool* last  = __fixed_pools_ + __num_fixed_pools_;
360bdd1243dSDimitry Andric       for (__fixed_pool* pool = first; pool != last; ++pool)
361bdd1243dSDimitry Andric         ::new ((void*)pool) __fixed_pool;
362bdd1243dSDimitry Andric     }
363bdd1243dSDimitry Andric     void* result = __fixed_pools_[i].__try_allocate_from_vacancies();
364bdd1243dSDimitry Andric     if (result == nullptr) {
365bdd1243dSDimitry Andric       auto min = [](size_t a, size_t b) { return a < b ? a : b; };
366bdd1243dSDimitry Andric       auto max = [](size_t a, size_t b) { return a < b ? b : a; };
367bdd1243dSDimitry Andric 
368bdd1243dSDimitry Andric       size_t prev_chunk_size_in_bytes  = __fixed_pools_[i].__previous_chunk_size_in_bytes();
369bdd1243dSDimitry Andric       size_t prev_chunk_size_in_blocks = prev_chunk_size_in_bytes >> __log2_pool_block_size(i);
370bdd1243dSDimitry Andric 
371bdd1243dSDimitry Andric       size_t chunk_size_in_blocks;
372bdd1243dSDimitry Andric 
373bdd1243dSDimitry Andric       if (prev_chunk_size_in_blocks == 0) {
374bdd1243dSDimitry Andric         size_t min_blocks_per_chunk = max(__min_bytes_per_chunk >> __log2_pool_block_size(i), __min_blocks_per_chunk);
375bdd1243dSDimitry Andric         chunk_size_in_blocks        = min_blocks_per_chunk;
376bdd1243dSDimitry Andric       } else {
377bdd1243dSDimitry Andric         static_assert(__max_bytes_per_chunk <= SIZE_MAX - (__max_bytes_per_chunk / 4), "unsigned overflow is possible");
378bdd1243dSDimitry Andric         chunk_size_in_blocks = prev_chunk_size_in_blocks + (prev_chunk_size_in_blocks / 4);
379bdd1243dSDimitry Andric       }
380bdd1243dSDimitry Andric 
381bdd1243dSDimitry Andric       size_t max_blocks_per_chunk =
382bdd1243dSDimitry Andric           min((__max_bytes_per_chunk >> __log2_pool_block_size(i)),
383bdd1243dSDimitry Andric               min(__max_blocks_per_chunk, __options_max_blocks_per_chunk_));
384bdd1243dSDimitry Andric       if (chunk_size_in_blocks > max_blocks_per_chunk)
385bdd1243dSDimitry Andric         chunk_size_in_blocks = max_blocks_per_chunk;
386bdd1243dSDimitry Andric 
387bdd1243dSDimitry Andric       size_t block_size = __pool_block_size(i);
388bdd1243dSDimitry Andric 
389bdd1243dSDimitry Andric       size_t chunk_size_in_bytes = (chunk_size_in_blocks << __log2_pool_block_size(i));
390bdd1243dSDimitry Andric       result                     = __fixed_pools_[i].__allocate_in_new_chunk(__res_, block_size, chunk_size_in_bytes);
391bdd1243dSDimitry Andric     }
392bdd1243dSDimitry Andric     return result;
393bdd1243dSDimitry Andric   }
394bdd1243dSDimitry Andric }
395bdd1243dSDimitry Andric 
do_deallocate(void * p,size_t bytes,size_t align)396bdd1243dSDimitry Andric void unsynchronized_pool_resource::do_deallocate(void* p, size_t bytes, size_t align) {
397bdd1243dSDimitry Andric   // Returns the memory at p to the pool. It is unspecified if,
398bdd1243dSDimitry Andric   // or under what circumstances, this operation will result in
399bdd1243dSDimitry Andric   // a call to upstream_resource()->deallocate().
400bdd1243dSDimitry Andric 
401bdd1243dSDimitry Andric   int i = __pool_index(bytes, align);
402bdd1243dSDimitry Andric   if (i == __num_fixed_pools_)
403bdd1243dSDimitry Andric     return __adhoc_pool_.__do_deallocate(__res_, p, bytes, align);
404bdd1243dSDimitry Andric   else {
4055f757f3fSDimitry Andric     _LIBCPP_ASSERT_NON_NULL(
40606c3fb27SDimitry Andric         __fixed_pools_ != nullptr, "deallocating a block that was not allocated with this allocator");
407bdd1243dSDimitry Andric     __fixed_pools_[i].__evacuate(p);
408bdd1243dSDimitry Andric   }
409bdd1243dSDimitry Andric }
410bdd1243dSDimitry Andric 
do_is_equal(const memory_resource & other) const411bdd1243dSDimitry Andric bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const noexcept { return &other == this; }
412bdd1243dSDimitry Andric 
413bdd1243dSDimitry Andric // 23.12.6, mem.res.monotonic.buffer
414bdd1243dSDimitry Andric 
align_down(size_t align,size_t size,void * & ptr,size_t & space)415bdd1243dSDimitry Andric static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
416bdd1243dSDimitry Andric   if (size > space)
417bdd1243dSDimitry Andric     return nullptr;
418bdd1243dSDimitry Andric 
419bdd1243dSDimitry Andric   char* p1      = static_cast<char*>(ptr);
420bdd1243dSDimitry Andric   char* new_ptr = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 - size) & ~(align - 1));
421bdd1243dSDimitry Andric 
422bdd1243dSDimitry Andric   if (new_ptr < (p1 - space))
423bdd1243dSDimitry Andric     return nullptr;
424bdd1243dSDimitry Andric 
425bdd1243dSDimitry Andric   ptr = new_ptr;
426bdd1243dSDimitry Andric   space -= p1 - new_ptr;
427bdd1243dSDimitry Andric 
428bdd1243dSDimitry Andric   return ptr;
429bdd1243dSDimitry Andric }
430bdd1243dSDimitry Andric 
__try_allocate_from_chunk(size_t bytes,size_t align)431bdd1243dSDimitry Andric void* monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk(size_t bytes, size_t align) {
432bdd1243dSDimitry Andric   if (!__cur_)
433bdd1243dSDimitry Andric     return nullptr;
434bdd1243dSDimitry Andric   void* new_ptr       = static_cast<void*>(__cur_);
435bdd1243dSDimitry Andric   size_t new_capacity = (__cur_ - __start_);
436bdd1243dSDimitry Andric   void* aligned_ptr   = align_down(align, bytes, new_ptr, new_capacity);
437bdd1243dSDimitry Andric   if (aligned_ptr != nullptr)
438bdd1243dSDimitry Andric     __cur_ = static_cast<char*>(new_ptr);
439bdd1243dSDimitry Andric   return aligned_ptr;
440bdd1243dSDimitry Andric }
441bdd1243dSDimitry Andric 
__try_allocate_from_chunk(size_t bytes,size_t align)442bdd1243dSDimitry Andric void* monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk(size_t bytes, size_t align) {
443bdd1243dSDimitry Andric   void* new_ptr       = static_cast<void*>(__cur_);
444bdd1243dSDimitry Andric   size_t new_capacity = (__cur_ - __start_);
445bdd1243dSDimitry Andric   void* aligned_ptr   = align_down(align, bytes, new_ptr, new_capacity);
446bdd1243dSDimitry Andric   if (aligned_ptr != nullptr)
447bdd1243dSDimitry Andric     __cur_ = static_cast<char*>(new_ptr);
448bdd1243dSDimitry Andric   return aligned_ptr;
449bdd1243dSDimitry Andric }
450bdd1243dSDimitry Andric 
do_allocate(size_t bytes,size_t align)451bdd1243dSDimitry Andric void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
452bdd1243dSDimitry Andric   const size_t footer_size  = sizeof(__chunk_footer);
453bdd1243dSDimitry Andric   const size_t footer_align = alignof(__chunk_footer);
454bdd1243dSDimitry Andric 
455bdd1243dSDimitry Andric   auto previous_allocation_size = [&]() {
456bdd1243dSDimitry Andric     if (__chunks_ != nullptr)
457bdd1243dSDimitry Andric       return __chunks_->__allocation_size();
458bdd1243dSDimitry Andric 
459bdd1243dSDimitry Andric     size_t newsize = (__initial_.__start_ != nullptr) ? (__initial_.__end_ - __initial_.__start_) : __initial_.__size_;
460bdd1243dSDimitry Andric 
461bdd1243dSDimitry Andric     return roundup(newsize, footer_align) + footer_size;
462bdd1243dSDimitry Andric   };
463bdd1243dSDimitry Andric 
464bdd1243dSDimitry Andric   if (void* result = __initial_.__try_allocate_from_chunk(bytes, align))
465bdd1243dSDimitry Andric     return result;
466bdd1243dSDimitry Andric   if (__chunks_ != nullptr) {
467bdd1243dSDimitry Andric     if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align))
468bdd1243dSDimitry Andric       return result;
469bdd1243dSDimitry Andric   }
470bdd1243dSDimitry Andric 
471bdd1243dSDimitry Andric   // Allocate a brand-new chunk.
472bdd1243dSDimitry Andric 
473bdd1243dSDimitry Andric   if (align < footer_align)
474bdd1243dSDimitry Andric     align = footer_align;
475bdd1243dSDimitry Andric 
476bdd1243dSDimitry Andric   size_t aligned_capacity  = roundup(bytes, footer_align) + footer_size;
477bdd1243dSDimitry Andric   size_t previous_capacity = previous_allocation_size();
478bdd1243dSDimitry Andric 
479bdd1243dSDimitry Andric   if (aligned_capacity <= previous_capacity) {
480bdd1243dSDimitry Andric     size_t newsize   = 2 * (previous_capacity - footer_size);
481bdd1243dSDimitry Andric     aligned_capacity = roundup(newsize, footer_align) + footer_size;
482bdd1243dSDimitry Andric   }
483bdd1243dSDimitry Andric 
484bdd1243dSDimitry Andric   char* start            = (char*)__res_->allocate(aligned_capacity, align);
485bdd1243dSDimitry Andric   auto end               = start + aligned_capacity - footer_size;
486bdd1243dSDimitry Andric   __chunk_footer* footer = (__chunk_footer*)(end);
487bdd1243dSDimitry Andric   footer->__next_        = __chunks_;
488bdd1243dSDimitry Andric   footer->__start_       = start;
489bdd1243dSDimitry Andric   footer->__cur_         = end;
490bdd1243dSDimitry Andric   footer->__align_       = align;
491bdd1243dSDimitry Andric   __chunks_              = footer;
492bdd1243dSDimitry Andric 
493bdd1243dSDimitry Andric   return __chunks_->__try_allocate_from_chunk(bytes, align);
494bdd1243dSDimitry Andric }
495bdd1243dSDimitry Andric 
496bdd1243dSDimitry Andric } // namespace pmr
497bdd1243dSDimitry Andric 
498bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
499