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 const 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()); |
|
cpu_(ooo_6.6-7.5)
2015/01/07 17:25:26
use http://msdn.microsoft.com/en-us/library/csd157
Will Harris
2015/01/07 17:29:37
yes I was going to use _get_heap_handle, but for s
| |
| 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 |
| 25 return true; | 26 return true; |
| 26 } | 27 } |
| 27 | 28 |
| 28 void* win_heap_malloc(size_t size) { | 29 void* win_heap_malloc(size_t size) { |
| 29 return HeapAlloc(win_heap, 0, size); | 30 if (size < kMaxWindowsAllocation) |
| 31 return HeapAlloc(win_heap, 0, size); | |
| 32 return NULL; | |
| 30 } | 33 } |
| 31 | 34 |
| 32 void win_heap_free(void* size) { | 35 void win_heap_free(void* size) { |
| 33 HeapFree(win_heap, 0, size); | 36 HeapFree(win_heap, 0, size); |
| 34 } | 37 } |
| 35 | 38 |
| 36 void* win_heap_realloc(void* ptr, size_t size) { | 39 void* win_heap_realloc(void* ptr, size_t size) { |
| 37 if (!ptr) | 40 if (!ptr) |
| 38 return win_heap_malloc(size); | 41 return win_heap_malloc(size); |
| 39 if (!size) { | 42 if (!size) { |
| 40 win_heap_free(ptr); | 43 win_heap_free(ptr); |
| 41 return NULL; | 44 return NULL; |
| 42 } | 45 } |
| 43 return HeapReAlloc(win_heap, 0, ptr, size); | 46 if (size < kMaxWindowsAllocation) |
|
cpu_(ooo_6.6-7.5)
2015/01/07 17:25:26
if (size > kMaxWindowsAllocation)
return NULL;
| |
| 47 return HeapReAlloc(win_heap, 0, ptr, size); | |
| 48 return NULL; | |
| 44 } | 49 } |
| 45 | 50 |
| 46 size_t win_heap_msize(void* ptr) { | 51 size_t win_heap_msize(void* ptr) { |
| 47 return HeapSize(win_heap, 0, ptr); | 52 return HeapSize(win_heap, 0, ptr); |
| 48 } | 53 } |
| 49 | 54 |
| 50 void* win_heap_memalign(size_t alignment, size_t size) { | 55 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 | 56 // 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. | 57 // original allocation for use with win_heap_memalign_free() later. |
| 53 size_t allocation_size = size + (alignment - 1) + sizeof(void*); | 58 size_t allocation_size = size + (alignment - 1) + sizeof(void*); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 69 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; | 74 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; |
| 70 return aligned_ptr; | 75 return aligned_ptr; |
| 71 } | 76 } |
| 72 | 77 |
| 73 void win_heap_memalign_free(void* ptr) { | 78 void win_heap_memalign_free(void* ptr) { |
| 74 if (ptr) | 79 if (ptr) |
| 75 win_heap_free(static_cast<void**>(ptr)[-1]); | 80 win_heap_free(static_cast<void**>(ptr)[-1]); |
| 76 } | 81 } |
| 77 | 82 |
| 78 } // extern "C" | 83 } // extern "C" |
| OLD | NEW |