summaryrefslogtreecommitdiff
path: root/deps/jemalloc/src/zone.c
blob: 2c1b2318ef4686b91cea9c3d4a1dac3ff2dcc325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "jemalloc/internal/jemalloc_internal.h"
#ifndef JEMALLOC_ZONE
#  error "This source file is for zones on Darwin (OS X)."
#endif

/******************************************************************************/
/* Data. */

static malloc_zone_t zone, szone;
static struct malloc_introspection_t zone_introspect, ozone_introspect;

/******************************************************************************/
/* Function prototypes for non-inline static functions. */

static size_t	zone_size(malloc_zone_t *zone, void *ptr);
static void	*zone_malloc(malloc_zone_t *zone, size_t size);
static void	*zone_calloc(malloc_zone_t *zone, size_t num, size_t size);
static void	*zone_valloc(malloc_zone_t *zone, size_t size);
static void	zone_free(malloc_zone_t *zone, void *ptr);
static void	*zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);
#if (JEMALLOC_ZONE_VERSION >= 6)
static void	*zone_memalign(malloc_zone_t *zone, size_t alignment,
    size_t size);
static void	zone_free_definite_size(malloc_zone_t *zone, void *ptr,
    size_t size);
#endif
static void	*zone_destroy(malloc_zone_t *zone);
static size_t	zone_good_size(malloc_zone_t *zone, size_t size);
static void	zone_force_lock(malloc_zone_t *zone);
static void	zone_force_unlock(malloc_zone_t *zone);
static size_t	ozone_size(malloc_zone_t *zone, void *ptr);
static void	ozone_free(malloc_zone_t *zone, void *ptr);
static void	*ozone_realloc(malloc_zone_t *zone, void *ptr, size_t size);
static unsigned	ozone_batch_malloc(malloc_zone_t *zone, size_t size,
    void **results, unsigned num_requested);
static void	ozone_batch_free(malloc_zone_t *zone, void **to_be_freed,
    unsigned num);
#if (JEMALLOC_ZONE_VERSION >= 6)
static void	ozone_free_definite_size(malloc_zone_t *zone, void *ptr,
    size_t size);
#endif
static void	ozone_force_lock(malloc_zone_t *zone);
static void	ozone_force_unlock(malloc_zone_t *zone);

/******************************************************************************/
/*
 * Functions.
 */

static size_t
zone_size(malloc_zone_t *zone, void *ptr)
{

	/*
	 * There appear to be places within Darwin (such as setenv(3)) that
	 * cause calls to this function with pointers that *no* zone owns.  If
	 * we knew that all pointers were owned by *some* zone, we could split
	 * our zone into two parts, and use one as the default allocator and
	 * the other as the default deallocator/reallocator.  Since that will
	 * not work in practice, we must check all pointers to assure that they
	 * reside within a mapped chunk before determining size.
	 */
	return (ivsalloc(ptr));
}

static void *
zone_malloc(malloc_zone_t *zone, size_t size)
{

	return (JEMALLOC_P(malloc)(size));
}

static void *
zone_calloc(malloc_zone_t *zone, size_t num, size_t size)
{

	return (JEMALLOC_P(calloc)(num, size));
}

static void *
zone_valloc(malloc_zone_t *zone, size_t size)
{
	void *ret = NULL; /* Assignment avoids useless compiler warning. */

	JEMALLOC_P(posix_memalign)(&ret, PAGE_SIZE, size);

	return (ret);
}

static void
zone_free(malloc_zone_t *zone, void *ptr)
{

	JEMALLOC_P(free)(ptr);
}

static void *
zone_realloc(malloc_zone_t *zone, void *ptr, size_t size)
{

	return (JEMALLOC_P(realloc)(ptr, size));
}

#if (JEMALLOC_ZONE_VERSION >= 6)
static void *
zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size)
{
	void *ret = NULL; /* Assignment avoids useless compiler warning. */

	JEMALLOC_P(posix_memalign)(&ret, alignment, size);

	return (ret);
}

static void
zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
{

	assert(ivsalloc(ptr) == size);
	JEMALLOC_P(free)(ptr);
}
#endif

static void *
zone_destroy(malloc_zone_t *zone)
{

	/* This function should never be called. */
	assert(false);
	return (NULL);
}

static size_t
zone_good_size(malloc_zone_t *zone, size_t size)
{
	size_t ret;
	void *p;

	/*
	 * Actually create an object of the appropriate size, then find out
	 * how large it could have been without moving up to the next size
	 * class.
	 */
	p = JEMALLOC_P(malloc)(size);
	if (p != NULL) {
		ret = isalloc(p);
		JEMALLOC_P(free)(p);
	} else
		ret = size;

	return (ret);
}

static void
zone_force_lock(malloc_zone_t *zone)
{

	if (isthreaded)
		jemalloc_prefork();
}

static void
zone_force_unlock(malloc_zone_t *zone)
{

	if (isthreaded)
		jemalloc_postfork();
}

