Coverage Report

Created: 2025-08-28 06:28

/src/llvm-project-18.1.8.build/include/c++/v1/new
Line
Count
Source (jump to first uncovered line)
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_NEW
11
#define _LIBCPP_NEW
12
13
/*
14
    new synopsis
15
16
namespace std
17
{
18
19
class bad_alloc
20
    : public exception
21
{
22
public:
23
    bad_alloc() noexcept;
24
    bad_alloc(const bad_alloc&) noexcept;
25
    bad_alloc& operator=(const bad_alloc&) noexcept;
26
    virtual const char* what() const noexcept;
27
};
28
29
class bad_array_new_length : public bad_alloc // C++14
30
{
31
public:
32
    bad_array_new_length() noexcept;
33
};
34
35
enum class align_val_t : size_t {}; // C++17
36
37
struct destroying_delete_t { // C++20
38
  explicit destroying_delete_t() = default;
39
};
40
inline constexpr destroying_delete_t destroying_delete{}; // C++20
41
42
struct nothrow_t { explicit nothrow_t() = default; };
43
extern const nothrow_t nothrow;
44
typedef void (*new_handler)();
45
new_handler set_new_handler(new_handler new_p) noexcept;
46
new_handler get_new_handler() noexcept;
47
48
// 21.6.4, pointer optimization barrier
49
template <class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;   // C++17, nodiscard since C++20
50
}  // std
51
52
void* operator new(std::size_t size);                                   // replaceable, nodiscard in C++20
53
void* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17, nodiscard in C++20
54
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable, nodiscard in C++20
55
void* operator new(std::size_t size, std::align_val_t alignment,
56
                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++20
57
void  operator delete(void* ptr) noexcept;                              // replaceable
58
void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
59
void  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
60
void  operator delete(void* ptr, std::size_t size,
61
                      std::align_val_t alignment) noexcept;             // replaceable, C++17
62
void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
63
void  operator delete(void* ptr, std:align_val_t alignment,
64
                      const std::nothrow_t&) noexcept;                  // replaceable, C++17
65
66
void* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++20
67
void* operator new[](std::size_t size,
68
                     std::align_val_t alignment) noexcept;              // replaceable, C++17, nodiscard in C++20
69
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
70
void* operator new[](std::size_t size, std::align_val_t alignment,
71
                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++20
72
void  operator delete[](void* ptr) noexcept;                            // replaceable
73
void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
74
void  operator delete[](void* ptr,
75
                        std::align_val_t alignment) noexcept;           // replaceable, C++17
76
void  operator delete[](void* ptr, std::size_t size,
77
                        std::align_val_t alignment) noexcept;           // replaceable, C++17
78
void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
79
void  operator delete[](void* ptr, std::align_val_t alignment,
80
                        const std::nothrow_t&) noexcept;                // replaceable, C++17
81
82
void* operator new  (std::size_t size, void* ptr) noexcept;             // nodiscard in C++20
83
void* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++20
84
void  operator delete  (void* ptr, void*) noexcept;
85
void  operator delete[](void* ptr, void*) noexcept;
86
87
*/
88
89
#include <__assert> // all public C++ headers provide the assertion handler
90
#include <__availability>
91
#include <__config>
92
#include <__exception/exception.h>
93
#include <__type_traits/is_function.h>
94
#include <__type_traits/is_same.h>
95
#include <__type_traits/remove_cv.h>
96
#include <cstddef>
97
#include <version>
98
99
#if defined(_LIBCPP_ABI_VCRUNTIME)
100
#  include <new.h>
101
#endif
102
103
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
104
#  pragma GCC system_header
105
#endif
106
107
#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
108
#  define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
109
#endif
110
111
#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
112
#  define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
113
#endif
114
115
#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
116
#  define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
117
#endif
118
119
namespace std // purposefully not using versioning namespace
120
{
121
122
#if !defined(_LIBCPP_ABI_VCRUNTIME)
123
struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
124
  explicit nothrow_t() = default;
125
};
126
extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
127
128
class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
129
public:
130
  bad_alloc() _NOEXCEPT;
