1 /* 2 * Copyright 2010-2011 PathScale, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 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. 13 * 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. 25 */ 26 27 /** 28 * memory.cc - Contains stub definition of C++ new/delete operators. 29 * 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. 34 */ 35 36 #include <stddef.h> 37 #include <stdlib.h> 38 #include "stdexcept.h" 39 #include "atomic.h" 40 41 42 namespace std 43 { 44 struct nothrow_t {}; 45 } 46 47 48 /// The type of the function called when allocation fails. 49 typedef void (*new_handler)(); 50 /** 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. 53 */ 54 static new_handler new_handl; 55 56 namespace std 57 { 58 /** 59 * Sets a function to be called when there is a failure in new. 60 */ 61 __attribute__((weak)) 62 new_handler set_new_handler(new_handler handler) 63 { 64 return ATOMIC_SWAP(&new_handl, handler); 65 } 66 __attribute__((weak)) 67 new_handler get_new_handler(void) 68 { 69 return ATOMIC_LOAD(&new_handl); 70 } 71 } 72 73 74 #if __cplusplus < 201103L 75 #define NOEXCEPT throw() 76 #define BADALLOC throw(std::bad_alloc) 77 #else 78 #define NOEXCEPT noexcept 79 #define BADALLOC 80 #endif 81 82 namespace 83 { 84 /** 85 * Helper for forwarding from no-throw operators to versions that can 86 * return nullptr. Catches any exception and converts it into a nullptr 87 * return. 88 */ 89 template<void*(New)(size_t)> 90 void *noexcept_new(size_t size) 91 { 92 #if !defined(_CXXRT_NO_EXCEPTIONS) 93 try 94 { 95 return New(size); 96 } catch (...) 97 { 98 // nothrow operator new should return NULL in case of 99 // std::bad_alloc exception in new handler 100 return nullptr; 101 } 102 #else 103 return New(size); 104 #endif 105 } 106 } 107 108 109 __attribute__((weak)) 110 void* operator new(size_t size) BADALLOC 111 { 112 if (0 == size) 113 { 114 size = 1; 115 } 116 void * mem = malloc(size); 117 while (0 == mem) 118 { 119 new_handler h = std::get_new_handler(); 120 if (0 != h) 121 { 122 h(); 123 } 124 else 125 { 126 #if !defined(_CXXRT_NO_EXCEPTIONS) 127 throw std::bad_alloc(); 128 #else 129 break; 130 #endif 131 } 132 mem = malloc(size); 133 } 134 135 return mem; 136 } 137 138 139 __attribute__((weak)) 140 void* operator new(size_t size, const std::nothrow_t &) NOEXCEPT 141 { 142 return noexcept_new<(::operator new)>(size); 143 } 144 145 146 __attribute__((weak)) 147 void operator delete(void * ptr) NOEXCEPT 148 { 149 free(ptr); 150 } 151 152 153 __attribute__((weak)) 154 void * operator new[](size_t size) BADALLOC 155 { 156 return ::operator new(size); 157 } 158 159 160 __attribute__((weak)) 161 void * operator new[](size_t size, const std::nothrow_t &) NOEXCEPT 162 { 163 return noexcept_new<(::operator new[])>(size); 164 } 165 166 167 __attribute__((weak)) 168 void operator delete[](void * ptr) NOEXCEPT 169 { 170 ::operator delete(ptr); 171 } 172 173 // C++14 additional delete operators 174 175 #if __cplusplus >= 201402L 176 177 __attribute__((weak)) 178 void operator delete(void * ptr, size_t) NOEXCEPT 179 { 180 ::operator delete(ptr); 181 } 182 183 184 __attribute__((weak)) 185 void operator delete[](void * ptr, size_t) NOEXCEPT 186 { 187 ::operator delete(ptr); 188 } 189 190 #endif 191