mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-06-02 02:04:20 +03:00
Folds several historical *_types/_structs/_externs/_inlines splits where the layering is no longer load-bearing. - large_externs.h -> large.h: renamed; it was a single-purpose function-prototype file. - background_thread_structs.h + background_thread_externs.h -> background_thread.h: merged. background_thread_inlines.h is kept separate because it depends on arena_inlines_a.h. - bin_inlines.h folded into bin.h, along with BIN_SHARDS_MAX / N_BIN_SHARDS_DEFAULT from bin_types.h. bin.h carries a forward decl of arena_binind_div_info (declared in arena_externs.h) so it stays hermetic without re-introducing the bin.h <-> arena_externs.h cycle. - tsd_binshards.h (new): houses tsd_binshards_t and its zero initializer. Keeping these out of bin.h lets tsd_internals.h pull in just what it needs during X-macro expansion, avoiding bin.h's mutex.h dependency (mutex.h itself depends on TSD machinery, so routing it through tsd_internals.h forms a chicken-and-egg). jemalloc_internal_includes.h: drops the now-redundant references to the deleted/merged headers.
227 lines
7.6 KiB
C
227 lines
7.6 KiB
C
#ifndef JEMALLOC_INTERNAL_BIN_H
|
|
#define JEMALLOC_INTERNAL_BIN_H
|
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
#include "jemalloc/internal/bin_info.h"
|
|
#include "jemalloc/internal/bin_stats.h"
|
|
#include "jemalloc/internal/bitmap.h"
|
|
#include "jemalloc/internal/div.h"
|
|
#include "jemalloc/internal/edata.h"
|
|
#include "jemalloc/internal/mutex.h"
|
|
#include "jemalloc/internal/sc.h"
|
|
|
|
#define BIN_SHARDS_MAX (1 << EDATA_BITS_BINSHARD_WIDTH)
|
|
#define N_BIN_SHARDS_DEFAULT 1
|
|
|
|
/*
|
|
* A bin contains a set of extents that are currently being used for slab
|
|
* allocations.
|
|
*/
|
|
typedef struct bin_s bin_t;
|
|
struct bin_s {
|
|
/* All operations on bin_t fields require lock ownership. */
|
|
malloc_mutex_t lock;
|
|
|
|
/*
|
|
* Bin statistics. These get touched every time the lock is acquired,
|
|
* so put them close by in the hopes of getting some cache locality.
|
|
*/
|
|
bin_stats_t stats;
|
|
|
|
/*
|
|
* Current slab being used to service allocations of this bin's size
|
|
* class. slabcur is independent of slabs_{nonfull,full}; whenever
|
|
* slabcur is reassigned, the previous slab must be deallocated or
|
|
* inserted into slabs_{nonfull,full}.
|
|
*/
|
|
edata_t *slabcur;
|
|
|
|
/*
|
|
* Heap of non-full slabs. This heap is used to assure that new
|
|
* allocations come from the non-full slab that is oldest/lowest in
|
|
* memory.
|
|
*/
|
|
edata_heap_t slabs_nonfull;
|
|
|
|
/* List used to track full slabs. */
|
|
edata_list_active_t slabs_full;
|
|
};
|
|
|
|
/* A set of sharded bins of the same size class. */
|
|
typedef struct bins_s bins_t;
|
|
struct bins_s {
|
|
/* Sharded bins. Dynamically sized. */
|
|
bin_t *bin_shards;
|
|
};
|
|
|
|
void bin_shard_sizes_boot(unsigned bin_shard_sizes[SC_NBINS]);
|
|
bool bin_update_shard_size(unsigned bin_shards[SC_NBINS], size_t start_size,
|
|
size_t end_size, size_t nshards);
|
|
|
|
/* Initializes a bin to empty. Returns true on error. */
|
|
bool bin_init(bin_t *bin);
|
|
|
|
/* Forking. */
|
|
void bin_prefork(tsdn_t *tsdn, bin_t *bin);
|
|
void bin_postfork_parent(tsdn_t *tsdn, bin_t *bin);
|
|
void bin_postfork_child(tsdn_t *tsdn, bin_t *bin);
|
|
|
|
/* Slab region allocation. */
|
|
void *bin_slab_reg_alloc(edata_t *slab, const bin_info_t *bin_info);
|
|
void bin_slab_reg_alloc_batch(
|
|
edata_t *slab, const bin_info_t *bin_info, unsigned cnt, void **ptrs);
|
|
|
|
/* Slab list management. */
|
|
void bin_slabs_nonfull_insert(bin_t *bin, edata_t *slab);
|
|
void bin_slabs_nonfull_remove(bin_t *bin, edata_t *slab);
|
|
edata_t *bin_slabs_nonfull_tryget(bin_t *bin);
|
|
void bin_slabs_full_insert(bool is_auto, bin_t *bin, edata_t *slab);
|
|
void bin_slabs_full_remove(bool is_auto, bin_t *bin, edata_t *slab);
|
|
|
|
/* Slab association / demotion. */
|
|
void bin_dissociate_slab(bool is_auto, edata_t *slab, bin_t *bin);
|
|
void bin_lower_slab(tsdn_t *tsdn, bool is_auto, edata_t *slab, bin_t *bin);
|
|
|
|
/* Deallocation helpers (called under bin lock). */
|
|
void bin_dalloc_slab_prepare(tsdn_t *tsdn, edata_t *slab, bin_t *bin);
|
|
void bin_dalloc_locked_handle_newly_empty(
|
|
tsdn_t *tsdn, bool is_auto, edata_t *slab, bin_t *bin);
|
|
void bin_dalloc_locked_handle_newly_nonempty(
|
|
tsdn_t *tsdn, bool is_auto, edata_t *slab, bin_t *bin);
|
|
|
|
/* Slabcur refill and allocation. */
|
|
void bin_refill_slabcur_with_fresh_slab(tsdn_t *tsdn, bin_t *bin,
|
|
szind_t binind, edata_t *fresh_slab);
|
|
void *bin_malloc_with_fresh_slab(tsdn_t *tsdn, bin_t *bin,
|
|
szind_t binind, edata_t *fresh_slab);
|
|
bool bin_refill_slabcur_no_fresh_slab(tsdn_t *tsdn, bool is_auto,
|
|
bin_t *bin);
|
|
void *bin_malloc_no_fresh_slab(tsdn_t *tsdn, bool is_auto, bin_t *bin,
|
|
szind_t binind);
|
|
|
|
/* Slab queries. */
|
|
void *bin_current_slab_addr(tsdn_t *tsdn, bin_t *bin);
|
|
|
|
/* Bin selection. */
|
|
bin_t *bin_choose(tsdn_t *tsdn, arena_t *arena, szind_t binind,
|
|
unsigned *binshard_p);
|
|
|
|
/* Stats. */
|
|
static inline void
|
|
bin_stats_nrequests_add(tsdn_t *tsdn, bin_t *bin, uint64_t n) {
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
|
bin->stats.nrequests += n;
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
|
}
|
|
|
|
static inline void
|
|
bin_stats_merge(tsdn_t *tsdn, bin_stats_data_t *dst_bin_stats, bin_t *bin) {
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
|
malloc_mutex_prof_accum(tsdn, &dst_bin_stats->mutex_data, &bin->lock);
|
|
bin_stats_t *stats = &dst_bin_stats->stats_data;
|
|
stats->nmalloc += bin->stats.nmalloc;
|
|
stats->ndalloc += bin->stats.ndalloc;
|
|
stats->nrequests += bin->stats.nrequests;
|
|
stats->curregs += bin->stats.curregs;
|
|
stats->nfills += bin->stats.nfills;
|
|
stats->nflushes += bin->stats.nflushes;
|
|
stats->nslabs += bin->stats.nslabs;
|
|
stats->reslabs += bin->stats.reslabs;
|
|
stats->curslabs += bin->stats.curslabs;
|
|
stats->nonfull_slabs += bin->stats.nonfull_slabs;
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
|
}
|
|
|
|
/*
|
|
* The dalloc bin info contains just the information that the common paths need
|
|
* during tcache flushes. By force-inlining these paths, and using local copies
|
|
* of data (so that the compiler knows it's constant), we avoid a whole bunch of
|
|
* redundant loads and stores by leaving this information in registers.
|
|
*/
|
|
typedef struct bin_dalloc_locked_info_s bin_dalloc_locked_info_t;
|
|
struct bin_dalloc_locked_info_s {
|
|
div_info_t div_info;
|
|
uint32_t nregs;
|
|
uint64_t ndalloc;
|
|
};
|
|
|
|
/* Find the region index of a pointer within a slab. */
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
|
bin_slab_regind_impl(const div_info_t *div_info, szind_t binind,
|
|
const edata_t *slab, const void *ptr) {
|
|
size_t diff, regind;
|
|
|
|
/* Freeing a pointer outside the slab can cause assertion failure. */
|
|
assert((uintptr_t)ptr >= (uintptr_t)edata_addr_get(slab));
|
|
assert((uintptr_t)ptr < (uintptr_t)edata_past_get(slab));
|
|
/* Freeing an interior pointer can cause assertion failure. */
|
|
assert(((uintptr_t)ptr - (uintptr_t)edata_addr_get(slab))
|
|
% (uintptr_t)bin_infos[binind].reg_size
|
|
== 0);
|
|
|
|
diff = (size_t)((uintptr_t)ptr - (uintptr_t)edata_addr_get(slab));
|
|
|
|
/* Avoid doing division with a variable divisor. */
|
|
regind = div_compute(div_info, diff);
|
|
assert(regind < bin_infos[binind].nregs);
|
|
return regind;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
|
bin_slab_regind(const bin_dalloc_locked_info_t *info, szind_t binind,
|
|
const edata_t *slab, const void *ptr) {
|
|
size_t regind = bin_slab_regind_impl(
|
|
&info->div_info, binind, slab, ptr);
|
|
return regind;
|
|
}
|
|
|
|
/*
|
|
* Does the deallocation work associated with freeing a single pointer (a
|
|
* "step") in between a bin_dalloc_locked begin and end call.
|
|
*
|
|
* Returns true if arena_slab_dalloc must be called on slab. Doesn't do
|
|
* stats updates, which happen during finish (this lets running counts get left
|
|
* in a register).
|
|
*/
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
bin_dalloc_locked_step(tsdn_t *tsdn, bool is_auto, bin_t *bin,
|
|
bin_dalloc_locked_info_t *info, szind_t binind, edata_t *slab,
|
|
void *ptr) {
|
|
const bin_info_t *bin_info = &bin_infos[binind];
|
|
size_t regind = bin_slab_regind(info, binind, slab, ptr);
|
|
slab_data_t *slab_data = edata_slab_data_get(slab);
|
|
|
|
assert(edata_nfree_get(slab) < bin_info->nregs);
|
|
/* Freeing an unallocated pointer can cause assertion failure. */
|
|
assert(bitmap_get(slab_data->bitmap, &bin_info->bitmap_info, regind));
|
|
|
|
bitmap_unset(slab_data->bitmap, &bin_info->bitmap_info, regind);
|
|
edata_nfree_inc(slab);
|
|
|
|
if (config_stats) {
|
|
info->ndalloc++;
|
|
}
|
|
|
|
unsigned nfree = edata_nfree_get(slab);
|
|
if (nfree == bin_info->nregs) {
|
|
bin_dalloc_locked_handle_newly_empty(
|
|
tsdn, is_auto, slab, bin);
|
|
return true;
|
|
} else if (nfree == 1 && slab != bin->slabcur) {
|
|
bin_dalloc_locked_handle_newly_nonempty(
|
|
tsdn, is_auto, slab, bin);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
bin_dalloc_locked_finish(tsdn_t *tsdn, bin_t *bin,
|
|
bin_dalloc_locked_info_t *info) {
|
|
if (config_stats) {
|
|
bin->stats.ndalloc += info->ndalloc;
|
|
assert(bin->stats.curregs >= (size_t)info->ndalloc);
|
|
bin->stats.curregs -= (size_t)info->ndalloc;
|
|
}
|
|
}
|
|
|
|
#endif /* JEMALLOC_INTERNAL_BIN_H */
|