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 |
16 namespace base { | 18 namespace base { |
17 namespace allocator { | 19 namespace allocator { |
18 | 20 |
19 bool g_is_win_shim_layer_initialized = false; | 21 bool g_is_win_shim_layer_initialized = false; |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 const size_t kWindowsPageSize = 4096; | 25 const size_t kWindowsPageSize = 4096; |
24 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | 26 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; |
25 | 27 |
(...skipping 18 matching lines...) Expand all Loading... |
44 return WinHeapMalloc(size); | 46 return WinHeapMalloc(size); |
45 if (!size) { | 47 if (!size) { |
46 WinHeapFree(ptr); | 48 WinHeapFree(ptr); |
47 return nullptr; | 49 return nullptr; |
48 } | 50 } |
49 if (size < kMaxWindowsAllocation) | 51 if (size < kMaxWindowsAllocation) |
50 return HeapReAlloc(get_heap_handle(), 0, ptr, size); | 52 return HeapReAlloc(get_heap_handle(), 0, ptr, size); |
51 return nullptr; | 53 return nullptr; |
52 } | 54 } |
53 | 55 |
| 56 size_t WinHeapGetSizeEstimate(void* ptr) { |
| 57 if (!ptr) |
| 58 return 0; |
| 59 |
| 60 // Get the user size of the allocation. |
| 61 size_t size = HeapSize(get_heap_handle(), 0, ptr); |
| 62 |
| 63 // Account for the 8-byte HEAP_HEADER preceding the block. |
| 64 size += 8; |
| 65 |
| 66 // Round up to the nearest allocation granularity, which is 8 for |
| 67 // 32 bit machines, and 16 for 64 bit machines. |
| 68 #if defined(ARCH_CPU_64_BITS) |
| 69 const size_t kAllocationGranularity = 16; |
| 70 #else |
| 71 const size_t kAllocationGranularity = 8; |
| 72 #endif |
| 73 |
| 74 return base::bits::Align(size, kAllocationGranularity); |
| 75 } |
| 76 |
54 // Call the new handler, if one has been set. | 77 // Call the new handler, if one has been set. |
55 // Returns true on successfully calling the handler, false otherwise. | 78 // Returns true on successfully calling the handler, false otherwise. |
56 bool WinCallNewHandler(size_t size) { | 79 bool WinCallNewHandler(size_t size) { |
57 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS | 80 #if !defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS |
58 #error "Exceptions in allocator shim are not supported!" | 81 #error "Exceptions in allocator shim are not supported!" |
59 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 82 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
60 // Get the current new handler. | 83 // Get the current new handler. |
61 _PNH nh = _query_new_handler(); | 84 _PNH nh = _query_new_handler(); |
62 if (!nh) | 85 if (!nh) |
63 return false; | 86 return false; |
64 // Since exceptions are disabled, we don't really know if new_handler | 87 // Since exceptions are disabled, we don't really know if new_handler |
65 // failed. Assume it will abort if it fails. | 88 // failed. Assume it will abort if it fails. |
66 return nh(size) ? true : false; | 89 return nh(size) ? true : false; |
67 } | 90 } |
68 | 91 |
69 } // namespace allocator | 92 } // namespace allocator |
70 } // namespace base | 93 } // namespace base |
OLD | NEW |