]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/jemalloc/src/huge.c
MFV: Import atf-0.20.
[FreeBSD/FreeBSD.git] / contrib / jemalloc / src / huge.c
1 #define JEMALLOC_HUGE_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
3
4 /******************************************************************************/
5 /* Data. */
6
7 uint64_t        huge_nmalloc;
8 uint64_t        huge_ndalloc;
9 size_t          huge_allocated;
10
11 malloc_mutex_t  huge_mtx;
12
13 /******************************************************************************/
14
15 /* Tree of chunks that are stand-alone huge allocations. */
16 static extent_tree_t    huge;
17
18 void *
19 huge_malloc(size_t size, bool zero)
20 {
21
22         return (huge_palloc(size, chunksize, zero));
23 }
24
25 void *
26 huge_palloc(size_t size, size_t alignment, bool zero)
27 {
28         void *ret;
29         size_t csize;
30         extent_node_t *node;
31         bool is_zeroed;
32
33         /* Allocate one or more contiguous chunks for this request. */
34
35         csize = CHUNK_CEILING(size);
36         if (csize == 0) {
37                 /* size is large enough to cause size_t wrap-around. */
38                 return (NULL);
39         }
40
41         /* Allocate an extent node with which to track the chunk. */
42         node = base_node_alloc();
43         if (node == NULL)
44                 return (NULL);
45
46         /*
47          * Copy zero into is_zeroed and pass the copy to chunk_alloc(), so that
48          * it is possible to make correct junk/zero fill decisions below.
49          */
50         is_zeroed = zero;
51         ret = chunk_alloc(csize, alignment, false, &is_zeroed,
52             chunk_dss_prec_get());
53         if (ret == NULL) {
54                 base_node_dealloc(node);
55                 return (NULL);
56         }
57
58         /* Insert node into huge. */
59         node->addr = ret;
60         node->size = csize;
61
62         malloc_mutex_lock(&huge_mtx);
63         extent_tree_ad_insert(&huge, node);
64         if (config_stats) {
65                 stats_cactive_add(csize);
66                 huge_nmalloc++;
67                 huge_allocated += csize;
68         }
69         malloc_mutex_unlock(&huge_mtx);
70
71         if (config_fill && zero == false) {
72                 if (opt_junk)
73                         memset(ret, 0xa5, csize);
74                 else if (opt_zero && is_zeroed == false)
75                         memset(ret, 0, csize);
76         }
77
78         return (ret);
79 }
80
81 bool
82 huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra)
83 {
84
85         /*
86          * Avoid moving the allocation if the size class can be left the same.
87          */
88         if (oldsize > arena_maxclass
89             && CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size)
90             && CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
91                 assert(CHUNK_CEILING(oldsize) == oldsize);
92                 return (false);
93         }
94
95         /* Reallocation would require a move. */
96         return (true);
97 }
98
99 void *
100 huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra,
101     size_t alignment, bool zero, bool try_tcache_dalloc)
102 {
103         void *ret;
104         size_t copysize;
105
106         /* Try to avoid moving the allocation. */
107         if (huge_ralloc_no_move(ptr, oldsize, size, extra) == false)
108                 return (ptr);
109
110         /*
111          * size and oldsize are different enough that we need to use a
112          * different size class.  In that case, fall back to allocating new
113          * space and copying.
114          */
115         if (alignment > chunksize)
116                 ret = huge_palloc(size + extra, alignment, zero);
117         else
118                 ret = huge_malloc(size + extra, zero);
119
120         if (ret == NULL) {
121                 if (extra == 0)
122                         return (NULL);
123                 /* Try again, this time without extra. */
124                 if (alignment > chunksize)
125                         ret = huge_palloc(size, alignment, zero);
126                 else
127                         ret = huge_malloc(size, zero);
128
129                 if (ret == NULL)
130                         return (NULL);
131         }
132
133         /*
134          * Copy at most size bytes (not size+extra), since the caller has no
135          * expectation that the extra bytes will be reliably preserved.
136          */
137         copysize = (size < oldsize) ? size : oldsize;
138
139 #ifdef JEMALLOC_MREMAP
140         /*
141          * Use mremap(2) if this is a huge-->huge reallocation, and neither the
142          * source nor the destination are in dss.
143          */
144         if (oldsize >= chunksize && (config_dss == false || (chunk_in_dss(ptr)
145             == false && chunk_in_dss(ret) == false))) {
146                 size_t newsize = huge_salloc(ret);
147
148                 /*
149                  * Remove ptr from the tree of huge allocations before
150                  * performing the remap operation, in order to avoid the
151                  * possibility of another thread acquiring that mapping before
152                  * this one removes it from the tree.
153                  */
154                 huge_dalloc(ptr, false);
155                 if (mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE|MREMAP_FIXED,
156                     ret) == MAP_FAILED) {
157                         /*
158                          * Assuming no chunk management bugs in the allocator,
159                          * the only documented way an error can occur here is
160                          * if the application changed the map type for a
161                          * portion of the old allocation.  This is firmly in
162                          * undefined behavior territory, so write a diagnostic
163                          * message, and optionally abort.
164                          */
165                         char buf[BUFERROR_BUF];
166
167                         buferror(get_errno(), buf, sizeof(buf));
168                         malloc_printf("<jemalloc>: Error in mremap(): %s\n",
169                             buf);
170                         if (opt_abort)
171                                 abort();
172                         memcpy(ret, ptr, copysize);
173                         chunk_dealloc_mmap(ptr, oldsize);
174                 }
175         } else
176 #endif
177         {
178                 memcpy(ret, ptr, copysize);
179                 iqalloct(ptr, try_tcache_dalloc);
180         }
181         return (ret);
182 }
183
184 #ifdef JEMALLOC_JET
185 #undef huge_dalloc_junk
186 #define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk_impl)
187 #endif
188 static void
189 huge_dalloc_junk(void *ptr, size_t usize)
190 {
191
192         if (config_fill && config_dss && opt_junk) {
193                 /*
194                  * Only bother junk filling if the chunk isn't about to be
195                  * unmapped.
196                  */
197                 if (config_munmap == false || (config_dss && chunk_in_dss(ptr)))
198                         memset(ptr, 0x5a, usize);
199         }
200 }
201 #ifdef JEMALLOC_JET
202 #undef huge_dalloc_junk
203 #define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk)
204 huge_dalloc_junk_t *huge_dalloc_junk = JEMALLOC_N(huge_dalloc_junk_impl);
205 #endif
206
207 void
208 huge_dalloc(void *ptr, bool unmap)
209 {
210         extent_node_t *node, key;
211
212         malloc_mutex_lock(&huge_mtx);
213
214         /* Extract from tree of huge allocations. */
215         key.addr = ptr;
216         node = extent_tree_ad_search(&huge, &key);
217         assert(node != NULL);
218         assert(node->addr == ptr);
219         extent_tree_ad_remove(&huge, node);
220
221         if (config_stats) {
222                 stats_cactive_sub(node->size);
223                 huge_ndalloc++;
224                 huge_allocated -= node->size;
225         }
226
227         malloc_mutex_unlock(&huge_mtx);
228
229         if (unmap)
230                 huge_dalloc_junk(node->addr, node->size);
231
232         chunk_dealloc(node->addr, node->size, unmap);
233
234         base_node_dealloc(node);
235 }
236
237 size_t
238 huge_salloc(const void *ptr)
239 {
240         size_t ret;
241         extent_node_t *node, key;
242
243         malloc_mutex_lock(&huge_mtx);
244
245         /* Extract from tree of huge allocations. */
246         key.addr = __DECONST(void *, ptr);
247         node = extent_tree_ad_search(&huge, &key);
248         assert(node != NULL);
249
250         ret = node->size;
251
252         malloc_mutex_unlock(&huge_mtx);
253
254         return (ret);
255 }
256
257 prof_ctx_t *
258 huge_prof_ctx_get(const void *ptr)
259 {
260         prof_ctx_t *ret;
261         extent_node_t *node, key;
262
263         malloc_mutex_lock(&huge_mtx);
264
265         /* Extract from tree of huge allocations. */
266         key.addr = __DECONST(void *, ptr);
267         node = extent_tree_ad_search(&huge, &key);
268         assert(node != NULL);
269
270         ret = node->prof_ctx;
271
272         malloc_mutex_unlock(&huge_mtx);
273
274         return (ret);
275 }
276
277 void
278 huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx)
279 {
280         extent_node_t *node, key;
281
282         malloc_mutex_lock(&huge_mtx);
283
284         /* Extract from tree of huge allocations. */
285         key.addr = __DECONST(void *, ptr);
286         node = extent_tree_ad_search(&huge, &key);
287         assert(node != NULL);
288
289         node->prof_ctx = ctx;
290
291         malloc_mutex_unlock(&huge_mtx);
292 }
293
294 bool
295 huge_boot(void)
296 {
297
298         /* Initialize chunks data. */
299         if (malloc_mutex_init(&huge_mtx))
300                 return (true);
301         extent_tree_ad_new(&huge);
302
303         if (config_stats) {
304                 huge_nmalloc = 0;
305                 huge_ndalloc = 0;
306                 huge_allocated = 0;
307         }
308
309         return (false);
310 }
311
312 void
313 huge_prefork(void)
314 {
315
316         malloc_mutex_prefork(&huge_mtx);
317 }
318
319 void
320 huge_postfork_parent(void)
321 {
322
323         malloc_mutex_postfork_parent(&huge_mtx);
324 }
325
326 void
327 huge_postfork_child(void)
328 {
329
330         malloc_mutex_postfork_child(&huge_mtx);
331 }