Coverage Report

Created: 2024-09-08 06:18

/src/llvm-project-16.0.6.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> constexpr T* launder(T* p) noexcept; // C++17
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 <__type_traits/is_function.h>
93
#include <__type_traits/is_same.h>
94
#include <__type_traits/remove_cv.h>
95
#include <cstddef>
96
#include <cstdlib>
97
#include <exception>
98
#include <version>
99
100
#if defined(_LIBCPP_ABI_VCRUNTIME)
101
#include <new.h>
102
#endif
103
104
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
105
#  pragma GCC system_header
106
#endif
107
108
#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation  < 201309L
109
#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
110
#endif
111
112
#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
113
    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
114
# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
115
#endif
116
117
#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
118
    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
119
# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
120
#endif
121
122
namespace std  // purposefully not using versioning namespace
123
{
124
125
#if !defined(_LIBCPP_ABI_VCRUNTIME)
126
struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
127
extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
128
129
class _LIBCPP_EXCEPTION_ABI bad_alloc
130
    : public exception
131
{
132
public:
133
    bad_alloc() _NOEXCEPT;
134
    ~bad_alloc() _NOEXCEPT override;
135
    const char* what() const _NOEXCEPT override;
136
};
137
138
class _LIBCPP_EXCEPTION_ABI bad_array_new_length
139
    : public bad_alloc
140
{
141
public:
142
    bad_array_new_length() _NOEXCEPT;
143
    ~bad_array_new_length() _NOEXCEPT override;
144
    const char* what() const _NOEXCEPT override;
145
};
146
147
typedef void (*new_handler)();
148
_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
149
_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
150
151
#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
152
153
// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
154
// since they would normally be provided in vcruntime_exception.h
155
class bad_alloc : public exception {
156
public:
157
  bad_alloc() noexcept : exception("bad allocation") {}
158
159
private:
160
  friend class bad_array_new_length;
161
162
  bad_alloc(char const* const __message) noexcept : exception(__message) {}
163
};
164
165
class bad_array_new_length : public bad_alloc {
166
public:
167
  bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
168
};
169
#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
170
171
_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
172
173
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
174
void __throw_bad_array_new_length()
175
0
{
176
0
#ifndef _LIBCPP_NO_EXCEPTIONS
177
0
    throw bad_array_new_length();
178
#else
179
    _VSTD::abort();
180
#endif
181
0
}
182
183
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
184
    !defined(_LIBCPP_ABI_VCRUNTIME)
185
#ifndef _LIBCPP_CXX03_LANG
186
enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
187
#else
188
enum align_val_t { __zero = 0, __max = (size_t)-1 };
189
#endif
190
#endif
191
192
#if _LIBCPP_STD_VER > 17
193
// Enable the declaration even if the compiler doesn't support the language
194
// feature.
195
struct destroying_delete_t {
196
  explicit destroying_delete_t() = default;
197
};
198
inline constexpr destroying_delete_t destroying_delete{};
199
#endif // _LIBCPP_STD_VER > 17
200
201
} // namespace std
202
203
#if defined(_LIBCPP_CXX03_LANG)
204
#define _THROW_BAD_ALLOC throw(std::bad_alloc)
205
#else
206
#define _THROW_BAD_ALLOC
207
#endif
208
209
#if !defined(_LIBCPP_ABI_VCRUNTIME)
210
211
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
212
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
213
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
214
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
215
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
216
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
217
#endif
218
219
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
220
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
221
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
222
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
223
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
224
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
225
#endif
226
227
#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
228
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
229
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 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 _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE 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* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
237
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
238
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
239
_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
240
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
241
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
242
#endif
243
#endif
244
245
0
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
246
0
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
247
0
inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
248
0
inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
249
250
#endif // !_LIBCPP_ABI_VCRUNTIME
251
252
_LIBCPP_BEGIN_NAMESPACE_STD
253
254
11.1M
_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
255
11.1M
#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
256
11.1M
  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
257
#else
258
  return __align > alignment_of<max_align_t>::value;
259
#endif
260
11.1M
}
261
262
template <class ..._Args>
263
_LIBCPP_INLINE_VISIBILITY
264
5.56M
void* __libcpp_operator_new(_Args ...__args) {
265
5.56M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
266
5.56M
  return __builtin_operator_new(__args...);
267
#else
268
  return ::operator new(__args...);
269
#endif
270
5.56M
}
Unexecuted instantiation: void* std::__1::__libcpp_operator_new[abi:v160006]<unsigned long, std::align_val_t>(unsigned long, std::align_val_t)
void* std::__1::__libcpp_operator_new[abi:v160006]<unsigned long>(unsigned long)
Line
Count
Source
264
5.56M
void* __libcpp_operator_new(_Args ...__args) {
265
5.56M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
266
5.56M
  return __builtin_operator_new(__args...);
267
#else
268
  return ::operator new(__args...);
269
#endif
270
5.56M
}
271
272
template <class ..._Args>
273
_LIBCPP_INLINE_VISIBILITY
274
5.57M
void __libcpp_operator_delete(_Args ...__args) {
275
5.57M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
276
5.57M
  __builtin_operator_delete(__args...);
277
#else
278
  ::operator delete(__args...);
279
#endif
280
5.57M
}
Unexecuted instantiation: void std::__1::__libcpp_operator_delete[abi:v160006]<void*, std::align_val_t>(void*, std::align_val_t)
void std::__1::__libcpp_operator_delete[abi:v160006]<void*>(void*)
Line
Count
Source
274
5.57M
void __libcpp_operator_delete(_Args ...__args) {
275
5.57M
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
276
5.57M
  __builtin_operator_delete(__args...);
277
#else
278
  ::operator delete(__args...);
279
#endif
280
5.57M
}
281
282
inline _LIBCPP_INLINE_VISIBILITY
283
5.56M
void *__libcpp_allocate(size_t __size, size_t __align) {
284
5.56M
#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
285
5.56M
  if (__is_overaligned_for_new(__align)) {
286
0
    const align_val_t __align_val = static_cast<align_val_t>(__align);
287
0
    return __libcpp_operator_new(__size, __align_val);
288
0
  }
289
5.56M
#endif
290
291
5.56M
  (void)__align;
292
5.56M
  return __libcpp_operator_new(__size);
293
5.56M
}
294
295
template <class ..._Args>
296
_LIBCPP_INLINE_VISIBILITY
297
5.57M
void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
298
5.57M
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
299
5.57M
  (void)__size;
