Chromium Code Reviews| 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 #include <limits.h> | |
| 6 #include <malloc.h> | |
| 7 #include <new.h> | |
| 8 #include <windows.h> | |
| 9 #include <stddef.h> | |
| 10 | |
| 11 #include "allocator_impl_win.h" | |
| 12 #include "allocator_impl_win.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace allocator { | |
| 16 | |
| 17 bool g_is_win_shim_layer_initialized = false; | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const size_t kWindowsPageSize = 4096; | |
| 22 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | |
| 23 | |
| 24 inline HANDLE get_heap_handle() { | |
| 25 return reinterpret_cast<HANDLE>(_get_heap_handle()); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 void* WinHeapMalloc(size_t size) { | |
|
Primiano Tucci (use gerrit)
2016/07/12 14:51:04
I wonder if all these should be inline functions i
Sigurður Ásgeirsson
2016/07/14 19:04:27
In practice this won't matter for the official bui
Primiano Tucci (use gerrit)
2016/07/15 14:02:10
SG leaving them as they are. Will see if the perf
| |
| 31 if (size < kMaxWindowsAllocation) | |
| 32 return HeapAlloc(get_heap_handle(), 0, size); | |
| 33 return nullptr; | |
| 34 } | |
| 35 | |
| 36 void WinHeapFree(void* size) { | |
| 37 HeapFree(get_heap_handle(), 0, size); | |
| 38 } | |
| 39 | |
| 40 void* WinHeapRealloc(void* ptr, size_t size) { | |
| 41 if (!ptr) | |
| 42 return WinHeapMalloc(size); | |
| 43 if (!size) { | |
| 44 WinHeapFree(ptr); | |
| 45 return nullptr; | |
| 46 } | |
| 47 if (size < kMaxWindowsAllocation) | |
| 48 return HeapReAlloc(get_heap_handle(), 0, ptr, size); | |
| 49 return nullptr; | |
| 50 } | |
| 51 | |
| 52 void* WinHeapCalloc(size_t n, size_t elem_size) { | |
| 53 // Overflow check. | |
| 54 const size_t size = n * elem_size; | |
| 55 if (elem_size != 0 && size / elem_size != n) | |
| 56 return nullptr; | |
| 57 | |
| 58 void* result = WinHeapMalloc(size); | |
| 59 if (result) { | |
| 60 memset(result, 0, size); | |
| 61 } | |
| 62 return result; | |
| 63 } | |
| 64 | |
| 65 // Call the new handler, if one has been set. | |
| 66 // Returns true on successfully calling the handler, false otherwise. | |
| 67 bool WinCallNewHandler(size_t size) { | |
| 68 // Get the current new handler. | |
| 69 _PNH nh = _query_new_handler(); | |
| 70 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
| 71 if (!nh) | |
| 72 return false; | |
| 73 // Since exceptions are disabled, we don't really know if new_handler | |
| 74 // failed. Assume it will abort if it fails. | |
| 75 return nh(size) ? true : false; | |
| 76 #else | |
| 77 #error "Exceptions in allocator shim are not supported!" | |
| 78 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
| 79 | |
| 80 } | |
| 81 | |
| 82 } // namespace allocator | |
| 83 } // namespace base} | |
| OLD | NEW |