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 <__memory/allocator_traits.h>
14 #include <cstddef>
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 #if _LIBCPP_STD_VER > 20
23 template <class _Pointer>
24 struct allocation_result {
25   _Pointer ptr;
26   size_t count;
27 };
28 
29 template <class _Alloc>
30 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr
31 allocation_result<typename allocator_traits<_Alloc>::pointer> allocate_at_least(_Alloc& __alloc, size_t __n) {
32   if constexpr (requires { __alloc.allocate_at_least(__n); }) {
33     return __alloc.allocate_at_least(__n);
34   } else {
35     return {__alloc.allocate(__n), __n};
36   }
37 }
38 
39 template <class _Alloc>
40 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr
41 auto __allocate_at_least(_Alloc& __alloc, size_t __n) {
42   return std::allocate_at_least(__alloc, __n);
43 }
44 #else
45 template <class _Pointer>
46 struct __allocation_result {
47   _Pointer ptr;
48   size_t count;
49 };
50 
51 template <class _Alloc>
52 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
53 __allocation_result<typename allocator_traits<_Alloc>::pointer> __allocate_at_least(_Alloc& __alloc, size_t __n) {
54   return {__alloc.allocate(__n), __n};
55 }
56 
57 #endif // _LIBCPP_STD_VER > 20
58 
59 _LIBCPP_END_NAMESPACE_STD
60 
61 #endif // _LIBCPP___MEMORY_ALLOCATE_AT_LEAST_H
62