| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "base/process/memory.h" | 5 #include "base/process/memory.h" |
| 6 | 6 |
| 7 #include <new.h> | 7 #include <new.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 return 0; | 43 return 0; |
| 44 } | 44 } |
| 45 | 45 |
| 46 #pragma warning(pop) | 46 #pragma warning(pop) |
| 47 | 47 |
| 48 // HeapSetInformation function pointer. | 48 // HeapSetInformation function pointer. |
| 49 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 49 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 bool EnableLowFragmentationHeap() { | |
| 54 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll"); | |
| 55 HeapSetFn heap_set = reinterpret_cast<HeapSetFn>(GetProcAddress( | |
| 56 kernel32, | |
| 57 "HeapSetInformation")); | |
| 58 | |
| 59 // On Windows 2000, the function is not exported. This is not a reason to | |
| 60 // fail. | |
| 61 if (!heap_set) | |
| 62 return true; | |
| 63 | |
| 64 unsigned number_heaps = GetProcessHeaps(0, NULL); | |
| 65 if (!number_heaps) | |
| 66 return false; | |
| 67 | |
| 68 // Gives us some extra space in the array in case a thread is creating heaps | |
| 69 // at the same time we're querying them. | |
| 70 static const int MARGIN = 8; | |
| 71 scoped_ptr<HANDLE[]> heaps(new HANDLE[number_heaps + MARGIN]); | |
| 72 number_heaps = GetProcessHeaps(number_heaps + MARGIN, heaps.get()); | |
| 73 if (!number_heaps) | |
| 74 return false; | |
| 75 | |
| 76 for (unsigned i = 0; i < number_heaps; ++i) { | |
| 77 ULONG lfh_flag = 2; | |
| 78 // Don't bother with the result code. It may fails on heaps that have the | |
| 79 // HEAP_NO_SERIALIZE flag. This is expected and not a problem at all. | |
| 80 heap_set(heaps[i], | |
| 81 HeapCompatibilityInformation, | |
| 82 &lfh_flag, | |
| 83 sizeof(lfh_flag)); | |
| 84 } | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 void EnableTerminationOnHeapCorruption() { | 53 void EnableTerminationOnHeapCorruption() { |
| 89 // Ignore the result code. Supported on XP SP3 and Vista. | 54 // Ignore the result code. Supported on XP SP3 and Vista. |
| 90 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 55 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
| 91 } | 56 } |
| 92 | 57 |
| 93 void EnableTerminationOnOutOfMemory() { | 58 void EnableTerminationOnOutOfMemory() { |
| 94 _set_new_handler(&OnNoMemory); | 59 _set_new_handler(&OnNoMemory); |
| 95 _set_new_mode(1); | 60 _set_new_mode(1); |
| 96 } | 61 } |
| 97 | 62 |
| 98 HMODULE GetModuleFromAddress(void* address) { | 63 HMODULE GetModuleFromAddress(void* address) { |
| 99 HMODULE instance = NULL; | 64 HMODULE instance = NULL; |
| 100 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | 65 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 101 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | 66 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 102 static_cast<char*>(address), | 67 static_cast<char*>(address), |
| 103 &instance)) { | 68 &instance)) { |
| 104 NOTREACHED(); | 69 NOTREACHED(); |
| 105 } | 70 } |
| 106 return instance; | 71 return instance; |
| 107 } | 72 } |
| 108 | 73 |
| 109 // Implemented using a weak symbol. | 74 // Implemented using a weak symbol. |
| 110 bool UncheckedMalloc(size_t size, void** result) { | 75 bool UncheckedMalloc(size_t size, void** result) { |
| 111 *result = malloc_unchecked(size); | 76 *result = malloc_unchecked(size); |
| 112 return *result != NULL; | 77 return *result != NULL; |
| 113 } | 78 } |
| 114 | 79 |
| 115 } // namespace base | 80 } // namespace base |
| OLD | NEW |