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 >= 23
23 template <class _Pointer>
24 struct allocation_result {
25   _Pointer ptr;
26   size_t count;
27 };
28 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(allocation_result);
29 
30 template <class _Alloc>
31 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<typename allocator_traits<_Alloc>::pointer>
allocate_at_least(_Alloc & __alloc,size_t __n)32 allocate_at_least(_Alloc& __alloc, size_t __n) {
33   if constexpr (requires { __alloc.allocate_at_least(__n); }) {
34     return __alloc.allocate_at_least(__n);
35   } else {
36     return {__alloc.allocate(__n), __n};
37   }
38 }
39 
40 template <class _Alloc>
__allocate_at_least(_Alloc & __alloc,size_t __n)41 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr 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>
54     __allocate_at_least(_Alloc& __alloc, size_t __n) {
55   return {__alloc.allocate(__n), __n};
56 }
57 
58 #endif // _LIBCPP_STD_VER >= 23
59 
60 _LIBCPP_END_NAMESPACE_STD
61 
62 #endif // _LIBCPP___MEMORY_ALLOCATE_AT_LEAST_H
63