Path: blob/master/libs/c++abi/src/stdlib_new_delete.cpp
12346 views
//===--------------------- stdlib_new_delete.cpp --------------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//8// This file implements the new and delete operators.9//===----------------------------------------------------------------------===//1011#define _LIBCPP_BUILDING_LIBRARY12#include "__cxxabi_config.h"13#include <new>14#include <cstdlib>1516#if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK)17#error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \18already be defined by libc++.19#endif20// Implement all new and delete operators as weak definitions21// in this shared library, so that they can be overridden by programs22// that define non-weak copies of the functions.2324_LIBCXXABI_WEAK25void *26operator new(std::size_t size) _THROW_BAD_ALLOC27{28if (size == 0)29size = 1;30void* p;31while ((p = ::malloc(size)) == 0)32{33// If malloc fails and there is a new_handler,34// call it to try free up memory.35std::new_handler nh = std::get_new_handler();36if (nh)37nh();38else39#ifndef _LIBCXXABI_NO_EXCEPTIONS40throw std::bad_alloc();41#else42break;43#endif44}45return p;46}4748_LIBCXXABI_WEAK49void*50operator new(size_t size, const std::nothrow_t&) _NOEXCEPT51{52void* p = 0;53#ifndef _LIBCXXABI_NO_EXCEPTIONS54try55{56#endif // _LIBCXXABI_NO_EXCEPTIONS57p = ::operator new(size);58#ifndef _LIBCXXABI_NO_EXCEPTIONS59}60catch (...)61{62}63#endif // _LIBCXXABI_NO_EXCEPTIONS64return p;65}6667_LIBCXXABI_WEAK68void*69operator new[](size_t size) _THROW_BAD_ALLOC70{71return ::operator new(size);72}7374_LIBCXXABI_WEAK75void*76operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT77{78void* p = 0;79#ifndef _LIBCXXABI_NO_EXCEPTIONS80try81{82#endif // _LIBCXXABI_NO_EXCEPTIONS83p = ::operator new[](size);84#ifndef _LIBCXXABI_NO_EXCEPTIONS85}86catch (...)87{88}89#endif // _LIBCXXABI_NO_EXCEPTIONS90return p;91}9293_LIBCXXABI_WEAK94void95operator delete(void* ptr) _NOEXCEPT96{97if (ptr)98::free(ptr);99}100101_LIBCXXABI_WEAK102void103operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT104{105::operator delete(ptr);106}107108_LIBCXXABI_WEAK109void110operator delete(void* ptr, size_t) _NOEXCEPT111{112::operator delete(ptr);113}114115_LIBCXXABI_WEAK116void117operator delete[] (void* ptr) _NOEXCEPT118{119::operator delete(ptr);120}121122_LIBCXXABI_WEAK123void124operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT125{126::operator delete[](ptr);127}128129_LIBCXXABI_WEAK130void131operator delete[] (void* ptr, size_t) _NOEXCEPT132{133::operator delete[](ptr);134}135136#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)137138_LIBCXXABI_WEAK139void *140operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC141{142if (size == 0)143size = 1;144if (static_cast<size_t>(alignment) < sizeof(void*))145alignment = std::align_val_t(sizeof(void*));146void* p;147#if defined(_LIBCPP_WIN32API)148while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)149#else150while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)151#endif152{153// If posix_memalign fails and there is a new_handler,154// call it to try free up memory.155std::new_handler nh = std::get_new_handler();156if (nh)157nh();158else {159#ifndef _LIBCXXABI_NO_EXCEPTIONS160throw std::bad_alloc();161#else162p = nullptr; // posix_memalign doesn't initialize 'p' on failure163break;164#endif165}166}167return p;168}169170_LIBCXXABI_WEAK171void*172operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT173{174void* p = 0;175#ifndef _LIBCXXABI_NO_EXCEPTIONS176try177{178#endif // _LIBCXXABI_NO_EXCEPTIONS179p = ::operator new(size, alignment);180#ifndef _LIBCXXABI_NO_EXCEPTIONS181}182catch (...)183{184}185#endif // _LIBCXXABI_NO_EXCEPTIONS186return p;187}188189_LIBCXXABI_WEAK190void*191operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC192{193return ::operator new(size, alignment);194}195196_LIBCXXABI_WEAK197void*198operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT199{200void* p = 0;201#ifndef _LIBCXXABI_NO_EXCEPTIONS202try203{204#endif // _LIBCXXABI_NO_EXCEPTIONS205p = ::operator new[](size, alignment);206#ifndef _LIBCXXABI_NO_EXCEPTIONS207}208catch (...)209{210}211#endif // _LIBCXXABI_NO_EXCEPTIONS212return p;213}214215_LIBCXXABI_WEAK216void217operator delete(void* ptr, std::align_val_t) _NOEXCEPT218{219if (ptr)220#if defined(_LIBCPP_WIN32API)221::_aligned_free(ptr);222#else223::free(ptr);224#endif225}226227_LIBCXXABI_WEAK228void229operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT230{231::operator delete(ptr, alignment);232}233234_LIBCXXABI_WEAK235void236operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT237{238::operator delete(ptr, alignment);239}240241_LIBCXXABI_WEAK242void243operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT244{245::operator delete(ptr, alignment);246}247248_LIBCXXABI_WEAK249void250operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT251{252::operator delete[](ptr, alignment);253}254255_LIBCXXABI_WEAK256void257operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT258{259::operator delete[](ptr, alignment);260}261262#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION263264265