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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 size += 8; | 62 size += 8; |
65 | 63 |
66 // Round up to the nearest allocation granularity, which is 8 for | 64 // Round up to the nearest allocation granularity, which is 8 for |
67 // 32 bit machines, and 16 for 64 bit machines. | 65 // 32 bit machines, and 16 for 64 bit machines. |
68 #if defined(ARCH_CPU_64_BITS) | 66 #if defined(ARCH_CPU_64_BITS) |
69 const size_t kAllocationGranularity = 16; | 67 const size_t kAllocationGranularity = 16; |
70 #else | 68 #else |
71 const size_t kAllocationGranularity = 8; | 69 const size_t kAllocationGranularity = 8; |
72 #endif | 70 #endif |
73 | 71 |
74 return base::bits::Align(size, kAllocationGranularity); | 72 return (size + kAllocationGranularity - 1) & ~(kAllocationGranularity - 1); |
75 } | 73 } |
76 | 74 |
77 // Call the new handler, if one has been set. | 75 // Call the new handler, if one has been set. |
78 // Returns true on successfully calling the handler, false otherwise. | 76 // Returns true on successfully calling the handler, false otherwise. |
79 bool WinCallNewHandler(size_t size) { | 77 bool WinCallNewHandler(size_t size) { |
80 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS | 78 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS |
81 #error "Exceptions in allocator shim are not supported!" | 79 #error "Exceptions in allocator shim are not supported!" |
82 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
83 // Get the current new handler. | 81 // Get the current new handler. |
84 _PNH nh = _query_new_handler(); | 82 _PNH nh = _query_new_handler(); |
85 if (!nh) | 83 if (!nh) |
86 return false; | 84 return false; |
87 // Since exceptions are disabled, we don't really know if new_handler | 85 // Since exceptions are disabled, we don't really know if new_handler |
88 // failed. Assume it will abort if it fails. | 86 // failed. Assume it will abort if it fails. |
89 return nh(size) ? true : false; | 87 return nh(size) ? true : false; |
90 } | 88 } |
91 | 89 |
92 } // namespace allocator | 90 } // namespace allocator |
93 } // namespace base | 91 } // namespace base |
OLD | NEW |