2010-01-17 01:53:50 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#ifndef SIZE_T_MAX
|
|
|
|
# define SIZE_T_MAX SIZE_MAX
|
|
|
|
#endif
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2010-10-02 08:35:43 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#ifndef offsetof
|
|
|
|
# define offsetof(type, member) ((size_t)&(((type *)NULL)->member))
|
|
|
|
#endif
|
2010-01-25 08:41:01 +08:00
|
|
|
#include <inttypes.h>
|
2010-01-17 01:53:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
2010-10-24 09:37:06 +08:00
|
|
|
#include <ctype.h>
|
2010-01-17 01:53:50 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
2010-10-21 10:05:59 +08:00
|
|
|
#include <math.h>
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-02 09:19:20 +08:00
|
|
|
#define JEMALLOC_NO_DEMANGLE
|
2010-01-17 17:49:20 +08:00
|
|
|
#include "../jemalloc@install_suffix@.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-07-31 08:58:07 +08:00
|
|
|
#include "jemalloc/internal/private_namespace.h"
|
2011-07-31 07:40:52 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
#ifdef JEMALLOC_CC_SILENCE
|
|
|
|
#define UNUSED JEMALLOC_ATTR(unused)
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const bool config_debug =
|
|
|
|
#ifdef JEMALLOC_DEBUG
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_dss =
|
|
|
|
#ifdef JEMALLOC_DSS
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_dynamic_page_shift =
|
|
|
|
#ifdef JEMALLOC_DYNAMIC_PAGE_SHIFT
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_fill =
|
|
|
|
#ifdef JEMALLOC_FILL
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_lazy_lock =
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_prof =
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_prof_libgcc =
|
|
|
|
#ifdef JEMALLOC_PROF_LIBGCC
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_prof_libunwind =
|
|
|
|
#ifdef JEMALLOC_PROF_LIBUNWIND
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_stats =
|
|
|
|
#ifdef JEMALLOC_STATS
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_tcache =
|
|
|
|
#ifdef JEMALLOC_TCACHE
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_tls =
|
|
|
|
#ifdef JEMALLOC_TLS
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_xmalloc =
|
|
|
|
#ifdef JEMALLOC_XMALLOC
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_ivsalloc =
|
|
|
|
#ifdef JEMALLOC_IVSALLOC
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
|
2011-03-19 10:10:31 +08:00
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#endif
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef JEMALLOC_ZONE
|
|
|
|
#include <mach/mach_error.h>
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/vm_map.h>
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-01 07:00:18 +08:00
|
|
|
#define RB_COMPACT
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/rb.h"
|
|
|
|
#include "jemalloc/internal/qr.h"
|
|
|
|
#include "jemalloc/internal/ql.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-02 09:19:20 +08:00
|
|
|
extern void (*je_malloc_message)(void *wcbopaque, const char *s);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Define a custom assert() in order to reduce the chances of deadlock during
|
|
|
|
* assertion failure.
|
|
|
|
*/
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#ifndef assert
|
|
|
|
# ifdef JEMALLOC_DEBUG
|
|
|
|
# define assert(e) do { \
|
2010-01-17 01:53:50 +08:00
|
|
|
if (!(e)) { \
|
|
|
|
char line_buf[UMAX2S_BUFSIZE]; \
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write("<jemalloc>: "); \
|
|
|
|
malloc_write(__FILE__); \
|
|
|
|
malloc_write(":"); \
|
2010-10-24 09:37:06 +08:00
|
|
|
malloc_write(u2s(__LINE__, 10, line_buf)); \
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write(": Failed assertion: "); \
|
|
|
|
malloc_write("\""); \
|
|
|
|
malloc_write(#e); \
|
|
|
|
malloc_write("\"\n"); \
|
2010-01-17 01:53:50 +08:00
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
# else
|
|
|
|
# define assert(e)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
/* Use to assert a particular configuration, e.g., cassert(config_debug). */
|
|
|
|
#define cassert(c) do { \
|
|
|
|
if ((c) == false) \
|
|
|
|
assert(false); \
|
|
|
|
} while (0)
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
2010-02-12 05:38:12 +08:00
|
|
|
* jemalloc can conceptually be broken into components (arena, tcache, etc.),
|
|
|
|
* but there are circular dependencies that cannot be broken without
|
2010-01-17 01:53:50 +08:00
|
|
|
* substantial performance degradation. In order to reduce the effect on
|
|
|
|
* visual code flow, read the header files in multiple passes, with one of the
|
|
|
|
* following cpp variables defined during each pass:
|
|
|
|
*
|
|
|
|
* JEMALLOC_H_TYPES : Preprocessor-defined constants and psuedo-opaque data
|
|
|
|
* types.
|
|
|
|
* JEMALLOC_H_STRUCTS : Data structures.
|
|
|
|
* JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.
|
|
|
|
* JEMALLOC_H_INLINES : Inline functions.
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
#define JEMALLOC_H_TYPES
|
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
#define ALLOCM_LG_ALIGN_MASK ((int)0x3f)
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
#define ZU(z) ((size_t)z)
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#ifndef __DECONST
|
|
|
|
# define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_DEBUG
|
|
|
|
/* Disable inlining to make debugging easier. */
|
|
|
|
# define JEMALLOC_INLINE
|
|
|
|
# define inline
|
|
|
|
#else
|
|
|
|
# define JEMALLOC_ENABLE_INLINE
|
|
|
|
# define JEMALLOC_INLINE static inline
|
|
|
|
#endif
|
|
|
|
|
2010-09-21 07:05:41 +08:00
|
|
|
/* Size of stack-allocated buffer passed to buferror(). */
|
|
|
|
#define BUFERROR_BUF 64
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
/* Smallest size class to support. */
|
|
|
|
#define LG_TINY_MIN 3
|
|
|
|
#define TINY_MIN (1U << LG_TINY_MIN)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimum alignment of allocations is 2^LG_QUANTUM bytes (ignoring tiny size
|
|
|
|
* classes).
|
|
|
|
*/
|
|
|
|
#ifndef LG_QUANTUM
|
|
|
|
# ifdef __i386__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __ia64__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __alpha__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __sparc64__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# if (defined(__amd64__) || defined(__x86_64__))
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __arm__
|
|
|
|
# define LG_QUANTUM 3
|
|
|
|
# endif
|
|
|
|
# ifdef __mips__
|
|
|
|
# define LG_QUANTUM 3
|
|
|
|
# endif
|
|
|
|
# ifdef __powerpc__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __s390x__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
2012-03-06 04:16:57 +08:00
|
|
|
# ifdef __SH4__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
2012-02-29 08:50:47 +08:00
|
|
|
# ifdef __tile__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifndef LG_QUANTUM
|
|
|
|
# error "No LG_QUANTUM definition for architecture; specify via CPPFLAGS"
|
|
|
|
# endif
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define QUANTUM ((size_t)(1U << LG_QUANTUM))
|
|
|
|
#define QUANTUM_MASK (QUANTUM - 1)
|
|
|
|
|
|
|
|
/* Return the smallest quantum multiple that is >= a. */
|
|
|
|
#define QUANTUM_CEILING(a) \
|
|
|
|
(((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
|
|
|
|
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#define LONG ((size_t)(1U << LG_SIZEOF_LONG))
|
|
|
|
#define LONG_MASK (LONG - 1)
|
|
|
|
|
|
|
|
/* Return the smallest long multiple that is >= a. */
|
|
|
|
#define LONG_CEILING(a) \
|
|
|
|
(((a) + LONG_MASK) & ~LONG_MASK)
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#define SIZEOF_PTR (1U << LG_SIZEOF_PTR)
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#define PTR_MASK (SIZEOF_PTR - 1)
|
|
|
|
|
|
|
|
/* Return the smallest (void *) multiple that is >= a. */
|
|
|
|
#define PTR_CEILING(a) \
|
|
|
|
(((a) + PTR_MASK) & ~PTR_MASK)
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum size of L1 cache line. This is used to avoid cache line aliasing.
|
|
|
|
* In addition, this controls the spacing of cacheline-spaced size classes.
|
|
|
|
*/
|
|
|
|
#define LG_CACHELINE 6
|
|
|
|
#define CACHELINE ((size_t)(1U << LG_CACHELINE))
|
|
|
|
#define CACHELINE_MASK (CACHELINE - 1)
|
|
|
|
|
|
|
|
/* Return the smallest cacheline multiple that is >= s. */
|
|
|
|
#define CACHELINE_CEILING(s) \
|
|
|
|
(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
/* Page size. STATIC_PAGE_SHIFT is determined by the configure script. */
|
2010-01-17 01:53:50 +08:00
|
|
|
#define STATIC_PAGE_SIZE ((size_t)(1U << STATIC_PAGE_SHIFT))
|
|
|
|
#define STATIC_PAGE_MASK ((size_t)(STATIC_PAGE_SIZE - 1))
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef PAGE_SHIFT
|
|
|
|
# undef PAGE_SHIFT
|
|
|
|
#endif
|
|
|
|
#ifdef PAGE_SIZE
|
|
|
|
# undef PAGE_SIZE
|
|
|
|
#endif
|
|
|
|
#ifdef PAGE_MASK
|
|
|
|
# undef PAGE_MASK
|
|
|
|
#endif
|
2012-02-29 08:50:47 +08:00
|
|
|
#define PAGE_SHIFT STATIC_PAGE_SHIFT
|
|
|
|
#define PAGE_SIZE STATIC_PAGE_SIZE
|
|
|
|
#define PAGE_MASK STATIC_PAGE_MASK
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Return the smallest pagesize multiple that is >= s. */
|
|
|
|
#define PAGE_CEILING(s) \
|
|
|
|
(((s) + PAGE_MASK) & ~PAGE_MASK)
|
|
|
|
|
2011-03-19 08:56:14 +08:00
|
|
|
#include "jemalloc/internal/atomic.h"
|
2012-03-03 07:59:45 +08:00
|
|
|
#include "jemalloc/internal/prng.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/ckh.h"
|
2012-02-29 08:50:47 +08:00
|
|
|
#include "jemalloc/internal/size_classes.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/stats.h"
|
|
|
|
#include "jemalloc/internal/ctl.h"
|
|
|
|
#include "jemalloc/internal/mutex.h"
|
2010-02-12 07:56:23 +08:00
|
|
|
#include "jemalloc/internal/mb.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/extent.h"
|
|
|
|
#include "jemalloc/internal/arena.h"
|
2011-03-23 00:00:56 +08:00
|
|
|
#include "jemalloc/internal/bitmap.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/base.h"
|
|
|
|
#include "jemalloc/internal/chunk.h"
|
|
|
|
#include "jemalloc/internal/huge.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/tcache.h"
|
|
|
|
#include "jemalloc/internal/hash.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef JEMALLOC_ZONE
|
|
|
|
#include "jemalloc/internal/zone.h"
|
|
|
|
#endif
|
2010-10-21 10:05:59 +08:00
|
|
|
#include "jemalloc/internal/prof.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#undef JEMALLOC_H_TYPES
|
|
|
|
/******************************************************************************/
|
|
|
|
#define JEMALLOC_H_STRUCTS
|
|
|
|
|
2011-03-19 08:56:14 +08:00
|
|
|
#include "jemalloc/internal/atomic.h"
|
2012-03-03 07:59:45 +08:00
|
|
|
#include "jemalloc/internal/prng.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/ckh.h"
|
2012-02-29 08:50:47 +08:00
|
|
|
#include "jemalloc/internal/size_classes.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/stats.h"
|
|
|
|
#include "jemalloc/internal/ctl.h"
|
|
|
|
#include "jemalloc/internal/mutex.h"
|
2010-02-12 07:56:23 +08:00
|
|
|
#include "jemalloc/internal/mb.h"
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#include "jemalloc/internal/bitmap.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/extent.h"
|
|
|
|
#include "jemalloc/internal/arena.h"
|
|
|
|
#include "jemalloc/internal/base.h"
|
|
|
|
#include "jemalloc/internal/chunk.h"
|
|
|
|
#include "jemalloc/internal/huge.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/tcache.h"
|
|
|
|
#include "jemalloc/internal/hash.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef JEMALLOC_ZONE
|
|
|
|
#include "jemalloc/internal/zone.h"
|
|
|
|
#endif
|
2010-10-21 10:05:59 +08:00
|
|
|
#include "jemalloc/internal/prof.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-02-14 10:11:54 +08:00
|
|
|
typedef struct {
|
|
|
|
uint64_t allocated;
|
|
|
|
uint64_t deallocated;
|
|
|
|
} thread_allocated_t;
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#undef JEMALLOC_H_STRUCTS
|
|
|
|
/******************************************************************************/
|
|
|
|
#define JEMALLOC_H_EXTERNS
|
|
|
|
|
|
|
|
extern bool opt_abort;
|
|
|
|
extern bool opt_junk;
|
|
|
|
extern bool opt_xmalloc;
|
|
|
|
extern bool opt_zero;
|
2010-10-24 09:37:06 +08:00
|
|
|
extern size_t opt_narenas;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#ifdef DYNAMIC_PAGE_SHIFT
|
|
|
|
extern size_t pagesize;
|
|
|
|
extern size_t pagesize_mask;
|
|
|
|
extern size_t lg_pagesize;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Number of CPUs. */
|
|
|
|
extern unsigned ncpus;
|
|
|
|
|
2010-01-28 05:10:55 +08:00
|
|
|
extern malloc_mutex_t arenas_lock; /* Protects arenas initialization. */
|
2011-03-19 04:41:33 +08:00
|
|
|
extern pthread_key_t arenas_tsd;
|
2010-01-17 01:53:50 +08:00
|
|
|
#ifndef NO_TLS
|
|
|
|
/*
|
|
|
|
* Map of pthread_self() --> arenas[???], used for selecting an arena to use
|
|
|
|
* for allocations.
|
|
|
|
*/
|
2010-09-06 01:35:13 +08:00
|
|
|
extern __thread arena_t *arenas_tls JEMALLOC_ATTR(tls_model("initial-exec"));
|
|
|
|
# define ARENA_GET() arenas_tls
|
|
|
|
# define ARENA_SET(v) do { \
|
|
|
|
arenas_tls = (v); \
|
2011-03-19 04:41:33 +08:00
|
|
|
pthread_setspecific(arenas_tsd, (void *)(v)); \
|
2010-09-06 01:35:13 +08:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
# define ARENA_GET() ((arena_t *)pthread_getspecific(arenas_tsd))
|
|
|
|
# define ARENA_SET(v) do { \
|
|
|
|
pthread_setspecific(arenas_tsd, (void *)(v)); \
|
|
|
|
} while (0)
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* Arenas that are used to service external requests. Not all elements of the
|
|
|
|
* arenas array are necessarily used; arenas are created lazily as needed.
|
|
|
|
*/
|
|
|
|
extern arena_t **arenas;
|
|
|
|
extern unsigned narenas;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
#ifndef NO_TLS
|
2010-10-21 08:39:18 +08:00
|
|
|
extern __thread thread_allocated_t thread_allocated_tls;
|
2011-02-14 10:11:54 +08:00
|
|
|
# define ALLOCATED_GET() (thread_allocated_tls.allocated)
|
|
|
|
# define ALLOCATEDP_GET() (&thread_allocated_tls.allocated)
|
|
|
|
# define DEALLOCATED_GET() (thread_allocated_tls.deallocated)
|
|
|
|
# define DEALLOCATEDP_GET() (&thread_allocated_tls.deallocated)
|
2010-10-21 08:39:18 +08:00
|
|
|
# define ALLOCATED_ADD(a, d) do { \
|
|
|
|
thread_allocated_tls.allocated += a; \
|
|
|
|
thread_allocated_tls.deallocated += d; \
|
|
|
|
} while (0)
|
2012-02-11 12:22:09 +08:00
|
|
|
#else
|
2011-02-14 10:11:54 +08:00
|
|
|
# define ALLOCATED_GET() (thread_allocated_get()->allocated)
|
|
|
|
# define ALLOCATEDP_GET() (&thread_allocated_get()->allocated)
|
|
|
|
# define DEALLOCATED_GET() (thread_allocated_get()->deallocated)
|
|
|
|
# define DEALLOCATEDP_GET() (&thread_allocated_get()->deallocated)
|
2010-10-21 08:39:18 +08:00
|
|
|
# define ALLOCATED_ADD(a, d) do { \
|
2011-02-14 10:11:54 +08:00
|
|
|
thread_allocated_t *thread_allocated = thread_allocated_get(); \
|
|
|
|
thread_allocated->allocated += (a); \
|
|
|
|
thread_allocated->deallocated += (d); \
|
2010-10-21 08:39:18 +08:00
|
|
|
} while (0)
|
|
|
|
#endif
|
2012-02-11 12:22:09 +08:00
|
|
|
extern pthread_key_t thread_allocated_tsd;
|
|
|
|
thread_allocated_t *thread_allocated_get_hard(void);
|
2010-10-21 08:39:18 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_t *arenas_extend(unsigned ind);
|
|
|
|
arena_t *choose_arena_hard(void);
|
2010-09-21 07:05:41 +08:00
|
|
|
int buferror(int errnum, char *buf, size_t buflen);
|
2010-09-06 01:35:13 +08:00
|
|
|
void jemalloc_prefork(void);
|
|
|
|
void jemalloc_postfork(void);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-03-19 08:56:14 +08:00
|
|
|
#include "jemalloc/internal/atomic.h"
|
2012-03-03 07:59:45 +08:00
|
|
|
#include "jemalloc/internal/prng.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/ckh.h"
|
2012-02-29 08:50:47 +08:00
|
|
|
#include "jemalloc/internal/size_classes.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/stats.h"
|
|
|
|
#include "jemalloc/internal/ctl.h"
|
|
|
|
#include "jemalloc/internal/mutex.h"
|
2010-02-12 07:56:23 +08:00
|
|
|
#include "jemalloc/internal/mb.h"
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#include "jemalloc/internal/bitmap.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/extent.h"
|
|
|
|
#include "jemalloc/internal/arena.h"
|
|
|
|
#include "jemalloc/internal/base.h"
|
|
|
|
#include "jemalloc/internal/chunk.h"
|
|
|
|
#include "jemalloc/internal/huge.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/tcache.h"
|
|
|
|
#include "jemalloc/internal/hash.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef JEMALLOC_ZONE
|
|
|
|
#include "jemalloc/internal/zone.h"
|
|
|
|
#endif
|
2010-10-21 10:05:59 +08:00
|
|
|
#include "jemalloc/internal/prof.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#undef JEMALLOC_H_EXTERNS
|
|
|
|
/******************************************************************************/
|
|
|
|
#define JEMALLOC_H_INLINES
|
|
|
|
|
2011-03-19 08:56:14 +08:00
|
|
|
#include "jemalloc/internal/atomic.h"
|
2012-03-03 07:59:45 +08:00
|
|
|
#include "jemalloc/internal/prng.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/ckh.h"
|
2012-02-29 08:50:47 +08:00
|
|
|
#include "jemalloc/internal/size_classes.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/stats.h"
|
|
|
|
#include "jemalloc/internal/ctl.h"
|
|
|
|
#include "jemalloc/internal/mutex.h"
|
2010-02-12 07:56:23 +08:00
|
|
|
#include "jemalloc/internal/mb.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/extent.h"
|
|
|
|
#include "jemalloc/internal/base.h"
|
|
|
|
#include "jemalloc/internal/chunk.h"
|
|
|
|
#include "jemalloc/internal/huge.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
2010-09-06 01:35:13 +08:00
|
|
|
size_t pow2_ceil(size_t x);
|
2010-10-21 08:39:18 +08:00
|
|
|
size_t s2u(size_t size);
|
|
|
|
size_t sa2u(size_t size, size_t alignment, size_t *run_size_p);
|
2010-03-04 09:45:38 +08:00
|
|
|
void malloc_write(const char *s);
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_t *choose_arena(void);
|
2011-02-14 10:11:54 +08:00
|
|
|
thread_allocated_t *thread_allocated_get(void);
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
|
2010-09-06 01:35:13 +08:00
|
|
|
/* Compute the smallest power of 2 that is >= x. */
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
pow2_ceil(size_t x)
|
|
|
|
{
|
|
|
|
|
|
|
|
x--;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
|
|
|
x |= x >> 32;
|
|
|
|
#endif
|
|
|
|
x++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
/*
|
|
|
|
* Compute usable size that would result from allocating an object with the
|
|
|
|
* specified size.
|
|
|
|
*/
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
s2u(size_t size)
|
|
|
|
{
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
if (size <= SMALL_MAXCLASS)
|
2011-03-16 04:59:15 +08:00
|
|
|
return (arena_bin_info[SMALL_SIZE2BIN(size)].reg_size);
|
2010-10-21 08:39:18 +08:00
|
|
|
if (size <= arena_maxclass)
|
2011-02-14 10:44:59 +08:00
|
|
|
return (PAGE_CEILING(size));
|
|
|
|
return (CHUNK_CEILING(size));
|
2010-10-21 08:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute usable size that would result from allocating an object with the
|
|
|
|
* specified size and alignment.
|
|
|
|
*/
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
sa2u(size_t size, size_t alignment, size_t *run_size_p)
|
|
|
|
{
|
|
|
|
size_t usize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Round size up to the nearest multiple of alignment.
|
|
|
|
*
|
|
|
|
* This done, we can take advantage of the fact that for each small
|
|
|
|
* size class, every object is aligned at the smallest power of two
|
|
|
|
* that is non-zero in the base two representation of the size. For
|
|
|
|
* example:
|
|
|
|
*
|
|
|
|
* Size | Base 2 | Minimum alignment
|
|
|
|
* -----+----------+------------------
|
|
|
|
* 96 | 1100000 | 32
|
|
|
|
* 144 | 10100000 | 32
|
|
|
|
* 192 | 11000000 | 64
|
|
|
|
*/
|
|
|
|
usize = (size + (alignment - 1)) & (-alignment);
|
|
|
|
/*
|
|
|
|
* (usize < size) protects against the combination of maximal
|
|
|
|
* alignment and size greater than maximal alignment.
|
|
|
|
*/
|
|
|
|
if (usize < size) {
|
|
|
|
/* size_t overflow. */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usize <= arena_maxclass && alignment <= PAGE_SIZE) {
|
2012-02-29 08:50:47 +08:00
|
|
|
if (usize <= SMALL_MAXCLASS)
|
2011-03-16 04:59:15 +08:00
|
|
|
return (arena_bin_info[SMALL_SIZE2BIN(usize)].reg_size);
|
2010-10-21 08:39:18 +08:00
|
|
|
return (PAGE_CEILING(usize));
|
|
|
|
} else {
|
|
|
|
size_t run_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't achieve subpage alignment, so round up alignment
|
|
|
|
* permanently; it makes later calculations simpler.
|
|
|
|
*/
|
|
|
|
alignment = PAGE_CEILING(alignment);
|
|
|
|
usize = PAGE_CEILING(size);
|
|
|
|
/*
|
|
|
|
* (usize < size) protects against very large sizes within
|
|
|
|
* PAGE_SIZE of SIZE_T_MAX.
|
|
|
|
*
|
|
|
|
* (usize + alignment < usize) protects against the
|
|
|
|
* combination of maximal alignment and usize large enough
|
|
|
|
* to cause overflow. This is similar to the first overflow
|
|
|
|
* check above, but it needs to be repeated due to the new
|
|
|
|
* usize value, which may now be *equal* to maximal
|
|
|
|
* alignment, whereas before we only detected overflow if the
|
|
|
|
* original size was *greater* than maximal alignment.
|
|
|
|
*/
|
|
|
|
if (usize < size || usize + alignment < usize) {
|
|
|
|
/* size_t overflow. */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the size of the over-size run that arena_palloc()
|
|
|
|
* would need to allocate in order to guarantee the alignment.
|
|
|
|
*/
|
|
|
|
if (usize >= alignment)
|
|
|
|
run_size = usize + alignment - PAGE_SIZE;
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* It is possible that (alignment << 1) will cause
|
|
|
|
* overflow, but it doesn't matter because we also
|
|
|
|
* subtract PAGE_SIZE, which in the case of overflow
|
|
|
|
* leaves us with a very large run_size. That causes
|
|
|
|
* the first conditional below to fail, which means
|
|
|
|
* that the bogus run_size value never gets used for
|
|
|
|
* anything important.
|
|
|
|
*/
|
|
|
|
run_size = (alignment << 1) - PAGE_SIZE;
|
|
|
|
}
|
|
|
|
if (run_size_p != NULL)
|
|
|
|
*run_size_p = run_size;
|
|
|
|
|
|
|
|
if (run_size <= arena_maxclass)
|
|
|
|
return (PAGE_CEILING(usize));
|
|
|
|
return (CHUNK_CEILING(usize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* Wrapper around malloc_message() that avoids the need for
|
2012-03-02 09:19:20 +08:00
|
|
|
* je_malloc_message(...) throughout the code.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
|
|
|
JEMALLOC_INLINE void
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write(const char *s)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
2012-03-02 09:19:20 +08:00
|
|
|
je_malloc_message(NULL, s);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Choose an arena based on a per-thread value (fast-path code, calls slow-path
|
|
|
|
* code if necessary).
|
|
|
|
*/
|
|
|
|
JEMALLOC_INLINE arena_t *
|
|
|
|
choose_arena(void)
|
|
|
|
{
|
|
|
|
arena_t *ret;
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
ret = ARENA_GET();
|
2010-01-17 01:53:50 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
ret = choose_arena_hard();
|
|
|
|
assert(ret != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2011-02-14 10:11:54 +08:00
|
|
|
|
|
|
|
JEMALLOC_INLINE thread_allocated_t *
|
|
|
|
thread_allocated_get(void)
|
|
|
|
{
|
|
|
|
thread_allocated_t *thread_allocated = (thread_allocated_t *)
|
|
|
|
pthread_getspecific(thread_allocated_tsd);
|
|
|
|
|
|
|
|
if (thread_allocated == NULL)
|
|
|
|
return (thread_allocated_get_hard());
|
|
|
|
return (thread_allocated);
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
#include "jemalloc/internal/bitmap.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/tcache.h"
|
|
|
|
#include "jemalloc/internal/arena.h"
|
|
|
|
#include "jemalloc/internal/hash.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef JEMALLOC_ZONE
|
|
|
|
#include "jemalloc/internal/zone.h"
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
|
|
|
void *imalloc(size_t size);
|
|
|
|
void *icalloc(size_t size);
|
2011-03-23 15:37:29 +08:00
|
|
|
void *ipalloc(size_t usize, size_t alignment, bool zero);
|
2010-01-17 01:53:50 +08:00
|
|
|
size_t isalloc(const void *ptr);
|
2010-09-06 01:35:13 +08:00
|
|
|
size_t ivsalloc(const void *ptr);
|
2010-09-21 10:20:48 +08:00
|
|
|
void idalloc(void *ptr);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
void *iralloc(void *ptr, size_t size, size_t extra, size_t alignment,
|
|
|
|
bool zero, bool no_move);
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
|
|
|
|
JEMALLOC_INLINE void *
|
|
|
|
imalloc(size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
|
|
|
|
if (size <= arena_maxclass)
|
|
|
|
return (arena_malloc(size, false));
|
|
|
|
else
|
|
|
|
return (huge_malloc(size, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_INLINE void *
|
|
|
|
icalloc(size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (size <= arena_maxclass)
|
|
|
|
return (arena_malloc(size, true));
|
|
|
|
else
|
|
|
|
return (huge_malloc(size, true));
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
JEMALLOC_INLINE void *
|
2011-03-23 15:37:29 +08:00
|
|
|
ipalloc(size_t usize, size_t alignment, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2010-02-11 02:37:56 +08:00
|
|
|
void *ret;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-03-23 15:37:29 +08:00
|
|
|
assert(usize != 0);
|
|
|
|
assert(usize == sa2u(usize, alignment, NULL));
|
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
if (usize <= arena_maxclass && alignment <= PAGE_SIZE)
|
|
|
|
ret = arena_malloc(usize, zero);
|
2011-03-23 15:37:29 +08:00
|
|
|
else {
|
|
|
|
size_t run_size
|
|
|
|
#ifdef JEMALLOC_CC_SILENCE
|
|
|
|
= 0
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ideally we would only ever call sa2u() once per aligned
|
|
|
|
* allocation request, and the caller of this function has
|
|
|
|
* already done so once. However, it's rather burdensome to
|
|
|
|
* require every caller to pass in run_size, especially given
|
|
|
|
* that it's only relevant to large allocations. Therefore,
|
|
|
|
* just call it again here in order to get run_size.
|
|
|
|
*/
|
|
|
|
sa2u(usize, alignment, &run_size);
|
|
|
|
if (run_size <= arena_maxclass) {
|
|
|
|
ret = arena_palloc(choose_arena(), usize, run_size,
|
|
|
|
alignment, zero);
|
|
|
|
} else if (alignment <= chunksize)
|
|
|
|
ret = huge_malloc(usize, zero);
|
|
|
|
else
|
|
|
|
ret = huge_palloc(usize, alignment, zero);
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
assert(((uintptr_t)ret & (alignment - 1)) == 0);
|
|
|
|
return (ret);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
isalloc(const void *ptr)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
|
|
|
assert(ptr != NULL);
|
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk != ptr) {
|
|
|
|
/* Region. */
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_prof)
|
|
|
|
ret = arena_salloc_demote(ptr);
|
|
|
|
else
|
|
|
|
ret = arena_salloc(ptr);
|
2010-01-17 01:53:50 +08:00
|
|
|
} else
|
|
|
|
ret = huge_salloc(ptr);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
ivsalloc(const void *ptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Return 0 if ptr is not within a chunk managed by jemalloc. */
|
|
|
|
if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (isalloc(ptr));
|
|
|
|
}
|
|
|
|
|
2010-09-21 10:20:48 +08:00
|
|
|
JEMALLOC_INLINE void
|
|
|
|
idalloc(void *ptr)
|
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
|
|
|
assert(ptr != NULL);
|
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk != ptr)
|
|
|
|
arena_dalloc(chunk->arena, chunk, ptr);
|
|
|
|
else
|
2010-12-01 08:50:58 +08:00
|
|
|
huge_dalloc(ptr, true);
|
2010-09-21 10:20:48 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
JEMALLOC_INLINE void *
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
|
|
|
|
bool no_move)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
void *ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t oldsize;
|
|
|
|
|
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size != 0);
|
|
|
|
|
|
|
|
oldsize = isalloc(ptr);
|
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
|
|
|
|
!= 0) {
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t usize, copysize;
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Existing object alignment is inadquate; allocate new space
|
|
|
|
* and copy.
|
|
|
|
*/
|
|
|
|
if (no_move)
|
|
|
|
return (NULL);
|
2011-03-23 15:37:29 +08:00
|
|
|
usize = sa2u(size + extra, alignment, NULL);
|
|
|
|
if (usize == 0)
|
|
|
|
return (NULL);
|
|
|
|
ret = ipalloc(usize, alignment, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
if (extra == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Try again, without extra this time. */
|
2011-03-23 15:37:29 +08:00
|
|
|
usize = sa2u(size, alignment, NULL);
|
|
|
|
if (usize == 0)
|
|
|
|
return (NULL);
|
|
|
|
ret = ipalloc(usize, alignment, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Copy at most size bytes (not size+extra), since the caller
|
|
|
|
* has no expectation that the extra bytes will be reliably
|
|
|
|
* preserved.
|
|
|
|
*/
|
|
|
|
copysize = (size < oldsize) ? size : oldsize;
|
|
|
|
memcpy(ret, ptr, copysize);
|
|
|
|
idalloc(ptr);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_move) {
|
|
|
|
if (size <= arena_maxclass) {
|
|
|
|
return (arena_ralloc_no_move(ptr, oldsize, size,
|
|
|
|
extra, zero));
|
|
|
|
} else {
|
|
|
|
return (huge_ralloc_no_move(ptr, oldsize, size,
|
|
|
|
extra));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (size + extra <= arena_maxclass) {
|
|
|
|
return (arena_ralloc(ptr, oldsize, size, extra,
|
|
|
|
alignment, zero));
|
|
|
|
} else {
|
|
|
|
return (huge_ralloc(ptr, oldsize, size, extra,
|
|
|
|
alignment, zero));
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
#include "jemalloc/internal/prof.h"
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#undef JEMALLOC_H_INLINES
|
|
|
|
/******************************************************************************/
|