mozmemory_wrap.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_wrap_h
  5. #define mozmemory_wrap_h
  6. /*
  7. * This header contains #defines which tweak the names of various memory
  8. * allocation functions.
  9. *
  10. * There are several types of functions related to memory allocation
  11. * that are meant to be used publicly by the Gecko codebase:
  12. *
  13. * - malloc implementation functions:
  14. * - malloc
  15. * - posix_memalign
  16. * - aligned_alloc
  17. * - calloc
  18. * - realloc
  19. * - free
  20. * - memalign
  21. * - valloc
  22. * - malloc_usable_size
  23. * - malloc_good_size
  24. * Some of these functions are specific to some systems, but for
  25. * convenience, they are treated as being cross-platform, and available
  26. * as such.
  27. *
  28. * - duplication functions:
  29. * - strndup
  30. * - strdup
  31. * - wcsdup (Windows only)
  32. *
  33. * - jemalloc specific functions:
  34. * - jemalloc_stats
  35. * - jemalloc_purge_freed_pages
  36. * - jemalloc_free_dirty_pages
  37. * (these functions are native to mozjemalloc)
  38. *
  39. * These functions are all exported as part of libmozglue (see
  40. * $(topsrcdir)/mozglue/build/Makefile.in), with a few implementation
  41. * peculiarities:
  42. *
  43. * - On Windows, the malloc implementation functions are all prefixed with
  44. * "je_", the duplication functions are prefixed with "wrap_", and jemalloc
  45. * specific functions are left unprefixed. All these functions are however
  46. * aliased when exporting them, such that the resulting mozglue.dll exports
  47. * them unprefixed (see $(topsrcdir)/mozglue/build/mozglue.def.in). The
  48. * prefixed malloc implementation and duplication functions are not
  49. * exported.
  50. *
  51. * - On MacOSX, the system libc has a zone allocator, which allows us to
  52. * hook custom malloc implementation functions without exporting them.
  53. * The malloc implementation functions are all prefixed with "je_" and used
  54. * this way from the custom zone allocator. They are not exported.
  55. * Duplication functions are not included, since they will call the custom
  56. * zone allocator anyways. Jemalloc-specific functions are also left
  57. * unprefixed.
  58. *
  59. * - On other systems (mostly Linux), all functions are left unprefixed.
  60. *
  61. *
  62. * Proper exporting of the various functions is done with the MOZ_MEMORY_API
  63. * and MOZ_JEMALLOC_API macros. MOZ_MEMORY_API is meant to be used for malloc
  64. * implementation and duplication functions, while MOZ_JEMALLOC_API is
  65. * dedicated to jemalloc specific functions.
  66. *
  67. *
  68. * Within libmozglue (when MOZ_MEMORY_IMPL is defined), all the functions
  69. * should be suffixed with "_impl" both for declarations and use.
  70. * That is, the implementation declaration for e.g. strdup would look like:
  71. * char* strdup_impl(const char *)
  72. * That implementation would call malloc by using "malloc_impl".
  73. *
  74. *
  75. * When building with replace-malloc support, the above still holds, but
  76. * the malloc implementation and jemalloc specific functions are the
  77. * replace-malloc functions from replace_malloc.c.
  78. *
  79. * The actual mozjemalloc implementation is prefixed with "je_".
  80. *
  81. * Thus, when MOZ_REPLACE_MALLOC is defined, the "_impl" suffixed macros
  82. * expand to "je_" prefixed function when building mozjemalloc, where
  83. * MOZ_JEMALLOC_IMPL is defined.
  84. *
  85. * In other cases, the "_impl" suffixed macros follow the original scheme,
  86. * except on Windows and MacOSX, where they would expand to "je_" prefixed
  87. * functions. Instead, they are left unmodified (malloc_impl expands to
  88. * malloc_impl).
  89. */
  90. #ifndef MOZ_MEMORY
  91. # error Should only include mozmemory_wrap.h when MOZ_MEMORY is set.
  92. #endif
  93. #if defined(MOZ_JEMALLOC_IMPL) && !defined(MOZ_MEMORY_IMPL)
  94. # define MOZ_MEMORY_IMPL
  95. #endif
  96. #if defined(MOZ_MEMORY_IMPL) && !defined(IMPL_MFBT)
  97. # ifdef MFBT_API /* mozilla/Types.h was already included */
  98. # error mozmemory_wrap.h has to be included before mozilla/Types.h when MOZ_MEMORY_IMPL is set and IMPL_MFBT is not.
  99. # endif
  100. # define IMPL_MFBT
  101. #endif
  102. #include "mozilla/Types.h"
  103. #ifdef MOZ_MEMORY_IMPL
  104. # if defined(MOZ_JEMALLOC_IMPL) && defined(MOZ_REPLACE_MALLOC)
  105. # define mozmem_malloc_impl(a) je_ ## a
  106. # define mozmem_jemalloc_impl(a) je_ ## a
  107. # else
  108. # define MOZ_JEMALLOC_API MFBT_API
  109. # if defined(XP_WIN)
  110. # if defined(MOZ_REPLACE_MALLOC)
  111. # define mozmem_malloc_impl(a) a ## _impl
  112. # else
  113. # define mozmem_malloc_impl(a) je_ ## a
  114. # endif
  115. # else
  116. # define MOZ_MEMORY_API MFBT_API
  117. # endif
  118. # endif
  119. # ifdef XP_WIN
  120. # define mozmem_dup_impl(a) wrap_ ## a
  121. # endif
  122. #endif
  123. #if !defined(MOZ_MEMORY_IMPL)
  124. # define MOZ_MEMORY_API MFBT_API
  125. # define MOZ_JEMALLOC_API MFBT_API
  126. #endif
  127. #ifndef MOZ_MEMORY_API
  128. # define MOZ_MEMORY_API
  129. #endif
  130. #ifndef MOZ_JEMALLOC_API
  131. # define MOZ_JEMALLOC_API
  132. #endif
  133. #ifndef mozmem_malloc_impl
  134. # define mozmem_malloc_impl(a) a
  135. #endif
  136. #ifndef mozmem_dup_impl
  137. # define mozmem_dup_impl(a) a
  138. #endif
  139. #ifndef mozmem_jemalloc_impl
  140. # define mozmem_jemalloc_impl(a) a
  141. #endif
  142. /* Malloc implementation functions */
  143. #define malloc_impl mozmem_malloc_impl(malloc)
  144. #define posix_memalign_impl mozmem_malloc_impl(posix_memalign)
  145. #define aligned_alloc_impl mozmem_malloc_impl(aligned_alloc)
  146. #define calloc_impl mozmem_malloc_impl(calloc)
  147. #define realloc_impl mozmem_malloc_impl(realloc)
  148. #define free_impl mozmem_malloc_impl(free)
  149. #define memalign_impl mozmem_malloc_impl(memalign)
  150. #define valloc_impl mozmem_malloc_impl(valloc)
  151. #define malloc_usable_size_impl mozmem_malloc_impl(malloc_usable_size)
  152. #define malloc_good_size_impl mozmem_malloc_impl(malloc_good_size)
  153. /* Duplication functions */
  154. #define strndup_impl mozmem_dup_impl(strndup)
  155. #define strdup_impl mozmem_dup_impl(strdup)
  156. #ifdef XP_WIN
  157. # define wcsdup_impl mozmem_dup_impl(wcsdup)
  158. #endif
  159. /* Jemalloc specific function */
  160. #define jemalloc_stats_impl mozmem_jemalloc_impl(jemalloc_stats)
  161. #define jemalloc_purge_freed_pages_impl mozmem_jemalloc_impl(jemalloc_purge_freed_pages)
  162. #define jemalloc_free_dirty_pages_impl mozmem_jemalloc_impl(jemalloc_free_dirty_pages)
  163. #endif /* mozmemory_wrap_h */