@@ -429,23 +429,17 @@ static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
429429 return ptr;
430430}
431431
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_);
432+ template <typename Chunk>
433+ void * monotonic_buffer_resource::__try_allocate_from_chunk (Chunk& self, size_t bytes, size_t align) {
434+ if constexpr (is_same_v<decay<Chunk>, monotonic_buffer_resource::__initial_descriptor>) {
435+ if (self.__cur_ )
436+ return nullptr ;
437+ }
438+ void * new_ptr = static_cast <void *>(self.__cur_ );
439+ size_t new_capacity = (self.__cur_ - self.__start_ );
446440 void * aligned_ptr = align_down (align, bytes, new_ptr, new_capacity);
447441 if (aligned_ptr != nullptr )
448- __cur_ = static_cast <char *>(new_ptr);
442+ self. __cur_ = static_cast <char *>(new_ptr);
449443 return aligned_ptr;
450444}
451445
@@ -462,10 +456,10 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
462456 return roundup (newsize, footer_align) + footer_size;
463457 };
464458
465- if (void * result = __initial_. __try_allocate_from_chunk (bytes, align))
459+ if (void * result = __try_allocate_from_chunk (__initial_, bytes, align))
466460 return result;
467461 if (__chunks_ != nullptr ) {
468- if (void * result = __chunks_-> __try_allocate_from_chunk (bytes, align))
462+ if (void * result = __try_allocate_from_chunk (*__chunks_, bytes, align))
469463 return result;
470464 }
471465
@@ -478,7 +472,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
478472 size_t previous_capacity = previous_allocation_size ();
479473
480474 if (aligned_capacity <= previous_capacity) {
481- size_t newsize = 2 * (previous_capacity - footer_size);
475+ size_t newsize = __default_growth_factor * (previous_capacity - footer_size);
482476 aligned_capacity = roundup (newsize, footer_align) + footer_size;
483477 }
484478
@@ -491,7 +485,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
491485 footer->__align_ = align;
492486 __chunks_ = footer;
493487
494- return __chunks_-> __try_allocate_from_chunk (bytes, align);
488+ return __try_allocate_from_chunk (*__chunks_, bytes, align);
495489}
496490
497491} // namespace pmr
0 commit comments