OLD | NEW |
---|---|
(Empty) | |
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. | |
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 void* WinHeapCalloc(size_t n, size_t elem_size) { | |
Primiano Tucci (use gerrit)
2016/07/13 17:42:49
Not sure you need this here. This function is not
Sigurður Ásgeirsson
2016/07/13 18:47:45
Removed, thanks.
| |
55 // Overflow check. | |
56 const size_t size = n * elem_size; | |
57 if (elem_size != 0 && size / elem_size != n) | |
58 return nullptr; | |
59 | |
60 void* result = WinHeapMalloc(size); | |
61 if (result) { | |
62 memset(result, 0, size); | |
63 } | |
64 return result; | |
65 } | |
66 | |
67 // Call the new handler, if one has been set. | |
68 // Returns true on successfully calling the handler, false otherwise. | |
69 bool WinCallNewHandler(size_t size) { | |
Will Harris
2016/07/13 18:10:37
this function might need to be added to the "funct
Sigurður Ásgeirsson
2016/07/13 18:47:45
Acknowledged.
| |
70 // Get the current new handler. | |
71 _PNH nh = _query_new_handler(); | |
72 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
73 if (!nh) | |
74 return false; | |
75 // Since exceptions are disabled, we don't really know if new_handler | |
76 // failed. Assume it will abort if it fails. | |
77 return nh(size) ? true : false; | |
78 #else | |
79 #error "Exceptions in allocator shim are not supported!" | |
80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
81 } | |
82 | |
83 } // namespace allocator | |
84 } // namespace base | |
OLD | NEW |