blob: 86428f281dc16e006f1ff4e80629eddb0aa03e37 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:5924 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:1628};
29
Eric Fiselier9acbffa2016-10-14 06:46:3030class bad_array_length : public bad_alloc // FIXME: Not part of C++
Marshall Clow7f9f52e2013-09-11 01:38:4231{
32public:
33 bad_array_length() noexcept;
34};
35
Eric Fiselier9acbffa2016-10-14 06:46:3036class bad_array_new_length : public bad_alloc // C++14
Marshall Clow7f9f52e2013-09-11 01:38:4237{
38public:
39 bad_array_new_length() noexcept;
40};
41
Eric Fiselier9acbffa2016-10-14 06:46:3042enum class align_val_t : size_t {}; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:1643struct nothrow_t {};
44extern const nothrow_t nothrow;
45typedef void (*new_handler)();
Howard Hinnanted569212011-05-26 18:23:5946new_handler set_new_handler(new_handler new_p) noexcept;
47new_handler get_new_handler() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1648
49} // std
50
Howard Hinnanted569212011-05-26 18:23:5951void* operator new(std::size_t size); // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3052void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:5953void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3054void* 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:5956void operator delete(void* ptr) noexcept; // replaceable
Larisse Voufo19efe012015-02-15 05:18:5557void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier9acbffa2016-10-14 06:46:3058void 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:5961void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3062void operator delete(void* ptr, std:align_val_t alignment,
63 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantbc8d3f92010-05-11 19:42:1664
Howard Hinnanted569212011-05-26 18:23:5965void* operator new[](std::size_t size); // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3066void* operator new[](std::size_t size,
67 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnanted569212011-05-26 18:23:5968void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3069void* 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:5971void operator delete[](void* ptr) noexcept; // replaceable
Larisse Voufo19efe012015-02-15 05:18:5572void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier9acbffa2016-10-14 06:46:3073void 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:5977void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier9acbffa2016-10-14 06:46:3078void operator delete[](void* ptr, std::align_val_t alignment,
79 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantbc8d3f92010-05-11 19:42:1680
Howard Hinnanted569212011-05-26 18:23:5981void* 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:1685
86*/
87
88#include <__config>
89#include <exception>
90#include <cstddef>
Eric Fiselierdbf60fa2016-09-06 21:25:2791#ifdef _LIBCPP_NO_EXCEPTIONS
92#include <cstdlib>
93#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1694
Howard Hinnant08e17472011-10-17 20:05:1095#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1696#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1097#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1698
Eric Fiselier9acbffa2016-10-14 06:46:3099#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
Eric Fiselierd54d9742017-01-20 01:47:26104#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)))
Eric Fiselier9acbffa2016-10-14 06:46:30107# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
108#endif
109
Howard Hinnantbc8d3f92010-05-11 19:42:16110namespace std // purposefully not using versioning namespace
111{
112
113class _LIBCPP_EXCEPTION_ABI bad_alloc
114 : public exception
115{
116public:
Howard Hinnanted569212011-05-26 18:23:59117 bad_alloc() _NOEXCEPT;
118 virtual ~bad_alloc() _NOEXCEPT;
119 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16120};
121
122class _LIBCPP_EXCEPTION_ABI bad_array_new_length
123 : public bad_alloc
124{
125public:
Howard Hinnanted569212011-05-26 18:23:59126 bad_array_new_length() _NOEXCEPT;
127 virtual ~bad_array_new_length() _NOEXCEPT;
128 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16129};
130
Marshall Clow14c09a22016-08-25 15:09:01131_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
132
Marshall Clow7f9f52e2013-09-11 01:38:42133#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant29250b72013-11-07 17:15:51134
Marshall Clow7f9f52e2013-09-11 01:38:42135class _LIBCPP_EXCEPTION_ABI bad_array_length
136 : public bad_alloc
137{
138public:
139 bad_array_length() _NOEXCEPT;
140 virtual ~bad_array_length() _NOEXCEPT;
141 virtual const char* what() const _NOEXCEPT;
142};
Howard Hinnant29250b72013-11-07 17:15:51143
144#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
145
146#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow7f9f52e2013-09-11 01:38:42147
Eric Fiselier85e34e42017-01-21 14:42:44148#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14
Eric Fiselier9acbffa2016-10-14 06:46:30149#ifndef _LIBCPP_CXX03_LANG
150enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
151#else
152enum align_val_t { __zero = 0, __max = (size_t)-1 };
153#endif
154#endif
155
Howard Hinnant83eade62013-03-06 23:30:19156struct _LIBCPP_TYPE_VIS nothrow_t {};
157extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantbc8d3f92010-05-11 19:42:16158typedef void (*new_handler)();
Howard Hinnant83eade62013-03-06 23:30:19159_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
160_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16161
162} // std
163
Eric Fiselierb45121d2017-01-02 23:27:42164#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier9acbffa2016-10-14 06:46:30165#define _THROW_BAD_ALLOC throw(std::bad_alloc)
166#else
167#define _THROW_BAD_ALLOC
Howard Hinnanted569212011-05-26 18:23:59168#endif
Eric Fiselier9acbffa2016-10-14 06:46:30169
Shoaib Meenaie6479bc2016-11-16 22:18:10170_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;
Eric Fiselier9acbffa2016-10-14 06:46:30174#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10175_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05176#endif
Howard Hinnant0f678bd2013-08-12 18:38:34177
Shoaib Meenaie6479bc2016-11-16 22:18:10178_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;
Shoaib Meenaif66194b2016-11-18 04:31:09182#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10183_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05184#endif
Howard Hinnant0f678bd2013-08-12 18:38:34185
Eric Fiselier9acbffa2016-10-14 06:46:30186#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10187_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;
Eric Fiselier9acbffa2016-10-14 06:46:30191#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10192_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30193#endif
194
Shoaib Meenaie6479bc2016-11-16 22:18:10195_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;
Eric Fiselier9acbffa2016-10-14 06:46:30199#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
Shoaib Meenaie6479bc2016-11-16 22:18:10200_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier9acbffa2016-10-14 06:46:30201#endif
202#endif
203
Howard Hinnant1e564242013-10-04 22:09:00204inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
205inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
206inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
207inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16208
Richard Smith73c1fce2014-06-04 19:54:15209_LIBCPP_BEGIN_NAMESPACE_STD
210
211inline _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
Eric Fiselier32b19c32017-01-07 03:01:24219inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) {
Richard Smith73c1fce2014-06-04 19:54:15220#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
221 ::operator delete(__ptr);
222#else
223 __builtin_operator_delete(__ptr);
224#endif
225}
226
Marshall Clow14c09a22016-08-25 15:09:01227#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
228_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
229void __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
Richard Smith73c1fce2014-06-04 19:54:15239_LIBCPP_END_NAMESPACE_STD
240
Howard Hinnantbc8d3f92010-05-11 19:42:16241#endif // _LIBCPP_NEW