Skip to content

Commit 4a9bb8e

Browse files
committed
[libc++] Make __allocate_long_buffer a constructor of __long
1 parent bb4ed55 commit 4a9bb8e

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

libcxx/include/string

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,18 @@ private:
884884
struct __long {
885885
__long() = default;
886886

887-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)
888-
: __is_long_(true), __cap_(__alloc.count / __endian_factor), __size_(__size), __data_(__alloc.ptr) {
889-
_LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");
887+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(_Allocator& __alloc, size_type __size)
888+
: __is_long_(true), __size_(__size) {
889+
_LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__size), "Long capacity should always be larger than the SSO");
890+
auto __allocation = std::__allocate_at_least(__alloc, __recommend(__size) + 1);
891+
892+
__cap_ = __allocation.count / __endian_factor;
893+
__data_ = __allocation.ptr;
894+
895+
if (__libcpp_is_constant_evaluated()) {
896+
for (size_type __i = 0; __i != __allocation.count; ++__i)
897+
std::__construct_at(std::addressof(__allocation.ptr[__i]));
898+
}
890899
}
891900

892901
struct _LIBCPP_PACKED {
@@ -2255,19 +2264,6 @@ private:
22552264
// These functions are only responsible for managing the buffer itself, not the value inside the buffer. As such,
22562265
// none of these facilities ensure that there is a null terminator at `data()[size()]`.
22572266

2258-
// Allocate a buffer of __capacity size with __alloc and return it
2259-
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
2260-
__allocate_long_buffer(_Allocator& __alloc, size_type __capacity) {
2261-
auto __buffer = std::__allocate_at_least(__alloc, __recommend(__capacity) + 1);
2262-
2263-
if (__libcpp_is_constant_evaluated()) {
2264-
for (size_type __i = 0; __i != __buffer.count; ++__i)
2265-
std::__construct_at(std::addressof(__buffer.ptr[__i]));
2266-
}
2267-
2268-
return __long(__buffer, __capacity);
2269-
}
2270-
22712267
// Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
22722268
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
22732269
__annotate_delete();
@@ -2290,7 +2286,7 @@ private:
22902286
__annotate_new(__size);
22912287
return __get_short_pointer();
22922288
} else {
2293-
__rep_.__l = __allocate_long_buffer(__alloc_, __size);
2289+
__rep_ = __long(__alloc_, __size);
22942290
__annotate_new(__size);
22952291
return __get_long_pointer();
22962292
}
@@ -2447,7 +2443,7 @@ private:
24472443
__annotate_delete();
24482444
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
24492445
auto __alloc = __str.__alloc_;
2450-
__reset_internal_buffer(__allocate_long_buffer(__alloc, __str.size()));
2446+
__reset_internal_buffer(__long(__alloc, __str.size()));
24512447
__alloc_ = std::move(__alloc);
24522448
}
24532449
}
@@ -2700,8 +2696,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27002696
size_type __cap =
27012697
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
27022698
__annotate_delete();
2703-
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2704-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
2699+
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2700+
__long __buffer(__alloc_, __cap);
27052701
if (__n_copy != 0)
27062702
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27072703
if (__n_add != 0)
@@ -2737,7 +2733,7 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27372733
pointer __old_p = __get_pointer();
27382734
size_type __cap =
27392735
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
2740-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
2736+
__long __buffer(__alloc_, __cap);
27412737
if (__n_copy != 0)
27422738
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27432739
size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
@@ -3394,7 +3390,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
33943390
return;
33953391

33963392
__annotation_guard __g(*this);
3397-
__long __buffer = __allocate_long_buffer(__alloc_, __requested_capacity);
3393+
__long __buffer(__alloc_, __requested_capacity);
33983394
__buffer.__size_ = size();
33993395
traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);
34003396
__reset_internal_buffer(__buffer);
@@ -3425,7 +3421,7 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34253421
try {
34263422
# endif // _LIBCPP_HAS_EXCEPTIONS
34273423
__annotation_guard __g(*this);
3428-
__long __buffer = __allocate_long_buffer(__alloc_, __size);
3424+
__long __buffer(__alloc_, __size);
34293425

34303426
// The Standard mandates shrink_to_fit() does not increase the capacity.
34313427
// With equal capacity keep the existing buffer. This avoids extra work

0 commit comments

Comments
 (0)