Skip to content

Commit 389a848

Browse files
committed
[libc++] Refactor some code in monotonic_buffer_resource
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.
1 parent 3f540e1 commit 389a848

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

libcxx/include/__memory_resource/monotonic_buffer_resource.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace pmr {
2727
// [mem.res.monotonic.buffer]
2828

2929
class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resource : public memory_resource {
30-
static const size_t __default_buffer_capacity = 1024;
31-
static const size_t __default_buffer_alignment = 16;
30+
static constexpr size_t __default_buffer_capacity = 1024;
31+
static constexpr size_t __default_growth_factor = 2;
3232

3333
struct __chunk_footer {
3434
__chunk_footer* __next_;
@@ -38,7 +38,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
3838
_LIBCPP_HIDE_FROM_ABI size_t __allocation_size() {
3939
return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this);
4040
}
41-
void* __try_allocate_from_chunk(size_t, size_t);
4241
};
4342

4443
struct __initial_descriptor {
@@ -48,9 +47,11 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
4847
char* __end_;
4948
size_t __size_;
5049
};
51-
void* __try_allocate_from_chunk(size_t, size_t);
5250
};
5351

52+
template <typename Chunk>
53+
_LIBCPP_HIDE_FROM_ABI void* __try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align);
54+
5455
public:
5556
_LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource()
5657
: monotonic_buffer_resource(nullptr, __default_buffer_capacity, get_default_resource()) {}

libcxx/src/memory_resource.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)