memory.h 3.5 KB

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