| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // When possible, we implement allocator functions on top of the basic | 5 // When possible, we implement allocator functions on top of the basic |
| 6 // low-level functions malloc() and free(). This way, including a new | 6 // low-level functions malloc() and free(). This way, including a new |
| 7 // allocator is as simple as providing just a small interface. | 7 // allocator is as simple as providing just a small interface. |
| 8 // | 8 // |
| 9 // As such, this file should not contain any allocator-specific code. | 9 // As such, this file should not contain any allocator-specific code. |
| 10 | 10 |
| 11 // Implement a C++ style allocation, which always calls the new_handler | 11 // Implement a C++ style allocation, which always calls the new_handler |
| 12 // on failure. | 12 // on failure. |
| 13 inline void* generic_cpp_alloc(size_t size, bool nothrow) { | 13 inline void* generic_cpp_alloc(size_t size, bool nothrow) { |
| 14 void* ptr; | 14 void* ptr; |
| 15 for (;;) { | 15 for (;;) { |
| 16 ptr = malloc(size); | 16 ptr = malloc(size); |
| 17 if (ptr) | 17 if (ptr) |
| 18 return ptr; | 18 return ptr; |
| 19 if (!call_new_handler(nothrow)) | 19 if (!call_new_handler(nothrow)) |
| 20 break; | 20 break; |
| 21 } | 21 } |
| 22 return ptr; | 22 return ptr; |
| 23 } | 23 } |
| 24 | 24 |
| 25 extern "C++" { | 25 extern "C++" { |
| 26 | 26 |
| 27 void* __cdecl operator new(size_t size) { | 27 void* operator new(size_t size) { |
| 28 return generic_cpp_alloc(size, false); | 28 return generic_cpp_alloc(size, false); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void operator delete(void* p) throw() { | 31 void operator delete(void* p) { |
| 32 free(p); | 32 free(p); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void* operator new[](size_t size) { | 35 void* operator new[](size_t size) { |
| 36 return generic_cpp_alloc(size, false); | 36 return generic_cpp_alloc(size, false); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void operator delete[](void* p) throw() { | 39 void operator delete[](void* p) { |
| 40 free(p); | 40 free(p); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void* operator new(size_t size, const std::nothrow_t& nt) throw() { | 43 void* operator new(size_t size, const std::nothrow_t& nt) { |
| 44 return generic_cpp_alloc(size, true); | 44 return generic_cpp_alloc(size, true); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void* operator new[](size_t size, const std::nothrow_t& nt) throw() { | 47 void operator delete(void* p, const std::nothrow_t& nt) { |
| 48 free(p); |
| 49 } |
| 50 |
| 51 void* operator new[](size_t size, const std::nothrow_t& nt) { |
| 48 return generic_cpp_alloc(size, true); | 52 return generic_cpp_alloc(size, true); |
| 49 } | 53 } |
| 50 | 54 |
| 55 void operator delete[](void* p, const std::nothrow_t& nt) { |
| 56 free(p); |
| 57 } |
| 58 |
| 51 // This function behaves similarly to MSVC's _set_new_mode. | 59 // This function behaves similarly to MSVC's _set_new_mode. |
| 52 // If flag is 0 (default), calls to malloc will behave normally. | 60 // If flag is 0 (default), calls to malloc will behave normally. |
| 53 // If flag is 1, calls to malloc will behave like calls to new, | 61 // If flag is 1, calls to malloc will behave like calls to new, |
| 54 // and the std_new_handler will be invoked on failure. | 62 // and the std_new_handler will be invoked on failure. |
| 55 // Returns the previous mode. | 63 // Returns the previous mode. |
| 56 int _set_new_mode(int flag) throw() { | 64 int _set_new_mode(int flag) throw() { |
| 57 int old_mode = new_mode; | 65 int old_mode = new_mode; |
| 58 new_mode = flag; | 66 new_mode = flag; |
| 59 return old_mode; | 67 return old_mode; |
| 60 } | 68 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 167 |
| 160 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { | 168 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
| 161 return calloc(n, size); | 169 return calloc(n, size); |
| 162 } | 170 } |
| 163 #endif // NDEBUG | 171 #endif // NDEBUG |
| 164 | 172 |
| 165 #endif // WIN32 | 173 #endif // WIN32 |
| 166 | 174 |
| 167 } // extern C | 175 } // extern C |
| 168 | 176 |
| OLD | NEW |