memory.h 3.2 KB

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