Skip to content

Commit

Permalink
[libc++] Refactor some code in monotonic_buffer_resource (#117271)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
love1angel authored Dec 23, 2024
1 parent 7b23f41 commit 6285c46
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 41 deletions.
5 changes: 1 addition & 4 deletions libcxx/include/__memory_resource/monotonic_buffer_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace pmr {
// [mem.res.monotonic.buffer]

class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resource : public memory_resource {
static const size_t __default_buffer_capacity = 1024;
static const size_t __default_buffer_alignment = 16;
static constexpr size_t __default_buffer_capacity = 1024;

struct __chunk_footer {
__chunk_footer* __next_;
Expand All @@ -38,7 +37,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
_LIBCPP_HIDE_FROM_ABI size_t __allocation_size() {
return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this);
}
void* __try_allocate_from_chunk(size_t, size_t);
};

struct __initial_descriptor {
Expand All @@ -48,7 +46,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
char* __end_;
size_t __size_;
};
void* __try_allocate_from_chunk(size_t, size_t);
};

public:
Expand Down
11 changes: 11 additions & 0 deletions libcxx/lib/abi/CHANGELOG.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ New entries should be added directly below the "Version" header.
Version 20.0
------------

* [libc++][PMR] Remove duplicate implementation of __try_allocate_from_chunk

This patch removes member functions __try_allocate_from_chunk from __chunk_footer and __initial_descriptor.
The two implementations were almost identical and can be easily moved to be implementation details of the built library.
Since they were only used from within the built library, the removal of these symbols should not be an ABI break.

All platforms
-------------
Symbol removed: _ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm
Symbol removed: _ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm

* [libc++] Remove the pointer safety functions from the dylib

The pointer safety functions were never implemented by anyone in a non-trivial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,6 @@
{'is_defined': True, 'name': '__ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,6 @@
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource11do_allocateEjj', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEjj', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEjj', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource11do_allocateEjj', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEjj', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,6 @@
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,6 @@
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,6 @@
{'is_defined': True, 'name': '__ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,6 @@
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,8 +1245,6 @@
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,8 +1243,6 @@
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1214,8 +1214,6 @@
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}
Expand Down
37 changes: 18 additions & 19 deletions libcxx/src/memory_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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
Expand Down

0 comments on commit 6285c46

Please sign in to comment.