2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_HUGE_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
uint64_t huge_nmalloc;
|
|
|
|
uint64_t huge_ndalloc;
|
|
|
|
size_t huge_allocated;
|
|
|
|
|
|
|
|
malloc_mutex_t huge_mtx;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/* Tree of chunks that are stand-alone huge allocations. */
|
|
|
|
static extent_tree_t huge;
|
|
|
|
|
|
|
|
void *
|
|
|
|
huge_malloc(size_t size, bool zero)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t csize;
|
|
|
|
extent_node_t *node;
|
|
|
|
|
|
|
|
/* Allocate one or more contiguous chunks for this request. */
|
|
|
|
|
|
|
|
csize = CHUNK_CEILING(size);
|
|
|
|
if (csize == 0) {
|
|
|
|
/* size is large enough to cause size_t wrap-around. */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate an extent node with which to track the chunk. */
|
|
|
|
node = base_node_alloc();
|
|
|
|
if (node == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
ret = chunk_alloc(csize, false, &zero);
|
2010-01-17 01:53:50 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
base_node_dealloc(node);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node into huge. */
|
|
|
|
node->addr = ret;
|
|
|
|
node->size = csize;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
extent_tree_ad_insert(&huge, node);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
stats_cactive_add(csize);
|
|
|
|
huge_nmalloc++;
|
|
|
|
huge_allocated += csize;
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill && zero == false) {
|
2010-01-17 01:53:50 +08:00
|
|
|
if (opt_junk)
|
|
|
|
memset(ret, 0xa5, csize);
|
|
|
|
else if (opt_zero)
|
|
|
|
memset(ret, 0, csize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only handles large allocations that require more than chunk alignment. */
|
|
|
|
void *
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
huge_palloc(size_t size, size_t alignment, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t alloc_size, chunk_size, offset;
|
|
|
|
extent_node_t *node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This allocation requires alignment that is even larger than chunk
|
|
|
|
* alignment. This means that huge_malloc() isn't good enough.
|
|
|
|
*
|
|
|
|
* Allocate almost twice as many chunks as are demanded by the size or
|
|
|
|
* alignment, in order to assure the alignment can be achieved, then
|
|
|
|
* unmap leading and trailing chunks.
|
|
|
|
*/
|
2011-02-01 11:58:22 +08:00
|
|
|
assert(alignment > chunksize);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
chunk_size = CHUNK_CEILING(size);
|
|
|
|
|
|
|
|
if (size >= alignment)
|
|
|
|
alloc_size = chunk_size + alignment - chunksize;
|
|
|
|
else
|
|
|
|
alloc_size = (alignment << 1) - chunksize;
|
|
|
|
|
|
|
|
/* Allocate an extent node with which to track the chunk. */
|
|
|
|
node = base_node_alloc();
|
|
|
|
if (node == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
ret = chunk_alloc(alloc_size, false, &zero);
|
2010-01-17 01:53:50 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
base_node_dealloc(node);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = (uintptr_t)ret & (alignment - 1);
|
|
|
|
assert((offset & chunksize_mask) == 0);
|
|
|
|
assert(offset < alloc_size);
|
|
|
|
if (offset == 0) {
|
|
|
|
/* Trim trailing space. */
|
|
|
|
chunk_dealloc((void *)((uintptr_t)ret + chunk_size), alloc_size
|
2011-11-12 06:41:59 +08:00
|
|
|
- chunk_size, true);
|
2010-01-17 01:53:50 +08:00
|
|
|
} else {
|
|
|
|
size_t trailsize;
|
|
|
|
|
|
|
|
/* Trim leading space. */
|
2011-11-12 06:41:59 +08:00
|
|
|
chunk_dealloc(ret, alignment - offset, true);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
ret = (void *)((uintptr_t)ret + (alignment - offset));
|
|
|
|
|
|
|
|
trailsize = alloc_size - (alignment - offset) - chunk_size;
|
|
|
|
if (trailsize != 0) {
|
|
|
|
/* Trim trailing space. */
|
|
|
|
assert(trailsize < alloc_size);
|
|
|
|
chunk_dealloc((void *)((uintptr_t)ret + chunk_size),
|
2011-11-12 06:41:59 +08:00
|
|
|
trailsize, true);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node into huge. */
|
|
|
|
node->addr = ret;
|
|
|
|
node->size = chunk_size;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
extent_tree_ad_insert(&huge, node);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
stats_cactive_add(chunk_size);
|
|
|
|
huge_nmalloc++;
|
|
|
|
huge_allocated += chunk_size;
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill && zero == false) {
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
if (opt_junk)
|
|
|
|
memset(ret, 0xa5, chunk_size);
|
|
|
|
else if (opt_zero)
|
|
|
|
memset(ret, 0, chunk_size);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
/*
|
|
|
|
* Avoid moving the allocation if the size class can be left the same.
|
|
|
|
*/
|
|
|
|
if (oldsize > arena_maxclass
|
|
|
|
&& CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size)
|
|
|
|
&& CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
|
|
|
|
assert(CHUNK_CEILING(oldsize) == oldsize);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill && opt_junk && size < oldsize) {
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
memset((void *)((uintptr_t)ptr + size), 0x5a,
|
|
|
|
oldsize - size);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
/* Reallocation would require a move. */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra,
|
|
|
|
size_t alignment, bool zero)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t copysize;
|
|
|
|
|
|
|
|
/* Try to avoid moving the allocation. */
|
|
|
|
ret = huge_ralloc_no_move(ptr, oldsize, size, extra);
|
|
|
|
if (ret != NULL)
|
|
|
|
return (ret);
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
* size and oldsize are different enough that we need to use a
|
|
|
|
* different size class. In that case, fall back to allocating new
|
|
|
|
* space and copying.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
2011-02-01 11:58:22 +08:00
|
|
|
if (alignment > chunksize)
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
ret = huge_palloc(size + extra, alignment, zero);
|
|
|
|
else
|
|
|
|
ret = huge_malloc(size + extra, zero);
|
|
|
|
|
|
|
|
if (ret == NULL) {
|
|
|
|
if (extra == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Try again, this time without extra. */
|
2011-02-01 11:58:22 +08:00
|
|
|
if (alignment > chunksize)
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
ret = huge_palloc(size, alignment, zero);
|
|
|
|
else
|
|
|
|
ret = huge_malloc(size, zero);
|
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
/*
|
|
|
|
* Copy at most size bytes (not size+extra), since the caller has no
|
|
|
|
* expectation that the extra bytes will be reliably preserved.
|
|
|
|
*/
|
2010-01-17 01:53:50 +08:00
|
|
|
copysize = (size < oldsize) ? size : oldsize;
|
2010-12-01 08:50:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use mremap(2) if this is a huge-->huge reallocation, and neither the
|
2012-02-14 02:56:17 +08:00
|
|
|
* source nor the destination are in dss.
|
2010-12-01 08:50:58 +08:00
|
|
|
*/
|
|
|
|
#ifdef JEMALLOC_MREMAP_FIXED
|
2012-02-14 02:56:17 +08:00
|
|
|
if (oldsize >= chunksize && (config_dss == false || (chunk_in_dss(ptr)
|
|
|
|
== false && chunk_in_dss(ret) == false))) {
|
2010-12-01 08:50:58 +08:00
|
|
|
size_t newsize = huge_salloc(ret);
|
|
|
|
|
2011-11-10 03:55:19 +08:00
|
|
|
/*
|
|
|
|
* Remove ptr from the tree of huge allocations before
|
|
|
|
* performing the remap operation, in order to avoid the
|
|
|
|
* possibility of another thread acquiring that mapping before
|
|
|
|
* this one removes it from the tree.
|
|
|
|
*/
|
|
|
|
huge_dalloc(ptr, false);
|
2010-12-01 08:50:58 +08:00
|
|
|
if (mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE|MREMAP_FIXED,
|
|
|
|
ret) == MAP_FAILED) {
|
|
|
|
/*
|
|
|
|
* Assuming no chunk management bugs in the allocator,
|
|
|
|
* the only documented way an error can occur here is
|
|
|
|
* if the application changed the map type for a
|
|
|
|
* portion of the old allocation. This is firmly in
|
|
|
|
* undefined behavior territory, so write a diagnostic
|
|
|
|
* message, and optionally abort.
|
|
|
|
*/
|
|
|
|
char buf[BUFERROR_BUF];
|
|
|
|
|
|
|
|
buferror(errno, buf, sizeof(buf));
|
2012-03-07 06:57:45 +08:00
|
|
|
malloc_printf("<jemalloc>: Error in mremap(): %s\n",
|
|
|
|
buf);
|
2010-12-01 08:50:58 +08:00
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
|
|
|
memcpy(ret, ptr, copysize);
|
2011-11-12 06:41:59 +08:00
|
|
|
chunk_dealloc_mmap(ptr, oldsize);
|
2011-11-10 03:55:19 +08:00
|
|
|
}
|
2010-12-01 08:50:58 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
memcpy(ret, ptr, copysize);
|
|
|
|
idalloc(ptr);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-01 08:50:58 +08:00
|
|
|
huge_dalloc(void *ptr, bool unmap)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = ptr;
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(node->addr == ptr);
|
|
|
|
extent_tree_ad_remove(&huge, node);
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
stats_cactive_sub(node->size);
|
|
|
|
huge_ndalloc++;
|
|
|
|
huge_allocated -= node->size;
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2012-02-14 02:56:17 +08:00
|
|
|
if (unmap && config_fill && config_dss && opt_junk)
|
2012-02-11 12:22:09 +08:00
|
|
|
memset(node->addr, 0x5a, node->size);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-11-12 06:41:59 +08:00
|
|
|
chunk_dealloc(node->addr, node->size, unmap);
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
base_node_dealloc(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
huge_salloc(const void *ptr)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
|
|
|
ret = node->size;
|
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2010-04-14 07:13:54 +08:00
|
|
|
prof_ctx_t *
|
|
|
|
huge_prof_ctx_get(const void *ptr)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2010-04-14 07:13:54 +08:00
|
|
|
prof_ctx_t *ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
2010-04-14 07:13:54 +08:00
|
|
|
ret = node->prof_ctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-14 07:13:54 +08:00
|
|
|
huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
2010-04-14 07:13:54 +08:00
|
|
|
node->prof_ctx = ctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
bool
|
|
|
|
huge_boot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Initialize chunks data. */
|
|
|
|
if (malloc_mutex_init(&huge_mtx))
|
|
|
|
return (true);
|
|
|
|
extent_tree_ad_new(&huge);
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
huge_nmalloc = 0;
|
|
|
|
huge_ndalloc = 0;
|
|
|
|
huge_allocated = 0;
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
huge_prefork(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_prefork(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
huge_postfork_parent(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_parent(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
huge_postfork_child(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_child(&huge_mtx);
|
|
|
|
}
|