1 #define JEMALLOC_HUGE_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
11 malloc_mutex_t huge_mtx;
13 /******************************************************************************/
15 /* Tree of chunks that are stand-alone huge allocations. */
16 static extent_tree_t huge;
19 huge_malloc(size_t size, bool zero)
22 return (huge_palloc(size, chunksize, zero));
26 huge_palloc(size_t size, size_t alignment, bool zero)
32 /* Allocate one or more contiguous chunks for this request. */
34 csize = CHUNK_CEILING(size);
36 /* size is large enough to cause size_t wrap-around. */
40 /* Allocate an extent node with which to track the chunk. */
41 node = base_node_alloc();
45 ret = chunk_alloc(csize, alignment, false, &zero);
47 base_node_dealloc(node);
51 /* Insert node into huge. */
55 malloc_mutex_lock(&huge_mtx);
56 extent_tree_ad_insert(&huge, node);
58 stats_cactive_add(csize);
60 huge_allocated += csize;
62 malloc_mutex_unlock(&huge_mtx);
64 if (config_fill && zero == false) {
66 memset(ret, 0xa5, csize);
68 memset(ret, 0, csize);
75 huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra)
79 * Avoid moving the allocation if the size class can be left the same.
81 if (oldsize > arena_maxclass
82 && CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size)
83 && CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
84 assert(CHUNK_CEILING(oldsize) == oldsize);
85 if (config_fill && opt_junk && size < oldsize) {
86 memset((void *)((uintptr_t)ptr + size), 0x5a,
92 /* Reallocation would require a move. */
97 huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra,
98 size_t alignment, bool zero)
103 /* Try to avoid moving the allocation. */
104 ret = huge_ralloc_no_move(ptr, oldsize, size, extra);
109 * size and oldsize are different enough that we need to use a
110 * different size class. In that case, fall back to allocating new
113 if (alignment > chunksize)
114 ret = huge_palloc(size + extra, alignment, zero);
116 ret = huge_malloc(size + extra, zero);
121 /* Try again, this time without extra. */
122 if (alignment > chunksize)
123 ret = huge_palloc(size, alignment, zero);
125 ret = huge_malloc(size, zero);
132 * Copy at most size bytes (not size+extra), since the caller has no
133 * expectation that the extra bytes will be reliably preserved.
135 copysize = (size < oldsize) ? size : oldsize;
138 * Use mremap(2) if this is a huge-->huge reallocation, and neither the
139 * source nor the destination are in dss.
141 #ifdef JEMALLOC_MREMAP_FIXED
142 if (oldsize >= chunksize && (config_dss == false || (chunk_in_dss(ptr)
143 == false && chunk_in_dss(ret) == false))) {
144 size_t newsize = huge_salloc(ret);
147 * Remove ptr from the tree of huge allocations before
148 * performing the remap operation, in order to avoid the
149 * possibility of another thread acquiring that mapping before
150 * this one removes it from the tree.
152 huge_dalloc(ptr, false);
153 if (mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE|MREMAP_FIXED,
154 ret) == MAP_FAILED) {
156 * Assuming no chunk management bugs in the allocator,
157 * the only documented way an error can occur here is
158 * if the application changed the map type for a
159 * portion of the old allocation. This is firmly in
160 * undefined behavior territory, so write a diagnostic
161 * message, and optionally abort.
163 char buf[BUFERROR_BUF];
165 buferror(errno, buf, sizeof(buf));
166 malloc_printf("<jemalloc>: Error in mremap(): %s\n",
170 memcpy(ret, ptr, copysize);
171 chunk_dealloc_mmap(ptr, oldsize);
176 memcpy(ret, ptr, copysize);
183 huge_dalloc(void *ptr, bool unmap)
185 extent_node_t *node, key;
187 malloc_mutex_lock(&huge_mtx);
189 /* Extract from tree of huge allocations. */
191 node = extent_tree_ad_search(&huge, &key);
192 assert(node != NULL);
193 assert(node->addr == ptr);
194 extent_tree_ad_remove(&huge, node);
197 stats_cactive_sub(node->size);
199 huge_allocated -= node->size;
202 malloc_mutex_unlock(&huge_mtx);
204 if (unmap && config_fill && config_dss && opt_junk)
205 memset(node->addr, 0x5a, node->size);
207 chunk_dealloc(node->addr, node->size, unmap);
209 base_node_dealloc(node);
213 huge_salloc(const void *ptr)
216 extent_node_t *node, key;
218 malloc_mutex_lock(&huge_mtx);
220 /* Extract from tree of huge allocations. */
221 key.addr = __DECONST(void *, ptr);
222 node = extent_tree_ad_search(&huge, &key);
223 assert(node != NULL);
227 malloc_mutex_unlock(&huge_mtx);
233 huge_prof_ctx_get(const void *ptr)
236 extent_node_t *node, key;
238 malloc_mutex_lock(&huge_mtx);
240 /* Extract from tree of huge allocations. */
241 key.addr = __DECONST(void *, ptr);
242 node = extent_tree_ad_search(&huge, &key);
243 assert(node != NULL);
245 ret = node->prof_ctx;
247 malloc_mutex_unlock(&huge_mtx);
253 huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx)
255 extent_node_t *node, key;
257 malloc_mutex_lock(&huge_mtx);
259 /* Extract from tree of huge allocations. */
260 key.addr = __DECONST(void *, ptr);
261 node = extent_tree_ad_search(&huge, &key);
262 assert(node != NULL);
264 node->prof_ctx = ctx;
266 malloc_mutex_unlock(&huge_mtx);
273 /* Initialize chunks data. */
274 if (malloc_mutex_init(&huge_mtx))
276 extent_tree_ad_new(&huge);
291 malloc_mutex_prefork(&huge_mtx);
295 huge_postfork_parent(void)
298 malloc_mutex_postfork_parent(&huge_mtx);
302 huge_postfork_child(void)
305 malloc_mutex_postfork_child(&huge_mtx);