2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_BASE_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
static malloc_mutex_t base_mtx;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Current pages that are being used for internal memory allocations. These
|
|
|
|
* pages are carved up in cacheline-size quanta, so that there is no chance of
|
|
|
|
* false cache line sharing.
|
|
|
|
*/
|
|
|
|
static void *base_pages;
|
|
|
|
static void *base_next_addr;
|
|
|
|
static void *base_past_addr; /* Addr immediately past base_pages. */
|
|
|
|
static extent_node_t *base_nodes;
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
static bool base_pages_alloc(size_t minsize);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
static bool
|
2010-01-24 18:53:40 +08:00
|
|
|
base_pages_alloc(size_t minsize)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
size_t csize;
|
2010-01-25 09:13:07 +08:00
|
|
|
bool zero;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
assert(minsize != 0);
|
2010-01-24 18:53:40 +08:00
|
|
|
csize = CHUNK_CEILING(minsize);
|
2010-01-25 09:13:07 +08:00
|
|
|
zero = false;
|
2014-05-06 06:16:56 +08:00
|
|
|
base_pages = chunk_alloc(NULL, csize, chunksize, true, &zero,
|
2012-10-12 04:53:15 +08:00
|
|
|
chunk_dss_prec_get());
|
2010-01-17 01:53:50 +08:00
|
|
|
if (base_pages == NULL)
|
|
|
|
return (true);
|
|
|
|
base_next_addr = base_pages;
|
|
|
|
base_past_addr = (void *)((uintptr_t)base_pages + csize);
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
base_alloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t csize;
|
|
|
|
|
|
|
|
/* Round size up to nearest multiple of the cacheline size. */
|
|
|
|
csize = CACHELINE_CEILING(size);
|
|
|
|
|
|
|
|
malloc_mutex_lock(&base_mtx);
|
|
|
|
/* Make sure there's enough space for the allocation. */
|
|
|
|
if ((uintptr_t)base_next_addr + csize > (uintptr_t)base_past_addr) {
|
|
|
|
if (base_pages_alloc(csize)) {
|
|
|
|
malloc_mutex_unlock(&base_mtx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Allocate. */
|
|
|
|
ret = base_next_addr;
|
|
|
|
base_next_addr = (void *)((uintptr_t)base_next_addr + csize);
|
|
|
|
malloc_mutex_unlock(&base_mtx);
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, csize);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2012-02-03 14:04:57 +08:00
|
|
|
void *
|
|
|
|
base_calloc(size_t number, size_t size)
|
|
|
|
{
|
|
|
|
void *ret = base_alloc(number * size);
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
memset(ret, 0, number * size);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
extent_node_t *
|
|
|
|
base_node_alloc(void)
|
|
|
|
{
|
|
|
|
extent_node_t *ret;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&base_mtx);
|
|
|
|
if (base_nodes != NULL) {
|
|
|
|
ret = base_nodes;
|
|
|
|
base_nodes = *(extent_node_t **)ret;
|
|
|
|
malloc_mutex_unlock(&base_mtx);
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret,
|
|
|
|
sizeof(extent_node_t));
|
2010-01-17 01:53:50 +08:00
|
|
|
} else {
|
|
|
|
malloc_mutex_unlock(&base_mtx);
|
|
|
|
ret = (extent_node_t *)base_alloc(sizeof(extent_node_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
base_node_dealloc(extent_node_t *node)
|
|
|
|
{
|
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(node, sizeof(extent_node_t));
|
2010-01-17 01:53:50 +08:00
|
|
|
malloc_mutex_lock(&base_mtx);
|
|
|
|
*(extent_node_t **)node = base_nodes;
|
|
|
|
base_nodes = node;
|
|
|
|
malloc_mutex_unlock(&base_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
base_boot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
base_nodes = NULL;
|
|
|
|
if (malloc_mutex_init(&base_mtx))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
base_prefork(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_prefork(&base_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
base_postfork_parent(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_parent(&base_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
base_postfork_child(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_child(&base_mtx);
|
|
|
|
}
|