blob: ea4a4a01a993dfe9e81c4ffd8bf533b3ddc59859 [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
53void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantc51e1022010-05-11 19:42:16 +000054
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000055void* operator new[](std::size_t size); // replaceable
56void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
57void operator delete[](void* ptr) noexcept; // replaceable
58void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Howard Hinnantc51e1022010-05-11 19:42:16 +000059
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000060void* operator new (std::size_t size, void* ptr) noexcept;
61void* operator new[](std::size_t size, void* ptr) noexcept;
62void operator delete (void* ptr, void*) noexcept;
63void operator delete[](void* ptr, void*) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000064
65*/
66
67#include <__config>
68#include <exception>
69#include <cstddef>
70
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000071#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000072#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000073#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000074
75namespace std // purposefully not using versioning namespace
76{
77
78class _LIBCPP_EXCEPTION_ABI bad_alloc
79 : public exception
80{
81public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000082 bad_alloc() _NOEXCEPT;
83 virtual ~bad_alloc() _NOEXCEPT;
84 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000085};
86
87class _LIBCPP_EXCEPTION_ABI bad_array_new_length
88 : public bad_alloc
89{
90public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000091 bad_array_new_length() _NOEXCEPT;
92 virtual ~bad_array_new_length() _NOEXCEPT;
93 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000094};
95
Marshall Clow3bf77132013-09-11 01:38:42 +000096#if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Howard Hinnant4582a2b2013-11-07 17:15:51 +000097
Marshall Clow3bf77132013-09-11 01:38:42 +000098class _LIBCPP_EXCEPTION_ABI bad_array_length
99 : public bad_alloc
100{
101public:
102 bad_array_length() _NOEXCEPT;
103 virtual ~bad_array_length() _NOEXCEPT;
104 virtual const char* what() const _NOEXCEPT;
105};
Howard Hinnant4582a2b2013-11-07 17:15:51 +0000106
107#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
108
109#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
Marshall Clow3bf77132013-09-11 01:38:42 +0000110
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000111_LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112
Howard Hinnant8331b762013-03-06 23:30:19 +0000113struct _LIBCPP_TYPE_VIS nothrow_t {};
114extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115typedef void (*new_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000116_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
117_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118
119} // std
120
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000121#if defined(_WIN32) && !defined(cxx_EXPORTS)
122# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS_ONLY
123#else
124# define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000127_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000128#if !__has_feature(cxx_noexcept)
129 throw(std::bad_alloc)
130#endif
131;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000132_LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
133_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p) _NOEXCEPT;
134_LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
135
136_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz)
137#if !__has_feature(cxx_noexcept)
138 throw(std::bad_alloc)
139#endif
140;
141_LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
142_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p) _NOEXCEPT;
143_LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
144
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000145inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
146inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
147inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
148inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150#endif // _LIBCPP_NEW