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 #ifndef BASE_PROCESS_MEMORY_H_
6 #define BASE_PROCESS_MEMORY_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
10 #include "base/process/process_handle.h"
11 #include "build/build_config.h"
17 #ifdef PVALLOC_AVAILABLE
18 // Build config explicitly tells us whether or not pvalloc is available.
19 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
20 #define PVALLOC_AVAILABLE 1
22 #define PVALLOC_AVAILABLE 0
27 // Enables low fragmentation heap (LFH) for every heaps of this process. This
28 // won't have any effect on heaps created after this function call. It will not
29 // modify data allocated in the heaps before calling this function. So it is
30 // better to call this function early in initialization and again before
31 // entering the main loop.
32 // Note: Returns true on Windows 2000 without doing anything.
33 BASE_EXPORT
bool EnableLowFragmentationHeap();
35 // Enables 'terminate on heap corruption' flag. Helps protect against heap
36 // overflow. Has no effect if the OS doesn't provide the necessary facility.
37 BASE_EXPORT
void EnableTerminationOnHeapCorruption();
39 // Turns on process termination if memory runs out.
40 BASE_EXPORT
void EnableTerminationOnOutOfMemory();
42 // Terminates process. Should be called only for out of memory errors.
43 // Crash reporting classifies such crashes as OOM.
44 BASE_EXPORT
void TerminateBecauseOutOfMemory(size_t size
);
47 // Returns the module handle to which an address belongs. The reference count
48 // of the module is not incremented.
49 BASE_EXPORT HMODULE
GetModuleFromAddress(void* address
);
52 #if defined(OS_LINUX) || defined(OS_ANDROID)
53 BASE_EXPORT
extern size_t g_oom_size
;
55 // The maximum allowed value for the OOM score.
56 const int kMaxOomScore
= 1000;
58 // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
59 // prefer to kill certain process types over others. The range for the
60 // adjustment is [-1000, 1000], with [0, 1000] being user accessible.
61 // If the Linux system doesn't support the newer oom_score_adj range
62 // of [0, 1000], then we revert to using the older oom_adj, and
63 // translate the given value into [0, 15]. Some aliasing of values
64 // may occur in that case, of course.
65 BASE_EXPORT
bool AdjustOOMScore(ProcessId process
, int score
);
68 // Special allocator functions for callers that want to check for OOM.
69 // These will not abort if the allocation fails even if
70 // EnableTerminationOnOutOfMemory has been called.
71 // This can be useful for huge and/or unpredictable size memory allocations.
72 // Please only use this if you really handle the case when the allocation
73 // fails. Doing otherwise would risk security.
74 // These functions may still crash on OOM when running under memory tools,
75 // specifically ASan and other sanitizers.
76 // Return value tells whether the allocation succeeded. If it fails |result| is
77 // set to NULL, otherwise it holds the memory address.
78 BASE_EXPORT WARN_UNUSED_RESULT
bool UncheckedMalloc(size_t size
,
80 BASE_EXPORT WARN_UNUSED_RESULT
bool UncheckedCalloc(size_t num_items
,
86 #endif // BASE_PROCESS_MEMORY_H_