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.
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.
9 // As such, this file should not contain any allocator-specific code.
11 // Implement a C++ style allocation, which always calls the new_handler
13 inline void* generic_cpp_alloc(size_t size
, bool nothrow
) {
19 if (!call_new_handler(nothrow
))
27 void* operator new(size_t size
) {
28 return generic_cpp_alloc(size
, false);
31 void operator delete(void* p
) {
35 void* operator new[](size_t size
) {
36 return generic_cpp_alloc(size
, false);
39 void operator delete[](void* p
) {
43 void* operator new(size_t size
, const std::nothrow_t
& nt
) {
44 return generic_cpp_alloc(size
, true);
47 void operator delete(void* p
, const std::nothrow_t
& nt
) {
51 void* operator new[](size_t size
, const std::nothrow_t
& nt
) {
52 return generic_cpp_alloc(size
, true);
55 void operator delete[](void* p
, const std::nothrow_t
& nt
) {
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.
64 int _set_new_mode(int flag
) throw() {
65 int old_mode
= new_mode
;
74 void* calloc(size_t n
, size_t elem_size
) {
76 const size_t size
= n
* elem_size
;
77 if (elem_size
!= 0 && size
/ elem_size
!= n
) return NULL
;
79 void* result
= malloc(size
);
81 memset(result
, 0, size
);
86 void cfree(void* p
) __THROW
{
92 void* _recalloc(void* p
, size_t n
, size_t elem_size
) {
94 return calloc(n
, elem_size
);
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
);
108 void* _calloc_impl(size_t n
, size_t size
) {
109 return calloc(n
, size
);
117 static int error_handler(int reportType
) {
118 switch (reportType
) {
123 case 1: // _CRT_ERROR
127 case 2: // _CRT_ASSERT
136 int _CrtDbgReport(int reportType
,
141 return error_handler(reportType
);
144 int _CrtDbgReportW(int reportType
,
149 return error_handler(reportType
);
152 int _CrtSetReportMode(int, int) {
156 void* _malloc_dbg(size_t size
, int , const char*, int) {
160 void* _realloc_dbg(void* ptr
, size_t size
, int, const char*, int) {
161 return realloc(ptr
, size
);
164 void _free_dbg(void* ptr
, int) {
168 void* _calloc_dbg(size_t n
, size_t size
, int, const char*, int) {
169 return calloc(n
, size
);