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