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