jemalloc_internal.h.in revision ae03bf6a57f0dd6a009288fa6477a300cabf6d5e
1#ifndef JEMALLOC_INTERNAL_H
2#define JEMALLOC_INTERNAL_H
3#include <math.h>
4#ifdef _WIN32
5#  include <windows.h>
6#  define ENOENT ERROR_PATH_NOT_FOUND
7#  define EINVAL ERROR_BAD_ARGUMENTS
8#  define EAGAIN ERROR_OUTOFMEMORY
9#  define EPERM  ERROR_WRITE_FAULT
10#  define EFAULT ERROR_INVALID_ADDRESS
11#  define ENOMEM ERROR_NOT_ENOUGH_MEMORY
12#  undef ERANGE
13#  define ERANGE ERROR_INVALID_DATA
14#else
15#  include <sys/param.h>
16#  include <sys/mman.h>
17#  include <sys/syscall.h>
18#  if !defined(SYS_write) && defined(__NR_write)
19#    define SYS_write __NR_write
20#  endif
21#  include <sys/uio.h>
22#  include <pthread.h>
23#  include <errno.h>
24#endif
25#include <sys/types.h>
26
27#include <limits.h>
28#ifndef SIZE_T_MAX
29#  define SIZE_T_MAX	SIZE_MAX
30#endif
31#include <stdarg.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <stdint.h>
36#include <stddef.h>
37#ifndef offsetof
38#  define offsetof(type, member)	((size_t)&(((type *)NULL)->member))
39#endif
40#include <inttypes.h>
41#include <string.h>
42#include <strings.h>
43#include <ctype.h>
44#ifdef _MSC_VER
45#  include <io.h>
46typedef intptr_t ssize_t;
47#  define PATH_MAX 1024
48#  define STDERR_FILENO 2
49#  define __func__ __FUNCTION__
50/* Disable warnings about deprecated system functions */
51#  pragma warning(disable: 4996)
52#else
53#  include <unistd.h>
54#endif
55#include <fcntl.h>
56
57#define	JEMALLOC_NO_DEMANGLE
58#include "../jemalloc@install_suffix@.h"
59
60#ifdef JEMALLOC_UTRACE
61#include <sys/ktrace.h>
62#endif
63
64#ifdef JEMALLOC_VALGRIND
65#include <valgrind/valgrind.h>
66#include <valgrind/memcheck.h>
67#endif
68
69#include "jemalloc/internal/private_namespace.h"
70
71#ifdef JEMALLOC_CC_SILENCE
72#define	UNUSED JEMALLOC_ATTR(unused)
73#else
74#define	UNUSED
75#endif
76
77static const bool config_debug =
78#ifdef JEMALLOC_DEBUG
79    true
80#else
81    false
82#endif
83    ;
84static const bool config_dss =
85#ifdef JEMALLOC_DSS
86    true
87#else
88    false
89#endif
90    ;
91static const bool config_fill =
92#ifdef JEMALLOC_FILL
93    true
94#else
95    false
96#endif
97    ;
98static const bool config_lazy_lock =
99#ifdef JEMALLOC_LAZY_LOCK
100    true
101#else
102    false
103#endif
104    ;
105static const bool config_prof =
106#ifdef JEMALLOC_PROF
107    true
108#else
109    false
110#endif
111    ;
112static const bool config_prof_libgcc =
113#ifdef JEMALLOC_PROF_LIBGCC
114    true
115#else
116    false
117#endif
118    ;
119static const bool config_prof_libunwind =
120#ifdef JEMALLOC_PROF_LIBUNWIND
121    true
122#else
123    false
124#endif
125    ;
126static const bool config_mremap =
127#ifdef JEMALLOC_MREMAP
128    true
129#else
130    false
131#endif
132    ;
133static const bool config_munmap =
134#ifdef JEMALLOC_MUNMAP
135    true
136#else
137    false
138#endif
139    ;
140static const bool config_stats =
141#ifdef JEMALLOC_STATS
142    true
143#else
144    false
145#endif
146    ;
147static const bool config_tcache =
148#ifdef JEMALLOC_TCACHE
149    true
150#else
151    false
152#endif
153    ;
154static const bool config_tls =
155#ifdef JEMALLOC_TLS
156    true
157#else
158    false
159#endif
160    ;
161static const bool config_utrace =
162#ifdef JEMALLOC_UTRACE
163    true
164#else
165    false
166#endif
167    ;
168static const bool config_valgrind =
169#ifdef JEMALLOC_VALGRIND
170    true
171#else
172    false
173#endif
174    ;
175static const bool config_xmalloc =
176#ifdef JEMALLOC_XMALLOC
177    true
178#else
179    false
180#endif
181    ;
182static const bool config_ivsalloc =
183#ifdef JEMALLOC_IVSALLOC
184    true
185#else
186    false
187#endif
188    ;
189
190#ifdef JEMALLOC_ATOMIC9
191#include <machine/atomic.h>
192#endif
193
194#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
195#include <libkern/OSAtomic.h>
196#endif
197
198#ifdef JEMALLOC_ZONE
199#include <mach/mach_error.h>
200#include <mach/mach_init.h>
201#include <mach/vm_map.h>
202#include <malloc/malloc.h>
203#endif
204
205#define	RB_COMPACT
206#include "jemalloc/internal/rb.h"
207#include "jemalloc/internal/qr.h"
208#include "jemalloc/internal/ql.h"
209
210/*
211 * jemalloc can conceptually be broken into components (arena, tcache, etc.),
212 * but there are circular dependencies that cannot be broken without
213 * substantial performance degradation.  In order to reduce the effect on
214 * visual code flow, read the header files in multiple passes, with one of the
215 * following cpp variables defined during each pass:
216 *
217 *   JEMALLOC_H_TYPES   : Preprocessor-defined constants and psuedo-opaque data
218 *                        types.
219 *   JEMALLOC_H_STRUCTS : Data structures.
220 *   JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.
221 *   JEMALLOC_H_INLINES : Inline functions.
222 */
223/******************************************************************************/
224#define JEMALLOC_H_TYPES
225
226#define	ALLOCM_LG_ALIGN_MASK	((int)0x3f)
227
228#define	ZU(z)	((size_t)z)
229#define	QU(q)	((uint64_t)q)
230
231#ifndef __DECONST
232#  define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
233#endif
234
235#ifdef JEMALLOC_DEBUG
236   /* Disable inlining to make debugging easier. */
237#  define JEMALLOC_ALWAYS_INLINE
238#  define JEMALLOC_INLINE
239#  define inline
240#else
241#  define JEMALLOC_ENABLE_INLINE
242#  ifdef JEMALLOC_HAVE_ATTR
243#    define JEMALLOC_ALWAYS_INLINE \
244	 static JEMALLOC_ATTR(unused) JEMALLOC_ATTR(always_inline)
245#  else
246#    define JEMALLOC_ALWAYS_INLINE static inline
247#  endif
248#  define JEMALLOC_INLINE static inline
249#  ifdef _MSC_VER
250#    define inline _inline
251#  endif
252#endif
253
254/* Smallest size class to support. */
255#define	LG_TINY_MIN		3
256#define	TINY_MIN		(1U << LG_TINY_MIN)
257
258/*
259 * Minimum alignment of allocations is 2^LG_QUANTUM bytes (ignoring tiny size
260 * classes).
261 */
262#ifndef LG_QUANTUM
263#  if (defined(__i386__) || defined(_M_IX86))
264#    define LG_QUANTUM		4
265#  endif
266#  ifdef __ia64__
267#    define LG_QUANTUM		4
268#  endif
269#  ifdef __alpha__
270#    define LG_QUANTUM		4
271#  endif
272#  ifdef __sparc64__
273#    define LG_QUANTUM		4
274#  endif
275#  if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64))
276#    define LG_QUANTUM		4
277#  endif
278#  ifdef __arm__
279#    define LG_QUANTUM		3
280#  endif
281#  ifdef __hppa__
282#    define LG_QUANTUM		4
283#  endif
284#  ifdef __mips__
285#    define LG_QUANTUM		3
286#  endif
287#  ifdef __powerpc__
288#    define LG_QUANTUM		4
289#  endif
290#  ifdef __s390x__
291#    define LG_QUANTUM		4
292#  endif
293#  ifdef __SH4__
294#    define LG_QUANTUM		4
295#  endif
296#  ifdef __tile__
297#    define LG_QUANTUM		4
298#  endif
299#  ifndef LG_QUANTUM
300#    error "No LG_QUANTUM definition for architecture; specify via CPPFLAGS"
301#  endif
302#endif
303
304#define	QUANTUM			((size_t)(1U << LG_QUANTUM))
305#define	QUANTUM_MASK		(QUANTUM - 1)
306
307/* Return the smallest quantum multiple that is >= a. */
308#define	QUANTUM_CEILING(a)						\
309	(((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
310
311#define	LONG			((size_t)(1U << LG_SIZEOF_LONG))
312#define	LONG_MASK		(LONG - 1)
313
314/* Return the smallest long multiple that is >= a. */
315#define	LONG_CEILING(a)							\
316	(((a) + LONG_MASK) & ~LONG_MASK)
317
318#define	SIZEOF_PTR		(1U << LG_SIZEOF_PTR)
319#define	PTR_MASK		(SIZEOF_PTR - 1)
320
321/* Return the smallest (void *) multiple that is >= a. */
322#define	PTR_CEILING(a)							\
323	(((a) + PTR_MASK) & ~PTR_MASK)
324
325/*
326 * Maximum size of L1 cache line.  This is used to avoid cache line aliasing.
327 * In addition, this controls the spacing of cacheline-spaced size classes.
328 *
329 * CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can
330 * only handle raw constants.
331 */
332#define	LG_CACHELINE		6
333#define	CACHELINE		64
334#define	CACHELINE_MASK		(CACHELINE - 1)
335
336/* Return the smallest cacheline multiple that is >= s. */
337#define	CACHELINE_CEILING(s)						\
338	(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
339
340/* Page size.  STATIC_PAGE_SHIFT is determined by the configure script. */
341#ifdef PAGE_MASK
342#  undef PAGE_MASK
343#endif
344#define	LG_PAGE		STATIC_PAGE_SHIFT
345#define	PAGE		((size_t)(1U << STATIC_PAGE_SHIFT))
346#define	PAGE_MASK	((size_t)(PAGE - 1))
347
348/* Return the smallest pagesize multiple that is >= s. */
349#define	PAGE_CEILING(s)							\
350	(((s) + PAGE_MASK) & ~PAGE_MASK)
351
352/* Return the nearest aligned address at or below a. */
353#define	ALIGNMENT_ADDR2BASE(a, alignment)				\
354	((void *)((uintptr_t)(a) & (-(alignment))))
355
356/* Return the offset between a and the nearest aligned address at or below a. */
357#define	ALIGNMENT_ADDR2OFFSET(a, alignment)				\
358	((size_t)((uintptr_t)(a) & (alignment - 1)))
359
360/* Return the smallest alignment multiple that is >= s. */
361#define	ALIGNMENT_CEILING(s, alignment)					\
362	(((s) + (alignment - 1)) & (-(alignment)))
363
364/* Declare a variable length array */
365#if __STDC_VERSION__ < 199901L
366#  ifdef _MSC_VER
367#    include <malloc.h>
368#    define alloca _alloca
369#  else
370#    ifdef JEMALLOC_HAS_ALLOCA_H
371#      include <alloca.h>
372#    else
373#      include <stdlib.h>
374#    endif
375#  endif
376#  define VARIABLE_ARRAY(type, name, count) \
377	type *name = alloca(sizeof(type) * count)
378#else
379#  define VARIABLE_ARRAY(type, name, count) type name[count]
380#endif
381
382#ifdef JEMALLOC_VALGRIND
383/*
384 * The JEMALLOC_VALGRIND_*() macros must be macros rather than functions
385 * so that when Valgrind reports errors, there are no extra stack frames
386 * in the backtraces.
387 *
388 * The size that is reported to valgrind must be consistent through a chain of
389 * malloc..realloc..realloc calls.  Request size isn't recorded anywhere in
390 * jemalloc, so it is critical that all callers of these macros provide usize
391 * rather than request size.  As a result, buffer overflow detection is
392 * technically weakened for the standard API, though it is generally accepted
393 * practice to consider any extra bytes reported by malloc_usable_size() as
394 * usable space.
395 */
396#define	JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero) do {		\
397	if (config_valgrind && opt_valgrind && cond)			\
398		VALGRIND_MALLOCLIKE_BLOCK(ptr, usize, p2rz(ptr), zero);	\
399} while (0)
400#define	JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize,	\
401    old_rzsize, zero)  do {						\
402	if (config_valgrind && opt_valgrind) {				\
403		size_t rzsize = p2rz(ptr);				\
404									\
405		if (ptr == old_ptr) {					\
406			VALGRIND_RESIZEINPLACE_BLOCK(ptr, old_usize,	\
407			    usize, rzsize);				\
408			if (zero && old_usize < usize) {		\
409				VALGRIND_MAKE_MEM_DEFINED(		\
410				    (void *)((uintptr_t)ptr +		\
411				    old_usize), usize - old_usize);	\
412			}						\
413		} else {						\
414			if (old_ptr != NULL) {				\
415				VALGRIND_FREELIKE_BLOCK(old_ptr,	\
416				    old_rzsize);			\
417			}						\
418			if (ptr != NULL) {				\
419				size_t copy_size = (old_usize < usize)	\
420				    ?  old_usize : usize;		\
421				size_t tail_size = usize - copy_size;	\
422				VALGRIND_MALLOCLIKE_BLOCK(ptr, usize,	\
423				    rzsize, false);			\
424				if (copy_size > 0) {			\
425					VALGRIND_MAKE_MEM_DEFINED(ptr,	\
426					    copy_size);			\
427				}					\
428				if (zero && tail_size > 0) {		\
429					VALGRIND_MAKE_MEM_DEFINED(	\
430					    (void *)((uintptr_t)ptr +	\
431					    copy_size), tail_size);	\
432				}					\
433			}						\
434		}							\
435	}								\
436} while (0)
437#define	JEMALLOC_VALGRIND_FREE(ptr, rzsize) do {			\
438	if (config_valgrind && opt_valgrind)				\
439		VALGRIND_FREELIKE_BLOCK(ptr, rzsize);			\
440} while (0)
441#else
442#define	RUNNING_ON_VALGRIND	((unsigned)0)
443#define	VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
444#define	VALGRIND_RESIZEINPLACE_BLOCK(addr, oldSizeB, newSizeB, rzB)
445#define	VALGRIND_FREELIKE_BLOCK(addr, rzB)
446#define	VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
447#define	VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
448#define	JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero)
449#define	JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize,	\
450    old_rzsize, zero)
451#define	JEMALLOC_VALGRIND_FREE(ptr, rzsize)
452#endif
453
454#include "jemalloc/internal/util.h"
455#include "jemalloc/internal/atomic.h"
456#include "jemalloc/internal/prng.h"
457#include "jemalloc/internal/ckh.h"
458#include "jemalloc/internal/size_classes.h"
459#include "jemalloc/internal/stats.h"
460#include "jemalloc/internal/ctl.h"
461#include "jemalloc/internal/mutex.h"
462#include "jemalloc/internal/tsd.h"
463#include "jemalloc/internal/mb.h"
464#include "jemalloc/internal/extent.h"
465#include "jemalloc/internal/arena.h"
466#include "jemalloc/internal/bitmap.h"
467#include "jemalloc/internal/base.h"
468#include "jemalloc/internal/chunk.h"
469#include "jemalloc/internal/huge.h"
470#include "jemalloc/internal/rtree.h"
471#include "jemalloc/internal/tcache.h"
472#include "jemalloc/internal/hash.h"
473#include "jemalloc/internal/quarantine.h"
474#include "jemalloc/internal/prof.h"
475
476#undef JEMALLOC_H_TYPES
477/******************************************************************************/
478#define JEMALLOC_H_STRUCTS
479
480#include "jemalloc/internal/util.h"
481#include "jemalloc/internal/atomic.h"
482#include "jemalloc/internal/prng.h"
483#include "jemalloc/internal/ckh.h"
484#include "jemalloc/internal/size_classes.h"
485#include "jemalloc/internal/stats.h"
486#include "jemalloc/internal/ctl.h"
487#include "jemalloc/internal/mutex.h"
488#include "jemalloc/internal/tsd.h"
489#include "jemalloc/internal/mb.h"
490#include "jemalloc/internal/bitmap.h"
491#include "jemalloc/internal/extent.h"
492#include "jemalloc/internal/arena.h"
493#include "jemalloc/internal/base.h"
494#include "jemalloc/internal/chunk.h"
495#include "jemalloc/internal/huge.h"
496#include "jemalloc/internal/rtree.h"
497#include "jemalloc/internal/tcache.h"
498#include "jemalloc/internal/hash.h"
499#include "jemalloc/internal/quarantine.h"
500#include "jemalloc/internal/prof.h"
501
502typedef struct {
503	uint64_t	allocated;
504	uint64_t	deallocated;
505} thread_allocated_t;
506/*
507 * The JEMALLOC_CONCAT() wrapper is necessary to pass {0, 0} via a cpp macro
508 * argument.
509 */
510#define	THREAD_ALLOCATED_INITIALIZER	JEMALLOC_CONCAT({0, 0})
511
512#undef JEMALLOC_H_STRUCTS
513/******************************************************************************/
514#define JEMALLOC_H_EXTERNS
515
516extern bool	opt_abort;
517extern bool	opt_junk;
518extern size_t	opt_quarantine;
519extern bool	opt_redzone;
520extern bool	opt_utrace;
521extern bool	opt_valgrind;
522extern bool	opt_xmalloc;
523extern bool	opt_zero;
524extern size_t	opt_narenas;
525
526/* Number of CPUs. */
527extern unsigned		ncpus;
528
529/* Protects arenas initialization (arenas, arenas_total). */
530extern malloc_mutex_t	arenas_lock;
531/*
532 * Arenas that are used to service external requests.  Not all elements of the
533 * arenas array are necessarily used; arenas are created lazily as needed.
534 *
535 * arenas[0..narenas_auto) are used for automatic multiplexing of threads and
536 * arenas.  arenas[narenas_auto..narenas_total) are only used if the application
537 * takes some action to create them and allocate from them.
538 */
539extern arena_t		**arenas;
540extern unsigned		narenas_total;
541extern unsigned		narenas_auto; /* Read-only after initialization. */
542
543arena_t	*arenas_extend(unsigned ind);
544void	arenas_cleanup(void *arg);
545arena_t	*choose_arena_hard(void);
546void	jemalloc_prefork(void);
547void	jemalloc_postfork_parent(void);
548void	jemalloc_postfork_child(void);
549
550#include "jemalloc/internal/util.h"
551#include "jemalloc/internal/atomic.h"
552#include "jemalloc/internal/prng.h"
553#include "jemalloc/internal/ckh.h"
554#include "jemalloc/internal/size_classes.h"
555#include "jemalloc/internal/stats.h"
556#include "jemalloc/internal/ctl.h"
557#include "jemalloc/internal/mutex.h"
558#include "jemalloc/internal/tsd.h"
559#include "jemalloc/internal/mb.h"
560#include "jemalloc/internal/bitmap.h"
561#include "jemalloc/internal/extent.h"
562#include "jemalloc/internal/arena.h"
563#include "jemalloc/internal/base.h"
564#include "jemalloc/internal/chunk.h"
565#include "jemalloc/internal/huge.h"
566#include "jemalloc/internal/rtree.h"
567#include "jemalloc/internal/tcache.h"
568#include "jemalloc/internal/hash.h"
569#include "jemalloc/internal/quarantine.h"
570#include "jemalloc/internal/prof.h"
571
572#undef JEMALLOC_H_EXTERNS
573/******************************************************************************/
574#define JEMALLOC_H_INLINES
575
576#include "jemalloc/internal/util.h"
577#include "jemalloc/internal/atomic.h"
578#include "jemalloc/internal/prng.h"
579#include "jemalloc/internal/ckh.h"
580#include "jemalloc/internal/size_classes.h"
581#include "jemalloc/internal/stats.h"
582#include "jemalloc/internal/ctl.h"
583#include "jemalloc/internal/mutex.h"
584#include "jemalloc/internal/tsd.h"
585#include "jemalloc/internal/mb.h"
586#include "jemalloc/internal/extent.h"
587#include "jemalloc/internal/base.h"
588#include "jemalloc/internal/chunk.h"
589#include "jemalloc/internal/huge.h"
590
591#ifndef JEMALLOC_ENABLE_INLINE
592malloc_tsd_protos(JEMALLOC_ATTR(unused), arenas, arena_t *)
593
594size_t	s2u(size_t size);
595size_t	sa2u(size_t size, size_t alignment);
596unsigned	narenas_total_get(void);
597arena_t	*choose_arena(arena_t *arena);
598#endif
599
600#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
601/*
602 * Map of pthread_self() --> arenas[???], used for selecting an arena to use
603 * for allocations.
604 */
605malloc_tsd_externs(arenas, arena_t *)
606malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, arenas, arena_t *, NULL,
607    arenas_cleanup)
608
609/*
610 * Compute usable size that would result from allocating an object with the
611 * specified size.
612 */
613JEMALLOC_ALWAYS_INLINE size_t
614s2u(size_t size)
615{
616
617	if (size <= SMALL_MAXCLASS)
618		return (arena_bin_info[SMALL_SIZE2BIN(size)].reg_size);
619	if (size <= arena_maxclass)
620		return (PAGE_CEILING(size));
621	return (CHUNK_CEILING(size));
622}
623
624/*
625 * Compute usable size that would result from allocating an object with the
626 * specified size and alignment.
627 */
628JEMALLOC_ALWAYS_INLINE size_t
629sa2u(size_t size, size_t alignment)
630{
631	size_t usize;
632
633	assert(alignment != 0 && ((alignment - 1) & alignment) == 0);
634
635	/*
636	 * Round size up to the nearest multiple of alignment.
637	 *
638	 * This done, we can take advantage of the fact that for each small
639	 * size class, every object is aligned at the smallest power of two
640	 * that is non-zero in the base two representation of the size.  For
641	 * example:
642	 *
643	 *   Size |   Base 2 | Minimum alignment
644	 *   -----+----------+------------------
645	 *     96 |  1100000 |  32
646	 *    144 | 10100000 |  32
647	 *    192 | 11000000 |  64
648	 */
649	usize = ALIGNMENT_CEILING(size, alignment);
650	/*
651	 * (usize < size) protects against the combination of maximal
652	 * alignment and size greater than maximal alignment.
653	 */
654	if (usize < size) {
655		/* size_t overflow. */
656		return (0);
657	}
658
659	if (usize <= arena_maxclass && alignment <= PAGE) {
660		if (usize <= SMALL_MAXCLASS)
661			return (arena_bin_info[SMALL_SIZE2BIN(usize)].reg_size);
662		return (PAGE_CEILING(usize));
663	} else {
664		size_t run_size;
665
666		/*
667		 * We can't achieve subpage alignment, so round up alignment
668		 * permanently; it makes later calculations simpler.
669		 */
670		alignment = PAGE_CEILING(alignment);
671		usize = PAGE_CEILING(size);
672		/*
673		 * (usize < size) protects against very large sizes within
674		 * PAGE of SIZE_T_MAX.
675		 *
676		 * (usize + alignment < usize) protects against the
677		 * combination of maximal alignment and usize large enough
678		 * to cause overflow.  This is similar to the first overflow
679		 * check above, but it needs to be repeated due to the new
680		 * usize value, which may now be *equal* to maximal
681		 * alignment, whereas before we only detected overflow if the
682		 * original size was *greater* than maximal alignment.
683		 */
684		if (usize < size || usize + alignment < usize) {
685			/* size_t overflow. */
686			return (0);
687		}
688
689		/*
690		 * Calculate the size of the over-size run that arena_palloc()
691		 * would need to allocate in order to guarantee the alignment.
692		 * If the run wouldn't fit within a chunk, round up to a huge
693		 * allocation size.
694		 */
695		run_size = usize + alignment - PAGE;
696		if (run_size <= arena_maxclass)
697			return (PAGE_CEILING(usize));
698		return (CHUNK_CEILING(usize));
699	}
700}
701
702JEMALLOC_INLINE unsigned
703narenas_total_get(void)
704{
705	unsigned narenas;
706
707	malloc_mutex_lock(&arenas_lock);
708	narenas = narenas_total;
709	malloc_mutex_unlock(&arenas_lock);
710
711	return (narenas);
712}
713
714/* Choose an arena based on a per-thread value. */
715JEMALLOC_INLINE arena_t *
716choose_arena(arena_t *arena)
717{
718	arena_t *ret;
719
720	if (arena != NULL)
721		return (arena);
722
723	if ((ret = *arenas_tsd_get()) == NULL) {
724		ret = choose_arena_hard();
725		assert(ret != NULL);
726	}
727
728	return (ret);
729}
730#endif
731
732#include "jemalloc/internal/bitmap.h"
733#include "jemalloc/internal/rtree.h"
734/*
735 * Include arena.h twice in order to resolve circular dependencies with
736 * tcache.h.
737 */
738#define	JEMALLOC_ARENA_INLINE_A
739#include "jemalloc/internal/arena.h"
740#undef JEMALLOC_ARENA_INLINE_A
741#include "jemalloc/internal/tcache.h"
742#define	JEMALLOC_ARENA_INLINE_B
743#include "jemalloc/internal/arena.h"
744#undef JEMALLOC_ARENA_INLINE_B
745#include "jemalloc/internal/hash.h"
746#include "jemalloc/internal/quarantine.h"
747
748#ifndef JEMALLOC_ENABLE_INLINE
749void	*imallocx(size_t size, bool try_tcache, arena_t *arena);
750void	*imalloc(size_t size);
751void	*icallocx(size_t size, bool try_tcache, arena_t *arena);
752void	*icalloc(size_t size);
753void	*ipallocx(size_t usize, size_t alignment, bool zero, bool try_tcache,
754    arena_t *arena);
755void	*ipalloc(size_t usize, size_t alignment, bool zero);
756size_t	isalloc(const void *ptr, bool demote);
757size_t	ivsalloc(const void *ptr, bool demote);
758size_t	u2rz(size_t usize);
759size_t	p2rz(const void *ptr);
760void	idallocx(void *ptr, bool try_tcache);
761void	idalloc(void *ptr);
762void	iqallocx(void *ptr, bool try_tcache);
763void	iqalloc(void *ptr);
764void	*irallocx(void *ptr, size_t size, size_t extra, size_t alignment,
765    bool zero, bool no_move, bool try_tcache_alloc, bool try_tcache_dalloc,
766    arena_t *arena);
767void	*iralloc(void *ptr, size_t size, size_t extra, size_t alignment,
768    bool zero, bool no_move);
769malloc_tsd_protos(JEMALLOC_ATTR(unused), thread_allocated, thread_allocated_t)
770#endif
771
772#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
773JEMALLOC_ALWAYS_INLINE void *
774imallocx(size_t size, bool try_tcache, arena_t *arena)
775{
776
777	assert(size != 0);
778
779	if (size <= arena_maxclass)
780		return (arena_malloc(arena, size, false, try_tcache));
781	else
782		return (huge_malloc(size, false));
783}
784
785JEMALLOC_ALWAYS_INLINE void *
786imalloc(size_t size)
787{
788
789	return (imallocx(size, true, NULL));
790}
791
792JEMALLOC_ALWAYS_INLINE void *
793icallocx(size_t size, bool try_tcache, arena_t *arena)
794{
795
796	if (size <= arena_maxclass)
797		return (arena_malloc(arena, size, true, try_tcache));
798	else
799		return (huge_malloc(size, true));
800}
801
802JEMALLOC_ALWAYS_INLINE void *
803icalloc(size_t size)
804{
805
806	return (icallocx(size, true, NULL));
807}
808
809JEMALLOC_ALWAYS_INLINE void *
810ipallocx(size_t usize, size_t alignment, bool zero, bool try_tcache,
811    arena_t *arena)
812{
813	void *ret;
814
815	assert(usize != 0);
816	assert(usize == sa2u(usize, alignment));
817
818	if (usize <= arena_maxclass && alignment <= PAGE)
819		ret = arena_malloc(arena, usize, zero, try_tcache);
820	else {
821		if (usize <= arena_maxclass) {
822			ret = arena_palloc(choose_arena(arena), usize,
823			    alignment, zero);
824		} else if (alignment <= chunksize)
825			ret = huge_malloc(usize, zero);
826		else
827			ret = huge_palloc(usize, alignment, zero);
828	}
829
830	assert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret);
831	return (ret);
832}
833
834JEMALLOC_ALWAYS_INLINE void *
835ipalloc(size_t usize, size_t alignment, bool zero)
836{
837
838	return (ipallocx(usize, alignment, zero, true, NULL));
839}
840
841/*
842 * Typical usage:
843 *   void *ptr = [...]
844 *   size_t sz = isalloc(ptr, config_prof);
845 */
846JEMALLOC_ALWAYS_INLINE size_t
847isalloc(const void *ptr, bool demote)
848{
849	size_t ret;
850	arena_chunk_t *chunk;
851
852	assert(ptr != NULL);
853	/* Demotion only makes sense if config_prof is true. */
854	assert(config_prof || demote == false);
855
856	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
857	if (chunk != ptr)
858		ret = arena_salloc(ptr, demote);
859	else
860		ret = huge_salloc(ptr);
861
862	return (ret);
863}
864
865JEMALLOC_ALWAYS_INLINE size_t
866ivsalloc(const void *ptr, bool demote)
867{
868
869	/* Return 0 if ptr is not within a chunk managed by jemalloc. */
870	if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == NULL)
871		return (0);
872
873	return (isalloc(ptr, demote));
874}
875
876JEMALLOC_INLINE size_t
877u2rz(size_t usize)
878{
879	size_t ret;
880
881	if (usize <= SMALL_MAXCLASS) {
882		size_t binind = SMALL_SIZE2BIN(usize);
883		ret = arena_bin_info[binind].redzone_size;
884	} else
885		ret = 0;
886
887	return (ret);
888}
889
890JEMALLOC_INLINE size_t
891p2rz(const void *ptr)
892{
893	size_t usize = isalloc(ptr, false);
894
895	return (u2rz(usize));
896}
897
898JEMALLOC_ALWAYS_INLINE void
899idallocx(void *ptr, bool try_tcache)
900{
901	arena_chunk_t *chunk;
902
903	assert(ptr != NULL);
904
905	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
906	if (chunk != ptr)
907		arena_dalloc(chunk->arena, chunk, ptr, try_tcache);
908	else
909		huge_dalloc(ptr, true);
910}
911
912JEMALLOC_ALWAYS_INLINE void
913idalloc(void *ptr)
914{
915
916	idallocx(ptr, true);
917}
918
919JEMALLOC_ALWAYS_INLINE void
920iqallocx(void *ptr, bool try_tcache)
921{
922
923	if (config_fill && opt_quarantine)
924		quarantine(ptr);
925	else
926		idallocx(ptr, try_tcache);
927}
928
929JEMALLOC_ALWAYS_INLINE void
930iqalloc(void *ptr)
931{
932
933	iqallocx(ptr, true);
934}
935
936JEMALLOC_ALWAYS_INLINE void *
937irallocx(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
938    bool no_move, bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena)
939{
940	void *ret;
941	size_t oldsize;
942
943	assert(ptr != NULL);
944	assert(size != 0);
945
946	oldsize = isalloc(ptr, config_prof);
947
948	if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
949	    != 0) {
950		size_t usize, copysize;
951
952		/*
953		 * Existing object alignment is inadequate; allocate new space
954		 * and copy.
955		 */
956		if (no_move)
957			return (NULL);
958		usize = sa2u(size + extra, alignment);
959		if (usize == 0)
960			return (NULL);
961		ret = ipallocx(usize, alignment, zero, try_tcache_alloc, arena);
962		if (ret == NULL) {
963			if (extra == 0)
964				return (NULL);
965			/* Try again, without extra this time. */
966			usize = sa2u(size, alignment);
967			if (usize == 0)
968				return (NULL);
969			ret = ipallocx(usize, alignment, zero, try_tcache_alloc,
970			    arena);
971			if (ret == NULL)
972				return (NULL);
973		}
974		/*
975		 * Copy at most size bytes (not size+extra), since the caller
976		 * has no expectation that the extra bytes will be reliably
977		 * preserved.
978		 */
979		copysize = (size < oldsize) ? size : oldsize;
980		memcpy(ret, ptr, copysize);
981		iqallocx(ptr, try_tcache_dalloc);
982		return (ret);
983	}
984
985	if (no_move) {
986		if (size <= arena_maxclass) {
987			return (arena_ralloc_no_move(ptr, oldsize, size,
988			    extra, zero));
989		} else {
990			return (huge_ralloc_no_move(ptr, oldsize, size,
991			    extra));
992		}
993	} else {
994		if (size + extra <= arena_maxclass) {
995			return (arena_ralloc(arena, ptr, oldsize, size, extra,
996			    alignment, zero, try_tcache_alloc,
997			    try_tcache_dalloc));
998		} else {
999			return (huge_ralloc(ptr, oldsize, size, extra,
1000			    alignment, zero, try_tcache_dalloc));
1001		}
1002	}
1003}
1004
1005JEMALLOC_ALWAYS_INLINE void *
1006iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
1007    bool no_move)
1008{
1009
1010	return (irallocx(ptr, size, extra, alignment, zero, no_move, true, true,
1011	    NULL));
1012}
1013
1014malloc_tsd_externs(thread_allocated, thread_allocated_t)
1015malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, thread_allocated, thread_allocated_t,
1016    THREAD_ALLOCATED_INITIALIZER, malloc_tsd_no_cleanup)
1017#endif
1018
1019#include "jemalloc/internal/prof.h"
1020
1021#undef JEMALLOC_H_INLINES
1022/******************************************************************************/
1023#endif /* JEMALLOC_INTERNAL_H */
1024