300
5.57M
  return std::__libcpp_operator_delete(__ptr, __args...);
301
#else
302
  return std::__libcpp_operator_delete(__ptr, __size, __args...);
303
#endif
304
5.57M
}
Unexecuted instantiation: void std::__1::__do_deallocate_handle_size[abi:v160006]<std::align_val_t>(void*, unsigned long, std::align_val_t)
void std::__1::__do_deallocate_handle_size[abi:v160006]<>(void*, unsigned long)
Line
Count
Source
297
5.57M
void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
298
5.57M
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
299
5.57M
  (void)__size;
300
5.57M
  return std::__libcpp_operator_delete(__ptr, __args...);
301
#else
302
  return std::__libcpp_operator_delete(__ptr, __size, __args...);
303
#endif
304
5.57M
}
305
306
inline _LIBCPP_INLINE_VISIBILITY
307
5.57M
void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
308
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
309
    (void)__align;
310
    return __do_deallocate_handle_size(__ptr, __size);
311
#else
312
5.57M
    if (__is_overaligned_for_new(__align)) {
313
0
      const align_val_t __align_val = static_cast<align_val_t>(__align);
314
0
      return __do_deallocate_handle_size(__ptr, __size, __align_val);
315
5.57M
    } else {
316
5.57M
      return __do_deallocate_handle_size(__ptr, __size);
317
5.57M
    }
318
5.57M
#endif
319
5.57M
}
320
321
0
inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
322
0
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
323
0
    (void)__align;
324
0
    return __libcpp_operator_delete(__ptr);
325
0
#else
326
0
    if (__is_overaligned_for_new(__align)) {
327
0
      const align_val_t __align_val = static_cast<align_val_t>(__align);
328
0
      return __libcpp_operator_delete(__ptr, __align_val);
329
0
    } else {
330
0
      return __libcpp_operator_delete(__ptr);
331
0
    }
332
0
#endif
333
0
}
334
335
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
336
// Low-level helpers to call the aligned allocation and deallocation functions
337
// on the target platform. This is used to implement libc++'s own memory
338
// allocation routines -- if you need to allocate memory inside the library,
339
// chances are that you want to use `__libcpp_allocate` instead.
340
//
341
// Returns the allocated memory, or `nullptr` on failure.
342
inline _LIBCPP_INLINE_VISIBILITY void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
343
#  if defined(_LIBCPP_MSVCRT_LIKE)
344
    return ::_aligned_malloc(__size, __alignment);
345
#  elif _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC)
346
    // aligned_alloc() requires that __size is a multiple of __alignment,
347
    // but for C++ [new.delete.general], only states "if the value of an
348
    // alignment argument passed to any of these functions is not a valid
349
    // alignment value, the behavior is undefined".
350
    // To handle calls such as ::operator new(1, std::align_val_t(128)), we
351
    // round __size up to the next multiple of __alignment.
352
    size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
353
    // Rounding up could have wrapped around to zero, so we have to add another
354
    // max() ternary to the actual call site to avoid succeeded in that case.
355
    return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
356
#  else
357
    void* __result = nullptr;
358
    (void)::posix_memalign(&__result, __alignment, __size);
359
    // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
360
    return __result;
361
#  endif
362
}
363
364
inline _LIBCPP_INLINE_VISIBILITY
365
void __libcpp_aligned_free(void* __ptr) {
366
#if defined(_LIBCPP_MSVCRT_LIKE)
367
  ::_aligned_free(__ptr);
368
#else
369
  ::free(__ptr);
370
#endif
371
}
372
#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
373
374
375
template <class _Tp>
376
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
377
_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
378
{
379
    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
380
    static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
381
    return __builtin_launder(__p);
382
}
383
384
#if _LIBCPP_STD_VER > 14
385
template <class _Tp>
386
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
387
constexpr _Tp* launder(_Tp* __p) noexcept
388
{
389
    return _VSTD::__launder(__p);
390
}
391
#endif
392
393
#if _LIBCPP_STD_VER > 14
394
395
#if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
396
397
inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
398
inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
399
400
#endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
401
402
#endif // _LIBCPP_STD_VER > 14
403
404
_LIBCPP_END_NAMESPACE_STD
405
406
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
407
#  include <type_traits>
408
#endif
409
410
#endif // _LIBCPP_NEW