99#include < cstddef>
1010#include < memory>
1111#include < memory_resource>
12+ #include < type_traits>
1213
1314#if _LIBCPP_HAS_ATOMIC_HEADER
1415# include < atomic>
@@ -429,23 +430,17 @@ static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
429430 return ptr;
430431}
431432
432- void * monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk (size_t bytes, size_t align) {
433- if (!__cur_)
434- return nullptr ;
435- void * new_ptr = static_cast <void *>(__cur_);
436- size_t new_capacity = (__cur_ - __start_);
437- void * aligned_ptr = align_down (align, bytes, new_ptr, new_capacity);
438- if (aligned_ptr != nullptr )
439- __cur_ = static_cast <char *>(new_ptr);
440- return aligned_ptr;
441- }
442-
443- void * monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk (size_t bytes, size_t align) {
444- void * new_ptr = static_cast <void *>(__cur_);
445- size_t new_capacity = (__cur_ - __start_);
433+ template <typename Chunk>
434+ void * monotonic_buffer_resource::__try_allocate_from_chunk (Chunk& self, size_t bytes, size_t align) {
435+ if constexpr (is_same_v<decay<Chunk>, monotonic_buffer_resource::__initial_descriptor>) {
436+ if (self.__cur_ )
437+ return nullptr ;
438+ }
439+ void * new_ptr = static_cast <void *>(self.__cur_ );
440+ size_t new_capacity = (self.__cur_ - self.__start_ );
446441 void * aligned_ptr = align_down (align, bytes, new_ptr, new_capacity);
447442 if (aligned_ptr != nullptr )
448- __cur_ = static_cast <char *>(new_ptr);
443+ self. __cur_ = static_cast <char *>(new_ptr);
449444 return aligned_ptr;
450445}
451446
@@ -462,10 +457,10 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
462457 return roundup (newsize, footer_align) + footer_size;
463458 };
464459
465- if (void * result = __initial_. __try_allocate_from_chunk (bytes, align))
460+ if (void * result = __try_allocate_from_chunk (__initial_, bytes, align))
466461 return result;
467462 if (__chunks_ != nullptr ) {
468- if (void * result = __chunks_-> __try_allocate_from_chunk (bytes, align))
463+ if (void * result = __try_allocate_from_chunk (*__chunks_, bytes, align))
469464 return result;
470465 }
471466
@@ -478,7 +473,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
478473 size_t previous_capacity = previous_allocation_size ();
479474
480475 if (aligned_capacity <= previous_capacity) {
481- size_t newsize = 2 * (previous_capacity - footer_size);
476+ size_t newsize = __default_growth_factor * (previous_capacity - footer_size);
482477 aligned_capacity = roundup (newsize, footer_align) + footer_size;
483478 }
484479
@@ -491,7 +486,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
491486 footer->__align_ = align;
492487 __chunks_ = footer;
493488
494- return __chunks_-> __try_allocate_from_chunk (bytes, align);
489+ return __try_allocate_from_chunk (*__chunks_, bytes, align);
495490}
496491
497492} // namespace pmr
0 commit comments