Blame jemalloc/include/jemalloc/internal/sc.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_SC_H
Packit 345191
#define JEMALLOC_INTERNAL_SC_H
Packit 345191
Packit 345191
#include "jemalloc/internal/jemalloc_internal_types.h"
Packit 345191
Packit 345191
/*
Packit 345191
 * Size class computations:
Packit 345191
 *
Packit 345191
 * These are a little tricky; we'll first start by describing how things
Packit 345191
 * generally work, and then describe some of the details.
Packit 345191
 *
Packit 345191
 * Ignore the first few size classes for a moment. We can then split all the
Packit 345191
 * remaining size classes into groups. The size classes in a group are spaced
Packit 345191
 * such that they cover allocation request sizes in a power-of-2 range. The
Packit 345191
 * power of two is called the base of the group, and the size classes in it
Packit 345191
 * satisfy allocations in the half-open range (base, base * 2]. There are
Packit 345191
 * SC_NGROUP size classes in each group, equally spaced in the range, so that
Packit 345191
 * each one covers allocations for base / SC_NGROUP possible allocation sizes.
Packit 345191
 * We call that value (base / SC_NGROUP) the delta of the group. Each size class
Packit 345191
 * is delta larger than the one before it (including the initial size class in a
Packit 345191
 * group, which is delta larger than base, the largest size class in the
Packit 345191
 * previous group).
Packit 345191
 * To make the math all work out nicely, we require that SC_NGROUP is a power of
Packit 345191
 * two, and define it in terms of SC_LG_NGROUP. We'll often talk in terms of
Packit 345191
 * lg_base and lg_delta. For each of these groups then, we have that
Packit 345191
 * lg_delta == lg_base - SC_LG_NGROUP.
Packit 345191
 * The size classes in a group with a given lg_base and lg_delta (which, recall,
Packit 345191
 * can be computed from lg_base for these groups) are therefore:
Packit 345191
 *   base + 1 * delta
Packit 345191
 *     which covers allocations in (base, base + 1 * delta]
Packit 345191
 *   base + 2 * delta
Packit 345191
 *     which covers allocations in (base + 1 * delta, base + 2 * delta].
Packit 345191
 *   base + 3 * delta
Packit 345191
 *     which covers allocations in (base + 2 * delta, base + 3 * delta].
Packit 345191
 *   ...
Packit 345191
 *   base + SC_NGROUP * delta ( == 2 * base)
Packit 345191
 *     which covers allocations in (base + (SC_NGROUP - 1) * delta, 2 * base].
Packit 345191
 * (Note that currently SC_NGROUP is always 4, so the "..." is empty in
Packit 345191
 * practice.)
Packit 345191
 * Note that the last size class in the group is the next power of two (after
Packit 345191
 * base), so that we've set up the induction correctly for the next group's
Packit 345191
 * selection of delta.
Packit 345191
 *
Packit 345191
 * Now, let's start considering the first few size classes. Two extra constants
Packit 345191
 * come into play here: LG_QUANTUM and SC_LG_TINY_MIN. LG_QUANTUM ensures
Packit 345191
 * correct platform alignment; all objects of size (1 << LG_QUANTUM) or larger
Packit 345191
 * are at least (1 << LG_QUANTUM) aligned; this can be used to ensure that we
Packit 345191
 * never return improperly aligned memory, by making (1 << LG_QUANTUM) equal the
Packit 345191
 * highest required alignment of a platform. For allocation sizes smaller than
Packit 345191
 * (1 << LG_QUANTUM) though, we can be more relaxed (since we don't support
Packit 345191
 * platforms with types with alignment larger than their size). To allow such
Packit 345191
 * allocations (without wasting space unnecessarily), we introduce tiny size
Packit 345191
 * classes; one per power of two, up until we hit the quantum size. There are
Packit 345191
 * therefore LG_QUANTUM - SC_LG_TINY_MIN such size classes.
Packit 345191
 *
Packit 345191
 * Next, we have a size class of size (1 << LG_QUANTUM).  This can't be the
Packit 345191
 * start of a group in the sense we described above (covering a power of two
Packit 345191
 * range) since, if we divided into it to pick a value of delta, we'd get a
Packit 345191
 * delta smaller than (1 << LG_QUANTUM) for sizes >= (1 << LG_QUANTUM), which
Packit 345191
 * is against the rules.
Packit 345191
 *
Packit 345191
 * The first base we can divide by SC_NGROUP while still being at least
Packit 345191
 * (1 << LG_QUANTUM) is SC_NGROUP * (1 << LG_QUANTUM). We can get there by
Packit 345191
 * having SC_NGROUP size classes, spaced (1 << LG_QUANTUM) apart. These size
Packit 345191
 * classes are:
Packit 345191
 *   1 * (1 << LG_QUANTUM)
Packit 345191
 *   2 * (1 << LG_QUANTUM)
Packit 345191
 *   3 * (1 << LG_QUANTUM)
Packit 345191
 *   ... (although, as above, this "..." is empty in practice)
Packit 345191
 *   SC_NGROUP * (1 << LG_QUANTUM).
Packit 345191
 *
Packit 345191
 * There are SC_NGROUP of these size classes, so we can regard it as a sort of
Packit 345191
 * pseudo-group, even though it spans multiple powers of 2, is divided
Packit 345191
 * differently, and both starts and ends on a power of 2 (as opposed to just
Packit 345191
 * ending). SC_NGROUP is itself a power of two, so the first group after the
Packit 345191
 * pseudo-group has the power-of-two base SC_NGROUP * (1 << LG_QUANTUM), for a
Packit 345191
 * lg_base of LG_QUANTUM + SC_LG_NGROUP. We can divide this base into SC_NGROUP
Packit 345191
 * sizes without violating our LG_QUANTUM requirements, so we can safely set
Packit 345191
 * lg_delta = lg_base - SC_LG_GROUP (== LG_QUANTUM).
Packit 345191
 *
Packit 345191
 * So, in order, the size classes are:
Packit 345191
 *
Packit 345191
 * Tiny size classes:
Packit 345191
 * - Count: LG_QUANTUM - SC_LG_TINY_MIN.
Packit 345191
 * - Sizes:
Packit 345191
 *     1 << SC_LG_TINY_MIN
Packit 345191
 *     1 << (SC_LG_TINY_MIN + 1)
Packit 345191
 *     1 << (SC_LG_TINY_MIN + 2)
Packit 345191
 *     ...
Packit 345191
 *     1 << (LG_QUANTUM - 1)
Packit 345191
 *
Packit 345191
 * Initial pseudo-group:
Packit 345191
 * - Count: SC_NGROUP
Packit 345191
 * - Sizes:
Packit 345191
 *     1 * (1 << LG_QUANTUM)
Packit 345191
 *     2 * (1 << LG_QUANTUM)
Packit 345191
 *     3 * (1 << LG_QUANTUM)
Packit 345191
 *     ...
Packit 345191
 *     SC_NGROUP * (1 << LG_QUANTUM)
Packit 345191
 *
Packit 345191
 * Regular group 0:
Packit 345191
 * - Count: SC_NGROUP
Packit 345191
 * - Sizes:
Packit 345191
 *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP and lg_delta of
Packit 345191
 *   lg_base - SC_LG_NGROUP)
Packit 345191
 *     (1 << lg_base) + 1 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 2 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 3 * (1 << lg_delta)
Packit 345191
 *     ...
Packit 345191
 *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]
Packit 345191
 *
Packit 345191
 * Regular group 1:
Packit 345191
 * - Count: SC_NGROUP
Packit 345191
 * - Sizes:
Packit 345191
 *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + 1 and lg_delta of
Packit 345191
 *   lg_base - SC_LG_NGROUP)
Packit 345191
 *     (1 << lg_base) + 1 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 2 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 3 * (1 << lg_delta)
Packit 345191
 *     ...
Packit 345191
 *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]
Packit 345191
 *
Packit 345191
 * ...
Packit 345191
 *
Packit 345191
 * Regular group N:
Packit 345191
 * - Count: SC_NGROUP
Packit 345191
 * - Sizes:
Packit 345191
 *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + N and lg_delta of
Packit 345191
 *   lg_base - SC_LG_NGROUP)
Packit 345191
 *     (1 << lg_base) + 1 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 2 * (1 << lg_delta)
Packit 345191
 *     (1 << lg_base) + 3 * (1 << lg_delta)
Packit 345191
 *     ...
Packit 345191
 *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]
Packit 345191
 *
Packit 345191
 *
Packit 345191
 * Representation of metadata:
Packit 345191
 * To make the math easy, we'll mostly work in lg quantities. We record lg_base,
Packit 345191
 * lg_delta, and ndelta (i.e. number of deltas above the base) on a
Packit 345191
 * per-size-class basis, and maintain the invariant that, across all size
Packit 345191
 * classes, size == (1 << lg_base) + ndelta * (1 << lg_delta).
Packit 345191
 *
Packit 345191
 * For regular groups (i.e. those with lg_base >= LG_QUANTUM + SC_LG_NGROUP),
Packit 345191
 * lg_delta is lg_base - SC_LG_NGROUP, and ndelta goes from 1 to SC_NGROUP.
Packit 345191
 *
Packit 345191
 * For the initial tiny size classes (if any), lg_base is lg(size class size).
Packit 345191
 * lg_delta is lg_base for the first size class, and lg_base - 1 for all
Packit 345191
 * subsequent ones. ndelta is always 0.
Packit 345191
 *
Packit 345191
 * For the pseudo-group, if there are no tiny size classes, then we set
Packit 345191
 * lg_base == LG_QUANTUM, lg_delta == LG_QUANTUM, and have ndelta range from 0
Packit 345191
 * to SC_NGROUP - 1. (Note that delta == base, so base + (SC_NGROUP - 1) * delta
Packit 345191
 * is just SC_NGROUP * base, or (1 << (SC_LG_NGROUP + LG_QUANTUM)), so we do
Packit 345191
 * indeed get a power of two that way). If there *are* tiny size classes, then
Packit 345191
 * the first size class needs to have lg_delta relative to the largest tiny size
Packit 345191
 * class. We therefore set lg_base == LG_QUANTUM - 1,
Packit 345191
 * lg_delta == LG_QUANTUM - 1, and ndelta == 1, keeping the rest of the
Packit 345191
 * pseudo-group the same.
Packit 345191
 *
Packit 345191
 *
Packit 345191
 * Other terminology:
Packit 345191
 * "Small" size classes mean those that are allocated out of bins, which is the
Packit 345191
 * same as those that are slab allocated.
Packit 345191
 * "Large" size classes are those that are not small. The cutoff for counting as
Packit 345191
 * large is page size * group size.
Packit 345191
 */
