Skip to content

[libc++][NFC] Simplify __split_buffer a bit #114224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 54 additions & 60 deletions libcxx/include/__split_buffer
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ public:

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __alloc_; }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT { return __alloc_; }

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_; }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT { return __end_cap_; }

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }

Expand All @@ -129,15 +123,15 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
return static_cast<size_type>(__end_cap() - __first_);
return static_cast<size_type>(__end_cap_ - __first_);
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
return static_cast<size_type>(__begin_ - __first_);
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
return static_cast<size_type>(__end_cap() - __end_);
return static_cast<size_type>(__end_cap_ - __end_);
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
Expand Down Expand Up @@ -196,7 +190,7 @@ public:
private:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
_NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
__alloc() = std::move(__c.__alloc());
__alloc_ = std::move(__c.__alloc_);
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
Expand Down Expand Up @@ -225,14 +219,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants
return false;
if (__end_ != nullptr)
return false;
if (__end_cap() != nullptr)
if (__end_cap_ != nullptr)
return false;
} else {
if (__begin_ < __first_)
return false;
if (__end_ < __begin_)
return false;
if (__end_cap() < __end_)
if (__end_cap_ < __end_)
return false;
}
return true;
Expand All @@ -247,7 +241,7 @@ template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_));
}
}

Expand All @@ -262,7 +256,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), __x);
}
}

Expand All @@ -277,14 +271,14 @@ template <class _Tp, class _Allocator>
template <class _Iterator, class _Sentinel>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
__alloc_rr& __a = this->__alloc();
__alloc_rr& __a = __alloc_;
for (; __first != __last; ++__first) {
if (__end_ == __end_cap()) {
size_type __old_cap = __end_cap() - __first_;
if (__end_ == __end_cap_) {
size_type __old_cap = __end_cap_ - __first_;
size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
__split_buffer __buf(__new_cap, 0, __a);
for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
__alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
__alloc_traits::construct(__buf.__alloc_, std::__to_address(__buf.__end_), std::move(*__p));
swap(__buf);
}
__alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
Expand All @@ -304,15 +298,15 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), *__first);
}
}

template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
while (__begin_ != __new_begin)
__alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
__alloc_traits::destroy(__alloc_, std::__to_address(__begin_++));
}

template <class _Tp, class _Allocator>
Expand All @@ -325,7 +319,7 @@ template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
while (__new_last != __end_)
__alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
__alloc_traits::destroy(__alloc_, std::__to_address(--__end_));
}

template <class _Tp, class _Allocator>
Expand All @@ -341,19 +335,19 @@ __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __sta
if (__cap == 0) {
__first_ = nullptr;
} else {
auto __allocation = std::__allocate_at_least(__alloc(), __cap);
auto __allocation = std::__allocate_at_least(__alloc_, __cap);
__first_ = __allocation.ptr;
__cap = __allocation.count;
}
__begin_ = __end_ = __first_ + __start;
__end_cap() = __first_ + __cap;
__end_cap_ = __first_ + __cap;
}

template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
clear();
if (__first_)
__alloc_traits::deallocate(__alloc(), __first_, capacity());
__alloc_traits::deallocate(__alloc_, __first_, capacity());
}

template <class _Tp, class _Allocator>
Expand All @@ -364,30 +358,30 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__
__end_(std::move(__c.__end_)),
__end_cap_(std::move(__c.__end_cap_)),
__alloc_(std::move(__c.__alloc_)) {
__c.__first_ = nullptr;
__c.__begin_ = nullptr;
__c.__end_ = nullptr;
__c.__end_cap() = nullptr;
__c.__first_ = nullptr;
__c.__begin_ = nullptr;
__c.__end_ = nullptr;
__c.__end_cap_ = nullptr;
}

