Skip to content

Commit 4462194

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

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

libcxx/include/string

Lines changed: 27 additions & 27 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& __allocator, 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(__allocator, __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 {
@@ -906,6 +915,13 @@ private:
906915
};
907916
_LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
908917
value_type __data_[__min_cap];
918+
919+
__short() = default;
920+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __short(size_type __size)
921+
: __is_long_(false), __size_(static_cast<unsigned char>(__size)), __padding_(), __data_() {
922+
_LIBCPP_ASSERT_INTERNAL(
923+
__fits_in_sso(__size), "Trying to construct short string with a size greater than the small buffer size");
924+
}
909925
};
910926

911927
# endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
@@ -2255,19 +2271,6 @@ private:
22552271
// These functions are only responsible for managing the buffer itself, not the value inside the buffer. As such,
22562272
// none of these facilities ensure that there is a null terminator at `data()[size()]`.
22572273

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-
22712274
// Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
22722275
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
22732276
__annotate_delete();
@@ -2279,18 +2282,15 @@ private:
22792282
// Initialize the internal buffer to hold __size elements
22802283
// The elements and null terminator have to be set by the caller
22812284
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __init_internal_buffer(size_type __size) {
2282-
if (__libcpp_is_constant_evaluated())
2283-
__rep_ = __rep();
2284-
22852285
if (__size > max_size())
22862286
__throw_length_error();
22872287

22882288
if (__fits_in_sso(__size)) {
2289-
__set_short_size(__size);
2289+
__rep_ = __short(__size);
22902290
__annotate_new(__size);
22912291
return __get_short_pointer();
22922292
} else {
2293-
__rep_.__l = __allocate_long_buffer(__alloc_, __size);
2293+
__rep_ = __long(__alloc_, __size);
22942294
__annotate_new(__size);
22952295
return __get_long_pointer();
22962296
}
@@ -2447,7 +2447,7 @@ private:
24472447
__annotate_delete();
24482448
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
24492449
auto __alloc = __str.__alloc_;
2450-
__reset_internal_buffer(__allocate_long_buffer(__alloc, __str.size()));
2450+
__reset_internal_buffer(__long(__alloc, __str.size()));
24512451
__alloc_ = std::move(__alloc);
24522452
}
24532453
}
@@ -2700,8 +2700,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27002700
size_type __cap =
27012701
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
27022702
__annotate_delete();
2703-
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2704-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
2703+
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2704+
__long __buffer(__alloc_, __cap);
27052705
if (__n_copy != 0)
27062706
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27072707
if (__n_add != 0)
@@ -2737,7 +2737,7 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27372737
pointer __old_p = __get_pointer();
27382738
size_type __cap =
27392739
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
2740-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
2740+
__long __buffer(__alloc_, __cap);
27412741
if (__n_copy != 0)
27422742
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27432743
size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
@@ -3394,7 +3394,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
33943394
return;
33953395

33963396
__annotation_guard __g(*this);
3397-
__long __buffer = __allocate_long_buffer(__alloc_, __requested_capacity);
3397+
__long __buffer(__alloc_, __requested_capacity);
33983398
__buffer.__size_ = size();
33993399
traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);
34003400
__reset_internal_buffer(__buffer);
@@ -3425,7 +3425,7 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34253425
try {
34263426
# endif // _LIBCPP_HAS_EXCEPTIONS
34273427
__annotation_guard __g(*this);
3428-
__long __buffer = __allocate_long_buffer(__alloc_, __size);
3428+
__long __buffer(__alloc_, __size);
34293429

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

0 commit comments

Comments
 (0)