1*1208bc7eSAndroid Build Coastguard Worker #define JEMALLOC_ARENA_C_
2*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/jemalloc_preamble.h"
3*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/jemalloc_internal_includes.h"
4*1208bc7eSAndroid Build Coastguard Worker
5*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/assert.h"
6*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/div.h"
7*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/extent_dss.h"
8*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/extent_mmap.h"
9*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/mutex.h"
10*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/rtree.h"
11*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/size_classes.h"
12*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/util.h"
13*1208bc7eSAndroid Build Coastguard Worker
14*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
15*1208bc7eSAndroid Build Coastguard Worker /* Data. */
16*1208bc7eSAndroid Build Coastguard Worker
17*1208bc7eSAndroid Build Coastguard Worker /*
18*1208bc7eSAndroid Build Coastguard Worker * Define names for both unininitialized and initialized phases, so that
19*1208bc7eSAndroid Build Coastguard Worker * options and mallctl processing are straightforward.
20*1208bc7eSAndroid Build Coastguard Worker */
21*1208bc7eSAndroid Build Coastguard Worker const char *percpu_arena_mode_names[] = {
22*1208bc7eSAndroid Build Coastguard Worker "percpu",
23*1208bc7eSAndroid Build Coastguard Worker "phycpu",
24*1208bc7eSAndroid Build Coastguard Worker "disabled",
25*1208bc7eSAndroid Build Coastguard Worker "percpu",
26*1208bc7eSAndroid Build Coastguard Worker "phycpu"
27*1208bc7eSAndroid Build Coastguard Worker };
28*1208bc7eSAndroid Build Coastguard Worker percpu_arena_mode_t opt_percpu_arena = PERCPU_ARENA_DEFAULT;
29*1208bc7eSAndroid Build Coastguard Worker
30*1208bc7eSAndroid Build Coastguard Worker ssize_t opt_dirty_decay_ms = DIRTY_DECAY_MS_DEFAULT;
31*1208bc7eSAndroid Build Coastguard Worker ssize_t opt_muzzy_decay_ms = MUZZY_DECAY_MS_DEFAULT;
32*1208bc7eSAndroid Build Coastguard Worker
33*1208bc7eSAndroid Build Coastguard Worker static atomic_zd_t dirty_decay_ms_default;
34*1208bc7eSAndroid Build Coastguard Worker static atomic_zd_t muzzy_decay_ms_default;
35*1208bc7eSAndroid Build Coastguard Worker
36*1208bc7eSAndroid Build Coastguard Worker const uint64_t h_steps[SMOOTHSTEP_NSTEPS] = {
37*1208bc7eSAndroid Build Coastguard Worker #define STEP(step, h, x, y) \
38*1208bc7eSAndroid Build Coastguard Worker h,
39*1208bc7eSAndroid Build Coastguard Worker SMOOTHSTEP
40*1208bc7eSAndroid Build Coastguard Worker #undef STEP
41*1208bc7eSAndroid Build Coastguard Worker };
42*1208bc7eSAndroid Build Coastguard Worker
43*1208bc7eSAndroid Build Coastguard Worker static div_info_t arena_binind_div_info[NBINS];
44*1208bc7eSAndroid Build Coastguard Worker
45*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
46*1208bc7eSAndroid Build Coastguard Worker /*
47*1208bc7eSAndroid Build Coastguard Worker * Function prototypes for static functions that are referenced prior to
48*1208bc7eSAndroid Build Coastguard Worker * definition.
49*1208bc7eSAndroid Build Coastguard Worker */
50*1208bc7eSAndroid Build Coastguard Worker
51*1208bc7eSAndroid Build Coastguard Worker static void arena_decay_to_limit(tsdn_t *tsdn, arena_t *arena,
52*1208bc7eSAndroid Build Coastguard Worker arena_decay_t *decay, extents_t *extents, bool all, size_t npages_limit,
53*1208bc7eSAndroid Build Coastguard Worker size_t npages_decay_max, bool is_background_thread);
54*1208bc7eSAndroid Build Coastguard Worker static bool arena_decay_dirty(tsdn_t *tsdn, arena_t *arena,
55*1208bc7eSAndroid Build Coastguard Worker bool is_background_thread, bool all);
56*1208bc7eSAndroid Build Coastguard Worker static void arena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,
57*1208bc7eSAndroid Build Coastguard Worker bin_t *bin);
58*1208bc7eSAndroid Build Coastguard Worker static void arena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,
59*1208bc7eSAndroid Build Coastguard Worker bin_t *bin);
60*1208bc7eSAndroid Build Coastguard Worker
61*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
62*1208bc7eSAndroid Build Coastguard Worker
63*1208bc7eSAndroid Build Coastguard Worker void
arena_basic_stats_merge(UNUSED tsdn_t * tsdn,arena_t * arena,unsigned * nthreads,const char ** dss,ssize_t * dirty_decay_ms,ssize_t * muzzy_decay_ms,size_t * nactive,size_t * ndirty,size_t * nmuzzy)64*1208bc7eSAndroid Build Coastguard Worker arena_basic_stats_merge(UNUSED tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
65*1208bc7eSAndroid Build Coastguard Worker const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,
66*1208bc7eSAndroid Build Coastguard Worker size_t *nactive, size_t *ndirty, size_t *nmuzzy) {
67*1208bc7eSAndroid Build Coastguard Worker *nthreads += arena_nthreads_get(arena, false);
68*1208bc7eSAndroid Build Coastguard Worker *dss = dss_prec_names[arena_dss_prec_get(arena)];
69*1208bc7eSAndroid Build Coastguard Worker *dirty_decay_ms = arena_dirty_decay_ms_get(arena);
70*1208bc7eSAndroid Build Coastguard Worker *muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);
71*1208bc7eSAndroid Build Coastguard Worker *nactive += atomic_load_zu(&arena->nactive, ATOMIC_RELAXED);
72*1208bc7eSAndroid Build Coastguard Worker *ndirty += extents_npages_get(&arena->extents_dirty);
73*1208bc7eSAndroid Build Coastguard Worker *nmuzzy += extents_npages_get(&arena->extents_muzzy);
74*1208bc7eSAndroid Build Coastguard Worker }
75*1208bc7eSAndroid Build Coastguard Worker
76*1208bc7eSAndroid Build Coastguard Worker void
arena_stats_merge(tsdn_t * tsdn,arena_t * arena,unsigned * nthreads,const char ** dss,ssize_t * dirty_decay_ms,ssize_t * muzzy_decay_ms,size_t * nactive,size_t * ndirty,size_t * nmuzzy,arena_stats_t * astats,bin_stats_t * bstats,arena_stats_large_t * lstats)77*1208bc7eSAndroid Build Coastguard Worker arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
78*1208bc7eSAndroid Build Coastguard Worker const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,
79*1208bc7eSAndroid Build Coastguard Worker size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,
80*1208bc7eSAndroid Build Coastguard Worker bin_stats_t *bstats, arena_stats_large_t *lstats) {
81*1208bc7eSAndroid Build Coastguard Worker cassert(config_stats);
82*1208bc7eSAndroid Build Coastguard Worker
83*1208bc7eSAndroid Build Coastguard Worker arena_basic_stats_merge(tsdn, arena, nthreads, dss, dirty_decay_ms,
84*1208bc7eSAndroid Build Coastguard Worker muzzy_decay_ms, nactive, ndirty, nmuzzy);
85*1208bc7eSAndroid Build Coastguard Worker
86*1208bc7eSAndroid Build Coastguard Worker size_t base_allocated, base_resident, base_mapped, metadata_thp;
87*1208bc7eSAndroid Build Coastguard Worker base_stats_get(tsdn, arena->base, &base_allocated, &base_resident,
88*1208bc7eSAndroid Build Coastguard Worker &base_mapped, &metadata_thp);
89*1208bc7eSAndroid Build Coastguard Worker
90*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
91*1208bc7eSAndroid Build Coastguard Worker
92*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->mapped, base_mapped
93*1208bc7eSAndroid Build Coastguard Worker + arena_stats_read_zu(tsdn, &arena->stats, &arena->stats.mapped));
94*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
95*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->retained,
96*1208bc7eSAndroid Build Coastguard Worker extents_npages_get(&arena->extents_retained) << LG_PAGE);
97*1208bc7eSAndroid Build Coastguard Worker
98*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_dirty.npurge,
99*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
100*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_dirty.npurge));
101*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_dirty.nmadvise,
102*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
103*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_dirty.nmadvise));
104*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_dirty.purged,
105*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
106*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_dirty.purged));
107*1208bc7eSAndroid Build Coastguard Worker
108*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_muzzy.npurge,
109*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
110*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_muzzy.npurge));
111*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_muzzy.nmadvise,
112*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
113*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_muzzy.nmadvise));
114*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->decay_muzzy.purged,
115*1208bc7eSAndroid Build Coastguard Worker arena_stats_read_u64(tsdn, &arena->stats,
116*1208bc7eSAndroid Build Coastguard Worker &arena->stats.decay_muzzy.purged));
117*1208bc7eSAndroid Build Coastguard Worker
118*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->base, base_allocated);
119*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->internal, arena_internal_get(arena));
120*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->metadata_thp, metadata_thp);
121*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->resident, base_resident +
122*1208bc7eSAndroid Build Coastguard Worker (((atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) +
123*1208bc7eSAndroid Build Coastguard Worker extents_npages_get(&arena->extents_dirty) +
124*1208bc7eSAndroid Build Coastguard Worker extents_npages_get(&arena->extents_muzzy)) << LG_PAGE)));
125*1208bc7eSAndroid Build Coastguard Worker #endif
126*1208bc7eSAndroid Build Coastguard Worker
127*1208bc7eSAndroid Build Coastguard Worker for (szind_t i = 0; i < NSIZES - NBINS; i++) {
128*1208bc7eSAndroid Build Coastguard Worker uint64_t nmalloc = arena_stats_read_u64(tsdn, &arena->stats,
129*1208bc7eSAndroid Build Coastguard Worker &arena->stats.lstats[i].nmalloc);
130*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&lstats[i].nmalloc, nmalloc);
131*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
132*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->nmalloc_large, nmalloc);
133*1208bc7eSAndroid Build Coastguard Worker #endif
134*1208bc7eSAndroid Build Coastguard Worker
135*1208bc7eSAndroid Build Coastguard Worker uint64_t ndalloc = arena_stats_read_u64(tsdn, &arena->stats,
136*1208bc7eSAndroid Build Coastguard Worker &arena->stats.lstats[i].ndalloc);
137*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&lstats[i].ndalloc, ndalloc);
138*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
139*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->ndalloc_large, ndalloc);
140*1208bc7eSAndroid Build Coastguard Worker #endif
141*1208bc7eSAndroid Build Coastguard Worker
142*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
143*1208bc7eSAndroid Build Coastguard Worker uint64_t nrequests = arena_stats_read_u64(tsdn, &arena->stats,
144*1208bc7eSAndroid Build Coastguard Worker &arena->stats.lstats[i].nrequests);
145*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&lstats[i].nrequests,
146*1208bc7eSAndroid Build Coastguard Worker nmalloc + nrequests);
147*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_u64(&astats->nrequests_large,
148*1208bc7eSAndroid Build Coastguard Worker nmalloc + nrequests);
149*1208bc7eSAndroid Build Coastguard Worker #endif
150*1208bc7eSAndroid Build Coastguard Worker
151*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
152*1208bc7eSAndroid Build Coastguard Worker assert(nmalloc >= ndalloc);
153*1208bc7eSAndroid Build Coastguard Worker assert(nmalloc - ndalloc <= SIZE_T_MAX);
154*1208bc7eSAndroid Build Coastguard Worker size_t curlextents = (size_t)(nmalloc - ndalloc);
155*1208bc7eSAndroid Build Coastguard Worker lstats[i].curlextents += curlextents;
156*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->allocated_large,
157*1208bc7eSAndroid Build Coastguard Worker curlextents * sz_index2size(NBINS + i));
158*1208bc7eSAndroid Build Coastguard Worker #endif
159*1208bc7eSAndroid Build Coastguard Worker }
160*1208bc7eSAndroid Build Coastguard Worker
161*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
162*1208bc7eSAndroid Build Coastguard Worker
163*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS) && defined(ANDROID_ENABLE_TCACHE)
164*1208bc7eSAndroid Build Coastguard Worker /* tcache_bytes counts currently cached bytes. */
165*1208bc7eSAndroid Build Coastguard Worker atomic_store_zu(&astats->tcache_bytes, 0, ATOMIC_RELAXED);
166*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
167*1208bc7eSAndroid Build Coastguard Worker cache_bin_array_descriptor_t *descriptor;
168*1208bc7eSAndroid Build Coastguard Worker ql_foreach(descriptor, &arena->cache_bin_array_descriptor_ql, link) {
169*1208bc7eSAndroid Build Coastguard Worker szind_t i = 0;
170*1208bc7eSAndroid Build Coastguard Worker for (; i < NBINS; i++) {
171*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = &descriptor->bins_small[i];
172*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->tcache_bytes,
173*1208bc7eSAndroid Build Coastguard Worker tbin->ncached * sz_index2size(i));
174*1208bc7eSAndroid Build Coastguard Worker }
175*1208bc7eSAndroid Build Coastguard Worker for (; i < nhbins; i++) {
176*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = &descriptor->bins_large[i];
177*1208bc7eSAndroid Build Coastguard Worker arena_stats_accum_zu(&astats->tcache_bytes,
178*1208bc7eSAndroid Build Coastguard Worker tbin->ncached * sz_index2size(i));
179*1208bc7eSAndroid Build Coastguard Worker }
180*1208bc7eSAndroid Build Coastguard Worker }
181*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prof_read(tsdn,
182*1208bc7eSAndroid Build Coastguard Worker &astats->mutex_prof_data[arena_prof_mutex_tcache_list],
183*1208bc7eSAndroid Build Coastguard Worker &arena->tcache_ql_mtx);
184*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
185*1208bc7eSAndroid Build Coastguard Worker #endif
186*1208bc7eSAndroid Build Coastguard Worker
187*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
188*1208bc7eSAndroid Build Coastguard Worker #define READ_ARENA_MUTEX_PROF_DATA(mtx, ind) \
189*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->mtx); \
190*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prof_read(tsdn, &astats->mutex_prof_data[ind], \
191*1208bc7eSAndroid Build Coastguard Worker &arena->mtx); \
192*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->mtx);
193*1208bc7eSAndroid Build Coastguard Worker
194*1208bc7eSAndroid Build Coastguard Worker /* Gather per arena mutex profiling data. */
195*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(large_mtx, arena_prof_mutex_large);
196*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(extent_avail_mtx,
197*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_extent_avail)
198*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(extents_dirty.mtx,
199*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_extents_dirty)
200*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(extents_muzzy.mtx,
201*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_extents_muzzy)
202*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(extents_retained.mtx,
203*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_extents_retained)
204*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(decay_dirty.mtx,
205*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_decay_dirty)
206*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(decay_muzzy.mtx,
207*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_decay_muzzy)
208*1208bc7eSAndroid Build Coastguard Worker READ_ARENA_MUTEX_PROF_DATA(base->mtx,
209*1208bc7eSAndroid Build Coastguard Worker arena_prof_mutex_base)
210*1208bc7eSAndroid Build Coastguard Worker #undef READ_ARENA_MUTEX_PROF_DATA
211*1208bc7eSAndroid Build Coastguard Worker #endif
212*1208bc7eSAndroid Build Coastguard Worker
213*1208bc7eSAndroid Build Coastguard Worker nstime_copy(&astats->uptime, &arena->create_time);
214*1208bc7eSAndroid Build Coastguard Worker nstime_update(&astats->uptime);
215*1208bc7eSAndroid Build Coastguard Worker nstime_subtract(&astats->uptime, &arena->create_time);
216*1208bc7eSAndroid Build Coastguard Worker
217*1208bc7eSAndroid Build Coastguard Worker for (szind_t i = 0; i < NBINS; i++) {
218*1208bc7eSAndroid Build Coastguard Worker bin_stats_merge(tsdn, &bstats[i], &arena->bins[i]);
219*1208bc7eSAndroid Build Coastguard Worker }
220*1208bc7eSAndroid Build Coastguard Worker }
221*1208bc7eSAndroid Build Coastguard Worker
222*1208bc7eSAndroid Build Coastguard Worker void
arena_extents_dirty_dalloc(tsdn_t * tsdn,arena_t * arena,extent_hooks_t ** r_extent_hooks,extent_t * extent)223*1208bc7eSAndroid Build Coastguard Worker arena_extents_dirty_dalloc(tsdn_t *tsdn, arena_t *arena,
224*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t **r_extent_hooks, extent_t *extent) {
225*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
226*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 0);
227*1208bc7eSAndroid Build Coastguard Worker
228*1208bc7eSAndroid Build Coastguard Worker extents_dalloc(tsdn, arena, r_extent_hooks, &arena->extents_dirty,
229*1208bc7eSAndroid Build Coastguard Worker extent);
230*1208bc7eSAndroid Build Coastguard Worker if (arena_dirty_decay_ms_get(arena) == 0) {
231*1208bc7eSAndroid Build Coastguard Worker arena_decay_dirty(tsdn, arena, false, true);
232*1208bc7eSAndroid Build Coastguard Worker } else {
233*1208bc7eSAndroid Build Coastguard Worker arena_background_thread_inactivity_check(tsdn, arena, false);
234*1208bc7eSAndroid Build Coastguard Worker }
235*1208bc7eSAndroid Build Coastguard Worker }
236*1208bc7eSAndroid Build Coastguard Worker
237*1208bc7eSAndroid Build Coastguard Worker static void *
arena_slab_reg_alloc(extent_t * slab,const bin_info_t * bin_info)238*1208bc7eSAndroid Build Coastguard Worker arena_slab_reg_alloc(extent_t *slab, const bin_info_t *bin_info) {
239*1208bc7eSAndroid Build Coastguard Worker void *ret;
240*1208bc7eSAndroid Build Coastguard Worker arena_slab_data_t *slab_data = extent_slab_data_get(slab);
241*1208bc7eSAndroid Build Coastguard Worker size_t regind;
242*1208bc7eSAndroid Build Coastguard Worker
243*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(slab) > 0);
244*1208bc7eSAndroid Build Coastguard Worker assert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));
245*1208bc7eSAndroid Build Coastguard Worker
246*1208bc7eSAndroid Build Coastguard Worker regind = bitmap_sfu(slab_data->bitmap, &bin_info->bitmap_info);
247*1208bc7eSAndroid Build Coastguard Worker ret = (void *)((uintptr_t)extent_addr_get(slab) +
248*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)(bin_info->reg_size * regind));
249*1208bc7eSAndroid Build Coastguard Worker extent_nfree_dec(slab);
250*1208bc7eSAndroid Build Coastguard Worker return ret;
251*1208bc7eSAndroid Build Coastguard Worker }
252*1208bc7eSAndroid Build Coastguard Worker
253*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_JET
254*1208bc7eSAndroid Build Coastguard Worker static
255*1208bc7eSAndroid Build Coastguard Worker #endif
256*1208bc7eSAndroid Build Coastguard Worker size_t
arena_slab_regind(extent_t * slab,szind_t binind,const void * ptr)257*1208bc7eSAndroid Build Coastguard Worker arena_slab_regind(extent_t *slab, szind_t binind, const void *ptr) {
258*1208bc7eSAndroid Build Coastguard Worker size_t diff, regind;
259*1208bc7eSAndroid Build Coastguard Worker
260*1208bc7eSAndroid Build Coastguard Worker /* Freeing a pointer outside the slab can cause assertion failure. */
261*1208bc7eSAndroid Build Coastguard Worker assert((uintptr_t)ptr >= (uintptr_t)extent_addr_get(slab));
262*1208bc7eSAndroid Build Coastguard Worker assert((uintptr_t)ptr < (uintptr_t)extent_past_get(slab));
263*1208bc7eSAndroid Build Coastguard Worker /* Freeing an interior pointer can cause assertion failure. */
264*1208bc7eSAndroid Build Coastguard Worker assert(((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab)) %
265*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)bin_infos[binind].reg_size == 0);
266*1208bc7eSAndroid Build Coastguard Worker
267*1208bc7eSAndroid Build Coastguard Worker diff = (size_t)((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab));
268*1208bc7eSAndroid Build Coastguard Worker
269*1208bc7eSAndroid Build Coastguard Worker /* Avoid doing division with a variable divisor. */
270*1208bc7eSAndroid Build Coastguard Worker regind = div_compute(&arena_binind_div_info[binind], diff);
271*1208bc7eSAndroid Build Coastguard Worker
272*1208bc7eSAndroid Build Coastguard Worker assert(regind < bin_infos[binind].nregs);
273*1208bc7eSAndroid Build Coastguard Worker
274*1208bc7eSAndroid Build Coastguard Worker return regind;
275*1208bc7eSAndroid Build Coastguard Worker }
276*1208bc7eSAndroid Build Coastguard Worker
277*1208bc7eSAndroid Build Coastguard Worker static void
arena_slab_reg_dalloc(extent_t * slab,arena_slab_data_t * slab_data,void * ptr)278*1208bc7eSAndroid Build Coastguard Worker arena_slab_reg_dalloc(extent_t *slab, arena_slab_data_t *slab_data, void *ptr) {
279*1208bc7eSAndroid Build Coastguard Worker szind_t binind = extent_szind_get(slab);
280*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info = &bin_infos[binind];
281*1208bc7eSAndroid Build Coastguard Worker size_t regind = arena_slab_regind(slab, binind, ptr);
282*1208bc7eSAndroid Build Coastguard Worker
283*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(slab) < bin_info->nregs);
284*1208bc7eSAndroid Build Coastguard Worker /* Freeing an unallocated pointer can cause assertion failure. */
285*1208bc7eSAndroid Build Coastguard Worker assert(bitmap_get(slab_data->bitmap, &bin_info->bitmap_info, regind));
286*1208bc7eSAndroid Build Coastguard Worker
287*1208bc7eSAndroid Build Coastguard Worker bitmap_unset(slab_data->bitmap, &bin_info->bitmap_info, regind);
288*1208bc7eSAndroid Build Coastguard Worker extent_nfree_inc(slab);
289*1208bc7eSAndroid Build Coastguard Worker }
290*1208bc7eSAndroid Build Coastguard Worker
291*1208bc7eSAndroid Build Coastguard Worker static void
arena_nactive_add(arena_t * arena,size_t add_pages)292*1208bc7eSAndroid Build Coastguard Worker arena_nactive_add(arena_t *arena, size_t add_pages) {
293*1208bc7eSAndroid Build Coastguard Worker atomic_fetch_add_zu(&arena->nactive, add_pages, ATOMIC_RELAXED);
294*1208bc7eSAndroid Build Coastguard Worker }
295*1208bc7eSAndroid Build Coastguard Worker
296*1208bc7eSAndroid Build Coastguard Worker static void
arena_nactive_sub(arena_t * arena,size_t sub_pages)297*1208bc7eSAndroid Build Coastguard Worker arena_nactive_sub(arena_t *arena, size_t sub_pages) {
298*1208bc7eSAndroid Build Coastguard Worker assert(atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) >= sub_pages);
299*1208bc7eSAndroid Build Coastguard Worker atomic_fetch_sub_zu(&arena->nactive, sub_pages, ATOMIC_RELAXED);
300*1208bc7eSAndroid Build Coastguard Worker }
301*1208bc7eSAndroid Build Coastguard Worker
302*1208bc7eSAndroid Build Coastguard Worker static void
arena_large_malloc_stats_update(tsdn_t * tsdn,arena_t * arena,size_t usize)303*1208bc7eSAndroid Build Coastguard Worker arena_large_malloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
304*1208bc7eSAndroid Build Coastguard Worker szind_t index, hindex;
305*1208bc7eSAndroid Build Coastguard Worker
306*1208bc7eSAndroid Build Coastguard Worker cassert(config_stats);
307*1208bc7eSAndroid Build Coastguard Worker
308*1208bc7eSAndroid Build Coastguard Worker if (usize < LARGE_MINCLASS) {
309*1208bc7eSAndroid Build Coastguard Worker usize = LARGE_MINCLASS;
310*1208bc7eSAndroid Build Coastguard Worker }
311*1208bc7eSAndroid Build Coastguard Worker index = sz_size2index(usize);
312*1208bc7eSAndroid Build Coastguard Worker hindex = (index >= NBINS) ? index - NBINS : 0;
313*1208bc7eSAndroid Build Coastguard Worker
314*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_u64(tsdn, &arena->stats,
315*1208bc7eSAndroid Build Coastguard Worker &arena->stats.lstats[hindex].nmalloc, 1);
316*1208bc7eSAndroid Build Coastguard Worker }
317*1208bc7eSAndroid Build Coastguard Worker
318*1208bc7eSAndroid Build Coastguard Worker static void
arena_large_dalloc_stats_update(tsdn_t * tsdn,arena_t * arena,size_t usize)319*1208bc7eSAndroid Build Coastguard Worker arena_large_dalloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
320*1208bc7eSAndroid Build Coastguard Worker szind_t index, hindex;
321*1208bc7eSAndroid Build Coastguard Worker
322*1208bc7eSAndroid Build Coastguard Worker cassert(config_stats);
323*1208bc7eSAndroid Build Coastguard Worker
324*1208bc7eSAndroid Build Coastguard Worker if (usize < LARGE_MINCLASS) {
325*1208bc7eSAndroid Build Coastguard Worker usize = LARGE_MINCLASS;
326*1208bc7eSAndroid Build Coastguard Worker }
327*1208bc7eSAndroid Build Coastguard Worker index = sz_size2index(usize);
328*1208bc7eSAndroid Build Coastguard Worker hindex = (index >= NBINS) ? index - NBINS : 0;
329*1208bc7eSAndroid Build Coastguard Worker
330*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_u64(tsdn, &arena->stats,
331*1208bc7eSAndroid Build Coastguard Worker &arena->stats.lstats[hindex].ndalloc, 1);
332*1208bc7eSAndroid Build Coastguard Worker }
333*1208bc7eSAndroid Build Coastguard Worker
334*1208bc7eSAndroid Build Coastguard Worker static void
arena_large_ralloc_stats_update(tsdn_t * tsdn,arena_t * arena,size_t oldusize,size_t usize)335*1208bc7eSAndroid Build Coastguard Worker arena_large_ralloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t oldusize,
336*1208bc7eSAndroid Build Coastguard Worker size_t usize) {
337*1208bc7eSAndroid Build Coastguard Worker arena_large_dalloc_stats_update(tsdn, arena, oldusize);
338*1208bc7eSAndroid Build Coastguard Worker arena_large_malloc_stats_update(tsdn, arena, usize);
339*1208bc7eSAndroid Build Coastguard Worker }
340*1208bc7eSAndroid Build Coastguard Worker
341*1208bc7eSAndroid Build Coastguard Worker extent_t *
arena_extent_alloc_large(tsdn_t * tsdn,arena_t * arena,size_t usize,size_t alignment,bool * zero)342*1208bc7eSAndroid Build Coastguard Worker arena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena, size_t usize,
343*1208bc7eSAndroid Build Coastguard Worker size_t alignment, bool *zero) {
344*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;
345*1208bc7eSAndroid Build Coastguard Worker
346*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
347*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 0);
348*1208bc7eSAndroid Build Coastguard Worker
349*1208bc7eSAndroid Build Coastguard Worker szind_t szind = sz_size2index(usize);
350*1208bc7eSAndroid Build Coastguard Worker size_t mapped_add;
351*1208bc7eSAndroid Build Coastguard Worker bool commit = true;
352*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = extents_alloc(tsdn, arena, &extent_hooks,
353*1208bc7eSAndroid Build Coastguard Worker &arena->extents_dirty, NULL, usize, sz_large_pad, alignment, false,
354*1208bc7eSAndroid Build Coastguard Worker szind, zero, &commit);
355*1208bc7eSAndroid Build Coastguard Worker if (extent == NULL) {
356*1208bc7eSAndroid Build Coastguard Worker extent = extents_alloc(tsdn, arena, &extent_hooks,
357*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, NULL, usize, sz_large_pad, alignment,
358*1208bc7eSAndroid Build Coastguard Worker false, szind, zero, &commit);
359*1208bc7eSAndroid Build Coastguard Worker }
360*1208bc7eSAndroid Build Coastguard Worker size_t size = usize + sz_large_pad;
361*1208bc7eSAndroid Build Coastguard Worker if (extent == NULL) {
362*1208bc7eSAndroid Build Coastguard Worker extent = extent_alloc_wrapper(tsdn, arena, &extent_hooks, NULL,
363*1208bc7eSAndroid Build Coastguard Worker usize, sz_large_pad, alignment, false, szind, zero,
364*1208bc7eSAndroid Build Coastguard Worker &commit);
365*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
366*1208bc7eSAndroid Build Coastguard Worker /*
367*1208bc7eSAndroid Build Coastguard Worker * extent may be NULL on OOM, but in that case
368*1208bc7eSAndroid Build Coastguard Worker * mapped_add isn't used below, so there's no need to
369*1208bc7eSAndroid Build Coastguard Worker * conditionlly set it to 0 here.
370*1208bc7eSAndroid Build Coastguard Worker */
371*1208bc7eSAndroid Build Coastguard Worker mapped_add = size;
372*1208bc7eSAndroid Build Coastguard Worker }
373*1208bc7eSAndroid Build Coastguard Worker } else if (config_stats) {
374*1208bc7eSAndroid Build Coastguard Worker mapped_add = 0;
375*1208bc7eSAndroid Build Coastguard Worker }
376*1208bc7eSAndroid Build Coastguard Worker
377*1208bc7eSAndroid Build Coastguard Worker if (extent != NULL) {
378*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
379*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
380*1208bc7eSAndroid Build Coastguard Worker arena_large_malloc_stats_update(tsdn, arena, usize);
381*1208bc7eSAndroid Build Coastguard Worker if (mapped_add != 0) {
382*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_zu(tsdn, &arena->stats,
383*1208bc7eSAndroid Build Coastguard Worker &arena->stats.mapped, mapped_add);
384*1208bc7eSAndroid Build Coastguard Worker }
385*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
386*1208bc7eSAndroid Build Coastguard Worker }
387*1208bc7eSAndroid Build Coastguard Worker arena_nactive_add(arena, size >> LG_PAGE);
388*1208bc7eSAndroid Build Coastguard Worker }
389*1208bc7eSAndroid Build Coastguard Worker
390*1208bc7eSAndroid Build Coastguard Worker return extent;
391*1208bc7eSAndroid Build Coastguard Worker }
392*1208bc7eSAndroid Build Coastguard Worker
393*1208bc7eSAndroid Build Coastguard Worker void
arena_extent_dalloc_large_prep(tsdn_t * tsdn,arena_t * arena,extent_t * extent)394*1208bc7eSAndroid Build Coastguard Worker arena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {
395*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
396*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
397*1208bc7eSAndroid Build Coastguard Worker arena_large_dalloc_stats_update(tsdn, arena,
398*1208bc7eSAndroid Build Coastguard Worker extent_usize_get(extent));
399*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
400*1208bc7eSAndroid Build Coastguard Worker }
401*1208bc7eSAndroid Build Coastguard Worker arena_nactive_sub(arena, extent_size_get(extent) >> LG_PAGE);
402*1208bc7eSAndroid Build Coastguard Worker }
403*1208bc7eSAndroid Build Coastguard Worker
404*1208bc7eSAndroid Build Coastguard Worker void
arena_extent_ralloc_large_shrink(tsdn_t * tsdn,arena_t * arena,extent_t * extent,size_t oldusize)405*1208bc7eSAndroid Build Coastguard Worker arena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena, extent_t *extent,
406*1208bc7eSAndroid Build Coastguard Worker size_t oldusize) {
407*1208bc7eSAndroid Build Coastguard Worker size_t usize = extent_usize_get(extent);
408*1208bc7eSAndroid Build Coastguard Worker size_t udiff = oldusize - usize;
409*1208bc7eSAndroid Build Coastguard Worker
410*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
411*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
412*1208bc7eSAndroid Build Coastguard Worker arena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);
413*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
414*1208bc7eSAndroid Build Coastguard Worker }
415*1208bc7eSAndroid Build Coastguard Worker arena_nactive_sub(arena, udiff >> LG_PAGE);
416*1208bc7eSAndroid Build Coastguard Worker }
417*1208bc7eSAndroid Build Coastguard Worker
418*1208bc7eSAndroid Build Coastguard Worker void
arena_extent_ralloc_large_expand(tsdn_t * tsdn,arena_t * arena,extent_t * extent,size_t oldusize)419*1208bc7eSAndroid Build Coastguard Worker arena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena, extent_t *extent,
420*1208bc7eSAndroid Build Coastguard Worker size_t oldusize) {
421*1208bc7eSAndroid Build Coastguard Worker size_t usize = extent_usize_get(extent);
422*1208bc7eSAndroid Build Coastguard Worker size_t udiff = usize - oldusize;
423*1208bc7eSAndroid Build Coastguard Worker
424*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
425*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
426*1208bc7eSAndroid Build Coastguard Worker arena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);
427*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
428*1208bc7eSAndroid Build Coastguard Worker }
429*1208bc7eSAndroid Build Coastguard Worker arena_nactive_add(arena, udiff >> LG_PAGE);
430*1208bc7eSAndroid Build Coastguard Worker }
431*1208bc7eSAndroid Build Coastguard Worker
432*1208bc7eSAndroid Build Coastguard Worker static ssize_t
arena_decay_ms_read(arena_decay_t * decay)433*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_read(arena_decay_t *decay) {
434*1208bc7eSAndroid Build Coastguard Worker return atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);
435*1208bc7eSAndroid Build Coastguard Worker }
436*1208bc7eSAndroid Build Coastguard Worker
437*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_ms_write(arena_decay_t * decay,ssize_t decay_ms)438*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_write(arena_decay_t *decay, ssize_t decay_ms) {
439*1208bc7eSAndroid Build Coastguard Worker atomic_store_zd(&decay->time_ms, decay_ms, ATOMIC_RELAXED);
440*1208bc7eSAndroid Build Coastguard Worker }
441*1208bc7eSAndroid Build Coastguard Worker
442*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_deadline_init(arena_decay_t * decay)443*1208bc7eSAndroid Build Coastguard Worker arena_decay_deadline_init(arena_decay_t *decay) {
444*1208bc7eSAndroid Build Coastguard Worker /*
445*1208bc7eSAndroid Build Coastguard Worker * Generate a new deadline that is uniformly random within the next
446*1208bc7eSAndroid Build Coastguard Worker * epoch after the current one.
447*1208bc7eSAndroid Build Coastguard Worker */
448*1208bc7eSAndroid Build Coastguard Worker nstime_copy(&decay->deadline, &decay->epoch);
449*1208bc7eSAndroid Build Coastguard Worker nstime_add(&decay->deadline, &decay->interval);
450*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_ms_read(decay) > 0) {
451*1208bc7eSAndroid Build Coastguard Worker nstime_t jitter;
452*1208bc7eSAndroid Build Coastguard Worker
453*1208bc7eSAndroid Build Coastguard Worker nstime_init(&jitter, prng_range_u64(&decay->jitter_state,
454*1208bc7eSAndroid Build Coastguard Worker nstime_ns(&decay->interval)));
455*1208bc7eSAndroid Build Coastguard Worker nstime_add(&decay->deadline, &jitter);
456*1208bc7eSAndroid Build Coastguard Worker }
457*1208bc7eSAndroid Build Coastguard Worker }
458*1208bc7eSAndroid Build Coastguard Worker
459*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_deadline_reached(const arena_decay_t * decay,const nstime_t * time)460*1208bc7eSAndroid Build Coastguard Worker arena_decay_deadline_reached(const arena_decay_t *decay, const nstime_t *time) {
461*1208bc7eSAndroid Build Coastguard Worker return (nstime_compare(&decay->deadline, time) <= 0);
462*1208bc7eSAndroid Build Coastguard Worker }
463*1208bc7eSAndroid Build Coastguard Worker
464*1208bc7eSAndroid Build Coastguard Worker static size_t
arena_decay_backlog_npages_limit(const arena_decay_t * decay)465*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_npages_limit(const arena_decay_t *decay) {
466*1208bc7eSAndroid Build Coastguard Worker uint64_t sum;
467*1208bc7eSAndroid Build Coastguard Worker size_t npages_limit_backlog;
468*1208bc7eSAndroid Build Coastguard Worker unsigned i;
469*1208bc7eSAndroid Build Coastguard Worker
470*1208bc7eSAndroid Build Coastguard Worker /*
471*1208bc7eSAndroid Build Coastguard Worker * For each element of decay_backlog, multiply by the corresponding
472*1208bc7eSAndroid Build Coastguard Worker * fixed-point smoothstep decay factor. Sum the products, then divide
473*1208bc7eSAndroid Build Coastguard Worker * to round down to the nearest whole number of pages.
474*1208bc7eSAndroid Build Coastguard Worker */
475*1208bc7eSAndroid Build Coastguard Worker sum = 0;
476*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {
477*1208bc7eSAndroid Build Coastguard Worker sum += decay->backlog[i] * h_steps[i];
478*1208bc7eSAndroid Build Coastguard Worker }
479*1208bc7eSAndroid Build Coastguard Worker npages_limit_backlog = (size_t)(sum >> SMOOTHSTEP_BFP);
480*1208bc7eSAndroid Build Coastguard Worker
481*1208bc7eSAndroid Build Coastguard Worker return npages_limit_backlog;
482*1208bc7eSAndroid Build Coastguard Worker }
483*1208bc7eSAndroid Build Coastguard Worker
484*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_backlog_update_last(arena_decay_t * decay,size_t current_npages)485*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_update_last(arena_decay_t *decay, size_t current_npages) {
486*1208bc7eSAndroid Build Coastguard Worker size_t npages_delta = (current_npages > decay->nunpurged) ?
487*1208bc7eSAndroid Build Coastguard Worker current_npages - decay->nunpurged : 0;
488*1208bc7eSAndroid Build Coastguard Worker decay->backlog[SMOOTHSTEP_NSTEPS-1] = npages_delta;
489*1208bc7eSAndroid Build Coastguard Worker
490*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
491*1208bc7eSAndroid Build Coastguard Worker if (config_debug) {
492*1208bc7eSAndroid Build Coastguard Worker if (current_npages > decay->ceil_npages) {
493*1208bc7eSAndroid Build Coastguard Worker decay->ceil_npages = current_npages;
494*1208bc7eSAndroid Build Coastguard Worker }
495*1208bc7eSAndroid Build Coastguard Worker size_t npages_limit = arena_decay_backlog_npages_limit(decay);
496*1208bc7eSAndroid Build Coastguard Worker assert(decay->ceil_npages >= npages_limit);
497*1208bc7eSAndroid Build Coastguard Worker if (decay->ceil_npages > npages_limit) {
498*1208bc7eSAndroid Build Coastguard Worker decay->ceil_npages = npages_limit;
499*1208bc7eSAndroid Build Coastguard Worker }
500*1208bc7eSAndroid Build Coastguard Worker }
501*1208bc7eSAndroid Build Coastguard Worker #endif
502*1208bc7eSAndroid Build Coastguard Worker }
503*1208bc7eSAndroid Build Coastguard Worker
504*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_backlog_update(arena_decay_t * decay,uint64_t nadvance_u64,size_t current_npages)505*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_update(arena_decay_t *decay, uint64_t nadvance_u64,
506*1208bc7eSAndroid Build Coastguard Worker size_t current_npages) {
507*1208bc7eSAndroid Build Coastguard Worker if (nadvance_u64 >= SMOOTHSTEP_NSTEPS) {
508*1208bc7eSAndroid Build Coastguard Worker memset(decay->backlog, 0, (SMOOTHSTEP_NSTEPS-1) *
509*1208bc7eSAndroid Build Coastguard Worker sizeof(size_t));
510*1208bc7eSAndroid Build Coastguard Worker } else {
511*1208bc7eSAndroid Build Coastguard Worker size_t nadvance_z = (size_t)nadvance_u64;
512*1208bc7eSAndroid Build Coastguard Worker
513*1208bc7eSAndroid Build Coastguard Worker assert((uint64_t)nadvance_z == nadvance_u64);
514*1208bc7eSAndroid Build Coastguard Worker
515*1208bc7eSAndroid Build Coastguard Worker memmove(decay->backlog, &decay->backlog[nadvance_z],
516*1208bc7eSAndroid Build Coastguard Worker (SMOOTHSTEP_NSTEPS - nadvance_z) * sizeof(size_t));
517*1208bc7eSAndroid Build Coastguard Worker if (nadvance_z > 1) {
518*1208bc7eSAndroid Build Coastguard Worker memset(&decay->backlog[SMOOTHSTEP_NSTEPS -
519*1208bc7eSAndroid Build Coastguard Worker nadvance_z], 0, (nadvance_z-1) * sizeof(size_t));
520*1208bc7eSAndroid Build Coastguard Worker }
521*1208bc7eSAndroid Build Coastguard Worker }
522*1208bc7eSAndroid Build Coastguard Worker
523*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_update_last(decay, current_npages);
524*1208bc7eSAndroid Build Coastguard Worker }
525*1208bc7eSAndroid Build Coastguard Worker
526*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_try_purge(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,size_t current_npages,size_t npages_limit,bool is_background_thread)527*1208bc7eSAndroid Build Coastguard Worker arena_decay_try_purge(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
528*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, size_t current_npages, size_t npages_limit,
529*1208bc7eSAndroid Build Coastguard Worker bool is_background_thread) {
530*1208bc7eSAndroid Build Coastguard Worker if (current_npages > npages_limit) {
531*1208bc7eSAndroid Build Coastguard Worker arena_decay_to_limit(tsdn, arena, decay, extents, false,
532*1208bc7eSAndroid Build Coastguard Worker npages_limit, current_npages - npages_limit,
533*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
534*1208bc7eSAndroid Build Coastguard Worker }
535*1208bc7eSAndroid Build Coastguard Worker }
536*1208bc7eSAndroid Build Coastguard Worker
537*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_epoch_advance_helper(arena_decay_t * decay,const nstime_t * time,size_t current_npages)538*1208bc7eSAndroid Build Coastguard Worker arena_decay_epoch_advance_helper(arena_decay_t *decay, const nstime_t *time,
539*1208bc7eSAndroid Build Coastguard Worker size_t current_npages) {
540*1208bc7eSAndroid Build Coastguard Worker assert(arena_decay_deadline_reached(decay, time));
541*1208bc7eSAndroid Build Coastguard Worker
542*1208bc7eSAndroid Build Coastguard Worker nstime_t delta;
543*1208bc7eSAndroid Build Coastguard Worker nstime_copy(&delta, time);
544*1208bc7eSAndroid Build Coastguard Worker nstime_subtract(&delta, &decay->epoch);
545*1208bc7eSAndroid Build Coastguard Worker
546*1208bc7eSAndroid Build Coastguard Worker uint64_t nadvance_u64 = nstime_divide(&delta, &decay->interval);
547*1208bc7eSAndroid Build Coastguard Worker assert(nadvance_u64 > 0);
548*1208bc7eSAndroid Build Coastguard Worker
549*1208bc7eSAndroid Build Coastguard Worker /* Add nadvance_u64 decay intervals to epoch. */
550*1208bc7eSAndroid Build Coastguard Worker nstime_copy(&delta, &decay->interval);
551*1208bc7eSAndroid Build Coastguard Worker nstime_imultiply(&delta, nadvance_u64);
552*1208bc7eSAndroid Build Coastguard Worker nstime_add(&decay->epoch, &delta);
553*1208bc7eSAndroid Build Coastguard Worker
554*1208bc7eSAndroid Build Coastguard Worker /* Set a new deadline. */
555*1208bc7eSAndroid Build Coastguard Worker arena_decay_deadline_init(decay);
556*1208bc7eSAndroid Build Coastguard Worker
557*1208bc7eSAndroid Build Coastguard Worker /* Update the backlog. */
558*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_update(decay, nadvance_u64, current_npages);
559*1208bc7eSAndroid Build Coastguard Worker }
560*1208bc7eSAndroid Build Coastguard Worker
561*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_epoch_advance(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,const nstime_t * time,bool is_background_thread)562*1208bc7eSAndroid Build Coastguard Worker arena_decay_epoch_advance(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
563*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, const nstime_t *time, bool is_background_thread) {
564*1208bc7eSAndroid Build Coastguard Worker size_t current_npages = extents_npages_get(extents);
565*1208bc7eSAndroid Build Coastguard Worker arena_decay_epoch_advance_helper(decay, time, current_npages);
566*1208bc7eSAndroid Build Coastguard Worker
567*1208bc7eSAndroid Build Coastguard Worker size_t npages_limit = arena_decay_backlog_npages_limit(decay);
568*1208bc7eSAndroid Build Coastguard Worker /* We may unlock decay->mtx when try_purge(). Finish logging first. */
569*1208bc7eSAndroid Build Coastguard Worker decay->nunpurged = (npages_limit > current_npages) ? npages_limit :
570*1208bc7eSAndroid Build Coastguard Worker current_npages;
571*1208bc7eSAndroid Build Coastguard Worker
572*1208bc7eSAndroid Build Coastguard Worker if (!background_thread_enabled() || is_background_thread) {
573*1208bc7eSAndroid Build Coastguard Worker arena_decay_try_purge(tsdn, arena, decay, extents,
574*1208bc7eSAndroid Build Coastguard Worker current_npages, npages_limit, is_background_thread);
575*1208bc7eSAndroid Build Coastguard Worker }
576*1208bc7eSAndroid Build Coastguard Worker }
577*1208bc7eSAndroid Build Coastguard Worker
578*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_reinit(arena_decay_t * decay,ssize_t decay_ms)579*1208bc7eSAndroid Build Coastguard Worker arena_decay_reinit(arena_decay_t *decay, ssize_t decay_ms) {
580*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_write(decay, decay_ms);
581*1208bc7eSAndroid Build Coastguard Worker if (decay_ms > 0) {
582*1208bc7eSAndroid Build Coastguard Worker nstime_init(&decay->interval, (uint64_t)decay_ms *
583*1208bc7eSAndroid Build Coastguard Worker KQU(1000000));
584*1208bc7eSAndroid Build Coastguard Worker nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS);
585*1208bc7eSAndroid Build Coastguard Worker }
586*1208bc7eSAndroid Build Coastguard Worker
587*1208bc7eSAndroid Build Coastguard Worker nstime_init(&decay->epoch, 0);
588*1208bc7eSAndroid Build Coastguard Worker nstime_update(&decay->epoch);
589*1208bc7eSAndroid Build Coastguard Worker decay->jitter_state = (uint64_t)(uintptr_t)decay;
590*1208bc7eSAndroid Build Coastguard Worker arena_decay_deadline_init(decay);
591*1208bc7eSAndroid Build Coastguard Worker decay->nunpurged = 0;
592*1208bc7eSAndroid Build Coastguard Worker memset(decay->backlog, 0, SMOOTHSTEP_NSTEPS * sizeof(size_t));
593*1208bc7eSAndroid Build Coastguard Worker }
594*1208bc7eSAndroid Build Coastguard Worker
595*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_init(arena_decay_t * decay,ssize_t decay_ms,arena_stats_decay_t * stats)596*1208bc7eSAndroid Build Coastguard Worker arena_decay_init(arena_decay_t *decay, ssize_t decay_ms,
597*1208bc7eSAndroid Build Coastguard Worker arena_stats_decay_t *stats) {
598*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
599*1208bc7eSAndroid Build Coastguard Worker if (config_debug) {
600*1208bc7eSAndroid Build Coastguard Worker for (size_t i = 0; i < sizeof(arena_decay_t); i++) {
601*1208bc7eSAndroid Build Coastguard Worker assert(((char *)decay)[i] == 0);
602*1208bc7eSAndroid Build Coastguard Worker }
603*1208bc7eSAndroid Build Coastguard Worker decay->ceil_npages = 0;
604*1208bc7eSAndroid Build Coastguard Worker }
605*1208bc7eSAndroid Build Coastguard Worker #endif
606*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&decay->mtx, "decay", WITNESS_RANK_DECAY,
607*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_rank_exclusive)) {
608*1208bc7eSAndroid Build Coastguard Worker return true;
609*1208bc7eSAndroid Build Coastguard Worker }
610*1208bc7eSAndroid Build Coastguard Worker decay->purging = false;
611*1208bc7eSAndroid Build Coastguard Worker arena_decay_reinit(decay, decay_ms);
612*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
613*1208bc7eSAndroid Build Coastguard Worker /* Memory is zeroed, so there is no need to clear stats. */
614*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
615*1208bc7eSAndroid Build Coastguard Worker decay->stats = stats;
616*1208bc7eSAndroid Build Coastguard Worker }
617*1208bc7eSAndroid Build Coastguard Worker #endif
618*1208bc7eSAndroid Build Coastguard Worker return false;
619*1208bc7eSAndroid Build Coastguard Worker }
620*1208bc7eSAndroid Build Coastguard Worker
621*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_ms_valid(ssize_t decay_ms)622*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_valid(ssize_t decay_ms) {
623*1208bc7eSAndroid Build Coastguard Worker if (decay_ms < -1) {
624*1208bc7eSAndroid Build Coastguard Worker return false;
625*1208bc7eSAndroid Build Coastguard Worker }
626*1208bc7eSAndroid Build Coastguard Worker if (decay_ms == -1 || (uint64_t)decay_ms <= NSTIME_SEC_MAX *
627*1208bc7eSAndroid Build Coastguard Worker KQU(1000)) {
628*1208bc7eSAndroid Build Coastguard Worker return true;
629*1208bc7eSAndroid Build Coastguard Worker }
630*1208bc7eSAndroid Build Coastguard Worker return false;
631*1208bc7eSAndroid Build Coastguard Worker }
632*1208bc7eSAndroid Build Coastguard Worker
633*1208bc7eSAndroid Build Coastguard Worker static bool
arena_maybe_decay(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,bool is_background_thread)634*1208bc7eSAndroid Build Coastguard Worker arena_maybe_decay(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
635*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, bool is_background_thread) {
636*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_assert_owner(tsdn, &decay->mtx);
637*1208bc7eSAndroid Build Coastguard Worker
638*1208bc7eSAndroid Build Coastguard Worker /* Purge all or nothing if the option is disabled. */
639*1208bc7eSAndroid Build Coastguard Worker ssize_t decay_ms = arena_decay_ms_read(decay);
640*1208bc7eSAndroid Build Coastguard Worker if (decay_ms <= 0) {
641*1208bc7eSAndroid Build Coastguard Worker if (decay_ms == 0) {
642*1208bc7eSAndroid Build Coastguard Worker arena_decay_to_limit(tsdn, arena, decay, extents, false,
643*1208bc7eSAndroid Build Coastguard Worker 0, extents_npages_get(extents),
644*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
645*1208bc7eSAndroid Build Coastguard Worker }
646*1208bc7eSAndroid Build Coastguard Worker return false;
647*1208bc7eSAndroid Build Coastguard Worker }
648*1208bc7eSAndroid Build Coastguard Worker
649*1208bc7eSAndroid Build Coastguard Worker nstime_t time;
650*1208bc7eSAndroid Build Coastguard Worker nstime_init(&time, 0);
651*1208bc7eSAndroid Build Coastguard Worker nstime_update(&time);
652*1208bc7eSAndroid Build Coastguard Worker if (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time)
653*1208bc7eSAndroid Build Coastguard Worker > 0)) {
654*1208bc7eSAndroid Build Coastguard Worker /*
655*1208bc7eSAndroid Build Coastguard Worker * Time went backwards. Move the epoch back in time and
656*1208bc7eSAndroid Build Coastguard Worker * generate a new deadline, with the expectation that time
657*1208bc7eSAndroid Build Coastguard Worker * typically flows forward for long enough periods of time that
658*1208bc7eSAndroid Build Coastguard Worker * epochs complete. Unfortunately, this strategy is susceptible
659*1208bc7eSAndroid Build Coastguard Worker * to clock jitter triggering premature epoch advances, but
660*1208bc7eSAndroid Build Coastguard Worker * clock jitter estimation and compensation isn't feasible here
661*1208bc7eSAndroid Build Coastguard Worker * because calls into this code are event-driven.
662*1208bc7eSAndroid Build Coastguard Worker */
663*1208bc7eSAndroid Build Coastguard Worker nstime_copy(&decay->epoch, &time);
664*1208bc7eSAndroid Build Coastguard Worker arena_decay_deadline_init(decay);
665*1208bc7eSAndroid Build Coastguard Worker } else {
666*1208bc7eSAndroid Build Coastguard Worker /* Verify that time does not go backwards. */
667*1208bc7eSAndroid Build Coastguard Worker assert(nstime_compare(&decay->epoch, &time) <= 0);
668*1208bc7eSAndroid Build Coastguard Worker }
669*1208bc7eSAndroid Build Coastguard Worker
670*1208bc7eSAndroid Build Coastguard Worker /*
671*1208bc7eSAndroid Build Coastguard Worker * If the deadline has been reached, advance to the current epoch and
672*1208bc7eSAndroid Build Coastguard Worker * purge to the new limit if necessary. Note that dirty pages created
673*1208bc7eSAndroid Build Coastguard Worker * during the current epoch are not subject to purge until a future
674*1208bc7eSAndroid Build Coastguard Worker * epoch, so as a result purging only happens during epoch advances, or
675*1208bc7eSAndroid Build Coastguard Worker * being triggered by background threads (scheduled event).
676*1208bc7eSAndroid Build Coastguard Worker */
677*1208bc7eSAndroid Build Coastguard Worker bool advance_epoch = arena_decay_deadline_reached(decay, &time);
678*1208bc7eSAndroid Build Coastguard Worker if (advance_epoch) {
679*1208bc7eSAndroid Build Coastguard Worker arena_decay_epoch_advance(tsdn, arena, decay, extents, &time,
680*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
681*1208bc7eSAndroid Build Coastguard Worker } else if (is_background_thread) {
682*1208bc7eSAndroid Build Coastguard Worker arena_decay_try_purge(tsdn, arena, decay, extents,
683*1208bc7eSAndroid Build Coastguard Worker extents_npages_get(extents),
684*1208bc7eSAndroid Build Coastguard Worker arena_decay_backlog_npages_limit(decay),
685*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
686*1208bc7eSAndroid Build Coastguard Worker }
687*1208bc7eSAndroid Build Coastguard Worker
688*1208bc7eSAndroid Build Coastguard Worker return advance_epoch;
689*1208bc7eSAndroid Build Coastguard Worker }
690*1208bc7eSAndroid Build Coastguard Worker
691*1208bc7eSAndroid Build Coastguard Worker static ssize_t
arena_decay_ms_get(arena_decay_t * decay)692*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_get(arena_decay_t *decay) {
693*1208bc7eSAndroid Build Coastguard Worker return arena_decay_ms_read(decay);
694*1208bc7eSAndroid Build Coastguard Worker }
695*1208bc7eSAndroid Build Coastguard Worker
696*1208bc7eSAndroid Build Coastguard Worker ssize_t
arena_dirty_decay_ms_get(arena_t * arena)697*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_get(arena_t *arena) {
698*1208bc7eSAndroid Build Coastguard Worker return arena_decay_ms_get(&arena->decay_dirty);
699*1208bc7eSAndroid Build Coastguard Worker }
700*1208bc7eSAndroid Build Coastguard Worker
701*1208bc7eSAndroid Build Coastguard Worker ssize_t
arena_muzzy_decay_ms_get(arena_t * arena)702*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_get(arena_t *arena) {
703*1208bc7eSAndroid Build Coastguard Worker return arena_decay_ms_get(&arena->decay_muzzy);
704*1208bc7eSAndroid Build Coastguard Worker }
705*1208bc7eSAndroid Build Coastguard Worker
706*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_ms_set(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,ssize_t decay_ms)707*1208bc7eSAndroid Build Coastguard Worker arena_decay_ms_set(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
708*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, ssize_t decay_ms) {
709*1208bc7eSAndroid Build Coastguard Worker if (!arena_decay_ms_valid(decay_ms)) {
710*1208bc7eSAndroid Build Coastguard Worker return true;
711*1208bc7eSAndroid Build Coastguard Worker }
712*1208bc7eSAndroid Build Coastguard Worker
713*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &decay->mtx);
714*1208bc7eSAndroid Build Coastguard Worker /*
715*1208bc7eSAndroid Build Coastguard Worker * Restart decay backlog from scratch, which may cause many dirty pages
716*1208bc7eSAndroid Build Coastguard Worker * to be immediately purged. It would conceptually be possible to map
717*1208bc7eSAndroid Build Coastguard Worker * the old backlog onto the new backlog, but there is no justification
718*1208bc7eSAndroid Build Coastguard Worker * for such complexity since decay_ms changes are intended to be
719*1208bc7eSAndroid Build Coastguard Worker * infrequent, either between the {-1, 0, >0} states, or a one-time
720*1208bc7eSAndroid Build Coastguard Worker * arbitrary change during initial arena configuration.
721*1208bc7eSAndroid Build Coastguard Worker */
722*1208bc7eSAndroid Build Coastguard Worker arena_decay_reinit(decay, decay_ms);
723*1208bc7eSAndroid Build Coastguard Worker arena_maybe_decay(tsdn, arena, decay, extents, false);
724*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &decay->mtx);
725*1208bc7eSAndroid Build Coastguard Worker
726*1208bc7eSAndroid Build Coastguard Worker return false;
727*1208bc7eSAndroid Build Coastguard Worker }
728*1208bc7eSAndroid Build Coastguard Worker
729*1208bc7eSAndroid Build Coastguard Worker bool
arena_dirty_decay_ms_set(tsdn_t * tsdn,arena_t * arena,ssize_t decay_ms)730*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_set(tsdn_t *tsdn, arena_t *arena,
731*1208bc7eSAndroid Build Coastguard Worker ssize_t decay_ms) {
732*1208bc7eSAndroid Build Coastguard Worker return arena_decay_ms_set(tsdn, arena, &arena->decay_dirty,
733*1208bc7eSAndroid Build Coastguard Worker &arena->extents_dirty, decay_ms);
734*1208bc7eSAndroid Build Coastguard Worker }
735*1208bc7eSAndroid Build Coastguard Worker
736*1208bc7eSAndroid Build Coastguard Worker bool
arena_muzzy_decay_ms_set(tsdn_t * tsdn,arena_t * arena,ssize_t decay_ms)737*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_set(tsdn_t *tsdn, arena_t *arena,
738*1208bc7eSAndroid Build Coastguard Worker ssize_t decay_ms) {
739*1208bc7eSAndroid Build Coastguard Worker return arena_decay_ms_set(tsdn, arena, &arena->decay_muzzy,
740*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, decay_ms);
741*1208bc7eSAndroid Build Coastguard Worker }
742*1208bc7eSAndroid Build Coastguard Worker
743*1208bc7eSAndroid Build Coastguard Worker static size_t
arena_stash_decayed(tsdn_t * tsdn,arena_t * arena,extent_hooks_t ** r_extent_hooks,extents_t * extents,size_t npages_limit,size_t npages_decay_max,extent_list_t * decay_extents)744*1208bc7eSAndroid Build Coastguard Worker arena_stash_decayed(tsdn_t *tsdn, arena_t *arena,
745*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t **r_extent_hooks, extents_t *extents, size_t npages_limit,
746*1208bc7eSAndroid Build Coastguard Worker size_t npages_decay_max, extent_list_t *decay_extents) {
747*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
748*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 0);
749*1208bc7eSAndroid Build Coastguard Worker
750*1208bc7eSAndroid Build Coastguard Worker /* Stash extents according to npages_limit. */
751*1208bc7eSAndroid Build Coastguard Worker size_t nstashed = 0;
752*1208bc7eSAndroid Build Coastguard Worker extent_t *extent;
753*1208bc7eSAndroid Build Coastguard Worker while (nstashed < npages_decay_max &&
754*1208bc7eSAndroid Build Coastguard Worker (extent = extents_evict(tsdn, arena, r_extent_hooks, extents,
755*1208bc7eSAndroid Build Coastguard Worker npages_limit)) != NULL) {
756*1208bc7eSAndroid Build Coastguard Worker extent_list_append(decay_extents, extent);
757*1208bc7eSAndroid Build Coastguard Worker nstashed += extent_size_get(extent) >> LG_PAGE;
758*1208bc7eSAndroid Build Coastguard Worker }
759*1208bc7eSAndroid Build Coastguard Worker return nstashed;
760*1208bc7eSAndroid Build Coastguard Worker }
761*1208bc7eSAndroid Build Coastguard Worker
762*1208bc7eSAndroid Build Coastguard Worker static size_t
arena_decay_stashed(tsdn_t * tsdn,arena_t * arena,extent_hooks_t ** r_extent_hooks,arena_decay_t * decay,extents_t * extents,bool all,extent_list_t * decay_extents,bool is_background_thread)763*1208bc7eSAndroid Build Coastguard Worker arena_decay_stashed(tsdn_t *tsdn, arena_t *arena,
764*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t **r_extent_hooks, arena_decay_t *decay, extents_t *extents,
765*1208bc7eSAndroid Build Coastguard Worker bool all, extent_list_t *decay_extents, bool is_background_thread) {
766*1208bc7eSAndroid Build Coastguard Worker UNUSED size_t nmadvise, nunmapped;
767*1208bc7eSAndroid Build Coastguard Worker size_t npurged;
768*1208bc7eSAndroid Build Coastguard Worker
769*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
770*1208bc7eSAndroid Build Coastguard Worker nmadvise = 0;
771*1208bc7eSAndroid Build Coastguard Worker nunmapped = 0;
772*1208bc7eSAndroid Build Coastguard Worker }
773*1208bc7eSAndroid Build Coastguard Worker npurged = 0;
774*1208bc7eSAndroid Build Coastguard Worker
775*1208bc7eSAndroid Build Coastguard Worker ssize_t muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);
776*1208bc7eSAndroid Build Coastguard Worker for (extent_t *extent = extent_list_first(decay_extents); extent !=
777*1208bc7eSAndroid Build Coastguard Worker NULL; extent = extent_list_first(decay_extents)) {
778*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
779*1208bc7eSAndroid Build Coastguard Worker nmadvise++;
780*1208bc7eSAndroid Build Coastguard Worker }
781*1208bc7eSAndroid Build Coastguard Worker size_t npages = extent_size_get(extent) >> LG_PAGE;
782*1208bc7eSAndroid Build Coastguard Worker npurged += npages;
783*1208bc7eSAndroid Build Coastguard Worker extent_list_remove(decay_extents, extent);
784*1208bc7eSAndroid Build Coastguard Worker switch (extents_state_get(extents)) {
785*1208bc7eSAndroid Build Coastguard Worker case extent_state_active:
786*1208bc7eSAndroid Build Coastguard Worker not_reached();
787*1208bc7eSAndroid Build Coastguard Worker case extent_state_dirty:
788*1208bc7eSAndroid Build Coastguard Worker if (!all && muzzy_decay_ms != 0 &&
789*1208bc7eSAndroid Build Coastguard Worker !extent_purge_lazy_wrapper(tsdn, arena,
790*1208bc7eSAndroid Build Coastguard Worker r_extent_hooks, extent, 0,
791*1208bc7eSAndroid Build Coastguard Worker extent_size_get(extent))) {
792*1208bc7eSAndroid Build Coastguard Worker extents_dalloc(tsdn, arena, r_extent_hooks,
793*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, extent);
794*1208bc7eSAndroid Build Coastguard Worker arena_background_thread_inactivity_check(tsdn,
795*1208bc7eSAndroid Build Coastguard Worker arena, is_background_thread);
796*1208bc7eSAndroid Build Coastguard Worker break;
797*1208bc7eSAndroid Build Coastguard Worker }
798*1208bc7eSAndroid Build Coastguard Worker /* Fall through. */
799*1208bc7eSAndroid Build Coastguard Worker case extent_state_muzzy:
800*1208bc7eSAndroid Build Coastguard Worker extent_dalloc_wrapper(tsdn, arena, r_extent_hooks,
801*1208bc7eSAndroid Build Coastguard Worker extent);
802*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
803*1208bc7eSAndroid Build Coastguard Worker nunmapped += npages;
804*1208bc7eSAndroid Build Coastguard Worker }
805*1208bc7eSAndroid Build Coastguard Worker break;
806*1208bc7eSAndroid Build Coastguard Worker case extent_state_retained:
807*1208bc7eSAndroid Build Coastguard Worker default:
808*1208bc7eSAndroid Build Coastguard Worker not_reached();
809*1208bc7eSAndroid Build Coastguard Worker }
810*1208bc7eSAndroid Build Coastguard Worker }
811*1208bc7eSAndroid Build Coastguard Worker
812*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
813*1208bc7eSAndroid Build Coastguard Worker arena_stats_lock(tsdn, &arena->stats);
814*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
815*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_u64(tsdn, &arena->stats, &decay->stats->npurge,
816*1208bc7eSAndroid Build Coastguard Worker 1);
817*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_u64(tsdn, &arena->stats,
818*1208bc7eSAndroid Build Coastguard Worker &decay->stats->nmadvise, nmadvise);
819*1208bc7eSAndroid Build Coastguard Worker arena_stats_add_u64(tsdn, &arena->stats, &decay->stats->purged,
820*1208bc7eSAndroid Build Coastguard Worker npurged);
821*1208bc7eSAndroid Build Coastguard Worker #endif
822*1208bc7eSAndroid Build Coastguard Worker arena_stats_sub_zu(tsdn, &arena->stats, &arena->stats.mapped,
823*1208bc7eSAndroid Build Coastguard Worker nunmapped << LG_PAGE);
824*1208bc7eSAndroid Build Coastguard Worker arena_stats_unlock(tsdn, &arena->stats);
825*1208bc7eSAndroid Build Coastguard Worker }
826*1208bc7eSAndroid Build Coastguard Worker
827*1208bc7eSAndroid Build Coastguard Worker return npurged;
828*1208bc7eSAndroid Build Coastguard Worker }
829*1208bc7eSAndroid Build Coastguard Worker
830*1208bc7eSAndroid Build Coastguard Worker /*
831*1208bc7eSAndroid Build Coastguard Worker * npages_limit: Decay at most npages_decay_max pages without violating the
832*1208bc7eSAndroid Build Coastguard Worker * invariant: (extents_npages_get(extents) >= npages_limit). We need an upper
833*1208bc7eSAndroid Build Coastguard Worker * bound on number of pages in order to prevent unbounded growth (namely in
834*1208bc7eSAndroid Build Coastguard Worker * stashed), otherwise unbounded new pages could be added to extents during the
835*1208bc7eSAndroid Build Coastguard Worker * current decay run, so that the purging thread never finishes.
836*1208bc7eSAndroid Build Coastguard Worker */
837*1208bc7eSAndroid Build Coastguard Worker static void
arena_decay_to_limit(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,bool all,size_t npages_limit,size_t npages_decay_max,bool is_background_thread)838*1208bc7eSAndroid Build Coastguard Worker arena_decay_to_limit(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
839*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, bool all, size_t npages_limit, size_t npages_decay_max,
840*1208bc7eSAndroid Build Coastguard Worker bool is_background_thread) {
841*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
842*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 1);
843*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_assert_owner(tsdn, &decay->mtx);
844*1208bc7eSAndroid Build Coastguard Worker
845*1208bc7eSAndroid Build Coastguard Worker if (decay->purging) {
846*1208bc7eSAndroid Build Coastguard Worker return;
847*1208bc7eSAndroid Build Coastguard Worker }
848*1208bc7eSAndroid Build Coastguard Worker decay->purging = true;
849*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &decay->mtx);
850*1208bc7eSAndroid Build Coastguard Worker
851*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = extent_hooks_get(arena);
852*1208bc7eSAndroid Build Coastguard Worker
853*1208bc7eSAndroid Build Coastguard Worker extent_list_t decay_extents;
854*1208bc7eSAndroid Build Coastguard Worker extent_list_init(&decay_extents);
855*1208bc7eSAndroid Build Coastguard Worker
856*1208bc7eSAndroid Build Coastguard Worker size_t npurge = arena_stash_decayed(tsdn, arena, &extent_hooks, extents,
857*1208bc7eSAndroid Build Coastguard Worker npages_limit, npages_decay_max, &decay_extents);
858*1208bc7eSAndroid Build Coastguard Worker if (npurge != 0) {
859*1208bc7eSAndroid Build Coastguard Worker UNUSED size_t npurged = arena_decay_stashed(tsdn, arena,
860*1208bc7eSAndroid Build Coastguard Worker &extent_hooks, decay, extents, all, &decay_extents,
861*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
862*1208bc7eSAndroid Build Coastguard Worker assert(npurged == npurge);
863*1208bc7eSAndroid Build Coastguard Worker }
864*1208bc7eSAndroid Build Coastguard Worker
865*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &decay->mtx);
866*1208bc7eSAndroid Build Coastguard Worker decay->purging = false;
867*1208bc7eSAndroid Build Coastguard Worker }
868*1208bc7eSAndroid Build Coastguard Worker
869*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_impl(tsdn_t * tsdn,arena_t * arena,arena_decay_t * decay,extents_t * extents,bool is_background_thread,bool all)870*1208bc7eSAndroid Build Coastguard Worker arena_decay_impl(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
871*1208bc7eSAndroid Build Coastguard Worker extents_t *extents, bool is_background_thread, bool all) {
872*1208bc7eSAndroid Build Coastguard Worker if (all) {
873*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &decay->mtx);
874*1208bc7eSAndroid Build Coastguard Worker arena_decay_to_limit(tsdn, arena, decay, extents, all, 0,
875*1208bc7eSAndroid Build Coastguard Worker extents_npages_get(extents), is_background_thread);
876*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &decay->mtx);
877*1208bc7eSAndroid Build Coastguard Worker
878*1208bc7eSAndroid Build Coastguard Worker return false;
879*1208bc7eSAndroid Build Coastguard Worker }
880*1208bc7eSAndroid Build Coastguard Worker
881*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_trylock(tsdn, &decay->mtx)) {
882*1208bc7eSAndroid Build Coastguard Worker /* No need to wait if another thread is in progress. */
883*1208bc7eSAndroid Build Coastguard Worker return true;
884*1208bc7eSAndroid Build Coastguard Worker }
885*1208bc7eSAndroid Build Coastguard Worker
886*1208bc7eSAndroid Build Coastguard Worker bool epoch_advanced = arena_maybe_decay(tsdn, arena, decay, extents,
887*1208bc7eSAndroid Build Coastguard Worker is_background_thread);
888*1208bc7eSAndroid Build Coastguard Worker UNUSED size_t npages_new;
889*1208bc7eSAndroid Build Coastguard Worker if (epoch_advanced) {
890*1208bc7eSAndroid Build Coastguard Worker /* Backlog is updated on epoch advance. */
891*1208bc7eSAndroid Build Coastguard Worker npages_new = decay->backlog[SMOOTHSTEP_NSTEPS-1];
892*1208bc7eSAndroid Build Coastguard Worker }
893*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &decay->mtx);
894*1208bc7eSAndroid Build Coastguard Worker
895*1208bc7eSAndroid Build Coastguard Worker if (have_background_thread && background_thread_enabled() &&
896*1208bc7eSAndroid Build Coastguard Worker epoch_advanced && !is_background_thread) {
897*1208bc7eSAndroid Build Coastguard Worker background_thread_interval_check(tsdn, arena, decay,
898*1208bc7eSAndroid Build Coastguard Worker npages_new);
899*1208bc7eSAndroid Build Coastguard Worker }
900*1208bc7eSAndroid Build Coastguard Worker
901*1208bc7eSAndroid Build Coastguard Worker return false;
902*1208bc7eSAndroid Build Coastguard Worker }
903*1208bc7eSAndroid Build Coastguard Worker
904*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_dirty(tsdn_t * tsdn,arena_t * arena,bool is_background_thread,bool all)905*1208bc7eSAndroid Build Coastguard Worker arena_decay_dirty(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,
906*1208bc7eSAndroid Build Coastguard Worker bool all) {
907*1208bc7eSAndroid Build Coastguard Worker return arena_decay_impl(tsdn, arena, &arena->decay_dirty,
908*1208bc7eSAndroid Build Coastguard Worker &arena->extents_dirty, is_background_thread, all);
909*1208bc7eSAndroid Build Coastguard Worker }
910*1208bc7eSAndroid Build Coastguard Worker
911*1208bc7eSAndroid Build Coastguard Worker static bool
arena_decay_muzzy(tsdn_t * tsdn,arena_t * arena,bool is_background_thread,bool all)912*1208bc7eSAndroid Build Coastguard Worker arena_decay_muzzy(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,
913*1208bc7eSAndroid Build Coastguard Worker bool all) {
914*1208bc7eSAndroid Build Coastguard Worker return arena_decay_impl(tsdn, arena, &arena->decay_muzzy,
915*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, is_background_thread, all);
916*1208bc7eSAndroid Build Coastguard Worker }
917*1208bc7eSAndroid Build Coastguard Worker
918*1208bc7eSAndroid Build Coastguard Worker void
arena_decay(tsdn_t * tsdn,arena_t * arena,bool is_background_thread,bool all)919*1208bc7eSAndroid Build Coastguard Worker arena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread, bool all) {
920*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_dirty(tsdn, arena, is_background_thread, all)) {
921*1208bc7eSAndroid Build Coastguard Worker return;
922*1208bc7eSAndroid Build Coastguard Worker }
923*1208bc7eSAndroid Build Coastguard Worker arena_decay_muzzy(tsdn, arena, is_background_thread, all);
924*1208bc7eSAndroid Build Coastguard Worker }
925*1208bc7eSAndroid Build Coastguard Worker
926*1208bc7eSAndroid Build Coastguard Worker static void
arena_slab_dalloc(tsdn_t * tsdn,arena_t * arena,extent_t * slab)927*1208bc7eSAndroid Build Coastguard Worker arena_slab_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *slab) {
928*1208bc7eSAndroid Build Coastguard Worker arena_nactive_sub(arena, extent_size_get(slab) >> LG_PAGE);
929*1208bc7eSAndroid Build Coastguard Worker
930*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;
931*1208bc7eSAndroid Build Coastguard Worker arena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, slab);
932*1208bc7eSAndroid Build Coastguard Worker }
933*1208bc7eSAndroid Build Coastguard Worker
934*1208bc7eSAndroid Build Coastguard Worker static void
arena_bin_slabs_nonfull_insert(bin_t * bin,extent_t * slab)935*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_insert(bin_t *bin, extent_t *slab) {
936*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(slab) > 0);
937*1208bc7eSAndroid Build Coastguard Worker extent_heap_insert(&bin->slabs_nonfull, slab);
938*1208bc7eSAndroid Build Coastguard Worker }
939*1208bc7eSAndroid Build Coastguard Worker
940*1208bc7eSAndroid Build Coastguard Worker static void
arena_bin_slabs_nonfull_remove(bin_t * bin,extent_t * slab)941*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_remove(bin_t *bin, extent_t *slab) {
942*1208bc7eSAndroid Build Coastguard Worker extent_heap_remove(&bin->slabs_nonfull, slab);
943*1208bc7eSAndroid Build Coastguard Worker }
944*1208bc7eSAndroid Build Coastguard Worker
945*1208bc7eSAndroid Build Coastguard Worker static extent_t *
arena_bin_slabs_nonfull_tryget(bin_t * bin)946*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_tryget(bin_t *bin) {
947*1208bc7eSAndroid Build Coastguard Worker extent_t *slab = extent_heap_remove_first(&bin->slabs_nonfull);
948*1208bc7eSAndroid Build Coastguard Worker if (slab == NULL) {
949*1208bc7eSAndroid Build Coastguard Worker return NULL;
950*1208bc7eSAndroid Build Coastguard Worker }
951*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
952*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
953*1208bc7eSAndroid Build Coastguard Worker bin->stats.reslabs++;
954*1208bc7eSAndroid Build Coastguard Worker }
955*1208bc7eSAndroid Build Coastguard Worker #endif
956*1208bc7eSAndroid Build Coastguard Worker return slab;
957*1208bc7eSAndroid Build Coastguard Worker }
958*1208bc7eSAndroid Build Coastguard Worker
959*1208bc7eSAndroid Build Coastguard Worker static void
arena_bin_slabs_full_insert(arena_t * arena,bin_t * bin,extent_t * slab)960*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_insert(arena_t *arena, bin_t *bin, extent_t *slab) {
961*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(slab) == 0);
962*1208bc7eSAndroid Build Coastguard Worker /*
963*1208bc7eSAndroid Build Coastguard Worker * Tracking extents is required by arena_reset, which is not allowed
964*1208bc7eSAndroid Build Coastguard Worker * for auto arenas. Bypass this step to avoid touching the extent
965*1208bc7eSAndroid Build Coastguard Worker * linkage (often results in cache misses) for auto arenas.
966*1208bc7eSAndroid Build Coastguard Worker */
967*1208bc7eSAndroid Build Coastguard Worker if (arena_is_auto(arena)) {
968*1208bc7eSAndroid Build Coastguard Worker return;
969*1208bc7eSAndroid Build Coastguard Worker }
970*1208bc7eSAndroid Build Coastguard Worker extent_list_append(&bin->slabs_full, slab);
971*1208bc7eSAndroid Build Coastguard Worker }
972*1208bc7eSAndroid Build Coastguard Worker
973*1208bc7eSAndroid Build Coastguard Worker static void
arena_bin_slabs_full_remove(arena_t * arena,bin_t * bin,extent_t * slab)974*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_remove(arena_t *arena, bin_t *bin, extent_t *slab) {
975*1208bc7eSAndroid Build Coastguard Worker if (arena_is_auto(arena)) {
976*1208bc7eSAndroid Build Coastguard Worker return;
977*1208bc7eSAndroid Build Coastguard Worker }
978*1208bc7eSAndroid Build Coastguard Worker extent_list_remove(&bin->slabs_full, slab);
979*1208bc7eSAndroid Build Coastguard Worker }
980*1208bc7eSAndroid Build Coastguard Worker
981*1208bc7eSAndroid Build Coastguard Worker void
arena_reset(tsd_t * tsd,arena_t * arena)982*1208bc7eSAndroid Build Coastguard Worker arena_reset(tsd_t *tsd, arena_t *arena) {
983*1208bc7eSAndroid Build Coastguard Worker /*
984*1208bc7eSAndroid Build Coastguard Worker * Locking in this function is unintuitive. The caller guarantees that
985*1208bc7eSAndroid Build Coastguard Worker * no concurrent operations are happening in this arena, but there are
986*1208bc7eSAndroid Build Coastguard Worker * still reasons that some locking is necessary:
987*1208bc7eSAndroid Build Coastguard Worker *
988*1208bc7eSAndroid Build Coastguard Worker * - Some of the functions in the transitive closure of calls assume
989*1208bc7eSAndroid Build Coastguard Worker * appropriate locks are held, and in some cases these locks are
990*1208bc7eSAndroid Build Coastguard Worker * temporarily dropped to avoid lock order reversal or deadlock due to
991*1208bc7eSAndroid Build Coastguard Worker * reentry.
992*1208bc7eSAndroid Build Coastguard Worker * - mallctl("epoch", ...) may concurrently refresh stats. While
993*1208bc7eSAndroid Build Coastguard Worker * strictly speaking this is a "concurrent operation", disallowing
994*1208bc7eSAndroid Build Coastguard Worker * stats refreshes would impose an inconvenient burden.
995*1208bc7eSAndroid Build Coastguard Worker */
996*1208bc7eSAndroid Build Coastguard Worker
997*1208bc7eSAndroid Build Coastguard Worker /* Large allocations. */
998*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);
999*1208bc7eSAndroid Build Coastguard Worker
1000*1208bc7eSAndroid Build Coastguard Worker for (extent_t *extent = extent_list_first(&arena->large); extent !=
1001*1208bc7eSAndroid Build Coastguard Worker NULL; extent = extent_list_first(&arena->large)) {
1002*1208bc7eSAndroid Build Coastguard Worker void *ptr = extent_base_get(extent);
1003*1208bc7eSAndroid Build Coastguard Worker size_t usize;
1004*1208bc7eSAndroid Build Coastguard Worker
1005*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);
1006*1208bc7eSAndroid Build Coastguard Worker alloc_ctx_t alloc_ctx;
1007*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);
1008*1208bc7eSAndroid Build Coastguard Worker rtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,
1009*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);
1010*1208bc7eSAndroid Build Coastguard Worker assert(alloc_ctx.szind != NSIZES);
1011*1208bc7eSAndroid Build Coastguard Worker
1012*1208bc7eSAndroid Build Coastguard Worker if (config_stats || (config_prof && opt_prof)) {
1013*1208bc7eSAndroid Build Coastguard Worker usize = sz_index2size(alloc_ctx.szind);
1014*1208bc7eSAndroid Build Coastguard Worker assert(usize == isalloc(tsd_tsdn(tsd), ptr));
1015*1208bc7eSAndroid Build Coastguard Worker }
1016*1208bc7eSAndroid Build Coastguard Worker /* Remove large allocation from prof sample set. */
1017*1208bc7eSAndroid Build Coastguard Worker if (config_prof && opt_prof) {
1018*1208bc7eSAndroid Build Coastguard Worker prof_free(tsd, ptr, usize, &alloc_ctx);
1019*1208bc7eSAndroid Build Coastguard Worker }
1020*1208bc7eSAndroid Build Coastguard Worker large_dalloc(tsd_tsdn(tsd), extent);
1021*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);
1022*1208bc7eSAndroid Build Coastguard Worker }
1023*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);
1024*1208bc7eSAndroid Build Coastguard Worker
1025*1208bc7eSAndroid Build Coastguard Worker /* Bins. */
1026*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < NBINS; i++) {
1027*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1028*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &arena->bins[i];
1029*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
1030*1208bc7eSAndroid Build Coastguard Worker if (bin->slabcur != NULL) {
1031*1208bc7eSAndroid Build Coastguard Worker slab = bin->slabcur;
1032*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = NULL;
1033*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
1034*1208bc7eSAndroid Build Coastguard Worker arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
1035*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
1036*1208bc7eSAndroid Build Coastguard Worker }
1037*1208bc7eSAndroid Build Coastguard Worker while ((slab = extent_heap_remove_first(&bin->slabs_nonfull)) !=
1038*1208bc7eSAndroid Build Coastguard Worker NULL) {
1039*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
1040*1208bc7eSAndroid Build Coastguard Worker arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
1041*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
1042*1208bc7eSAndroid Build Coastguard Worker }
1043*1208bc7eSAndroid Build Coastguard Worker for (slab = extent_list_first(&bin->slabs_full); slab != NULL;
1044*1208bc7eSAndroid Build Coastguard Worker slab = extent_list_first(&bin->slabs_full)) {
1045*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_remove(arena, bin, slab);
1046*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
1047*1208bc7eSAndroid Build Coastguard Worker arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
1048*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
1049*1208bc7eSAndroid Build Coastguard Worker }
1050*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1051*1208bc7eSAndroid Build Coastguard Worker bin->stats.curregs = 0;
1052*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1053*1208bc7eSAndroid Build Coastguard Worker bin->stats.curslabs = 0;
1054*1208bc7eSAndroid Build Coastguard Worker #endif
1055*1208bc7eSAndroid Build Coastguard Worker }
1056*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
1057*1208bc7eSAndroid Build Coastguard Worker }
1058*1208bc7eSAndroid Build Coastguard Worker
1059*1208bc7eSAndroid Build Coastguard Worker atomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);
1060*1208bc7eSAndroid Build Coastguard Worker }
1061*1208bc7eSAndroid Build Coastguard Worker
1062*1208bc7eSAndroid Build Coastguard Worker static void
arena_destroy_retained(tsdn_t * tsdn,arena_t * arena)1063*1208bc7eSAndroid Build Coastguard Worker arena_destroy_retained(tsdn_t *tsdn, arena_t *arena) {
1064*1208bc7eSAndroid Build Coastguard Worker /*
1065*1208bc7eSAndroid Build Coastguard Worker * Iterate over the retained extents and destroy them. This gives the
1066*1208bc7eSAndroid Build Coastguard Worker * extent allocator underlying the extent hooks an opportunity to unmap
1067*1208bc7eSAndroid Build Coastguard Worker * all retained memory without having to keep its own metadata
1068*1208bc7eSAndroid Build Coastguard Worker * structures. In practice, virtual memory for dss-allocated extents is
1069*1208bc7eSAndroid Build Coastguard Worker * leaked here, so best practice is to avoid dss for arenas to be
1070*1208bc7eSAndroid Build Coastguard Worker * destroyed, or provide custom extent hooks that track retained
1071*1208bc7eSAndroid Build Coastguard Worker * dss-based extents for later reuse.
1072*1208bc7eSAndroid Build Coastguard Worker */
1073*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = extent_hooks_get(arena);
1074*1208bc7eSAndroid Build Coastguard Worker extent_t *extent;
1075*1208bc7eSAndroid Build Coastguard Worker while ((extent = extents_evict(tsdn, arena, &extent_hooks,
1076*1208bc7eSAndroid Build Coastguard Worker &arena->extents_retained, 0)) != NULL) {
1077*1208bc7eSAndroid Build Coastguard Worker extent_destroy_wrapper(tsdn, arena, &extent_hooks, extent);
1078*1208bc7eSAndroid Build Coastguard Worker }
1079*1208bc7eSAndroid Build Coastguard Worker }
1080*1208bc7eSAndroid Build Coastguard Worker
1081*1208bc7eSAndroid Build Coastguard Worker void
arena_destroy(tsd_t * tsd,arena_t * arena)1082*1208bc7eSAndroid Build Coastguard Worker arena_destroy(tsd_t *tsd, arena_t *arena) {
1083*1208bc7eSAndroid Build Coastguard Worker assert(base_ind_get(arena->base) >= narenas_auto);
1084*1208bc7eSAndroid Build Coastguard Worker assert(arena_nthreads_get(arena, false) == 0);
1085*1208bc7eSAndroid Build Coastguard Worker assert(arena_nthreads_get(arena, true) == 0);
1086*1208bc7eSAndroid Build Coastguard Worker
1087*1208bc7eSAndroid Build Coastguard Worker /*
1088*1208bc7eSAndroid Build Coastguard Worker * No allocations have occurred since arena_reset() was called.
1089*1208bc7eSAndroid Build Coastguard Worker * Furthermore, the caller (arena_i_destroy_ctl()) purged all cached
1090*1208bc7eSAndroid Build Coastguard Worker * extents, so only retained extents may remain.
1091*1208bc7eSAndroid Build Coastguard Worker */
1092*1208bc7eSAndroid Build Coastguard Worker assert(extents_npages_get(&arena->extents_dirty) == 0);
1093*1208bc7eSAndroid Build Coastguard Worker assert(extents_npages_get(&arena->extents_muzzy) == 0);
1094*1208bc7eSAndroid Build Coastguard Worker
1095*1208bc7eSAndroid Build Coastguard Worker /* Deallocate retained memory. */
1096*1208bc7eSAndroid Build Coastguard Worker arena_destroy_retained(tsd_tsdn(tsd), arena);
1097*1208bc7eSAndroid Build Coastguard Worker
1098*1208bc7eSAndroid Build Coastguard Worker /*
1099*1208bc7eSAndroid Build Coastguard Worker * Remove the arena pointer from the arenas array. We rely on the fact
1100*1208bc7eSAndroid Build Coastguard Worker * that there is no way for the application to get a dirty read from the
1101*1208bc7eSAndroid Build Coastguard Worker * arenas array unless there is an inherent race in the application
1102*1208bc7eSAndroid Build Coastguard Worker * involving access of an arena being concurrently destroyed. The
1103*1208bc7eSAndroid Build Coastguard Worker * application must synchronize knowledge of the arena's validity, so as
1104*1208bc7eSAndroid Build Coastguard Worker * long as we use an atomic write to update the arenas array, the
1105*1208bc7eSAndroid Build Coastguard Worker * application will get a clean read any time after it synchronizes
1106*1208bc7eSAndroid Build Coastguard Worker * knowledge that the arena is no longer valid.
1107*1208bc7eSAndroid Build Coastguard Worker */
1108*1208bc7eSAndroid Build Coastguard Worker arena_set(base_ind_get(arena->base), NULL);
1109*1208bc7eSAndroid Build Coastguard Worker
1110*1208bc7eSAndroid Build Coastguard Worker /*
1111*1208bc7eSAndroid Build Coastguard Worker * Destroy the base allocator, which manages all metadata ever mapped by
1112*1208bc7eSAndroid Build Coastguard Worker * this arena.
1113*1208bc7eSAndroid Build Coastguard Worker */
1114*1208bc7eSAndroid Build Coastguard Worker base_delete(tsd_tsdn(tsd), arena->base);
1115*1208bc7eSAndroid Build Coastguard Worker }
1116*1208bc7eSAndroid Build Coastguard Worker
1117*1208bc7eSAndroid Build Coastguard Worker static extent_t *
arena_slab_alloc_hard(tsdn_t * tsdn,arena_t * arena,extent_hooks_t ** r_extent_hooks,const bin_info_t * bin_info,szind_t szind)1118*1208bc7eSAndroid Build Coastguard Worker arena_slab_alloc_hard(tsdn_t *tsdn, arena_t *arena,
1119*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t **r_extent_hooks, const bin_info_t *bin_info,
1120*1208bc7eSAndroid Build Coastguard Worker szind_t szind) {
1121*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1122*1208bc7eSAndroid Build Coastguard Worker bool zero, commit;
1123*1208bc7eSAndroid Build Coastguard Worker
1124*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
1125*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 0);
1126*1208bc7eSAndroid Build Coastguard Worker
1127*1208bc7eSAndroid Build Coastguard Worker zero = false;
1128*1208bc7eSAndroid Build Coastguard Worker commit = true;
1129*1208bc7eSAndroid Build Coastguard Worker slab = extent_alloc_wrapper(tsdn, arena, r_extent_hooks, NULL,
1130*1208bc7eSAndroid Build Coastguard Worker bin_info->slab_size, 0, PAGE, true, szind, &zero, &commit);
1131*1208bc7eSAndroid Build Coastguard Worker
1132*1208bc7eSAndroid Build Coastguard Worker if (config_stats && slab != NULL) {
1133*1208bc7eSAndroid Build Coastguard Worker arena_stats_mapped_add(tsdn, &arena->stats,
1134*1208bc7eSAndroid Build Coastguard Worker bin_info->slab_size);
1135*1208bc7eSAndroid Build Coastguard Worker }
1136*1208bc7eSAndroid Build Coastguard Worker
1137*1208bc7eSAndroid Build Coastguard Worker return slab;
1138*1208bc7eSAndroid Build Coastguard Worker }
1139*1208bc7eSAndroid Build Coastguard Worker
1140*1208bc7eSAndroid Build Coastguard Worker static extent_t *
arena_slab_alloc(tsdn_t * tsdn,arena_t * arena,szind_t binind,const bin_info_t * bin_info)1141*1208bc7eSAndroid Build Coastguard Worker arena_slab_alloc(tsdn_t *tsdn, arena_t *arena, szind_t binind,
1142*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info) {
1143*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
1144*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_CORE, 0);
1145*1208bc7eSAndroid Build Coastguard Worker
1146*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;
1147*1208bc7eSAndroid Build Coastguard Worker szind_t szind = sz_size2index(bin_info->reg_size);
1148*1208bc7eSAndroid Build Coastguard Worker bool zero = false;
1149*1208bc7eSAndroid Build Coastguard Worker bool commit = true;
1150*1208bc7eSAndroid Build Coastguard Worker extent_t *slab = extents_alloc(tsdn, arena, &extent_hooks,
1151*1208bc7eSAndroid Build Coastguard Worker &arena->extents_dirty, NULL, bin_info->slab_size, 0, PAGE, true,
1152*1208bc7eSAndroid Build Coastguard Worker binind, &zero, &commit);
1153*1208bc7eSAndroid Build Coastguard Worker if (slab == NULL) {
1154*1208bc7eSAndroid Build Coastguard Worker slab = extents_alloc(tsdn, arena, &extent_hooks,
1155*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, NULL, bin_info->slab_size, 0, PAGE,
1156*1208bc7eSAndroid Build Coastguard Worker true, binind, &zero, &commit);
1157*1208bc7eSAndroid Build Coastguard Worker }
1158*1208bc7eSAndroid Build Coastguard Worker if (slab == NULL) {
1159*1208bc7eSAndroid Build Coastguard Worker slab = arena_slab_alloc_hard(tsdn, arena, &extent_hooks,
1160*1208bc7eSAndroid Build Coastguard Worker bin_info, szind);
1161*1208bc7eSAndroid Build Coastguard Worker if (slab == NULL) {
1162*1208bc7eSAndroid Build Coastguard Worker return NULL;
1163*1208bc7eSAndroid Build Coastguard Worker }
1164*1208bc7eSAndroid Build Coastguard Worker }
1165*1208bc7eSAndroid Build Coastguard Worker assert(extent_slab_get(slab));
1166*1208bc7eSAndroid Build Coastguard Worker
1167*1208bc7eSAndroid Build Coastguard Worker /* Initialize slab internals. */
1168*1208bc7eSAndroid Build Coastguard Worker arena_slab_data_t *slab_data = extent_slab_data_get(slab);
1169*1208bc7eSAndroid Build Coastguard Worker extent_nfree_set(slab, bin_info->nregs);
1170*1208bc7eSAndroid Build Coastguard Worker bitmap_init(slab_data->bitmap, &bin_info->bitmap_info, false);
1171*1208bc7eSAndroid Build Coastguard Worker
1172*1208bc7eSAndroid Build Coastguard Worker arena_nactive_add(arena, extent_size_get(slab) >> LG_PAGE);
1173*1208bc7eSAndroid Build Coastguard Worker
1174*1208bc7eSAndroid Build Coastguard Worker return slab;
1175*1208bc7eSAndroid Build Coastguard Worker }
1176*1208bc7eSAndroid Build Coastguard Worker
1177*1208bc7eSAndroid Build Coastguard Worker static extent_t *
arena_bin_nonfull_slab_get(tsdn_t * tsdn,arena_t * arena,bin_t * bin,szind_t binind)1178*1208bc7eSAndroid Build Coastguard Worker arena_bin_nonfull_slab_get(tsdn_t *tsdn, arena_t *arena, bin_t *bin,
1179*1208bc7eSAndroid Build Coastguard Worker szind_t binind) {
1180*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1181*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info;
1182*1208bc7eSAndroid Build Coastguard Worker
1183*1208bc7eSAndroid Build Coastguard Worker /* Look for a usable slab. */
1184*1208bc7eSAndroid Build Coastguard Worker slab = arena_bin_slabs_nonfull_tryget(bin);
1185*1208bc7eSAndroid Build Coastguard Worker if (slab != NULL) {
1186*1208bc7eSAndroid Build Coastguard Worker return slab;
1187*1208bc7eSAndroid Build Coastguard Worker }
1188*1208bc7eSAndroid Build Coastguard Worker /* No existing slabs have any space available. */
1189*1208bc7eSAndroid Build Coastguard Worker
1190*1208bc7eSAndroid Build Coastguard Worker bin_info = &bin_infos[binind];
1191*1208bc7eSAndroid Build Coastguard Worker
1192*1208bc7eSAndroid Build Coastguard Worker /* Allocate a new slab. */
1193*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1194*1208bc7eSAndroid Build Coastguard Worker /******************************/
1195*1208bc7eSAndroid Build Coastguard Worker slab = arena_slab_alloc(tsdn, arena, binind, bin_info);
1196*1208bc7eSAndroid Build Coastguard Worker /********************************/
1197*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
1198*1208bc7eSAndroid Build Coastguard Worker if (slab != NULL) {
1199*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1200*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1201*1208bc7eSAndroid Build Coastguard Worker bin->stats.nslabs++;
1202*1208bc7eSAndroid Build Coastguard Worker bin->stats.curslabs++;
1203*1208bc7eSAndroid Build Coastguard Worker }
1204*1208bc7eSAndroid Build Coastguard Worker #endif
1205*1208bc7eSAndroid Build Coastguard Worker return slab;
1206*1208bc7eSAndroid Build Coastguard Worker }
1207*1208bc7eSAndroid Build Coastguard Worker
1208*1208bc7eSAndroid Build Coastguard Worker /*
1209*1208bc7eSAndroid Build Coastguard Worker * arena_slab_alloc() failed, but another thread may have made
1210*1208bc7eSAndroid Build Coastguard Worker * sufficient memory available while this one dropped bin->lock above,
1211*1208bc7eSAndroid Build Coastguard Worker * so search one more time.
1212*1208bc7eSAndroid Build Coastguard Worker */
1213*1208bc7eSAndroid Build Coastguard Worker slab = arena_bin_slabs_nonfull_tryget(bin);
1214*1208bc7eSAndroid Build Coastguard Worker if (slab != NULL) {
1215*1208bc7eSAndroid Build Coastguard Worker return slab;
1216*1208bc7eSAndroid Build Coastguard Worker }
1217*1208bc7eSAndroid Build Coastguard Worker
1218*1208bc7eSAndroid Build Coastguard Worker return NULL;
1219*1208bc7eSAndroid Build Coastguard Worker }
1220*1208bc7eSAndroid Build Coastguard Worker
1221*1208bc7eSAndroid Build Coastguard Worker /* Re-fill bin->slabcur, then call arena_slab_reg_alloc(). */
1222*1208bc7eSAndroid Build Coastguard Worker static void *
arena_bin_malloc_hard(tsdn_t * tsdn,arena_t * arena,bin_t * bin,szind_t binind)1223*1208bc7eSAndroid Build Coastguard Worker arena_bin_malloc_hard(tsdn_t *tsdn, arena_t *arena, bin_t *bin,
1224*1208bc7eSAndroid Build Coastguard Worker szind_t binind) {
1225*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info;
1226*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1227*1208bc7eSAndroid Build Coastguard Worker
1228*1208bc7eSAndroid Build Coastguard Worker bin_info = &bin_infos[binind];
1229*1208bc7eSAndroid Build Coastguard Worker if (!arena_is_auto(arena) && bin->slabcur != NULL) {
1230*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_insert(arena, bin, bin->slabcur);
1231*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = NULL;
1232*1208bc7eSAndroid Build Coastguard Worker }
1233*1208bc7eSAndroid Build Coastguard Worker slab = arena_bin_nonfull_slab_get(tsdn, arena, bin, binind);
1234*1208bc7eSAndroid Build Coastguard Worker if (bin->slabcur != NULL) {
1235*1208bc7eSAndroid Build Coastguard Worker /*
1236*1208bc7eSAndroid Build Coastguard Worker * Another thread updated slabcur while this one ran without the
1237*1208bc7eSAndroid Build Coastguard Worker * bin lock in arena_bin_nonfull_slab_get().
1238*1208bc7eSAndroid Build Coastguard Worker */
1239*1208bc7eSAndroid Build Coastguard Worker if (extent_nfree_get(bin->slabcur) > 0) {
1240*1208bc7eSAndroid Build Coastguard Worker void *ret = arena_slab_reg_alloc(bin->slabcur,
1241*1208bc7eSAndroid Build Coastguard Worker bin_info);
1242*1208bc7eSAndroid Build Coastguard Worker if (slab != NULL) {
1243*1208bc7eSAndroid Build Coastguard Worker /*
1244*1208bc7eSAndroid Build Coastguard Worker * arena_slab_alloc() may have allocated slab,
1245*1208bc7eSAndroid Build Coastguard Worker * or it may have been pulled from
1246*1208bc7eSAndroid Build Coastguard Worker * slabs_nonfull. Therefore it is unsafe to
1247*1208bc7eSAndroid Build Coastguard Worker * make any assumptions about how slab has
1248*1208bc7eSAndroid Build Coastguard Worker * previously been used, and
1249*1208bc7eSAndroid Build Coastguard Worker * arena_bin_lower_slab() must be called, as if
1250*1208bc7eSAndroid Build Coastguard Worker * a region were just deallocated from the slab.
1251*1208bc7eSAndroid Build Coastguard Worker */
1252*1208bc7eSAndroid Build Coastguard Worker if (extent_nfree_get(slab) == bin_info->nregs) {
1253*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_slab(tsdn, arena, slab,
1254*1208bc7eSAndroid Build Coastguard Worker bin);
1255*1208bc7eSAndroid Build Coastguard Worker } else {
1256*1208bc7eSAndroid Build Coastguard Worker arena_bin_lower_slab(tsdn, arena, slab,
1257*1208bc7eSAndroid Build Coastguard Worker bin);
1258*1208bc7eSAndroid Build Coastguard Worker }
1259*1208bc7eSAndroid Build Coastguard Worker }
1260*1208bc7eSAndroid Build Coastguard Worker return ret;
1261*1208bc7eSAndroid Build Coastguard Worker }
1262*1208bc7eSAndroid Build Coastguard Worker
1263*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_insert(arena, bin, bin->slabcur);
1264*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = NULL;
1265*1208bc7eSAndroid Build Coastguard Worker }
1266*1208bc7eSAndroid Build Coastguard Worker
1267*1208bc7eSAndroid Build Coastguard Worker if (slab == NULL) {
1268*1208bc7eSAndroid Build Coastguard Worker return NULL;
1269*1208bc7eSAndroid Build Coastguard Worker }
1270*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = slab;
1271*1208bc7eSAndroid Build Coastguard Worker
1272*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(bin->slabcur) > 0);
1273*1208bc7eSAndroid Build Coastguard Worker
1274*1208bc7eSAndroid Build Coastguard Worker return arena_slab_reg_alloc(slab, bin_info);
1275*1208bc7eSAndroid Build Coastguard Worker }
1276*1208bc7eSAndroid Build Coastguard Worker
1277*1208bc7eSAndroid Build Coastguard Worker void
arena_tcache_fill_small(tsdn_t * tsdn,arena_t * arena,tcache_t * tcache,cache_bin_t * tbin,szind_t binind,uint64_t prof_accumbytes)1278*1208bc7eSAndroid Build Coastguard Worker arena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,
1279*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin, szind_t binind, uint64_t prof_accumbytes) {
1280*1208bc7eSAndroid Build Coastguard Worker unsigned i, nfill;
1281*1208bc7eSAndroid Build Coastguard Worker bin_t *bin;
1282*1208bc7eSAndroid Build Coastguard Worker
1283*1208bc7eSAndroid Build Coastguard Worker assert(tbin->ncached == 0);
1284*1208bc7eSAndroid Build Coastguard Worker
1285*1208bc7eSAndroid Build Coastguard Worker if (config_prof && arena_prof_accum(tsdn, arena, prof_accumbytes)) {
1286*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsdn);
1287*1208bc7eSAndroid Build Coastguard Worker }
1288*1208bc7eSAndroid Build Coastguard Worker bin = &arena->bins[binind];
1289*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
1290*1208bc7eSAndroid Build Coastguard Worker for (i = 0, nfill = (tcache_bin_info[binind].ncached_max >>
1291*1208bc7eSAndroid Build Coastguard Worker tcache->lg_fill_div[binind]); i < nfill; i++) {
1292*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1293*1208bc7eSAndroid Build Coastguard Worker void *ptr;
1294*1208bc7eSAndroid Build Coastguard Worker if ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) >
1295*1208bc7eSAndroid Build Coastguard Worker 0) {
1296*1208bc7eSAndroid Build Coastguard Worker ptr = arena_slab_reg_alloc(slab, &bin_infos[binind]);
1297*1208bc7eSAndroid Build Coastguard Worker } else {
1298*1208bc7eSAndroid Build Coastguard Worker ptr = arena_bin_malloc_hard(tsdn, arena, bin, binind);
1299*1208bc7eSAndroid Build Coastguard Worker }
1300*1208bc7eSAndroid Build Coastguard Worker if (ptr == NULL) {
1301*1208bc7eSAndroid Build Coastguard Worker /*
1302*1208bc7eSAndroid Build Coastguard Worker * OOM. tbin->avail isn't yet filled down to its first
1303*1208bc7eSAndroid Build Coastguard Worker * element, so the successful allocations (if any) must
1304*1208bc7eSAndroid Build Coastguard Worker * be moved just before tbin->avail before bailing out.
1305*1208bc7eSAndroid Build Coastguard Worker */
1306*1208bc7eSAndroid Build Coastguard Worker if (i > 0) {
1307*1208bc7eSAndroid Build Coastguard Worker memmove(tbin->avail - i, tbin->avail - nfill,
1308*1208bc7eSAndroid Build Coastguard Worker i * sizeof(void *));
1309*1208bc7eSAndroid Build Coastguard Worker }
1310*1208bc7eSAndroid Build Coastguard Worker break;
1311*1208bc7eSAndroid Build Coastguard Worker }
1312*1208bc7eSAndroid Build Coastguard Worker if (config_fill && unlikely(opt_junk_alloc)) {
1313*1208bc7eSAndroid Build Coastguard Worker arena_alloc_junk_small(ptr, &bin_infos[binind], true);
1314*1208bc7eSAndroid Build Coastguard Worker }
1315*1208bc7eSAndroid Build Coastguard Worker /* Insert such that low regions get used first. */
1316*1208bc7eSAndroid Build Coastguard Worker *(tbin->avail - nfill + i) = ptr;
1317*1208bc7eSAndroid Build Coastguard Worker }
1318*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1319*1208bc7eSAndroid Build Coastguard Worker bin->stats.nmalloc += i;
1320*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
1321*1208bc7eSAndroid Build Coastguard Worker bin->stats.nrequests += tbin->tstats.nrequests;
1322*1208bc7eSAndroid Build Coastguard Worker #endif
1323*1208bc7eSAndroid Build Coastguard Worker bin->stats.curregs += i;
1324*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1325*1208bc7eSAndroid Build Coastguard Worker bin->stats.nfills++;
1326*1208bc7eSAndroid Build Coastguard Worker #endif
1327*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
1328*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
1329*1208bc7eSAndroid Build Coastguard Worker #endif
1330*1208bc7eSAndroid Build Coastguard Worker }
1331*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1332*1208bc7eSAndroid Build Coastguard Worker tbin->ncached = i;
1333*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, arena);
1334*1208bc7eSAndroid Build Coastguard Worker }
1335*1208bc7eSAndroid Build Coastguard Worker
1336*1208bc7eSAndroid Build Coastguard Worker void
arena_alloc_junk_small(void * ptr,const bin_info_t * bin_info,bool zero)1337*1208bc7eSAndroid Build Coastguard Worker arena_alloc_junk_small(void *ptr, const bin_info_t *bin_info, bool zero) {
1338*1208bc7eSAndroid Build Coastguard Worker if (!zero) {
1339*1208bc7eSAndroid Build Coastguard Worker memset(ptr, JEMALLOC_ALLOC_JUNK, bin_info->reg_size);
1340*1208bc7eSAndroid Build Coastguard Worker }
1341*1208bc7eSAndroid Build Coastguard Worker }
1342*1208bc7eSAndroid Build Coastguard Worker
1343*1208bc7eSAndroid Build Coastguard Worker static void
arena_dalloc_junk_small_impl(void * ptr,const bin_info_t * bin_info)1344*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_junk_small_impl(void *ptr, const bin_info_t *bin_info) {
1345*1208bc7eSAndroid Build Coastguard Worker memset(ptr, JEMALLOC_FREE_JUNK, bin_info->reg_size);
1346*1208bc7eSAndroid Build Coastguard Worker }
1347*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_junk_small_t *JET_MUTABLE arena_dalloc_junk_small =
1348*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_junk_small_impl;
1349*1208bc7eSAndroid Build Coastguard Worker
1350*1208bc7eSAndroid Build Coastguard Worker static void *
arena_malloc_small(tsdn_t * tsdn,arena_t * arena,szind_t binind,bool zero)1351*1208bc7eSAndroid Build Coastguard Worker arena_malloc_small(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero) {
1352*1208bc7eSAndroid Build Coastguard Worker void *ret;
1353*1208bc7eSAndroid Build Coastguard Worker bin_t *bin;
1354*1208bc7eSAndroid Build Coastguard Worker size_t usize;
1355*1208bc7eSAndroid Build Coastguard Worker extent_t *slab;
1356*1208bc7eSAndroid Build Coastguard Worker
1357*1208bc7eSAndroid Build Coastguard Worker assert(binind < NBINS);
1358*1208bc7eSAndroid Build Coastguard Worker bin = &arena->bins[binind];
1359*1208bc7eSAndroid Build Coastguard Worker usize = sz_index2size(binind);
1360*1208bc7eSAndroid Build Coastguard Worker
1361*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
1362*1208bc7eSAndroid Build Coastguard Worker if ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) > 0) {
1363*1208bc7eSAndroid Build Coastguard Worker ret = arena_slab_reg_alloc(slab, &bin_infos[binind]);
1364*1208bc7eSAndroid Build Coastguard Worker } else {
1365*1208bc7eSAndroid Build Coastguard Worker ret = arena_bin_malloc_hard(tsdn, arena, bin, binind);
1366*1208bc7eSAndroid Build Coastguard Worker }
1367*1208bc7eSAndroid Build Coastguard Worker
1368*1208bc7eSAndroid Build Coastguard Worker if (ret == NULL) {
1369*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1370*1208bc7eSAndroid Build Coastguard Worker return NULL;
1371*1208bc7eSAndroid Build Coastguard Worker }
1372*1208bc7eSAndroid Build Coastguard Worker
1373*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1374*1208bc7eSAndroid Build Coastguard Worker bin->stats.nmalloc++;
1375*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1376*1208bc7eSAndroid Build Coastguard Worker bin->stats.nrequests++;
1377*1208bc7eSAndroid Build Coastguard Worker #endif
1378*1208bc7eSAndroid Build Coastguard Worker bin->stats.curregs++;
1379*1208bc7eSAndroid Build Coastguard Worker }
1380*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1381*1208bc7eSAndroid Build Coastguard Worker if (config_prof && arena_prof_accum(tsdn, arena, usize)) {
1382*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsdn);
1383*1208bc7eSAndroid Build Coastguard Worker }
1384*1208bc7eSAndroid Build Coastguard Worker
1385*1208bc7eSAndroid Build Coastguard Worker if (!zero) {
1386*1208bc7eSAndroid Build Coastguard Worker if (config_fill) {
1387*1208bc7eSAndroid Build Coastguard Worker if (unlikely(opt_junk_alloc)) {
1388*1208bc7eSAndroid Build Coastguard Worker arena_alloc_junk_small(ret,
1389*1208bc7eSAndroid Build Coastguard Worker &bin_infos[binind], false);
1390*1208bc7eSAndroid Build Coastguard Worker } else if (unlikely(opt_zero)) {
1391*1208bc7eSAndroid Build Coastguard Worker memset(ret, 0, usize);
1392*1208bc7eSAndroid Build Coastguard Worker }
1393*1208bc7eSAndroid Build Coastguard Worker }
1394*1208bc7eSAndroid Build Coastguard Worker } else {
1395*1208bc7eSAndroid Build Coastguard Worker if (config_fill && unlikely(opt_junk_alloc)) {
1396*1208bc7eSAndroid Build Coastguard Worker arena_alloc_junk_small(ret, &bin_infos[binind],
1397*1208bc7eSAndroid Build Coastguard Worker true);
1398*1208bc7eSAndroid Build Coastguard Worker }
1399*1208bc7eSAndroid Build Coastguard Worker memset(ret, 0, usize);
1400*1208bc7eSAndroid Build Coastguard Worker }
1401*1208bc7eSAndroid Build Coastguard Worker
1402*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, arena);
1403*1208bc7eSAndroid Build Coastguard Worker return ret;
1404*1208bc7eSAndroid Build Coastguard Worker }
1405*1208bc7eSAndroid Build Coastguard Worker
1406*1208bc7eSAndroid Build Coastguard Worker void *
arena_malloc_hard(tsdn_t * tsdn,arena_t * arena,size_t size,szind_t ind,bool zero)1407*1208bc7eSAndroid Build Coastguard Worker arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,
1408*1208bc7eSAndroid Build Coastguard Worker bool zero) {
1409*1208bc7eSAndroid Build Coastguard Worker assert(!tsdn_null(tsdn) || arena != NULL);
1410*1208bc7eSAndroid Build Coastguard Worker
1411*1208bc7eSAndroid Build Coastguard Worker if (likely(!tsdn_null(tsdn))) {
1412*1208bc7eSAndroid Build Coastguard Worker arena = arena_choose(tsdn_tsd(tsdn), arena);
1413*1208bc7eSAndroid Build Coastguard Worker }
1414*1208bc7eSAndroid Build Coastguard Worker if (unlikely(arena == NULL)) {
1415*1208bc7eSAndroid Build Coastguard Worker return NULL;
1416*1208bc7eSAndroid Build Coastguard Worker }
1417*1208bc7eSAndroid Build Coastguard Worker
1418*1208bc7eSAndroid Build Coastguard Worker if (likely(size <= SMALL_MAXCLASS)) {
1419*1208bc7eSAndroid Build Coastguard Worker return arena_malloc_small(tsdn, arena, ind, zero);
1420*1208bc7eSAndroid Build Coastguard Worker }
1421*1208bc7eSAndroid Build Coastguard Worker return large_malloc(tsdn, arena, sz_index2size(ind), zero);
1422*1208bc7eSAndroid Build Coastguard Worker }
1423*1208bc7eSAndroid Build Coastguard Worker
1424*1208bc7eSAndroid Build Coastguard Worker void *
arena_palloc(tsdn_t * tsdn,arena_t * arena,size_t usize,size_t alignment,bool zero,tcache_t * tcache)1425*1208bc7eSAndroid Build Coastguard Worker arena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,
1426*1208bc7eSAndroid Build Coastguard Worker bool zero, tcache_t *tcache) {
1427*1208bc7eSAndroid Build Coastguard Worker void *ret;
1428*1208bc7eSAndroid Build Coastguard Worker
1429*1208bc7eSAndroid Build Coastguard Worker if (usize <= SMALL_MAXCLASS && (alignment < PAGE || (alignment == PAGE
1430*1208bc7eSAndroid Build Coastguard Worker && (usize & PAGE_MASK) == 0))) {
1431*1208bc7eSAndroid Build Coastguard Worker /* Small; alignment doesn't require special slab placement. */
1432*1208bc7eSAndroid Build Coastguard Worker ret = arena_malloc(tsdn, arena, usize, sz_size2index(usize),
1433*1208bc7eSAndroid Build Coastguard Worker zero, tcache, true);
1434*1208bc7eSAndroid Build Coastguard Worker } else {
1435*1208bc7eSAndroid Build Coastguard Worker if (likely(alignment <= CACHELINE)) {
1436*1208bc7eSAndroid Build Coastguard Worker ret = large_malloc(tsdn, arena, usize, zero);
1437*1208bc7eSAndroid Build Coastguard Worker } else {
1438*1208bc7eSAndroid Build Coastguard Worker ret = large_palloc(tsdn, arena, usize, alignment, zero);
1439*1208bc7eSAndroid Build Coastguard Worker }
1440*1208bc7eSAndroid Build Coastguard Worker }
1441*1208bc7eSAndroid Build Coastguard Worker return ret;
1442*1208bc7eSAndroid Build Coastguard Worker }
1443*1208bc7eSAndroid Build Coastguard Worker
1444*1208bc7eSAndroid Build Coastguard Worker void
arena_prof_promote(tsdn_t * tsdn,const void * ptr,size_t usize)1445*1208bc7eSAndroid Build Coastguard Worker arena_prof_promote(tsdn_t *tsdn, const void *ptr, size_t usize) {
1446*1208bc7eSAndroid Build Coastguard Worker cassert(config_prof);
1447*1208bc7eSAndroid Build Coastguard Worker assert(ptr != NULL);
1448*1208bc7eSAndroid Build Coastguard Worker assert(isalloc(tsdn, ptr) == LARGE_MINCLASS);
1449*1208bc7eSAndroid Build Coastguard Worker assert(usize <= SMALL_MAXCLASS);
1450*1208bc7eSAndroid Build Coastguard Worker
1451*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t rtree_ctx_fallback;
1452*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
1453*1208bc7eSAndroid Build Coastguard Worker
1454*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,
1455*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)ptr, true);
1456*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = extent_arena_get(extent);
1457*1208bc7eSAndroid Build Coastguard Worker
1458*1208bc7eSAndroid Build Coastguard Worker szind_t szind = sz_size2index(usize);
1459*1208bc7eSAndroid Build Coastguard Worker extent_szind_set(extent, szind);
1460*1208bc7eSAndroid Build Coastguard Worker rtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,
1461*1208bc7eSAndroid Build Coastguard Worker szind, false);
1462*1208bc7eSAndroid Build Coastguard Worker
1463*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1464*1208bc7eSAndroid Build Coastguard Worker prof_accum_cancel(tsdn, &arena->prof_accum, usize);
1465*1208bc7eSAndroid Build Coastguard Worker #endif
1466*1208bc7eSAndroid Build Coastguard Worker
1467*1208bc7eSAndroid Build Coastguard Worker assert(isalloc(tsdn, ptr) == usize);
1468*1208bc7eSAndroid Build Coastguard Worker }
1469*1208bc7eSAndroid Build Coastguard Worker
1470*1208bc7eSAndroid Build Coastguard Worker static size_t
arena_prof_demote(tsdn_t * tsdn,extent_t * extent,const void * ptr)1471*1208bc7eSAndroid Build Coastguard Worker arena_prof_demote(tsdn_t *tsdn, extent_t *extent, const void *ptr) {
1472*1208bc7eSAndroid Build Coastguard Worker cassert(config_prof);
1473*1208bc7eSAndroid Build Coastguard Worker assert(ptr != NULL);
1474*1208bc7eSAndroid Build Coastguard Worker
1475*1208bc7eSAndroid Build Coastguard Worker extent_szind_set(extent, NBINS);
1476*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t rtree_ctx_fallback;
1477*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
1478*1208bc7eSAndroid Build Coastguard Worker rtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,
1479*1208bc7eSAndroid Build Coastguard Worker NBINS, false);
1480*1208bc7eSAndroid Build Coastguard Worker
1481*1208bc7eSAndroid Build Coastguard Worker assert(isalloc(tsdn, ptr) == LARGE_MINCLASS);
1482*1208bc7eSAndroid Build Coastguard Worker
1483*1208bc7eSAndroid Build Coastguard Worker return LARGE_MINCLASS;
1484*1208bc7eSAndroid Build Coastguard Worker }
1485*1208bc7eSAndroid Build Coastguard Worker
1486*1208bc7eSAndroid Build Coastguard Worker void
arena_dalloc_promoted(tsdn_t * tsdn,void * ptr,tcache_t * tcache,bool slow_path)1487*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,
1488*1208bc7eSAndroid Build Coastguard Worker bool slow_path) {
1489*1208bc7eSAndroid Build Coastguard Worker cassert(config_prof);
1490*1208bc7eSAndroid Build Coastguard Worker assert(opt_prof);
1491*1208bc7eSAndroid Build Coastguard Worker
1492*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = iealloc(tsdn, ptr);
1493*1208bc7eSAndroid Build Coastguard Worker size_t usize = arena_prof_demote(tsdn, extent, ptr);
1494*1208bc7eSAndroid Build Coastguard Worker if (usize <= tcache_maxclass) {
1495*1208bc7eSAndroid Build Coastguard Worker tcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr,
1496*1208bc7eSAndroid Build Coastguard Worker sz_size2index(usize), slow_path);
1497*1208bc7eSAndroid Build Coastguard Worker } else {
1498*1208bc7eSAndroid Build Coastguard Worker large_dalloc(tsdn, extent);
1499*1208bc7eSAndroid Build Coastguard Worker }
1500*1208bc7eSAndroid Build Coastguard Worker }
1501*1208bc7eSAndroid Build Coastguard Worker
1502*1208bc7eSAndroid Build Coastguard Worker static void
arena_dissociate_bin_slab(arena_t * arena,extent_t * slab,bin_t * bin)1503*1208bc7eSAndroid Build Coastguard Worker arena_dissociate_bin_slab(arena_t *arena, extent_t *slab, bin_t *bin) {
1504*1208bc7eSAndroid Build Coastguard Worker /* Dissociate slab from bin. */
1505*1208bc7eSAndroid Build Coastguard Worker if (slab == bin->slabcur) {
1506*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = NULL;
1507*1208bc7eSAndroid Build Coastguard Worker } else {
1508*1208bc7eSAndroid Build Coastguard Worker szind_t binind = extent_szind_get(slab);
1509*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info = &bin_infos[binind];
1510*1208bc7eSAndroid Build Coastguard Worker
1511*1208bc7eSAndroid Build Coastguard Worker /*
1512*1208bc7eSAndroid Build Coastguard Worker * The following block's conditional is necessary because if the
1513*1208bc7eSAndroid Build Coastguard Worker * slab only contains one region, then it never gets inserted
1514*1208bc7eSAndroid Build Coastguard Worker * into the non-full slabs heap.
1515*1208bc7eSAndroid Build Coastguard Worker */
1516*1208bc7eSAndroid Build Coastguard Worker if (bin_info->nregs == 1) {
1517*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_remove(arena, bin, slab);
1518*1208bc7eSAndroid Build Coastguard Worker } else {
1519*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_remove(bin, slab);
1520*1208bc7eSAndroid Build Coastguard Worker }
1521*1208bc7eSAndroid Build Coastguard Worker }
1522*1208bc7eSAndroid Build Coastguard Worker }
1523*1208bc7eSAndroid Build Coastguard Worker
1524*1208bc7eSAndroid Build Coastguard Worker static void
arena_dalloc_bin_slab(tsdn_t * tsdn,arena_t * arena,extent_t * slab,bin_t * bin)1525*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,
1526*1208bc7eSAndroid Build Coastguard Worker bin_t *bin) {
1527*1208bc7eSAndroid Build Coastguard Worker assert(slab != bin->slabcur);
1528*1208bc7eSAndroid Build Coastguard Worker
1529*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1530*1208bc7eSAndroid Build Coastguard Worker /******************************/
1531*1208bc7eSAndroid Build Coastguard Worker arena_slab_dalloc(tsdn, arena, slab);
1532*1208bc7eSAndroid Build Coastguard Worker /****************************/
1533*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
1534*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1535*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1536*1208bc7eSAndroid Build Coastguard Worker bin->stats.curslabs--;
1537*1208bc7eSAndroid Build Coastguard Worker }
1538*1208bc7eSAndroid Build Coastguard Worker #endif
1539*1208bc7eSAndroid Build Coastguard Worker }
1540*1208bc7eSAndroid Build Coastguard Worker
1541*1208bc7eSAndroid Build Coastguard Worker static void
arena_bin_lower_slab(UNUSED tsdn_t * tsdn,arena_t * arena,extent_t * slab,bin_t * bin)1542*1208bc7eSAndroid Build Coastguard Worker arena_bin_lower_slab(UNUSED tsdn_t *tsdn, arena_t *arena, extent_t *slab,
1543*1208bc7eSAndroid Build Coastguard Worker bin_t *bin) {
1544*1208bc7eSAndroid Build Coastguard Worker assert(extent_nfree_get(slab) > 0);
1545*1208bc7eSAndroid Build Coastguard Worker
1546*1208bc7eSAndroid Build Coastguard Worker /*
1547*1208bc7eSAndroid Build Coastguard Worker * Make sure that if bin->slabcur is non-NULL, it refers to the
1548*1208bc7eSAndroid Build Coastguard Worker * oldest/lowest non-full slab. It is okay to NULL slabcur out rather
1549*1208bc7eSAndroid Build Coastguard Worker * than proactively keeping it pointing at the oldest/lowest non-full
1550*1208bc7eSAndroid Build Coastguard Worker * slab.
1551*1208bc7eSAndroid Build Coastguard Worker */
1552*1208bc7eSAndroid Build Coastguard Worker if (bin->slabcur != NULL && extent_snad_comp(bin->slabcur, slab) > 0) {
1553*1208bc7eSAndroid Build Coastguard Worker /* Switch slabcur. */
1554*1208bc7eSAndroid Build Coastguard Worker if (extent_nfree_get(bin->slabcur) > 0) {
1555*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_insert(bin, bin->slabcur);
1556*1208bc7eSAndroid Build Coastguard Worker } else {
1557*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_insert(arena, bin, bin->slabcur);
1558*1208bc7eSAndroid Build Coastguard Worker }
1559*1208bc7eSAndroid Build Coastguard Worker bin->slabcur = slab;
1560*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1561*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1562*1208bc7eSAndroid Build Coastguard Worker bin->stats.reslabs++;
1563*1208bc7eSAndroid Build Coastguard Worker }
1564*1208bc7eSAndroid Build Coastguard Worker #endif
1565*1208bc7eSAndroid Build Coastguard Worker } else {
1566*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_nonfull_insert(bin, slab);
1567*1208bc7eSAndroid Build Coastguard Worker }
1568*1208bc7eSAndroid Build Coastguard Worker }
1569*1208bc7eSAndroid Build Coastguard Worker
1570*1208bc7eSAndroid Build Coastguard Worker static void
arena_dalloc_bin_locked_impl(tsdn_t * tsdn,arena_t * arena,extent_t * slab,void * ptr,bool junked)1571*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_locked_impl(tsdn_t *tsdn, arena_t *arena, extent_t *slab,
1572*1208bc7eSAndroid Build Coastguard Worker void *ptr, bool junked) {
1573*1208bc7eSAndroid Build Coastguard Worker arena_slab_data_t *slab_data = extent_slab_data_get(slab);
1574*1208bc7eSAndroid Build Coastguard Worker szind_t binind = extent_szind_get(slab);
1575*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &arena->bins[binind];
1576*1208bc7eSAndroid Build Coastguard Worker const bin_info_t *bin_info = &bin_infos[binind];
1577*1208bc7eSAndroid Build Coastguard Worker
1578*1208bc7eSAndroid Build Coastguard Worker if (!junked && config_fill && unlikely(opt_junk_free)) {
1579*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_junk_small(ptr, bin_info);
1580*1208bc7eSAndroid Build Coastguard Worker }
1581*1208bc7eSAndroid Build Coastguard Worker
1582*1208bc7eSAndroid Build Coastguard Worker arena_slab_reg_dalloc(slab, slab_data, ptr);
1583*1208bc7eSAndroid Build Coastguard Worker unsigned nfree = extent_nfree_get(slab);
1584*1208bc7eSAndroid Build Coastguard Worker if (nfree == bin_info->nregs) {
1585*1208bc7eSAndroid Build Coastguard Worker arena_dissociate_bin_slab(arena, slab, bin);
1586*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_slab(tsdn, arena, slab, bin);
1587*1208bc7eSAndroid Build Coastguard Worker } else if (nfree == 1 && slab != bin->slabcur) {
1588*1208bc7eSAndroid Build Coastguard Worker arena_bin_slabs_full_remove(arena, bin, slab);
1589*1208bc7eSAndroid Build Coastguard Worker arena_bin_lower_slab(tsdn, arena, slab, bin);
1590*1208bc7eSAndroid Build Coastguard Worker }
1591*1208bc7eSAndroid Build Coastguard Worker
1592*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1593*1208bc7eSAndroid Build Coastguard Worker bin->stats.ndalloc++;
1594*1208bc7eSAndroid Build Coastguard Worker bin->stats.curregs--;
1595*1208bc7eSAndroid Build Coastguard Worker }
1596*1208bc7eSAndroid Build Coastguard Worker }
1597*1208bc7eSAndroid Build Coastguard Worker
1598*1208bc7eSAndroid Build Coastguard Worker void
arena_dalloc_bin_junked_locked(tsdn_t * tsdn,arena_t * arena,extent_t * extent,void * ptr)1599*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena, extent_t *extent,
1600*1208bc7eSAndroid Build Coastguard Worker void *ptr) {
1601*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_locked_impl(tsdn, arena, extent, ptr, true);
1602*1208bc7eSAndroid Build Coastguard Worker }
1603*1208bc7eSAndroid Build Coastguard Worker
1604*1208bc7eSAndroid Build Coastguard Worker static void
arena_dalloc_bin(tsdn_t * tsdn,arena_t * arena,extent_t * extent,void * ptr)1605*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin(tsdn_t *tsdn, arena_t *arena, extent_t *extent, void *ptr) {
1606*1208bc7eSAndroid Build Coastguard Worker szind_t binind = extent_szind_get(extent);
1607*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &arena->bins[binind];
1608*1208bc7eSAndroid Build Coastguard Worker
1609*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
1610*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_locked_impl(tsdn, arena, extent, ptr, false);
1611*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
1612*1208bc7eSAndroid Build Coastguard Worker }
1613*1208bc7eSAndroid Build Coastguard Worker
1614*1208bc7eSAndroid Build Coastguard Worker void
arena_dalloc_small(tsdn_t * tsdn,void * ptr)1615*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_small(tsdn_t *tsdn, void *ptr) {
1616*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = iealloc(tsdn, ptr);
1617*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = extent_arena_get(extent);
1618*1208bc7eSAndroid Build Coastguard Worker
1619*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin(tsdn, arena, extent, ptr);
1620*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, arena);
1621*1208bc7eSAndroid Build Coastguard Worker }
1622*1208bc7eSAndroid Build Coastguard Worker
1623*1208bc7eSAndroid Build Coastguard Worker bool
arena_ralloc_no_move(tsdn_t * tsdn,void * ptr,size_t oldsize,size_t size,size_t extra,bool zero)1624*1208bc7eSAndroid Build Coastguard Worker arena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,
1625*1208bc7eSAndroid Build Coastguard Worker size_t extra, bool zero) {
1626*1208bc7eSAndroid Build Coastguard Worker /* Calls with non-zero extra had to clamp extra. */
1627*1208bc7eSAndroid Build Coastguard Worker assert(extra == 0 || size + extra <= LARGE_MAXCLASS);
1628*1208bc7eSAndroid Build Coastguard Worker
1629*1208bc7eSAndroid Build Coastguard Worker if (unlikely(size > LARGE_MAXCLASS)) {
1630*1208bc7eSAndroid Build Coastguard Worker return true;
1631*1208bc7eSAndroid Build Coastguard Worker }
1632*1208bc7eSAndroid Build Coastguard Worker
1633*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = iealloc(tsdn, ptr);
1634*1208bc7eSAndroid Build Coastguard Worker size_t usize_min = sz_s2u(size);
1635*1208bc7eSAndroid Build Coastguard Worker size_t usize_max = sz_s2u(size + extra);
1636*1208bc7eSAndroid Build Coastguard Worker if (likely(oldsize <= SMALL_MAXCLASS && usize_min <= SMALL_MAXCLASS)) {
1637*1208bc7eSAndroid Build Coastguard Worker /*
1638*1208bc7eSAndroid Build Coastguard Worker * Avoid moving the allocation if the size class can be left the
1639*1208bc7eSAndroid Build Coastguard Worker * same.
1640*1208bc7eSAndroid Build Coastguard Worker */
1641*1208bc7eSAndroid Build Coastguard Worker assert(bin_infos[sz_size2index(oldsize)].reg_size ==
1642*1208bc7eSAndroid Build Coastguard Worker oldsize);
1643*1208bc7eSAndroid Build Coastguard Worker if ((usize_max > SMALL_MAXCLASS || sz_size2index(usize_max) !=
1644*1208bc7eSAndroid Build Coastguard Worker sz_size2index(oldsize)) && (size > oldsize || usize_max <
1645*1208bc7eSAndroid Build Coastguard Worker oldsize)) {
1646*1208bc7eSAndroid Build Coastguard Worker return true;
1647*1208bc7eSAndroid Build Coastguard Worker }
1648*1208bc7eSAndroid Build Coastguard Worker
1649*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, extent_arena_get(extent));
1650*1208bc7eSAndroid Build Coastguard Worker return false;
1651*1208bc7eSAndroid Build Coastguard Worker } else if (oldsize >= LARGE_MINCLASS && usize_max >= LARGE_MINCLASS) {
1652*1208bc7eSAndroid Build Coastguard Worker return large_ralloc_no_move(tsdn, extent, usize_min, usize_max,
1653*1208bc7eSAndroid Build Coastguard Worker zero);
1654*1208bc7eSAndroid Build Coastguard Worker }
1655*1208bc7eSAndroid Build Coastguard Worker
1656*1208bc7eSAndroid Build Coastguard Worker return true;
1657*1208bc7eSAndroid Build Coastguard Worker }
1658*1208bc7eSAndroid Build Coastguard Worker
1659*1208bc7eSAndroid Build Coastguard Worker static void *
arena_ralloc_move_helper(tsdn_t * tsdn,arena_t * arena,size_t usize,size_t alignment,bool zero,tcache_t * tcache)1660*1208bc7eSAndroid Build Coastguard Worker arena_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,
1661*1208bc7eSAndroid Build Coastguard Worker size_t alignment, bool zero, tcache_t *tcache) {
1662*1208bc7eSAndroid Build Coastguard Worker if (alignment == 0) {
1663*1208bc7eSAndroid Build Coastguard Worker return arena_malloc(tsdn, arena, usize, sz_size2index(usize),
1664*1208bc7eSAndroid Build Coastguard Worker zero, tcache, true);
1665*1208bc7eSAndroid Build Coastguard Worker }
1666*1208bc7eSAndroid Build Coastguard Worker usize = sz_sa2u(usize, alignment);
1667*1208bc7eSAndroid Build Coastguard Worker if (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {
1668*1208bc7eSAndroid Build Coastguard Worker return NULL;
1669*1208bc7eSAndroid Build Coastguard Worker }
1670*1208bc7eSAndroid Build Coastguard Worker return ipalloct(tsdn, usize, alignment, zero, tcache, arena);
1671*1208bc7eSAndroid Build Coastguard Worker }
1672*1208bc7eSAndroid Build Coastguard Worker
1673*1208bc7eSAndroid Build Coastguard Worker void *
arena_ralloc(tsdn_t * tsdn,arena_t * arena,void * ptr,size_t oldsize,size_t size,size_t alignment,bool zero,tcache_t * tcache)1674*1208bc7eSAndroid Build Coastguard Worker arena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,
1675*1208bc7eSAndroid Build Coastguard Worker size_t size, size_t alignment, bool zero, tcache_t *tcache) {
1676*1208bc7eSAndroid Build Coastguard Worker size_t usize = sz_s2u(size);
1677*1208bc7eSAndroid Build Coastguard Worker if (unlikely(usize == 0 || size > LARGE_MAXCLASS)) {
1678*1208bc7eSAndroid Build Coastguard Worker return NULL;
1679*1208bc7eSAndroid Build Coastguard Worker }
1680*1208bc7eSAndroid Build Coastguard Worker
1681*1208bc7eSAndroid Build Coastguard Worker if (likely(usize <= SMALL_MAXCLASS)) {
1682*1208bc7eSAndroid Build Coastguard Worker /* Try to avoid moving the allocation. */
1683*1208bc7eSAndroid Build Coastguard Worker if (!arena_ralloc_no_move(tsdn, ptr, oldsize, usize, 0, zero)) {
1684*1208bc7eSAndroid Build Coastguard Worker return ptr;
1685*1208bc7eSAndroid Build Coastguard Worker }
1686*1208bc7eSAndroid Build Coastguard Worker }
1687*1208bc7eSAndroid Build Coastguard Worker
1688*1208bc7eSAndroid Build Coastguard Worker if (oldsize >= LARGE_MINCLASS && usize >= LARGE_MINCLASS) {
1689*1208bc7eSAndroid Build Coastguard Worker return large_ralloc(tsdn, arena, iealloc(tsdn, ptr), usize,
1690*1208bc7eSAndroid Build Coastguard Worker alignment, zero, tcache);
1691*1208bc7eSAndroid Build Coastguard Worker }
1692*1208bc7eSAndroid Build Coastguard Worker
1693*1208bc7eSAndroid Build Coastguard Worker /*
1694*1208bc7eSAndroid Build Coastguard Worker * size and oldsize are different enough that we need to move the
1695*1208bc7eSAndroid Build Coastguard Worker * object. In that case, fall back to allocating new space and copying.
1696*1208bc7eSAndroid Build Coastguard Worker */
1697*1208bc7eSAndroid Build Coastguard Worker void *ret = arena_ralloc_move_helper(tsdn, arena, usize, alignment,
1698*1208bc7eSAndroid Build Coastguard Worker zero, tcache);
1699*1208bc7eSAndroid Build Coastguard Worker if (ret == NULL) {
1700*1208bc7eSAndroid Build Coastguard Worker return NULL;
1701*1208bc7eSAndroid Build Coastguard Worker }
1702*1208bc7eSAndroid Build Coastguard Worker
1703*1208bc7eSAndroid Build Coastguard Worker /*
1704*1208bc7eSAndroid Build Coastguard Worker * Junk/zero-filling were already done by
1705*1208bc7eSAndroid Build Coastguard Worker * ipalloc()/arena_malloc().
1706*1208bc7eSAndroid Build Coastguard Worker */
1707*1208bc7eSAndroid Build Coastguard Worker
1708*1208bc7eSAndroid Build Coastguard Worker size_t copysize = (usize < oldsize) ? usize : oldsize;
1709*1208bc7eSAndroid Build Coastguard Worker memcpy(ret, ptr, copysize);
1710*1208bc7eSAndroid Build Coastguard Worker isdalloct(tsdn, ptr, oldsize, tcache, NULL, true);
1711*1208bc7eSAndroid Build Coastguard Worker return ret;
1712*1208bc7eSAndroid Build Coastguard Worker }
1713*1208bc7eSAndroid Build Coastguard Worker
1714*1208bc7eSAndroid Build Coastguard Worker dss_prec_t
arena_dss_prec_get(arena_t * arena)1715*1208bc7eSAndroid Build Coastguard Worker arena_dss_prec_get(arena_t *arena) {
1716*1208bc7eSAndroid Build Coastguard Worker return (dss_prec_t)atomic_load_u(&arena->dss_prec, ATOMIC_ACQUIRE);
1717*1208bc7eSAndroid Build Coastguard Worker }
1718*1208bc7eSAndroid Build Coastguard Worker
1719*1208bc7eSAndroid Build Coastguard Worker bool
arena_dss_prec_set(arena_t * arena,dss_prec_t dss_prec)1720*1208bc7eSAndroid Build Coastguard Worker arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) {
1721*1208bc7eSAndroid Build Coastguard Worker if (!have_dss) {
1722*1208bc7eSAndroid Build Coastguard Worker return (dss_prec != dss_prec_disabled);
1723*1208bc7eSAndroid Build Coastguard Worker }
1724*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->dss_prec, (unsigned)dss_prec, ATOMIC_RELEASE);
1725*1208bc7eSAndroid Build Coastguard Worker return false;
1726*1208bc7eSAndroid Build Coastguard Worker }
1727*1208bc7eSAndroid Build Coastguard Worker
1728*1208bc7eSAndroid Build Coastguard Worker ssize_t
arena_dirty_decay_ms_default_get(void)1729*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_default_get(void) {
1730*1208bc7eSAndroid Build Coastguard Worker return atomic_load_zd(&dirty_decay_ms_default, ATOMIC_RELAXED);
1731*1208bc7eSAndroid Build Coastguard Worker }
1732*1208bc7eSAndroid Build Coastguard Worker
1733*1208bc7eSAndroid Build Coastguard Worker bool
arena_dirty_decay_ms_default_set(ssize_t decay_ms)1734*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_default_set(ssize_t decay_ms) {
1735*1208bc7eSAndroid Build Coastguard Worker if (!arena_decay_ms_valid(decay_ms)) {
1736*1208bc7eSAndroid Build Coastguard Worker return true;
1737*1208bc7eSAndroid Build Coastguard Worker }
1738*1208bc7eSAndroid Build Coastguard Worker atomic_store_zd(&dirty_decay_ms_default, decay_ms, ATOMIC_RELAXED);
1739*1208bc7eSAndroid Build Coastguard Worker return false;
1740*1208bc7eSAndroid Build Coastguard Worker }
1741*1208bc7eSAndroid Build Coastguard Worker
1742*1208bc7eSAndroid Build Coastguard Worker ssize_t
arena_muzzy_decay_ms_default_get(void)1743*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_default_get(void) {
1744*1208bc7eSAndroid Build Coastguard Worker return atomic_load_zd(&muzzy_decay_ms_default, ATOMIC_RELAXED);
1745*1208bc7eSAndroid Build Coastguard Worker }
1746*1208bc7eSAndroid Build Coastguard Worker
1747*1208bc7eSAndroid Build Coastguard Worker bool
arena_muzzy_decay_ms_default_set(ssize_t decay_ms)1748*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_default_set(ssize_t decay_ms) {
1749*1208bc7eSAndroid Build Coastguard Worker if (!arena_decay_ms_valid(decay_ms)) {
1750*1208bc7eSAndroid Build Coastguard Worker return true;
1751*1208bc7eSAndroid Build Coastguard Worker }
1752*1208bc7eSAndroid Build Coastguard Worker atomic_store_zd(&muzzy_decay_ms_default, decay_ms, ATOMIC_RELAXED);
1753*1208bc7eSAndroid Build Coastguard Worker return false;
1754*1208bc7eSAndroid Build Coastguard Worker }
1755*1208bc7eSAndroid Build Coastguard Worker
1756*1208bc7eSAndroid Build Coastguard Worker bool
arena_retain_grow_limit_get_set(tsd_t * tsd,arena_t * arena,size_t * old_limit,size_t * new_limit)1757*1208bc7eSAndroid Build Coastguard Worker arena_retain_grow_limit_get_set(tsd_t *tsd, arena_t *arena, size_t *old_limit,
1758*1208bc7eSAndroid Build Coastguard Worker size_t *new_limit) {
1759*1208bc7eSAndroid Build Coastguard Worker assert(opt_retain);
1760*1208bc7eSAndroid Build Coastguard Worker
1761*1208bc7eSAndroid Build Coastguard Worker pszind_t new_ind JEMALLOC_CC_SILENCE_INIT(0);
1762*1208bc7eSAndroid Build Coastguard Worker if (new_limit != NULL) {
1763*1208bc7eSAndroid Build Coastguard Worker size_t limit = *new_limit;
1764*1208bc7eSAndroid Build Coastguard Worker /* Grow no more than the new limit. */
1765*1208bc7eSAndroid Build Coastguard Worker if ((new_ind = sz_psz2ind(limit + 1) - 1) >
1766*1208bc7eSAndroid Build Coastguard Worker EXTENT_GROW_MAX_PIND) {
1767*1208bc7eSAndroid Build Coastguard Worker return true;
1768*1208bc7eSAndroid Build Coastguard Worker }
1769*1208bc7eSAndroid Build Coastguard Worker }
1770*1208bc7eSAndroid Build Coastguard Worker
1771*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &arena->extent_grow_mtx);
1772*1208bc7eSAndroid Build Coastguard Worker if (old_limit != NULL) {
1773*1208bc7eSAndroid Build Coastguard Worker *old_limit = sz_pind2sz(arena->retain_grow_limit);
1774*1208bc7eSAndroid Build Coastguard Worker }
1775*1208bc7eSAndroid Build Coastguard Worker if (new_limit != NULL) {
1776*1208bc7eSAndroid Build Coastguard Worker arena->retain_grow_limit = new_ind;
1777*1208bc7eSAndroid Build Coastguard Worker }
1778*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &arena->extent_grow_mtx);
1779*1208bc7eSAndroid Build Coastguard Worker
1780*1208bc7eSAndroid Build Coastguard Worker return false;
1781*1208bc7eSAndroid Build Coastguard Worker }
1782*1208bc7eSAndroid Build Coastguard Worker
1783*1208bc7eSAndroid Build Coastguard Worker unsigned
arena_nthreads_get(arena_t * arena,bool internal)1784*1208bc7eSAndroid Build Coastguard Worker arena_nthreads_get(arena_t *arena, bool internal) {
1785*1208bc7eSAndroid Build Coastguard Worker return atomic_load_u(&arena->nthreads[internal], ATOMIC_RELAXED);
1786*1208bc7eSAndroid Build Coastguard Worker }
1787*1208bc7eSAndroid Build Coastguard Worker
1788*1208bc7eSAndroid Build Coastguard Worker void
arena_nthreads_inc(arena_t * arena,bool internal)1789*1208bc7eSAndroid Build Coastguard Worker arena_nthreads_inc(arena_t *arena, bool internal) {
1790*1208bc7eSAndroid Build Coastguard Worker atomic_fetch_add_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);
1791*1208bc7eSAndroid Build Coastguard Worker }
1792*1208bc7eSAndroid Build Coastguard Worker
1793*1208bc7eSAndroid Build Coastguard Worker void
arena_nthreads_dec(arena_t * arena,bool internal)1794*1208bc7eSAndroid Build Coastguard Worker arena_nthreads_dec(arena_t *arena, bool internal) {
1795*1208bc7eSAndroid Build Coastguard Worker atomic_fetch_sub_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);
1796*1208bc7eSAndroid Build Coastguard Worker }
1797*1208bc7eSAndroid Build Coastguard Worker
1798*1208bc7eSAndroid Build Coastguard Worker size_t
arena_extent_sn_next(arena_t * arena)1799*1208bc7eSAndroid Build Coastguard Worker arena_extent_sn_next(arena_t *arena) {
1800*1208bc7eSAndroid Build Coastguard Worker return atomic_fetch_add_zu(&arena->extent_sn_next, 1, ATOMIC_RELAXED);
1801*1208bc7eSAndroid Build Coastguard Worker }
1802*1208bc7eSAndroid Build Coastguard Worker
1803*1208bc7eSAndroid Build Coastguard Worker arena_t *
arena_new(tsdn_t * tsdn,unsigned ind,extent_hooks_t * extent_hooks)1804*1208bc7eSAndroid Build Coastguard Worker arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {
1805*1208bc7eSAndroid Build Coastguard Worker arena_t *arena;
1806*1208bc7eSAndroid Build Coastguard Worker base_t *base;
1807*1208bc7eSAndroid Build Coastguard Worker unsigned i;
1808*1208bc7eSAndroid Build Coastguard Worker
1809*1208bc7eSAndroid Build Coastguard Worker if (ind == 0) {
1810*1208bc7eSAndroid Build Coastguard Worker base = b0get();
1811*1208bc7eSAndroid Build Coastguard Worker } else {
1812*1208bc7eSAndroid Build Coastguard Worker base = base_new(tsdn, ind, extent_hooks);
1813*1208bc7eSAndroid Build Coastguard Worker if (base == NULL) {
1814*1208bc7eSAndroid Build Coastguard Worker return NULL;
1815*1208bc7eSAndroid Build Coastguard Worker }
1816*1208bc7eSAndroid Build Coastguard Worker }
1817*1208bc7eSAndroid Build Coastguard Worker
1818*1208bc7eSAndroid Build Coastguard Worker arena = (arena_t *)base_alloc(tsdn, base, sizeof(arena_t), CACHELINE);
1819*1208bc7eSAndroid Build Coastguard Worker if (arena == NULL) {
1820*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1821*1208bc7eSAndroid Build Coastguard Worker }
1822*1208bc7eSAndroid Build Coastguard Worker
1823*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);
1824*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);
1825*1208bc7eSAndroid Build Coastguard Worker arena->last_thd = NULL;
1826*1208bc7eSAndroid Build Coastguard Worker
1827*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1828*1208bc7eSAndroid Build Coastguard Worker if (arena_stats_init(tsdn, &arena->stats)) {
1829*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1830*1208bc7eSAndroid Build Coastguard Worker }
1831*1208bc7eSAndroid Build Coastguard Worker
1832*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
1833*1208bc7eSAndroid Build Coastguard Worker ql_new(&arena->tcache_ql);
1834*1208bc7eSAndroid Build Coastguard Worker ql_new(&arena->cache_bin_array_descriptor_ql);
1835*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&arena->tcache_ql_mtx, "tcache_ql",
1836*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_TCACHE_QL, malloc_mutex_rank_exclusive)) {
1837*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1838*1208bc7eSAndroid Build Coastguard Worker }
1839*1208bc7eSAndroid Build Coastguard Worker #endif
1840*1208bc7eSAndroid Build Coastguard Worker }
1841*1208bc7eSAndroid Build Coastguard Worker
1842*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1843*1208bc7eSAndroid Build Coastguard Worker if (config_prof) {
1844*1208bc7eSAndroid Build Coastguard Worker if (prof_accum_init(tsdn, &arena->prof_accum)) {
1845*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1846*1208bc7eSAndroid Build Coastguard Worker }
1847*1208bc7eSAndroid Build Coastguard Worker }
1848*1208bc7eSAndroid Build Coastguard Worker #endif
1849*1208bc7eSAndroid Build Coastguard Worker
1850*1208bc7eSAndroid Build Coastguard Worker if (config_cache_oblivious) {
1851*1208bc7eSAndroid Build Coastguard Worker /*
1852*1208bc7eSAndroid Build Coastguard Worker * A nondeterministic seed based on the address of arena reduces
1853*1208bc7eSAndroid Build Coastguard Worker * the likelihood of lockstep non-uniform cache index
1854*1208bc7eSAndroid Build Coastguard Worker * utilization among identical concurrent processes, but at the
1855*1208bc7eSAndroid Build Coastguard Worker * cost of test repeatability. For debug builds, instead use a
1856*1208bc7eSAndroid Build Coastguard Worker * deterministic seed.
1857*1208bc7eSAndroid Build Coastguard Worker */
1858*1208bc7eSAndroid Build Coastguard Worker atomic_store_zu(&arena->offset_state, config_debug ? ind :
1859*1208bc7eSAndroid Build Coastguard Worker (size_t)(uintptr_t)arena, ATOMIC_RELAXED);
1860*1208bc7eSAndroid Build Coastguard Worker }
1861*1208bc7eSAndroid Build Coastguard Worker
1862*1208bc7eSAndroid Build Coastguard Worker atomic_store_zu(&arena->extent_sn_next, 0, ATOMIC_RELAXED);
1863*1208bc7eSAndroid Build Coastguard Worker
1864*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->dss_prec, (unsigned)extent_dss_prec_get(),
1865*1208bc7eSAndroid Build Coastguard Worker ATOMIC_RELAXED);
1866*1208bc7eSAndroid Build Coastguard Worker
1867*1208bc7eSAndroid Build Coastguard Worker atomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);
1868*1208bc7eSAndroid Build Coastguard Worker
1869*1208bc7eSAndroid Build Coastguard Worker extent_list_init(&arena->large);
1870*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&arena->large_mtx, "arena_large",
1871*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_ARENA_LARGE, malloc_mutex_rank_exclusive)) {
1872*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1873*1208bc7eSAndroid Build Coastguard Worker }
1874*1208bc7eSAndroid Build Coastguard Worker
1875*1208bc7eSAndroid Build Coastguard Worker /*
1876*1208bc7eSAndroid Build Coastguard Worker * Delay coalescing for dirty extents despite the disruptive effect on
1877*1208bc7eSAndroid Build Coastguard Worker * memory layout for best-fit extent allocation, since cached extents
1878*1208bc7eSAndroid Build Coastguard Worker * are likely to be reused soon after deallocation, and the cost of
1879*1208bc7eSAndroid Build Coastguard Worker * merging/splitting extents is non-trivial.
1880*1208bc7eSAndroid Build Coastguard Worker */
1881*1208bc7eSAndroid Build Coastguard Worker if (extents_init(tsdn, &arena->extents_dirty, extent_state_dirty,
1882*1208bc7eSAndroid Build Coastguard Worker true)) {
1883*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1884*1208bc7eSAndroid Build Coastguard Worker }
1885*1208bc7eSAndroid Build Coastguard Worker /*
1886*1208bc7eSAndroid Build Coastguard Worker * Coalesce muzzy extents immediately, because operations on them are in
1887*1208bc7eSAndroid Build Coastguard Worker * the critical path much less often than for dirty extents.
1888*1208bc7eSAndroid Build Coastguard Worker */
1889*1208bc7eSAndroid Build Coastguard Worker if (extents_init(tsdn, &arena->extents_muzzy, extent_state_muzzy,
1890*1208bc7eSAndroid Build Coastguard Worker false)) {
1891*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1892*1208bc7eSAndroid Build Coastguard Worker }
1893*1208bc7eSAndroid Build Coastguard Worker /*
1894*1208bc7eSAndroid Build Coastguard Worker * Coalesce retained extents immediately, in part because they will
1895*1208bc7eSAndroid Build Coastguard Worker * never be evicted (and therefore there's no opportunity for delayed
1896*1208bc7eSAndroid Build Coastguard Worker * coalescing), but also because operations on retained extents are not
1897*1208bc7eSAndroid Build Coastguard Worker * in the critical path.
1898*1208bc7eSAndroid Build Coastguard Worker */
1899*1208bc7eSAndroid Build Coastguard Worker if (extents_init(tsdn, &arena->extents_retained, extent_state_retained,
1900*1208bc7eSAndroid Build Coastguard Worker false)) {
1901*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1902*1208bc7eSAndroid Build Coastguard Worker }
1903*1208bc7eSAndroid Build Coastguard Worker
1904*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
1905*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_init(&arena->decay_dirty,
1906*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_default_get(), &arena->stats.decay_dirty)) {
1907*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1908*1208bc7eSAndroid Build Coastguard Worker }
1909*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_init(&arena->decay_muzzy,
1910*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_default_get(), &arena->stats.decay_muzzy)) {
1911*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1912*1208bc7eSAndroid Build Coastguard Worker }
1913*1208bc7eSAndroid Build Coastguard Worker #else
1914*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_init(&arena->decay_dirty,
1915*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_default_get(), NULL)) {
1916*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1917*1208bc7eSAndroid Build Coastguard Worker }
1918*1208bc7eSAndroid Build Coastguard Worker if (arena_decay_init(&arena->decay_muzzy,
1919*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_default_get(), NULL)) {
1920*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1921*1208bc7eSAndroid Build Coastguard Worker }
1922*1208bc7eSAndroid Build Coastguard Worker #endif
1923*1208bc7eSAndroid Build Coastguard Worker
1924*1208bc7eSAndroid Build Coastguard Worker arena->extent_grow_next = sz_psz2ind(HUGEPAGE);
1925*1208bc7eSAndroid Build Coastguard Worker arena->retain_grow_limit = EXTENT_GROW_MAX_PIND;
1926*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&arena->extent_grow_mtx, "extent_grow",
1927*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_EXTENT_GROW, malloc_mutex_rank_exclusive)) {
1928*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1929*1208bc7eSAndroid Build Coastguard Worker }
1930*1208bc7eSAndroid Build Coastguard Worker
1931*1208bc7eSAndroid Build Coastguard Worker extent_avail_new(&arena->extent_avail);
1932*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&arena->extent_avail_mtx, "extent_avail",
1933*1208bc7eSAndroid Build Coastguard Worker WITNESS_RANK_EXTENT_AVAIL, malloc_mutex_rank_exclusive)) {
1934*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1935*1208bc7eSAndroid Build Coastguard Worker }
1936*1208bc7eSAndroid Build Coastguard Worker
1937*1208bc7eSAndroid Build Coastguard Worker /* Initialize bins. */
1938*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NBINS; i++) {
1939*1208bc7eSAndroid Build Coastguard Worker bool err = bin_init(&arena->bins[i]);
1940*1208bc7eSAndroid Build Coastguard Worker if (err) {
1941*1208bc7eSAndroid Build Coastguard Worker goto label_error;
1942*1208bc7eSAndroid Build Coastguard Worker }
1943*1208bc7eSAndroid Build Coastguard Worker }
1944*1208bc7eSAndroid Build Coastguard Worker
1945*1208bc7eSAndroid Build Coastguard Worker arena->base = base;
1946*1208bc7eSAndroid Build Coastguard Worker /* Set arena before creating background threads. */
1947*1208bc7eSAndroid Build Coastguard Worker arena_set(ind, arena);
1948*1208bc7eSAndroid Build Coastguard Worker
1949*1208bc7eSAndroid Build Coastguard Worker nstime_init(&arena->create_time, 0);
1950*1208bc7eSAndroid Build Coastguard Worker nstime_update(&arena->create_time);
1951*1208bc7eSAndroid Build Coastguard Worker
1952*1208bc7eSAndroid Build Coastguard Worker /* We don't support reentrancy for arena 0 bootstrapping. */
1953*1208bc7eSAndroid Build Coastguard Worker if (ind != 0) {
1954*1208bc7eSAndroid Build Coastguard Worker /*
1955*1208bc7eSAndroid Build Coastguard Worker * If we're here, then arena 0 already exists, so bootstrapping
1956*1208bc7eSAndroid Build Coastguard Worker * is done enough that we should have tsd.
1957*1208bc7eSAndroid Build Coastguard Worker */
1958*1208bc7eSAndroid Build Coastguard Worker assert(!tsdn_null(tsdn));
1959*1208bc7eSAndroid Build Coastguard Worker pre_reentrancy(tsdn_tsd(tsdn), arena);
1960*1208bc7eSAndroid Build Coastguard Worker if (hooks_arena_new_hook) {
1961*1208bc7eSAndroid Build Coastguard Worker hooks_arena_new_hook();
1962*1208bc7eSAndroid Build Coastguard Worker }
1963*1208bc7eSAndroid Build Coastguard Worker post_reentrancy(tsdn_tsd(tsdn));
1964*1208bc7eSAndroid Build Coastguard Worker }
1965*1208bc7eSAndroid Build Coastguard Worker
1966*1208bc7eSAndroid Build Coastguard Worker return arena;
1967*1208bc7eSAndroid Build Coastguard Worker label_error:
1968*1208bc7eSAndroid Build Coastguard Worker if (ind != 0) {
1969*1208bc7eSAndroid Build Coastguard Worker base_delete(tsdn, base);
1970*1208bc7eSAndroid Build Coastguard Worker }
1971*1208bc7eSAndroid Build Coastguard Worker return NULL;
1972*1208bc7eSAndroid Build Coastguard Worker }
1973*1208bc7eSAndroid Build Coastguard Worker
1974*1208bc7eSAndroid Build Coastguard Worker void
arena_boot(void)1975*1208bc7eSAndroid Build Coastguard Worker arena_boot(void) {
1976*1208bc7eSAndroid Build Coastguard Worker arena_dirty_decay_ms_default_set(opt_dirty_decay_ms);
1977*1208bc7eSAndroid Build Coastguard Worker arena_muzzy_decay_ms_default_set(opt_muzzy_decay_ms);
1978*1208bc7eSAndroid Build Coastguard Worker #define REGIND_bin_yes(index, reg_size) \
1979*1208bc7eSAndroid Build Coastguard Worker div_init(&arena_binind_div_info[(index)], (reg_size));
1980*1208bc7eSAndroid Build Coastguard Worker #define REGIND_bin_no(index, reg_size)
1981*1208bc7eSAndroid Build Coastguard Worker #define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs, \
1982*1208bc7eSAndroid Build Coastguard Worker lg_delta_lookup) \
1983*1208bc7eSAndroid Build Coastguard Worker REGIND_bin_##bin(index, (1U<<lg_grp) + (ndelta << lg_delta))
1984*1208bc7eSAndroid Build Coastguard Worker SIZE_CLASSES
1985*1208bc7eSAndroid Build Coastguard Worker #undef REGIND_bin_yes
1986*1208bc7eSAndroid Build Coastguard Worker #undef REGIND_bin_no
1987*1208bc7eSAndroid Build Coastguard Worker #undef SC
1988*1208bc7eSAndroid Build Coastguard Worker }
1989*1208bc7eSAndroid Build Coastguard Worker
1990*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork0(tsdn_t * tsdn,arena_t * arena)1991*1208bc7eSAndroid Build Coastguard Worker arena_prefork0(tsdn_t *tsdn, arena_t *arena) {
1992*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->decay_dirty.mtx);
1993*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->decay_muzzy.mtx);
1994*1208bc7eSAndroid Build Coastguard Worker }
1995*1208bc7eSAndroid Build Coastguard Worker
1996*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork1(tsdn_t * tsdn,arena_t * arena)1997*1208bc7eSAndroid Build Coastguard Worker arena_prefork1(tsdn_t *tsdn, arena_t *arena) {
1998*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
1999*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
2000*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->tcache_ql_mtx);
2001*1208bc7eSAndroid Build Coastguard Worker #endif
2002*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_ATOMIC_U64
2003*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->stats.mtx);
2004*1208bc7eSAndroid Build Coastguard Worker #endif
2005*1208bc7eSAndroid Build Coastguard Worker }
2006*1208bc7eSAndroid Build Coastguard Worker }
2007*1208bc7eSAndroid Build Coastguard Worker
2008*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork2(tsdn_t * tsdn,arena_t * arena)2009*1208bc7eSAndroid Build Coastguard Worker arena_prefork2(tsdn_t *tsdn, arena_t *arena) {
2010*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->extent_grow_mtx);
2011*1208bc7eSAndroid Build Coastguard Worker }
2012*1208bc7eSAndroid Build Coastguard Worker
2013*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork3(tsdn_t * tsdn,arena_t * arena)2014*1208bc7eSAndroid Build Coastguard Worker arena_prefork3(tsdn_t *tsdn, arena_t *arena) {
2015*1208bc7eSAndroid Build Coastguard Worker extents_prefork(tsdn, &arena->extents_dirty);
2016*1208bc7eSAndroid Build Coastguard Worker extents_prefork(tsdn, &arena->extents_muzzy);
2017*1208bc7eSAndroid Build Coastguard Worker extents_prefork(tsdn, &arena->extents_retained);
2018*1208bc7eSAndroid Build Coastguard Worker }
2019*1208bc7eSAndroid Build Coastguard Worker
2020*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork4(tsdn_t * tsdn,arena_t * arena)2021*1208bc7eSAndroid Build Coastguard Worker arena_prefork4(tsdn_t *tsdn, arena_t *arena) {
2022*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->extent_avail_mtx);
2023*1208bc7eSAndroid Build Coastguard Worker }
2024*1208bc7eSAndroid Build Coastguard Worker
2025*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork5(tsdn_t * tsdn,arena_t * arena)2026*1208bc7eSAndroid Build Coastguard Worker arena_prefork5(tsdn_t *tsdn, arena_t *arena) {
2027*1208bc7eSAndroid Build Coastguard Worker base_prefork(tsdn, arena->base);
2028*1208bc7eSAndroid Build Coastguard Worker }
2029*1208bc7eSAndroid Build Coastguard Worker
2030*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork6(tsdn_t * tsdn,arena_t * arena)2031*1208bc7eSAndroid Build Coastguard Worker arena_prefork6(tsdn_t *tsdn, arena_t *arena) {
2032*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &arena->large_mtx);
2033*1208bc7eSAndroid Build Coastguard Worker }
2034*1208bc7eSAndroid Build Coastguard Worker
2035*1208bc7eSAndroid Build Coastguard Worker void
arena_prefork7(tsdn_t * tsdn,arena_t * arena)2036*1208bc7eSAndroid Build Coastguard Worker arena_prefork7(tsdn_t *tsdn, arena_t *arena) {
2037*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < NBINS; i++) {
2038*1208bc7eSAndroid Build Coastguard Worker bin_prefork(tsdn, &arena->bins[i]);
2039*1208bc7eSAndroid Build Coastguard Worker }
2040*1208bc7eSAndroid Build Coastguard Worker }
2041*1208bc7eSAndroid Build Coastguard Worker
2042*1208bc7eSAndroid Build Coastguard Worker void
arena_postfork_parent(tsdn_t * tsdn,arena_t * arena)2043*1208bc7eSAndroid Build Coastguard Worker arena_postfork_parent(tsdn_t *tsdn, arena_t *arena) {
2044*1208bc7eSAndroid Build Coastguard Worker unsigned i;
2045*1208bc7eSAndroid Build Coastguard Worker
2046*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NBINS; i++) {
2047*1208bc7eSAndroid Build Coastguard Worker bin_postfork_parent(tsdn, &arena->bins[i]);
2048*1208bc7eSAndroid Build Coastguard Worker }
2049*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->large_mtx);
2050*1208bc7eSAndroid Build Coastguard Worker base_postfork_parent(tsdn, arena->base);
2051*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->extent_avail_mtx);
2052*1208bc7eSAndroid Build Coastguard Worker extents_postfork_parent(tsdn, &arena->extents_retained);
2053*1208bc7eSAndroid Build Coastguard Worker extents_postfork_parent(tsdn, &arena->extents_muzzy);
2054*1208bc7eSAndroid Build Coastguard Worker extents_postfork_parent(tsdn, &arena->extents_dirty);
2055*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->extent_grow_mtx);
2056*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->decay_dirty.mtx);
2057*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->decay_muzzy.mtx);
2058*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
2059*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_ATOMIC_U64
2060*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->stats.mtx);
2061*1208bc7eSAndroid Build Coastguard Worker #endif
2062*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
2063*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &arena->tcache_ql_mtx);
2064*1208bc7eSAndroid Build Coastguard Worker #endif
2065*1208bc7eSAndroid Build Coastguard Worker }
2066*1208bc7eSAndroid Build Coastguard Worker }
2067*1208bc7eSAndroid Build Coastguard Worker
2068*1208bc7eSAndroid Build Coastguard Worker void
arena_postfork_child(tsdn_t * tsdn,arena_t * arena)2069*1208bc7eSAndroid Build Coastguard Worker arena_postfork_child(tsdn_t *tsdn, arena_t *arena) {
2070*1208bc7eSAndroid Build Coastguard Worker unsigned i;
2071*1208bc7eSAndroid Build Coastguard Worker
2072*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);
2073*1208bc7eSAndroid Build Coastguard Worker atomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);
2074*1208bc7eSAndroid Build Coastguard Worker if (tsd_arena_get(tsdn_tsd(tsdn)) == arena) {
2075*1208bc7eSAndroid Build Coastguard Worker arena_nthreads_inc(arena, false);
2076*1208bc7eSAndroid Build Coastguard Worker }
2077*1208bc7eSAndroid Build Coastguard Worker if (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) {
2078*1208bc7eSAndroid Build Coastguard Worker arena_nthreads_inc(arena, true);
2079*1208bc7eSAndroid Build Coastguard Worker }
2080*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
2081*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
2082*1208bc7eSAndroid Build Coastguard Worker ql_new(&arena->tcache_ql);
2083*1208bc7eSAndroid Build Coastguard Worker ql_new(&arena->cache_bin_array_descriptor_ql);
2084*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tcache_get(tsdn_tsd(tsdn));
2085*1208bc7eSAndroid Build Coastguard Worker if (tcache != NULL && tcache->arena == arena) {
2086*1208bc7eSAndroid Build Coastguard Worker ql_elm_new(tcache, link);
2087*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&arena->tcache_ql, tcache, link);
2088*1208bc7eSAndroid Build Coastguard Worker cache_bin_array_descriptor_init(
2089*1208bc7eSAndroid Build Coastguard Worker &tcache->cache_bin_array_descriptor,
2090*1208bc7eSAndroid Build Coastguard Worker tcache->bins_small, tcache->bins_large);
2091*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
2092*1208bc7eSAndroid Build Coastguard Worker &tcache->cache_bin_array_descriptor, link);
2093*1208bc7eSAndroid Build Coastguard Worker }
2094*1208bc7eSAndroid Build Coastguard Worker }
2095*1208bc7eSAndroid Build Coastguard Worker #endif
2096*1208bc7eSAndroid Build Coastguard Worker
2097*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NBINS; i++) {
2098*1208bc7eSAndroid Build Coastguard Worker bin_postfork_child(tsdn, &arena->bins[i]);
2099*1208bc7eSAndroid Build Coastguard Worker }
2100*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->large_mtx);
2101*1208bc7eSAndroid Build Coastguard Worker base_postfork_child(tsdn, arena->base);
2102*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->extent_avail_mtx);
2103*1208bc7eSAndroid Build Coastguard Worker extents_postfork_child(tsdn, &arena->extents_retained);
2104*1208bc7eSAndroid Build Coastguard Worker extents_postfork_child(tsdn, &arena->extents_muzzy);
2105*1208bc7eSAndroid Build Coastguard Worker extents_postfork_child(tsdn, &arena->extents_dirty);
2106*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->extent_grow_mtx);
2107*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->decay_dirty.mtx);
2108*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->decay_muzzy.mtx);
2109*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
2110*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_ATOMIC_U64
2111*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->stats.mtx);
2112*1208bc7eSAndroid Build Coastguard Worker #endif
2113*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
2114*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &arena->tcache_ql_mtx);
2115*1208bc7eSAndroid Build Coastguard Worker #endif
2116*1208bc7eSAndroid Build Coastguard Worker }
2117*1208bc7eSAndroid Build Coastguard Worker }
2118