2 * Copyright 2010-2011 PathScale, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * memory.cc - Contains stub definition of C++ new/delete operators.
30 * These definitions are intended to be used for testing and are weak symbols
31 * to allow them to be replaced by definitions from a STL implementation.
32 * These versions simply wrap malloc() and free(), they do not provide a
33 * C++-specific allocator.
38 #include "stdexcept.h"
48 /// The type of the function called when allocation fails.
49 typedef void (*new_handler)();
51 * The function to call when allocation fails. By default, there is no
52 * handler and a bad allocation exception is thrown if an allocation fails.
54 static atomic<new_handler> new_handl{nullptr};
59 * Sets a function to be called when there is a failure in new.
62 new_handler set_new_handler(new_handler handler)
64 return new_handl.exchange(handler);
68 new_handler get_new_handler(void)
70 return new_handl.load();
75 #if __cplusplus < 201103L
76 #define NOEXCEPT throw()
77 #define BADALLOC throw(std::bad_alloc)
79 #define NOEXCEPT noexcept
86 * Helper for forwarding from no-throw operators to versions that can
87 * return nullptr. Catches any exception and converts it into a nullptr
90 template<void*(New)(size_t)>
91 void *noexcept_new(size_t size)
93 #if !defined(_CXXRT_NO_EXCEPTIONS)
99 // nothrow operator new should return NULL in case of
100 // std::bad_alloc exception in new handler
110 __attribute__((weak))
111 void* operator new(size_t size) BADALLOC
117 void * mem = malloc(size);
120 new_handler h = std::get_new_handler();
127 #if !defined(_CXXRT_NO_EXCEPTIONS)
128 throw std::bad_alloc();
140 __attribute__((weak))
141 void* operator new(size_t size, const std::nothrow_t &) NOEXCEPT
143 return noexcept_new<(::operator new)>(size);
147 __attribute__((weak))
148 void operator delete(void * ptr) NOEXCEPT
154 __attribute__((weak))
155 void * operator new[](size_t size) BADALLOC
157 return ::operator new(size);
161 __attribute__((weak))
162 void * operator new[](size_t size, const std::nothrow_t &) NOEXCEPT
164 return noexcept_new<(::operator new[])>(size);
168 __attribute__((weak))
169 void operator delete[](void * ptr) NOEXCEPT
171 ::operator delete(ptr);
174 // C++14 additional delete operators
176 #if __cplusplus >= 201402L
178 __attribute__((weak))
179 void operator delete(void * ptr, size_t) NOEXCEPT
181 ::operator delete(ptr);
185 __attribute__((weak))
186 void operator delete[](void * ptr, size_t) NOEXCEPT
188 ::operator delete(ptr);