|
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 replace_malloc_h |
|
6 #define replace_malloc_h |
|
7 |
|
8 /* |
|
9 * The replace_malloc facility allows an external library to replace or |
|
10 * supplement the jemalloc implementation. |
|
11 * |
|
12 * The external library may be hooked by setting one of the following |
|
13 * environment variables to the library path: |
|
14 * - LD_PRELOAD on Linux, |
|
15 * - DYLD_INSERT_LIBRARIES on OSX, |
|
16 * - MOZ_REPLACE_MALLOC_LIB on Windows and Android. |
|
17 * |
|
18 * An initialization function is called before any malloc replacement |
|
19 * function, and has the following declaration: |
|
20 * |
|
21 * void replace_init(const malloc_table_t *) |
|
22 * |
|
23 * The const malloc_table_t pointer given to that function is a table |
|
24 * containing pointers to the original jemalloc implementation, so that |
|
25 * replacement functions can call them back if they need to. The pointer |
|
26 * itself can safely be kept around (no need to copy the table itself). |
|
27 * |
|
28 * The functions to be implemented in the external library are of the form: |
|
29 * |
|
30 * void *replace_malloc(size_t size) |
|
31 * { |
|
32 * // Fiddle with the size if necessary. |
|
33 * // orig->malloc doesn't have to be called if the external library |
|
34 * // provides its own allocator, but in this case it will have to |
|
35 * // implement all functions. |
|
36 * void *ptr = orig->malloc(size); |
|
37 * // Do whatever you want with the ptr. |
|
38 * return ptr; |
|
39 * } |
|
40 * |
|
41 * where "orig" is the pointer obtained from replace_init. |
|
42 * |
|
43 * See malloc_decls.h for a list of functions that can be replaced this |
|
44 * way. The implementations are all in the form: |
|
45 * return_type replace_name(arguments [,...]) |
|
46 * |
|
47 * They don't all need to be provided. |
|
48 * |
|
49 * Building a replace-malloc library is like rocket science. It can end up |
|
50 * with things blowing up, especially when trying to use complex types, and |
|
51 * even more especially when these types come from XPCOM or other parts of the |
|
52 * Mozilla codebase. |
|
53 * It is recommended to add the following to a replace-malloc implementation's |
|
54 * Makefile.in: |
|
55 * MOZ_GLUE_LDFLAGS = # Don't link against mozglue |
|
56 * WRAP_LDFLAGS = # Never wrap malloc function calls with -Wl,--wrap |
|
57 * and the following to the implementation's moz.build: |
|
58 * DISABLE_STL_WRAPPING = True # Avoid STL wrapping |
|
59 * |
|
60 * If your replace-malloc implementation lives under memory/replace, these |
|
61 * are taken care of by memory/replace/defs.mk. |
|
62 */ |
|
63 |
|
64 /* Implementing a replace-malloc library is incompatible with using mozalloc. */ |
|
65 #define MOZ_NO_MOZALLOC 1 |
|
66 |
|
67 #include "mozilla/Types.h" |
|
68 |
|
69 MOZ_BEGIN_EXTERN_C |
|
70 |
|
71 #define MALLOC_DECL(name, return_type, ...) \ |
|
72 typedef return_type(name ## _impl_t)(__VA_ARGS__); |
|
73 |
|
74 #include "malloc_decls.h" |
|
75 |
|
76 #define MALLOC_DECL(name, return_type, ...) \ |
|
77 name ## _impl_t * name; |
|
78 |
|
79 typedef struct { |
|
80 #include "malloc_decls.h" |
|
81 } malloc_table_t; |
|
82 |
|
83 |
|
84 /* MOZ_NO_REPLACE_FUNC_DECL and MOZ_REPLACE_WEAK are only defined in |
|
85 * replace_malloc.c. Normally including this header will add function |
|
86 * definitions. */ |
|
87 #ifndef MOZ_NO_REPLACE_FUNC_DECL |
|
88 |
|
89 # ifndef MOZ_REPLACE_WEAK |
|
90 # define MOZ_REPLACE_WEAK |
|
91 # endif |
|
92 |
|
93 # define MALLOC_DECL(name, return_type, ...) \ |
|
94 MOZ_EXPORT return_type replace_ ## name(__VA_ARGS__) MOZ_REPLACE_WEAK; |
|
95 |
|
96 # define MALLOC_FUNCS MALLOC_FUNCS_ALL |
|
97 # include "malloc_decls.h" |
|
98 |
|
99 #endif /* MOZ_NO_REPLACE_FUNC_DECL */ |
|
100 |
|
101 /* |
|
102 * posix_memalign, aligned_alloc, memalign and valloc all implement some |
|
103 * kind of aligned memory allocation. For convenience, replace_posix_memalign, |
|
104 * replace_aligned_alloc and replace_valloc can be automatically derived from |
|
105 * memalign when MOZ_REPLACE_ONLY_MEMALIGN is defined before including this |
|
106 * header. PAGE_SIZE also needs to be defined to the appropriate expression. |
|
107 */ |
|
108 #ifdef MOZ_REPLACE_ONLY_MEMALIGN |
|
109 #include <errno.h> |
|
110 |
|
111 int replace_posix_memalign(void **ptr, size_t alignment, size_t size) |
|
112 { |
|
113 if (size == 0) { |
|
114 *ptr = NULL; |
|
115 return 0; |
|
116 } |
|
117 /* alignment must be a power of two and a multiple of sizeof(void *) */ |
|
118 if (((alignment - 1) & alignment) != 0 || (alignment % sizeof(void *))) |
|
119 return EINVAL; |
|
120 *ptr = replace_memalign(alignment, size); |
|
121 return *ptr ? 0 : ENOMEM; |
|
122 } |
|
123 |
|
124 void *replace_aligned_alloc(size_t alignment, size_t size) |
|
125 { |
|
126 /* size should be a multiple of alignment */ |
|
127 if (size % alignment) |
|
128 return NULL; |
|
129 return replace_memalign(alignment, size); |
|
130 } |
|
131 |
|
132 void *replace_valloc(size_t size) |
|
133 { |
|
134 return replace_memalign(PAGE_SIZE, size); |
|
135 } |
|
136 #endif |
|
137 |
|
138 MOZ_END_EXTERN_C |
|
139 |
|
140 #endif /* replace_malloc_h */ |