1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozmemory_h
8 #define mozmemory_h
9 
10 // This header is meant to be used when the following functions are
11 // necessary:
12 //   - malloc_good_size (used to be called je_malloc_usable_in_advance)
13 //   - jemalloc_stats
14 //   - jemalloc_stats_num_bins
15 //   - jemalloc_purge_freed_pages
16 //   - jemalloc_free_dirty_pages
17 //   - jemalloc_thread_local_arena
18 //   - jemalloc_ptr_info
19 
20 #ifdef MALLOC_H
21 #  include MALLOC_H
22 #endif
23 #include "mozmemory_wrap.h"
24 #include "mozilla/Attributes.h"
25 #include "mozilla/Types.h"
26 #include "mozjemalloc_types.h"
27 
28 #ifdef MOZ_MEMORY
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 #  ifndef XP_DARWIN
32 MOZ_MEMORY_API size_t malloc_good_size_impl(size_t size);
33 
34 // Note: the MOZ_GLUE_IN_PROGRAM ifdef below is there to avoid -Werror turning
35 // the protective if into errors. MOZ_GLUE_IN_PROGRAM is what triggers MFBT_API
36 // to use weak imports.
_malloc_good_size(size_t size)37 static inline size_t _malloc_good_size(size_t size) {
38 #    if defined(MOZ_GLUE_IN_PROGRAM) && !defined(IMPL_MFBT)
39   if (!malloc_good_size) return size;
40 #    endif
41   return malloc_good_size_impl(size);
42 }
43 
44 #    define malloc_good_size _malloc_good_size
45 #  endif
46 
47 #  define MALLOC_DECL(name, return_type, ...) \
48     MOZ_JEMALLOC_API return_type name(__VA_ARGS__);
49 #  define MALLOC_FUNCS MALLOC_FUNCS_JEMALLOC
50 #  include "malloc_decls.h"
51 
52 #  ifdef __cplusplus
53 static inline void jemalloc_stats(jemalloc_stats_t* aStats,
54                                   jemalloc_bin_stats_t* aBinStats = nullptr) {
55   jemalloc_stats_internal(aStats, aBinStats);
56 }
57 #  else
jemalloc_stats(jemalloc_stats_t * aStats)58 static inline void jemalloc_stats(jemalloc_stats_t* aStats) {
59   jemalloc_stats_internal(aStats, NULL);
60 }
61 #  endif
62 
63 #endif  // MOZ_MEMORY
64 
65 #define NOTHROW_MALLOC_DECL(name, return_type, ...) \
66   MOZ_JEMALLOC_API return_type name(__VA_ARGS__) noexcept(true);
67 #define MALLOC_DECL(name, return_type, ...) \
68   MOZ_JEMALLOC_API return_type name(__VA_ARGS__);
69 #define MALLOC_FUNCS MALLOC_FUNCS_ARENA
70 #include "malloc_decls.h"
71 
72 #ifdef __cplusplus
73 #  define moz_create_arena() moz_create_arena_with_params(nullptr)
74 #else
75 #  define moz_create_arena() moz_create_arena_with_params(NULL)
76 #endif
77 
78 #endif  // mozmemory_h
79