Packit 345191
Packit 345191
/*
Packit 345191
 * Size class N + (1 << SC_LG_NGROUP) twice the size of size class N.
Packit 345191
 */
Packit 345191
#define SC_LG_NGROUP 2
Packit 345191
#define SC_LG_TINY_MIN 3
Packit 345191
Packit 345191
#if SC_LG_TINY_MIN == 0
Packit 345191
/* The div module doesn't support division by 1, which this would require. */
Packit 345191
#error "Unsupported LG_TINY_MIN"
Packit 345191
#endif
Packit 345191
Packit 345191
/*
Packit 345191
 * The definitions below are all determined by the above settings and system
Packit 345191
 * characteristics.
Packit 345191
 */
Packit 345191
#define SC_NGROUP (1ULL << SC_LG_NGROUP)
Packit 345191
#define SC_PTR_BITS ((1ULL << LG_SIZEOF_PTR) * 8)
Packit 345191
#define SC_NTINY (LG_QUANTUM - SC_LG_TINY_MIN)
Packit 345191
#define SC_LG_TINY_MAXCLASS (LG_QUANTUM > SC_LG_TINY_MIN ? LG_QUANTUM - 1 : -1)
Packit 345191
#define SC_NPSEUDO SC_NGROUP
Packit 345191
#define SC_LG_FIRST_REGULAR_BASE (LG_QUANTUM + SC_LG_NGROUP)
Packit 345191
/*
Packit 345191
 * We cap allocations to be less than 2 ** (ptr_bits - 1), so the highest base
Packit 345191
 * we need is 2 ** (ptr_bits - 2). (This also means that the last group is 1
Packit 345191
 * size class shorter than the others).
Packit 345191
 * We could probably save some space in arenas by capping this at LG_VADDR size.
Packit 345191
 */