malloc_zone_t *
create_zone(void)
{

	zone.size = (void *)zone_size;
	zone.malloc = (void *)zone_malloc;
	zone.calloc = (void *)zone_calloc;
	zone.valloc = (void *)zone_valloc;
	zone.free = (void *)zone_free;
	zone.realloc = (void *)zone_realloc;
	zone.destroy = (void *)zone_destroy;
	zone.zone_name = "jemalloc_zone";
	zone.batch_malloc = NULL;
	zone.batch_free = NULL;
	zone.introspect = &zone_introspect;
	zone.version = JEMALLOC_ZONE_VERSION;
#if (JEMALLOC_ZONE_VERSION >= 6)
	zone.memalign = zone_memalign;
	zone.free_definite_size = zone_free_definite_size;
#endif

	zone_introspect.enumerator = NULL;
	zone_introspect.good_size = (void *)zone_good_size;
	zone_introspect.check = NULL;
	zone_introspect.print = NULL;
	zone_introspect.log = NULL;
	zone_introspect.force_lock = (void *)zone_force_lock;
	zone_introspect.force_unlock = (void *)zone_force_unlock;
	zone_introspect.statistics = NULL;
#if (JEMALLOC_ZONE_VERSION >= 6)
	zone_introspect.zone_locked = NULL;
#endif

	return (&zone);
}

static size_t
ozone_size(malloc_zone_t *zone, void *ptr)
{
	size_t ret;

	ret = ivsalloc(ptr);
	if (ret == 0)
		ret = szone.size(zone, ptr);

	return (ret);
}

static void
ozone_free(malloc_zone_t *zone, void *ptr)
{

	if (ivsalloc(ptr) != 0)
		JEMALLOC_P(free)(ptr);
	else {
		size_t size = szone.size(zone, ptr);
		if (size != 0)
			(szone.free)(zone, ptr);
	}
}

static void *
ozone_realloc(malloc_zone_t *zone, void *ptr, size_t size)
{
	size_t oldsize;

	if (ptr == NULL)
		return (JEMALLOC_P(malloc)(size));

	oldsize = ivsalloc(ptr);
	if (oldsize != 0)
		return (JEMALLOC_P(realloc)(ptr, size));
	else {
		oldsize = szone.size(zone, ptr);
		if (oldsize == 0)
			return (JEMALLOC_P(malloc)(size));
		else {
			void *ret = JEMALLOC_P(malloc)(size);
			if (ret != NULL) {
				memcpy(ret, ptr, (oldsize < size) ? oldsize :
				    size);
				(szone.free)(zone, ptr);
			}
			return (ret);
		}
	}
}

static unsigned
ozone_batch_malloc(malloc_zone_t *zone, size_t size, void **results,
    unsigned num_requested)
{

	/* Don't bother implementing this interface, since it isn't required. */
	return (0);
}

static void
ozone_batch_free(malloc_zone_t *zone, void **to_be_freed, unsigned num)
{
	unsigned i;

	for (i = 0; i < num; i++)
		ozone_free(zone, to_be_freed[i]);
}

#if (JEMALLOC_ZONE_VERSION >= 6)
static void
ozone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
{

	if (ivsalloc(ptr) != 0) {
		assert(ivsalloc(ptr) == size);
		JEMALLOC_P(free)(ptr);
	} else {
		assert(size == szone.size(zone, ptr));
		szone.free_definite_size(zone, ptr, size);
	}
}
#endif

static void
ozone_force_lock(malloc_zone_t *zone)
{

	/* jemalloc locking is taken care of by the normal jemalloc zone. */
	szone.introspect->force_lock(zone);
}

static void
ozone_force_unlock(malloc_zone_t *zone)
{

	/* jemalloc locking is taken care of by the normal jemalloc zone. */
	szone.introspect->force_unlock(zone);
}

/*
 * Overlay the default scalable zone (szone) such that existing allocations are
 * drained, and further allocations come from jemalloc.  This is necessary
 * because Core Foundation directly accesses and uses the szone before the
 * jemalloc library is even loaded.
 */
void
szone2ozone(malloc_zone_t *zone)
{

	/*
	 * Stash a copy of the original szone so that we can call its
	 * functions as needed.  Note that the internally, the szone stores its
	 * bookkeeping data structures immediately following the malloc_zone_t
	 * header, so when calling szone functions, we need to pass a pointer
	 * to the original zone structure.
	 */
	memcpy(&szone, zone, sizeof(malloc_zone_t));

	zone->size = (void *)ozone_size;
	zone->malloc = (void *)zone_malloc;
	zone->calloc = (void *)zone_calloc;
	zone->valloc = (void *)zone_valloc;
	zone->free = (void *)ozone_free;
	zone->realloc = (void *)ozone_realloc;
	zone->destroy = (void *)zone_destroy;
	zone->zone_name = "jemalloc_ozone";
	zone->batch_malloc = ozone_batch_malloc;
	zone->batch_free = ozone_batch_free;
	zone->introspect = &ozone_introspect;
	zone->version = JEMALLOC_ZONE_VERSION;
#if (JEMALLOC_ZONE_VERSION >= 6)
	zone->memalign = zone_memalign;
	zone->free_definite_size = ozone_free_definite_size;
#endif

	ozone_introspect.enumerator = NULL;
	ozone_introspect.good_size = (void *)zone_good_size;
	ozone_introspect.check = NULL;
	ozone_introspect.print = NULL;
	ozone_introspect.log = NULL;
	ozone_introspect.force_lock = (void *)ozone_force_lock;
	ozone_introspect.force_unlock = (void *)ozone_force_unlock;
	ozone_introspect.statistics = NULL;
#if (JEMALLOC_ZONE_VERSION >= 6)
	ozone_introspect.zone_locked = NULL;
#endif
}