diff options
author | Peng Xie <helianthus547@gmail.com> | 2024-12-23 14:32:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-23 14:32:59 +0800 |
commit | 6285c46e164bcb9cbc66bc91c03f41dbbeff69e5 (patch) | |
tree | 301782fadddc0fe1be2c8461e24643531ed3e027 /libcxx/src | |
parent | 7b23f413d1f76532825e470b523e971818d453ca (diff) | |
download | llvm-6285c46e164bcb9cbc66bc91c03f41dbbeff69e5.zip llvm-6285c46e164bcb9cbc66bc91c03f41dbbeff69e5.tar.gz llvm-6285c46e164bcb9cbc66bc91c03f41dbbeff69e5.tar.bz2 |
[libc++] Refactor some code in monotonic_buffer_resource (#117271)
1. remove unused __default_buffer_alignment
2. two __try_allocate_from_chunk are same, put it together
This patch refactor some code in monotonic_buffer_resource.
Diffstat (limited to 'libcxx/src')
-rw-r--r-- | libcxx/src/memory_resource.cpp | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/libcxx/src/memory_resource.cpp b/libcxx/src/memory_resource.cpp index 0cd575e..e182e5a 100644 --- a/libcxx/src/memory_resource.cpp +++ b/libcxx/src/memory_resource.cpp @@ -413,6 +413,8 @@ bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const // 23.12.6, mem.res.monotonic.buffer +constexpr size_t __default_growth_factor = 2; + static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) { if (size > space) return nullptr; @@ -429,23 +431,20 @@ static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) { return ptr; } -void* monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk(size_t bytes, size_t align) { - if (!__cur_) - return nullptr; - void* new_ptr = static_cast<void*>(__cur_); - size_t new_capacity = (__cur_ - __start_); - void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity); - if (aligned_ptr != nullptr) - __cur_ = static_cast<char*>(new_ptr); - return aligned_ptr; -} - -void* monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk(size_t bytes, size_t align) { - void* new_ptr = static_cast<void*>(__cur_); - size_t new_capacity = (__cur_ - __start_); +template <bool is_initial, typename Chunk> +void* __try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align) { + if constexpr (is_initial) { + // only for __initial_descriptor. + // if __initial_descriptor.__cur_ equals nullptr, means no available buffer given when ctor. + // here we just return nullptr, let the caller do the next handling. + if (!self.__cur_) + return nullptr; + } + void* new_ptr = static_cast<void*>(self.__cur_); + size_t new_capacity = (self.__cur_ - self.__start_); void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity); if (aligned_ptr != nullptr) - __cur_ = static_cast<char*>(new_ptr); + self.__cur_ = static_cast<char*>(new_ptr); return aligned_ptr; } @@ -462,10 +461,10 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) { return roundup(newsize, footer_align) + footer_size; }; - if (void* result = __initial_.__try_allocate_from_chunk(bytes, align)) + if (void* result = __try_allocate_from_chunk<true, __initial_descriptor>(__initial_, bytes, align)) return result; if (__chunks_ != nullptr) { - if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align)) + if (void* result = __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align)) return result; } @@ -478,7 +477,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) { size_t previous_capacity = previous_allocation_size(); if (aligned_capacity <= previous_capacity) { - size_t newsize = 2 * (previous_capacity - footer_size); + size_t newsize = __default_growth_factor * (previous_capacity - footer_size); aligned_capacity = roundup(newsize, footer_align) + footer_size; } @@ -491,7 +490,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) { footer->__align_ = align; __chunks_ = footer; - return __chunks_->__try_allocate_from_chunk(bytes, align); + return __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align); } } // namespace pmr |