Packit 345191
#define SC_LG_BASE_MAX (SC_PTR_BITS - 2)
Packit 345191
#define SC_NREGULAR (SC_NGROUP * 					\
Packit 345191
    (SC_LG_BASE_MAX - SC_LG_FIRST_REGULAR_BASE + 1) - 1)
Packit 345191
#define SC_NSIZES (SC_NTINY + SC_NPSEUDO + SC_NREGULAR)
Packit 345191
Packit 345191
/* The number of size classes that are a multiple of the page size. */
Packit 345191
#define SC_NPSIZES (							\
Packit 345191
    /* Start with all the size classes. */				\
Packit 345191
    SC_NSIZES								\
Packit 345191
    /* Subtract out those groups with too small a base. */		\
Packit 345191
    - (LG_PAGE - 1 - SC_LG_FIRST_REGULAR_BASE) * SC_NGROUP		\
Packit 345191
    /* And the pseudo-group. */						\
Packit 345191
    - SC_NPSEUDO							\
Packit 345191
    /* And the tiny group. */						\
Packit 345191
    - SC_NTINY								\
Packit 345191
    /* Sizes where ndelta*delta is not a multiple of the page size. */	\
Packit 345191
    - (SC_LG_NGROUP * SC_NGROUP))
Packit 345191
/*
Packit 345191
 * Note that the last line is computed as the sum of the second column in the
Packit 345191
 * following table:
Packit 345191
 *                      lg(base) | count of sizes to exclude
Packit 345191
 * ------------------------------|-----------------------------
Packit 345191
 *                   LG_PAGE - 1 | SC_NGROUP - 1
Packit 345191
 *                       LG_PAGE | SC_NGROUP - 1
Packit 345191
 *                   LG_PAGE + 1 | SC_NGROUP - 2
Packit 345191
 *                   LG_PAGE + 2 | SC_NGROUP - 4
Packit 345191
 *                           ... | ...
Packit 345191
 *  LG_PAGE + (SC_LG_NGROUP - 1) | SC_NGROUP - (SC_NGROUP / 2)
Packit 345191
 */
