Skip to content

Commit 1f0adad

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 1f0adad

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-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: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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

Comments
 (0)