xref: /aosp_15_r20/external/jemalloc_new/include/jemalloc/internal/bin.h (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
1*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_INTERNAL_BIN_H
2*1208bc7eSAndroid Build Coastguard Worker #define JEMALLOC_INTERNAL_BIN_H
3*1208bc7eSAndroid Build Coastguard Worker 
4*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/extent_types.h"
5*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/extent_structs.h"
6*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/mutex.h"
7*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/bin_stats.h"
8*1208bc7eSAndroid Build Coastguard Worker 
9*1208bc7eSAndroid Build Coastguard Worker /*
10*1208bc7eSAndroid Build Coastguard Worker  * A bin contains a set of extents that are currently being used for slab
11*1208bc7eSAndroid Build Coastguard Worker  * allocations.
12*1208bc7eSAndroid Build Coastguard Worker  */
13*1208bc7eSAndroid Build Coastguard Worker 
14*1208bc7eSAndroid Build Coastguard Worker /*
15*1208bc7eSAndroid Build Coastguard Worker  * Read-only information associated with each element of arena_t's bins array
16*1208bc7eSAndroid Build Coastguard Worker  * is stored separately, partly to reduce memory usage (only one copy, rather
17*1208bc7eSAndroid Build Coastguard Worker  * than one per arena), but mainly to avoid false cacheline sharing.
18*1208bc7eSAndroid Build Coastguard Worker  *
19*1208bc7eSAndroid Build Coastguard Worker  * Each slab has the following layout:
20*1208bc7eSAndroid Build Coastguard Worker  *
21*1208bc7eSAndroid Build Coastguard Worker  *   /--------------------\
22*1208bc7eSAndroid Build Coastguard Worker  *   | region 0           |
23*1208bc7eSAndroid Build Coastguard Worker  *   |--------------------|
24*1208bc7eSAndroid Build Coastguard Worker  *   | region 1           |
25*1208bc7eSAndroid Build Coastguard Worker  *   |--------------------|
26*1208bc7eSAndroid Build Coastguard Worker  *   | ...                |
27*1208bc7eSAndroid Build Coastguard Worker  *   | ...                |
28*1208bc7eSAndroid Build Coastguard Worker  *   | ...                |
29*1208bc7eSAndroid Build Coastguard Worker  *   |--------------------|
30*1208bc7eSAndroid Build Coastguard Worker  *   | region nregs-1     |
31*1208bc7eSAndroid Build Coastguard Worker  *   \--------------------/
32*1208bc7eSAndroid Build Coastguard Worker  */
33*1208bc7eSAndroid Build Coastguard Worker typedef struct bin_info_s bin_info_t;
34*1208bc7eSAndroid Build Coastguard Worker struct bin_info_s {
35*1208bc7eSAndroid Build Coastguard Worker 	/* Size of regions in a slab for this bin's size class. */
36*1208bc7eSAndroid Build Coastguard Worker 	size_t			reg_size;
37*1208bc7eSAndroid Build Coastguard Worker 
38*1208bc7eSAndroid Build Coastguard Worker 	/* Total size of a slab for this bin's size class. */
39*1208bc7eSAndroid Build Coastguard Worker 	size_t			slab_size;
40*1208bc7eSAndroid Build Coastguard Worker 
41*1208bc7eSAndroid Build Coastguard Worker 	/* Total number of regions in a slab for this bin's size class. */
42*1208bc7eSAndroid Build Coastguard Worker 	uint32_t		nregs;
43*1208bc7eSAndroid Build Coastguard Worker 
44*1208bc7eSAndroid Build Coastguard Worker 	/*
45*1208bc7eSAndroid Build Coastguard Worker 	 * Metadata used to manipulate bitmaps for slabs associated with this
46*1208bc7eSAndroid Build Coastguard Worker 	 * bin.
47*1208bc7eSAndroid Build Coastguard Worker 	 */
48*1208bc7eSAndroid Build Coastguard Worker 	bitmap_info_t		bitmap_info;
49*1208bc7eSAndroid Build Coastguard Worker };
50*1208bc7eSAndroid Build Coastguard Worker 
51*1208bc7eSAndroid Build Coastguard Worker extern const bin_info_t bin_infos[NBINS];
52*1208bc7eSAndroid Build Coastguard Worker 
53*1208bc7eSAndroid Build Coastguard Worker 
54*1208bc7eSAndroid Build Coastguard Worker typedef struct bin_s bin_t;
55*1208bc7eSAndroid Build Coastguard Worker struct bin_s {
56*1208bc7eSAndroid Build Coastguard Worker 	/* All operations on bin_t fields require lock ownership. */
57*1208bc7eSAndroid Build Coastguard Worker 	malloc_mutex_t		lock;
58*1208bc7eSAndroid Build Coastguard Worker 
59*1208bc7eSAndroid Build Coastguard Worker 	/*
60*1208bc7eSAndroid Build Coastguard Worker 	 * Current slab being used to service allocations of this bin's size
61*1208bc7eSAndroid Build Coastguard Worker 	 * class.  slabcur is independent of slabs_{nonfull,full}; whenever
62*1208bc7eSAndroid Build Coastguard Worker 	 * slabcur is reassigned, the previous slab must be deallocated or
63*1208bc7eSAndroid Build Coastguard Worker 	 * inserted into slabs_{nonfull,full}.
64*1208bc7eSAndroid Build Coastguard Worker 	 */
65*1208bc7eSAndroid Build Coastguard Worker 	extent_t		*slabcur;
66*1208bc7eSAndroid Build Coastguard Worker 
67*1208bc7eSAndroid Build Coastguard Worker 	/*
68*1208bc7eSAndroid Build Coastguard Worker 	 * Heap of non-full slabs.  This heap is used to assure that new
69*1208bc7eSAndroid Build Coastguard Worker 	 * allocations come from the non-full slab that is oldest/lowest in
70*1208bc7eSAndroid Build Coastguard Worker 	 * memory.
71*1208bc7eSAndroid Build Coastguard Worker 	 */
72*1208bc7eSAndroid Build Coastguard Worker 	extent_heap_t		slabs_nonfull;
73*1208bc7eSAndroid Build Coastguard Worker 
74*1208bc7eSAndroid Build Coastguard Worker 	/* List used to track full slabs. */
75*1208bc7eSAndroid Build Coastguard Worker 	extent_list_t		slabs_full;
76*1208bc7eSAndroid Build Coastguard Worker 
77*1208bc7eSAndroid Build Coastguard Worker 	/* Bin statistics. */
78*1208bc7eSAndroid Build Coastguard Worker 	bin_stats_t	stats;
79*1208bc7eSAndroid Build Coastguard Worker };
80*1208bc7eSAndroid Build Coastguard Worker 
81*1208bc7eSAndroid Build Coastguard Worker /* Initializes a bin to empty.  Returns true on error. */
82*1208bc7eSAndroid Build Coastguard Worker bool bin_init(bin_t *bin);
83*1208bc7eSAndroid Build Coastguard Worker 
84*1208bc7eSAndroid Build Coastguard Worker /* Forking. */
85*1208bc7eSAndroid Build Coastguard Worker void bin_prefork(tsdn_t *tsdn, bin_t *bin);
86*1208bc7eSAndroid Build Coastguard Worker void bin_postfork_parent(tsdn_t *tsdn, bin_t *bin);
87*1208bc7eSAndroid Build Coastguard Worker void bin_postfork_child(tsdn_t *tsdn, bin_t *bin);
88*1208bc7eSAndroid Build Coastguard Worker 
89*1208bc7eSAndroid Build Coastguard Worker /* Stats. */
90*1208bc7eSAndroid Build Coastguard Worker static inline void
bin_stats_merge(tsdn_t * tsdn,bin_stats_t * dst_bin_stats,bin_t * bin)91*1208bc7eSAndroid Build Coastguard Worker bin_stats_merge(tsdn_t *tsdn, bin_stats_t *dst_bin_stats, bin_t *bin) {
92*1208bc7eSAndroid Build Coastguard Worker 	malloc_mutex_lock(tsdn, &bin->lock);
93*1208bc7eSAndroid Build Coastguard Worker 	malloc_mutex_prof_read(tsdn, &dst_bin_stats->mutex_data, &bin->lock);
94*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->nmalloc += bin->stats.nmalloc;
95*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->ndalloc += bin->stats.ndalloc;
96*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
97*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->nrequests += bin->stats.nrequests;
98*1208bc7eSAndroid Build Coastguard Worker #endif
99*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->curregs += bin->stats.curregs;
100*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
101*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->nfills += bin->stats.nfills;
102*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->nflushes += bin->stats.nflushes;
103*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->nslabs += bin->stats.nslabs;
104*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->reslabs += bin->stats.reslabs;
105*1208bc7eSAndroid Build Coastguard Worker 	dst_bin_stats->curslabs += bin->stats.curslabs;
106*1208bc7eSAndroid Build Coastguard Worker #endif
107*1208bc7eSAndroid Build Coastguard Worker 	malloc_mutex_unlock(tsdn, &bin->lock);
108*1208bc7eSAndroid Build Coastguard Worker }
109*1208bc7eSAndroid Build Coastguard Worker 
110*1208bc7eSAndroid Build Coastguard Worker #endif /* JEMALLOC_INTERNAL_BIN_H */
111