template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20
__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
: __end_cap_(nullptr), __alloc_(__a) {
if (__a == __c.__alloc()) {
__first_ = __c.__first_;
__begin_ = __c.__begin_;
__end_ = __c.__end_;
__end_cap() = __c.__end_cap();
__c.__first_ = nullptr;
__c.__begin_ = nullptr;
__c.__end_ = nullptr;
__c.__end_cap() = nullptr;
if (__a == __c.__alloc_) {
__first_ = __c.__first_;
__begin_ = __c.__begin_;
__end_ = __c.__end_;
__end_cap_ = __c.__end_cap_;
__c.__first_ = nullptr;
__c.__begin_ = nullptr;
__c.__end_ = nullptr;
__c.__end_cap_ = nullptr;
} else {
auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
__first_ = __allocation.ptr;
__begin_ = __end_ = __first_;
__end_cap() = __first_ + __allocation.count;
__end_cap_ = __first_ + __allocation.count;
typedef move_iterator<iterator> _Ip;
__construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
}
Expand All @@ -401,12 +395,12 @@ __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
!__alloc_traits::propagate_on_container_move_assignment::value) {
clear();
shrink_to_fit();
__first_ = __c.__first_;
__begin_ = __c.__begin_;
__end_ = __c.__end_;
__end_cap() = __c.__end_cap();
__first_ = __c.__first_;
__begin_ = __c.__begin_;
__end_ = __c.__end_;
__end_cap_ = __c.__end_cap_;
__move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap_ = nullptr;
return *this;
}

Expand All @@ -416,19 +410,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split
std::swap(__first_, __x.__first_);
std::swap(__begin_, __x.__begin_);
std::swap(__end_, __x.__end_);
std::swap(__end_cap(), __x.__end_cap());
std::__swap_allocator(__alloc(), __x.__alloc());
std::swap(__end_cap_, __x.__end_cap_);
std::__swap_allocator(__alloc_, __x.__alloc_);
}

template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
if (__n < capacity()) {
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
std::swap(__end_cap(), __t.__end_cap());
std::swap(__end_cap_, __t.__end_cap_);
}
}

Expand All @@ -438,13 +432,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
__t.__end_ = __t.__begin_ + (__end_ - __begin_);
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
std::swap(__end_cap(), __t.__end_cap());
std::swap(__end_cap_, __t.__end_cap_);
#if _LIBCPP_HAS_EXCEPTIONS
} catch (...) {
}
Expand All @@ -456,45 +450,45 @@ template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
if (__begin_ == __first_) {
if (__end_ < __end_cap()) {
difference_type __d = __end_cap() - __end_;
if (__end_ < __end_cap_) {
difference_type __d = __end_cap_ - __end_;
__d = (__d + 1) / 2;
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
__end_ += __d;
} else {
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
std::swap(__end_cap(), __t.__end_cap());
std::swap(__end_cap_, __t.__end_cap_);
}
}
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
__alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
--__begin_;
}

template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
if (__end_ == __end_cap()) {
if (__end_ == __end_cap_) {
if (__begin_ > __first_) {
difference_type __d = __begin_ - __first_;
__d = (__d + 1) / 2;
__end_ = std::move(__begin_, __end_, __begin_ - __d);
__begin_ -= __d;
} else {
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
std::swap(__end_cap(), __t.__end_cap());
std::swap(__end_cap_, __t.__end_cap_);
}
}
__alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
__alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);
++__end_;
}

Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
__end_ = __begin_; // All the objects have been destroyed by relocating them.
std::swap(this->__begin_, __v.__begin_);
std::swap(this->__end_, __v.__end_);
std::swap(this->__end_cap(), __v.__end_cap());
std::swap(this->__end_cap(), __v.__end_cap_);
__v.__first_ = __v.__begin_;
__annotate_new(size());
}
Expand Down Expand Up @@ -773,7 +773,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a

std::swap(this->__begin_, __v.__begin_);
std::swap(this->__end_, __v.__end_);
std::swap(this->__end_cap(), __v.__end_cap());
std::swap(this->__end_cap(), __v.__end_cap_);
__v.__first_ = __v.__begin_;
__annotate_new(size());
return __ret;
Expand Down
Loading
Loading