1 // Copyright (c) 2012 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.
9 #include "base/basictypes.h"
11 // This shim make it possible to perform additional checks on allocations
12 // before passing them to the Heap functions.
14 // Heap functions are stripped from libcmt.lib using the prep_libc.py
15 // for each object file stripped, we re-implement them here to allow us to
16 // perform additional checks:
17 // 1. Enforcing the maximum size that can be allocated to 2Gb.
18 // 2. Calling new_handler if malloc fails.
21 // We set this to 1 because part of the CRT uses a check of _crtheap != 0
22 // to test whether the CRT has been initialized. Once we've ripped out
23 // the allocators from libcmt, we need to provide this definition so that
24 // the rest of the CRT is still usable.
26 void* _crtheap
= reinterpret_cast<void*>(1);
31 const size_t kWindowsPageSize
= 4096;
32 const size_t kMaxWindowsAllocation
= INT_MAX
- kWindowsPageSize
;
35 // VS2013 crt uses the process heap as its heap, so we do the same here.
36 // See heapinit.c in VS CRT sources.
37 bool win_heap_init() {
38 // Set the _crtheap global here. THis allows us to offload most of the
39 // memory management to the CRT, except the functions we need to shim.
40 _crtheap
= GetProcessHeap();
45 // NOTE: Setting LFH may fail. Vista already has it enabled.
46 // And under the debugger, it won't use LFH. So we
48 HeapSetInformation(_crtheap
, HeapCompatibilityInformation
, &enable_lfh
,
54 void* win_heap_malloc(size_t size
) {
55 if (size
< kMaxWindowsAllocation
)
56 return HeapAlloc(_crtheap
, 0, size
);
60 void win_heap_free(void* size
) {
61 HeapFree(_crtheap
, 0, size
);
64 void* win_heap_realloc(void* ptr
, size_t size
) {
66 return win_heap_malloc(size
);
71 if (size
< kMaxWindowsAllocation
)
72 return HeapReAlloc(_crtheap
, 0, ptr
, size
);
76 void win_heap_term() {
80 // Call the new handler, if one has been set.
81 // Returns true on successfully calling the handler, false otherwise.
82 inline bool call_new_handler(bool nothrow
, size_t size
) {
83 // Get the current new handler.
84 _PNH nh
= _query_new_handler();
85 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
88 // Since exceptions are disabled, we don't really know if new_handler
89 // failed. Assume it will abort if it fails.
92 #error "Exceptions in allocator shim are not supported!"
93 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
97 // Implement a C++ style allocation, which always calls the new_handler
99 inline void* generic_cpp_alloc(size_t size
, bool nothrow
) {
105 if (!call_new_handler(nothrow
, size
))
114 void* operator new(size_t size
) {
115 return generic_cpp_alloc(size
, false);
119 void operator delete(void* p
) throw() {
124 void* operator new[](size_t size
) {
125 return generic_cpp_alloc(size
, false);
129 void operator delete[](void* p
) throw() {
134 void* operator new(size_t size
, const std::nothrow_t
& nt
) {
135 return generic_cpp_alloc(size
, true);
139 void* operator new[](size_t size
, const std::nothrow_t
& nt
) {
140 return generic_cpp_alloc(size
, true);
143 // This function behaves similarly to MSVC's _set_new_mode.
144 // If flag is 0 (default), calls to malloc will behave normally.
145 // If flag is 1, calls to malloc will behave like calls to new,
146 // and the std_new_handler will be invoked on failure.
147 // Returns the previous mode.
149 int _set_new_mode(int flag
) throw() {
150 int old_mode
= new_mode
;
156 int _query_new_mode() {
162 void* malloc(size_t size
) {
165 ptr
= win_heap_malloc(size
);
169 if (!new_mode
|| !call_new_handler(true, size
))
182 void* realloc(void* ptr
, size_t size
) {
183 // Webkit is brittle for allocators that return NULL for malloc(0). The
184 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure
185 // to call malloc for this case.
191 new_ptr
= win_heap_realloc(ptr
, size
);
193 // Subtle warning: NULL return does not alwas indicate out-of-memory. If
194 // the requested new size is zero, realloc should free the ptr and return
196 if (new_ptr
|| !size
)
198 if (!new_mode
|| !call_new_handler(true, size
))
205 intptr_t _get_heap_handle() {
206 return reinterpret_cast<intptr_t>(_crtheap
);
211 return win_heap_init() ? 1 : 0;
220 void* calloc(size_t n
, size_t elem_size
) {
222 const size_t size
= n
* elem_size
;
223 if (elem_size
!= 0 && size
/ elem_size
!= n
)
226 void* result
= malloc(size
);
227 if (result
!= NULL
) {
228 memset(result
, 0, size
);
234 void* _recalloc(void* p
, size_t n
, size_t elem_size
) {
236 return calloc(n
, elem_size
);
238 // This API is a bit odd.
239 // Note: recalloc only guarantees zeroed memory when p is NULL.
240 // Generally, calls to malloc() have padding. So a request
241 // to malloc N bytes actually malloc's N+x bytes. Later, if
242 // that buffer is passed to recalloc, we don't know what N
243 // was anymore. We only know what N+x is. As such, there is
244 // no way to know what to zero out.
245 const size_t size
= n
* elem_size
;
246 if (elem_size
!= 0 && size
/ elem_size
!= n
)
248 return realloc(p
, size
);
252 void* _calloc_impl(size_t n
, size_t size
) {
253 return calloc(n
, size
);
261 static int error_handler(int reportType
) {
262 switch (reportType
) {
267 case 1: // _CRT_ERROR
271 case 2: // _CRT_ASSERT
280 int _CrtDbgReport(int reportType
,
286 return error_handler(reportType
);
289 int _CrtDbgReportW(int reportType
,
295 return error_handler(reportType
);
298 int _CrtSetReportMode(int, int) {
302 void* _malloc_dbg(size_t size
, int, const char*, int) {
306 void* _realloc_dbg(void* ptr
, size_t size
, int, const char*, int) {
307 return realloc(ptr
, size
);
310 void _free_dbg(void* ptr
, int) {
314 void* _calloc_dbg(size_t n
, size_t size
, int, const char*, int) {
315 return calloc(n
, size
);