blob: c42ea0838afb3323a30b6d8b8206ff721dc28512 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant1bc52cf2011-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 Hinnantc51e1022010-05-11 19:42:16 +000028};
29
Eric Fiselier162922f2016-10-14 06:46:30 +000030class bad_array_length : public bad_alloc // FIXME: Not part of C++
Marshall Clow3bf77132013-09-11 01:38:42 +000031{
32public:
33 bad_array_length() noexcept;
34};
35
Eric Fiselier162922f2016-10-14 06:46:30 +000036class bad_array_new_length : public bad_alloc // C++14
Marshall Clow3bf77132013-09-11 01:38:42 +000037{
38public:
39 bad_array_new_length() noexcept;
40};
41
Eric Fiselier162922f2016-10-14 06:46:30 +000042enum class align_val_t : size_t {}; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000043struct nothrow_t {};
44extern const nothrow_t nothrow;
45typedef void (*new_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000046new_handler set_new_handler(new_handler new_p) noexcept;
47new_handler get_new_handler() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
49} // std
50
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000051void* operator new(std::size_t size); // replaceable
Eric Fiselier162922f2016-10-14 06:46:30 +000052void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000053void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-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 Hinnant1bc52cf2011-05-26 18:23:59 +000056void operator delete(void* ptr) noexcept; // replaceable
Larisse Voufo574190d2015-02-15 05:18:55 +000057void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier162922f2016-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 Hinnant1bc52cf2011-05-26 18:23:59 +000061void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-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 Hinnantc51e1022010-05-11 19:42:16 +000064
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000065void* operator new[](std::size_t size); // replaceable
Eric Fiselier162922f2016-10-14 06:46:30 +000066void* operator new[](std::size_t size,
67 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000068void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-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 Hinnant1bc52cf2011-05-26 18:23:59 +000071void operator delete[](void* ptr) noexcept; // replaceable
Larisse Voufo574190d2015-02-15 05:18:55 +000072void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier162922f2016-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 Hinnant1bc52cf2011-05-26 18:23:59 +000077void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-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 Hinnantc51e1022010-05-11 19:42:16 +000080
Howard Hinnant1bc52cf2011-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 Hinnantc51e1022010-05-11 19:42:16 +000085
86*/
87
88#include <__config>
89#include <exception>
90#include <cstddef>
Eric Fiselier279c3192016-09-06 21:25:27 +000091#ifdef _LIBCPP_NO_EXCEPTIONS
92#include <cstdlib>
93#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000094
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +000095#include <__undef___deallocate>
96
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000097#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000098#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000099#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100
Eric Fiselier162922f2016-10-14 06:46:30 +0000101#if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
102 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309))
103# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
104#endif
105
106#if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \
107 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606))
108# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
109#endif
110
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111namespace std // purposefully not using versioning namespace
112{
113
114class _LIBCPP_EXCEPTION_ABI bad_alloc
115 : public exception
116{
117public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000118 bad_alloc() _NOEXCEPT;
119 virtual ~bad_alloc() _NOEXCEPT;
120 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121};
122
123class _LIBCPP_EXCEPTION_ABI bad_array_new_length
124 : public bad_alloc
125{
126public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000127 bad_array_new_length() _NOEXCEPT;
128 virtual ~bad_array_new_length() _NOEXCEPT;
129 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130};
131
Marshall Clow8fea1612016-08-25 15:09:01 +0000132_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
133
Marshall Clow3bf77132013-09-11 01:38:42 +0000134#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant4582a2b2013-11-07 17:15:51 +0000135
Marshall Clow3bf77132013-09-11 01:38:42 +0000136class _LIBCPP_EXCEPTION_ABI bad_array_length
137 : public bad_alloc
138{
139public:
140 bad_array_length() _NOEXCEPT;
141 virtual ~bad_array_length() _NOEXCEPT;
142 virtual const char* what() const _NOEXCEPT;
143};
Howard Hinnant4582a2b2013-11-07 17:15:51 +0000144
145#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
146
147#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow3bf77132013-09-11 01:38:42 +0000148
Eric Fiselier162922f2016-10-14 06:46:30 +0000149#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
150#ifndef _LIBCPP_CXX03_LANG
151enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
152#else
153enum align_val_t { __zero = 0, __max = (size_t)-1 };
154#endif
155#endif
156
Howard Hinnant8331b762013-03-06 23:30:19 +0000157struct _LIBCPP_TYPE_VIS nothrow_t {};
158extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159typedef void (*new_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000160_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
161_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162
163} // std
164
Shoaib Meenaiaaa8acd2016-10-12 13:48:14 +0000165#if defined(_WIN32) && !defined(_LIBCPP_BUILDING_LIBRARY)
Shoaib Meenai1732d222016-09-28 22:28:51 +0000166# define _LIBCPP_NEW_DELETE_VIS
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000167#else
168# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000171#if !__has_feature(cxx_noexcept)
Eric Fiselier162922f2016-10-14 06:46:30 +0000172#define _THROW_BAD_ALLOC throw(std::bad_alloc)
173#else
174#define _THROW_BAD_ALLOC
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000175#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000176
177_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000178_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
179_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p) _NOEXCEPT;
180_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselier162922f2016-10-14 06:46:30 +0000181#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000182_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000183#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000184
Eric Fiselier162922f2016-10-14 06:46:30 +0000185_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000186_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
187_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p) _NOEXCEPT;
188_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselier162922f2016-10-14 06:46:30 +0000189#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000190_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000191#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000192
Eric Fiselier162922f2016-10-14 06:46:30 +0000193#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
194_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
195_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
196_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
197_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
198#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
199_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
200#endif
201
202_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
203_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
204_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
205_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
206#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
207_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
208#endif
209#endif
210
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000211inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
212inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
213inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
214inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
Richard Smith1f1c1472014-06-04 19:54:15 +0000216_LIBCPP_BEGIN_NAMESPACE_STD
217
218inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
219#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
220 return ::operator new(__size);
221#else
222 return __builtin_operator_new(__size);
223#endif
224}
225
226inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) {
227#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
228 ::operator delete(__ptr);
229#else
230 __builtin_operator_delete(__ptr);
231#endif
232}
233
Marshall Clow8fea1612016-08-25 15:09:01 +0000234#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
235_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
236void __throw_bad_array_length()
237{
238#ifndef _LIBCPP_NO_EXCEPTIONS
239 throw bad_array_length();
240#else
241 _VSTD::abort();
242#endif
243}
244#endif
245
Richard Smith1f1c1472014-06-04 19:54:15 +0000246_LIBCPP_END_NAMESPACE_STD
247
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248#endif // _LIBCPP_NEW