xref: /llvm-project-15.0.7/libcxx/include/new (revision 189e52fc)
1// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_NEW
12#define _LIBCPP_NEW
13
14/*
15    new synopsis
16
17namespace std
18{
19
20class bad_alloc
21    : public exception
22{
23public:
24    bad_alloc() noexcept;
25    bad_alloc(const bad_alloc&) noexcept;
26    bad_alloc& operator=(const bad_alloc&) noexcept;
27    virtual const char* what() const noexcept;
28};
29
30class bad_array_length : public bad_alloc // C++14
31{
32public:
33    bad_array_length() noexcept;
34};
35
36class bad_array_new_length : public bad_alloc
37{
38public:
39    bad_array_new_length() noexcept;
40};
41
42struct nothrow_t {};
43extern const nothrow_t nothrow;
44typedef void (*new_handler)();
45new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
47
48}  // std
49
50void* operator new(std::size_t size);                                   // replaceable
51void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable
52void  operator delete(void* ptr) noexcept;                              // replaceable
53void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
54void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
55
56void* operator new[](std::size_t size);                                 // replaceable
57void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
58void  operator delete[](void* ptr) noexcept;                            // replaceable
59void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
60void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
61
62void* operator new  (std::size_t size, void* ptr) noexcept;
63void* operator new[](std::size_t size, void* ptr) noexcept;
64void  operator delete  (void* ptr, void*) noexcept;
65void  operator delete[](void* ptr, void*) noexcept;
66
67*/
68
69#include <__config>
70#include <exception>
71#include <cstddef>
72#ifdef _LIBCPP_NO_EXCEPTIONS
73#include <cstdlib>
74#endif
75
76#include <__undef___deallocate>
77
78#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
79#pragma GCC system_header
80#endif
81
82namespace std  // purposefully not using versioning namespace
83{
84
85class _LIBCPP_EXCEPTION_ABI bad_alloc
86    : public exception
87{
88public:
89    bad_alloc() _NOEXCEPT;
90    virtual ~bad_alloc() _NOEXCEPT;
91    virtual const char* what() const _NOEXCEPT;
92};
93
94class _LIBCPP_EXCEPTION_ABI bad_array_new_length
95    : public bad_alloc
96{
97public:
98    bad_array_new_length() _NOEXCEPT;
99    virtual ~bad_array_new_length() _NOEXCEPT;
100    virtual const char* what() const _NOEXCEPT;
101};
102
103_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
104
105#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
106
107class _LIBCPP_EXCEPTION_ABI bad_array_length
108    : public bad_alloc
109{
110public:
111    bad_array_length() _NOEXCEPT;
112    virtual ~bad_array_length() _NOEXCEPT;
113    virtual const char* what() const _NOEXCEPT;
114};
115
116#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
117
118#endif  // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
119
120struct _LIBCPP_TYPE_VIS nothrow_t {};
121extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
122typedef void (*new_handler)();
123_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
124_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
125
126}  // std
127
128#if defined(_LIBCPP_MSVCRT) && !defined(_LIBCPP_BUILDING_LIBRARY)
129# define _LIBCPP_NEW_DELETE_VIS
130#else
131# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
132#endif
133
134_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
135#if !__has_feature(cxx_noexcept)
136    throw(std::bad_alloc)
137#endif
138;
139_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
140_LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p) _NOEXCEPT;
141_LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
142#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
143    (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
144_LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
145#endif
146
147_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz)
148#if !__has_feature(cxx_noexcept)
149    throw(std::bad_alloc)
150#endif
151;
152_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
153_LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p) _NOEXCEPT;
154_LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
155#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
156    (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
157_LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
158#endif
159
160inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
161inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
162inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
163inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
164
165_LIBCPP_BEGIN_NAMESPACE_STD
166
167inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
168#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
169  return ::operator new(__size);
170#else
171  return __builtin_operator_new(__size);
172#endif
173}
174
175inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) {
176#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
177  ::operator delete(__ptr);
178#else
179  __builtin_operator_delete(__ptr);
180#endif
181}
182
183#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
184_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
185void __throw_bad_array_length()
186{
187#ifndef _LIBCPP_NO_EXCEPTIONS
188    throw bad_array_length();
189#else
190	_VSTD::abort();
191#endif
192}
193#endif
194
195_LIBCPP_END_NAMESPACE_STD
196
197#endif  // _LIBCPP_NEW
198