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. 13inline 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 25extern "C++" { 26 27void* operator new(size_t size) { 28 return generic_cpp_alloc(size, false); 29} 30 31void operator delete(void* p) { 32 free(p); 33} 34 35void* operator new[](size_t size) { 36 return generic_cpp_alloc(size, false); 37} 38 39void operator delete[](void* p) { 40 free(p); 41} 42 43void* operator new(size_t size, const std::nothrow_t& nt) { 44 return generic_cpp_alloc(size, true); 45} 46 47void operator delete(void* p, const std::nothrow_t& nt) { 48 free(p); 49} 50 51void* operator new[](size_t size, const std::nothrow_t& nt) { 52 return generic_cpp_alloc(size, true); 53} 54 55void operator delete[](void* p, const std::nothrow_t& nt) { 56 free(p); 57} 58 59// This function behaves similarly to MSVC's _set_new_mode. 60// If flag is 0 (default), calls to malloc will behave normally. 61// If flag is 1, calls to malloc will behave like calls to new, 62// and the std_new_handler will be invoked on failure. 63// Returns the previous mode. 64int _set_new_mode(int flag) throw() { 65 int old_mode = new_mode; 66 new_mode = flag; 67 return old_mode; 68} 69 70} // extern "C++" 71 72extern "C" { 73 74void* calloc(size_t n, size_t elem_size) { 75 // Overflow check 76 const size_t size = n * elem_size; 77 if (elem_size != 0 && size / elem_size != n) return NULL; 78 79 void* result = malloc(size); 80 if (result != NULL) { 81 memset(result, 0, size); 82 } 83 return result; 84} 85 86void cfree(void* p) __THROW { 87 free(p); 88} 89 90#ifdef WIN32 91 92void* _recalloc(void* p, size_t n, size_t elem_size) { 93 if (!p) 94 return calloc(n, elem_size); 95 96 // This API is a bit odd. 97 // Note: recalloc only guarantees zeroed memory when p is NULL. 98 // Generally, calls to malloc() have padding. So a request 99 // to malloc N bytes actually malloc's N+x bytes. Later, if 100 // that buffer is passed to recalloc, we don't know what N 101 // was anymore. We only know what N+x is. As such, there is 102 // no way to know what to zero out. 103 const size_t size = n * elem_size; 104 if (elem_size != 0 && size / elem_size != n) return NULL; 105 return realloc(p, size); 106} 107 108void* _calloc_impl(size_t n, size_t size) { 109 return calloc(n, size); 110} 111 112#ifndef NDEBUG 113#undef malloc 114#undef free 115#undef calloc 116 117static int error_handler(int reportType) { 118 switch (reportType) { 119 case 0: // _CRT_WARN 120 __debugbreak(); 121 return 0; 122 123 case 1: // _CRT_ERROR 124 __debugbreak(); 125 return 0; 126 127 case 2: // _CRT_ASSERT 128 __debugbreak(); 129 return 0; 130 } 131 char* p = NULL; 132 *p = '\0'; 133 return 0; 134} 135 136int _CrtDbgReport(int reportType, 137 const char*, 138 int, const char*, 139 const char*, 140 ...) { 141 return error_handler(reportType); 142} 143 144int _CrtDbgReportW(int reportType, 145 const wchar_t*, 146 int, const wchar_t*, 147 const wchar_t*, 148 ...) { 149 return error_handler(reportType); 150} 151 152int _CrtSetReportMode(int, int) { 153 return 0; 154} 155 156void* _malloc_dbg(size_t size, int , const char*, int) { 157 return malloc(size); 158} 159 160void* _realloc_dbg(void* ptr, size_t size, int, const char*, int) { 161 return realloc(ptr, size); 162} 163 164void _free_dbg(void* ptr, int) { 165 free(ptr); 166} 167 168void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { 169 return calloc(n, size); 170} 171#endif // NDEBUG 172 173#endif // WIN32 174 175} // extern C 176 177