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