131
  _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT            = default;
132
  _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
133
  ~bad_alloc() _NOEXCEPT override;
134
  const char* what() const _NOEXCEPT override;
135
};
136
137
class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
138
public:
139
  bad_array_new_length() _NOEXCEPT;
140
  _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT            = default;
141
  _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
142
  ~bad_array_new_length() _NOEXCEPT override;
143
  const char* what() const _NOEXCEPT override;
144
};
145
146
typedef void (*new_handler)();
147
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
148
_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
149
150
#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
151
152
// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
153
// since they would normally be provided in vcruntime_exception.h
154
class bad_alloc : public exception {
155
public:
156
  bad_alloc() noexcept : exception("bad allocation") {}
157
158
private:
159
  friend class bad_array_new_length;
160
161
  bad_alloc(char const* const __message) noexcept : exception(__message) {}
162
};
163
164
class bad_array_new_length : public bad_alloc {
165
public:
166
  bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
167
};
168
#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
169
170
_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
171
172
0
_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {
173
0
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
174
0
  throw bad_array_new_length();
175
0
#else
176
0
  _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
177
0
#endif
178
0
}
179
180
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && !defined(_LIBCPP_ABI_VCRUNTIME)
181
#  ifndef _LIBCPP_CXX03_LANG
182
enum class align_val_t : size_t {};
183
#  else
184
enum align_val_t { __zero = 0, __max = (size_t)-1 };
185
#  endif
186
#endif
187
188
#if _LIBCPP_STD_VER >= 20
189
// Enable the declaration even if the compiler doesn't support the language
190
// feature.
191
struct destroying_delete_t {
192
  explicit destroying_delete_t() = default;
193
};
194
inline constexpr destroying_delete_t destroying_delete{};
195
#endif // _LIBCPP_STD_VER >= 20
196
197
} // namespace std
198
199
#if defined(_LIBCPP_CXX03_LANG)
200
#  define _THROW_BAD_ALLOC throw(std::bad_alloc)
201
#else
202
#  define _THROW_BAD_ALLOC
203
#endif
204
205
#if !defined(_LIBCPP_ABI_VCRUNTIME)
206
207
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
208
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
209
operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
210
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
211
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
212
#  ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
213
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
214
#  endif
215
216
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
217
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
218
operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
219
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
220
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
221
#  ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
222
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
223
#  endif
224
225
#  ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
226
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
227
operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
228
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
229
operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
230
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
231
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
232
#    ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
233
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
234
#    endif
235
236
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
237
operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
238
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void*
239
operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
240
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
241
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
242
#    ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
243
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
244
#    endif
245
#  endif
246
247
0
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new(std::size_t, void* __p) _NOEXCEPT {
248
0
  return __p;
249
0
}
250
0
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new[](std::size_t, void* __p) _NOEXCEPT {
251
0
  return __p;
252
0
}
253
0
inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}
254
0
inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}
255
256
#endif // !_LIBCPP_ABI_VCRUNTIME
257
258
_LIBCPP_BEGIN_NAMESPACE_STD
259
260
12.9M
_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
261
12.9M
#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
262
12.9M
  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
263
#else
264
  return __align > _LIBCPP_ALIGNOF(max_align_t);
265
#endif
266
12.9M
}
267
268
template <class... _Args>
269
6.47M
_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
270
6.47M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
271
6.47M
  return __builtin_operator_new(__args...);
272
#else
273
  return ::operator new(__args...);
274
#endif
275
6.47M
}
Unexecuted instantiation: void* std::__1::__libcpp_operator_new[abi:ne180100]<unsigned long, std::align_val_t>(unsigned long, std::align_val_t)
void* std::__1::__libcpp_operator_new[abi:ne180100]<unsigned long>(unsigned long)
Line
Count
Source
269
6.47M
_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
270
6.47M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
271
6.47M
  return __builtin_operator_new(__args...);
272
#else
273
  return ::operator new(__args...);
