2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_EXTENT_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-06-02 03:10:39 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
rtree_t extents_rtree;
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
static void *extent_alloc_default(extent_hooks_t *extent_hooks,
|
|
|
|
void *new_addr, size_t size, size_t alignment, bool *zero, bool *commit,
|
|
|
|
unsigned arena_ind);
|
|
|
|
static bool extent_dalloc_default(extent_hooks_t *extent_hooks, void *addr,
|
|
|
|
size_t size, bool committed, unsigned arena_ind);
|
|
|
|
static bool extent_commit_default(extent_hooks_t *extent_hooks, void *addr,
|
|
|
|
size_t size, size_t offset, size_t length, unsigned arena_ind);
|
|
|
|
static bool extent_decommit_default(extent_hooks_t *extent_hooks,
|
|
|
|
void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
|
2016-12-04 07:38:25 +08:00
|
|
|
#ifdef PAGES_CAN_PURGE_LAZY
|
|
|
|
static bool extent_purge_lazy_default(extent_hooks_t *extent_hooks,
|
|
|
|
void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
|
|
|
|
#endif
|
|
|
|
#ifdef PAGES_CAN_PURGE_FORCED
|
|
|
|
static bool extent_purge_forced_default(extent_hooks_t *extent_hooks,
|
|
|
|
void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
|
|
|
|
#endif
|
|
|
|
#ifdef JEMALLOC_MAPS_COALESCE
|
2016-06-04 03:05:53 +08:00
|
|
|
static bool extent_split_default(extent_hooks_t *extent_hooks, void *addr,
|
|
|
|
size_t size, size_t size_a, size_t size_b, bool committed,
|
|
|
|
unsigned arena_ind);
|
|
|
|
static bool extent_merge_default(extent_hooks_t *extent_hooks, void *addr_a,
|
|
|
|
size_t size_a, void *addr_b, size_t size_b, bool committed,
|
2016-06-02 03:59:02 +08:00
|
|
|
unsigned arena_ind);
|
2016-12-04 07:38:25 +08:00
|
|
|
#endif
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
const extent_hooks_t extent_hooks_default = {
|
|
|
|
extent_alloc_default,
|
|
|
|
extent_dalloc_default,
|
|
|
|
extent_commit_default,
|
2016-12-04 07:38:25 +08:00
|
|
|
extent_decommit_default
|
|
|
|
#ifdef PAGES_CAN_PURGE_LAZY
|
|
|
|
,
|
|
|
|
extent_purge_lazy_default
|
|
|
|
#else
|
|
|
|
,
|
|
|
|
NULL
|
|
|
|
#endif
|
|
|
|
#ifdef PAGES_CAN_PURGE_FORCED
|
|
|
|
,
|
|
|
|
extent_purge_forced_default
|
|
|
|
#else
|
|
|
|
,
|
|
|
|
NULL
|
|
|
|
#endif
|
|
|
|
#ifdef JEMALLOC_MAPS_COALESCE
|
|
|
|
,
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_split_default,
|
|
|
|
extent_merge_default
|
2016-12-04 07:38:25 +08:00
|
|
|
#endif
|
2016-06-02 03:59:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Used exclusively for gdump triggering. */
|
2016-06-06 05:43:20 +08:00
|
|
|
static size_t curpages;
|
|
|
|
static size_t highpages;
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
* Function prototypes for static functions that are referenced prior to
|
|
|
|
* definition.
|
|
|
|
*/
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
static void extent_deregister(tsdn_t *tsdn, extent_t *extent);
|
|
|
|
static void extent_record(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, extents_t *extents, extent_t *extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-05-24 05:56:35 +08:00
|
|
|
extent_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_alloc(tsdn_t *tsdn, arena_t *arena) {
|
2016-05-24 05:56:35 +08:00
|
|
|
extent_t *extent;
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
|
|
|
malloc_mutex_lock(tsdn, &arena->extent_freelist_mtx);
|
|
|
|
extent = extent_list_last(&arena->extent_freelist);
|
2016-05-24 05:56:35 +08:00
|
|
|
if (extent == NULL) {
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->extent_freelist_mtx);
|
|
|
|
return base_alloc(tsdn, arena->base, sizeof(extent_t), QUANTUM);
|
2016-05-24 05:56:35 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_list_remove(&arena->extent_freelist, extent);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->extent_freelist_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-05-24 05:56:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
|
|
|
malloc_mutex_lock(tsdn, &arena->extent_freelist_mtx);
|
|
|
|
extent_list_append(&arena->extent_freelist, extent);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->extent_freelist_mtx);
|
2016-05-24 05:56:35 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_get(arena_t *arena) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return base_extent_hooks_get(arena->base);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_set(arena_t *arena, extent_hooks_t *extent_hooks) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return base_extent_hooks_set(arena->base, extent_hooks);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_assure_initialized(arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks) {
|
|
|
|
if (*r_extent_hooks == EXTENT_HOOKS_INITIALIZER) {
|
2016-06-04 03:05:53 +08:00
|
|
|
*r_extent_hooks = extent_hooks_get(arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-05-18 05:58:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef extent_size_quantize_floor
|
2017-01-20 13:41:41 +08:00
|
|
|
#define extent_size_quantize_floor JEMALLOC_N(n_extent_size_quantize_floor)
|
2016-05-18 05:58:56 +08:00
|
|
|
#endif
|
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_size_quantize_floor(size_t size) {
|
2016-05-18 05:58:56 +08:00
|
|
|
size_t ret;
|
|
|
|
pszind_t pind;
|
2015-05-05 00:58:36 +08:00
|
|
|
|
2016-05-18 05:58:56 +08:00
|
|
|
assert(size > 0);
|
|
|
|
assert((size & PAGE_MASK) == 0);
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
assert(size == PAGE_CEILING(size));
|
|
|
|
|
|
|
|
pind = psz2ind(size - large_pad + 1);
|
|
|
|
if (pind == 0) {
|
|
|
|
/*
|
|
|
|
* Avoid underflow. This short-circuit would also do the right
|
|
|
|
* thing for all sizes in the range for which there are
|
|
|
|
* PAGE-spaced size classes, but it's simplest to just handle
|
|
|
|
* the one case that would cause erroneous results.
|
|
|
|
*/
|
2017-01-20 10:15:45 +08:00
|
|
|
return size;
|
2016-05-18 05:58:56 +08:00
|
|
|
}
|
|
|
|
ret = pind2sz(pind - 1) + large_pad;
|
|
|
|
assert(ret <= size);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2015-05-05 00:58:36 +08:00
|
|
|
}
|
2016-05-18 05:58:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef extent_size_quantize_floor
|
2017-01-20 13:41:41 +08:00
|
|
|
#define extent_size_quantize_floor JEMALLOC_N(extent_size_quantize_floor)
|
2016-05-18 05:58:56 +08:00
|
|
|
extent_size_quantize_t *extent_size_quantize_floor =
|
|
|
|
JEMALLOC_N(n_extent_size_quantize_floor);
|
|
|
|
#endif
|
2015-05-05 00:58:36 +08:00
|
|
|
|
2016-05-18 05:58:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef extent_size_quantize_ceil
|
2017-01-20 13:41:41 +08:00
|
|
|
#define extent_size_quantize_ceil JEMALLOC_N(n_extent_size_quantize_ceil)
|
2016-05-18 05:58:56 +08:00
|
|
|
#endif
|
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_size_quantize_ceil(size_t size) {
|
2016-05-18 05:58:56 +08:00
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
assert(size > 0);
|
2016-06-01 05:50:21 +08:00
|
|
|
assert(size - large_pad <= LARGE_MAXCLASS);
|
2016-05-18 05:58:56 +08:00
|
|
|
assert((size & PAGE_MASK) == 0);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-18 05:58:56 +08:00
|
|
|
ret = extent_size_quantize_floor(size);
|
|
|
|
if (ret < size) {
|
|
|
|
/*
|
|
|
|
* Skip a quantization that may have an adequately large extent,
|
|
|
|
* because under-sized extents may be mixed in. This only
|
|
|
|
* happens when an unusual size is requested, i.e. for aligned
|
|
|
|
* allocation, and is just one of several places where linear
|
|
|
|
* search would potentially find sufficiently aligned available
|
|
|
|
* memory somewhere lower.
|
|
|
|
*/
|
|
|
|
ret = pind2sz(psz2ind(ret - large_pad + 1)) + large_pad;
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2016-05-18 05:58:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef extent_size_quantize_ceil
|
2017-01-20 13:41:41 +08:00
|
|
|
#define extent_size_quantize_ceil JEMALLOC_N(extent_size_quantize_ceil)
|
2016-05-18 05:58:56 +08:00
|
|
|
extent_size_quantize_t *extent_size_quantize_ceil =
|
|
|
|
JEMALLOC_N(n_extent_size_quantize_ceil);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Generate pairing heap functions. */
|
2016-11-16 05:07:53 +08:00
|
|
|
ph_gen(, extent_heap_, extent_heap_t, extent_t, ph_link, extent_snad_comp)
|
2016-06-02 03:10:39 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
bool
|
|
|
|
extents_init(tsdn_t *tsdn, extents_t *extents, extent_state_t state) {
|
|
|
|
if (malloc_mutex_init(&extents->mtx, "extents", WITNESS_RANK_EXTENTS)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < NPSIZES+1; i++) {
|
|
|
|
extent_heap_new(&extents->heaps[i]);
|
|
|
|
}
|
|
|
|
extent_list_init(&extents->lru);
|
|
|
|
extents->npages = 0;
|
|
|
|
extents->state = state;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
extent_state_t
|
|
|
|
extents_state_get(const extents_t *extents) {
|
|
|
|
return extents->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
extents_npages_get(extents_t *extents) {
|
|
|
|
return atomic_read_zu(&extents->npages);
|
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
static void
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_insert_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent) {
|
|
|
|
malloc_mutex_assert_owner(tsdn, &extents->mtx);
|
|
|
|
assert(extent_state_get(extent) == extents->state);
|
|
|
|
|
|
|
|
size_t size = extent_size_get(extent);
|
|
|
|
size_t psz = extent_size_quantize_floor(size);
|
2016-11-04 12:18:50 +08:00
|
|
|
pszind_t pind = psz2ind(psz);
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_heap_insert(&extents->heaps[pind], extent);
|
|
|
|
extent_list_append(&extents->lru, extent);
|
|
|
|
size_t npages = size >> LG_PAGE;
|
|
|
|
atomic_add_zu(&extents->npages, npages);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
extents_remove_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent) {
|
|
|
|
malloc_mutex_assert_owner(tsdn, &extents->mtx);
|
|
|
|
assert(extent_state_get(extent) == extents->state);
|
|
|
|
|
|
|
|
size_t size = extent_size_get(extent);
|
|
|
|
size_t psz = extent_size_quantize_floor(size);
|
|
|
|
pszind_t pind = psz2ind(psz);
|
|
|
|
extent_heap_remove(&extents->heaps[pind], extent);
|
|
|
|
extent_list_remove(&extents->lru, extent);
|
|
|
|
size_t npages = size >> LG_PAGE;
|
|
|
|
assert(atomic_read_zu(&extents->npages) >= npages);
|
|
|
|
atomic_sub_zu(&extents->npages, size >> LG_PAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do first-best-fit extent selection, i.e. select the oldest/lowest extent that
|
|
|
|
* best fits.
|
|
|
|
*/
|
|
|
|
static extent_t *
|
|
|
|
extents_first_best_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,
|
|
|
|
size_t size) {
|
|
|
|
malloc_mutex_assert_owner(tsdn, &extents->mtx);
|
|
|
|
|
|
|
|
pszind_t pind = psz2ind(extent_size_quantize_ceil(size));
|
|
|
|
for (pszind_t i = pind; i < NPSIZES+1; i++) {
|
|
|
|
extent_t *extent = extent_heap_first(&extents->heaps[i]);
|
|
|
|
if (extent != NULL) {
|
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extent_t *
|
|
|
|
extents_evict(tsdn_t *tsdn, extents_t *extents, size_t npages_min) {
|
|
|
|
malloc_mutex_lock(tsdn, &extents->mtx);
|
|
|
|
|
|
|
|
/* Get the LRU extent, if any. */
|
|
|
|
extent_t *extent = extent_list_first(&extents->lru);
|
|
|
|
if (extent == NULL) {
|
|
|
|
goto label_return;
|
|
|
|
}
|
|
|
|
/* Check the eviction limit. */
|
|
|
|
size_t npages = extent_size_get(extent) >> LG_PAGE;
|
|
|
|
if (atomic_read_zu(&extents->npages) - npages < npages_min) {
|
|
|
|
extent = NULL;
|
|
|
|
goto label_return;
|
|
|
|
}
|
|
|
|
extents_remove_locked(tsdn, extents, extent);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either mark the extent active or deregister it to protect against
|
|
|
|
* concurrent operations.
|
|
|
|
*/
|
|
|
|
switch (extents_state_get(extents)) {
|
|
|
|
case extent_state_dirty:
|
|
|
|
extent_state_set(extent, extent_state_active);
|
|
|
|
break;
|
|
|
|
case extent_state_retained:
|
|
|
|
extent_deregister(tsdn, extent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
label_return:
|
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
extents_leak(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,
|
|
|
|
extents_t *extents, extent_t *extent) {
|
|
|
|
/*
|
|
|
|
* Leak extent after making sure its pages have already been purged, so
|
|
|
|
* that this is only a virtual memory leak.
|
|
|
|
*/
|
|
|
|
if (extents_state_get(extents) == extent_state_dirty) {
|
|
|
|
if (extent_purge_lazy_wrapper(tsdn, arena, r_extent_hooks,
|
|
|
|
extent, 0, extent_size_get(extent))) {
|
|
|
|
extent_purge_forced_wrapper(tsdn, arena, r_extent_hooks,
|
|
|
|
extent, 0, extent_size_get(extent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extent_dalloc(tsdn, arena, extent);
|
|
|
|
}
|
2016-09-23 02:57:28 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
void
|
|
|
|
extents_prefork(tsdn_t *tsdn, extents_t *extents) {
|
|
|
|
malloc_mutex_prefork(tsdn, &extents->mtx);
|
|
|
|
}
|
2016-09-23 02:57:28 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
void
|
|
|
|
extents_postfork_parent(tsdn_t *tsdn, extents_t *extents) {
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &extents->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
extents_postfork_child(tsdn_t *tsdn, extents_t *extents) {
|
|
|
|
malloc_mutex_postfork_child(tsdn, &extents->mtx);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_deactivate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_t *extent) {
|
2017-01-30 13:57:14 +08:00
|
|
|
assert(extent_arena_get(extent) == arena);
|
|
|
|
assert(extent_state_get(extent) == extent_state_active);
|
2016-09-23 02:57:28 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_state_set(extent, extents_state_get(extents));
|
|
|
|
extents_insert_locked(tsdn, extents, extent);
|
|
|
|
}
|
2016-09-23 02:57:28 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
static void
|
|
|
|
extent_deactivate(tsdn_t *tsdn, arena_t *arena, extents_t *extents,
|
|
|
|
extent_t *extent) {
|
|
|
|
malloc_mutex_lock(tsdn, &extents->mtx);
|
|
|
|
extent_deactivate_locked(tsdn, arena, extents, extent);
|
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
extent_activate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,
|
|
|
|
extent_t *extent) {
|
|
|
|
assert(extent_arena_get(extent) == arena);
|
|
|
|
assert(extent_state_get(extent) == extents_state_get(extents));
|
|
|
|
|
|
|
|
extents_remove_locked(tsdn, extents, extent);
|
|
|
|
extent_state_set(extent, extent_state_active);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_rtree_acquire(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,
|
|
|
|
const extent_t *extent, bool dependent, bool init_missing,
|
2017-01-16 08:56:30 +08:00
|
|
|
rtree_elm_t **r_elm_a, rtree_elm_t **r_elm_b) {
|
2016-06-03 09:43:10 +08:00
|
|
|
*r_elm_a = rtree_elm_acquire(tsdn, &extents_rtree, rtree_ctx,
|
2016-06-02 03:59:02 +08:00
|
|
|
(uintptr_t)extent_base_get(extent), dependent, init_missing);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!dependent && *r_elm_a == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
assert(*r_elm_a != NULL);
|
|
|
|
|
|
|
|
if (extent_size_get(extent) > PAGE) {
|
2016-06-03 09:43:10 +08:00
|
|
|
*r_elm_b = rtree_elm_acquire(tsdn, &extents_rtree, rtree_ctx,
|
2016-06-02 03:59:02 +08:00
|
|
|
(uintptr_t)extent_last_get(extent), dependent,
|
|
|
|
init_missing);
|
2016-11-01 07:32:33 +08:00
|
|
|
if (!dependent && *r_elm_b == NULL) {
|
|
|
|
rtree_elm_release(tsdn, &extents_rtree, *r_elm_a);
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2016-11-01 07:32:33 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
assert(*r_elm_b != NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-06-02 03:59:02 +08:00
|
|
|
*r_elm_b = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
extent_rtree_write_acquired(tsdn_t *tsdn, rtree_elm_t *elm_a,
|
2017-01-16 08:56:30 +08:00
|
|
|
rtree_elm_t *elm_b, const extent_t *extent) {
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_write_acquired(tsdn, &extents_rtree, elm_a, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (elm_b != NULL) {
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_write_acquired(tsdn, &extents_rtree, elm_b, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_rtree_release(tsdn_t *tsdn, rtree_elm_t *elm_a, rtree_elm_t *elm_b) {
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_release(tsdn, &extents_rtree, elm_a);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (elm_b != NULL) {
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_release(tsdn, &extents_rtree, elm_b);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_interior_register(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,
|
2017-01-16 08:56:30 +08:00
|
|
|
const extent_t *extent) {
|
2016-06-02 03:59:02 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(extent_slab_get(extent));
|
|
|
|
|
|
|
|
for (i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_write(tsdn, &extents_rtree, rtree_ctx,
|
2016-06-02 03:59:02 +08:00
|
|
|
(uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<
|
|
|
|
LG_PAGE), extent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 15:23:03 +08:00
|
|
|
static void
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_add(tsdn_t *tsdn, const extent_t *extent) {
|
2016-11-22 15:23:03 +08:00
|
|
|
cassert(config_prof);
|
2017-01-30 13:57:14 +08:00
|
|
|
/* prof_gdump() requirement. */
|
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
if (opt_prof && extent_state_get(extent) == extent_state_active) {
|
2016-06-06 05:43:20 +08:00
|
|
|
size_t nadd = extent_size_get(extent) >> LG_PAGE;
|
2016-11-08 03:27:48 +08:00
|
|
|
size_t cur = atomic_add_zu(&curpages, nadd);
|
|
|
|
size_t high = atomic_read_zu(&highpages);
|
|
|
|
while (cur > high && atomic_cas_zu(&highpages, high, cur)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
/*
|
|
|
|
* Don't refresh cur, because it may have decreased
|
2016-06-06 05:43:20 +08:00
|
|
|
* since this thread lost the highpages update race.
|
2016-06-02 03:59:02 +08:00
|
|
|
*/
|
2016-11-08 03:27:48 +08:00
|
|
|
high = atomic_read_zu(&highpages);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (cur > high && prof_gdump_get_unlocked()) {
|
2016-06-02 03:59:02 +08:00
|
|
|
prof_gdump(tsdn);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_sub(tsdn_t *tsdn, const extent_t *extent) {
|
2016-11-22 15:23:03 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
if (opt_prof && extent_state_get(extent) == extent_state_active) {
|
2016-11-22 15:23:03 +08:00
|
|
|
size_t nsub = extent_size_get(extent) >> LG_PAGE;
|
|
|
|
assert(atomic_read_zu(&curpages) >= nsub);
|
|
|
|
atomic_sub_zu(&curpages, nsub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-02-04 16:43:32 +08:00
|
|
|
extent_register_impl(tsdn_t *tsdn, const extent_t *extent, bool gdump_add) {
|
2016-11-22 15:23:03 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
|
|
|
rtree_elm_t *elm_a, *elm_b;
|
|
|
|
|
|
|
|
if (extent_rtree_acquire(tsdn, rtree_ctx, extent, false, true, &elm_a,
|
2017-01-16 08:56:30 +08:00
|
|
|
&elm_b)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_rtree_write_acquired(tsdn, elm_a, elm_b, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent_slab_get(extent)) {
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_interior_register(tsdn, rtree_ctx, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_rtree_release(tsdn, elm_a, elm_b);
|
|
|
|
|
2017-02-04 16:43:32 +08:00
|
|
|
if (config_prof && gdump_add) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_add(tsdn, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-04 16:43:32 +08:00
|
|
|
static bool
|
|
|
|
extent_register(tsdn_t *tsdn, const extent_t *extent) {
|
|
|
|
return extent_register_impl(tsdn, extent, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
extent_register_no_gdump_add(tsdn_t *tsdn, const extent_t *extent) {
|
|
|
|
return extent_register_impl(tsdn, extent, false);
|
|
|
|
}
|
|
|
|
|
2016-06-06 11:39:25 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_reregister(tsdn_t *tsdn, const extent_t *extent) {
|
2016-06-06 11:39:25 +08:00
|
|
|
bool err = extent_register(tsdn, extent);
|
|
|
|
assert(!err);
|
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
static void
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_interior_deregister(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,
|
2017-01-16 08:56:30 +08:00
|
|
|
const extent_t *extent) {
|
2016-06-02 03:59:02 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(extent_slab_get(extent));
|
|
|
|
|
|
|
|
for (i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_clear(tsdn, &extents_rtree, rtree_ctx,
|
2016-06-02 03:59:02 +08:00
|
|
|
(uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<
|
|
|
|
LG_PAGE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_deregister(tsdn_t *tsdn, extent_t *extent) {
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_t *elm_a, *elm_b;
|
|
|
|
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_rtree_acquire(tsdn, rtree_ctx, extent, true, false, &elm_a,
|
|
|
|
&elm_b);
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_rtree_write_acquired(tsdn, elm_a, elm_b, NULL);
|
2016-06-06 11:39:25 +08:00
|
|
|
if (extent_slab_get(extent)) {
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_interior_deregister(tsdn, rtree_ctx, extent);
|
2016-06-06 11:39:25 +08:00
|
|
|
extent_slab_set(extent, false);
|
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_rtree_release(tsdn, elm_a, elm_b);
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_prof) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_sub(tsdn, extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static extent_t *
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_recycle_extract(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,
|
|
|
|
bool locked, void *new_addr, size_t usize, size_t pad, size_t alignment,
|
|
|
|
bool *zero, bool *commit) {
|
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, locked ? 1 : 0);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (locked) {
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, &extents->mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-04 14:49:21 +08:00
|
|
|
assert(alignment > 0);
|
2016-09-24 03:11:01 +08:00
|
|
|
if (config_debug && new_addr != NULL) {
|
|
|
|
/*
|
|
|
|
* Non-NULL new_addr has two use cases:
|
|
|
|
*
|
|
|
|
* 1) Recycle a known-extant extent, e.g. during purging.
|
|
|
|
* 2) Perform in-place expanding reallocation.
|
|
|
|
*
|
|
|
|
* Regardless of use case, new_addr must either refer to a
|
|
|
|
* non-existing extent, or to the base of an extant extent,
|
|
|
|
* since only active slabs support interior lookups (which of
|
|
|
|
* course cannot be recycled).
|
|
|
|
*/
|
|
|
|
assert(PAGE_ADDR2BASE(new_addr) == new_addr);
|
2016-10-04 05:18:55 +08:00
|
|
|
assert(pad == 0);
|
|
|
|
assert(alignment <= PAGE);
|
2016-09-24 03:11:01 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
size_t size = usize + pad;
|
|
|
|
size_t alloc_size = size + PAGE_CEILING(alignment) - PAGE;
|
2016-11-04 14:49:21 +08:00
|
|
|
/* Beware size_t wrap-around. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (alloc_size < usize) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (!locked) {
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_lock(tsdn, &extents->mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_t *extent;
|
2016-06-02 03:59:02 +08:00
|
|
|
if (new_addr != NULL) {
|
|
|
|
rtree_elm_t *elm;
|
|
|
|
|
2016-06-03 09:43:10 +08:00
|
|
|
elm = rtree_elm_acquire(tsdn, &extents_rtree, rtree_ctx,
|
2016-06-02 03:59:02 +08:00
|
|
|
(uintptr_t)new_addr, false, false);
|
|
|
|
if (elm != NULL) {
|
|
|
|
extent = rtree_elm_read_acquired(tsdn, &extents_rtree,
|
|
|
|
elm);
|
2016-09-24 03:11:01 +08:00
|
|
|
if (extent != NULL) {
|
|
|
|
assert(extent_base_get(extent) == new_addr);
|
|
|
|
if (extent_arena_get(extent) != arena ||
|
|
|
|
extent_size_get(extent) < size ||
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_state_get(extent) !=
|
|
|
|
extents_state_get(extents)) {
|
2016-09-24 03:11:01 +08:00
|
|
|
extent = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-09-24 03:11:01 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_release(tsdn, &extents_rtree, elm);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-09-24 03:18:36 +08:00
|
|
|
} else {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent = extents_first_best_fit_locked(tsdn, arena, extents,
|
2016-09-24 03:18:36 +08:00
|
|
|
alloc_size);
|
|
|
|
}
|
2016-09-24 03:11:01 +08:00
|
|
|
if (extent == NULL) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!locked) {
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_activate_locked(tsdn, arena, extents, extent);
|
|
|
|
if (!locked) {
|
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent_zeroed_get(extent)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
*zero = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (extent_committed_get(extent)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
*commit = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static extent_t *
|
|
|
|
extent_recycle_split(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,
|
|
|
|
void *new_addr, size_t usize, size_t pad, size_t alignment,
|
|
|
|
extent_t *extent) {
|
|
|
|
size_t size = usize + pad;
|
|
|
|
size_t leadsize = ALIGNMENT_CEILING((uintptr_t)extent_base_get(extent),
|
|
|
|
PAGE_CEILING(alignment)) - (uintptr_t)extent_base_get(extent);
|
|
|
|
assert(new_addr == NULL || leadsize == 0);
|
|
|
|
assert(extent_size_get(extent) >= leadsize + size);
|
|
|
|
size_t trailsize = extent_size_get(extent) - leadsize - size;
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
/* Split the lead. */
|
|
|
|
if (leadsize != 0) {
|
|
|
|
extent_t *lead = extent;
|
2016-06-04 03:05:53 +08:00
|
|
|
extent = extent_split_wrapper(tsdn, arena, r_extent_hooks,
|
|
|
|
lead, leadsize, leadsize, size + trailsize, usize +
|
|
|
|
trailsize);
|
2016-06-02 03:59:02 +08:00
|
|
|
if (extent == NULL) {
|
2016-06-06 13:08:20 +08:00
|
|
|
extent_deregister(tsdn, lead);
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, r_extent_hooks, extents,
|
|
|
|
lead);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_deactivate(tsdn, arena, extents, lead);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Split the trail. */
|
|
|
|
if (trailsize != 0) {
|
|
|
|
extent_t *trail = extent_split_wrapper(tsdn, arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
r_extent_hooks, extent, size, usize, trailsize, trailsize);
|
2016-06-02 03:59:02 +08:00
|
|
|
if (trail == NULL) {
|
2016-06-06 13:08:20 +08:00
|
|
|
extent_deregister(tsdn, extent);
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, r_extent_hooks, extents,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_deactivate(tsdn, arena, extents, trail);
|
2016-06-02 03:59:02 +08:00
|
|
|
} else if (leadsize == 0) {
|
|
|
|
/*
|
|
|
|
* Splitting causes usize to be set as a side effect, but no
|
|
|
|
* splitting occurred.
|
|
|
|
*/
|
|
|
|
extent_usize_set(extent, usize);
|
|
|
|
}
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static extent_t *
|
|
|
|
extent_recycle(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,
|
|
|
|
extents_t *extents, void *new_addr, size_t usize, size_t pad,
|
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
assert(new_addr == NULL || !slab);
|
|
|
|
assert(pad == 0 || !slab);
|
|
|
|
|
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
|
|
|
|
|
|
|
extent_t *extent = extent_recycle_extract(tsdn, arena, r_extent_hooks,
|
|
|
|
rtree_ctx, extents, false, new_addr, usize, pad, alignment, zero,
|
|
|
|
commit);
|
|
|
|
if (extent == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extent = extent_recycle_split(tsdn, arena, r_extent_hooks, rtree_ctx,
|
|
|
|
extents, new_addr, usize, pad, alignment, extent);
|
|
|
|
if (extent == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-22 15:23:03 +08:00
|
|
|
if (*commit && !extent_committed_get(extent)) {
|
|
|
|
if (extent_commit_wrapper(tsdn, arena, r_extent_hooks, extent,
|
|
|
|
0, extent_size_get(extent))) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks, extents,
|
|
|
|
extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
extent_zeroed_set(extent, true);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (pad != 0) {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_addr_randomize(tsdn, extent, alignment);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
assert(extent_state_get(extent) == extent_state_active);
|
2016-06-02 03:59:02 +08:00
|
|
|
if (slab) {
|
|
|
|
extent_slab_set(extent, slab);
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_interior_register(tsdn, rtree_ctx, extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*zero) {
|
|
|
|
if (!extent_zeroed_get(extent)) {
|
|
|
|
memset(extent_addr_get(extent), 0,
|
|
|
|
extent_usize_get(extent));
|
|
|
|
} else if (config_debug) {
|
|
|
|
size_t i;
|
|
|
|
size_t *p = (size_t *)(uintptr_t)
|
|
|
|
extent_addr_get(extent);
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < usize / sizeof(size_t); i++) {
|
2016-06-02 03:59:02 +08:00
|
|
|
assert(p[i] == 0);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the caller specifies (!*zero), it is still possible to receive zeroed
|
|
|
|
* memory, in which case *zero is toggled to true. arena_extent_alloc() takes
|
|
|
|
* advantage of this to avoid demanding zeroed extents, but taking advantage of
|
|
|
|
* them if they are returned.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
extent_alloc_core(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, dss_prec_t dss_prec) {
|
2016-06-02 03:59:02 +08:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
assert(alignment != 0);
|
|
|
|
|
|
|
|
/* "primary" dss. */
|
|
|
|
if (have_dss && dss_prec == dss_prec_primary && (ret =
|
|
|
|
extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,
|
2017-01-16 08:56:30 +08:00
|
|
|
commit)) != NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
/* mmap. */
|
|
|
|
if ((ret = extent_alloc_mmap(new_addr, size, alignment, zero, commit))
|
2017-01-16 08:56:30 +08:00
|
|
|
!= NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
/* "secondary" dss. */
|
|
|
|
if (have_dss && dss_prec == dss_prec_secondary && (ret =
|
|
|
|
extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,
|
2017-01-16 08:56:30 +08:00
|
|
|
commit)) != NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
/* All strategies for allocation failed. */
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 02:57:28 +08:00
|
|
|
extent_t *
|
|
|
|
extent_alloc_cache(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, void *new_addr, size_t usize, size_t pad,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
2017-01-30 13:57:14 +08:00
|
|
|
assert(usize + pad != 0);
|
|
|
|
assert(alignment != 0);
|
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
|
|
|
return extent_recycle(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_cached, new_addr, usize, pad, alignment, zero,
|
|
|
|
commit, slab);
|
2016-09-23 02:57:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 04:37:22 +08:00
|
|
|
static void *
|
|
|
|
extent_alloc_default_impl(tsdn_t *tsdn, arena_t *arena, void *new_addr,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t size, size_t alignment, bool *zero, bool *commit) {
|
2016-06-08 04:37:22 +08:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = extent_alloc_core(tsdn, arena, new_addr, size, alignment, zero,
|
|
|
|
commit, arena->dss_prec);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
static void *
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_alloc_default(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {
|
2016-06-02 03:59:02 +08:00
|
|
|
tsdn_t *tsdn;
|
|
|
|
arena_t *arena;
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
tsdn = tsdn_fetch();
|
|
|
|
arena = arena_get(tsdn, arena_ind, false);
|
|
|
|
/*
|
|
|
|
* The arena we're allocating on behalf of must have been initialized
|
|
|
|
* already.
|
|
|
|
*/
|
|
|
|
assert(arena != NULL);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent_alloc_default_impl(tsdn, arena, new_addr, size,
|
|
|
|
alignment, zero, commit);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 15:23:03 +08:00
|
|
|
/*
|
|
|
|
* If virtual memory is retained, create increasingly larger extents from which
|
|
|
|
* to split requested extents in order to limit the total number of disjoint
|
|
|
|
* virtual memory ranges retained by each arena.
|
|
|
|
*/
|
|
|
|
static extent_t *
|
|
|
|
extent_grow_retained(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, void *new_addr, size_t usize, size_t pad,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_t *extent;
|
|
|
|
void *ptr;
|
|
|
|
size_t size, alloc_size, alloc_size_min, leadsize, trailsize;
|
|
|
|
bool zeroed, committed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the next extent size in the series would be large
|
|
|
|
* enough to satisfy this request. If no, just bail, so that e.g. a
|
|
|
|
* series of unsatisfiable allocation requests doesn't cause unused
|
|
|
|
* extent creation as a side effect.
|
|
|
|
*/
|
|
|
|
size = usize + pad;
|
2017-01-28 07:03:11 +08:00
|
|
|
alloc_size = pind2sz(atomic_read_u(&arena->extent_grow_next));
|
2016-11-22 15:23:03 +08:00
|
|
|
alloc_size_min = size + PAGE_CEILING(alignment) - PAGE;
|
|
|
|
/* Beware size_t wrap-around. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (alloc_size_min < usize) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (alloc_size < alloc_size_min) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
extent = extent_alloc(tsdn, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
zeroed = false;
|
|
|
|
committed = false;
|
|
|
|
ptr = extent_alloc_core(tsdn, arena, new_addr, alloc_size, PAGE,
|
|
|
|
&zeroed, &committed, arena->dss_prec);
|
|
|
|
extent_init(extent, arena, ptr, alloc_size, alloc_size,
|
2017-02-04 16:43:32 +08:00
|
|
|
arena_extent_sn_next(arena), extent_state_active, zeroed,
|
2017-01-30 13:57:14 +08:00
|
|
|
committed, false);
|
2017-02-04 16:43:32 +08:00
|
|
|
if (ptr == NULL || extent_register_no_gdump_add(tsdn, extent)) {
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_dalloc(tsdn, arena, extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
leadsize = ALIGNMENT_CEILING((uintptr_t)ptr, PAGE_CEILING(alignment)) -
|
|
|
|
(uintptr_t)ptr;
|
|
|
|
assert(new_addr == NULL || leadsize == 0);
|
|
|
|
assert(alloc_size >= leadsize + size);
|
|
|
|
trailsize = alloc_size - leadsize - size;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent_zeroed_get(extent)) {
|
2016-11-22 15:23:03 +08:00
|
|
|
*zero = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (extent_committed_get(extent)) {
|
2016-11-22 15:23:03 +08:00
|
|
|
*commit = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
|
|
|
|
/* Split the lead. */
|
|
|
|
if (leadsize != 0) {
|
|
|
|
extent_t *lead = extent;
|
|
|
|
extent = extent_split_wrapper(tsdn, arena, r_extent_hooks, lead,
|
|
|
|
leadsize, leadsize, size + trailsize, usize + trailsize);
|
|
|
|
if (extent == NULL) {
|
|
|
|
extent_deregister(tsdn, lead);
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, r_extent_hooks, false, lead);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_retained, lead);
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Split the trail. */
|
|
|
|
if (trailsize != 0) {
|
|
|
|
extent_t *trail = extent_split_wrapper(tsdn, arena,
|
|
|
|
r_extent_hooks, extent, size, usize, trailsize, trailsize);
|
|
|
|
if (trail == NULL) {
|
|
|
|
extent_deregister(tsdn, extent);
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_retained, extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_retained, trail);
|
2016-11-22 15:23:03 +08:00
|
|
|
} else if (leadsize == 0) {
|
|
|
|
/*
|
|
|
|
* Splitting causes usize to be set as a side effect, but no
|
|
|
|
* splitting occurred.
|
|
|
|
*/
|
|
|
|
extent_usize_set(extent, usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*commit && !extent_committed_get(extent)) {
|
|
|
|
if (extent_commit_wrapper(tsdn, arena, r_extent_hooks, extent,
|
|
|
|
0, extent_size_get(extent))) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_retained, extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
extent_zeroed_set(extent, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config_prof) {
|
2017-01-30 13:57:14 +08:00
|
|
|
/* Adjust gdump stats now that extent is final size. */
|
|
|
|
extent_gdump_add(tsdn, extent);
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (pad != 0) {
|
2016-11-22 15:23:03 +08:00
|
|
|
extent_addr_randomize(tsdn, extent, alignment);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
if (slab) {
|
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,
|
|
|
|
&rtree_ctx_fallback);
|
|
|
|
|
|
|
|
extent_slab_set(extent, true);
|
|
|
|
extent_interior_register(tsdn, rtree_ctx, extent);
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (*zero && !extent_zeroed_get(extent)) {
|
2016-11-22 15:23:03 +08:00
|
|
|
memset(extent_addr_get(extent), 0, extent_usize_get(extent));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-28 07:03:11 +08:00
|
|
|
/*
|
|
|
|
* Increment extent_grow_next, but take care to do so atomically and
|
|
|
|
* bail out if the increment would exceed the legal range.
|
|
|
|
*/
|
|
|
|
while (true) {
|
|
|
|
pszind_t egn = atomic_read_u(&arena->extent_grow_next);
|
|
|
|
|
|
|
|
if (egn + 1 == NPSIZES) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(egn + 1 < NPSIZES);
|
|
|
|
if (!atomic_cas_u(&arena->extent_grow_next, egn, egn + 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
static extent_t *
|
|
|
|
extent_alloc_retained(tsdn_t *tsdn, arena_t *arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, void *new_addr, size_t usize, size_t pad,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_t *extent;
|
|
|
|
|
|
|
|
assert(usize != 0);
|
|
|
|
assert(alignment != 0);
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent = extent_recycle(tsdn, arena, r_extent_hooks,
|
2017-01-30 13:57:14 +08:00
|
|
|
&arena->extents_retained, new_addr, usize, pad, alignment, zero,
|
|
|
|
commit, slab);
|
2017-01-19 15:03:37 +08:00
|
|
|
if (extent != NULL) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_prof) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_add(tsdn, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-22 15:23:03 +08:00
|
|
|
}
|
|
|
|
if (!config_munmap && extent == NULL) {
|
|
|
|
extent = extent_grow_retained(tsdn, arena, r_extent_hooks,
|
|
|
|
new_addr, usize, pad, alignment, zero, commit, slab);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static extent_t *
|
|
|
|
extent_alloc_wrapper_hard(tsdn_t *tsdn, arena_t *arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, void *new_addr, size_t usize, size_t pad,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_t *extent;
|
|
|
|
size_t size;
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
size = usize + pad;
|
|
|
|
extent = extent_alloc(tsdn, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-08 04:37:22 +08:00
|
|
|
if (*r_extent_hooks == &extent_hooks_default) {
|
|
|
|
/* Call directly to propagate tsdn. */
|
|
|
|
addr = extent_alloc_default_impl(tsdn, arena, new_addr, size,
|
|
|
|
alignment, zero, commit);
|
|
|
|
} else {
|
|
|
|
addr = (*r_extent_hooks)->alloc(*r_extent_hooks, new_addr, size,
|
2016-12-23 06:39:10 +08:00
|
|
|
alignment, zero, commit, arena_ind_get(arena));
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
if (addr == NULL) {
|
|
|
|
extent_dalloc(tsdn, arena, extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2016-11-16 05:07:53 +08:00
|
|
|
extent_init(extent, arena, addr, size, usize,
|
2017-01-30 13:57:14 +08:00
|
|
|
arena_extent_sn_next(arena), extent_state_active, zero, commit,
|
|
|
|
slab);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (pad != 0) {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_addr_randomize(tsdn, extent, alignment);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
if (extent_register(tsdn, extent)) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, r_extent_hooks,
|
|
|
|
&arena->extents_retained, extent);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extent_t *
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_alloc_wrapper(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, void *new_addr, size_t usize, size_t pad,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit, bool slab) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_t *extent = extent_alloc_retained(tsdn, arena, r_extent_hooks,
|
|
|
|
new_addr, usize, pad, alignment, zero, commit, slab);
|
2016-06-02 03:59:02 +08:00
|
|
|
if (extent == NULL) {
|
2016-06-04 03:05:53 +08:00
|
|
|
extent = extent_alloc_wrapper_hard(tsdn, arena, r_extent_hooks,
|
2016-06-02 03:59:02 +08:00
|
|
|
new_addr, usize, pad, alignment, zero, commit, slab);
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_can_coalesce(arena_t *arena, extents_t *extents, const extent_t *inner,
|
|
|
|
const extent_t *outer) {
|
|
|
|
assert(extent_arena_get(inner) == arena);
|
|
|
|
if (extent_arena_get(outer) != arena) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-02-13 15:00:19 +08:00
|
|
|
|
|
|
|
assert(extent_state_get(inner) == extent_state_active);
|
|
|
|
if (extent_state_get(outer) != extents->state) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-02-13 15:00:19 +08:00
|
|
|
|
|
|
|
if (extent_committed_get(inner) != extent_committed_get(outer)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
static bool
|
2017-02-06 15:59:53 +08:00
|
|
|
extent_coalesce(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,
|
2017-02-13 15:00:19 +08:00
|
|
|
extents_t *extents, extent_t *inner, extent_t *outer, bool forward) {
|
|
|
|
assert(extent_can_coalesce(arena, extents, inner, outer));
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_activate_locked(tsdn, arena, extents, outer);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
2017-02-13 15:00:19 +08:00
|
|
|
bool err = extent_merge_wrapper(tsdn, arena, r_extent_hooks,
|
|
|
|
forward ? inner : outer, forward ? outer : inner);
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_lock(tsdn, &extents->mtx);
|
2017-02-13 15:00:19 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
if (err) {
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_deactivate_locked(tsdn, arena, extents, outer);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 15:00:19 +08:00
|
|
|
return err;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_record(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_t *extents, extent_t *extent) {
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
assert(extents_state_get(extents) != extent_state_dirty ||
|
|
|
|
!extent_zeroed_get(extent));
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_lock(tsdn, &extents->mtx);
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_usize_set(extent, 0);
|
|
|
|
if (extent_slab_get(extent)) {
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_interior_deregister(tsdn, rtree_ctx, extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_slab_set(extent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(extent_lookup(tsdn, extent_base_get(extent), true) == extent);
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
/*
|
|
|
|
* Continue attempting to coalesce until failure, to protect against
|
|
|
|
* races with other threads that are thwarted by this one.
|
|
|
|
*/
|
|
|
|
bool coalesced;
|
|
|
|
do {
|
|
|
|
coalesced = false;
|
|
|
|
|
|
|
|
/* Try to coalesce forward. */
|
2017-02-06 15:59:53 +08:00
|
|
|
rtree_elm_t *next_elm = rtree_elm_acquire(tsdn, &extents_rtree,
|
|
|
|
rtree_ctx, (uintptr_t)extent_past_get(extent), false,
|
|
|
|
false);
|
|
|
|
if (next_elm != NULL) {
|
|
|
|
extent_t *next = rtree_elm_read_acquired(tsdn,
|
|
|
|
&extents_rtree, next_elm);
|
|
|
|
/*
|
|
|
|
* extents->mtx only protects against races for
|
|
|
|
* like-state extents, so call extent_can_coalesce()
|
|
|
|
* before releasing the next_elm lock.
|
|
|
|
*/
|
|
|
|
bool can_coalesce = (next != NULL &&
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_can_coalesce(arena, extents, extent, next));
|
2017-02-06 15:59:53 +08:00
|
|
|
rtree_elm_release(tsdn, &extents_rtree, next_elm);
|
|
|
|
if (can_coalesce && !extent_coalesce(tsdn, arena,
|
2017-02-13 15:00:19 +08:00
|
|
|
r_extent_hooks, extents, extent, next, true)) {
|
2017-02-06 15:59:53 +08:00
|
|
|
coalesced = true;
|
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
/* Try to coalesce backward. */
|
2017-02-06 15:59:53 +08:00
|
|
|
rtree_elm_t *prev_elm = rtree_elm_acquire(tsdn, &extents_rtree,
|
|
|
|
rtree_ctx, (uintptr_t)extent_before_get(extent), false,
|
|
|
|
false);
|
|
|
|
if (prev_elm != NULL) {
|
|
|
|
extent_t *prev = rtree_elm_read_acquired(tsdn,
|
|
|
|
&extents_rtree, prev_elm);
|
|
|
|
bool can_coalesce = (prev != NULL &&
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_can_coalesce(arena, extents, extent, prev));
|
2017-02-06 15:59:53 +08:00
|
|
|
rtree_elm_release(tsdn, &extents_rtree, prev_elm);
|
|
|
|
if (can_coalesce && !extent_coalesce(tsdn, arena,
|
2017-02-13 15:00:19 +08:00
|
|
|
r_extent_hooks, extents, extent, prev, false)) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent = prev;
|
2017-02-06 15:59:53 +08:00
|
|
|
coalesced = true;
|
2017-01-30 13:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (coalesced);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-02-13 15:00:19 +08:00
|
|
|
extent_deactivate_locked(tsdn, arena, extents, extent);
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &extents->mtx);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-06 06:27:20 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dalloc_gap(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {
|
2016-06-06 06:27:20 +08:00
|
|
|
extent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
2016-06-06 06:27:20 +08:00
|
|
|
if (extent_register(tsdn, extent)) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extents_leak(tsdn, arena, &extent_hooks,
|
|
|
|
&arena->extents_retained, extent);
|
2016-06-06 06:27:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
extent_dalloc_wrapper(tsdn, arena, &extent_hooks, extent);
|
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
void
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_dalloc_cache(tsdn_t *tsdn, arena_t *arena,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent) {
|
2016-06-02 03:59:02 +08:00
|
|
|
assert(extent_base_get(extent) != NULL);
|
|
|
|
assert(extent_size_get(extent) != 0);
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_addr_set(extent, extent_base_get(extent));
|
|
|
|
extent_zeroed_set(extent, false);
|
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks, &arena->extents_cached,
|
2016-06-02 03:59:02 +08:00
|
|
|
extent);
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:37:22 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_dalloc_default_impl(void *addr, size_t size) {
|
|
|
|
if (!have_dss || !extent_in_dss(addr)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent_dalloc_mmap(addr, size);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
static bool
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_dalloc_default(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool committed, unsigned arena_ind) {
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent_dalloc_default_impl(addr, size);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 09:21:59 +08:00
|
|
|
bool
|
|
|
|
extent_dalloc_wrapper_try(tsdn_t *tsdn, arena_t *arena,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent) {
|
2017-01-04 09:21:59 +08:00
|
|
|
bool err;
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
assert(extent_base_get(extent) != NULL);
|
|
|
|
assert(extent_size_get(extent) != 0);
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_addr_set(extent, extent_base_get(extent));
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2017-01-30 13:57:14 +08:00
|
|
|
/* Try to deallocate. */
|
2016-06-08 04:37:22 +08:00
|
|
|
if (*r_extent_hooks == &extent_hooks_default) {
|
|
|
|
/* Call directly to propagate tsdn. */
|
2016-10-14 03:18:38 +08:00
|
|
|
err = extent_dalloc_default_impl(extent_base_get(extent),
|
2016-06-08 04:37:22 +08:00
|
|
|
extent_size_get(extent));
|
|
|
|
} else {
|
2016-12-04 07:38:25 +08:00
|
|
|
err = ((*r_extent_hooks)->dalloc == NULL ||
|
|
|
|
(*r_extent_hooks)->dalloc(*r_extent_hooks,
|
2016-06-08 04:37:22 +08:00
|
|
|
extent_base_get(extent), extent_size_get(extent),
|
2016-12-23 06:39:10 +08:00
|
|
|
extent_committed_get(extent), arena_ind_get(arena)));
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!err) {
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_dalloc(tsdn, arena, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-04 09:21:59 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return err;
|
2017-01-04 09:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
extent_dalloc_wrapper(tsdn_t *tsdn, arena_t *arena,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2017-01-04 09:21:59 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
/*
|
|
|
|
* Deregister first to avoid a race with other allocating threads, and
|
|
|
|
* reregister if deallocation fails.
|
|
|
|
*/
|
|
|
|
extent_deregister(tsdn, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!extent_dalloc_wrapper_try(tsdn, arena, r_extent_hooks, extent)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-04 09:21:59 +08:00
|
|
|
|
2016-06-06 11:39:25 +08:00
|
|
|
extent_reregister(tsdn, extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
/* Try to decommit; purge if that fails. */
|
2017-01-30 13:57:14 +08:00
|
|
|
bool zeroed;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!extent_committed_get(extent)) {
|
2016-12-04 07:38:25 +08:00
|
|
|
zeroed = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if (!extent_decommit_wrapper(tsdn, arena, r_extent_hooks, extent,
|
|
|
|
0, extent_size_get(extent))) {
|
2016-12-04 07:38:25 +08:00
|
|
|
zeroed = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if ((*r_extent_hooks)->purge_lazy != NULL &&
|
2016-12-04 07:38:25 +08:00
|
|
|
!(*r_extent_hooks)->purge_lazy(*r_extent_hooks,
|
|
|
|
extent_base_get(extent), extent_size_get(extent), 0,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_size_get(extent), arena_ind_get(arena))) {
|
2016-12-04 07:38:25 +08:00
|
|
|
zeroed = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if ((*r_extent_hooks)->purge_forced != NULL &&
|
2016-12-04 07:38:25 +08:00
|
|
|
!(*r_extent_hooks)->purge_forced(*r_extent_hooks,
|
|
|
|
extent_base_get(extent), extent_size_get(extent), 0,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_size_get(extent), arena_ind_get(arena))) {
|
2016-12-04 07:38:25 +08:00
|
|
|
zeroed = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-12-04 07:38:25 +08:00
|
|
|
zeroed = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
extent_zeroed_set(extent, zeroed);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_prof) {
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_gdump_sub(tsdn, extent);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_record(tsdn, arena, r_extent_hooks, &arena->extents_retained,
|
|
|
|
extent);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_commit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t offset, size_t length, unsigned arena_ind) {
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return pages_commit((void *)((uintptr_t)addr + (uintptr_t)offset),
|
|
|
|
length);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
extent_commit_wrapper(tsdn_t *tsdn, arena_t *arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t length) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2017-01-30 13:57:14 +08:00
|
|
|
bool err = ((*r_extent_hooks)->commit == NULL ||
|
2016-12-04 07:38:25 +08:00
|
|
|
(*r_extent_hooks)->commit(*r_extent_hooks, extent_base_get(extent),
|
2016-12-23 06:39:10 +08:00
|
|
|
extent_size_get(extent), offset, length, arena_ind_get(arena)));
|
2016-06-06 14:24:52 +08:00
|
|
|
extent_committed_set(extent, extent_committed_get(extent) || !err);
|
2017-01-20 10:15:45 +08:00
|
|
|
return err;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_decommit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t offset, size_t length, unsigned arena_ind) {
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return pages_decommit((void *)((uintptr_t)addr + (uintptr_t)offset),
|
|
|
|
length);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
extent_decommit_wrapper(tsdn_t *tsdn, arena_t *arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t length) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-06-06 14:24:52 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
bool err = ((*r_extent_hooks)->decommit == NULL ||
|
2016-12-04 07:38:25 +08:00
|
|
|
(*r_extent_hooks)->decommit(*r_extent_hooks,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_base_get(extent), extent_size_get(extent), offset, length,
|
2016-12-23 06:39:10 +08:00
|
|
|
arena_ind_get(arena)));
|
2016-06-06 14:24:52 +08:00
|
|
|
extent_committed_set(extent, extent_committed_get(extent) && err);
|
2017-01-20 10:15:45 +08:00
|
|
|
return err;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-04 07:38:25 +08:00
|
|
|
#ifdef PAGES_CAN_PURGE_LAZY
|
2016-06-02 03:59:02 +08:00
|
|
|
static bool
|
2016-12-04 07:38:25 +08:00
|
|
|
extent_purge_lazy_default(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t offset, size_t length, unsigned arena_ind) {
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
2016-06-02 03:59:02 +08:00
|
|
|
assert(addr != NULL);
|
|
|
|
assert((offset & PAGE_MASK) == 0);
|
|
|
|
assert(length != 0);
|
|
|
|
assert((length & PAGE_MASK) == 0);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return pages_purge_lazy((void *)((uintptr_t)addr + (uintptr_t)offset),
|
|
|
|
length);
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
#endif
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
bool
|
2016-12-04 07:38:25 +08:00
|
|
|
extent_purge_lazy_wrapper(tsdn_t *tsdn, arena_t *arena,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t length) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-12-04 07:38:25 +08:00
|
|
|
return ((*r_extent_hooks)->purge_lazy == NULL ||
|
|
|
|
(*r_extent_hooks)->purge_lazy(*r_extent_hooks,
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_base_get(extent), extent_size_get(extent), offset, length,
|
2016-12-23 06:39:10 +08:00
|
|
|
arena_ind_get(arena)));
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-04 07:38:25 +08:00
|
|
|
#ifdef PAGES_CAN_PURGE_FORCED
|
|
|
|
static bool
|
|
|
|
extent_purge_forced_default(extent_hooks_t *extent_hooks, void *addr,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t size, size_t offset, size_t length, unsigned arena_ind) {
|
2016-12-04 07:38:25 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
assert(addr != NULL);
|
|
|
|
assert((offset & PAGE_MASK) == 0);
|
|
|
|
assert(length != 0);
|
|
|
|
assert((length & PAGE_MASK) == 0);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return pages_purge_forced((void *)((uintptr_t)addr +
|
|
|
|
(uintptr_t)offset), length);
|
2016-12-04 07:38:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool
|
|
|
|
extent_purge_forced_wrapper(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t length) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
2016-12-04 07:38:25 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
|
|
|
return ((*r_extent_hooks)->purge_forced == NULL ||
|
|
|
|
(*r_extent_hooks)->purge_forced(*r_extent_hooks,
|
|
|
|
extent_base_get(extent), extent_size_get(extent), offset, length,
|
2016-12-23 06:39:10 +08:00
|
|
|
arena_ind_get(arena)));
|
2016-12-04 07:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_MAPS_COALESCE
|
2016-06-02 03:59:02 +08:00
|
|
|
static bool
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_split_default(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {
|
2016-06-04 03:05:53 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return !maps_coalesce;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
#endif
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_t *
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_split_wrapper(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,
|
2017-01-16 08:56:30 +08:00
|
|
|
size_t usize_a, size_t size_b, size_t usize_b) {
|
2017-01-30 13:57:14 +08:00
|
|
|
assert(extent_size_get(extent) == size_a + size_b);
|
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_t *trail;
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
2016-06-02 03:59:02 +08:00
|
|
|
rtree_elm_t *lead_elm_a, *lead_elm_b, *trail_elm_a, *trail_elm_b;
|
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if ((*r_extent_hooks)->split == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
trail = extent_alloc(tsdn, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (trail == NULL) {
|
2016-06-02 03:59:02 +08:00
|
|
|
goto label_error_a;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
extent_t lead;
|
|
|
|
|
|
|
|
extent_init(&lead, arena, extent_addr_get(extent), size_a,
|
2017-01-30 13:57:14 +08:00
|
|
|
usize_a, extent_sn_get(extent), extent_state_get(extent),
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_zeroed_get(extent), extent_committed_get(extent),
|
|
|
|
extent_slab_get(extent));
|
|
|
|
|
2016-06-03 09:43:10 +08:00
|
|
|
if (extent_rtree_acquire(tsdn, rtree_ctx, &lead, false, true,
|
2017-01-16 08:56:30 +08:00
|
|
|
&lead_elm_a, &lead_elm_b)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
goto label_error_b;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extent_init(trail, arena, (void *)((uintptr_t)extent_base_get(extent) +
|
2016-11-16 05:07:53 +08:00
|
|
|
size_a), size_b, usize_b, extent_sn_get(extent),
|
2017-01-30 13:57:14 +08:00
|
|
|
extent_state_get(extent), extent_zeroed_get(extent),
|
2016-11-16 05:07:53 +08:00
|
|
|
extent_committed_get(extent), extent_slab_get(extent));
|
2016-06-03 09:43:10 +08:00
|
|
|
if (extent_rtree_acquire(tsdn, rtree_ctx, trail, false, true,
|
2017-01-16 08:56:30 +08:00
|
|
|
&trail_elm_a, &trail_elm_b)) {
|
2016-06-02 03:59:02 +08:00
|
|
|
goto label_error_c;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
if ((*r_extent_hooks)->split(*r_extent_hooks, extent_base_get(extent),
|
|
|
|
size_a + size_b, size_a, size_b, extent_committed_get(extent),
|
2017-01-16 08:56:30 +08:00
|
|
|
arena_ind_get(arena))) {
|
2016-06-02 03:59:02 +08:00
|
|
|
goto label_error_d;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_size_set(extent, size_a);
|
|
|
|
extent_usize_set(extent, usize_a);
|
|
|
|
|
|
|
|
extent_rtree_write_acquired(tsdn, lead_elm_a, lead_elm_b, extent);
|
|
|
|
extent_rtree_write_acquired(tsdn, trail_elm_a, trail_elm_b, trail);
|
|
|
|
|
|
|
|
extent_rtree_release(tsdn, lead_elm_a, lead_elm_b);
|
|
|
|
extent_rtree_release(tsdn, trail_elm_a, trail_elm_b);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return trail;
|
2016-06-02 03:59:02 +08:00
|
|
|
label_error_d:
|
2016-06-06 13:08:20 +08:00
|
|
|
extent_rtree_release(tsdn, trail_elm_a, trail_elm_b);
|
2016-06-02 03:59:02 +08:00
|
|
|
label_error_c:
|
|
|
|
extent_rtree_release(tsdn, lead_elm_a, lead_elm_b);
|
|
|
|
label_error_b:
|
|
|
|
extent_dalloc(tsdn, arena, trail);
|
|
|
|
label_error_a:
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_merge_default_impl(void *addr_a, void *addr_b) {
|
|
|
|
if (!maps_coalesce) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (have_dss && !extent_dss_mergeable(addr_a, addr_b)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-04 07:38:25 +08:00
|
|
|
#ifdef JEMALLOC_MAPS_COALESCE
|
2016-06-08 04:37:22 +08:00
|
|
|
static bool
|
|
|
|
extent_merge_default(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
|
2017-01-16 08:56:30 +08:00
|
|
|
void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {
|
2016-06-08 04:37:22 +08:00
|
|
|
assert(extent_hooks == &extent_hooks_default);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return extent_merge_default_impl(addr_a, addr_b);
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
#endif
|
2016-06-08 04:37:22 +08:00
|
|
|
|
2016-06-02 03:59:02 +08:00
|
|
|
bool
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_merge_wrapper(tsdn_t *tsdn, arena_t *arena,
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b) {
|
2017-01-30 13:57:14 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn, WITNESS_RANK_CORE, 0);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2016-06-04 03:05:53 +08:00
|
|
|
extent_hooks_assure_initialized(arena, r_extent_hooks);
|
2016-12-04 07:38:25 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if ((*r_extent_hooks)->merge == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-12-04 07:38:25 +08:00
|
|
|
|
2017-01-30 13:57:14 +08:00
|
|
|
bool err;
|
2016-06-08 04:37:22 +08:00
|
|
|
if (*r_extent_hooks == &extent_hooks_default) {
|
|
|
|
/* Call directly to propagate tsdn. */
|
2016-10-14 03:18:38 +08:00
|
|
|
err = extent_merge_default_impl(extent_base_get(a),
|
2016-06-08 04:37:22 +08:00
|
|
|
extent_base_get(b));
|
|
|
|
} else {
|
|
|
|
err = (*r_extent_hooks)->merge(*r_extent_hooks,
|
|
|
|
extent_base_get(a), extent_size_get(a), extent_base_get(b),
|
2016-12-23 06:39:10 +08:00
|
|
|
extent_size_get(b), extent_committed_get(a),
|
|
|
|
arena_ind_get(arena));
|
2016-06-08 04:37:22 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (err) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The rtree writes must happen while all the relevant elements are
|
|
|
|
* owned, so the following code uses decomposed helper functions rather
|
|
|
|
* than extent_{,de}register() to do things in the right order.
|
|
|
|
*/
|
2017-01-30 13:57:14 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
|
|
|
rtree_elm_t *a_elm_a, *a_elm_b, *b_elm_a, *b_elm_b;
|
2016-06-03 09:43:10 +08:00
|
|
|
extent_rtree_acquire(tsdn, rtree_ctx, a, true, false, &a_elm_a,
|
|
|
|
&a_elm_b);
|
|
|
|
extent_rtree_acquire(tsdn, rtree_ctx, b, true, false, &b_elm_a,
|
|
|
|
&b_elm_b);
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
if (a_elm_b != NULL) {
|
|
|
|
rtree_elm_write_acquired(tsdn, &extents_rtree, a_elm_b, NULL);
|
|
|
|
rtree_elm_release(tsdn, &extents_rtree, a_elm_b);
|
|
|
|
}
|
|
|
|
if (b_elm_b != NULL) {
|
|
|
|
rtree_elm_write_acquired(tsdn, &extents_rtree, b_elm_a, NULL);
|
|
|
|
rtree_elm_release(tsdn, &extents_rtree, b_elm_a);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-06-02 03:59:02 +08:00
|
|
|
b_elm_b = b_elm_a;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
|
|
|
extent_size_set(a, extent_size_get(a) + extent_size_get(b));
|
|
|
|
extent_usize_set(a, extent_usize_get(a) + extent_usize_get(b));
|
2016-11-16 05:07:53 +08:00
|
|
|
extent_sn_set(a, (extent_sn_get(a) < extent_sn_get(b)) ?
|
|
|
|
extent_sn_get(a) : extent_sn_get(b));
|
2016-06-02 03:59:02 +08:00
|
|
|
extent_zeroed_set(a, extent_zeroed_get(a) && extent_zeroed_get(b));
|
|
|
|
|
|
|
|
extent_rtree_write_acquired(tsdn, a_elm_a, b_elm_b, a);
|
|
|
|
extent_rtree_release(tsdn, a_elm_a, b_elm_b);
|
|
|
|
|
|
|
|
extent_dalloc(tsdn, extent_arena_get(b), b);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-06-02 03:59:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 03:10:39 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
extent_boot(void) {
|
2017-02-07 05:17:12 +08:00
|
|
|
if (rtree_new(&extents_rtree)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:10:39 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (have_dss) {
|
2016-10-14 03:18:38 +08:00
|
|
|
extent_dss_boot();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-06-02 03:59:02 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-06-02 03:10:39 +08:00
|
|
|
}
|