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"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
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.
30 // HeapSetInformation function pointer.
31 typedef BOOL (WINAPI
* HeapSetFn
)(HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
);
35 bool EnableLowFragmentationHeap() {
36 HMODULE kernel32
= GetModuleHandle(L
"kernel32.dll");
37 HeapSetFn heap_set
= reinterpret_cast<HeapSetFn
>(GetProcAddress(
39 "HeapSetInformation"));
41 // On Windows 2000, the function is not exported. This is not a reason to
46 unsigned number_heaps
= GetProcessHeaps(0, NULL
);
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());
58 for (unsigned i
= 0; i
< number_heaps
; ++i
) {
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.
63 HeapCompatibilityInformation
,
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
);
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
),
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
97 bool UncheckedMalloc(size_t size
, void** result
) {
98 *result
= malloc(size
);
99 return *result
!= NULL
;