arena.h revision 665769357cd77b74e00a146f196fff19243b33c4
1/******************************************************************************/
2#ifdef JEMALLOC_H_TYPES
3
4/*
5 * RUN_MAX_OVRHD indicates maximum desired run header overhead.  Runs are sized
6 * as small as possible such that this setting is still honored, without
7 * violating other constraints.  The goal is to make runs as small as possible
8 * without exceeding a per run external fragmentation threshold.
9 *
10 * We use binary fixed point math for overhead computations, where the binary
11 * point is implicitly RUN_BFP bits to the left.
12 *
13 * Note that it is possible to set RUN_MAX_OVRHD low enough that it cannot be
14 * honored for some/all object sizes, since when heap profiling is enabled
15 * there is one pointer of header overhead per object (plus a constant).  This
16 * constraint is relaxed (ignored) for runs that are so small that the
17 * per-region overhead is greater than:
18 *
19 *   (RUN_MAX_OVRHD / (reg_interval << (3+RUN_BFP))
20 */
21#define	RUN_BFP			12
22/*                                    \/   Implicit binary fixed point. */
23#define	RUN_MAX_OVRHD		0x0000003dU
24#define	RUN_MAX_OVRHD_RELAX	0x00001800U
25
26/* Maximum number of regions in one run. */
27#define	LG_RUN_MAXREGS		11
28#define	RUN_MAXREGS		(1U << LG_RUN_MAXREGS)
29
30/*
31 * Minimum redzone size.  Redzones may be larger than this if necessary to
32 * preserve region alignment.
33 */
34#define	REDZONE_MINSIZE		16
35
36/*
37 * The minimum ratio of active:dirty pages per arena is computed as:
38 *
39 *   (nactive >> opt_lg_dirty_mult) >= ndirty
40 *
41 * So, supposing that opt_lg_dirty_mult is 3, there can be no less than 8 times
42 * as many active pages as dirty pages.
43 */
44#define	LG_DIRTY_MULT_DEFAULT	3
45
46typedef struct arena_chunk_map_s arena_chunk_map_t;
47typedef struct arena_chunk_s arena_chunk_t;
48typedef struct arena_run_s arena_run_t;
49typedef struct arena_bin_info_s arena_bin_info_t;
50typedef struct arena_bin_s arena_bin_t;
51typedef struct arena_s arena_t;
52
53#endif /* JEMALLOC_H_TYPES */
54/******************************************************************************/
55#ifdef JEMALLOC_H_STRUCTS
56
57/* Each element of the chunk map corresponds to one page within the chunk. */
58struct arena_chunk_map_s {
59#ifndef JEMALLOC_PROF
60	/*
61	 * Overlay prof_ctx in order to allow it to be referenced by dead code.
62	 * Such antics aren't warranted for per arena data structures, but
63	 * chunk map overhead accounts for a percentage of memory, rather than
64	 * being just a fixed cost.
65	 */
66	union {
67#endif
68	union {
69		/*
70		 * Linkage for run trees.  There are two disjoint uses:
71		 *
72		 * 1) arena_t's runs_avail tree.
73		 * 2) arena_run_t conceptually uses this linkage for in-use
74		 *    non-full runs, rather than directly embedding linkage.
75		 */
76		rb_node(arena_chunk_map_t)	rb_link;
77		/*
78		 * List of runs currently in purgatory.  arena_chunk_purge()
79		 * temporarily allocates runs that contain dirty pages while
80		 * purging, so that other threads cannot use the runs while the
81		 * purging thread is operating without the arena lock held.
82		 */
83		ql_elm(arena_chunk_map_t)	ql_link;
84	}				u;
85
86	/* Profile counters, used for large object runs. */
87	prof_ctx_t			*prof_ctx;
88#ifndef JEMALLOC_PROF
89	}; /* union { ... }; */
90#endif
91
92	/*
93	 * Run address (or size) and various flags are stored together.  The bit
94	 * layout looks like (assuming 32-bit system):
95	 *
96	 *   ???????? ???????? ????nnnn nnnndula
97	 *
98	 * ? : Unallocated: Run address for first/last pages, unset for internal
99	 *                  pages.
100	 *     Small: Run page offset.
101	 *     Large: Run size for first page, unset for trailing pages.
102	 * n : binind for small size class, BININD_INVALID for large size class.
103	 * d : dirty?
104	 * u : unzeroed?
105	 * l : large?
106	 * a : allocated?
107	 *
108	 * Following are example bit patterns for the three types of runs.
109	 *
110	 * p : run page offset
111	 * s : run size
112	 * n : binind for size class; large objects set these to BININD_INVALID
113	 *     except for promoted allocations (see prof_promote)
114	 * x : don't care
115	 * - : 0
116	 * + : 1
117	 * [DULA] : bit set
118	 * [dula] : bit unset
119	 *
120	 *   Unallocated (clean):
121	 *     ssssssss ssssssss ssss++++ ++++du-a
122	 *     xxxxxxxx xxxxxxxx xxxxxxxx xxxx-Uxx
123	 *     ssssssss ssssssss ssss++++ ++++dU-a
124	 *
125	 *   Unallocated (dirty):
126	 *     ssssssss ssssssss ssss++++ ++++D--a
127	 *     xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
128	 *     ssssssss ssssssss ssss++++ ++++D--a
129	 *
130	 *   Small:
131	 *     pppppppp pppppppp ppppnnnn nnnnd--A
132	 *     pppppppp pppppppp ppppnnnn nnnn---A
133	 *     pppppppp pppppppp ppppnnnn nnnnd--A
134	 *
135	 *   Large:
136	 *     ssssssss ssssssss ssss++++ ++++D-LA
137	 *     xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
138	 *     -------- -------- ----++++ ++++D-LA
139	 *
140	 *   Large (sampled, size <= PAGE):
141	 *     ssssssss ssssssss ssssnnnn nnnnD-LA
142	 *
143	 *   Large (not sampled, size == PAGE):
144	 *     ssssssss ssssssss ssss++++ ++++D-LA
145	 */
146	size_t				bits;
147#define	CHUNK_MAP_BININD_SHIFT	4
148#define	BININD_INVALID		((size_t)0xffU)
149/*     CHUNK_MAP_BININD_MASK == (BININD_INVALID << CHUNK_MAP_BININD_SHIFT) */
150#define	CHUNK_MAP_BININD_MASK	((size_t)0xff0U)
151#define	CHUNK_MAP_BININD_INVALID CHUNK_MAP_BININD_MASK
152#define	CHUNK_MAP_FLAGS_MASK	((size_t)0xcU)
153#define	CHUNK_MAP_DIRTY		((size_t)0x8U)
154#define	CHUNK_MAP_UNZEROED	((size_t)0x4U)
155#define	CHUNK_MAP_LARGE		((size_t)0x2U)
156#define	CHUNK_MAP_ALLOCATED	((size_t)0x1U)
157#define	CHUNK_MAP_KEY		CHUNK_MAP_ALLOCATED
158};
159typedef rb_tree(arena_chunk_map_t) arena_avail_tree_t;
160typedef rb_tree(arena_chunk_map_t) arena_run_tree_t;
161
162/* Arena chunk header. */
163struct arena_chunk_s {
164	/* Arena that owns the chunk. */
165	arena_t			*arena;
166
167	/* Linkage for tree of arena chunks that contain dirty runs. */
168	rb_node(arena_chunk_t)	dirty_link;
169
170	/* Number of dirty pages. */
171	size_t			ndirty;
172
173	/* Number of available runs. */
174	size_t			nruns_avail;
175
176	/*
177	 * Number of available run adjacencies that purging could coalesce.
178	 * Clean and dirty available runs are not coalesced, which causes
179	 * virtual memory fragmentation.  The ratio of
180	 * (nruns_avail-nruns_adjac):nruns_adjac is used for tracking this
181	 * fragmentation.
182	 */
183	size_t			nruns_adjac;
184
185	/*
186	 * Map of pages within chunk that keeps track of free/large/small.  The
187	 * first map_bias entries are omitted, since the chunk header does not
188	 * need to be tracked in the map.  This omission saves a header page
189	 * for common chunk sizes (e.g. 4 MiB).
190	 */
191	arena_chunk_map_t	map[1]; /* Dynamically sized. */
192};
193typedef rb_tree(arena_chunk_t) arena_chunk_tree_t;
194
195struct arena_run_s {
196	/* Bin this run is associated with. */
197	arena_bin_t	*bin;
198
199	/* Index of next region that has never been allocated, or nregs. */
200	uint32_t	nextind;
201
202	/* Number of free regions in run. */
203	unsigned	nfree;
204};
205
206/*
207 * Read-only information associated with each element of arena_t's bins array
208 * is stored separately, partly to reduce memory usage (only one copy, rather
209 * than one per arena), but mainly to avoid false cacheline sharing.
210 *
211 * Each run has the following layout:
212 *
213 *               /--------------------\
214 *               | arena_run_t header |
215 *               | ...                |
216 * bitmap_offset | bitmap             |
217 *               | ...                |
218 *   ctx0_offset | ctx map            |
219 *               | ...                |
220 *               |--------------------|
221 *               | redzone            |
222 *   reg0_offset | region 0           |
223 *               | redzone            |
224 *               |--------------------| \
225 *               | redzone            | |
226 *               | region 1           |  > reg_interval
227 *               | redzone            | /
228 *               |--------------------|
229 *               | ...                |
230 *               | ...                |
231 *               | ...                |
232 *               |--------------------|
233 *               | redzone            |
234 *               | region nregs-1     |
235 *               | redzone            |
236 *               |--------------------|
237 *               | alignment pad?     |
238 *               \--------------------/
239 *
240 * reg_interval has at least the same minimum alignment as reg_size; this
241 * preserves the alignment constraint that sa2u() depends on.  Alignment pad is
242 * either 0 or redzone_size; it is present only if needed to align reg0_offset.
243 */
244struct arena_bin_info_s {
245	/* Size of regions in a run for this bin's size class. */
246	size_t		reg_size;
247
248	/* Redzone size. */
249	size_t		redzone_size;
250
251	/* Interval between regions (reg_size + (redzone_size << 1)). */
252	size_t		reg_interval;
253
254	/* Total size of a run for this bin's size class. */
255	size_t		run_size;
256
257	/* Total number of regions in a run for this bin's size class. */
258	uint32_t	nregs;
259
260	/*
261	 * Offset of first bitmap_t element in a run header for this bin's size
262	 * class.
263	 */
264	uint32_t	bitmap_offset;
265
266	/*
267	 * Metadata used to manipulate bitmaps for runs associated with this
268	 * bin.
269	 */
270	bitmap_info_t	bitmap_info;
271
272	/*
273	 * Offset of first (prof_ctx_t *) in a run header for this bin's size
274	 * class, or 0 if (config_prof == false || opt_prof == false).
275	 */
276	uint32_t	ctx0_offset;
277
278	/* Offset of first region in a run for this bin's size class. */
279	uint32_t	reg0_offset;
280};
281
282struct arena_bin_s {
283	/*
284	 * All operations on runcur, runs, and stats require that lock be
285	 * locked.  Run allocation/deallocation are protected by the arena lock,
286	 * which may be acquired while holding one or more bin locks, but not
287	 * vise versa.
288	 */
289	malloc_mutex_t	lock;
290
291	/*
292	 * Current run being used to service allocations of this bin's size
293	 * class.
294	 */
295	arena_run_t	*runcur;
296
297	/*
298	 * Tree of non-full runs.  This tree is used when looking for an
299	 * existing run when runcur is no longer usable.  We choose the
300	 * non-full run that is lowest in memory; this policy tends to keep
301	 * objects packed well, and it can also help reduce the number of
302	 * almost-empty chunks.
303	 */
304	arena_run_tree_t runs;
305
306	/* Bin statistics. */
307	malloc_bin_stats_t stats;
308};
309
310struct arena_s {
311	/* This arena's index within the arenas array. */
312	unsigned		ind;
313
314	/*
315	 * Number of threads currently assigned to this arena.  This field is
316	 * protected by arenas_lock.
317	 */
318	unsigned		nthreads;
319
320	/*
321	 * There are three classes of arena operations from a locking
322	 * perspective:
323	 * 1) Thread asssignment (modifies nthreads) is protected by
324	 *    arenas_lock.
325	 * 2) Bin-related operations are protected by bin locks.
326	 * 3) Chunk- and run-related operations are protected by this mutex.
327	 */
328	malloc_mutex_t		lock;
329
330	arena_stats_t		stats;
331	/*
332	 * List of tcaches for extant threads associated with this arena.
333	 * Stats from these are merged incrementally, and at exit.
334	 */
335	ql_head(tcache_t)	tcache_ql;
336
337	uint64_t		prof_accumbytes;
338
339	dss_prec_t		dss_prec;
340
341	/* Tree of dirty-page-containing chunks this arena manages. */
342	arena_chunk_tree_t	chunks_dirty;
343
344	/*
345	 * In order to avoid rapid chunk allocation/deallocation when an arena
346	 * oscillates right on the cusp of needing a new chunk, cache the most
347	 * recently freed chunk.  The spare is left in the arena's chunk trees
348	 * until it is deleted.
349	 *
350	 * There is one spare chunk per arena, rather than one spare total, in
351	 * order to avoid interactions between multiple threads that could make
352	 * a single spare inadequate.
353	 */
354	arena_chunk_t		*spare;
355
356	/* Number of pages in active runs. */
357	size_t			nactive;
358
359	/*
360	 * Current count of pages within unused runs that are potentially
361	 * dirty, and for which madvise(... MADV_DONTNEED) has not been called.
362	 * By tracking this, we can institute a limit on how much dirty unused
363	 * memory is mapped for each arena.
364	 */
365	size_t			ndirty;
366
367	/*
368	 * Approximate number of pages being purged.  It is possible for
369	 * multiple threads to purge dirty pages concurrently, and they use
370	 * npurgatory to indicate the total number of pages all threads are
371	 * attempting to purge.
372	 */
373	size_t			npurgatory;
374
375	/*
376	 * Size/address-ordered trees of this arena's available runs.  The trees
377	 * are used for first-best-fit run allocation.
378	 */
379	arena_avail_tree_t	runs_avail;
380
381	/* bins is used to store trees of free regions. */
382	arena_bin_t		bins[NBINS];
383};
384
385#endif /* JEMALLOC_H_STRUCTS */
386/******************************************************************************/
387#ifdef JEMALLOC_H_EXTERNS
388
389extern ssize_t	opt_lg_dirty_mult;
390/*
391 * small_size2bin is a compact lookup table that rounds request sizes up to
392 * size classes.  In order to reduce cache footprint, the table is compressed,
393 * and all accesses are via the SMALL_SIZE2BIN macro.
394 */
395extern uint8_t const	small_size2bin[];
396#define	SMALL_SIZE2BIN(s)	(small_size2bin[(s-1) >> LG_TINY_MIN])
397
398extern arena_bin_info_t	arena_bin_info[NBINS];
399
400/* Number of large size classes. */
401#define			nlclasses (chunk_npages - map_bias)
402
403void	arena_purge_all(arena_t *arena);
404void	arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin,
405    size_t binind, uint64_t prof_accumbytes);
406void	arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info,
407    bool zero);
408void	arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info);
409void	*arena_malloc_small(arena_t *arena, size_t size, bool zero);
410void	*arena_malloc_large(arena_t *arena, size_t size, bool zero);
411void	*arena_palloc(arena_t *arena, size_t size, size_t alignment, bool zero);
412void	arena_prof_promoted(const void *ptr, size_t size);
413void	arena_dalloc_bin_locked(arena_t *arena, arena_chunk_t *chunk, void *ptr,
414    arena_chunk_map_t *mapelm);
415void	arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr,
416    size_t pageind, arena_chunk_map_t *mapelm);
417void	arena_dalloc_small(arena_t *arena, arena_chunk_t *chunk, void *ptr,
418    size_t pageind);
419void	arena_dalloc_large_locked(arena_t *arena, arena_chunk_t *chunk,
420    void *ptr);
421void	arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr);
422void	*arena_ralloc_no_move(void *ptr, size_t oldsize, size_t size,
423    size_t extra, bool zero);
424void	*arena_ralloc(arena_t *arena, void *ptr, size_t oldsize, size_t size,
425    size_t extra, size_t alignment, bool zero, bool try_tcache_alloc,
426    bool try_tcache_dalloc);
427dss_prec_t	arena_dss_prec_get(arena_t *arena);
428void	arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec);
429void	arena_stats_merge(arena_t *arena, const char **dss, size_t *nactive,
430    size_t *ndirty, arena_stats_t *astats, malloc_bin_stats_t *bstats,
431    malloc_large_stats_t *lstats);
432bool	arena_new(arena_t *arena, unsigned ind);
433void	arena_boot(void);
434void	arena_prefork(arena_t *arena);
435void	arena_postfork_parent(arena_t *arena);
436void	arena_postfork_child(arena_t *arena);
437
438#endif /* JEMALLOC_H_EXTERNS */
439/******************************************************************************/
440#ifdef JEMALLOC_H_INLINES
441
442#ifndef JEMALLOC_ENABLE_INLINE
443arena_chunk_map_t	*arena_mapp_get(arena_chunk_t *chunk, size_t pageind);
444size_t	*arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind);
445size_t	arena_mapbitsp_read(size_t *mapbitsp);
446size_t	arena_mapbits_get(arena_chunk_t *chunk, size_t pageind);
447size_t	arena_mapbits_unallocated_size_get(arena_chunk_t *chunk,
448    size_t pageind);
449size_t	arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind);
450size_t	arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind);
451size_t	arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind);
452size_t	arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind);
453size_t	arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind);
454size_t	arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind);
455size_t	arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind);
456void	arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits);
457void	arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind,
458    size_t size, size_t flags);
459void	arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind,
460    size_t size);
461void	arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind,
462    size_t size, size_t flags);
463void	arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind,
464    size_t binind);
465void	arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind,
466    size_t runind, size_t binind, size_t flags);
467void	arena_mapbits_unzeroed_set(arena_chunk_t *chunk, size_t pageind,
468    size_t unzeroed);
469bool	arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes);
470bool	arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes);
471bool	arena_prof_accum(arena_t *arena, uint64_t accumbytes);
472size_t	arena_ptr_small_binind_get(const void *ptr, size_t mapbits);
473size_t	arena_bin_index(arena_t *arena, arena_bin_t *bin);
474unsigned	arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info,
475    const void *ptr);
476prof_ctx_t	*arena_prof_ctx_get(const void *ptr);
477void	arena_prof_ctx_set(const void *ptr, size_t usize, prof_ctx_t *ctx);
478void	*arena_malloc(arena_t *arena, size_t size, bool zero, bool try_tcache);
479size_t	arena_salloc(const void *ptr, bool demote);
480void	arena_dalloc(arena_t *arena, arena_chunk_t *chunk, void *ptr,
481    bool try_tcache);
482#endif
483
484#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ARENA_C_))
485#  ifdef JEMALLOC_ARENA_INLINE_A
486JEMALLOC_ALWAYS_INLINE arena_chunk_map_t *
487arena_mapp_get(arena_chunk_t *chunk, size_t pageind)
488{
489
490	assert(pageind >= map_bias);
491	assert(pageind < chunk_npages);
492
493	return (&chunk->map[pageind-map_bias]);
494}
495
496JEMALLOC_ALWAYS_INLINE size_t *
497arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind)
498{
499
500	return (&arena_mapp_get(chunk, pageind)->bits);
501}
502
503JEMALLOC_ALWAYS_INLINE size_t
504arena_mapbitsp_read(size_t *mapbitsp)
505{
506
507	return (*mapbitsp);
508}
509
510JEMALLOC_ALWAYS_INLINE size_t
511arena_mapbits_get(arena_chunk_t *chunk, size_t pageind)
512{
513
514	return (arena_mapbitsp_read(arena_mapbitsp_get(chunk, pageind)));
515}
516
517JEMALLOC_ALWAYS_INLINE size_t
518arena_mapbits_unallocated_size_get(arena_chunk_t *chunk, size_t pageind)
519{
520	size_t mapbits;
521
522	mapbits = arena_mapbits_get(chunk, pageind);
523	assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0);
524	return (mapbits & ~PAGE_MASK);
525}
526
527JEMALLOC_ALWAYS_INLINE size_t
528arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind)
529{
530	size_t mapbits;
531
532	mapbits = arena_mapbits_get(chunk, pageind);
533	assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) ==
534	    (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED));
535	return (mapbits & ~PAGE_MASK);
536}
537
538JEMALLOC_ALWAYS_INLINE size_t
539arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind)
540{
541	size_t mapbits;
542
543	mapbits = arena_mapbits_get(chunk, pageind);
544	assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) ==
545	    CHUNK_MAP_ALLOCATED);
546	return (mapbits >> LG_PAGE);
547}
548
549JEMALLOC_ALWAYS_INLINE size_t
550arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind)
551{
552	size_t mapbits;
553	size_t binind;
554
555	mapbits = arena_mapbits_get(chunk, pageind);
556	binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT;
557	assert(binind < NBINS || binind == BININD_INVALID);
558	return (binind);
559}
560
561JEMALLOC_ALWAYS_INLINE size_t
562arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind)
563{
564	size_t mapbits;
565
566	mapbits = arena_mapbits_get(chunk, pageind);
567	return (mapbits & CHUNK_MAP_DIRTY);
568}
569
570JEMALLOC_ALWAYS_INLINE size_t
571arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind)
572{
573	size_t mapbits;
574
575	mapbits = arena_mapbits_get(chunk, pageind);
576	return (mapbits & CHUNK_MAP_UNZEROED);
577}
578
579JEMALLOC_ALWAYS_INLINE size_t
580arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind)
581{
582	size_t mapbits;
583
584	mapbits = arena_mapbits_get(chunk, pageind);
585	return (mapbits & CHUNK_MAP_LARGE);
586}
587
588JEMALLOC_ALWAYS_INLINE size_t
589arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind)
590{
591	size_t mapbits;
592
593	mapbits = arena_mapbits_get(chunk, pageind);
594	return (mapbits & CHUNK_MAP_ALLOCATED);
595}
596
597JEMALLOC_ALWAYS_INLINE void
598arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits)
599{
600
601	*mapbitsp = mapbits;
602}
603
604JEMALLOC_ALWAYS_INLINE void
605arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind, size_t size,
606    size_t flags)
607{
608	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
609
610	assert((size & PAGE_MASK) == 0);
611	assert((flags & ~CHUNK_MAP_FLAGS_MASK) == 0);
612	assert((flags & (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == flags);
613	arena_mapbitsp_write(mapbitsp, size | CHUNK_MAP_BININD_INVALID | flags);
614}
615
616JEMALLOC_ALWAYS_INLINE void
617arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind,
618    size_t size)
619{
620	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
621	size_t mapbits = arena_mapbitsp_read(mapbitsp);
622
623	assert((size & PAGE_MASK) == 0);
624	assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0);
625	arena_mapbitsp_write(mapbitsp, size | (mapbits & PAGE_MASK));
626}
627
628JEMALLOC_ALWAYS_INLINE void
629arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind, size_t size,
630    size_t flags)
631{
632	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
633	size_t mapbits = arena_mapbitsp_read(mapbitsp);
634	size_t unzeroed;
635
636	assert((size & PAGE_MASK) == 0);
637	assert((flags & CHUNK_MAP_DIRTY) == flags);
638	unzeroed = mapbits & CHUNK_MAP_UNZEROED; /* Preserve unzeroed. */
639	arena_mapbitsp_write(mapbitsp, size | CHUNK_MAP_BININD_INVALID | flags
640	    | unzeroed | CHUNK_MAP_LARGE | CHUNK_MAP_ALLOCATED);
641}
642
643JEMALLOC_ALWAYS_INLINE void
644arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind,
645    size_t binind)
646{
647	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
648	size_t mapbits = arena_mapbitsp_read(mapbitsp);
649
650	assert(binind <= BININD_INVALID);
651	assert(arena_mapbits_large_size_get(chunk, pageind) == PAGE);
652	arena_mapbitsp_write(mapbitsp, (mapbits & ~CHUNK_MAP_BININD_MASK) |
653	    (binind << CHUNK_MAP_BININD_SHIFT));
654}
655
656JEMALLOC_ALWAYS_INLINE void
657arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind, size_t runind,
658    size_t binind, size_t flags)
659{
660	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
661	size_t mapbits = arena_mapbitsp_read(mapbitsp);
662	size_t unzeroed;
663
664	assert(binind < BININD_INVALID);
665	assert(pageind - runind >= map_bias);
666	assert((flags & CHUNK_MAP_DIRTY) == flags);
667	unzeroed = mapbits & CHUNK_MAP_UNZEROED; /* Preserve unzeroed. */
668	arena_mapbitsp_write(mapbitsp, (runind << LG_PAGE) | (binind <<
669	    CHUNK_MAP_BININD_SHIFT) | flags | unzeroed | CHUNK_MAP_ALLOCATED);
670}
671
672JEMALLOC_ALWAYS_INLINE void
673arena_mapbits_unzeroed_set(arena_chunk_t *chunk, size_t pageind,
674    size_t unzeroed)
675{
676	size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
677	size_t mapbits = arena_mapbitsp_read(mapbitsp);
678
679	arena_mapbitsp_write(mapbitsp, (mapbits & ~CHUNK_MAP_UNZEROED) |
680	    unzeroed);
681}
682
683JEMALLOC_INLINE bool
684arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes)
685{
686
687	cassert(config_prof);
688	assert(prof_interval != 0);
689
690	arena->prof_accumbytes += accumbytes;
691	if (arena->prof_accumbytes >= prof_interval) {
692		arena->prof_accumbytes -= prof_interval;
693		return (true);
694	}
695	return (false);
696}
697
698JEMALLOC_INLINE bool
699arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes)
700{
701
702	cassert(config_prof);
703
704	if (prof_interval == 0)
705		return (false);
706	return (arena_prof_accum_impl(arena, accumbytes));
707}
708
709JEMALLOC_INLINE bool
710arena_prof_accum(arena_t *arena, uint64_t accumbytes)
711{
712
713	cassert(config_prof);
714
715	if (prof_interval == 0)
716		return (false);
717
718	{
719		bool ret;
720
721		malloc_mutex_lock(&arena->lock);
722		ret = arena_prof_accum_impl(arena, accumbytes);
723		malloc_mutex_unlock(&arena->lock);
724		return (ret);
725	}
726}
727
728JEMALLOC_ALWAYS_INLINE size_t
729arena_ptr_small_binind_get(const void *ptr, size_t mapbits)
730{
731	size_t binind;
732
733	binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT;
734
735	if (config_debug) {
736		arena_chunk_t *chunk;
737		arena_t *arena;
738		size_t pageind;
739		size_t actual_mapbits;
740		arena_run_t *run;
741		arena_bin_t *bin;
742		size_t actual_binind;
743		arena_bin_info_t *bin_info;
744
745		assert(binind != BININD_INVALID);
746		assert(binind < NBINS);
747		chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
748		arena = chunk->arena;
749		pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
750		actual_mapbits = arena_mapbits_get(chunk, pageind);
751		assert(mapbits == actual_mapbits);
752		assert(arena_mapbits_large_get(chunk, pageind) == 0);
753		assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
754		run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind -
755		    (actual_mapbits >> LG_PAGE)) << LG_PAGE));
756		bin = run->bin;
757		actual_binind = bin - arena->bins;
758		assert(binind == actual_binind);
759		bin_info = &arena_bin_info[actual_binind];
760		assert(((uintptr_t)ptr - ((uintptr_t)run +
761		    (uintptr_t)bin_info->reg0_offset)) % bin_info->reg_interval
762		    == 0);
763	}
764
765	return (binind);
766}
767#  endif /* JEMALLOC_ARENA_INLINE_A */
768
769#  ifdef JEMALLOC_ARENA_INLINE_B
770JEMALLOC_INLINE size_t
771arena_bin_index(arena_t *arena, arena_bin_t *bin)
772{
773	size_t binind = bin - arena->bins;
774	assert(binind < NBINS);
775	return (binind);
776}
777
778JEMALLOC_INLINE unsigned
779arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info, const void *ptr)
780{
781	unsigned shift, diff, regind;
782	size_t interval;
783
784	/*
785	 * Freeing a pointer lower than region zero can cause assertion
786	 * failure.
787	 */
788	assert((uintptr_t)ptr >= (uintptr_t)run +
789	    (uintptr_t)bin_info->reg0_offset);
790
791	/*
792	 * Avoid doing division with a variable divisor if possible.  Using
793	 * actual division here can reduce allocator throughput by over 20%!
794	 */
795	diff = (unsigned)((uintptr_t)ptr - (uintptr_t)run -
796	    bin_info->reg0_offset);
797
798	/* Rescale (factor powers of 2 out of the numerator and denominator). */
799	interval = bin_info->reg_interval;
800	shift = ffs(interval) - 1;
801	diff >>= shift;
802	interval >>= shift;
803
804	if (interval == 1) {
805		/* The divisor was a power of 2. */
806		regind = diff;
807	} else {
808		/*
809		 * To divide by a number D that is not a power of two we
810		 * multiply by (2^21 / D) and then right shift by 21 positions.
811		 *
812		 *   X / D
813		 *
814		 * becomes
815		 *
816		 *   (X * interval_invs[D - 3]) >> SIZE_INV_SHIFT
817		 *
818		 * We can omit the first three elements, because we never
819		 * divide by 0, and 1 and 2 are both powers of two, which are
820		 * handled above.
821		 */
822#define	SIZE_INV_SHIFT	((sizeof(unsigned) << 3) - LG_RUN_MAXREGS)
823#define	SIZE_INV(s)	(((1U << SIZE_INV_SHIFT) / (s)) + 1)
824		static const unsigned interval_invs[] = {
825		    SIZE_INV(3),
826		    SIZE_INV(4), SIZE_INV(5), SIZE_INV(6), SIZE_INV(7),
827		    SIZE_INV(8), SIZE_INV(9), SIZE_INV(10), SIZE_INV(11),
828		    SIZE_INV(12), SIZE_INV(13), SIZE_INV(14), SIZE_INV(15),
829		    SIZE_INV(16), SIZE_INV(17), SIZE_INV(18), SIZE_INV(19),
830		    SIZE_INV(20), SIZE_INV(21), SIZE_INV(22), SIZE_INV(23),
831		    SIZE_INV(24), SIZE_INV(25), SIZE_INV(26), SIZE_INV(27),
832		    SIZE_INV(28), SIZE_INV(29), SIZE_INV(30), SIZE_INV(31)
833		};
834
835		if (interval <= ((sizeof(interval_invs) / sizeof(unsigned)) +
836		    2)) {
837			regind = (diff * interval_invs[interval - 3]) >>
838			    SIZE_INV_SHIFT;
839		} else
840			regind = diff / interval;
841#undef SIZE_INV
842#undef SIZE_INV_SHIFT
843	}
844	assert(diff == regind * interval);
845	assert(regind < bin_info->nregs);
846
847	return (regind);
848}
849
850JEMALLOC_INLINE prof_ctx_t *
851arena_prof_ctx_get(const void *ptr)
852{
853	prof_ctx_t *ret;
854	arena_chunk_t *chunk;
855	size_t pageind, mapbits;
856
857	cassert(config_prof);
858	assert(ptr != NULL);
859	assert(CHUNK_ADDR2BASE(ptr) != ptr);
860
861	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
862	pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
863	mapbits = arena_mapbits_get(chunk, pageind);
864	assert((mapbits & CHUNK_MAP_ALLOCATED) != 0);
865	if ((mapbits & CHUNK_MAP_LARGE) == 0) {
866		if (prof_promote)
867			ret = (prof_ctx_t *)(uintptr_t)1U;
868		else {
869			arena_run_t *run = (arena_run_t *)((uintptr_t)chunk +
870			    (uintptr_t)((pageind - (mapbits >> LG_PAGE)) <<
871			    LG_PAGE));
872			size_t binind = arena_ptr_small_binind_get(ptr,
873			    mapbits);
874			arena_bin_info_t *bin_info = &arena_bin_info[binind];
875			unsigned regind;
876
877			regind = arena_run_regind(run, bin_info, ptr);
878			ret = *(prof_ctx_t **)((uintptr_t)run +
879			    bin_info->ctx0_offset + (regind *
880			    sizeof(prof_ctx_t *)));
881		}
882	} else
883		ret = arena_mapp_get(chunk, pageind)->prof_ctx;
884
885	return (ret);
886}
887
888JEMALLOC_INLINE void
889arena_prof_ctx_set(const void *ptr, size_t usize, prof_ctx_t *ctx)
890{
891	arena_chunk_t *chunk;
892	size_t pageind, mapbits;
893
894	cassert(config_prof);
895	assert(ptr != NULL);
896	assert(CHUNK_ADDR2BASE(ptr) != ptr);
897
898	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
899	pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
900	mapbits = arena_mapbits_get(chunk, pageind);
901	assert((mapbits & CHUNK_MAP_ALLOCATED) != 0);
902
903	if (usize > SMALL_MAXCLASS || (prof_promote &&
904	    ((uintptr_t)ctx != (uintptr_t)1U || ((mapbits & CHUNK_MAP_LARGE) !=
905	    0)))) {
906		assert((mapbits & CHUNK_MAP_LARGE) != 0);
907		arena_mapp_get(chunk, pageind)->prof_ctx = ctx;
908	} else {
909		assert((mapbits & CHUNK_MAP_LARGE) == 0);
910		if (prof_promote == false) {
911			arena_run_t *run = (arena_run_t *)((uintptr_t)chunk +
912			    (uintptr_t)((pageind - (mapbits >> LG_PAGE)) <<
913			    LG_PAGE));
914			size_t binind;
915			arena_bin_info_t *bin_info;
916			unsigned regind;
917
918			binind = arena_ptr_small_binind_get(ptr, mapbits);
919			bin_info = &arena_bin_info[binind];
920			regind = arena_run_regind(run, bin_info, ptr);
921
922			*((prof_ctx_t **)((uintptr_t)run +
923			    bin_info->ctx0_offset + (regind * sizeof(prof_ctx_t
924			    *)))) = ctx;
925		}
926	}
927}
928
929JEMALLOC_ALWAYS_INLINE void *
930arena_malloc(arena_t *arena, size_t size, bool zero, bool try_tcache)
931{
932	tcache_t *tcache;
933
934	assert(size != 0);
935	assert(size <= arena_maxclass);
936
937	if (size <= SMALL_MAXCLASS) {
938		if (try_tcache && (tcache = tcache_get(true)) != NULL)
939			return (tcache_alloc_small(tcache, size, zero));
940		else {
941			return (arena_malloc_small(choose_arena(arena), size,
942			    zero));
943		}
944	} else {
945		/*
946		 * Initialize tcache after checking size in order to avoid
947		 * infinite recursion during tcache initialization.
948		 */
949		if (try_tcache && size <= tcache_maxclass && (tcache =
950		    tcache_get(true)) != NULL)
951			return (tcache_alloc_large(tcache, size, zero));
952		else {
953			return (arena_malloc_large(choose_arena(arena), size,
954			    zero));
955		}
956	}
957}
958
959/* Return the size of the allocation pointed to by ptr. */
960JEMALLOC_ALWAYS_INLINE size_t
961arena_salloc(const void *ptr, bool demote)
962{
963	size_t ret;
964	arena_chunk_t *chunk;
965	size_t pageind, binind;
966
967	assert(ptr != NULL);
968	assert(CHUNK_ADDR2BASE(ptr) != ptr);
969
970	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
971	pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
972	assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
973	binind = arena_mapbits_binind_get(chunk, pageind);
974	if (binind == BININD_INVALID || (config_prof && demote == false &&
975	    prof_promote && arena_mapbits_large_get(chunk, pageind) != 0)) {
976		/*
977		 * Large allocation.  In the common case (demote == true), and
978		 * as this is an inline function, most callers will only end up
979		 * looking at binind to determine that ptr is a small
980		 * allocation.
981		 */
982		assert(((uintptr_t)ptr & PAGE_MASK) == 0);
983		ret = arena_mapbits_large_size_get(chunk, pageind);
984		assert(ret != 0);
985		assert(pageind + (ret>>LG_PAGE) <= chunk_npages);
986		assert(ret == PAGE || arena_mapbits_large_size_get(chunk,
987		    pageind+(ret>>LG_PAGE)-1) == 0);
988		assert(binind == arena_mapbits_binind_get(chunk,
989		    pageind+(ret>>LG_PAGE)-1));
990		assert(arena_mapbits_dirty_get(chunk, pageind) ==
991		    arena_mapbits_dirty_get(chunk, pageind+(ret>>LG_PAGE)-1));
992	} else {
993		/*
994		 * Small allocation (possibly promoted to a large object due to
995		 * prof_promote).
996		 */
997		assert(arena_mapbits_large_get(chunk, pageind) != 0 ||
998		    arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk,
999		    pageind)) == binind);
1000		ret = arena_bin_info[binind].reg_size;
1001	}
1002
1003	return (ret);
1004}
1005
1006JEMALLOC_ALWAYS_INLINE void
1007arena_dalloc(arena_t *arena, arena_chunk_t *chunk, void *ptr, bool try_tcache)
1008{
1009	size_t pageind, mapbits;
1010	tcache_t *tcache;
1011
1012	assert(arena != NULL);
1013	assert(chunk->arena == arena);
1014	assert(ptr != NULL);
1015	assert(CHUNK_ADDR2BASE(ptr) != ptr);
1016
1017	pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
1018	mapbits = arena_mapbits_get(chunk, pageind);
1019	assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
1020	if ((mapbits & CHUNK_MAP_LARGE) == 0) {
1021		/* Small allocation. */
1022		if (try_tcache && (tcache = tcache_get(false)) != NULL) {
1023			size_t binind;
1024
1025			binind = arena_ptr_small_binind_get(ptr, mapbits);
1026			tcache_dalloc_small(tcache, ptr, binind);
1027		} else
1028			arena_dalloc_small(arena, chunk, ptr, pageind);
1029	} else {
1030		size_t size = arena_mapbits_large_size_get(chunk, pageind);
1031
1032		assert(((uintptr_t)ptr & PAGE_MASK) == 0);
1033
1034		if (try_tcache && size <= tcache_maxclass && (tcache =
1035		    tcache_get(false)) != NULL) {
1036			tcache_dalloc_large(tcache, ptr, size);
1037		} else
1038			arena_dalloc_large(arena, chunk, ptr);
1039	}
1040}
1041#  endif /* JEMALLOC_ARENA_INLINE_B */
1042#endif
1043
1044#endif /* JEMALLOC_H_INLINES */
1045/******************************************************************************/
1046