]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/new
Merge llvm trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / new
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
17 namespace std
18 {
19
20 class bad_alloc
21     : public exception
22 {
23 public:
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
30 class bad_array_length : public bad_alloc // FIXME: Not part of C++
31 {
32 public:
33     bad_array_length() noexcept;
34 };
35
36 class bad_array_new_length : public bad_alloc // C++14
37 {
38 public:
39     bad_array_new_length() noexcept;
40 };
41
42 enum class align_val_t : size_t {}; // C++17
43 struct nothrow_t {};
44 extern const nothrow_t nothrow;
45 typedef void (*new_handler)();
46 new_handler set_new_handler(new_handler new_p) noexcept;
47 new_handler get_new_handler() noexcept;
48
49 }  // std
50
51 void* operator new(std::size_t size);                                   // replaceable
52 void* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17
53 void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable
54 void* operator new(std::size_t size, std::align_val_t alignment,
55                    const std::nothrow_t&) noexcept;                     // replaceable, C++17
56 void  operator delete(void* ptr) noexcept;                              // replaceable
57 void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
58 void  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
59 void  operator delete(void* ptr, std::size_t size,
60                       std::align_val_t alignment) noexcept;             // replaceable, C++17
61 void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
62 void  operator delete(void* ptr, std:align_val_t alignment,
63                       const std::nothrow_t&) noexcept;                  // replaceable, C++17
64
65 void* operator new[](std::size_t size);                                 // replaceable
66 void* operator new[](std::size_t size,
67                      std::align_val_t alignment) noexcept;              // replaceable, C++17
68 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
69 void* operator new[](std::size_t size, std::align_val_t alignment,
70                      const std::nothrow_t&) noexcept;                   // replaceable, C++17
71 void  operator delete[](void* ptr) noexcept;                            // replaceable
72 void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
73 void  operator delete[](void* ptr,
74                         std::align_val_t alignment) noexcept;           // replaceable, C++17
75 void  operator delete[](void* ptr, std::size_t size,
76                         std::align_val_t alignment) noexcept;           // replaceable, C++17
77 void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
78 void  operator delete[](void* ptr, std::align_val_t alignment,
79                         const std::nothrow_t&) noexcept;                // replaceable, C++17
80
81 void* operator new  (std::size_t size, void* ptr) noexcept;
82 void* operator new[](std::size_t size, void* ptr) noexcept;
83 void  operator delete  (void* ptr, void*) noexcept;
84 void  operator delete[](void* ptr, void*) noexcept;
85
86 */
87
88 #include <__config>
89 #include <exception>
90 #include <cstddef>
91 #ifdef _LIBCPP_NO_EXCEPTIONS
92 #include <cstdlib>
93 #endif
94
95 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
96 #pragma GCC system_header
97 #endif
98
99 #if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
100     (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309))
101 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
102 #endif
103
104 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) && \
105     (!(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \
106     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606)))
107 # define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
108 #endif
109
110 namespace std  // purposefully not using versioning namespace
111 {
112
113 class _LIBCPP_EXCEPTION_ABI bad_alloc
114     : public exception
115 {
116 public:
117     bad_alloc() _NOEXCEPT;
118     virtual ~bad_alloc() _NOEXCEPT;
119     virtual const char* what() const _NOEXCEPT;
120 };
121
122 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
123     : public bad_alloc
124 {
125 public:
126     bad_array_new_length() _NOEXCEPT;
127     virtual ~bad_array_new_length() _NOEXCEPT;
128     virtual const char* what() const _NOEXCEPT;
129 };
130
131 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
132
133 #if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
134
135 class _LIBCPP_EXCEPTION_ABI bad_array_length
136     : public bad_alloc
137 {
138 public:
139     bad_array_length() _NOEXCEPT;
140     virtual ~bad_array_length() _NOEXCEPT;
141     virtual const char* what() const _NOEXCEPT;
142 };
143
144 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
145
146 #endif  // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
147
148 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14
149 #ifndef _LIBCPP_CXX03_LANG
150 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
151 #else
152 enum align_val_t { __zero = 0, __max = (size_t)-1 };
153 #endif
154 #endif
155
156 struct _LIBCPP_TYPE_VIS nothrow_t {};
157 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
158 typedef void (*new_handler)();
159 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
160 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
161
162 }  // std
163
164 #if defined(_LIBCPP_CXX03_LANG)
165 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
166 #else
167 #define _THROW_BAD_ALLOC
168 #endif
169
170 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
171 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
172 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
173 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
174 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
175 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
176 #endif
177
178 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
179 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
180 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
181 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
182 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
183 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
184 #endif
185
186 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
187 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
188 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
189 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
190 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
191 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
192 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
193 #endif
194
195 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
196 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
197 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
198 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
199 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
200 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
201 #endif
202 #endif
203
204 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
205 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
206 inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
207 inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
208
209 _LIBCPP_BEGIN_NAMESPACE_STD
210
211 inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
212 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
213   return ::operator new(__size);
214 #else
215   return __builtin_operator_new(__size);
216 #endif
217 }
218
219 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) {
220 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
221   ::operator delete(__ptr);
222 #else
223   __builtin_operator_delete(__ptr);
224 #endif
225 }
226
227 #ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
228 _LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
229 void __throw_bad_array_length()
230 {
231 #ifndef _LIBCPP_NO_EXCEPTIONS
232     throw bad_array_length();
233 #else
234         _VSTD::abort();
235 #endif
236 }
237 #endif
238
239 _LIBCPP_END_NAMESPACE_STD
240
241 #endif  // _LIBCPP_NEW