1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file.
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#ifndef BASE_PROCESS_MEMORY_H_
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#define BASE_PROCESS_MEMORY_H_
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/base_export.h"
958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/basictypes.h"
1058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/process/process_handle.h"
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "build/build_config.h"
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <windows.h>
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifdef PVALLOC_AVAILABLE
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Build config explicitly tells us whether or not pvalloc is available.
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define PVALLOC_AVAILABLE 1
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define PVALLOC_AVAILABLE 0
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace base {
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Enables low fragmentation heap (LFH) for every heaps of this process. This
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// won't have any effect on heaps created after this function call. It will not
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// modify data allocated in the heaps before calling this function. So it is
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// better to call this function early in initialization and again before
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// entering the main loop.
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Note: Returns true on Windows 2000 without doing anything.
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT bool EnableLowFragmentationHeap();
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Enables 'terminate on heap corruption' flag. Helps protect against heap
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// overflow. Has no effect if the OS doesn't provide the necessary facility.
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT void EnableTerminationOnHeapCorruption();
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Turns on process termination if memory runs out.
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT void EnableTerminationOnOutOfMemory();
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns the module handle to which an address belongs. The reference count
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// of the module is not incremented.
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT HMODULE GetModuleFromAddress(void* address);
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_LINUX) || defined(OS_ANDROID)
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT extern size_t g_oom_size;
507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The maximum allowed value for the OOM score.
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kMaxOomScore = 1000;
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// prefer to kill certain process types over others. The range for the
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// adjustment is [-1000, 1000], with [0, 1000] being user accessible.
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If the Linux system doesn't support the newer oom_score_adj range
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// of [0, 1000], then we revert to using the older oom_adj, and
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// translate the given value into [0, 15].  Some aliasing of values
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// may occur in that case, of course.
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Special allocator functions for callers that want to check for OOM.
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// These will not abort if the allocation fails even if
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// EnableTerminationOnOutOfMemory has been called.
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// This can be useful for huge and/or unpredictable size memory allocations.
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Please only use this if you really handle the case when the allocation
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// fails. Doing otherwise would risk security.
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// These functions may still crash on OOM when running under memory tools,
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// specifically ASan and other sanitizers.
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Return value tells whether the allocation succeeded. If it fails |result| is
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// set to NULL, otherwise it holds the memory address.
74effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochBASE_EXPORT WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size,
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                    void** result);
76effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochBASE_EXPORT WARN_UNUSED_RESULT bool UncheckedCalloc(size_t num_items,
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                    size_t size,
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                    void** result);
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // namespace base
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif  // BASE_PROCESS_MEMORY_H_
83