this repo has no description
1// -*- C++ -*- 2//===----------------------------- new ------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 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: 24 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; 28}; 29 30class bad_array_length : public bad_alloc // FIXME: Not part of C++ 31{ 32public: 33 bad_array_length() noexcept; 34}; 35 36class bad_array_new_length : public bad_alloc // C++14 37{ 38public: 39 bad_array_new_length() noexcept; 40}; 41 42enum class align_val_t : size_t {}; // C++17 43struct nothrow_t {}; 44extern const nothrow_t nothrow; 45typedef void (*new_handler)(); 46new_handler set_new_handler(new_handler new_p) noexcept; 47new_handler get_new_handler() noexcept; 48 49} // std 50 51void* operator new(std::size_t size); // replaceable 52void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17 53void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable 54void* operator new(std::size_t size, std::align_val_t alignment, 55 const std::nothrow_t&) noexcept; // replaceable, C++17 56void operator delete(void* ptr) noexcept; // replaceable 57void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 58void 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 61void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable 62void operator delete(void* ptr, std:align_val_t alignment, 63 const std::nothrow_t&) noexcept; // replaceable, C++17 64 65void* operator new[](std::size_t size); // replaceable 66void* operator new[](std::size_t size, 67 std::align_val_t alignment) noexcept; // replaceable, C++17 68void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable 69void* operator new[](std::size_t size, std::align_val_t alignment, 70 const std::nothrow_t&) noexcept; // replaceable, C++17 71void operator delete[](void* ptr) noexcept; // replaceable 72void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 73void 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 77void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable 78void operator delete[](void* ptr, std::align_val_t alignment, 79 const std::nothrow_t&) noexcept; // replaceable, C++17 80 81void* 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; 85 86*/ 87 88#include <__config> 89#include <exception> 90#include <cstddef> 91#ifdef _LIBCPP_NO_EXCEPTIONS 92#include <cstdlib> 93#endif 94 95#if defined(_LIBCPP_ABI_MICROSOFT) 96#include <new.h> 97#endif 98 99#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 100#pragma GCC system_header 101#endif 102 103#if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \ 104 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)) 105# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION 106#endif 107 108#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) && \ 109 (!(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \ 110 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606))) 111# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 112#endif 113 114namespace std // purposefully not using versioning namespace 115{ 116 117#if !defined(_LIBCPP_ABI_MICROSOFT) 118struct _LIBCPP_TYPE_VIS nothrow_t {}; 119extern _LIBCPP_FUNC_VIS const nothrow_t nothrow; 120 121class _LIBCPP_EXCEPTION_ABI bad_alloc 122 : public exception 123{ 124public: 125 bad_alloc() _NOEXCEPT; 126 virtual ~bad_alloc() _NOEXCEPT; 127 virtual const char* what() const _NOEXCEPT; 128}; 129 130class _LIBCPP_EXCEPTION_ABI bad_array_new_length 131 : public bad_alloc 132{ 133public: 134 bad_array_new_length() _NOEXCEPT; 135 virtual ~bad_array_new_length() _NOEXCEPT; 136 virtual const char* what() const _NOEXCEPT; 137}; 138 139typedef void (*new_handler)(); 140_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT; 141_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT; 142 143#endif // !_LIBCPP_ABI_MICROSOFT 144 145_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec 146 147#if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11) 148 149class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH 150 bad_array_length : public bad_alloc { 151public: 152 bad_array_length() _NOEXCEPT; 153 virtual ~bad_array_length() _NOEXCEPT; 154 virtual const char* what() const _NOEXCEPT; 155}; 156 157#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED 158 159#endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11) 160 161#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14 162#ifndef _LIBCPP_CXX03_LANG 163enum class _LIBCPP_ENUM_VIS align_val_t : size_t { }; 164#else 165enum align_val_t { __zero = 0, __max = (size_t)-1 }; 166#endif 167#endif 168 169} // std 170 171#if defined(_LIBCPP_CXX03_LANG) 172#define _THROW_BAD_ALLOC throw(std::bad_alloc) 173#else 174#define _THROW_BAD_ALLOC 175#endif 176 177#if !defined(_LIBCPP_ABI_MICROSOFT) 178 179_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; 180_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 181_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; 182_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; 183#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 184_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; 185#endif 186 187_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 188_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 189_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 190_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; 191#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 192_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; 193#endif 194 195#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 196_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 197_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 198_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; 199_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 200#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 201_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 202#endif 203 204_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 205_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 206_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; 207_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 208#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 209_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 210#endif 211#endif 212 213inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} 214inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} 215inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} 216inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} 217 218#endif // !_LIBCPP_ABI_MICROSOFT 219 220_LIBCPP_BEGIN_NAMESPACE_STD 221 222inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) { 223#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 224 return ::operator new(__size); 225#else 226 return __builtin_operator_new(__size); 227#endif 228} 229 230inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) { 231#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 232 ::operator delete(__ptr); 233#else 234 __builtin_operator_delete(__ptr); 235#endif 236} 237 238#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED 239_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE 240#ifndef _LIBCPP_NO_EXCEPTIONS 241_LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH 242#endif 243void __throw_bad_array_length() 244{ 245#ifndef _LIBCPP_NO_EXCEPTIONS 246 throw bad_array_length(); 247#else 248 _VSTD::abort(); 249#endif 250} 251#endif 252 253_LIBCPP_END_NAMESPACE_STD 254 255#endif // _LIBCPP_NEW