274
#endif
275
6.47M
}
276
277
template <class... _Args>
278
6.48M
_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) {
279
6.48M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
280
6.48M
  __builtin_operator_delete(__args...);
281
#else
282
  ::operator delete(__args...);
283
#endif
284
6.48M
}
Unexecuted instantiation: void std::__1::__libcpp_operator_delete[abi:ne180100]<void*, std::align_val_t>(void*, std::align_val_t)
void std::__1::__libcpp_operator_delete[abi:ne180100]<void*>(void*)
Line
Count
Source
278
6.48M
_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) {
279
6.48M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
280
6.48M
  __builtin_operator_delete(__args...);
281
#else
282
  ::operator delete(__args...);
283
#endif
284
6.48M
}
285
286
6.47M
inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) {
287
6.47M
#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
288
6.47M
  if (__is_overaligned_for_new(__align)) {
289
0
    const align_val_t __align_val = static_cast<align_val_t>(__align);
290
0
    return __libcpp_operator_new(__size, __align_val);
291
0
  }
292
6.47M
#endif
293
294
6.47M
  (void)__align;
295
6.47M
  return __libcpp_operator_new(__size);
296
6.47M
}
297
298
template <class... _Args>
299
6.48M
_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) {
300
6.48M
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
301
6.48M
  (void)__size;
302
6.48M
  return std::__libcpp_operator_delete(__ptr, __args...);
303
#else
304
  return std::__libcpp_operator_delete(__ptr, __size, __args...);
305
#endif
306
6.48M
}
Unexecuted instantiation: void std::__1::__do_deallocate_handle_size[abi:ne180100]<std::align_val_t>(void*, unsigned long, std::align_val_t)
void std::__1::__do_deallocate_handle_size[abi:ne180100]<>(void*, unsigned long)
Line
Count
Source
299
6.48M
_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) {
300
6.48M
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
301
6.48M
  (void)__size;
302
6.48M
  return std::__libcpp_operator_delete(__ptr, __args...);
303
#else
304
  return std::__libcpp_operator_delete(__ptr, __size, __args...);
305
#endif
306
6.48M
}
307
308
6.48M
inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
309
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
310
  (void)__align;
311
  return __do_deallocate_handle_size(__ptr, __size);
312
#else
313
6.48M
  if (__is_overaligned_for_new(__align)) {
314
0
    const align_val_t __align_val = static_cast<align_val_t>(__align);
315
0
    return __do_deallocate_handle_size(__ptr, __size, __align_val);
316
6.48M
  } else {
317
6.48M
    return __do_deallocate_handle_size(__ptr, __size);
318
6.48M
  }
319
6.48M
#endif
320
6.48M
}
321
322
0
inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
323
0
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
324
0
  (void)__align;
325
0
  return __libcpp_operator_delete(__ptr);
326
0
#else
327
0
  if (__is_overaligned_for_new(__align)) {
328
0
    const align_val_t __align_val = static_cast<align_val_t>(__align);
329
0
    return __libcpp_operator_delete(__ptr, __align_val);
330
0
  } else {
331
0
    return __libcpp_operator_delete(__ptr);
332
0
  }
333
0
#endif
334
0
}
335
336
template <class _Tp>
337
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT {
338
  static_assert(!(is_function<_Tp>::value), "can't launder functions");
339
  static_assert(!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void");
340
  return __builtin_launder(__p);
341
}
342
343
#if _LIBCPP_STD_VER >= 17
344
template <class _Tp>
345
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept {
346
  return std::__launder(__p);
347
}
348
#endif
349
350
#if _LIBCPP_STD_VER >= 17
351
352
#  if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
353
354
inline constexpr size_t hardware_destructive_interference_size  = __GCC_DESTRUCTIVE_SIZE;
355
inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
356
357
#  endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
358
359
#endif // _LIBCPP_STD_VER >= 17
360
361
_LIBCPP_END_NAMESPACE_STD
362
363
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
364
#  include <cstdlib>
365
#  include <type_traits>
366
#endif
367
368
#endif // _LIBCPP_NEW