Skip to content

Commit

Permalink
[libc++] Refactor memory allocation in basic_string
Browse files Browse the repository at this point in the history
  • Loading branch information
philnik777 committed Feb 26, 2025
1 parent ec9c293 commit 7eb1656
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 206 deletions.
25 changes: 15 additions & 10 deletions libcxx/include/__memory/allocate_at_least.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Pointer, class _SizeT = size_t>
struct __allocation_result {
_Pointer ptr;
_SizeT count;

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __allocation_result(_Pointer __ptr, _SizeT __count)
: ptr(__ptr), count(__count) {}
};
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__allocation_result);

#if _LIBCPP_STD_VER >= 23

template <class _Alloc>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto __allocate_at_least(_Alloc& __alloc, size_t __n) {
return std::allocator_traits<_Alloc>::allocate_at_least(__alloc, __n);
auto __res = std::allocator_traits<_Alloc>::allocate_at_least(__alloc, __n);
return __allocation_result{__res.ptr, __res.count};
}

#else

template <class _Pointer>
struct __allocation_result {
_Pointer ptr;
size_t count;
};

template <class _Alloc>
template <class _Alloc, class _Traits = allocator_traits<_Alloc> >
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR __allocation_result<typename allocator_traits<_Alloc>::pointer>
_LIBCPP_CONSTEXPR __allocation_result<typename _Traits::pointer, typename _Traits::size_type>
__allocate_at_least(_Alloc& __alloc, size_t __n) {
return {__alloc.allocate(__n), __n};
return __allocation_result<typename _Traits::pointer, typename _Traits::size_type>(__alloc.allocate(__n), __n);
}

#endif // _LIBCPP_STD_VER >= 23
Expand Down
Loading

0 comments on commit 7eb1656

Please sign in to comment.