mozmemory.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef mozmemory_h
  5. #define mozmemory_h
  6. /*
  7. * This header is meant to be used when the following functions are
  8. * necessary:
  9. * - malloc_good_size (used to be called je_malloc_usable_in_advance)
  10. * - jemalloc_stats
  11. * - jemalloc_purge_freed_pages
  12. * - jemalloc_free_dirty_pages
  13. */
  14. #ifndef MOZ_MEMORY
  15. # error Should not include mozmemory.h when MOZ_MEMORY is not set
  16. #endif
  17. #include "mozmemory_wrap.h"
  18. #include "mozilla/Attributes.h"
  19. #include "mozilla/Types.h"
  20. #include "jemalloc_types.h"
  21. MOZ_BEGIN_EXTERN_C
  22. /*
  23. * On OSX, malloc/malloc.h contains the declaration for malloc_good_size,
  24. * which will call back in jemalloc, through the zone allocator so just use it.
  25. */
  26. #ifdef XP_DARWIN
  27. # include <malloc/malloc.h>
  28. #else
  29. MOZ_MEMORY_API size_t malloc_good_size_impl(size_t size);
  30. /* Note: the MOZ_GLUE_IN_PROGRAM ifdef below is there to avoid -Werror turning
  31. * the protective if into errors. MOZ_GLUE_IN_PROGRAM is what triggers MFBT_API
  32. * to use weak imports. */
  33. static inline size_t _malloc_good_size(size_t size) {
  34. # if defined(MOZ_GLUE_IN_PROGRAM) && !defined(IMPL_MFBT)
  35. if (!malloc_good_size)
  36. return size;
  37. # endif
  38. return malloc_good_size_impl(size);
  39. }
  40. # define malloc_good_size _malloc_good_size
  41. #endif
  42. MOZ_JEMALLOC_API void jemalloc_stats(jemalloc_stats_t *stats);
  43. /*
  44. * On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages
  45. * back to the operating system. On Mac, the operating system doesn't take
  46. * this memory back immediately; instead, the OS takes it back only when the
  47. * machine is running out of physical memory.
  48. *
  49. * This is great from the standpoint of efficiency, but it makes measuring our
  50. * actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count
  51. * against our RSS.
  52. *
  53. * This function explicitly purges any MADV_FREE'd pages from physical memory,
  54. * causing our reported RSS match the amount of memory we're actually using.
  55. *
  56. * Note that this call is expensive in two ways. First, it may be slow to
  57. * execute, because it may make a number of slow syscalls to free memory. This
  58. * function holds the big jemalloc locks, so basically all threads are blocked
  59. * while this function runs.
  60. *
  61. * This function is also expensive in that the next time we go to access a page
  62. * which we've just explicitly decommitted, the operating system has to attach
  63. * to it a physical page! If we hadn't run this function, the OS would have
  64. * less work to do.
  65. *
  66. * If MALLOC_DOUBLE_PURGE is not defined, this function does nothing.
  67. */
  68. MOZ_JEMALLOC_API void jemalloc_purge_freed_pages();
  69. /*
  70. * Free all unused dirty pages in all arenas. Calling this function will slow
  71. * down subsequent allocations so it is recommended to use it only when
  72. * memory needs to be reclaimed at all costs (see bug 805855). This function
  73. * provides functionality similar to mallctl("arenas.purge") in jemalloc 3.
  74. */
  75. MOZ_JEMALLOC_API void jemalloc_free_dirty_pages();
  76. MOZ_END_EXTERN_C
  77. #endif /* mozmemory_h */