memory.h revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
151a8d8528135ba4e3e4cf7cd711a9e47b19078a3Chris Lattner// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// Use of this source code is governed by a BSD-style license that can be
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// found in the LICENSE file.
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner#ifndef BASE_PROCESS_MEMORY_H_
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner#define BASE_PROCESS_MEMORY_H_
7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell#include "base/base_export.h"
9ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman#include "base/basictypes.h"
10e8b5413e5d0c7c0fc5b384e975c4ca87f4c00699Chris Lattner#include "base/process/process_handle.h"
11e8b5413e5d0c7c0fc5b384e975c4ca87f4c00699Chris Lattner#include "build/build_config.h"
1251a8d8528135ba4e3e4cf7cd711a9e47b19078a3Chris Lattner
13deb9654056939a12981446f6ed1139dca3412746Vikram S. Adve#if defined(OS_WIN)
14fce1143bcfa73f61845002fa50473d1a01384202Misha Brukman#include <windows.h>
15fce1143bcfa73f61845002fa50473d1a01384202Misha Brukman#endif
16deb9654056939a12981446f6ed1139dca3412746Vikram S. Adve
17c0b9dc5be79f009d260edb5cd5e1d8346587aaa2Alkis Evlogimenosnamespace base {
18551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer
197cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// Enables low fragmentation heap (LFH) for every heaps of this process. This
2013ba2dab631636e525a44bb259aaea56a860d1c7Jakob Stoklund Olesen// won't have any effect on heaps created after this function call. It will not
21d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// modify data allocated in the heaps before calling this function. So it is
22d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// better to call this function early in initialization and again before
23fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// entering the main loop.
24853d3fb8d24fab2258e9cd5dce3ec8ff4189eedaDan Gohman// Note: Returns true on Windows 2000 without doing anything.
25fed90b6d097d50881afb45e4d79f430db66dd741Dan GohmanBASE_EXPORT bool EnableLowFragmentationHeap();
26fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman
27a655f088b88cf015fc48721fd9869787d1b8ce13Chris Lattner// Enables 'terminate on heap corruption' flag. Helps protect against heap
28f4a1e1a69f0727762a73ef0d551e3bbd16b7c04eJakob Stoklund Olesen// overflow. Has no effect if the OS doesn't provide the necessary facility.
29a655f088b88cf015fc48721fd9869787d1b8ce13Chris LattnerBASE_EXPORT void EnableTerminationOnHeapCorruption();
301cd1d98232c3c3a0bd3810c3bf6c2572ea02f208Daniel Dunbar
317cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// Turns on process termination if memory runs out.
32d0fde30ce850b78371fd1386338350591f9ff494Brian GaekeBASE_EXPORT void EnableTerminationOnOutOfMemory();
3394dc07728f091c652f0a8059aba6dce5018485eeAlkis Evlogimenos
348e4018e2de52c534405d7155c7009d0b35afb861Cedric Venet#if defined(OS_WIN)
358e4018e2de52c534405d7155c7009d0b35afb861Cedric Venet// Returns the module handle to which an address belongs. The reference count
367309be6735666143bd9835b275dc8501617a2591Gabor Greif// of the module is not incremented.
37fed90b6d097d50881afb45e4d79f430db66dd741Dan GohmanBASE_EXPORT HMODULE GetModuleFromAddress(void* address);
388e5f2c6f65841542e2a7092553fe42a00048e4c7Dan Gohman#endif
3994dc07728f091c652f0a8059aba6dce5018485eeAlkis Evlogimenos
408e5f2c6f65841542e2a7092553fe42a00048e4c7Dan Gohman#if defined(OS_LINUX) || defined(OS_ANDROID)
4194dc07728f091c652f0a8059aba6dce5018485eeAlkis EvlogimenosBASE_EXPORT extern size_t g_oom_size;
428e5f2c6f65841542e2a7092553fe42a00048e4c7Dan Gohman
43c7f6b8c5d40e17bf43fd3a1549d7d89c9da735e1Gabor Greif// The maximum allowed value for the OOM score.
44c7f6b8c5d40e17bf43fd3a1549d7d89c9da735e1Gabor Greifconst int kMaxOomScore = 1000;
45c7f6b8c5d40e17bf43fd3a1549d7d89c9da735e1Gabor Greif
46fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
4794dc07728f091c652f0a8059aba6dce5018485eeAlkis Evlogimenos// prefer to kill certain process types over others. The range for the
48c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif// adjustment is [-1000, 1000], with [0, 1000] being user accessible.
49c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif// If the Linux system doesn't support the newer oom_score_adj range
50f3841fcbd587c31aa9842b3f33bd57de40c9f443Gabor Greif// of [0, 1000], then we revert to using the older oom_adj, and
51c23b8719ef9d6b1220e854b37d40e9e1c48a82bcGabor Greif// translate the given value into [0, 15].  Some aliasing of values
52aad5c0505183a5b7913f1a443a1f0650122551ccAlkis Evlogimenos// may occur in that case, of course.
53aad5c0505183a5b7913f1a443a1f0650122551ccAlkis EvlogimenosBASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
54fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman#endif
55fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman
56fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman#if defined(OS_MACOSX)
578e5f2c6f65841542e2a7092553fe42a00048e4c7Dan Gohman// Very large images or svg canvases can cause huge mallocs.  Skia
58fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// does tricks on tcmalloc-based systems to allow malloc to fail with
59fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// a NULL rather than hit the oom crasher.  This replicates that for
6094dc07728f091c652f0a8059aba6dce5018485eeAlkis Evlogimenos// OSX.
6194dc07728f091c652f0a8059aba6dce5018485eeAlkis Evlogimenos//
62fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// IF YOU USE THIS WITHOUT CONSULTING YOUR FRIENDLY OSX DEVELOPER,
63fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman// YOUR CODE IS LIKELY TO BE REVERTED.  THANK YOU.
64c0b9dc5be79f009d260edb5cd5e1d8346587aaa2Alkis EvlogimenosBASE_EXPORT void* UncheckedMalloc(size_t size);
651194e9501984daf0d3237ed1bf18a156173e7fd4Chris Lattner#endif  // defined(OS_MACOSX)
66c07d8d8a26f63dfc54dbd0e1ff776763ec6443adBrian Gaeke
67f20c1a497fe3922ac718429d65a5fe396890575eChris Lattner}  // namespace base
687cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak
6913d828567812041c1ca1817f4b66fce840903a1fEvan Cheng#endif  // BASE_PROCESS_MEMORY_H_
7013d828567812041c1ca1817f4b66fce840903a1fEvan Cheng