master
 1// -*- C++ -*-
 2//===----------------------------------------------------------------------===//
 3//
 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 5// See https://llvm.org/LICENSE.txt for license information.
 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 7//
 8//===----------------------------------------------------------------------===//
 9
10#ifndef _LIBCPP___MEMORY_UNIQUE_TEMPORARY_BUFFER_H
11#define _LIBCPP___MEMORY_UNIQUE_TEMPORARY_BUFFER_H
12
13#include <__assert>
14#include <__config>
15
16#include <__cstddef/ptrdiff_t.h>
17#include <__memory/allocator.h>
18#include <__memory/unique_ptr.h>
19#include <__new/allocate.h>
20#include <__new/global_new_delete.h>
21#include <__type_traits/is_constant_evaluated.h>
22
23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24#  pragma GCC system_header
25#endif
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29template <class _Tp>
30struct __temporary_buffer_deleter {
31  ptrdiff_t __count_; // ignored in non-constant evaluation
32
33  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __temporary_buffer_deleter() _NOEXCEPT : __count_(0) {}
34  _LIBCPP_HIDE_FROM_ABI
35  _LIBCPP_CONSTEXPR explicit __temporary_buffer_deleter(ptrdiff_t __count) _NOEXCEPT : __count_(__count) {}
36
37  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator()(_Tp* __ptr) _NOEXCEPT {
38    if (__libcpp_is_constant_evaluated()) {
39      allocator<_Tp>().deallocate(__ptr, __count_);
40      return;
41    }
42
43    std::__libcpp_deallocate_unsized<_Tp>(__ptr);
44  }
45};
46
47template <class _Tp>
48using __unique_temporary_buffer _LIBCPP_NODEBUG = unique_ptr<_Tp, __temporary_buffer_deleter<_Tp> >;
49
50template <class _Tp>
51inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_CONSTEXPR_SINCE_CXX23 __unique_temporary_buffer<_Tp>
52__allocate_unique_temporary_buffer(ptrdiff_t __count) {
53  using __deleter_type       = __temporary_buffer_deleter<_Tp>;
54  using __unique_buffer_type = __unique_temporary_buffer<_Tp>;
55
56  if (__libcpp_is_constant_evaluated()) {
57    return __unique_buffer_type(allocator<_Tp>().allocate(__count), __deleter_type(__count));
58  }
59
60  _Tp* __ptr = nullptr;
61  const ptrdiff_t __max_count =
62      (~ptrdiff_t(0) ^ ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1))) / sizeof(_Tp);
63  if (__count > __max_count)
64    __count = __max_count;
65  while (__count > 0) {
66#if _LIBCPP_HAS_ALIGNED_ALLOCATION
67    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) {
68      align_val_t __al = align_val_t(_LIBCPP_ALIGNOF(_Tp));
69      __ptr            = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), __al, nothrow));
70    } else {
71      __ptr = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), nothrow));
72    }
73#else
74    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) {
75      // Since aligned operator new is unavailable, constructs an empty buffer rather than one with invalid alignment.
76      return __unique_buffer_type();
77    }
78
79    __ptr = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), nothrow));
80#endif
81
82    if (__ptr) {
83      break;
84    }
85    __count /= 2;
86  }
87
88  return __unique_buffer_type(__ptr, __deleter_type(__count));
89}
90
91_LIBCPP_END_NAMESPACE_STD
92
93#endif // _LIBCPP___MEMORY_UNIQUE_TEMPORARY_BUFFER_H