blob: e77d3e6d624116263d3551d69c03e40afb9db8a8 [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
Marshall Clow3bf77132013-09-11 01:38:42 +000030class 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
Howard Hinnantc51e1022010-05-11 19:42:16 +000042struct nothrow_t {};
43extern const nothrow_t nothrow;
44typedef void (*new_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000045new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047
48} // std
49
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000050void* 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
Larisse Voufo574190d2015-02-15 05:18:55 +000053void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000054void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000056void* 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
Larisse Voufo574190d2015-02-15 05:18:55 +000059void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000060void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantc51e1022010-05-11 19:42:16 +000061
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000062void* 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;
Howard Hinnantc51e1022010-05-11 19:42:16 +000066
67*/
68
69#include <__config>
70#include <exception>
71#include <cstddef>
Eric Fiselier279c3192016-09-06 21:25:27 +000072#ifdef _LIBCPP_NO_EXCEPTIONS
73#include <cstdlib>
74#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +000076#include <__undef___deallocate>
77
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000078#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000079#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000080#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000081
82namespace std // purposefully not using versioning namespace
83{
84
85class _LIBCPP_EXCEPTION_ABI bad_alloc
86 : public exception
87{
88public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000089 bad_alloc() _NOEXCEPT;
90 virtual ~bad_alloc() _NOEXCEPT;
91 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000092};
93
94class _LIBCPP_EXCEPTION_ABI bad_array_new_length
95 : public bad_alloc
96{
97public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000098 bad_array_new_length() _NOEXCEPT;
99 virtual ~bad_array_new_length() _NOEXCEPT;
100 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101};
102
Marshall Clow8fea1612016-08-25 15:09:01 +0000103_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
104
Marshall Clow3bf77132013-09-11 01:38:42 +0000105#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant4582a2b2013-11-07 17:15:51 +0000106
Marshall Clow3bf77132013-09-11 01:38:42 +0000107class _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};
Howard Hinnant4582a2b2013-11-07 17:15:51 +0000115
116#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
117
118#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow3bf77132013-09-11 01:38:42 +0000119
Howard Hinnant8331b762013-03-06 23:30:19 +0000120struct _LIBCPP_TYPE_VIS nothrow_t {};
121extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122typedef void (*new_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000123_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
124_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125
126} // std
127
Shoaib Meenaiaaa8acd2016-10-12 13:48:14 +0000128#if defined(_WIN32) && !defined(_LIBCPP_BUILDING_LIBRARY)
Shoaib Meenai1732d222016-09-28 22:28:51 +0000129# define _LIBCPP_NEW_DELETE_VIS
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000130#else
131# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000132#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000134_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000135#if !__has_feature(cxx_noexcept)
136 throw(std::bad_alloc)
137#endif
138;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000139_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;
Eric Fiselierce8b5e52015-05-19 02:03:22 +0000142#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
143 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000144_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000145#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000146
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;
Eric Fiselierce8b5e52015-05-19 02:03:22 +0000155#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
156 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000157_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000158#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000159
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000160inline _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 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164
Richard Smith1f1c1472014-06-04 19:54:15 +0000165_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
Marshall Clow8fea1612016-08-25 15:09:01 +0000183#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
Richard Smith1f1c1472014-06-04 19:54:15 +0000195_LIBCPP_END_NAMESPACE_STD
196
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197#endif // _LIBCPP_NEW