Packit 345191
Packit 345191
/*
Packit 345191
 * We declare a size class is binnable if size < page size * group. Or, in other
Packit 345191
 * words, lg(size) < lg(page size) + lg(group size).
Packit 345191
 */
Packit 345191
#define SC_NBINS (							\
Packit 345191
    /* Sub-regular size classes. */					\
Packit 345191
    SC_NTINY + SC_NPSEUDO						\
Packit 345191
    /* Groups with lg_regular_min_base <= lg_base <= lg_base_max */	\
Packit 345191
    + SC_NGROUP * (LG_PAGE + SC_LG_NGROUP - SC_LG_FIRST_REGULAR_BASE)	\
Packit 345191
    /* Last SC of the last group hits the bound exactly; exclude it. */	\
Packit 345191
    - 1)
Packit 345191
Packit 345191
/*
Packit 345191
 * The size2index_tab lookup table uses uint8_t to encode each bin index, so we
Packit 345191
 * cannot support more than 256 small size classes.
Packit 345191
 */
Packit 345191
#if (SC_NBINS > 256)
Packit 345191
#  error "Too many small size classes"
Packit 345191
#endif
Packit 345191
Packit 345191
/* The largest size class in the lookup table. */
Packit 345191
#define SC_LOOKUP_MAXCLASS ((size_t)1 << 12)
Packit 345191
Packit 345191
/* Internal, only used for the definition of SC_SMALL_MAXCLASS. */
Packit 345191
#define SC_SMALL_MAX_BASE ((size_t)1 << (LG_PAGE + SC_LG_NGROUP - 1))
Packit 345191
#define SC_SMALL_MAX_DELTA ((size_t)1 << (LG_PAGE - 1))
Packit 345191
Packit 345191
/* The largest size class allocated out of a slab. */
Packit 345191
#define SC_SMALL_MAXCLASS (SC_SMALL_MAX_BASE				\
Packit 345191
    + (SC_NGROUP - 1) * SC_SMALL_MAX_DELTA)
