blob: 6859b3feeae0c69e26f546e1c2b1a7997fc2a0c9 [file] [log] [blame]
Jason Evans2dbecf12010-09-05 10:35:13 -07001#include "jemalloc/internal/jemalloc_internal.h"
2#ifndef JEMALLOC_ZONE
3# error "This source file is for zones on Darwin (OS X)."
4#endif
5
Mike Hommey3c2ba0d2012-03-27 14:20:13 +02006/*
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.
9 */
10extern malloc_zone_t *malloc_default_purgeable_zone(void)
11JEMALLOC_ATTR(weak_import);
12
Jason Evans2dbecf12010-09-05 10:35:13 -070013/******************************************************************************/
14/* Data. */
15
Mike Hommey154829d2012-03-20 18:01:38 +010016static malloc_zone_t zone;
17static struct malloc_introspection_t zone_introspect;
Jason Evans2dbecf12010-09-05 10:35:13 -070018
19/******************************************************************************/
20/* Function prototypes for non-inline static functions. */
21
22static size_t zone_size(malloc_zone_t *zone, void *ptr);
23static void *zone_malloc(malloc_zone_t *zone, size_t size);
24static void *zone_calloc(malloc_zone_t *zone, size_t num, size_t size);
25static void *zone_valloc(malloc_zone_t *zone, size_t size);
26static void zone_free(malloc_zone_t *zone, void *ptr);
27static void *zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);
Mike Hommey154829d2012-03-20 18:01:38 +010028#if (JEMALLOC_ZONE_VERSION >= 5)
Jason Evans2dbecf12010-09-05 10:35:13 -070029static void *zone_memalign(malloc_zone_t *zone, size_t alignment,
Mike Hommey154829d2012-03-20 18:01:38 +010030#endif
31#if (JEMALLOC_ZONE_VERSION >= 6)
Jason Evans2dbecf12010-09-05 10:35:13 -070032 size_t size);
33static void zone_free_definite_size(malloc_zone_t *zone, void *ptr,
34 size_t size);
35#endif
36static void *zone_destroy(malloc_zone_t *zone);
37static size_t zone_good_size(malloc_zone_t *zone, size_t size);
38static void zone_force_lock(malloc_zone_t *zone);
39static void zone_force_unlock(malloc_zone_t *zone);
Jason Evans2dbecf12010-09-05 10:35:13 -070040
41/******************************************************************************/
42/*
43 * Functions.
44 */
45
46static size_t
47zone_size(malloc_zone_t *zone, void *ptr)
48{
49
50 /*
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.
58 */
Jason Evans122449b2012-04-06 00:35:09 -070059 return (ivsalloc(ptr, config_prof));
Jason Evans2dbecf12010-09-05 10:35:13 -070060}
61
62static void *
63zone_malloc(malloc_zone_t *zone, size_t size)
64{
65
Jason Evans0a5489e2012-03-01 17:19:20 -080066 return (je_malloc(size));
Jason Evans2dbecf12010-09-05 10:35:13 -070067}
68
69static void *
70zone_calloc(malloc_zone_t *zone, size_t num, size_t size)
71{
72
Jason Evans0a5489e2012-03-01 17:19:20 -080073 return (je_calloc(num, size));
Jason Evans2dbecf12010-09-05 10:35:13 -070074}
75
76static void *
77zone_valloc(malloc_zone_t *zone, size_t size)
78{
79 void *ret = NULL; /* Assignment avoids useless compiler warning. */
80
Jason Evansae4c7b42012-04-02 07:04:34 -070081 je_posix_memalign(&ret, PAGE, size);
Jason Evans2dbecf12010-09-05 10:35:13 -070082
83 return (ret);
84}
85
86static void
87zone_free(malloc_zone_t *zone, void *ptr)
88{
89
Jason Evans122449b2012-04-06 00:35:09 -070090 if (ivsalloc(ptr, config_prof) != 0) {
Mike Hommey5b3db092012-03-26 18:39:35 +020091 je_free(ptr);
92 return;
93 }
94
95 free(ptr);
Jason Evans2dbecf12010-09-05 10:35:13 -070096}
97
98static void *
99zone_realloc(malloc_zone_t *zone, void *ptr, size_t size)
100{
101
Jason Evans122449b2012-04-06 00:35:09 -0700102 if (ivsalloc(ptr, config_prof) != 0)
Mike Hommey5b3db092012-03-26 18:39:35 +0200103 return (je_realloc(ptr, size));
104
105 return (realloc(ptr, size));
Jason Evans2dbecf12010-09-05 10:35:13 -0700106}
107
Mike Hommey154829d2012-03-20 18:01:38 +0100108#if (JEMALLOC_ZONE_VERSION >= 5)
Jason Evans2dbecf12010-09-05 10:35:13 -0700109static void *
110zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size)
111{
112 void *ret = NULL; /* Assignment avoids useless compiler warning. */
113
Jason Evans0a5489e2012-03-01 17:19:20 -0800114 je_posix_memalign(&ret, alignment, size);
Jason Evans2dbecf12010-09-05 10:35:13 -0700115
116 return (ret);
117}
Mike Hommey154829d2012-03-20 18:01:38 +0100118#endif
Jason Evans2dbecf12010-09-05 10:35:13 -0700119
Mike Hommey154829d2012-03-20 18:01:38 +0100120#if (JEMALLOC_ZONE_VERSION >= 6)
Jason Evans2dbecf12010-09-05 10:35:13 -0700121static void
122zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
123{
Dmitry-Meea59ebf2015-11-12 14:59:29 +0300124 size_t alloc_size;
Jason Evans2dbecf12010-09-05 10:35:13 -0700125
Dmitry-Meea59ebf2015-11-12 14:59:29 +0300126 alloc_size = ivsalloc(ptr, config_prof);
127 if (alloc_size != 0) {
128 assert(alloc_size == size);
Mike Hommey5b3db092012-03-26 18:39:35 +0200129 je_free(ptr);
130 return;
131 }
132
133 free(ptr);
Jason Evans2dbecf12010-09-05 10:35:13 -0700134}
135#endif
136
137static void *
138zone_destroy(malloc_zone_t *zone)
139{
140
141 /* This function should never be called. */
Jason Evans6556e282013-10-21 14:56:27 -0700142 not_reached();
Jason Evans2dbecf12010-09-05 10:35:13 -0700143 return (NULL);
144}
145
146static size_t
147zone_good_size(malloc_zone_t *zone, size_t size)
148{
Jason Evans2dbecf12010-09-05 10:35:13 -0700149
Jason Evans166a7452012-02-29 12:58:39 -0800150 if (size == 0)
151 size = 1;
152 return (s2u(size));
Jason Evans2dbecf12010-09-05 10:35:13 -0700153}
154
155static void
156zone_force_lock(malloc_zone_t *zone)
157{
158
159 if (isthreaded)
160 jemalloc_prefork();
161}
162
163static void
164zone_force_unlock(malloc_zone_t *zone)
165{
166
167 if (isthreaded)
Mike Hommeyf4d0fc32012-03-20 18:03:09 +0100168 jemalloc_postfork_parent();
Jason Evans2dbecf12010-09-05 10:35:13 -0700169}
170
Mike Hommey80b25932012-04-02 09:04:54 +0200171JEMALLOC_ATTR(constructor)
Mike Hommey71a93b82012-03-27 14:20:12 +0200172void
173register_zone(void)
Jason Evans2dbecf12010-09-05 10:35:13 -0700174{
175
Mike Hommey847ff222012-10-23 08:42:48 +0200176 /*
177 * If something else replaced the system default zone allocator, don't
178 * register jemalloc's.
179 */
180 malloc_zone_t *default_zone = malloc_default_zone();
Mike Hommey6f533c12014-06-10 18:18:22 +0900181 malloc_zone_t *purgeable_zone = NULL;
Mike Hommey847ff222012-10-23 08:42:48 +0200182 if (!default_zone->zone_name ||
183 strcmp(default_zone->zone_name, "DefaultMallocZone") != 0) {
184 return;
185 }
186
Jason Evans2dbecf12010-09-05 10:35:13 -0700187 zone.size = (void *)zone_size;
188 zone.malloc = (void *)zone_malloc;
189 zone.calloc = (void *)zone_calloc;
190 zone.valloc = (void *)zone_valloc;
191 zone.free = (void *)zone_free;
192 zone.realloc = (void *)zone_realloc;
193 zone.destroy = (void *)zone_destroy;
194 zone.zone_name = "jemalloc_zone";
195 zone.batch_malloc = NULL;
196 zone.batch_free = NULL;
197 zone.introspect = &zone_introspect;
198 zone.version = JEMALLOC_ZONE_VERSION;
Mike Hommey154829d2012-03-20 18:01:38 +0100199#if (JEMALLOC_ZONE_VERSION >= 5)
Jason Evans2dbecf12010-09-05 10:35:13 -0700200 zone.memalign = zone_memalign;
Mike Hommey154829d2012-03-20 18:01:38 +0100201#endif
202#if (JEMALLOC_ZONE_VERSION >= 6)
Jason Evans2dbecf12010-09-05 10:35:13 -0700203 zone.free_definite_size = zone_free_definite_size;
204#endif
Mike Hommey154829d2012-03-20 18:01:38 +0100205#if (JEMALLOC_ZONE_VERSION >= 8)
206 zone.pressure_relief = NULL;
207#endif
Jason Evans2dbecf12010-09-05 10:35:13 -0700208
209 zone_introspect.enumerator = NULL;
210 zone_introspect.good_size = (void *)zone_good_size;
211 zone_introspect.check = NULL;
212 zone_introspect.print = NULL;
213 zone_introspect.log = NULL;
214 zone_introspect.force_lock = (void *)zone_force_lock;
215 zone_introspect.force_unlock = (void *)zone_force_unlock;
216 zone_introspect.statistics = NULL;
217#if (JEMALLOC_ZONE_VERSION >= 6)
218 zone_introspect.zone_locked = NULL;
219#endif
Mike Hommey154829d2012-03-20 18:01:38 +0100220#if (JEMALLOC_ZONE_VERSION >= 7)
221 zone_introspect.enable_discharge_checking = NULL;
222 zone_introspect.disable_discharge_checking = NULL;
223 zone_introspect.discharge = NULL;
224#ifdef __BLOCKS__
225 zone_introspect.enumerate_discharged_pointers = NULL;
226#else
227 zone_introspect.enumerate_unavailable_without_blocks = NULL;
228#endif
229#endif
Mike Hommey71a93b82012-03-27 14:20:12 +0200230
Mike Hommey3c2ba0d2012-03-27 14:20:13 +0200231 /*
232 * The default purgeable zone is created lazily by OSX's libc. It uses
233 * the default zone when it is created for "small" allocations
234 * (< 15 KiB), but assumes the default zone is a scalable_zone. This
235 * obviously fails when the default zone is the jemalloc zone, so
236 * malloc_default_purgeable_zone is called beforehand so that the
237 * default purgeable zone is created when the default zone is still
238 * a scalable_zone. As purgeable zones only exist on >= 10.6, we need
239 * to check for the existence of malloc_default_purgeable_zone() at
240 * run time.
241 */
242 if (malloc_default_purgeable_zone != NULL)
Mike Hommey6f533c12014-06-10 18:18:22 +0900243 purgeable_zone = malloc_default_purgeable_zone();
Mike Hommey3c2ba0d2012-03-27 14:20:13 +0200244
245 /* Register the custom zone. At this point it won't be the default. */
Mike Hommey71a93b82012-03-27 14:20:12 +0200246 malloc_zone_register(&zone);
247
Mike Hommey71a93b82012-03-27 14:20:12 +0200248 do {
Mike Hommey847ff222012-10-23 08:42:48 +0200249 default_zone = malloc_default_zone();
Mike Hommey6f533c12014-06-10 18:18:22 +0900250 /*
251 * Unregister and reregister the default zone. On OSX >= 10.6,
252 * unregistering takes the last registered zone and places it
253 * at the location of the specified zone. Unregistering the
254 * default zone thus makes the last registered one the default.
255 * On OSX < 10.6, unregistering shifts all registered zones.
256 * The first registered zone then becomes the default.
257 */
Mike Hommey71a93b82012-03-27 14:20:12 +0200258 malloc_zone_unregister(default_zone);
259 malloc_zone_register(default_zone);
Mike Hommey6f533c12014-06-10 18:18:22 +0900260 /*
261 * On OSX 10.6, having the default purgeable zone appear before
262 * the default zone makes some things crash because it thinks it
Jason Evansc21b05e2014-09-04 22:27:26 -0700263 * owns the default zone allocated pointers. We thus
264 * unregister/re-register it in order to ensure it's always
265 * after the default zone. On OSX < 10.6, there is no purgeable
266 * zone, so this does nothing. On OSX >= 10.6, unregistering
267 * replaces the purgeable zone with the last registered zone
Jason Evanse12eaf92014-12-08 14:40:14 -0800268 * above, i.e. the default zone. Registering it again then puts
Jason Evansc21b05e2014-09-04 22:27:26 -0700269 * it at the end, obviously after the default zone.
Mike Hommey6f533c12014-06-10 18:18:22 +0900270 */
271 if (purgeable_zone) {
272 malloc_zone_unregister(purgeable_zone);
273 malloc_zone_register(purgeable_zone);
274 }
Mike Hommey71a93b82012-03-27 14:20:12 +0200275 } while (malloc_default_zone() != &zone);
Jason Evans2dbecf12010-09-05 10:35:13 -0700276}