OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 // This code should move into the default Windows shim once the win-specific | |
6 // allocation shim has been removed, and the generic shim has becaome the | |
7 // default. | |
8 | |
9 #include "winheap_stubs_win.h" | |
10 | |
11 #include <limits.h> | |
12 #include <malloc.h> | |
13 #include <new.h> | |
14 #include <windows.h> | |
15 | |
16 namespace base { | |
17 namespace allocator { | |
18 | |
19 bool g_is_win_shim_layer_initialized = false; | |
20 | |
21 namespace { | |
22 | |
23 const size_t kWindowsPageSize = 4096; | |
24 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | |
25 | |
26 inline HANDLE get_heap_handle() { | |
27 return reinterpret_cast<HANDLE>(_get_heap_handle()); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 void* WinHeapMalloc(size_t size) { | |
33 if (size < kMaxWindowsAllocation) | |
34 return HeapAlloc(get_heap_handle(), 0, size); | |
35 return nullptr; | |
36 } | |
37 | |
38 void WinHeapFree(void* size) { | |
39 HeapFree(get_heap_handle(), 0, size); | |
40 } | |
41 | |
42 void* WinHeapRealloc(void* ptr, size_t size) { | |
43 if (!ptr) | |
44 return WinHeapMalloc(size); | |
45 if (!size) { | |
46 WinHeapFree(ptr); | |
47 return nullptr; | |
48 } | |
49 if (size < kMaxWindowsAllocation) | |
50 return HeapReAlloc(get_heap_handle(), 0, ptr, size); | |
51 return nullptr; | |
52 } | |
53 | |
54 // Call the new handler, if one has been set. | |
55 // Returns true on successfully calling the handler, false otherwise. | |
56 bool WinCallNewHandler(size_t size) { | |
57 // Get the current new handler. | |
58 _PNH nh = _query_new_handler(); | |
59 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
60 if (!nh) | |
61 return false; | |
62 // Since exceptions are disabled, we don't really know if new_handler | |
63 // failed. Assume it will abort if it fails. | |
64 return nh(size) ? true : false; | |
65 #else | |
66 #error "Exceptions in allocator shim are not supported!" | |
Nico
2016/07/14 19:13:33
nit: I'd do
#if !defined(_HAS_EXCEPTIONS) || _HAS
Sigurður Ásgeirsson
2016/07/18 13:42:04
Done.
| |
67 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
68 } | |
69 | |
70 } // namespace allocator | |
71 } // namespace base | |
OLD | NEW |