blob: 1eee436e7f9af0e5d0d939a4ae7a586c30bc5230 [file] [log] [blame]
Jason Evanse476f8a2010-01-16 09:53:50 -08001#define JEMALLOC_HUGE_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evanse476f8a2010-01-16 09:53:50 -08003
4/******************************************************************************/
5/* Data. */
6
Jason Evanse476f8a2010-01-16 09:53:50 -08007uint64_t huge_nmalloc;
8uint64_t huge_ndalloc;
9size_t huge_allocated;
Jason Evanse476f8a2010-01-16 09:53:50 -080010
11malloc_mutex_t huge_mtx;
12
13/******************************************************************************/
14
15/* Tree of chunks that are stand-alone huge allocations. */
16static extent_tree_t huge;
17
18void *
19huge_malloc(size_t size, bool zero)
20{
21 void *ret;
22 size_t csize;
23 extent_node_t *node;
24
25 /* Allocate one or more contiguous chunks for this request. */
26
27 csize = CHUNK_CEILING(size);
28 if (csize == 0) {
29 /* size is large enough to cause size_t wrap-around. */
30 return (NULL);
31 }
32
33 /* Allocate an extent node with which to track the chunk. */
34 node = base_node_alloc();
35 if (node == NULL)
36 return (NULL);
37
Jason Evans2dbecf12010-09-05 10:35:13 -070038 ret = chunk_alloc(csize, false, &zero);
Jason Evanse476f8a2010-01-16 09:53:50 -080039 if (ret == NULL) {
40 base_node_dealloc(node);
41 return (NULL);
42 }
43
44 /* Insert node into huge. */
45 node->addr = ret;
46 node->size = csize;
47
48 malloc_mutex_lock(&huge_mtx);
49 extent_tree_ad_insert(&huge, node);
Jason Evans7372b152012-02-10 20:22:09 -080050 if (config_stats) {
51 stats_cactive_add(csize);
52 huge_nmalloc++;
53 huge_allocated += csize;
54 }
Jason Evanse476f8a2010-01-16 09:53:50 -080055 malloc_mutex_unlock(&huge_mtx);
56
Jason Evans7372b152012-02-10 20:22:09 -080057 if (config_fill && zero == false) {
Jason Evanse476f8a2010-01-16 09:53:50 -080058 if (opt_junk)
59 memset(ret, 0xa5, csize);
60 else if (opt_zero)
61 memset(ret, 0, csize);
62 }
Jason Evanse476f8a2010-01-16 09:53:50 -080063
64 return (ret);
65}
66
67/* Only handles large allocations that require more than chunk alignment. */
68void *
Jason Evans8e3c3c62010-09-17 15:46:18 -070069huge_palloc(size_t size, size_t alignment, bool zero)
Jason Evanse476f8a2010-01-16 09:53:50 -080070{
71 void *ret;
72 size_t alloc_size, chunk_size, offset;
73 extent_node_t *node;
74
75 /*
76 * This allocation requires alignment that is even larger than chunk
77 * alignment. This means that huge_malloc() isn't good enough.
78 *
79 * Allocate almost twice as many chunks as are demanded by the size or
80 * alignment, in order to assure the alignment can be achieved, then
81 * unmap leading and trailing chunks.
82 */
Jason Evans31bfb3e2011-01-31 19:58:22 -080083 assert(alignment > chunksize);
Jason Evanse476f8a2010-01-16 09:53:50 -080084
85 chunk_size = CHUNK_CEILING(size);
86
87 if (size >= alignment)
88 alloc_size = chunk_size + alignment - chunksize;
89 else
90 alloc_size = (alignment << 1) - chunksize;
91
92 /* Allocate an extent node with which to track the chunk. */
93 node = base_node_alloc();
94 if (node == NULL)
95 return (NULL);
96
Jason Evans2dbecf12010-09-05 10:35:13 -070097 ret = chunk_alloc(alloc_size, false, &zero);
Jason Evanse476f8a2010-01-16 09:53:50 -080098 if (ret == NULL) {
99 base_node_dealloc(node);
100 return (NULL);
101 }
102
103 offset = (uintptr_t)ret & (alignment - 1);
104 assert((offset & chunksize_mask) == 0);
105 assert(offset < alloc_size);
106 if (offset == 0) {
107 /* Trim trailing space. */
108 chunk_dealloc((void *)((uintptr_t)ret + chunk_size), alloc_size
Jason Evans12a48872011-11-11 14:41:59 -0800109 - chunk_size, true);
Jason Evanse476f8a2010-01-16 09:53:50 -0800110 } else {
111 size_t trailsize;
112
113 /* Trim leading space. */
Jason Evans12a48872011-11-11 14:41:59 -0800114 chunk_dealloc(ret, alignment - offset, true);
Jason Evanse476f8a2010-01-16 09:53:50 -0800115
116 ret = (void *)((uintptr_t)ret + (alignment - offset));
117
118 trailsize = alloc_size - (alignment - offset) - chunk_size;
119 if (trailsize != 0) {
120 /* Trim trailing space. */
121 assert(trailsize < alloc_size);
122 chunk_dealloc((void *)((uintptr_t)ret + chunk_size),
Jason Evans12a48872011-11-11 14:41:59 -0800123 trailsize, true);
Jason Evanse476f8a2010-01-16 09:53:50 -0800124 }
125 }
126
127 /* Insert node into huge. */
128 node->addr = ret;
129 node->size = chunk_size;
130
131 malloc_mutex_lock(&huge_mtx);
132 extent_tree_ad_insert(&huge, node);
Jason Evans7372b152012-02-10 20:22:09 -0800133 if (config_stats) {
134 stats_cactive_add(chunk_size);
135 huge_nmalloc++;
136 huge_allocated += chunk_size;
137 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800138 malloc_mutex_unlock(&huge_mtx);
139
Jason Evans7372b152012-02-10 20:22:09 -0800140 if (config_fill && zero == false) {
Jason Evans8e3c3c62010-09-17 15:46:18 -0700141 if (opt_junk)
142 memset(ret, 0xa5, chunk_size);
143 else if (opt_zero)
144 memset(ret, 0, chunk_size);
145 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800146
147 return (ret);
148}
149
150void *
Jason Evans8e3c3c62010-09-17 15:46:18 -0700151huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra)
Jason Evanse476f8a2010-01-16 09:53:50 -0800152{
Jason Evanse476f8a2010-01-16 09:53:50 -0800153
Jason Evans8e3c3c62010-09-17 15:46:18 -0700154 /*
155 * Avoid moving the allocation if the size class can be left the same.
156 */
157 if (oldsize > arena_maxclass
158 && CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size)
159 && CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
160 assert(CHUNK_CEILING(oldsize) == oldsize);
Jason Evans7372b152012-02-10 20:22:09 -0800161 if (config_fill && opt_junk && size < oldsize) {
Jason Evans8e3c3c62010-09-17 15:46:18 -0700162 memset((void *)((uintptr_t)ptr + size), 0x5a,
163 oldsize - size);
Jason Evanse476f8a2010-01-16 09:53:50 -0800164 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800165 return (ptr);
166 }
167
Jason Evans8e3c3c62010-09-17 15:46:18 -0700168 /* Reallocation would require a move. */
169 return (NULL);
170}
Jason Evanse476f8a2010-01-16 09:53:50 -0800171
Jason Evans8e3c3c62010-09-17 15:46:18 -0700172void *
173huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra,
174 size_t alignment, bool zero)
175{
176 void *ret;
177 size_t copysize;
178
179 /* Try to avoid moving the allocation. */
180 ret = huge_ralloc_no_move(ptr, oldsize, size, extra);
181 if (ret != NULL)
182 return (ret);
183
184 /*
185 * size and oldsize are different enough that we need to use a
186 * different size class. In that case, fall back to allocating new
187 * space and copying.
188 */
Jason Evans31bfb3e2011-01-31 19:58:22 -0800189 if (alignment > chunksize)
Jason Evans8e3c3c62010-09-17 15:46:18 -0700190 ret = huge_palloc(size + extra, alignment, zero);
191 else
192 ret = huge_malloc(size + extra, zero);
193
194 if (ret == NULL) {
195 if (extra == 0)
196 return (NULL);
197 /* Try again, this time without extra. */
Jason Evans31bfb3e2011-01-31 19:58:22 -0800198 if (alignment > chunksize)
Jason Evans8e3c3c62010-09-17 15:46:18 -0700199 ret = huge_palloc(size, alignment, zero);
200 else
201 ret = huge_malloc(size, zero);
202
203 if (ret == NULL)
204 return (NULL);
205 }
206
207 /*
208 * Copy at most size bytes (not size+extra), since the caller has no
209 * expectation that the extra bytes will be reliably preserved.
210 */
Jason Evanse476f8a2010-01-16 09:53:50 -0800211 copysize = (size < oldsize) ? size : oldsize;
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800212
213 /*
214 * Use mremap(2) if this is a huge-->huge reallocation, and neither the
215 * source nor the destination are in swap or dss.
216 */
217#ifdef JEMALLOC_MREMAP_FIXED
Jason Evans7372b152012-02-10 20:22:09 -0800218 if (oldsize >= chunksize && (config_swap == false || swap_enabled ==
219 false || (chunk_in_swap(ptr) == false && chunk_in_swap(ret) ==
220 false)) && (config_dss == false || (chunk_in_dss(ptr) == false &&
221 chunk_in_dss(ret) == false))) {
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800222 size_t newsize = huge_salloc(ret);
223
Jason Evansfa351d92011-11-09 11:55:19 -0800224 /*
225 * Remove ptr from the tree of huge allocations before
226 * performing the remap operation, in order to avoid the
227 * possibility of another thread acquiring that mapping before
228 * this one removes it from the tree.
229 */
230 huge_dalloc(ptr, false);
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800231 if (mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE|MREMAP_FIXED,
232 ret) == MAP_FAILED) {
233 /*
234 * Assuming no chunk management bugs in the allocator,
235 * the only documented way an error can occur here is
236 * if the application changed the map type for a
237 * portion of the old allocation. This is firmly in
238 * undefined behavior territory, so write a diagnostic
239 * message, and optionally abort.
240 */
241 char buf[BUFERROR_BUF];
242
243 buferror(errno, buf, sizeof(buf));
244 malloc_write("<jemalloc>: Error in mremap(): ");
245 malloc_write(buf);
246 malloc_write("\n");
247 if (opt_abort)
248 abort();
249 memcpy(ret, ptr, copysize);
Jason Evans12a48872011-11-11 14:41:59 -0800250 chunk_dealloc_mmap(ptr, oldsize);
Jason Evansfa351d92011-11-09 11:55:19 -0800251 }
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800252 } else
253#endif
254 {
255 memcpy(ret, ptr, copysize);
256 idalloc(ptr);
257 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800258 return (ret);
259}
260
261void
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800262huge_dalloc(void *ptr, bool unmap)
Jason Evanse476f8a2010-01-16 09:53:50 -0800263{
264 extent_node_t *node, key;
265
266 malloc_mutex_lock(&huge_mtx);
267
268 /* Extract from tree of huge allocations. */
269 key.addr = ptr;
270 node = extent_tree_ad_search(&huge, &key);
271 assert(node != NULL);
272 assert(node->addr == ptr);
273 extent_tree_ad_remove(&huge, node);
274
Jason Evans7372b152012-02-10 20:22:09 -0800275 if (config_stats) {
276 stats_cactive_sub(node->size);
277 huge_ndalloc++;
278 huge_allocated -= node->size;
279 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800280
281 malloc_mutex_unlock(&huge_mtx);
282
Jason Evans7372b152012-02-10 20:22:09 -0800283 if (unmap && config_fill && (config_swap || config_dss) && opt_junk)
284 memset(node->addr, 0x5a, node->size);
Jason Evanse476f8a2010-01-16 09:53:50 -0800285
Jason Evans12a48872011-11-11 14:41:59 -0800286 chunk_dealloc(node->addr, node->size, unmap);
287
Jason Evanse476f8a2010-01-16 09:53:50 -0800288 base_node_dealloc(node);
289}
290
291size_t
292huge_salloc(const void *ptr)
293{
294 size_t ret;
295 extent_node_t *node, key;
296
297 malloc_mutex_lock(&huge_mtx);
298
299 /* Extract from tree of huge allocations. */
300 key.addr = __DECONST(void *, ptr);
301 node = extent_tree_ad_search(&huge, &key);
302 assert(node != NULL);
303
304 ret = node->size;
305
306 malloc_mutex_unlock(&huge_mtx);
307
308 return (ret);
309}
310
Jason Evans50651562010-04-13 16:13:54 -0700311prof_ctx_t *
312huge_prof_ctx_get(const void *ptr)
Jason Evans6109fe02010-02-10 10:37:56 -0800313{
Jason Evans50651562010-04-13 16:13:54 -0700314 prof_ctx_t *ret;
Jason Evans6109fe02010-02-10 10:37:56 -0800315 extent_node_t *node, key;
316
317 malloc_mutex_lock(&huge_mtx);
318
319 /* Extract from tree of huge allocations. */
320 key.addr = __DECONST(void *, ptr);
321 node = extent_tree_ad_search(&huge, &key);
322 assert(node != NULL);
323
Jason Evans50651562010-04-13 16:13:54 -0700324 ret = node->prof_ctx;
Jason Evans6109fe02010-02-10 10:37:56 -0800325
326 malloc_mutex_unlock(&huge_mtx);
327
328 return (ret);
329}
330
331void
Jason Evans50651562010-04-13 16:13:54 -0700332huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx)
Jason Evans6109fe02010-02-10 10:37:56 -0800333{
334 extent_node_t *node, key;
335
336 malloc_mutex_lock(&huge_mtx);
337
338 /* Extract from tree of huge allocations. */
339 key.addr = __DECONST(void *, ptr);
340 node = extent_tree_ad_search(&huge, &key);
341 assert(node != NULL);
342
Jason Evans50651562010-04-13 16:13:54 -0700343 node->prof_ctx = ctx;
Jason Evans6109fe02010-02-10 10:37:56 -0800344
345 malloc_mutex_unlock(&huge_mtx);
346}
Jason Evans6109fe02010-02-10 10:37:56 -0800347
Jason Evanse476f8a2010-01-16 09:53:50 -0800348bool
349huge_boot(void)
350{
351
352 /* Initialize chunks data. */
353 if (malloc_mutex_init(&huge_mtx))
354 return (true);
355 extent_tree_ad_new(&huge);
356
Jason Evans7372b152012-02-10 20:22:09 -0800357 if (config_stats) {
358 huge_nmalloc = 0;
359 huge_ndalloc = 0;
360 huge_allocated = 0;
361 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800362
363 return (false);
364}