blob: 6589f16b7a16bf720f2c25c3090889bacf165e0e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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:
Howard Hinnanted569212011-05-26 18:23:59 +000024 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028};
29
Eric Fiselier9acbffa2016-10-14 06:46:30 +000030class bad_array_length : public bad_alloc // FIXME: Not part of C++
Marshall Clow7f9f52e2013-09-11 01:38:42 +000031{
32public:
33 bad_array_length() noexcept;
34};
35
Eric Fiselier9acbffa2016-10-14 06:46:30 +000036class bad_array_new_length : public bad_alloc // C++14
Marshall Clow7f9f52e2013-09-11 01:38:42 +000037{
38public:
39 bad_array_new_length() noexcept;
40};
41
Eric Fiselier9acbffa2016-10-14 06:46:30 +000042enum class align_val_t : size_t {}; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043struct nothrow_t {};
44extern const nothrow_t nothrow;
45typedef void (*new_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +000046new_handler set_new_handler(new_handler new_p) noexcept;
47new_handler get_new_handler() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048
49} // std
50
Howard Hinnanted569212011-05-26 18:23:59 +000051void* operator new(std::size_t size); // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000052void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000053void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000054void* operator new(std::size_t size, std::align_val_t alignment,
55 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000056void operator delete(void* ptr) noexcept; // replaceable
Larisse Voufo19efe012015-02-15 05:18:55 +000057void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier9acbffa2016-10-14 06:46:30 +000058void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
59void operator delete(void* ptr, std::size_t size,
60 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000061void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000062void operator delete(void* ptr, std:align_val_t alignment,
63 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064
Howard Hinnanted569212011-05-26 18:23:59 +000065void* operator new[](std::size_t size); // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000066void* operator new[](std::size_t size,
67 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000068void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000069void* operator new[](std::size_t size, std::align_val_t alignment,
70 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000071void operator delete[](void* ptr) noexcept; // replaceable
Larisse Voufo19efe012015-02-15 05:18:55 +000072void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier9acbffa2016-10-14 06:46:30 +000073void operator delete[](void* ptr,
74 std::align_val_t alignment) noexcept; // replaceable, C++17
75void operator delete[](void* ptr, std::size_t size,
76 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:59 +000077void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:30 +000078void operator delete[](void* ptr, std::align_val_t alignment,
79 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080
Howard Hinnanted569212011-05-26 18:23:59 +000081void* operator new (std::size_t size, void* ptr) noexcept;
82void* operator new[](std::size_t size, void* ptr) noexcept;
83void operator delete (void* ptr, void*) noexcept;
84void operator delete[](void* ptr, void*) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86*/
87
88#include <__config>
89#include <exception>
90#include <cstddef>
Eric Fiselierdbf60fa2016-09-06 21:25:27 +000091#ifdef _LIBCPP_NO_EXCEPTIONS
92#include <cstdlib>
93#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094
Howard Hinnant08e17472011-10-17 20:05:10 +000095#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000097#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
Eric Fiselier9acbffa2016-10-14 06:46:30 +000099#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_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \
105 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606))
106# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
107#endif
108
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109namespace std // purposefully not using versioning namespace
110{
111
112class _LIBCPP_EXCEPTION_ABI bad_alloc
113 : public exception
114{
115public:
Howard Hinnanted569212011-05-26 18:23:59 +0000116 bad_alloc() _NOEXCEPT;
117 virtual ~bad_alloc() _NOEXCEPT;
118 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119};
120
121class _LIBCPP_EXCEPTION_ABI bad_array_new_length
122 : public bad_alloc
123{
124public:
Howard Hinnanted569212011-05-26 18:23:59 +0000125 bad_array_new_length() _NOEXCEPT;
126 virtual ~bad_array_new_length() _NOEXCEPT;
127 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128};
129
Marshall Clow14c09a22016-08-25 15:09:01 +0000130_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
131
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000132#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant29250b72013-11-07 17:15:51 +0000133
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000134class _LIBCPP_EXCEPTION_ABI bad_array_length
135 : public bad_alloc
136{
137public:
138 bad_array_length() _NOEXCEPT;
139 virtual ~bad_array_length() _NOEXCEPT;
140 virtual const char* what() const _NOEXCEPT;
141};
Howard Hinnant29250b72013-11-07 17:15:51 +0000142
143#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
144
145#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000146
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000147#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
148#ifndef _LIBCPP_CXX03_LANG
149enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
150#else
151enum align_val_t { __zero = 0, __max = (size_t)-1 };
152#endif
153#endif
154
Howard Hinnant83eade62013-03-06 23:30:19 +0000155struct _LIBCPP_TYPE_VIS nothrow_t {};
156extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157typedef void (*new_handler)();
Howard Hinnant83eade62013-03-06 23:30:19 +0000158_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
159_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
161} // std
162
Eric Fiselierb45121d2017-01-02 23:27:42 +0000163#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000164#define _THROW_BAD_ALLOC throw(std::bad_alloc)
165#else
166#define _THROW_BAD_ALLOC
Howard Hinnanted569212011-05-26 18:23:59 +0000167#endif
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000168
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000169_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
170_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
171_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
172_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000173#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000174_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05 +0000175#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000176
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000177_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
178_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
179_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
180_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Shoaib Meenaif66194b2016-11-18 04:31:09 +0000181#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000182_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05 +0000183#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000184
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000185#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000186_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
187_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
188_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
189_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000190#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000192#endif
193
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000194_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
195_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
196_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
197_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000198#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10 +0000199_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30 +0000200#endif
201#endif
202
Howard Hinnant1e564242013-10-04 22:09:00 +0000203inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
204inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
205inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
206inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
Richard Smith73c1fce2014-06-04 19:54:15 +0000208_LIBCPP_BEGIN_NAMESPACE_STD
209
210inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
211#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
212 return ::operator new(__size);
213#else
214 return __builtin_operator_new(__size);
215#endif
216}
217
Eric Fiselier32b19c32017-01-07 03:01:24 +0000218inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) {
Richard Smith73c1fce2014-06-04 19:54:15 +0000219#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
220 ::operator delete(__ptr);
221#else
222 __builtin_operator_delete(__ptr);
223#endif
224}
225
Marshall Clow14c09a22016-08-25 15:09:01 +0000226#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
227_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
228void __throw_bad_array_length()
229{
230#ifndef _LIBCPP_NO_EXCEPTIONS
231 throw bad_array_length();
232#else
233 _VSTD::abort();
234#endif
235}
236#endif
237
Richard Smith73c1fce2014-06-04 19:54:15 +0000238_LIBCPP_END_NAMESPACE_STD
239
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240#endif // _LIBCPP_NEW