OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This code should move into the default Windows shim once the win-specific | 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 | 6 // allocation shim has been removed, and the generic shim has becaome the |
7 // default. | 7 // default. |
8 | 8 |
9 #include "winheap_stubs_win.h" | 9 #include "winheap_stubs_win.h" |
10 | 10 |
11 #include <limits.h> | 11 #include <limits.h> |
12 #include <malloc.h> | 12 #include <malloc.h> |
13 #include <new.h> | 13 #include <new.h> |
14 #include <windows.h> | 14 #include <windows.h> |
15 | 15 |
16 #include "base/bits.h" | |
17 | |
18 namespace base { | 16 namespace base { |
19 namespace allocator { | 17 namespace allocator { |
20 | 18 |
21 bool g_is_win_shim_layer_initialized = false; | 19 bool g_is_win_shim_layer_initialized = false; |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 const size_t kWindowsPageSize = 4096; | 23 const size_t kWindowsPageSize = 4096; |
26 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | 24 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; |
27 | 25 |
28 inline HANDLE get_heap_handle() { | 26 inline HANDLE get_heap_handle() { |
29 return reinterpret_cast<HANDLE>(_get_heap_handle()); | 27 return reinterpret_cast<HANDLE>(_get_heap_handle()); |
30 } | 28 } |
31 | 29 |
30 size_t Align(size_t size, size_t alignment) { | |
Primiano Tucci (use gerrit)
2016/09/12 15:44:22
At this point probably not worth having a separate
Sigurður Ásgeirsson
2016/09/12 17:15:22
Done.
| |
31 return (size + alignment - 1) & ~(alignment - 1); | |
32 } | |
33 | |
32 } // namespace | 34 } // namespace |
33 | 35 |
34 void* WinHeapMalloc(size_t size) { | 36 void* WinHeapMalloc(size_t size) { |
35 if (size < kMaxWindowsAllocation) | 37 if (size < kMaxWindowsAllocation) |
36 return HeapAlloc(get_heap_handle(), 0, size); | 38 return HeapAlloc(get_heap_handle(), 0, size); |
37 return nullptr; | 39 return nullptr; |
38 } | 40 } |
39 | 41 |
40 void WinHeapFree(void* size) { | 42 void WinHeapFree(void* size) { |
41 HeapFree(get_heap_handle(), 0, size); | 43 HeapFree(get_heap_handle(), 0, size); |
(...skipping 22 matching lines...) Expand all Loading... | |
64 size += 8; | 66 size += 8; |
65 | 67 |
66 // Round up to the nearest allocation granularity, which is 8 for | 68 // Round up to the nearest allocation granularity, which is 8 for |
67 // 32 bit machines, and 16 for 64 bit machines. | 69 // 32 bit machines, and 16 for 64 bit machines. |
68 #if defined(ARCH_CPU_64_BITS) | 70 #if defined(ARCH_CPU_64_BITS) |
69 const size_t kAllocationGranularity = 16; | 71 const size_t kAllocationGranularity = 16; |
70 #else | 72 #else |
71 const size_t kAllocationGranularity = 8; | 73 const size_t kAllocationGranularity = 8; |
72 #endif | 74 #endif |
73 | 75 |
74 return base::bits::Align(size, kAllocationGranularity); | 76 return Align(size, kAllocationGranularity); |
75 } | 77 } |
76 | 78 |
77 // Call the new handler, if one has been set. | 79 // Call the new handler, if one has been set. |
78 // Returns true on successfully calling the handler, false otherwise. | 80 // Returns true on successfully calling the handler, false otherwise. |
79 bool WinCallNewHandler(size_t size) { | 81 bool WinCallNewHandler(size_t size) { |
80 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS | 82 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS |
81 #error "Exceptions in allocator shim are not supported!" | 83 #error "Exceptions in allocator shim are not supported!" |
82 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 84 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
83 // Get the current new handler. | 85 // Get the current new handler. |
84 _PNH nh = _query_new_handler(); | 86 _PNH nh = _query_new_handler(); |
85 if (!nh) | 87 if (!nh) |
86 return false; | 88 return false; |
87 // Since exceptions are disabled, we don't really know if new_handler | 89 // Since exceptions are disabled, we don't really know if new_handler |
88 // failed. Assume it will abort if it fails. | 90 // failed. Assume it will abort if it fails. |
89 return nh(size) ? true : false; | 91 return nh(size) ? true : false; |
90 } | 92 } |
91 | 93 |
92 } // namespace allocator | 94 } // namespace allocator |
93 } // namespace base | 95 } // namespace base |
OLD | NEW |