1 #include "jemalloc/internal/jemalloc_internal.h"
3 # error "This source file is for zones on Darwin (OS X)."
7 * The malloc_default_purgeable_zone function is only available on >= 10.6.
8 * We need to check whether it is present at runtime, thus the weak_import.
10 extern malloc_zone_t
*malloc_default_purgeable_zone(void)
11 JEMALLOC_ATTR(weak_import
);
13 /******************************************************************************/
16 static malloc_zone_t zone
;
17 static struct malloc_introspection_t zone_introspect
;
19 /******************************************************************************/
20 /* Function prototypes for non-inline static functions. */
22 static size_t zone_size(malloc_zone_t
*zone
, void *ptr
);
23 static void *zone_malloc(malloc_zone_t
*zone
, size_t size
);
24 static void *zone_calloc(malloc_zone_t
*zone
, size_t num
, size_t size
);
25 static void *zone_valloc(malloc_zone_t
*zone
, size_t size
);
26 static void zone_free(malloc_zone_t
*zone
, void *ptr
);
27 static void *zone_realloc(malloc_zone_t
*zone
, void *ptr
, size_t size
);
28 #if (JEMALLOC_ZONE_VERSION >= 5)
29 static void *zone_memalign(malloc_zone_t
*zone
, size_t alignment
,
31 #if (JEMALLOC_ZONE_VERSION >= 6)
33 static void zone_free_definite_size(malloc_zone_t
*zone
, void *ptr
,
36 static void *zone_destroy(malloc_zone_t
*zone
);
37 static size_t zone_good_size(malloc_zone_t
*zone
, size_t size
);
38 static void zone_force_lock(malloc_zone_t
*zone
);
39 static void zone_force_unlock(malloc_zone_t
*zone
);
41 /******************************************************************************/
47 zone_size(malloc_zone_t
*zone
, void *ptr
)
51 * There appear to be places within Darwin (such as setenv(3)) that
52 * cause calls to this function with pointers that *no* zone owns. If
53 * we knew that all pointers were owned by *some* zone, we could split
54 * our zone into two parts, and use one as the default allocator and
55 * the other as the default deallocator/reallocator. Since that will
56 * not work in practice, we must check all pointers to assure that they
57 * reside within a mapped chunk before determining size.
59 return (ivsalloc(ptr
, config_prof
));
63 zone_malloc(malloc_zone_t
*zone
, size_t size
)
66 return (je_malloc(size
));
70 zone_calloc(malloc_zone_t
*zone
, size_t num
, size_t size
)
73 return (je_calloc(num
, size
));
77 zone_valloc(malloc_zone_t
*zone
, size_t size
)
79 void *ret
= NULL
; /* Assignment avoids useless compiler warning. */
81 je_posix_memalign(&ret
, PAGE
, size
);
87 zone_free(malloc_zone_t
*zone
, void *ptr
)
90 if (ivsalloc(ptr
, config_prof
) != 0) {
99 zone_realloc(malloc_zone_t
*zone
, void *ptr
, size_t size
)
102 if (ivsalloc(ptr
, config_prof
) != 0)
103 return (je_realloc(ptr
, size
));
105 return (realloc(ptr
, size
));
108 #if (JEMALLOC_ZONE_VERSION >= 5)
110 zone_memalign(malloc_zone_t
*zone
, size_t alignment
, size_t size
)
112 void *ret
= NULL
; /* Assignment avoids useless compiler warning. */
114 je_posix_memalign(&ret
, alignment
, size
);
120 #if (JEMALLOC_ZONE_VERSION >= 6)
122 zone_free_definite_size(malloc_zone_t
*zone
, void *ptr
, size_t size
)
125 if (ivsalloc(ptr
, config_prof
) != 0) {
126 assert(ivsalloc(ptr
, config_prof
) == size
);
136 zone_destroy(malloc_zone_t
*zone
)
139 /* This function should never be called. */
145 zone_good_size(malloc_zone_t
*zone
, size_t size
)
154 zone_force_lock(malloc_zone_t
*zone
)
162 zone_force_unlock(malloc_zone_t
*zone
)
166 jemalloc_postfork_parent();
169 JEMALLOC_ATTR(constructor
)
175 * If something else replaced the system default zone allocator, don't
176 * register jemalloc's.
178 malloc_zone_t
*default_zone
= malloc_default_zone();
179 if (!default_zone
->zone_name
||
180 strcmp(default_zone
->zone_name
, "DefaultMallocZone") != 0) {
184 zone
.size
= (void *)zone_size
;
185 zone
.malloc
= (void *)zone_malloc
;
186 zone
.calloc
= (void *)zone_calloc
;
187 zone
.valloc
= (void *)zone_valloc
;
188 zone
.free
= (void *)zone_free
;
189 zone
.realloc
= (void *)zone_realloc
;
190 zone
.destroy
= (void *)zone_destroy
;
191 zone
.zone_name
= "jemalloc_zone";
192 zone
.batch_malloc
= NULL
;
193 zone
.batch_free
= NULL
;
194 zone
.introspect
= &zone_introspect
;
195 zone
.version
= JEMALLOC_ZONE_VERSION
;
196 #if (JEMALLOC_ZONE_VERSION >= 5)
197 zone
.memalign
= zone_memalign
;
199 #if (JEMALLOC_ZONE_VERSION >= 6)
200 zone
.free_definite_size
= zone_free_definite_size
;
202 #if (JEMALLOC_ZONE_VERSION >= 8)
203 zone
.pressure_relief
= NULL
;
206 zone_introspect
.enumerator
= NULL
;
207 zone_introspect
.good_size
= (void *)zone_good_size
;
208 zone_introspect
.check
= NULL
;
209 zone_introspect
.print
= NULL
;
210 zone_introspect
.log
= NULL
;
211 zone_introspect
.force_lock
= (void *)zone_force_lock
;
212 zone_introspect
.force_unlock
= (void *)zone_force_unlock
;
213 zone_introspect
.statistics
= NULL
;
214 #if (JEMALLOC_ZONE_VERSION >= 6)
215 zone_introspect
.zone_locked
= NULL
;
217 #if (JEMALLOC_ZONE_VERSION >= 7)
218 zone_introspect
.enable_discharge_checking
= NULL
;
219 zone_introspect
.disable_discharge_checking
= NULL
;
220 zone_introspect
.discharge
= NULL
;
222 zone_introspect
.enumerate_discharged_pointers
= NULL
;
224 zone_introspect
.enumerate_unavailable_without_blocks
= NULL
;
229 * The default purgeable zone is created lazily by OSX's libc. It uses
230 * the default zone when it is created for "small" allocations
231 * (< 15 KiB), but assumes the default zone is a scalable_zone. This
232 * obviously fails when the default zone is the jemalloc zone, so
233 * malloc_default_purgeable_zone is called beforehand so that the
234 * default purgeable zone is created when the default zone is still
235 * a scalable_zone. As purgeable zones only exist on >= 10.6, we need
236 * to check for the existence of malloc_default_purgeable_zone() at
239 if (malloc_default_purgeable_zone
!= NULL
)
240 malloc_default_purgeable_zone();
242 /* Register the custom zone. At this point it won't be the default. */
243 malloc_zone_register(&zone
);
246 * Unregister and reregister the default zone. On OSX >= 10.6,
247 * unregistering takes the last registered zone and places it at the
248 * location of the specified zone. Unregistering the default zone thus
249 * makes the last registered one the default. On OSX < 10.6,
250 * unregistering shifts all registered zones. The first registered zone
251 * then becomes the default.
254 default_zone
= malloc_default_zone();
255 malloc_zone_unregister(default_zone
);
256 malloc_zone_register(default_zone
);
257 } while (malloc_default_zone() != &zone
);