| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___MEMORY_ALLOCATE_AT_LEAST_H |
| 10 | #define _LIBCPP___MEMORY_ALLOCATE_AT_LEAST_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__cstddef/size_t.h> |
| 14 | #include <__memory/allocator_traits.h> |
| 15 | |
| 16 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 17 | # pragma GCC system_header |
| 18 | #endif |
| 19 | |
| 20 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 21 | |
| 22 | template <class _Pointer, class _SizeT = size_t> |
| 23 | struct __allocation_result { |
| 24 | _Pointer ptr; |
| 25 | _SizeT count; |
| 26 | |
| 27 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __allocation_result(_Pointer __ptr, _SizeT __count) |
| 28 | : ptr(__ptr), count(__count) {} |
| 29 | }; |
| 30 | _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__allocation_result); |
| 31 | |
| 32 | #if _LIBCPP_STD_VER >= 23 |
| 33 | |
| 34 | template <class _Alloc> |
| 35 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto __allocate_at_least(_Alloc& __alloc, size_t __n) { |
| 36 | auto __res = std::allocator_traits<_Alloc>::allocate_at_least(__alloc, __n); |
| 37 | return __allocation_result{__res.ptr, __res.count}; |
| 38 | } |
| 39 | |
| 40 | #else |
| 41 | |
| 42 | template <class _Alloc, class _Traits = allocator_traits<_Alloc> > |
| 43 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI |
| 44 | _LIBCPP_CONSTEXPR __allocation_result<typename _Traits::pointer, typename _Traits::size_type> |
| 45 | __allocate_at_least(_Alloc& __alloc, size_t __n) { |
| 46 | return __allocation_result<typename _Traits::pointer, typename _Traits::size_type>(__alloc.allocate(__n), __n); |
| 47 | } |
| 48 | |
| 49 | #endif // _LIBCPP_STD_VER >= 23 |
| 50 | |
| 51 | _LIBCPP_END_NAMESPACE_STD |
| 52 | |
| 53 | #endif // _LIBCPP___MEMORY_ALLOCATE_AT_LEAST_H |
| 54 | |