Skip to content

Commit 6285c46

Browse files
authored
[libc++] Refactor some code in monotonic_buffer_resource (llvm#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.
1 parent 7b23f41 commit 6285c46

12 files changed

+30
-41
lines changed

libcxx/include/__memory_resource/monotonic_buffer_resource.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ 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;
3231

3332
struct __chunk_footer {
3433
__chunk_footer* __next_;
@@ -38,7 +37,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
3837
_LIBCPP_HIDE_FROM_ABI size_t __allocation_size() {
3938
return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this);
4039
}
41-
void* __try_allocate_from_chunk(size_t, size_t);
4240
};
4341

4442
struct __initial_descriptor {
@@ -48,7 +46,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
4846
char* __end_;
4947
size_t __size_;
5048
};
51-
void* __try_allocate_from_chunk(size_t, size_t);
5249
};
5350

5451
public:

libcxx/lib/abi/CHANGELOG.TXT

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ New entries should be added directly below the "Version" header.
1616
Version 20.0
1717
------------
1818

19+
* [libc++][PMR] Remove duplicate implementation of __try_allocate_from_chunk
20+
21+
This patch removes member functions __try_allocate_from_chunk from __chunk_footer and __initial_descriptor.
22+
The two implementations were almost identical and can be easily moved to be implementation details of the built library.
23+
Since they were only used from within the built library, the removal of these symbols should not be an ABI break.
24+
25+
All platforms
26+
-------------
27+
Symbol removed: _ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm
28+
Symbol removed: _ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm
29+
1930
* [libc++] Remove the pointer safety functions from the dylib
2031

2132
The pointer safety functions were never implemented by anyone in a non-trivial

libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,6 @@
15941594
{'is_defined': True, 'name': '__ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
15951595
{'is_defined': True, 'name': '__ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
15961596
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1597-
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1598-
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
15991597
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
16001598
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
16011599
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/i686-linux-android21.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,6 @@
12301230
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20null_memory_resourceEv', 'type': 'FUNC'}
12311231
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
12321232
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource11do_allocateEjj', 'type': 'FUNC'}
1233-
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEjj', 'type': 'FUNC'}
1234-
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEjj', 'type': 'FUNC'}
12351233
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource11do_allocateEjj', 'type': 'FUNC'}
12361234
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEjj', 'type': 'FUNC'}
12371235
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,6 @@
580580
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
581581
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
582582
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
583-
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
584-
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
585583
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
586584
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
587585
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}

libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,6 @@
580580
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
581581
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
582582
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
583-
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
584-
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
585583
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
586584
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
587585
{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}

libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,6 @@
15941594
{'is_defined': True, 'name': '__ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
15951595
{'is_defined': True, 'name': '__ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
15961596
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1597-
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1598-
{'is_defined': True, 'name': '__ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
15991597
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
16001598
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
16011599
{'is_defined': True, 'name': '__ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/x86_64-linux-android21.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,6 @@
12301230
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20null_memory_resourceEv', 'type': 'FUNC'}
12311231
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
12321232
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1233-
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1234-
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
12351233
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
12361234
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
12371235
{'is_defined': True, 'name': '_ZNSt6__ndk13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,6 @@
12451245
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
12461246
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
12471247
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1248-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1249-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
12501248
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
12511249
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
12521250
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,6 @@
12431243
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
12441244
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
12451245
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1246-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1247-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
12481246
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
12491247
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
12501248
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.noexceptions.nonew.abilist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,6 @@
12141214
{'is_defined': True, 'name': '_ZNSt3__13pmr20null_memory_resourceEv', 'type': 'FUNC'}
12151215
{'is_defined': True, 'name': '_ZNSt3__13pmr20set_default_resourceEPNS0_15memory_resourceE', 'type': 'FUNC'}
12161216
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource11do_allocateEmm', 'type': 'FUNC'}
1217-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource14__chunk_footer25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
1218-
{'is_defined': True, 'name': '_ZNSt3__13pmr25monotonic_buffer_resource20__initial_descriptor25__try_allocate_from_chunkEmm', 'type': 'FUNC'}
12191217
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource11do_allocateEmm', 'type': 'FUNC'}
12201218
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__do_allocateEPNS0_15memory_resourceEmm', 'type': 'FUNC'}
12211219
{'is_defined': True, 'name': '_ZNSt3__13pmr28unsynchronized_pool_resource12__adhoc_pool13__release_ptrEPNS0_15memory_resourceE', 'type': 'FUNC'}

libcxx/src/memory_resource.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const
413413

414414
// 23.12.6, mem.res.monotonic.buffer
415415

416+
constexpr size_t __default_growth_factor = 2;
417+
416418
static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
417419
if (size > space)
418420
return nullptr;
@@ -429,23 +431,20 @@ static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
429431
return ptr;
430432
}
431433

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_);
434+
template <bool is_initial, typename Chunk>
435+
void* __try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align) {
436+
if constexpr (is_initial) {
437+
// only for __initial_descriptor.
438+
// if __initial_descriptor.__cur_ equals nullptr, means no available buffer given when ctor.
439+
// here we just return nullptr, let the caller do the next handling.
440+
if (!self.__cur_)
441+
return nullptr;
442+
}
443+
void* new_ptr = static_cast<void*>(self.__cur_);
444+
size_t new_capacity = (self.__cur_ - self.__start_);
446445
void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity);
447446
if (aligned_ptr != nullptr)
448-
__cur_ = static_cast<char*>(new_ptr);
447+
self.__cur_ = static_cast<char*>(new_ptr);
449448
return aligned_ptr;
450449
}
451450

@@ -462,10 +461,10 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
462461
return roundup(newsize, footer_align) + footer_size;
463462
};
464463

465-
if (void* result = __initial_.__try_allocate_from_chunk(bytes, align))
464+
if (void* result = __try_allocate_from_chunk<true, __initial_descriptor>(__initial_, bytes, align))
466465
return result;
467466
if (__chunks_ != nullptr) {
468-
if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align))
467+
if (void* result = __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align))
469468
return result;
470469
}
471470

@@ -478,7 +477,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
478477
size_t previous_capacity = previous_allocation_size();
479478

480479
if (aligned_capacity <= previous_capacity) {
481-
size_t newsize = 2 * (previous_capacity - footer_size);
480+
size_t newsize = __default_growth_factor * (previous_capacity - footer_size);
482481
aligned_capacity = roundup(newsize, footer_align) + footer_size;
483482
}
484483

@@ -491,7 +490,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
491490
footer->__align_ = align;
492491
__chunks_ = footer;
493492

494-
return __chunks_->__try_allocate_from_chunk(bytes, align);
493+
return __try_allocate_from_chunk<false, __chunk_footer>(*__chunks_, bytes, align);
495494
}
496495

497496
} // namespace pmr

0 commit comments

Comments
 (0)