Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / process / memory_win.cc
blobfc57b48f1f768ccbeec35dfdef0e358c11533600
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
3 // found in the LICENSE file.
5 #include "base/process/memory.h"
7 #include <new.h>
8 #include <psapi.h>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
13 namespace base {
15 namespace {
17 #pragma warning(push)
18 #pragma warning(disable: 4702)
20 int OnNoMemory(size_t) {
21 // Kill the process. This is important for security since most of code
22 // does not check the result of memory allocation.
23 __debugbreak();
24 _exit(1);
25 return 0;
28 #pragma warning(pop)
30 // HeapSetInformation function pointer.
31 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
33 } // namespace
35 bool EnableLowFragmentationHeap() {
36 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll");
37 HeapSetFn heap_set = reinterpret_cast<HeapSetFn>(GetProcAddress(
38 kernel32,
39 "HeapSetInformation"));
41 // On Windows 2000, the function is not exported. This is not a reason to
42 // fail.
43 if (!heap_set)
44 return true;
46 unsigned number_heaps = GetProcessHeaps(0, NULL);
47 if (!number_heaps)
48 return false;
50 // Gives us some extra space in the array in case a thread is creating heaps
51 // at the same time we're querying them.
52 static const int MARGIN = 8;
53 scoped_ptr<HANDLE[]> heaps(new HANDLE[number_heaps + MARGIN]);
54 number_heaps = GetProcessHeaps(number_heaps + MARGIN, heaps.get());
55 if (!number_heaps)
56 return false;
58 for (unsigned i = 0; i < number_heaps; ++i) {
59 ULONG lfh_flag = 2;
60 // Don't bother with the result code. It may fails on heaps that have the
61 // HEAP_NO_SERIALIZE flag. This is expected and not a problem at all.
62 heap_set(heaps[i],
63 HeapCompatibilityInformation,
64 &lfh_flag,
65 sizeof(lfh_flag));
67 return true;
70 void EnableTerminationOnHeapCorruption() {
71 // Ignore the result code. Supported on XP SP3 and Vista.
72 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
75 void EnableTerminationOnOutOfMemory() {
76 _set_new_handler(&OnNoMemory);
77 _set_new_mode(1);
80 HMODULE GetModuleFromAddress(void* address) {
81 HMODULE instance = NULL;
82 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
83 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
84 static_cast<char*>(address),
85 &instance)) {
86 NOTREACHED();
88 return instance;
91 // TODO(b.kelemen): implement it with the required semantics. On Linux this is
92 // implemented with a weak symbol that is overridden by tcmalloc. This is
93 // neccessary because base cannot have a direct dependency on tcmalloc. Since
94 // weak symbols are not supported on Windows this will involve some build time
95 // magic, much like what is done for libcrt in order to override the allocation
96 // functions.
97 bool UncheckedMalloc(size_t size, void** result) {
98 *result = malloc(size);
99 return *result != NULL;
102 } // namespace base