]> git.saurik.com Git - redis.git/blob - deps/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in
jemalloc source added
[redis.git] / deps / jemalloc / include / jemalloc / internal / jemalloc_internal.h.in
1 #include <sys/mman.h>
2 #include <sys/param.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/sysctl.h>
6 #include <sys/uio.h>
7
8 #include <errno.h>
9 #include <limits.h>
10 #ifndef SIZE_T_MAX
11 # define SIZE_T_MAX SIZE_MAX
12 #endif
13 #include <pthread.h>
14 #include <sched.h>
15 #include <stdarg.h>
16 #include <stdbool.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <stddef.h>
21 #ifndef offsetof
22 # define offsetof(type, member) ((size_t)&(((type *)NULL)->member))
23 #endif
24 #include <inttypes.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <pthread.h>
31 #include <math.h>
32
33 #define JEMALLOC_MANGLE
34 #include "../jemalloc@install_suffix@.h"
35
36 #if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
37 #include <libkern/OSAtomic.h>
38 #endif
39
40 #ifdef JEMALLOC_ZONE
41 #include <mach/mach_error.h>
42 #include <mach/mach_init.h>
43 #include <mach/vm_map.h>
44 #include <malloc/malloc.h>
45 #endif
46
47 #ifdef JEMALLOC_LAZY_LOCK
48 #include <dlfcn.h>
49 #endif
50
51 #define RB_COMPACT
52 #include "jemalloc/internal/rb.h"
53 #include "jemalloc/internal/qr.h"
54 #include "jemalloc/internal/ql.h"
55
56 extern void (*JEMALLOC_P(malloc_message))(void *wcbopaque, const char *s);
57
58 /*
59 * Define a custom assert() in order to reduce the chances of deadlock during
60 * assertion failure.
61 */
62 #ifndef assert
63 # ifdef JEMALLOC_DEBUG
64 # define assert(e) do { \
65 if (!(e)) { \
66 char line_buf[UMAX2S_BUFSIZE]; \
67 malloc_write("<jemalloc>: "); \
68 malloc_write(__FILE__); \
69 malloc_write(":"); \
70 malloc_write(u2s(__LINE__, 10, line_buf)); \
71 malloc_write(": Failed assertion: "); \
72 malloc_write("\""); \
73 malloc_write(#e); \
74 malloc_write("\"\n"); \
75 abort(); \
76 } \
77 } while (0)
78 # else
79 # define assert(e)
80 # endif
81 #endif
82
83 #ifdef JEMALLOC_DEBUG
84 # define dassert(e) assert(e)
85 #else
86 # define dassert(e)
87 #endif
88
89 /*
90 * jemalloc can conceptually be broken into components (arena, tcache, etc.),
91 * but there are circular dependencies that cannot be broken without
92 * substantial performance degradation. In order to reduce the effect on
93 * visual code flow, read the header files in multiple passes, with one of the
94 * following cpp variables defined during each pass:
95 *
96 * JEMALLOC_H_TYPES : Preprocessor-defined constants and psuedo-opaque data
97 * types.
98 * JEMALLOC_H_STRUCTS : Data structures.
99 * JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.
100 * JEMALLOC_H_INLINES : Inline functions.
101 */
102 /******************************************************************************/
103 #define JEMALLOC_H_TYPES
104
105 #define ALLOCM_LG_ALIGN_MASK ((int)0x3f)
106
107 #define ZU(z) ((size_t)z)
108
109 #ifndef __DECONST
110 # define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
111 #endif
112
113 #ifdef JEMALLOC_DEBUG
114 /* Disable inlining to make debugging easier. */
115 # define JEMALLOC_INLINE
116 # define inline
117 #else
118 # define JEMALLOC_ENABLE_INLINE
119 # define JEMALLOC_INLINE static inline
120 #endif
121
122 /* Size of stack-allocated buffer passed to buferror(). */
123 #define BUFERROR_BUF 64
124
125 /* Minimum alignment of allocations is 2^LG_QUANTUM bytes. */
126 #ifdef __i386__
127 # define LG_QUANTUM 4
128 #endif
129 #ifdef __ia64__
130 # define LG_QUANTUM 4
131 #endif
132 #ifdef __alpha__
133 # define LG_QUANTUM 4
134 #endif
135 #ifdef __sparc64__
136 # define LG_QUANTUM 4
137 #endif
138 #if (defined(__amd64__) || defined(__x86_64__))
139 # define LG_QUANTUM 4
140 #endif
141 #ifdef __arm__
142 # define LG_QUANTUM 3
143 #endif
144 #ifdef __mips__
145 # define LG_QUANTUM 3
146 #endif
147 #ifdef __powerpc__
148 # define LG_QUANTUM 4
149 #endif
150 #ifdef __s390x__
151 # define LG_QUANTUM 4
152 #endif
153
154 #define QUANTUM ((size_t)(1U << LG_QUANTUM))
155 #define QUANTUM_MASK (QUANTUM - 1)
156
157 /* Return the smallest quantum multiple that is >= a. */
158 #define QUANTUM_CEILING(a) \
159 (((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
160
161 #define LONG ((size_t)(1U << LG_SIZEOF_LONG))
162 #define LONG_MASK (LONG - 1)
163
164 /* Return the smallest long multiple that is >= a. */
165 #define LONG_CEILING(a) \
166 (((a) + LONG_MASK) & ~LONG_MASK)
167
168 #define SIZEOF_PTR (1U << LG_SIZEOF_PTR)
169 #define PTR_MASK (SIZEOF_PTR - 1)
170
171 /* Return the smallest (void *) multiple that is >= a. */
172 #define PTR_CEILING(a) \
173 (((a) + PTR_MASK) & ~PTR_MASK)
174
175 /*
176 * Maximum size of L1 cache line. This is used to avoid cache line aliasing.
177 * In addition, this controls the spacing of cacheline-spaced size classes.
178 */
179 #define LG_CACHELINE 6
180 #define CACHELINE ((size_t)(1U << LG_CACHELINE))
181 #define CACHELINE_MASK (CACHELINE - 1)
182
183 /* Return the smallest cacheline multiple that is >= s. */
184 #define CACHELINE_CEILING(s) \
185 (((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
186
187 /*
188 * Page size. STATIC_PAGE_SHIFT is determined by the configure script. If
189 * DYNAMIC_PAGE_SHIFT is enabled, only use the STATIC_PAGE_* macros where
190 * compile-time values are required for the purposes of defining data
191 * structures.
192 */
193 #define STATIC_PAGE_SIZE ((size_t)(1U << STATIC_PAGE_SHIFT))
194 #define STATIC_PAGE_MASK ((size_t)(STATIC_PAGE_SIZE - 1))
195
196 #ifdef PAGE_SHIFT
197 # undef PAGE_SHIFT
198 #endif
199 #ifdef PAGE_SIZE
200 # undef PAGE_SIZE
201 #endif
202 #ifdef PAGE_MASK
203 # undef PAGE_MASK
204 #endif
205
206 #ifdef DYNAMIC_PAGE_SHIFT
207 # define PAGE_SHIFT lg_pagesize
208 # define PAGE_SIZE pagesize
209 # define PAGE_MASK pagesize_mask
210 #else
211 # define PAGE_SHIFT STATIC_PAGE_SHIFT
212 # define PAGE_SIZE STATIC_PAGE_SIZE
213 # define PAGE_MASK STATIC_PAGE_MASK
214 #endif
215
216 /* Return the smallest pagesize multiple that is >= s. */
217 #define PAGE_CEILING(s) \
218 (((s) + PAGE_MASK) & ~PAGE_MASK)
219
220 #include "jemalloc/internal/atomic.h"
221 #include "jemalloc/internal/prn.h"
222 #include "jemalloc/internal/ckh.h"
223 #include "jemalloc/internal/stats.h"
224 #include "jemalloc/internal/ctl.h"
225 #include "jemalloc/internal/mutex.h"
226 #include "jemalloc/internal/mb.h"
227 #include "jemalloc/internal/extent.h"
228 #include "jemalloc/internal/arena.h"
229 #include "jemalloc/internal/bitmap.h"
230 #include "jemalloc/internal/base.h"
231 #include "jemalloc/internal/chunk.h"
232 #include "jemalloc/internal/huge.h"
233 #include "jemalloc/internal/rtree.h"
234 #include "jemalloc/internal/tcache.h"
235 #include "jemalloc/internal/hash.h"
236 #ifdef JEMALLOC_ZONE
237 #include "jemalloc/internal/zone.h"
238 #endif
239 #include "jemalloc/internal/prof.h"
240
241 #undef JEMALLOC_H_TYPES
242 /******************************************************************************/
243 #define JEMALLOC_H_STRUCTS
244
245 #include "jemalloc/internal/atomic.h"
246 #include "jemalloc/internal/prn.h"
247 #include "jemalloc/internal/ckh.h"
248 #include "jemalloc/internal/stats.h"
249 #include "jemalloc/internal/ctl.h"
250 #include "jemalloc/internal/mutex.h"
251 #include "jemalloc/internal/mb.h"
252 #include "jemalloc/internal/bitmap.h"
253 #include "jemalloc/internal/extent.h"
254 #include "jemalloc/internal/arena.h"
255 #include "jemalloc/internal/base.h"
256 #include "jemalloc/internal/chunk.h"
257 #include "jemalloc/internal/huge.h"
258 #include "jemalloc/internal/rtree.h"
259 #include "jemalloc/internal/tcache.h"
260 #include "jemalloc/internal/hash.h"
261 #ifdef JEMALLOC_ZONE
262 #include "jemalloc/internal/zone.h"
263 #endif
264 #include "jemalloc/internal/prof.h"
265
266 #ifdef JEMALLOC_STATS
267 typedef struct {
268 uint64_t allocated;
269 uint64_t deallocated;
270 } thread_allocated_t;
271 #endif
272
273 #undef JEMALLOC_H_STRUCTS
274 /******************************************************************************/
275 #define JEMALLOC_H_EXTERNS
276
277 extern bool opt_abort;
278 #ifdef JEMALLOC_FILL
279 extern bool opt_junk;
280 #endif
281 #ifdef JEMALLOC_SYSV
282 extern bool opt_sysv;
283 #endif
284 #ifdef JEMALLOC_XMALLOC
285 extern bool opt_xmalloc;
286 #endif
287 #ifdef JEMALLOC_FILL
288 extern bool opt_zero;
289 #endif
290 extern size_t opt_narenas;
291
292 #ifdef DYNAMIC_PAGE_SHIFT
293 extern size_t pagesize;
294 extern size_t pagesize_mask;
295 extern size_t lg_pagesize;
296 #endif
297
298 /* Number of CPUs. */
299 extern unsigned ncpus;
300
301 extern malloc_mutex_t arenas_lock; /* Protects arenas initialization. */
302 extern pthread_key_t arenas_tsd;
303 #ifndef NO_TLS
304 /*
305 * Map of pthread_self() --> arenas[???], used for selecting an arena to use
306 * for allocations.
307 */
308 extern __thread arena_t *arenas_tls JEMALLOC_ATTR(tls_model("initial-exec"));
309 # define ARENA_GET() arenas_tls
310 # define ARENA_SET(v) do { \
311 arenas_tls = (v); \
312 pthread_setspecific(arenas_tsd, (void *)(v)); \
313 } while (0)
314 #else
315 # define ARENA_GET() ((arena_t *)pthread_getspecific(arenas_tsd))
316 # define ARENA_SET(v) do { \
317 pthread_setspecific(arenas_tsd, (void *)(v)); \
318 } while (0)
319 #endif
320
321 /*
322 * Arenas that are used to service external requests. Not all elements of the
323 * arenas array are necessarily used; arenas are created lazily as needed.
324 */
325 extern arena_t **arenas;
326 extern unsigned narenas;
327
328 #ifdef JEMALLOC_STATS
329 # ifndef NO_TLS
330 extern __thread thread_allocated_t thread_allocated_tls;
331 # define ALLOCATED_GET() (thread_allocated_tls.allocated)
332 # define ALLOCATEDP_GET() (&thread_allocated_tls.allocated)
333 # define DEALLOCATED_GET() (thread_allocated_tls.deallocated)
334 # define DEALLOCATEDP_GET() (&thread_allocated_tls.deallocated)
335 # define ALLOCATED_ADD(a, d) do { \
336 thread_allocated_tls.allocated += a; \
337 thread_allocated_tls.deallocated += d; \
338 } while (0)
339 # else
340 extern pthread_key_t thread_allocated_tsd;
341 thread_allocated_t *thread_allocated_get_hard(void);
342
343 # define ALLOCATED_GET() (thread_allocated_get()->allocated)
344 # define ALLOCATEDP_GET() (&thread_allocated_get()->allocated)
345 # define DEALLOCATED_GET() (thread_allocated_get()->deallocated)
346 # define DEALLOCATEDP_GET() (&thread_allocated_get()->deallocated)
347 # define ALLOCATED_ADD(a, d) do { \
348 thread_allocated_t *thread_allocated = thread_allocated_get(); \
349 thread_allocated->allocated += (a); \
350 thread_allocated->deallocated += (d); \
351 } while (0)
352 # endif
353 #endif
354
355 arena_t *arenas_extend(unsigned ind);
356 arena_t *choose_arena_hard(void);
357 int buferror(int errnum, char *buf, size_t buflen);
358 void jemalloc_prefork(void);
359 void jemalloc_postfork(void);
360
361 #include "jemalloc/internal/atomic.h"
362 #include "jemalloc/internal/prn.h"
363 #include "jemalloc/internal/ckh.h"
364 #include "jemalloc/internal/stats.h"
365 #include "jemalloc/internal/ctl.h"
366 #include "jemalloc/internal/mutex.h"
367 #include "jemalloc/internal/mb.h"
368 #include "jemalloc/internal/bitmap.h"
369 #include "jemalloc/internal/extent.h"
370 #include "jemalloc/internal/arena.h"
371 #include "jemalloc/internal/base.h"
372 #include "jemalloc/internal/chunk.h"
373 #include "jemalloc/internal/huge.h"
374 #include "jemalloc/internal/rtree.h"
375 #include "jemalloc/internal/tcache.h"
376 #include "jemalloc/internal/hash.h"
377 #ifdef JEMALLOC_ZONE
378 #include "jemalloc/internal/zone.h"
379 #endif
380 #include "jemalloc/internal/prof.h"
381
382 #undef JEMALLOC_H_EXTERNS
383 /******************************************************************************/
384 #define JEMALLOC_H_INLINES
385
386 #include "jemalloc/internal/atomic.h"
387 #include "jemalloc/internal/prn.h"
388 #include "jemalloc/internal/ckh.h"
389 #include "jemalloc/internal/stats.h"
390 #include "jemalloc/internal/ctl.h"
391 #include "jemalloc/internal/mutex.h"
392 #include "jemalloc/internal/mb.h"
393 #include "jemalloc/internal/extent.h"
394 #include "jemalloc/internal/base.h"
395 #include "jemalloc/internal/chunk.h"
396 #include "jemalloc/internal/huge.h"
397
398 #ifndef JEMALLOC_ENABLE_INLINE
399 size_t pow2_ceil(size_t x);
400 size_t s2u(size_t size);
401 size_t sa2u(size_t size, size_t alignment, size_t *run_size_p);
402 void malloc_write(const char *s);
403 arena_t *choose_arena(void);
404 # if (defined(JEMALLOC_STATS) && defined(NO_TLS))
405 thread_allocated_t *thread_allocated_get(void);
406 # endif
407 #endif
408
409 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
410 /* Compute the smallest power of 2 that is >= x. */
411 JEMALLOC_INLINE size_t
412 pow2_ceil(size_t x)
413 {
414
415 x--;
416 x |= x >> 1;
417 x |= x >> 2;
418 x |= x >> 4;
419 x |= x >> 8;
420 x |= x >> 16;
421 #if (LG_SIZEOF_PTR == 3)
422 x |= x >> 32;
423 #endif
424 x++;
425 return (x);
426 }
427
428 /*
429 * Compute usable size that would result from allocating an object with the
430 * specified size.
431 */
432 JEMALLOC_INLINE size_t
433 s2u(size_t size)
434 {
435
436 if (size <= small_maxclass)
437 return (arena_bin_info[SMALL_SIZE2BIN(size)].reg_size);
438 if (size <= arena_maxclass)
439 return (PAGE_CEILING(size));
440 return (CHUNK_CEILING(size));
441 }
442
443 /*
444 * Compute usable size that would result from allocating an object with the
445 * specified size and alignment.
446 */
447 JEMALLOC_INLINE size_t
448 sa2u(size_t size, size_t alignment, size_t *run_size_p)
449 {
450 size_t usize;
451
452 /*
453 * Round size up to the nearest multiple of alignment.
454 *
455 * This done, we can take advantage of the fact that for each small
456 * size class, every object is aligned at the smallest power of two
457 * that is non-zero in the base two representation of the size. For
458 * example:
459 *
460 * Size | Base 2 | Minimum alignment
461 * -----+----------+------------------
462 * 96 | 1100000 | 32
463 * 144 | 10100000 | 32
464 * 192 | 11000000 | 64
465 *
466 * Depending on runtime settings, it is possible that arena_malloc()
467 * will further round up to a power of two, but that never causes
468 * correctness issues.
469 */
470 usize = (size + (alignment - 1)) & (-alignment);
471 /*
472 * (usize < size) protects against the combination of maximal
473 * alignment and size greater than maximal alignment.
474 */
475 if (usize < size) {
476 /* size_t overflow. */
477 return (0);
478 }
479
480 if (usize <= arena_maxclass && alignment <= PAGE_SIZE) {
481 if (usize <= small_maxclass)
482 return (arena_bin_info[SMALL_SIZE2BIN(usize)].reg_size);
483 return (PAGE_CEILING(usize));
484 } else {
485 size_t run_size;
486
487 /*
488 * We can't achieve subpage alignment, so round up alignment
489 * permanently; it makes later calculations simpler.
490 */
491 alignment = PAGE_CEILING(alignment);
492 usize = PAGE_CEILING(size);
493 /*
494 * (usize < size) protects against very large sizes within
495 * PAGE_SIZE of SIZE_T_MAX.
496 *
497 * (usize + alignment < usize) protects against the
498 * combination of maximal alignment and usize large enough
499 * to cause overflow. This is similar to the first overflow
500 * check above, but it needs to be repeated due to the new
501 * usize value, which may now be *equal* to maximal
502 * alignment, whereas before we only detected overflow if the
503 * original size was *greater* than maximal alignment.
504 */
505 if (usize < size || usize + alignment < usize) {
506 /* size_t overflow. */
507 return (0);
508 }
509
510 /*
511 * Calculate the size of the over-size run that arena_palloc()
512 * would need to allocate in order to guarantee the alignment.
513 */
514 if (usize >= alignment)
515 run_size = usize + alignment - PAGE_SIZE;
516 else {
517 /*
518 * It is possible that (alignment << 1) will cause
519 * overflow, but it doesn't matter because we also
520 * subtract PAGE_SIZE, which in the case of overflow
521 * leaves us with a very large run_size. That causes
522 * the first conditional below to fail, which means
523 * that the bogus run_size value never gets used for
524 * anything important.
525 */
526 run_size = (alignment << 1) - PAGE_SIZE;
527 }
528 if (run_size_p != NULL)
529 *run_size_p = run_size;
530
531 if (run_size <= arena_maxclass)
532 return (PAGE_CEILING(usize));
533 return (CHUNK_CEILING(usize));
534 }
535 }
536
537 /*
538 * Wrapper around malloc_message() that avoids the need for
539 * JEMALLOC_P(malloc_message)(...) throughout the code.
540 */
541 JEMALLOC_INLINE void
542 malloc_write(const char *s)
543 {
544
545 JEMALLOC_P(malloc_message)(NULL, s);
546 }
547
548 /*
549 * Choose an arena based on a per-thread value (fast-path code, calls slow-path
550 * code if necessary).
551 */
552 JEMALLOC_INLINE arena_t *
553 choose_arena(void)
554 {
555 arena_t *ret;
556
557 ret = ARENA_GET();
558 if (ret == NULL) {
559 ret = choose_arena_hard();
560 assert(ret != NULL);
561 }
562
563 return (ret);
564 }
565
566 #if (defined(JEMALLOC_STATS) && defined(NO_TLS))
567 JEMALLOC_INLINE thread_allocated_t *
568 thread_allocated_get(void)
569 {
570 thread_allocated_t *thread_allocated = (thread_allocated_t *)
571 pthread_getspecific(thread_allocated_tsd);
572
573 if (thread_allocated == NULL)
574 return (thread_allocated_get_hard());
575 return (thread_allocated);
576 }
577 #endif
578 #endif
579
580 #include "jemalloc/internal/bitmap.h"
581 #include "jemalloc/internal/rtree.h"
582 #include "jemalloc/internal/tcache.h"
583 #include "jemalloc/internal/arena.h"
584 #include "jemalloc/internal/hash.h"
585 #ifdef JEMALLOC_ZONE
586 #include "jemalloc/internal/zone.h"
587 #endif
588
589 #ifndef JEMALLOC_ENABLE_INLINE
590 void *imalloc(size_t size);
591 void *icalloc(size_t size);
592 void *ipalloc(size_t usize, size_t alignment, bool zero);
593 size_t isalloc(const void *ptr);
594 # ifdef JEMALLOC_IVSALLOC
595 size_t ivsalloc(const void *ptr);
596 # endif
597 void idalloc(void *ptr);
598 void *iralloc(void *ptr, size_t size, size_t extra, size_t alignment,
599 bool zero, bool no_move);
600 #endif
601
602 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
603 JEMALLOC_INLINE void *
604 imalloc(size_t size)
605 {
606
607 assert(size != 0);
608
609 if (size <= arena_maxclass)
610 return (arena_malloc(size, false));
611 else
612 return (huge_malloc(size, false));
613 }
614
615 JEMALLOC_INLINE void *
616 icalloc(size_t size)
617 {
618
619 if (size <= arena_maxclass)
620 return (arena_malloc(size, true));
621 else
622 return (huge_malloc(size, true));
623 }
624
625 JEMALLOC_INLINE void *
626 ipalloc(size_t usize, size_t alignment, bool zero)
627 {
628 void *ret;
629
630 assert(usize != 0);
631 assert(usize == sa2u(usize, alignment, NULL));
632
633 if (usize <= arena_maxclass && alignment <= PAGE_SIZE)
634 ret = arena_malloc(usize, zero);
635 else {
636 size_t run_size = 0;
637
638 /*
639 * Ideally we would only ever call sa2u() once per aligned
640 * allocation request, and the caller of this function has
641 * already done so once. However, it's rather burdensome to
642 * require every caller to pass in run_size, especially given
643 * that it's only relevant to large allocations. Therefore,
644 * just call it again here in order to get run_size.
645 */
646 sa2u(usize, alignment, &run_size);
647 if (run_size <= arena_maxclass) {
648 ret = arena_palloc(choose_arena(), usize, run_size,
649 alignment, zero);
650 } else if (alignment <= chunksize)
651 ret = huge_malloc(usize, zero);
652 else
653 ret = huge_palloc(usize, alignment, zero);
654 }
655
656 assert(((uintptr_t)ret & (alignment - 1)) == 0);
657 return (ret);
658 }
659
660 JEMALLOC_INLINE size_t
661 isalloc(const void *ptr)
662 {
663 size_t ret;
664 arena_chunk_t *chunk;
665
666 assert(ptr != NULL);
667
668 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
669 if (chunk != ptr) {
670 /* Region. */
671 dassert(chunk->arena->magic == ARENA_MAGIC);
672
673 #ifdef JEMALLOC_PROF
674 ret = arena_salloc_demote(ptr);
675 #else
676 ret = arena_salloc(ptr);
677 #endif
678 } else
679 ret = huge_salloc(ptr);
680
681 return (ret);
682 }
683
684 #ifdef JEMALLOC_IVSALLOC
685 JEMALLOC_INLINE size_t
686 ivsalloc(const void *ptr)
687 {
688
689 /* Return 0 if ptr is not within a chunk managed by jemalloc. */
690 if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == NULL)
691 return (0);
692
693 return (isalloc(ptr));
694 }
695 #endif
696
697 JEMALLOC_INLINE void
698 idalloc(void *ptr)
699 {
700 arena_chunk_t *chunk;
701
702 assert(ptr != NULL);
703
704 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
705 if (chunk != ptr)
706 arena_dalloc(chunk->arena, chunk, ptr);
707 else
708 huge_dalloc(ptr, true);
709 }
710
711 JEMALLOC_INLINE void *
712 iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
713 bool no_move)
714 {
715 void *ret;
716 size_t oldsize;
717
718 assert(ptr != NULL);
719 assert(size != 0);
720
721 oldsize = isalloc(ptr);
722
723 if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
724 != 0) {
725 size_t usize, copysize;
726
727 /*
728 * Existing object alignment is inadquate; allocate new space
729 * and copy.
730 */
731 if (no_move)
732 return (NULL);
733 usize = sa2u(size + extra, alignment, NULL);
734 if (usize == 0)
735 return (NULL);
736 ret = ipalloc(usize, alignment, zero);
737 if (ret == NULL) {
738 if (extra == 0)
739 return (NULL);
740 /* Try again, without extra this time. */
741 usize = sa2u(size, alignment, NULL);
742 if (usize == 0)
743 return (NULL);
744 ret = ipalloc(usize, alignment, zero);
745 if (ret == NULL)
746 return (NULL);
747 }
748 /*
749 * Copy at most size bytes (not size+extra), since the caller
750 * has no expectation that the extra bytes will be reliably
751 * preserved.
752 */
753 copysize = (size < oldsize) ? size : oldsize;
754 memcpy(ret, ptr, copysize);
755 idalloc(ptr);
756 return (ret);
757 }
758
759 if (no_move) {
760 if (size <= arena_maxclass) {
761 return (arena_ralloc_no_move(ptr, oldsize, size,
762 extra, zero));
763 } else {
764 return (huge_ralloc_no_move(ptr, oldsize, size,
765 extra));
766 }
767 } else {
768 if (size + extra <= arena_maxclass) {
769 return (arena_ralloc(ptr, oldsize, size, extra,
770 alignment, zero));
771 } else {
772 return (huge_ralloc(ptr, oldsize, size, extra,
773 alignment, zero));
774 }
775 }
776 }
777 #endif
778
779 #include "jemalloc/internal/prof.h"
780
781 #undef JEMALLOC_H_INLINES
782 /******************************************************************************/