2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_EXTENT_DSS_C_
|
2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2017-05-24 05:36:09 +08:00
|
|
|
#include "jemalloc/internal/extent_dss.h"
|
2017-04-18 07:35:04 +08:00
|
|
|
#include "jemalloc/internal/spin.h"
|
2017-04-12 05:43:12 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2016-10-13 02:49:19 +08:00
|
|
|
const char *opt_dss = DSS_DEFAULT;
|
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
const char *dss_prec_names[] = {
|
|
|
|
"disabled",
|
|
|
|
"primary",
|
|
|
|
"secondary",
|
|
|
|
"N/A"
|
|
|
|
};
|
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
/*
|
2016-10-14 03:18:38 +08:00
|
|
|
* Current dss precedence default, used when creating new arenas. NB: This is
|
|
|
|
* stored as unsigned rather than dss_prec_t because in principle there's no
|
|
|
|
* guarantee that sizeof(dss_prec_t) is the same as sizeof(unsigned), and we use
|
|
|
|
* atomic operations to synchronize the setting.
|
2012-03-14 07:31:41 +08:00
|
|
|
*/
|
2017-04-05 08:58:06 +08:00
|
|
|
static atomic_u_t dss_prec_default = ATOMIC_INIT(
|
|
|
|
(unsigned)DSS_PREC_DEFAULT);
|
2010-01-24 18:53:40 +08:00
|
|
|
|
|
|
|
/* Base address of the DSS. */
|
2012-03-14 07:31:41 +08:00
|
|
|
static void *dss_base;
|
2017-05-17 04:25:17 +08:00
|
|
|
/* Atomic boolean indicating whether a thread is currently extending DSS. */
|
|
|
|
static atomic_b_t dss_extending;
|
2016-10-14 03:18:38 +08:00
|
|
|
/* Atomic boolean indicating whether the DSS is exhausted. */
|
2017-04-05 08:58:06 +08:00
|
|
|
static atomic_b_t dss_exhausted;
|
2016-10-14 03:18:38 +08:00
|
|
|
/* Atomic current upper limit on DSS addresses. */
|
2017-04-05 08:58:06 +08:00
|
|
|
static atomic_p_t dss_max;
|
2010-01-24 18:53:40 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_sbrk(intptr_t increment) {
|
2014-04-16 03:09:48 +08:00
|
|
|
#ifdef JEMALLOC_DSS
|
2017-01-20 10:15:45 +08:00
|
|
|
return sbrk(increment);
|
2013-12-04 13:49:36 +08:00
|
|
|
#else
|
2012-04-13 11:20:58 +08:00
|
|
|
not_implemented();
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2012-04-13 11:20:58 +08:00
|
|
|
#endif
|
2013-12-04 13:49:36 +08:00
|
|
|
}
|
2010-01-24 18:53:40 +08:00
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
dss_prec_t
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_prec_get(void) {
|
2012-10-12 04:53:15 +08:00
|
|
|
dss_prec_t ret;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!have_dss) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return dss_prec_disabled;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-04-05 08:58:06 +08:00
|
|
|
ret = (dss_prec_t)atomic_load_u(&dss_prec_default, ATOMIC_ACQUIRE);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_prec_set(dss_prec_t dss_prec) {
|
|
|
|
if (!have_dss) {
|
2014-04-16 03:09:48 +08:00
|
|
|
return (dss_prec != dss_prec_disabled);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-04-05 08:58:06 +08:00
|
|
|
atomic_store_u(&dss_prec_default, (unsigned)dss_prec, ATOMIC_RELEASE);
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:25:17 +08:00
|
|
|
static void
|
|
|
|
extent_dss_extending_start(void) {
|
|
|
|
spin_t spinner = SPIN_INITIALIZER;
|
|
|
|
while (true) {
|
|
|
|
bool expected = false;
|
|
|
|
if (atomic_compare_exchange_weak_b(&dss_extending, &expected,
|
|
|
|
true, ATOMIC_ACQ_REL, ATOMIC_RELAXED)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spin_adaptive(&spinner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
extent_dss_extending_finish(void) {
|
|
|
|
assert(atomic_load_b(&dss_extending, ATOMIC_RELAXED));
|
|
|
|
|
|
|
|
atomic_store_b(&dss_extending, false, ATOMIC_RELEASE);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_max_update(void *new_addr) {
|
2016-10-14 03:18:38 +08:00
|
|
|
/*
|
|
|
|
* Get the current end of the DSS as max_cur and assure that dss_max is
|
|
|
|
* up to date.
|
|
|
|
*/
|
2017-05-17 04:25:17 +08:00
|
|
|
void *max_cur = extent_dss_sbrk(0);
|
|
|
|
if (max_cur == (void *)-1) {
|
|
|
|
return NULL;
|
2016-10-14 03:18:38 +08:00
|
|
|
}
|
2017-05-17 04:25:17 +08:00
|
|
|
atomic_store_p(&dss_max, max_cur, ATOMIC_RELEASE);
|
2016-10-14 03:18:38 +08:00
|
|
|
/* Fixed new_addr can only be supported if it is at the edge of DSS. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (new_addr != NULL && max_cur != new_addr) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return max_cur;
|
2016-10-14 03:18:38 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
void *
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit) {
|
2016-06-06 06:27:20 +08:00
|
|
|
extent_t *gap;
|
2016-05-27 13:12:38 +08:00
|
|
|
|
2014-04-16 03:09:48 +08:00
|
|
|
cassert(have_dss);
|
2016-05-27 13:12:38 +08:00
|
|
|
assert(size > 0);
|
|
|
|
assert(alignment > 0);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
|
|
|
* sbrk() uses a signed increment argument, so take care not to
|
2016-06-01 05:50:21 +08:00
|
|
|
* interpret a large allocation request as a negative increment.
|
2010-01-24 18:53:40 +08:00
|
|
|
*/
|
2017-01-16 08:56:30 +08:00
|
|
|
if ((intptr_t)size < 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-01-24 18:53:40 +08:00
|
|
|
|
2016-06-06 06:27:20 +08:00
|
|
|
gap = extent_alloc(tsdn, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (gap == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-27 13:12:38 +08:00
|
|
|
|
2017-05-17 04:25:17 +08:00
|
|
|
extent_dss_extending_start();
|
2017-04-05 08:58:06 +08:00
|
|
|
if (!atomic_load_b(&dss_exhausted, ATOMIC_ACQUIRE)) {
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
|
|
|
* The loop is necessary to recover from races with other
|
|
|
|
* threads that are using the DSS for something other than
|
|
|
|
* malloc.
|
|
|
|
*/
|
2016-05-27 13:12:38 +08:00
|
|
|
while (true) {
|
2017-02-10 05:00:59 +08:00
|
|
|
void *max_cur = extent_dss_max_update(new_addr);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (max_cur == NULL) {
|
2016-10-14 03:18:38 +08:00
|
|
|
goto label_oom;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-11-04 03:02:52 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
2017-02-10 05:00:59 +08:00
|
|
|
* Compute how much page-aligned gap space (if any) is
|
|
|
|
* necessary to satisfy alignment. This space can be
|
|
|
|
* recycled for later use.
|
2010-01-24 18:53:40 +08:00
|
|
|
*/
|
2017-02-10 05:00:59 +08:00
|
|
|
void *gap_addr_page = (void *)(PAGE_CEILING(
|
|
|
|
(uintptr_t)max_cur));
|
|
|
|
void *ret = (void *)ALIGNMENT_CEILING(
|
|
|
|
(uintptr_t)gap_addr_page, alignment);
|
|
|
|
size_t gap_size_page = (uintptr_t)ret -
|
|
|
|
(uintptr_t)gap_addr_page;
|
|
|
|
if (gap_size_page != 0) {
|
|
|
|
extent_init(gap, arena, gap_addr_page,
|
2017-12-15 04:46:39 +08:00
|
|
|
gap_size_page, false, SC_NSIZES,
|
2017-02-10 05:00:59 +08:00
|
|
|
arena_extent_sn_next(arena),
|
2017-09-19 08:25:57 +08:00
|
|
|
extent_state_active, false, true, true);
|
2016-05-24 05:56:35 +08:00
|
|
|
}
|
2017-02-10 05:00:59 +08:00
|
|
|
/*
|
|
|
|
* Compute the address just past the end of the desired
|
|
|
|
* allocation space.
|
|
|
|
*/
|
|
|
|
void *dss_next = (void *)((uintptr_t)ret + size);
|
2016-10-14 03:18:38 +08:00
|
|
|
if ((uintptr_t)ret < (uintptr_t)max_cur ||
|
2017-01-16 08:56:30 +08:00
|
|
|
(uintptr_t)dss_next < (uintptr_t)max_cur) {
|
2016-10-14 03:18:38 +08:00
|
|
|
goto label_oom; /* Wrap-around. */
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-02-10 05:00:59 +08:00
|
|
|
/* Compute the increment, including subpage bytes. */
|
|
|
|
void *gap_addr_subpage = max_cur;
|
|
|
|
size_t gap_size_subpage = (uintptr_t)ret -
|
|
|
|
(uintptr_t)gap_addr_subpage;
|
|
|
|
intptr_t incr = gap_size_subpage + size;
|
|
|
|
|
|
|
|
assert((uintptr_t)max_cur + incr == (uintptr_t)ret +
|
|
|
|
size);
|
2016-10-14 03:18:38 +08:00
|
|
|
|
|
|
|
/* Try to allocate. */
|
2017-02-10 05:00:59 +08:00
|
|
|
void *dss_prev = extent_dss_sbrk(incr);
|
2016-10-14 03:18:38 +08:00
|
|
|
if (dss_prev == max_cur) {
|
2010-01-24 18:53:40 +08:00
|
|
|
/* Success. */
|
2017-05-17 04:25:17 +08:00
|
|
|
atomic_store_p(&dss_max, dss_next,
|
|
|
|
ATOMIC_RELEASE);
|
|
|
|
extent_dss_extending_finish();
|
|
|
|
|
2017-02-10 05:00:59 +08:00
|
|
|
if (gap_size_page != 0) {
|
2016-06-06 06:27:20 +08:00
|
|
|
extent_dalloc_gap(tsdn, arena, gap);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-06-06 06:27:20 +08:00
|
|
|
extent_dalloc(tsdn, arena, gap);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (!*commit) {
|
2015-08-13 01:26:54 +08:00
|
|
|
*commit = pages_decommit(ret, size);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
if (*zero && *commit) {
|
|
|
|
extent_hooks_t *extent_hooks =
|
|
|
|
EXTENT_HOOKS_INITIALIZER;
|
|
|
|
extent_t extent;
|
|
|
|
|
|
|
|
extent_init(&extent, arena, ret, size,
|
2017-12-15 04:46:39 +08:00
|
|
|
size, false, SC_NSIZES,
|
2017-09-19 08:25:57 +08:00
|
|
|
extent_state_active, false, true,
|
|
|
|
true);
|
2016-12-04 07:38:25 +08:00
|
|
|
if (extent_purge_forced_wrapper(tsdn,
|
|
|
|
arena, &extent_hooks, &extent, 0,
|
2017-01-16 08:56:30 +08:00
|
|
|
size)) {
|
2016-12-04 07:38:25 +08:00
|
|
|
memset(ret, 0, size);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-01-24 18:53:40 +08:00
|
|
|
}
|
2016-10-14 03:18:38 +08:00
|
|
|
/*
|
|
|
|
* Failure, whether due to OOM or a race with a raw
|
2017-05-17 04:25:17 +08:00
|
|
|
* sbrk() call from outside the allocator.
|
2016-10-14 03:18:38 +08:00
|
|
|
*/
|
|
|
|
if (dss_prev == (void *)-1) {
|
|
|
|
/* OOM. */
|
2017-04-05 08:58:06 +08:00
|
|
|
atomic_store_b(&dss_exhausted, true,
|
|
|
|
ATOMIC_RELEASE);
|
2016-10-14 03:18:38 +08:00
|
|
|
goto label_oom;
|
|
|
|
}
|
2016-05-27 13:12:38 +08:00
|
|
|
}
|
2010-01-24 18:53:40 +08:00
|
|
|
}
|
2016-10-14 03:18:38 +08:00
|
|
|
label_oom:
|
2017-05-17 04:25:17 +08:00
|
|
|
extent_dss_extending_finish();
|
2016-06-06 06:27:20 +08:00
|
|
|
extent_dalloc(tsdn, arena, gap);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2010-01-24 18:53:40 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_in_dss_helper(void *addr, void *max) {
|
2016-10-14 03:18:38 +08:00
|
|
|
return ((uintptr_t)addr >= (uintptr_t)dss_base && (uintptr_t)addr <
|
|
|
|
(uintptr_t)max);
|
2010-12-01 08:50:58 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_in_dss(void *addr) {
|
2014-04-16 03:09:48 +08:00
|
|
|
cassert(have_dss);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2017-04-05 08:58:06 +08:00
|
|
|
return extent_in_dss_helper(addr, atomic_load_p(&dss_max,
|
|
|
|
ATOMIC_ACQUIRE));
|
2010-01-24 18:53:40 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_mergeable(void *addr_a, void *addr_b) {
|
2016-10-14 03:18:38 +08:00
|
|
|
void *max;
|
2012-03-14 07:31:41 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
cassert(have_dss);
|
2012-03-14 07:31:41 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
if ((uintptr_t)addr_a < (uintptr_t)dss_base && (uintptr_t)addr_b <
|
2017-01-16 08:56:30 +08:00
|
|
|
(uintptr_t)dss_base) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
2017-04-05 08:58:06 +08:00
|
|
|
max = atomic_load_p(&dss_max, ATOMIC_ACQUIRE);
|
2016-10-14 03:18:38 +08:00
|
|
|
return (extent_in_dss_helper(addr_a, max) ==
|
|
|
|
extent_in_dss_helper(addr_b, max));
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dss_boot(void) {
|
2016-10-14 03:18:38 +08:00
|
|
|
cassert(have_dss);
|
|
|
|
|
|
|
|
dss_base = extent_dss_sbrk(0);
|
2017-05-17 04:25:17 +08:00
|
|
|
atomic_store_b(&dss_extending, false, ATOMIC_RELAXED);
|
2017-04-05 08:58:06 +08:00
|
|
|
atomic_store_b(&dss_exhausted, dss_base == (void *)-1, ATOMIC_RELAXED);
|
|
|
|
atomic_store_p(&dss_max, dss_base, ATOMIC_RELAXED);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|