Roll src/third_party/WebKit 6b63e20:35e1984 (svn 201060:201061)
[chromium-blink-merge.git] / base / allocator / allocator_shim_win.cc
bloba1473e5fd29f104f5759ade6982ca40a72848e96
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.
5 #include <malloc.h>
6 #include <new.h>
7 #include <windows.h>
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.
20 extern "C" {
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.
25 // heapinit.c
26 void* _crtheap = reinterpret_cast<void*>(1);
29 namespace {
31 const size_t kWindowsPageSize = 4096;
32 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize;
33 int new_mode = 0;
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();
41 if (_crtheap == NULL)
42 return false;
44 ULONG enable_lfh = 2;
45 // NOTE: Setting LFH may fail. Vista already has it enabled.
46 // And under the debugger, it won't use LFH. So we
47 // ignore any errors.
48 HeapSetInformation(_crtheap, HeapCompatibilityInformation, &enable_lfh,
49 sizeof(enable_lfh));
51 return true;
54 void* win_heap_malloc(size_t size) {
55 if (size < kMaxWindowsAllocation)
56 return HeapAlloc(_crtheap, 0, size);
57 return NULL;
60 void win_heap_free(void* size) {
61 HeapFree(_crtheap, 0, size);
64 void* win_heap_realloc(void* ptr, size_t size) {
65 if (!ptr)
66 return win_heap_malloc(size);
67 if (!size) {
68 win_heap_free(ptr);
69 return NULL;
71 if (size < kMaxWindowsAllocation)
72 return HeapReAlloc(_crtheap, 0, ptr, size);
73 return NULL;
76 void win_heap_term() {
77 _crtheap = NULL;
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
86 if (!nh)
87 return false;
88 // Since exceptions are disabled, we don't really know if new_handler
89 // failed. Assume it will abort if it fails.
90 return nh(size);
91 #else
92 #error "Exceptions in allocator shim are not supported!"
93 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
94 return false;
97 // Implement a C++ style allocation, which always calls the new_handler
98 // on failure.
99 inline void* generic_cpp_alloc(size_t size, bool nothrow) {
100 void* ptr;
101 for (;;) {
102 ptr = malloc(size);
103 if (ptr)
104 return ptr;
105 if (!call_new_handler(nothrow, size))
106 break;
108 return ptr;
111 } // namespace
113 // new.cpp
114 void* operator new(size_t size) {
115 return generic_cpp_alloc(size, false);
118 // delete.cpp
119 void operator delete(void* p) throw() {
120 free(p);
123 // new2.cpp
124 void* operator new[](size_t size) {
125 return generic_cpp_alloc(size, false);
128 // delete2.cpp
129 void operator delete[](void* p) throw() {
130 free(p);
133 // newopnt.cpp
134 void* operator new(size_t size, const std::nothrow_t& nt) {
135 return generic_cpp_alloc(size, true);
138 // newaopnt.cpp
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.
148 // new_mode.cpp
149 int _set_new_mode(int flag) throw() {
150 int old_mode = new_mode;
151 new_mode = flag;
152 return old_mode;
155 // new_mode.cpp
156 int _query_new_mode() {
157 return new_mode;
160 extern "C" {
161 // malloc.c
162 void* malloc(size_t size) {
163 void* ptr;
164 for (;;) {
165 ptr = win_heap_malloc(size);
166 if (ptr)
167 return ptr;
169 if (!new_mode || !call_new_handler(true, size))
170 break;
172 return ptr;
175 // free.c
176 void free(void* p) {
177 win_heap_free(p);
178 return;
181 // realloc.c
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.
186 if (!ptr)
187 return malloc(size);
189 void* new_ptr;
190 for (;;) {
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
195 // NULL.
196 if (new_ptr || !size)
197 return new_ptr;
198 if (!new_mode || !call_new_handler(true, size))
199 break;
201 return new_ptr;
204 // heapinit.c
205 intptr_t _get_heap_handle() {
206 return reinterpret_cast<intptr_t>(_crtheap);
209 // heapinit.c
210 int _heap_init() {
211 return win_heap_init() ? 1 : 0;
214 // heapinit.c
215 void _heap_term() {
216 win_heap_term();
219 // calloc.c
220 void* calloc(size_t n, size_t elem_size) {
221 // Overflow check.
222 const size_t size = n * elem_size;
223 if (elem_size != 0 && size / elem_size != n)
224 return NULL;
226 void* result = malloc(size);
227 if (result != NULL) {
228 memset(result, 0, size);
230 return result;
233 // recalloc.c
234 void* _recalloc(void* p, size_t n, size_t elem_size) {
235 if (!p)
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)
247 return NULL;
248 return realloc(p, size);
251 // calloc_impl.c
252 void* _calloc_impl(size_t n, size_t size) {
253 return calloc(n, size);
256 #ifndef NDEBUG
257 #undef malloc
258 #undef free
259 #undef calloc
261 static int error_handler(int reportType) {
262 switch (reportType) {
263 case 0: // _CRT_WARN
264 __debugbreak();
265 return 0;
267 case 1: // _CRT_ERROR
268 __debugbreak();
269 return 0;
271 case 2: // _CRT_ASSERT
272 __debugbreak();
273 return 0;
275 char* p = NULL;
276 *p = '\0';
277 return 0;
280 int _CrtDbgReport(int reportType,
281 const char*,
282 int,
283 const char*,
284 const char*,
285 ...) {
286 return error_handler(reportType);
289 int _CrtDbgReportW(int reportType,
290 const wchar_t*,
291 int,
292 const wchar_t*,
293 const wchar_t*,
294 ...) {
295 return error_handler(reportType);
298 int _CrtSetReportMode(int, int) {
299 return 0;
302 void* _malloc_dbg(size_t size, int, const char*, int) {
303 return malloc(size);
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) {
311 free(ptr);
314 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
315 return calloc(n, size);
317 #endif // NDEBUG
319 } // extern C