Packit 345191
Packit 345191
/* The smallest size class not allocated out of a slab. */
Packit 345191
#define SC_LARGE_MINCLASS ((size_t)1ULL << (LG_PAGE + SC_LG_NGROUP))
Packit 345191
#define SC_LG_LARGE_MINCLASS (LG_PAGE + SC_LG_NGROUP)
Packit 345191
Packit 345191
/* Internal; only used for the definition of SC_LARGE_MAXCLASS. */
Packit 345191
#define SC_MAX_BASE ((size_t)1 << (SC_PTR_BITS - 2))
Packit 345191
#define SC_MAX_DELTA ((size_t)1 << (SC_PTR_BITS - 2 - SC_LG_NGROUP))
Packit 345191
Packit 345191
/* The largest size class supported. */
Packit 345191
#define SC_LARGE_MAXCLASS (SC_MAX_BASE + (SC_NGROUP - 1) * SC_MAX_DELTA)
Packit 345191
Packit 345191
typedef struct sc_s sc_t;
Packit 345191
struct sc_s {
Packit 345191
	/* Size class index, or -1 if not a valid size class. */
Packit 345191
	int index;
Packit 345191
	/* Lg group base size (no deltas added). */
Packit 345191
	int lg_base;
Packit 345191
	/* Lg delta to previous size class. */
Packit 345191
	int lg_delta;
Packit 345191
	/* Delta multiplier.  size == 1<
Packit 345191
	int ndelta;
Packit 345191
	/*
Packit 345191
	 * True if the size class is a multiple of the page size, false
Packit 345191
	 * otherwise.
Packit 345191
	 */
Packit 345191
	bool psz;
Packit 345191
	/*
Packit 345191
	 * True if the size class is a small, bin, size class. False otherwise.
Packit 345191
	 */
Packit 345191
	bool bin;
Packit 345191
	/* The slab page count if a small bin size class, 0 otherwise. */
Packit 345191
	int pgs;
Packit 345191
	/* Same as lg_delta if a lookup table size class, 0 otherwise. */
Packit 345191
	int lg_delta_lookup;
Packit 345191
};
Packit 345191
Packit 345191
typedef struct sc_data_s sc_data_t;
Packit 345191
struct sc_data_s {
Packit 345191
	/* Number of tiny size classes. */
Packit 345191
	unsigned ntiny;
Packit 345191
	/* Number of bins supported by the lookup table. */
Packit 345191
	int nlbins;
Packit 345191
	/* Number of small size class bins. */
Packit 345191
	int nbins;
Packit 345191
	/* Number of size classes. */
Packit 345191
	int nsizes;
Packit 345191
	/* Number of bits required to store NSIZES. */
Packit 345191
	int lg_ceil_nsizes;
Packit 345191
	/* Number of size classes that are a multiple of (1U << LG_PAGE). */
Packit 345191
	unsigned npsizes;
Packit 345191
	/* Lg of maximum tiny size class (or -1, if none). */
Packit 345191
	int lg_tiny_maxclass;
Packit 345191
	/* Maximum size class included in lookup table. */
Packit 345191
	size_t lookup_maxclass;
Packit 345191
	/* Maximum small size class. */
Packit 345191
	size_t small_maxclass;
Packit 345191
	/* Lg of minimum large size class. */
Packit 345191
	int lg_large_minclass;
Packit 345191
	/* The minimum large size class. */
Packit 345191
	size_t large_minclass;
Packit 345191
	/* Maximum (large) size class. */
Packit 345191
	size_t large_maxclass;
Packit 345191
	/* True if the sc_data_t has been initialized (for debugging only). */
Packit 345191
	bool initialized;
Packit 345191
Packit 345191
	sc_t sc[SC_NSIZES];
Packit 345191
};
Packit 345191
Packit 345191
void sc_data_init(sc_data_t *data);
Packit 345191
/*
Packit 345191
 * Updates slab sizes in [begin, end] to be pgs pages in length, if possible.
Packit 345191
 * Otherwise, does its best to accomodate the request.
Packit 345191
 */
Packit 345191
void sc_data_update_slab_size(sc_data_t *data, size_t begin, size_t end,
Packit 345191
    int pgs);
Packit 345191
void sc_boot(sc_data_t *data);
Packit 345191
Packit 345191
#endif /* JEMALLOC_INTERNAL_SC_H */