OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // When possible, we implement allocator functions on top of the basic |
| 6 // low-level functions malloc() and free(). This way, including a new |
| 7 // allocator is as simple as providing just a small interface. |
| 8 // |
| 9 // As such, this file should not contain any allocator-specific code. |
| 10 |
| 11 // Implement a C++ style allocation, which always calls the new_handler |
| 12 // on failure. |
| 13 inline void* generic_cpp_alloc(size_t size, bool nothrow) { |
| 14 void* ptr; |
| 15 for (;;) { |
| 16 ptr = malloc(size); |
| 17 if (ptr) |
| 18 return ptr; |
| 19 if (!call_new_handler(nothrow)) |
| 20 break; |
| 21 } |
| 22 return ptr; |
| 23 } |
| 24 |
| 25 extern "C++" { |
| 26 |
| 27 void* __cdecl operator new(size_t size) { |
| 28 return generic_cpp_alloc(size, false); |
| 29 } |
| 30 |
| 31 void operator delete(void* p) __THROW { |
| 32 free(p); |
| 33 } |
| 34 |
| 35 void* operator new[](size_t size) { |
| 36 return generic_cpp_alloc(size, false); |
| 37 } |
| 38 |
| 39 void operator delete[](void* p) __THROW { |
| 40 free(p); |
| 41 } |
| 42 |
| 43 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { |
| 44 return generic_cpp_alloc(size, true); |
| 45 } |
| 46 |
| 47 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { |
| 48 return generic_cpp_alloc(size, true); |
| 49 } |
| 50 |
| 51 } // extern "C++" |
| 52 |
| 53 extern "C" { |
| 54 |
| 55 // This function behaves similarly to MSVC's _set_new_mode. |
| 56 // If flag is 0 (default), calls to malloc will behave normally. |
| 57 // If flag is 1, calls to malloc will behave like calls to new, |
| 58 // and the std_new_handler will be invoked on failure. |
| 59 // Returns the previous mode. |
| 60 int _set_new_mode(int flag) __THROW { |
| 61 int old_mode = new_mode; |
| 62 new_mode = flag; |
| 63 return old_mode; |
| 64 } |
| 65 |
| 66 void* calloc(size_t n, size_t elem_size) __THROW { |
| 67 // Overflow check |
| 68 const size_t size = n * elem_size; |
| 69 if (elem_size != 0 && size / elem_size != n) return NULL; |
| 70 |
| 71 void* result = malloc(size); |
| 72 if (result != NULL) { |
| 73 memset(result, 0, size); |
| 74 } |
| 75 return result; |
| 76 } |
| 77 |
| 78 void cfree(void* p) __THROW { |
| 79 free(p); |
| 80 } |
| 81 |
| 82 #ifdef WIN32 |
| 83 |
| 84 void* _recalloc(void* p, size_t n, size_t elem_size) { |
| 85 const size_t size = n * elem_size; |
| 86 if (elem_size != 0 && size / elem_size != n) return NULL; |
| 87 |
| 88 void* result = realloc(p, size); |
| 89 memset(result, 0, size); |
| 90 return result; |
| 91 } |
| 92 |
| 93 void* _calloc_impl(size_t n, size_t size) { |
| 94 return calloc(n, size); |
| 95 } |
| 96 |
| 97 #ifndef NDEBUG |
| 98 #undef malloc |
| 99 #undef free |
| 100 #undef calloc |
| 101 int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) { |
| 102 return 0; |
| 103 } |
| 104 |
| 105 int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, |
| 106 const wchar_t*, ...) { |
| 107 return 0; |
| 108 } |
| 109 |
| 110 int _CrtSetReportMode(int, int) { |
| 111 return 0; |
| 112 } |
| 113 |
| 114 void* _malloc_dbg(size_t size, int , const char*, int) { |
| 115 return malloc(size); |
| 116 } |
| 117 |
| 118 void _free_dbg(void* ptr, int) { |
| 119 free(ptr); |
| 120 } |
| 121 |
| 122 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
| 123 return calloc(n, size); |
| 124 } |
| 125 #endif // NDEBUG |
| 126 |
| 127 #endif // WIN32 |
| 128 |
| 129 } // extern C |
| 130 |
OLD | NEW |