blob: 952086712701be744e54579492bb65eba3418a4b [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
Marshall Clow7f9f52e2013-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 Hinnantbc8d3f92010-05-11 19:42:16 +000042struct nothrow_t {};
43extern const nothrow_t nothrow;
44typedef void (*new_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +000045new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047
48} // std
49
Howard Hinnanted569212011-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 Voufo19efe012015-02-15 05:18:55 +000053void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Howard Hinnanted569212011-05-26 18:23:59 +000054void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055
Howard Hinnanted569212011-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 Voufo19efe012015-02-15 05:18:55 +000059void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Howard Hinnanted569212011-05-26 18:23:59 +000060void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061
Howard Hinnanted569212011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000066
67*/
68
69#include <__config>
70#include <exception>
71#include <cstddef>
72
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +000073#include <__undef___deallocate>
74
Howard Hinnant08e17472011-10-17 20:05:10 +000075#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000077#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
79namespace std // purposefully not using versioning namespace
80{
81
82class _LIBCPP_EXCEPTION_ABI bad_alloc
83 : public exception
84{
85public:
Howard Hinnanted569212011-05-26 18:23:59 +000086 bad_alloc() _NOEXCEPT;
87 virtual ~bad_alloc() _NOEXCEPT;
88 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089};
90
91class _LIBCPP_EXCEPTION_ABI bad_array_new_length
92 : public bad_alloc
93{
94public:
Howard Hinnanted569212011-05-26 18:23:59 +000095 bad_array_new_length() _NOEXCEPT;
96 virtual ~bad_array_new_length() _NOEXCEPT;
97 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098};
99
Marshall Clow14c09a22016-08-25 15:09:01 +0000100_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
101
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000102#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant29250b72013-11-07 17:15:51 +0000103
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000104class _LIBCPP_EXCEPTION_ABI bad_array_length
105 : public bad_alloc
106{
107public:
108 bad_array_length() _NOEXCEPT;
109 virtual ~bad_array_length() _NOEXCEPT;
110 virtual const char* what() const _NOEXCEPT;
111};
Howard Hinnant29250b72013-11-07 17:15:51 +0000112
113#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
114
115#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000116
Howard Hinnant83eade62013-03-06 23:30:19 +0000117struct _LIBCPP_TYPE_VIS nothrow_t {};
118extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119typedef void (*new_handler)();
Howard Hinnant83eade62013-03-06 23:30:19 +0000120_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
121_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122
123} // std
124
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000125#if defined(_WIN32) && !defined(cxx_EXPORTS)
126# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS_ONLY
127#else
128# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
Howard Hinnanted569212011-05-26 18:23:59 +0000129#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000131_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
Howard Hinnanted569212011-05-26 18:23:59 +0000132#if !__has_feature(cxx_noexcept)
133 throw(std::bad_alloc)
134#endif
135;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000136_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
137_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p) _NOEXCEPT;
138_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierf4c97292015-05-19 02:03:22 +0000139#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
140 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
Larisse Voufo74f95a02015-02-20 06:13:05 +0000141_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05 +0000142#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000143
144_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz)
145#if !__has_feature(cxx_noexcept)
146 throw(std::bad_alloc)
147#endif
148;
149_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
150_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p) _NOEXCEPT;
151_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierf4c97292015-05-19 02:03:22 +0000152#if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
153 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
Larisse Voufo74f95a02015-02-20 06:13:05 +0000154_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufo74f95a02015-02-20 06:13:05 +0000155#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000156
Howard Hinnant1e564242013-10-04 22:09:00 +0000157inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
158inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
159inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
160inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161
Richard Smith73c1fce2014-06-04 19:54:15 +0000162_LIBCPP_BEGIN_NAMESPACE_STD
163
164inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
165#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
166 return ::operator new(__size);
167#else
168 return __builtin_operator_new(__size);
169#endif
170}
171
172inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) {
173#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
174 ::operator delete(__ptr);
175#else
176 __builtin_operator_delete(__ptr);
177#endif
178}
179
Marshall Clow14c09a22016-08-25 15:09:01 +0000180#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
181_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
182void __throw_bad_array_length()
183{
184#ifndef _LIBCPP_NO_EXCEPTIONS
185 throw bad_array_length();
186#else
187 _VSTD::abort();
188#endif
189}
190#endif
191
Richard Smith73c1fce2014-06-04 19:54:15 +0000192_LIBCPP_END_NAMESPACE_STD
193
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194#endif // _LIBCPP_NEW