Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 is a simple allocator based on the windows heap. | 5 // This is a simple allocator based on the windows heap. |
| 6 | 6 |
| 7 extern "C" { | 7 extern "C" { |
| 8 | 8 |
| 9 const size_t kWindowsPageSize = 4096; | |
| 10 size_t kMaxWindowsAllocation = | |
| 11 std::numeric_limits<int>::max() - kWindowsPageSize; | |
| 9 HANDLE win_heap; | 12 HANDLE win_heap; |
| 10 | 13 |
| 11 bool win_heap_init(bool use_lfh) { | 14 bool win_heap_init() { |
| 12 win_heap = HeapCreate(0, 0, 0); | 15 win_heap = static_cast<HANDLE>(GetProcessHeap()); |
| 13 if (win_heap == NULL) | 16 if (win_heap == NULL) |
| 14 return false; | 17 return false; |
| 15 | 18 |
| 16 if (use_lfh) { | 19 ULONG enable_lfh = 2; |
| 17 ULONG enable_lfh = 2; | 20 // NOTE: Setting LFH may fail. Vista already has it enabled. |
| 18 HeapSetInformation(win_heap, HeapCompatibilityInformation, | 21 // And under the debugger, it won't use LFH. So we |
| 19 &enable_lfh, sizeof(enable_lfh)); | 22 // ignore any errors. |
| 20 // NOTE: Setting LFH may fail. Vista already has it enabled. | 23 HeapSetInformation(win_heap, HeapCompatibilityInformation, &enable_lfh, |
| 21 // And under the debugger, it won't use LFH. So we | 24 sizeof(enable_lfh)); |
| 22 // ignore any errors. | |
| 23 } | |
| 24 | 25 |
| 26 // In Debug, _mt_init() performs an allocation before _cinit is called to | |
| 27 // initalize C data, so manually initialize kMaxWindowsAllocation here. | |
| 28 kMaxWindowsAllocation = std::numeric_limits<int>::max() - kWindowsPageSize; | |
| 25 return true; | 29 return true; |
| 26 } | 30 } |
| 27 | 31 |
| 28 void* win_heap_malloc(size_t size) { | 32 void* win_heap_malloc(size_t size) { |
| 29 return HeapAlloc(win_heap, 0, size); | 33 if (size < kMaxWindowsAllocation) |
| 34 return HeapAlloc(win_heap, 0, size); | |
| 35 return NULL; | |
| 30 } | 36 } |
| 31 | 37 |
| 32 void win_heap_free(void* size) { | 38 void win_heap_free(void* size) { |
| 33 HeapFree(win_heap, 0, size); | 39 HeapFree(win_heap, 0, size); |
| 34 } | 40 } |
| 35 | 41 |
| 36 void* win_heap_realloc(void* ptr, size_t size) { | 42 void* win_heap_realloc(void* ptr, size_t size) { |
| 37 if (!ptr) | 43 if (!ptr) |
| 38 return win_heap_malloc(size); | 44 return win_heap_malloc(size); |
| 39 if (!size) { | 45 if (!size) { |
| 40 win_heap_free(ptr); | 46 win_heap_free(ptr); |
| 41 return NULL; | 47 return NULL; |
| 42 } | 48 } |
| 43 return HeapReAlloc(win_heap, 0, ptr, size); | 49 if (size < kMaxWindowsAllocation) |
| 50 return HeapReAlloc(win_heap, 0, ptr, size); | |
| 51 return NULL; | |
| 44 } | 52 } |
| 45 | 53 |
| 46 size_t win_heap_msize(void* ptr) { | 54 size_t win_heap_msize(void* ptr) { |
| 47 return HeapSize(win_heap, 0, ptr); | 55 return HeapSize(win_heap, 0, ptr); |
| 48 } | 56 } |
| 49 | 57 |
| 50 void* win_heap_memalign(size_t alignment, size_t size) { | 58 void* win_heap_memalign(size_t alignment, size_t size) { |
| 51 // Reserve enough space to ensure we can align and set aligned_ptr[-1] to the | 59 // Reserve enough space to ensure we can align and set aligned_ptr[-1] to the |
| 52 // original allocation for use with win_heap_memalign_free() later. | 60 // original allocation for use with win_heap_memalign_free() later. |
| 53 size_t allocation_size = size + (alignment - 1) + sizeof(void*); | 61 size_t allocation_size = size + (alignment - 1) + sizeof(void*); |
| 54 | 62 |
| 55 // Check for overflow. Alignment and size are checked in allocator_shim. | 63 // Check for overflow. Alignment and size are checked in allocator_shim. |
| 56 DCHECK_LT(size, allocation_size); | 64 DCHECK_LT(size, allocation_size); |
| 57 DCHECK_LT(alignment, allocation_size); | 65 DCHECK_LT(alignment, allocation_size); |
|
cpu_(ooo_6.6-7.5)
2015/01/08 18:05:43
same deal with all these logging stuff.
Will Harris
2015/01/08 22:10:57
Done.
| |
| 58 | 66 |
| 59 // Since we're directly calling the allocator function, before OOM handling, | 67 // Since we're directly calling the allocator function, before OOM handling, |
| 60 // we need to NULL check to ensure the allocation succeeded. | 68 // we need to NULL check to ensure the allocation succeeded. |
| 61 void* ptr = win_heap_malloc(allocation_size); | 69 void* ptr = win_heap_malloc(allocation_size); |
| 62 if (!ptr) | 70 if (!ptr) |
| 63 return ptr; | 71 return ptr; |
| 64 | 72 |
| 65 char* aligned_ptr = static_cast<char*>(ptr) + sizeof(void*); | 73 char* aligned_ptr = static_cast<char*>(ptr) + sizeof(void*); |
| 66 aligned_ptr += | 74 aligned_ptr += |
| 67 alignment - reinterpret_cast<uintptr_t>(aligned_ptr) & (alignment - 1); | 75 alignment - reinterpret_cast<uintptr_t>(aligned_ptr) & (alignment - 1); |
| 68 | 76 |
| 69 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; | 77 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; |
| 70 return aligned_ptr; | 78 return aligned_ptr; |
| 71 } | 79 } |
| 72 | 80 |
| 73 void win_heap_memalign_free(void* ptr) { | 81 void win_heap_memalign_free(void* ptr) { |
| 74 if (ptr) | 82 if (ptr) |
| 75 win_heap_free(static_cast<void**>(ptr)[-1]); | 83 win_heap_free(static_cast<void**>(ptr)[-1]); |
| 76 } | 84 } |
| 77 | 85 |
| 78 } // extern "C" | 86 } // extern "C" |
| OLD | NEW |