1*1208bc7eSAndroid Build Coastguard Worker #define JEMALLOC_LARGE_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/extent_mmap.h"
7*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/mutex.h"
8*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/rtree.h"
9*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/util.h"
10*1208bc7eSAndroid Build Coastguard Worker
11*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
12*1208bc7eSAndroid Build Coastguard Worker
13*1208bc7eSAndroid Build Coastguard Worker void *
large_malloc(tsdn_t * tsdn,arena_t * arena,size_t usize,bool zero)14*1208bc7eSAndroid Build Coastguard Worker large_malloc(tsdn_t *tsdn, arena_t *arena, size_t usize, bool zero) {
15*1208bc7eSAndroid Build Coastguard Worker assert(usize == sz_s2u(usize));
16*1208bc7eSAndroid Build Coastguard Worker
17*1208bc7eSAndroid Build Coastguard Worker return large_palloc(tsdn, arena, usize, CACHELINE, zero);
18*1208bc7eSAndroid Build Coastguard Worker }
19*1208bc7eSAndroid Build Coastguard Worker
20*1208bc7eSAndroid Build Coastguard Worker void *
large_palloc(tsdn_t * tsdn,arena_t * arena,size_t usize,size_t alignment,bool zero)21*1208bc7eSAndroid Build Coastguard Worker large_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,
22*1208bc7eSAndroid Build Coastguard Worker bool zero) {
23*1208bc7eSAndroid Build Coastguard Worker size_t ausize;
24*1208bc7eSAndroid Build Coastguard Worker extent_t *extent;
25*1208bc7eSAndroid Build Coastguard Worker bool is_zeroed;
26*1208bc7eSAndroid Build Coastguard Worker UNUSED bool idump JEMALLOC_CC_SILENCE_INIT(false);
27*1208bc7eSAndroid Build Coastguard Worker
28*1208bc7eSAndroid Build Coastguard Worker assert(!tsdn_null(tsdn) || arena != NULL);
29*1208bc7eSAndroid Build Coastguard Worker
30*1208bc7eSAndroid Build Coastguard Worker ausize = sz_sa2u(usize, alignment);
31*1208bc7eSAndroid Build Coastguard Worker if (unlikely(ausize == 0 || ausize > LARGE_MAXCLASS)) {
32*1208bc7eSAndroid Build Coastguard Worker return NULL;
33*1208bc7eSAndroid Build Coastguard Worker }
34*1208bc7eSAndroid Build Coastguard Worker
35*1208bc7eSAndroid Build Coastguard Worker if (config_fill && unlikely(opt_zero)) {
36*1208bc7eSAndroid Build Coastguard Worker zero = true;
37*1208bc7eSAndroid Build Coastguard Worker }
38*1208bc7eSAndroid Build Coastguard Worker /*
39*1208bc7eSAndroid Build Coastguard Worker * Copy zero into is_zeroed and pass the copy when allocating the
40*1208bc7eSAndroid Build Coastguard Worker * extent, so that it is possible to make correct junk/zero fill
41*1208bc7eSAndroid Build Coastguard Worker * decisions below, even if is_zeroed ends up true when zero is false.
42*1208bc7eSAndroid Build Coastguard Worker */
43*1208bc7eSAndroid Build Coastguard Worker is_zeroed = zero;
44*1208bc7eSAndroid Build Coastguard Worker if (likely(!tsdn_null(tsdn))) {
45*1208bc7eSAndroid Build Coastguard Worker #if defined(__BIONIC__) && !defined(__LP64__) && !defined(JEMALLOC_JET) && !defined(JEMALLOC_INTEGRATION_TEST)
46*1208bc7eSAndroid Build Coastguard Worker /* On 32 bit systems, using a per arena cache can exhaust
47*1208bc7eSAndroid Build Coastguard Worker * virtual address space. Force all huge allocations to
48*1208bc7eSAndroid Build Coastguard Worker * always take place in the first arena.
49*1208bc7eSAndroid Build Coastguard Worker */
50*1208bc7eSAndroid Build Coastguard Worker arena = arena_get(tsdn, 0, false);
51*1208bc7eSAndroid Build Coastguard Worker #else
52*1208bc7eSAndroid Build Coastguard Worker arena = arena_choose(tsdn_tsd(tsdn), arena);
53*1208bc7eSAndroid Build Coastguard Worker #endif
54*1208bc7eSAndroid Build Coastguard Worker }
55*1208bc7eSAndroid Build Coastguard Worker if (unlikely(arena == NULL) || (extent = arena_extent_alloc_large(tsdn,
56*1208bc7eSAndroid Build Coastguard Worker arena, usize, alignment, &is_zeroed)) == NULL) {
57*1208bc7eSAndroid Build Coastguard Worker return NULL;
58*1208bc7eSAndroid Build Coastguard Worker }
59*1208bc7eSAndroid Build Coastguard Worker
60*1208bc7eSAndroid Build Coastguard Worker /* See comments in arena_bin_slabs_full_insert(). */
61*1208bc7eSAndroid Build Coastguard Worker if (!arena_is_auto(arena)) {
62*1208bc7eSAndroid Build Coastguard Worker /* Insert extent into large. */
63*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->large_mtx);
64*1208bc7eSAndroid Build Coastguard Worker extent_list_append(&arena->large, extent);
65*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->large_mtx);
66*1208bc7eSAndroid Build Coastguard Worker }
67*1208bc7eSAndroid Build Coastguard Worker if (config_prof && arena_prof_accum(tsdn, arena, usize)) {
68*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsdn);
69*1208bc7eSAndroid Build Coastguard Worker }
70*1208bc7eSAndroid Build Coastguard Worker
71*1208bc7eSAndroid Build Coastguard Worker if (zero) {
72*1208bc7eSAndroid Build Coastguard Worker assert(is_zeroed);
73*1208bc7eSAndroid Build Coastguard Worker } else if (config_fill && unlikely(opt_junk_alloc)) {
74*1208bc7eSAndroid Build Coastguard Worker memset(extent_addr_get(extent), JEMALLOC_ALLOC_JUNK,
75*1208bc7eSAndroid Build Coastguard Worker extent_usize_get(extent));
76*1208bc7eSAndroid Build Coastguard Worker }
77*1208bc7eSAndroid Build Coastguard Worker
78*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, arena);
79*1208bc7eSAndroid Build Coastguard Worker return extent_addr_get(extent);
80*1208bc7eSAndroid Build Coastguard Worker }
81*1208bc7eSAndroid Build Coastguard Worker
82*1208bc7eSAndroid Build Coastguard Worker static void
large_dalloc_junk_impl(void * ptr,size_t size)83*1208bc7eSAndroid Build Coastguard Worker large_dalloc_junk_impl(void *ptr, size_t size) {
84*1208bc7eSAndroid Build Coastguard Worker memset(ptr, JEMALLOC_FREE_JUNK, size);
85*1208bc7eSAndroid Build Coastguard Worker }
86*1208bc7eSAndroid Build Coastguard Worker large_dalloc_junk_t *JET_MUTABLE large_dalloc_junk = large_dalloc_junk_impl;
87*1208bc7eSAndroid Build Coastguard Worker
88*1208bc7eSAndroid Build Coastguard Worker static void
large_dalloc_maybe_junk_impl(void * ptr,size_t size)89*1208bc7eSAndroid Build Coastguard Worker large_dalloc_maybe_junk_impl(void *ptr, size_t size) {
90*1208bc7eSAndroid Build Coastguard Worker if (config_fill && have_dss && unlikely(opt_junk_free)) {
91*1208bc7eSAndroid Build Coastguard Worker /*
92*1208bc7eSAndroid Build Coastguard Worker * Only bother junk filling if the extent isn't about to be
93*1208bc7eSAndroid Build Coastguard Worker * unmapped.
94*1208bc7eSAndroid Build Coastguard Worker */
95*1208bc7eSAndroid Build Coastguard Worker if (opt_retain || (have_dss && extent_in_dss(ptr))) {
96*1208bc7eSAndroid Build Coastguard Worker large_dalloc_junk(ptr, size);
97*1208bc7eSAndroid Build Coastguard Worker }
98*1208bc7eSAndroid Build Coastguard Worker }
99*1208bc7eSAndroid Build Coastguard Worker }
100*1208bc7eSAndroid Build Coastguard Worker large_dalloc_maybe_junk_t *JET_MUTABLE large_dalloc_maybe_junk =
101*1208bc7eSAndroid Build Coastguard Worker large_dalloc_maybe_junk_impl;
102*1208bc7eSAndroid Build Coastguard Worker
103*1208bc7eSAndroid Build Coastguard Worker static bool
large_ralloc_no_move_shrink(tsdn_t * tsdn,extent_t * extent,size_t usize)104*1208bc7eSAndroid Build Coastguard Worker large_ralloc_no_move_shrink(tsdn_t *tsdn, extent_t *extent, size_t usize) {
105*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = extent_arena_get(extent);
106*1208bc7eSAndroid Build Coastguard Worker size_t oldusize = extent_usize_get(extent);
107*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = extent_hooks_get(arena);
108*1208bc7eSAndroid Build Coastguard Worker size_t diff = extent_size_get(extent) - (usize + sz_large_pad);
109*1208bc7eSAndroid Build Coastguard Worker
110*1208bc7eSAndroid Build Coastguard Worker assert(oldusize > usize);
111*1208bc7eSAndroid Build Coastguard Worker
112*1208bc7eSAndroid Build Coastguard Worker if (extent_hooks->split == NULL) {
113*1208bc7eSAndroid Build Coastguard Worker return true;
114*1208bc7eSAndroid Build Coastguard Worker }
115*1208bc7eSAndroid Build Coastguard Worker
116*1208bc7eSAndroid Build Coastguard Worker /* Split excess pages. */
117*1208bc7eSAndroid Build Coastguard Worker if (diff != 0) {
118*1208bc7eSAndroid Build Coastguard Worker extent_t *trail = extent_split_wrapper(tsdn, arena,
119*1208bc7eSAndroid Build Coastguard Worker &extent_hooks, extent, usize + sz_large_pad,
120*1208bc7eSAndroid Build Coastguard Worker sz_size2index(usize), false, diff, NSIZES, false);
121*1208bc7eSAndroid Build Coastguard Worker if (trail == NULL) {
122*1208bc7eSAndroid Build Coastguard Worker return true;
123*1208bc7eSAndroid Build Coastguard Worker }
124*1208bc7eSAndroid Build Coastguard Worker
125*1208bc7eSAndroid Build Coastguard Worker if (config_fill && unlikely(opt_junk_free)) {
126*1208bc7eSAndroid Build Coastguard Worker large_dalloc_maybe_junk(extent_addr_get(trail),
127*1208bc7eSAndroid Build Coastguard Worker extent_size_get(trail));
128*1208bc7eSAndroid Build Coastguard Worker }
129*1208bc7eSAndroid Build Coastguard Worker
130*1208bc7eSAndroid Build Coastguard Worker arena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, trail);
131*1208bc7eSAndroid Build Coastguard Worker }
132*1208bc7eSAndroid Build Coastguard Worker
133*1208bc7eSAndroid Build Coastguard Worker arena_extent_ralloc_large_shrink(tsdn, arena, extent, oldusize);
134*1208bc7eSAndroid Build Coastguard Worker
135*1208bc7eSAndroid Build Coastguard Worker return false;
136*1208bc7eSAndroid Build Coastguard Worker }
137*1208bc7eSAndroid Build Coastguard Worker
138*1208bc7eSAndroid Build Coastguard Worker static bool
large_ralloc_no_move_expand(tsdn_t * tsdn,extent_t * extent,size_t usize,bool zero)139*1208bc7eSAndroid Build Coastguard Worker large_ralloc_no_move_expand(tsdn_t *tsdn, extent_t *extent, size_t usize,
140*1208bc7eSAndroid Build Coastguard Worker bool zero) {
141*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = extent_arena_get(extent);
142*1208bc7eSAndroid Build Coastguard Worker size_t oldusize = extent_usize_get(extent);
143*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = extent_hooks_get(arena);
144*1208bc7eSAndroid Build Coastguard Worker size_t trailsize = usize - oldusize;
145*1208bc7eSAndroid Build Coastguard Worker
146*1208bc7eSAndroid Build Coastguard Worker if (extent_hooks->merge == NULL) {
147*1208bc7eSAndroid Build Coastguard Worker return true;
148*1208bc7eSAndroid Build Coastguard Worker }
149*1208bc7eSAndroid Build Coastguard Worker
150*1208bc7eSAndroid Build Coastguard Worker if (config_fill && unlikely(opt_zero)) {
151*1208bc7eSAndroid Build Coastguard Worker zero = true;
152*1208bc7eSAndroid Build Coastguard Worker }
153*1208bc7eSAndroid Build Coastguard Worker /*
154*1208bc7eSAndroid Build Coastguard Worker * Copy zero into is_zeroed_trail and pass the copy when allocating the
155*1208bc7eSAndroid Build Coastguard Worker * extent, so that it is possible to make correct junk/zero fill
156*1208bc7eSAndroid Build Coastguard Worker * decisions below, even if is_zeroed_trail ends up true when zero is
157*1208bc7eSAndroid Build Coastguard Worker * false.
158*1208bc7eSAndroid Build Coastguard Worker */
159*1208bc7eSAndroid Build Coastguard Worker bool is_zeroed_trail = zero;
160*1208bc7eSAndroid Build Coastguard Worker bool commit = true;
161*1208bc7eSAndroid Build Coastguard Worker extent_t *trail;
162*1208bc7eSAndroid Build Coastguard Worker bool new_mapping;
163*1208bc7eSAndroid Build Coastguard Worker if ((trail = extents_alloc(tsdn, arena, &extent_hooks,
164*1208bc7eSAndroid Build Coastguard Worker &arena->extents_dirty, extent_past_get(extent), trailsize, 0,
165*1208bc7eSAndroid Build Coastguard Worker CACHELINE, false, NSIZES, &is_zeroed_trail, &commit)) != NULL
166*1208bc7eSAndroid Build Coastguard Worker || (trail = extents_alloc(tsdn, arena, &extent_hooks,
167*1208bc7eSAndroid Build Coastguard Worker &arena->extents_muzzy, extent_past_get(extent), trailsize, 0,
168*1208bc7eSAndroid Build Coastguard Worker CACHELINE, false, NSIZES, &is_zeroed_trail, &commit)) != NULL) {
169*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
170*1208bc7eSAndroid Build Coastguard Worker new_mapping = false;
171*1208bc7eSAndroid Build Coastguard Worker }
172*1208bc7eSAndroid Build Coastguard Worker } else {
173*1208bc7eSAndroid Build Coastguard Worker if ((trail = extent_alloc_wrapper(tsdn, arena, &extent_hooks,
174*1208bc7eSAndroid Build Coastguard Worker extent_past_get(extent), trailsize, 0, CACHELINE, false,
175*1208bc7eSAndroid Build Coastguard Worker NSIZES, &is_zeroed_trail, &commit)) == NULL) {
176*1208bc7eSAndroid Build Coastguard Worker return true;
177*1208bc7eSAndroid Build Coastguard Worker }
178*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
179*1208bc7eSAndroid Build Coastguard Worker new_mapping = true;
180*1208bc7eSAndroid Build Coastguard Worker }
181*1208bc7eSAndroid Build Coastguard Worker }
182*1208bc7eSAndroid Build Coastguard Worker
183*1208bc7eSAndroid Build Coastguard Worker if (extent_merge_wrapper(tsdn, arena, &extent_hooks, extent, trail)) {
184*1208bc7eSAndroid Build Coastguard Worker extent_dalloc_wrapper(tsdn, arena, &extent_hooks, trail);
185*1208bc7eSAndroid Build Coastguard Worker return true;
186*1208bc7eSAndroid Build Coastguard Worker }
187*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t rtree_ctx_fallback;
188*1208bc7eSAndroid Build Coastguard Worker rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
189*1208bc7eSAndroid Build Coastguard Worker szind_t szind = sz_size2index(usize);
190*1208bc7eSAndroid Build Coastguard Worker extent_szind_set(extent, szind);
191*1208bc7eSAndroid Build Coastguard Worker rtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,
192*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)extent_addr_get(extent), szind, false);
193*1208bc7eSAndroid Build Coastguard Worker
194*1208bc7eSAndroid Build Coastguard Worker if (config_stats && new_mapping) {
195*1208bc7eSAndroid Build Coastguard Worker arena_stats_mapped_add(tsdn, &arena->stats, trailsize);
196*1208bc7eSAndroid Build Coastguard Worker }
197*1208bc7eSAndroid Build Coastguard Worker
198*1208bc7eSAndroid Build Coastguard Worker if (zero) {
199*1208bc7eSAndroid Build Coastguard Worker if (config_cache_oblivious) {
200*1208bc7eSAndroid Build Coastguard Worker /*
201*1208bc7eSAndroid Build Coastguard Worker * Zero the trailing bytes of the original allocation's
202*1208bc7eSAndroid Build Coastguard Worker * last page, since they are in an indeterminate state.
203*1208bc7eSAndroid Build Coastguard Worker * There will always be trailing bytes, because ptr's
204*1208bc7eSAndroid Build Coastguard Worker * offset from the beginning of the extent is a multiple
205*1208bc7eSAndroid Build Coastguard Worker * of CACHELINE in [0 .. PAGE).
206*1208bc7eSAndroid Build Coastguard Worker */
207*1208bc7eSAndroid Build Coastguard Worker void *zbase = (void *)
208*1208bc7eSAndroid Build Coastguard Worker ((uintptr_t)extent_addr_get(extent) + oldusize);
209*1208bc7eSAndroid Build Coastguard Worker void *zpast = PAGE_ADDR2BASE((void *)((uintptr_t)zbase +
210*1208bc7eSAndroid Build Coastguard Worker PAGE));
211*1208bc7eSAndroid Build Coastguard Worker size_t nzero = (uintptr_t)zpast - (uintptr_t)zbase;
212*1208bc7eSAndroid Build Coastguard Worker assert(nzero > 0);
213*1208bc7eSAndroid Build Coastguard Worker memset(zbase, 0, nzero);
214*1208bc7eSAndroid Build Coastguard Worker }
215*1208bc7eSAndroid Build Coastguard Worker assert(is_zeroed_trail);
216*1208bc7eSAndroid Build Coastguard Worker } else if (config_fill && unlikely(opt_junk_alloc)) {
217*1208bc7eSAndroid Build Coastguard Worker memset((void *)((uintptr_t)extent_addr_get(extent) + oldusize),
218*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_ALLOC_JUNK, usize - oldusize);
219*1208bc7eSAndroid Build Coastguard Worker }
220*1208bc7eSAndroid Build Coastguard Worker
221*1208bc7eSAndroid Build Coastguard Worker arena_extent_ralloc_large_expand(tsdn, arena, extent, oldusize);
222*1208bc7eSAndroid Build Coastguard Worker
223*1208bc7eSAndroid Build Coastguard Worker return false;
224*1208bc7eSAndroid Build Coastguard Worker }
225*1208bc7eSAndroid Build Coastguard Worker
226*1208bc7eSAndroid Build Coastguard Worker bool
large_ralloc_no_move(tsdn_t * tsdn,extent_t * extent,size_t usize_min,size_t usize_max,bool zero)227*1208bc7eSAndroid Build Coastguard Worker large_ralloc_no_move(tsdn_t *tsdn, extent_t *extent, size_t usize_min,
228*1208bc7eSAndroid Build Coastguard Worker size_t usize_max, bool zero) {
229*1208bc7eSAndroid Build Coastguard Worker size_t oldusize = extent_usize_get(extent);
230*1208bc7eSAndroid Build Coastguard Worker
231*1208bc7eSAndroid Build Coastguard Worker /* The following should have been caught by callers. */
232*1208bc7eSAndroid Build Coastguard Worker assert(usize_min > 0 && usize_max <= LARGE_MAXCLASS);
233*1208bc7eSAndroid Build Coastguard Worker /* Both allocation sizes must be large to avoid a move. */
234*1208bc7eSAndroid Build Coastguard Worker assert(oldusize >= LARGE_MINCLASS && usize_max >= LARGE_MINCLASS);
235*1208bc7eSAndroid Build Coastguard Worker
236*1208bc7eSAndroid Build Coastguard Worker if (usize_max > oldusize) {
237*1208bc7eSAndroid Build Coastguard Worker /* Attempt to expand the allocation in-place. */
238*1208bc7eSAndroid Build Coastguard Worker if (!large_ralloc_no_move_expand(tsdn, extent, usize_max,
239*1208bc7eSAndroid Build Coastguard Worker zero)) {
240*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, extent_arena_get(extent));
241*1208bc7eSAndroid Build Coastguard Worker return false;
242*1208bc7eSAndroid Build Coastguard Worker }
243*1208bc7eSAndroid Build Coastguard Worker /* Try again, this time with usize_min. */
244*1208bc7eSAndroid Build Coastguard Worker if (usize_min < usize_max && usize_min > oldusize &&
245*1208bc7eSAndroid Build Coastguard Worker large_ralloc_no_move_expand(tsdn, extent, usize_min,
246*1208bc7eSAndroid Build Coastguard Worker zero)) {
247*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, extent_arena_get(extent));
248*1208bc7eSAndroid Build Coastguard Worker return false;
249*1208bc7eSAndroid Build Coastguard Worker }
250*1208bc7eSAndroid Build Coastguard Worker }
251*1208bc7eSAndroid Build Coastguard Worker
252*1208bc7eSAndroid Build Coastguard Worker /*
253*1208bc7eSAndroid Build Coastguard Worker * Avoid moving the allocation if the existing extent size accommodates
254*1208bc7eSAndroid Build Coastguard Worker * the new size.
255*1208bc7eSAndroid Build Coastguard Worker */
256*1208bc7eSAndroid Build Coastguard Worker if (oldusize >= usize_min && oldusize <= usize_max) {
257*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, extent_arena_get(extent));
258*1208bc7eSAndroid Build Coastguard Worker return false;
259*1208bc7eSAndroid Build Coastguard Worker }
260*1208bc7eSAndroid Build Coastguard Worker
261*1208bc7eSAndroid Build Coastguard Worker /* Attempt to shrink the allocation in-place. */
262*1208bc7eSAndroid Build Coastguard Worker if (oldusize > usize_max) {
263*1208bc7eSAndroid Build Coastguard Worker if (!large_ralloc_no_move_shrink(tsdn, extent, usize_max)) {
264*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, extent_arena_get(extent));
265*1208bc7eSAndroid Build Coastguard Worker return false;
266*1208bc7eSAndroid Build Coastguard Worker }
267*1208bc7eSAndroid Build Coastguard Worker }
268*1208bc7eSAndroid Build Coastguard Worker return true;
269*1208bc7eSAndroid Build Coastguard Worker }
270*1208bc7eSAndroid Build Coastguard Worker
271*1208bc7eSAndroid Build Coastguard Worker static void *
large_ralloc_move_helper(tsdn_t * tsdn,arena_t * arena,size_t usize,size_t alignment,bool zero)272*1208bc7eSAndroid Build Coastguard Worker large_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,
273*1208bc7eSAndroid Build Coastguard Worker size_t alignment, bool zero) {
274*1208bc7eSAndroid Build Coastguard Worker if (alignment <= CACHELINE) {
275*1208bc7eSAndroid Build Coastguard Worker return large_malloc(tsdn, arena, usize, zero);
276*1208bc7eSAndroid Build Coastguard Worker }
277*1208bc7eSAndroid Build Coastguard Worker return large_palloc(tsdn, arena, usize, alignment, zero);
278*1208bc7eSAndroid Build Coastguard Worker }
279*1208bc7eSAndroid Build Coastguard Worker
280*1208bc7eSAndroid Build Coastguard Worker void *
large_ralloc(tsdn_t * tsdn,arena_t * arena,extent_t * extent,size_t usize,size_t alignment,bool zero,tcache_t * tcache)281*1208bc7eSAndroid Build Coastguard Worker large_ralloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent, size_t usize,
282*1208bc7eSAndroid Build Coastguard Worker size_t alignment, bool zero, tcache_t *tcache) {
283*1208bc7eSAndroid Build Coastguard Worker size_t oldusize = extent_usize_get(extent);
284*1208bc7eSAndroid Build Coastguard Worker
285*1208bc7eSAndroid Build Coastguard Worker /* The following should have been caught by callers. */
286*1208bc7eSAndroid Build Coastguard Worker assert(usize > 0 && usize <= LARGE_MAXCLASS);
287*1208bc7eSAndroid Build Coastguard Worker /* Both allocation sizes must be large to avoid a move. */
288*1208bc7eSAndroid Build Coastguard Worker assert(oldusize >= LARGE_MINCLASS && usize >= LARGE_MINCLASS);
289*1208bc7eSAndroid Build Coastguard Worker
290*1208bc7eSAndroid Build Coastguard Worker /* Try to avoid moving the allocation. */
291*1208bc7eSAndroid Build Coastguard Worker if (!large_ralloc_no_move(tsdn, extent, usize, usize, zero)) {
292*1208bc7eSAndroid Build Coastguard Worker return extent_addr_get(extent);
293*1208bc7eSAndroid Build Coastguard Worker }
294*1208bc7eSAndroid Build Coastguard Worker
295*1208bc7eSAndroid Build Coastguard Worker /*
296*1208bc7eSAndroid Build Coastguard Worker * usize and old size are different enough that we need to use a
297*1208bc7eSAndroid Build Coastguard Worker * different size class. In that case, fall back to allocating new
298*1208bc7eSAndroid Build Coastguard Worker * space and copying.
299*1208bc7eSAndroid Build Coastguard Worker */
300*1208bc7eSAndroid Build Coastguard Worker void *ret = large_ralloc_move_helper(tsdn, arena, usize, alignment,
301*1208bc7eSAndroid Build Coastguard Worker zero);
302*1208bc7eSAndroid Build Coastguard Worker if (ret == NULL) {
303*1208bc7eSAndroid Build Coastguard Worker return NULL;
304*1208bc7eSAndroid Build Coastguard Worker }
305*1208bc7eSAndroid Build Coastguard Worker
306*1208bc7eSAndroid Build Coastguard Worker size_t copysize = (usize < oldusize) ? usize : oldusize;
307*1208bc7eSAndroid Build Coastguard Worker memcpy(ret, extent_addr_get(extent), copysize);
308*1208bc7eSAndroid Build Coastguard Worker isdalloct(tsdn, extent_addr_get(extent), oldusize, tcache, NULL, true);
309*1208bc7eSAndroid Build Coastguard Worker return ret;
310*1208bc7eSAndroid Build Coastguard Worker }
311*1208bc7eSAndroid Build Coastguard Worker
312*1208bc7eSAndroid Build Coastguard Worker /*
313*1208bc7eSAndroid Build Coastguard Worker * junked_locked indicates whether the extent's data have been junk-filled, and
314*1208bc7eSAndroid Build Coastguard Worker * whether the arena's large_mtx is currently held.
315*1208bc7eSAndroid Build Coastguard Worker */
316*1208bc7eSAndroid Build Coastguard Worker static void
large_dalloc_prep_impl(tsdn_t * tsdn,arena_t * arena,extent_t * extent,bool junked_locked)317*1208bc7eSAndroid Build Coastguard Worker large_dalloc_prep_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent,
318*1208bc7eSAndroid Build Coastguard Worker bool junked_locked) {
319*1208bc7eSAndroid Build Coastguard Worker if (!junked_locked) {
320*1208bc7eSAndroid Build Coastguard Worker /* See comments in arena_bin_slabs_full_insert(). */
321*1208bc7eSAndroid Build Coastguard Worker if (!arena_is_auto(arena)) {
322*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->large_mtx);
323*1208bc7eSAndroid Build Coastguard Worker extent_list_remove(&arena->large, extent);
324*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->large_mtx);
325*1208bc7eSAndroid Build Coastguard Worker }
326*1208bc7eSAndroid Build Coastguard Worker large_dalloc_maybe_junk(extent_addr_get(extent),
327*1208bc7eSAndroid Build Coastguard Worker extent_usize_get(extent));
328*1208bc7eSAndroid Build Coastguard Worker } else {
329*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_assert_owner(tsdn, &arena->large_mtx);
330*1208bc7eSAndroid Build Coastguard Worker if (!arena_is_auto(arena)) {
331*1208bc7eSAndroid Build Coastguard Worker extent_list_remove(&arena->large, extent);
332*1208bc7eSAndroid Build Coastguard Worker }
333*1208bc7eSAndroid Build Coastguard Worker }
334*1208bc7eSAndroid Build Coastguard Worker arena_extent_dalloc_large_prep(tsdn, arena, extent);
335*1208bc7eSAndroid Build Coastguard Worker }
336*1208bc7eSAndroid Build Coastguard Worker
337*1208bc7eSAndroid Build Coastguard Worker static void
large_dalloc_finish_impl(tsdn_t * tsdn,arena_t * arena,extent_t * extent)338*1208bc7eSAndroid Build Coastguard Worker large_dalloc_finish_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {
339*1208bc7eSAndroid Build Coastguard Worker extent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;
340*1208bc7eSAndroid Build Coastguard Worker arena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, extent);
341*1208bc7eSAndroid Build Coastguard Worker }
342*1208bc7eSAndroid Build Coastguard Worker
343*1208bc7eSAndroid Build Coastguard Worker void
large_dalloc_prep_junked_locked(tsdn_t * tsdn,extent_t * extent)344*1208bc7eSAndroid Build Coastguard Worker large_dalloc_prep_junked_locked(tsdn_t *tsdn, extent_t *extent) {
345*1208bc7eSAndroid Build Coastguard Worker large_dalloc_prep_impl(tsdn, extent_arena_get(extent), extent, true);
346*1208bc7eSAndroid Build Coastguard Worker }
347*1208bc7eSAndroid Build Coastguard Worker
348*1208bc7eSAndroid Build Coastguard Worker void
large_dalloc_finish(tsdn_t * tsdn,extent_t * extent)349*1208bc7eSAndroid Build Coastguard Worker large_dalloc_finish(tsdn_t *tsdn, extent_t *extent) {
350*1208bc7eSAndroid Build Coastguard Worker large_dalloc_finish_impl(tsdn, extent_arena_get(extent), extent);
351*1208bc7eSAndroid Build Coastguard Worker }
352*1208bc7eSAndroid Build Coastguard Worker
353*1208bc7eSAndroid Build Coastguard Worker void
large_dalloc(tsdn_t * tsdn,extent_t * extent)354*1208bc7eSAndroid Build Coastguard Worker large_dalloc(tsdn_t *tsdn, extent_t *extent) {
355*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = extent_arena_get(extent);
356*1208bc7eSAndroid Build Coastguard Worker large_dalloc_prep_impl(tsdn, arena, extent, false);
357*1208bc7eSAndroid Build Coastguard Worker large_dalloc_finish_impl(tsdn, arena, extent);
358*1208bc7eSAndroid Build Coastguard Worker arena_decay_tick(tsdn, arena);
359*1208bc7eSAndroid Build Coastguard Worker }
360*1208bc7eSAndroid Build Coastguard Worker
361*1208bc7eSAndroid Build Coastguard Worker size_t
large_salloc(tsdn_t * tsdn,const extent_t * extent)362*1208bc7eSAndroid Build Coastguard Worker large_salloc(tsdn_t *tsdn, const extent_t *extent) {
363*1208bc7eSAndroid Build Coastguard Worker return extent_usize_get(extent);
364*1208bc7eSAndroid Build Coastguard Worker }
365*1208bc7eSAndroid Build Coastguard Worker
366*1208bc7eSAndroid Build Coastguard Worker prof_tctx_t *
large_prof_tctx_get(tsdn_t * tsdn,const extent_t * extent)367*1208bc7eSAndroid Build Coastguard Worker large_prof_tctx_get(tsdn_t *tsdn, const extent_t *extent) {
368*1208bc7eSAndroid Build Coastguard Worker return extent_prof_tctx_get(extent);
369*1208bc7eSAndroid Build Coastguard Worker }
370*1208bc7eSAndroid Build Coastguard Worker
371*1208bc7eSAndroid Build Coastguard Worker void
large_prof_tctx_set(tsdn_t * tsdn,extent_t * extent,prof_tctx_t * tctx)372*1208bc7eSAndroid Build Coastguard Worker large_prof_tctx_set(tsdn_t *tsdn, extent_t *extent, prof_tctx_t *tctx) {
373*1208bc7eSAndroid Build Coastguard Worker extent_prof_tctx_set(extent, tctx);
374*1208bc7eSAndroid Build Coastguard Worker }
375*1208bc7eSAndroid Build Coastguard Worker
376*1208bc7eSAndroid Build Coastguard Worker void
large_prof_tctx_reset(tsdn_t * tsdn,extent_t * extent)377*1208bc7eSAndroid Build Coastguard Worker large_prof_tctx_reset(tsdn_t *tsdn, extent_t *extent) {
378*1208bc7eSAndroid Build Coastguard Worker large_prof_tctx_set(tsdn, extent, (prof_tctx_t *)(uintptr_t)1U);
379*1208bc7eSAndroid Build Coastguard Worker }
380