2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_EXTENT_DSS_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
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
|
|
|
*/
|
2016-10-14 03:18:38 +08:00
|
|
|
static unsigned dss_prec_default = (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;
|
2016-10-14 03:18:38 +08:00
|
|
|
/* Atomic boolean indicating whether the DSS is exhausted. */
|
|
|
|
static unsigned dss_exhausted;
|
|
|
|
/* Atomic current upper limit on DSS addresses. */
|
2012-03-14 07:31:41 +08:00
|
|
|
static void *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
|
|
|
}
|
2016-10-14 03:18:38 +08:00
|
|
|
ret = (dss_prec_t)atomic_read_u(&dss_prec_default);
|
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
|
|
|
}
|
2016-10-14 03:18:38 +08:00
|
|
|
atomic_write_u(&dss_prec_default, (unsigned)dss_prec);
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
void *max_cur;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the current end of the DSS as max_cur and assure that dss_max is
|
|
|
|
* up to date.
|
|
|
|
*/
|
2017-02-06 15:57:16 +08:00
|
|
|
spin_t spinner = SPIN_INITIALIZER;
|
2016-10-14 03:18:38 +08:00
|
|
|
while (true) {
|
|
|
|
void *max_prev = atomic_read_p(&dss_max);
|
|
|
|
|
|
|
|
max_cur = extent_dss_sbrk(0);
|
|
|
|
if ((uintptr_t)max_prev > (uintptr_t)max_cur) {
|
|
|
|
/*
|
|
|
|
* Another thread optimistically updated dss_max. Wait
|
|
|
|
* for it to finish.
|
|
|
|
*/
|
|
|
|
spin_adaptive(&spinner);
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!atomic_cas_p(&dss_max, max_prev, max_cur)) {
|
2016-10-14 03:18:38 +08:00
|
|
|
break;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
}
|
2016-10-14 03:18:38 +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
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
if (!atomic_read_u(&dss_exhausted)) {
|
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-03-14 08:36:57 +08:00
|
|
|
gap_size_page, false, NSIZES,
|
2017-02-10 05:00:59 +08:00
|
|
|
arena_extent_sn_next(arena),
|
2017-03-14 08:36:57 +08:00
|
|
|
extent_state_active, false, 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
|
|
|
|
|
|
|
/*
|
|
|
|
* Optimistically update dss_max, and roll back below if
|
|
|
|
* sbrk() fails. No other thread will try to extend the
|
|
|
|
* DSS while dss_max is greater than the current DSS
|
|
|
|
* max reported by sbrk(0).
|
|
|
|
*/
|
2017-01-16 08:56:30 +08:00
|
|
|
if (atomic_cas_p(&dss_max, max_cur, dss_next)) {
|
2016-10-14 03:18:38 +08:00
|
|
|
continue;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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-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-03-14 08:36:57 +08:00
|
|
|
size, false, NSIZES,
|
|
|
|
extent_state_active, false, 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
|
|
|
|
* sbrk() call from outside the allocator. Try to roll
|
|
|
|
* back optimistic dss_max update; if rollback fails,
|
|
|
|
* it's due to another caller of this function having
|
|
|
|
* succeeded since this invocation started, in which
|
|
|
|
* case rollback is not necessary.
|
|
|
|
*/
|
|
|
|
atomic_cas_p(&dss_max, dss_next, max_cur);
|
|
|
|
if (dss_prev == (void *)-1) {
|
|
|
|
/* OOM. */
|
|
|
|
atomic_write_u(&dss_exhausted, (unsigned)true);
|
|
|
|
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:
|
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-01-20 10:15:45 +08:00
|
|
|
return extent_in_dss_helper(addr, atomic_read_p(&dss_max));
|
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
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
max = atomic_read_p(&dss_max);
|
|
|
|
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);
|
|
|
|
dss_exhausted = (unsigned)(dss_base == (void *)-1);
|
|
|
|
dss_max = dss_base;
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|