xref: /freebsd/contrib/llvm-project/libcxx/include/new (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===----------------------------- new ------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_NEW
110b57cec5SDimitry Andric#define _LIBCPP_NEW
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    new synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricclass bad_alloc
200b57cec5SDimitry Andric    : public exception
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric    bad_alloc() noexcept;
240b57cec5SDimitry Andric    bad_alloc(const bad_alloc&) noexcept;
250b57cec5SDimitry Andric    bad_alloc& operator=(const bad_alloc&) noexcept;
260b57cec5SDimitry Andric    virtual const char* what() const noexcept;
270b57cec5SDimitry Andric};
280b57cec5SDimitry Andric
290b57cec5SDimitry Andricclass bad_array_new_length : public bad_alloc // C++14
300b57cec5SDimitry Andric{
310b57cec5SDimitry Andricpublic:
320b57cec5SDimitry Andric    bad_array_new_length() noexcept;
330b57cec5SDimitry Andric};
340b57cec5SDimitry Andric
350b57cec5SDimitry Andricenum class align_val_t : size_t {}; // C++17
360b57cec5SDimitry Andric
370b57cec5SDimitry Andricstruct destroying_delete_t { // C++20
380b57cec5SDimitry Andric  explicit destroying_delete_t() = default;
390b57cec5SDimitry Andric};
400b57cec5SDimitry Andricinline constexpr destroying_delete_t destroying_delete{}; // C++20
410b57cec5SDimitry Andric
42*e40139ffSDimitry Andricstruct nothrow_t { explicit nothrow_t() = default; };
430b57cec5SDimitry Andricextern const nothrow_t nothrow;
440b57cec5SDimitry Andrictypedef void (*new_handler)();
450b57cec5SDimitry Andricnew_handler set_new_handler(new_handler new_p) noexcept;
460b57cec5SDimitry Andricnew_handler get_new_handler() noexcept;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric// 21.6.4, pointer optimization barrier
490b57cec5SDimitry Andrictemplate <class T> constexpr T* launder(T* p) noexcept; // C++17
500b57cec5SDimitry Andric}  // std
510b57cec5SDimitry Andric
520b57cec5SDimitry Andricvoid* operator new(std::size_t size);                                   // replaceable, nodiscard in C++2a
530b57cec5SDimitry Andricvoid* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17, nodiscard in C++2a
540b57cec5SDimitry Andricvoid* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable, nodiscard in C++2a
550b57cec5SDimitry Andricvoid* operator new(std::size_t size, std::align_val_t alignment,
560b57cec5SDimitry Andric                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++2a
570b57cec5SDimitry Andricvoid  operator delete(void* ptr) noexcept;                              // replaceable
580b57cec5SDimitry Andricvoid  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
590b57cec5SDimitry Andricvoid  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
600b57cec5SDimitry Andricvoid  operator delete(void* ptr, std::size_t size,
610b57cec5SDimitry Andric                      std::align_val_t alignment) noexcept;             // replaceable, C++17
620b57cec5SDimitry Andricvoid  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
630b57cec5SDimitry Andricvoid  operator delete(void* ptr, std:align_val_t alignment,
640b57cec5SDimitry Andric                      const std::nothrow_t&) noexcept;                  // replaceable, C++17
650b57cec5SDimitry Andric
660b57cec5SDimitry Andricvoid* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++2a
670b57cec5SDimitry Andricvoid* operator new[](std::size_t size,
680b57cec5SDimitry Andric                     std::align_val_t alignment) noexcept;              // replaceable, C++17, nodiscard in C++2a
690b57cec5SDimitry Andricvoid* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
700b57cec5SDimitry Andricvoid* operator new[](std::size_t size, std::align_val_t alignment,
710b57cec5SDimitry Andric                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++2a
720b57cec5SDimitry Andricvoid  operator delete[](void* ptr) noexcept;                            // replaceable
730b57cec5SDimitry Andricvoid  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
740b57cec5SDimitry Andricvoid  operator delete[](void* ptr,
750b57cec5SDimitry Andric                        std::align_val_t alignment) noexcept;           // replaceable, C++17
760b57cec5SDimitry Andricvoid  operator delete[](void* ptr, std::size_t size,
770b57cec5SDimitry Andric                        std::align_val_t alignment) noexcept;           // replaceable, C++17
780b57cec5SDimitry Andricvoid  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
790b57cec5SDimitry Andricvoid  operator delete[](void* ptr, std::align_val_t alignment,
800b57cec5SDimitry Andric                        const std::nothrow_t&) noexcept;                // replaceable, C++17
810b57cec5SDimitry Andric
820b57cec5SDimitry Andricvoid* operator new  (std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
830b57cec5SDimitry Andricvoid* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
840b57cec5SDimitry Andricvoid  operator delete  (void* ptr, void*) noexcept;
850b57cec5SDimitry Andricvoid  operator delete[](void* ptr, void*) noexcept;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric*/
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric#include <__config>
900b57cec5SDimitry Andric#include <exception>
910b57cec5SDimitry Andric#include <type_traits>
920b57cec5SDimitry Andric#include <cstddef>
930b57cec5SDimitry Andric#include <version>
940b57cec5SDimitry Andric#ifdef _LIBCPP_NO_EXCEPTIONS
950b57cec5SDimitry Andric#include <cstdlib>
960b57cec5SDimitry Andric#endif
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric#if defined(_LIBCPP_ABI_VCRUNTIME)
990b57cec5SDimitry Andric#include <new.h>
1000b57cec5SDimitry Andric#endif
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1030b57cec5SDimitry Andric#pragma GCC system_header
1040b57cec5SDimitry Andric#endif
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation  < 201309L
1070b57cec5SDimitry Andric#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
1080b57cec5SDimitry Andric#endif
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
1110b57cec5SDimitry Andric    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
1120b57cec5SDimitry Andric# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
1130b57cec5SDimitry Andric#endif
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
1160b57cec5SDimitry Andric    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
1170b57cec5SDimitry Andric# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
1180b57cec5SDimitry Andric#endif
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric#if !__has_builtin(__builtin_operator_new) || \
1210b57cec5SDimitry Andric   __has_builtin(__builtin_operator_new) < 201802L
1220b57cec5SDimitry Andric#define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
1230b57cec5SDimitry Andric#endif
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andricnamespace std  // purposefully not using versioning namespace
1260b57cec5SDimitry Andric{
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric#if !defined(_LIBCPP_ABI_VCRUNTIME)
129*e40139ffSDimitry Andricstruct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
1300b57cec5SDimitry Andricextern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI bad_alloc
1330b57cec5SDimitry Andric    : public exception
1340b57cec5SDimitry Andric{
1350b57cec5SDimitry Andricpublic:
1360b57cec5SDimitry Andric    bad_alloc() _NOEXCEPT;
1370b57cec5SDimitry Andric    virtual ~bad_alloc() _NOEXCEPT;
1380b57cec5SDimitry Andric    virtual const char* what() const _NOEXCEPT;
1390b57cec5SDimitry Andric};
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI bad_array_new_length
1420b57cec5SDimitry Andric    : public bad_alloc
1430b57cec5SDimitry Andric{
1440b57cec5SDimitry Andricpublic:
1450b57cec5SDimitry Andric    bad_array_new_length() _NOEXCEPT;
1460b57cec5SDimitry Andric    virtual ~bad_array_new_length() _NOEXCEPT;
1470b57cec5SDimitry Andric    virtual const char* what() const _NOEXCEPT;
1480b57cec5SDimitry Andric};
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andrictypedef void (*new_handler)();
1510b57cec5SDimitry Andric_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
1520b57cec5SDimitry Andric_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric#endif // !_LIBCPP_ABI_VCRUNTIME
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
1590b57cec5SDimitry Andric    !defined(_LIBCPP_ABI_VCRUNTIME)
1600b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1610b57cec5SDimitry Andricenum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
1620b57cec5SDimitry Andric#else
1630b57cec5SDimitry Andricenum align_val_t { __zero = 0, __max = (size_t)-1 };
1640b57cec5SDimitry Andric#endif
1650b57cec5SDimitry Andric#endif
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
1680b57cec5SDimitry Andric// Enable the declaration even if the compiler doesn't support the language
1690b57cec5SDimitry Andric// feature.
1700b57cec5SDimitry Andricstruct destroying_delete_t {
1710b57cec5SDimitry Andric  explicit destroying_delete_t() = default;
1720b57cec5SDimitry Andric};
1730b57cec5SDimitry Andric_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
1740b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 17
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric}  // std
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric#if defined(_LIBCPP_CXX03_LANG)
1790b57cec5SDimitry Andric#define _THROW_BAD_ALLOC throw(std::bad_alloc)
1800b57cec5SDimitry Andric#else
1810b57cec5SDimitry Andric#define _THROW_BAD_ALLOC
1820b57cec5SDimitry Andric#endif
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric#if !defined(_LIBCPP_ABI_VCRUNTIME)
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
1870b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
1880b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
1890b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
1900b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
1910b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
1920b57cec5SDimitry Andric#endif
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
1950b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
1960b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
1970b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
1980b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
1990b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
2000b57cec5SDimitry Andric#endif
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
2030b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
2040b57cec5SDimitry Andric_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;
2050b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
2060b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
2070b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
2080b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
2090b57cec5SDimitry Andric#endif
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
2120b57cec5SDimitry Andric_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;
2130b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
2140b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
2150b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
2160b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
2170b57cec5SDimitry Andric#endif
2180b57cec5SDimitry Andric#endif
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
2210b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
2220b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
2230b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric#endif // !_LIBCPP_ABI_VCRUNTIME
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
2300b57cec5SDimitry Andric#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
2310b57cec5SDimitry Andric  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
2320b57cec5SDimitry Andric#else
2330b57cec5SDimitry Andric  return __align > alignment_of<max_align_t>::value;
2340b57cec5SDimitry Andric#endif
2350b57cec5SDimitry Andric}
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
2380b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
2390b57cec5SDimitry Andric  if (__is_overaligned_for_new(__align)) {
2400b57cec5SDimitry Andric    const align_val_t __align_val = static_cast<align_val_t>(__align);
2410b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
2420b57cec5SDimitry Andric    return ::operator new(__size, __align_val);
2430b57cec5SDimitry Andric# else
2440b57cec5SDimitry Andric    return __builtin_operator_new(__size, __align_val);
2450b57cec5SDimitry Andric# endif
2460b57cec5SDimitry Andric  }
2470b57cec5SDimitry Andric#else
2480b57cec5SDimitry Andric  ((void)__align);
2490b57cec5SDimitry Andric#endif
2500b57cec5SDimitry Andric#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
2510b57cec5SDimitry Andric  return ::operator new(__size);
2520b57cec5SDimitry Andric#else
2530b57cec5SDimitry Andric  return __builtin_operator_new(__size);
2540b57cec5SDimitry Andric#endif
2550b57cec5SDimitry Andric}
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andricstruct _DeallocateCaller {
2580b57cec5SDimitry Andric  static inline _LIBCPP_INLINE_VISIBILITY
2590b57cec5SDimitry Andric  void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) {
2600b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
2610b57cec5SDimitry Andric    ((void)__align);
2620b57cec5SDimitry Andric    return __do_deallocate_handle_size(__ptr, __size);
2630b57cec5SDimitry Andric#else
2640b57cec5SDimitry Andric    if (__is_overaligned_for_new(__align)) {
2650b57cec5SDimitry Andric      const align_val_t __align_val = static_cast<align_val_t>(__align);
2660b57cec5SDimitry Andric      return __do_deallocate_handle_size(__ptr, __size, __align_val);
2670b57cec5SDimitry Andric    } else {
2680b57cec5SDimitry Andric      return __do_deallocate_handle_size(__ptr, __size);
2690b57cec5SDimitry Andric    }
2700b57cec5SDimitry Andric#endif
2710b57cec5SDimitry Andric  }
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andric  static inline _LIBCPP_INLINE_VISIBILITY
2740b57cec5SDimitry Andric  void __do_deallocate_handle_align(void *__ptr, size_t __align) {
2750b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
2760b57cec5SDimitry Andric    ((void)__align);
2770b57cec5SDimitry Andric    return __do_call(__ptr);
2780b57cec5SDimitry Andric#else
2790b57cec5SDimitry Andric    if (__is_overaligned_for_new(__align)) {
2800b57cec5SDimitry Andric      const align_val_t __align_val = static_cast<align_val_t>(__align);
2810b57cec5SDimitry Andric      return __do_call(__ptr, __align_val);
2820b57cec5SDimitry Andric    } else {
2830b57cec5SDimitry Andric      return __do_call(__ptr);
2840b57cec5SDimitry Andric    }
2850b57cec5SDimitry Andric#endif
2860b57cec5SDimitry Andric  }
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric private:
2890b57cec5SDimitry Andric  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) {
2900b57cec5SDimitry Andric#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
2910b57cec5SDimitry Andric    ((void)__size);
2920b57cec5SDimitry Andric    return __do_call(__ptr);
2930b57cec5SDimitry Andric#else
2940b57cec5SDimitry Andric    return __do_call(__ptr, __size);
2950b57cec5SDimitry Andric#endif
2960b57cec5SDimitry Andric  }
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
2990b57cec5SDimitry Andric  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) {
3000b57cec5SDimitry Andric#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
3010b57cec5SDimitry Andric    ((void)__size);
3020b57cec5SDimitry Andric    return __do_call(__ptr, __align);
3030b57cec5SDimitry Andric#else
3040b57cec5SDimitry Andric    return __do_call(__ptr, __size, __align);
3050b57cec5SDimitry Andric#endif
3060b57cec5SDimitry Andric  }
3070b57cec5SDimitry Andric#endif
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andricprivate:
3100b57cec5SDimitry Andric  template <class _A1, class _A2>
3110b57cec5SDimitry Andric  static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) {
3120b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
3130b57cec5SDimitry Andric    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
3140b57cec5SDimitry Andric    return ::operator delete(__ptr, __a1, __a2);
3150b57cec5SDimitry Andric#else
3160b57cec5SDimitry Andric    return __builtin_operator_delete(__ptr, __a1, __a2);
3170b57cec5SDimitry Andric#endif
3180b57cec5SDimitry Andric  }
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric  template <class _A1>
3210b57cec5SDimitry Andric  static inline void __do_call(void *__ptr, _A1 __a1) {
3220b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
3230b57cec5SDimitry Andric    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
3240b57cec5SDimitry Andric    return ::operator delete(__ptr, __a1);
3250b57cec5SDimitry Andric#else
3260b57cec5SDimitry Andric    return __builtin_operator_delete(__ptr, __a1);
3270b57cec5SDimitry Andric#endif
3280b57cec5SDimitry Andric  }
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andric  static inline void __do_call(void *__ptr) {
3310b57cec5SDimitry Andric#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
3320b57cec5SDimitry Andric    return ::operator delete(__ptr);
3330b57cec5SDimitry Andric#else
3340b57cec5SDimitry Andric    return __builtin_operator_delete(__ptr);
3350b57cec5SDimitry Andric#endif
3360b57cec5SDimitry Andric  }
3370b57cec5SDimitry Andric};
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
3400b57cec5SDimitry Andric  _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align);
3410b57cec5SDimitry Andric}
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
3440b57cec5SDimitry Andric  _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align);
3450b57cec5SDimitry Andric}
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andrictemplate <class _Tp>
3480b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline
3490b57cec5SDimitry Andric_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
3500b57cec5SDimitry Andric{
3510b57cec5SDimitry Andric    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
3520b57cec5SDimitry Andric    static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
3530b57cec5SDimitry Andric#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
3540b57cec5SDimitry Andric    return __builtin_launder(__p);
3550b57cec5SDimitry Andric#else
3560b57cec5SDimitry Andric    return __p;
3570b57cec5SDimitry Andric#endif
3580b57cec5SDimitry Andric}
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
3620b57cec5SDimitry Andrictemplate <class _Tp>
3630b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
3640b57cec5SDimitry Andricconstexpr _Tp* launder(_Tp* __p) noexcept
3650b57cec5SDimitry Andric{
3660b57cec5SDimitry Andric    return _VSTD::__launder(__p);
3670b57cec5SDimitry Andric}
3680b57cec5SDimitry Andric#endif
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric#endif  // _LIBCPP_NEW
373