memory.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.
  4. #ifndef BASE_PROCESS_MEMORY_H_
  5. #define BASE_PROCESS_MEMORY_H_
  6. #include <stddef.h>
  7. #include "base/base_export.h"
  8. #include "base/process/process_handle.h"
  9. #include "build/build_config.h"
  10. namespace base {
  11. // Enables 'terminate on heap corruption' flag. Helps protect against heap
  12. // overflow. Has no effect if the OS doesn't provide the necessary facility.
  13. BASE_EXPORT void EnableTerminationOnHeapCorruption();
  14. // Turns on process termination if memory runs out.
  15. BASE_EXPORT void EnableTerminationOnOutOfMemory();
  16. // Terminates process. Should be called only for out of memory errors.
  17. // Crash reporting classifies such crashes as OOM.
  18. BASE_EXPORT void TerminateBecauseOutOfMemory(size_t size);
  19. #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
  20. BASE_EXPORT extern size_t g_oom_size;
  21. // The maximum allowed value for the OOM score.
  22. const int kMaxOomScore = 1000;
  23. // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
  24. // prefer to kill certain process types over others. The range for the
  25. // adjustment is [-1000, 1000], with [0, 1000] being user accessible.
  26. // If the Linux system doesn't support the newer oom_score_adj range
  27. // of [0, 1000], then we revert to using the older oom_adj, and
  28. // translate the given value into [0, 15]. Some aliasing of values
  29. // may occur in that case, of course.
  30. BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
  31. #endif
  32. namespace internal {
  33. // Returns true if address-space was released. Some configurations reserve part
  34. // of the process address-space for special allocations (e.g. WASM).
  35. bool ReleaseAddressSpaceReservation();
  36. } // namespace internal
  37. #if defined(OS_WIN)
  38. namespace win {
  39. // Custom Windows exception code chosen to indicate an out of memory error.
  40. // See https://msdn.microsoft.com/en-us/library/het71c37.aspx.
  41. // "To make sure that you do not define a code that conflicts with an existing
  42. // exception code" ... "The resulting error code should therefore have the
  43. // highest four bits set to hexadecimal E."
  44. // 0xe0000008 was chosen arbitrarily, as 0x00000008 is ERROR_NOT_ENOUGH_MEMORY.
  45. const DWORD kOomExceptionCode = 0xe0000008;
  46. } // namespace win
  47. #endif
  48. namespace internal {
  49. // Handles out of memory, with the failed allocation |size|, or 0 when it is not
  50. // known.
  51. BASE_EXPORT NOINLINE void OnNoMemoryInternal(size_t size);
  52. } // namespace internal
  53. // Special allocator functions for callers that want to check for OOM.
  54. // These will not abort if the allocation fails even if
  55. // EnableTerminationOnOutOfMemory has been called.
  56. // This can be useful for huge and/or unpredictable size memory allocations.
  57. // Please only use this if you really handle the case when the allocation
  58. // fails. Doing otherwise would risk security.
  59. // These functions may still crash on OOM when running under memory tools,
  60. // specifically ASan and other sanitizers.
  61. // Return value tells whether the allocation succeeded. If it fails |result| is
  62. // set to NULL, otherwise it holds the memory address.
  63. BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size,
  64. void** result);
  65. BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedCalloc(size_t num_items,
  66. size_t size,
  67. void** result);
  68. } // namespace base
  69. #endif // BASE_PROCESS_MEMORY_H_