2012-04-16 22:30:26 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_H
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_INTERNAL_H
|
2012-04-30 18:38:26 +08:00
|
|
|
#include <math.h>
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
2012-04-30 18:38:26 +08:00
|
|
|
# define ENOENT ERROR_PATH_NOT_FOUND
|
|
|
|
# define EINVAL ERROR_BAD_ARGUMENTS
|
|
|
|
# define EAGAIN ERROR_OUTOFMEMORY
|
|
|
|
# define EPERM ERROR_WRITE_FAULT
|
|
|
|
# define EFAULT ERROR_INVALID_ADDRESS
|
|
|
|
# define ENOMEM ERROR_NOT_ENOUGH_MEMORY
|
|
|
|
# undef ERANGE
|
|
|
|
# define ERANGE ERROR_INVALID_DATA
|
2012-04-22 12:27:46 +08:00
|
|
|
#else
|
2012-04-30 18:38:31 +08:00
|
|
|
# include <sys/param.h>
|
2012-04-22 12:27:46 +08:00
|
|
|
# include <sys/mman.h>
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# if !defined(SYS_write) && defined(__NR_write)
|
|
|
|
# define SYS_write __NR_write
|
|
|
|
# endif
|
|
|
|
# include <sys/uio.h>
|
|
|
|
# include <pthread.h>
|
2012-04-30 18:38:26 +08:00
|
|
|
# include <errno.h>
|
2012-03-27 20:48:58 +08:00
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#ifndef SIZE_T_MAX
|
|
|
|
# define SIZE_T_MAX SIZE_MAX
|
|
|
|
#endif
|
|
|
|
#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>
|
2012-04-30 18:38:31 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <io.h>
|
|
|
|
typedef intptr_t ssize_t;
|
|
|
|
# define PATH_MAX 1024
|
|
|
|
# define STDERR_FILENO 2
|
|
|
|
# define __func__ __FUNCTION__
|
|
|
|
/* Disable warnings about deprecated system functions */
|
|
|
|
# pragma warning(disable: 4996)
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
Refactor to support more varied testing.
Refactor the test harness to support three types of tests:
- unit: White box unit tests. These tests have full access to all
internal jemalloc library symbols. Though in actuality all symbols
are prefixed by jet_, macro-based name mangling abstracts this away
from test code.
- integration: Black box integration tests. These tests link with
the installable shared jemalloc library, and with the exception of
some utility code and configure-generated macro definitions, they have
no access to jemalloc internals.
- stress: Black box stress tests. These tests link with the installable
shared jemalloc library, as well as with an internal allocator with
symbols prefixed by jet_ (same as for unit tests) that can be used to
allocate data structures that are internal to the test code.
Move existing tests into test/{unit,integration}/ as appropriate.
Split out internal parts of jemalloc_defs.h.in and put them in
jemalloc_internal_defs.h.in. This reduces internals exposure to
applications that #include <jemalloc/jemalloc.h>.
Refactor jemalloc.h header generation so that a single header file
results, and the prototypes can be used to generate jet_ prototypes for
tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in,
jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and
use a shell script to generate a unified jemalloc.h at configure time.
Change the default private namespace prefix from "" to "je_".
Add missing private namespace mangling.
Remove hard-coded private_namespace.h. Instead generate it and
private_unnamespace.h from private_symbols.txt. Use similar logic for
public symbols, which aids in name mangling for jet_ symbols.
Add test_warn() and test_fail(). Replace existing exit(1) calls with
test_fail() calls.
2013-12-01 07:25:42 +08:00
|
|
|
#include "jemalloc_internal_defs.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-04-06 04:36:17 +08:00
|
|
|
#ifdef JEMALLOC_UTRACE
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
|
|
|
|
Refactor to support more varied testing.
Refactor the test harness to support three types of tests:
- unit: White box unit tests. These tests have full access to all
internal jemalloc library symbols. Though in actuality all symbols
are prefixed by jet_, macro-based name mangling abstracts this away
from test code.
- integration: Black box integration tests. These tests link with
the installable shared jemalloc library, and with the exception of
some utility code and configure-generated macro definitions, they have
no access to jemalloc internals.
- stress: Black box stress tests. These tests link with the installable
shared jemalloc library, as well as with an internal allocator with
symbols prefixed by jet_ (same as for unit tests) that can be used to
allocate data structures that are internal to the test code.
Move existing tests into test/{unit,integration}/ as appropriate.
Split out internal parts of jemalloc_defs.h.in and put them in
jemalloc_internal_defs.h.in. This reduces internals exposure to
applications that #include <jemalloc/jemalloc.h>.
Refactor jemalloc.h header generation so that a single header file
results, and the prototypes can be used to generate jet_ prototypes for
tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in,
jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and
use a shell script to generate a unified jemalloc.h at configure time.
Change the default private namespace prefix from "" to "je_".
Add missing private namespace mangling.
Remove hard-coded private_namespace.h. Instead generate it and
private_unnamespace.h from private_symbols.txt. Use similar logic for
public symbols, which aids in name mangling for jet_ symbols.
Add test_warn() and test_fail(). Replace existing exit(1) calls with
test_fail() calls.
2013-12-01 07:25:42 +08:00
|
|
|
#define JEMALLOC_NO_DEMANGLE
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
# define JEMALLOC_N(n) jet_##n
|
|
|
|
# include "jemalloc/internal/public_namespace.h"
|
|
|
|
# define JEMALLOC_NO_RENAME
|
|
|
|
# include "../jemalloc@install_suffix@.h"
|
2014-01-17 09:38:01 +08:00
|
|
|
# undef JEMALLOC_NO_RENAME
|
Refactor to support more varied testing.
Refactor the test harness to support three types of tests:
- unit: White box unit tests. These tests have full access to all
internal jemalloc library symbols. Though in actuality all symbols
are prefixed by jet_, macro-based name mangling abstracts this away
from test code.
- integration: Black box integration tests. These tests link with
the installable shared jemalloc library, and with the exception of
some utility code and configure-generated macro definitions, they have
no access to jemalloc internals.
- stress: Black box stress tests. These tests link with the installable
shared jemalloc library, as well as with an internal allocator with
symbols prefixed by jet_ (same as for unit tests) that can be used to
allocate data structures that are internal to the test code.
Move existing tests into test/{unit,integration}/ as appropriate.
Split out internal parts of jemalloc_defs.h.in and put them in
jemalloc_internal_defs.h.in. This reduces internals exposure to
applications that #include <jemalloc/jemalloc.h>.
Refactor jemalloc.h header generation so that a single header file
results, and the prototypes can be used to generate jet_ prototypes for
tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in,
jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and
use a shell script to generate a unified jemalloc.h at configure time.
Change the default private namespace prefix from "" to "je_".
Add missing private namespace mangling.
Remove hard-coded private_namespace.h. Instead generate it and
private_unnamespace.h from private_symbols.txt. Use similar logic for
public symbols, which aids in name mangling for jet_ symbols.
Add test_warn() and test_fail(). Replace existing exit(1) calls with
test_fail() calls.
2013-12-01 07:25:42 +08:00
|
|
|
#else
|
|
|
|
# define JEMALLOC_N(n) @private_namespace@##n
|
|
|
|
# include "../jemalloc@install_suffix@.h"
|
|
|
|
#endif
|
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
|
|
|
static const bool config_debug =
|
|
|
|
#ifdef JEMALLOC_DEBUG
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2014-04-16 03:09:48 +08:00
|
|
|
static const bool have_dss =
|
2012-02-11 12:22:09 +08:00
|
|
|
#ifdef JEMALLOC_DSS
|
|
|
|
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
|
|
|
|
;
|
2012-05-10 07:12:00 +08:00
|
|
|
static const bool config_mremap =
|
|
|
|
#ifdef JEMALLOC_MREMAP
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2012-04-13 11:20:58 +08:00
|
|
|
static const bool config_munmap =
|
|
|
|
#ifdef JEMALLOC_MUNMAP
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2012-02-11 12:22:09 +08:00
|
|
|
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
|
|
|
|
;
|
2012-04-06 04:36:17 +08:00
|
|
|
static const bool config_utrace =
|
|
|
|
#ifdef JEMALLOC_UTRACE
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2012-04-06 15:35:09 +08:00
|
|
|
static const bool config_valgrind =
|
|
|
|
#ifdef JEMALLOC_VALGRIND
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2012-02-11 12:22:09 +08:00
|
|
|
static const bool config_xmalloc =
|
|
|
|
#ifdef JEMALLOC_XMALLOC
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static const bool config_ivsalloc =
|
|
|
|
#ifdef JEMALLOC_IVSALLOC
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2012-04-18 04:17:54 +08:00
|
|
|
#ifdef JEMALLOC_ATOMIC9
|
|
|
|
#include <machine/atomic.h>
|
|
|
|
#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
|
|
|
|
|
|
|
/*
|
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.
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_H_TYPES
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2013-12-06 13:43:46 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_macros.h"
|
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
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
#define MALLOCX_LG_ALIGN_MASK ((int)0x3f)
|
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
|
2012-04-30 18:38:31 +08:00
|
|
|
# if (defined(__i386__) || defined(_M_IX86))
|
2012-02-29 08:50:47 +08:00
|
|
|
# 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
|
2012-04-30 18:38:31 +08:00
|
|
|
# if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64))
|
2012-02-29 08:50:47 +08:00
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
|
|
|
# ifdef __arm__
|
|
|
|
# define LG_QUANTUM 3
|
|
|
|
# endif
|
2013-03-18 22:40:20 +08:00
|
|
|
# ifdef __aarch64__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
2012-10-09 06:41:06 +08:00
|
|
|
# ifdef __hppa__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
2012-02-29 08:50:47 +08:00
|
|
|
# ifdef __mips__
|
|
|
|
# define LG_QUANTUM 3
|
|
|
|
# endif
|
|
|
|
# ifdef __powerpc__
|
|
|
|
# define LG_QUANTUM 4
|
|
|
|
# endif
|
2013-01-29 04:19:34 +08:00
|
|
|
# ifdef __s390__
|
2012-02-29 08:50:47 +08:00
|
|
|
# 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. */
|
2012-03-14 02:09:23 +08:00
|
|
|
#define LONG_CEILING(a) \
|
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
|
|
|
(((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. */
|
2012-03-14 02:09:23 +08:00
|
|
|
#define PTR_CEILING(a) \
|
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
|
|
|
(((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.
|
2012-05-02 16:22:16 +08:00
|
|
|
*
|
|
|
|
* CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can
|
|
|
|
* only handle raw constants.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
|
|
|
#define LG_CACHELINE 6
|
2012-05-02 16:22:16 +08:00
|
|
|
#define CACHELINE 64
|
2010-01-17 01:53:50 +08:00
|
|
|
#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-09-06 01:35:13 +08:00
|
|
|
#ifdef PAGE_MASK
|
|
|
|
# undef PAGE_MASK
|
|
|
|
#endif
|
2012-04-02 22:04:34 +08:00
|
|
|
#define LG_PAGE STATIC_PAGE_SHIFT
|
|
|
|
#define PAGE ((size_t)(1U << STATIC_PAGE_SHIFT))
|
|
|
|
#define PAGE_MASK ((size_t)(PAGE - 1))
|
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)
|
|
|
|
|
2012-04-12 09:13:45 +08:00
|
|
|
/* Return the nearest aligned address at or below a. */
|
|
|
|
#define ALIGNMENT_ADDR2BASE(a, alignment) \
|
|
|
|
((void *)((uintptr_t)(a) & (-(alignment))))
|
|
|
|
|
|
|
|
/* Return the offset between a and the nearest aligned address at or below a. */
|
|
|
|
#define ALIGNMENT_ADDR2OFFSET(a, alignment) \
|
|
|
|
((size_t)((uintptr_t)(a) & (alignment - 1)))
|
|
|
|
|
|
|
|
/* Return the smallest alignment multiple that is >= s. */
|
|
|
|
#define ALIGNMENT_CEILING(s, alignment) \
|
|
|
|
(((s) + (alignment - 1)) & (-(alignment)))
|
|
|
|
|
2012-04-25 05:22:02 +08:00
|
|
|
/* Declare a variable length array */
|
|
|
|
#if __STDC_VERSION__ < 199901L
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# include <malloc.h>
|
|
|
|
# define alloca _alloca
|
|
|
|
# else
|
2012-12-03 09:58:40 +08:00
|
|
|
# ifdef JEMALLOC_HAS_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
# else
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
2012-04-25 05:22:02 +08:00
|
|
|
# endif
|
|
|
|
# define VARIABLE_ARRAY(type, name, count) \
|
2014-04-22 11:52:35 +08:00
|
|
|
type *name = alloca(sizeof(type) * (count))
|
2012-04-25 05:22:02 +08:00
|
|
|
#else
|
2014-04-22 11:52:35 +08:00
|
|
|
# define VARIABLE_ARRAY(type, name, count) type name[(count)]
|
2012-04-25 05:22:02 +08:00
|
|
|
#endif
|
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
#include "jemalloc/internal/valgrind.h"
|
2012-03-07 06:57:45 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
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"
|
2012-03-22 09:33:03 +08:00
|
|
|
#include "jemalloc/internal/tsd.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"
|
2012-04-06 15:35:09 +08:00
|
|
|
#include "jemalloc/internal/quarantine.h"
|
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
|
|
|
|
/******************************************************************************/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_H_STRUCTS
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
#include "jemalloc/internal/valgrind.h"
|
2012-03-07 06:57:45 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
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"
|
2012-03-22 09:33:03 +08:00
|
|
|
#include "jemalloc/internal/tsd.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"
|
2012-04-06 15:35:09 +08:00
|
|
|
#include "jemalloc/internal/quarantine.h"
|
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;
|
2012-03-22 09:33:03 +08:00
|
|
|
/*
|
Refactor to support more varied testing.
Refactor the test harness to support three types of tests:
- unit: White box unit tests. These tests have full access to all
internal jemalloc library symbols. Though in actuality all symbols
are prefixed by jet_, macro-based name mangling abstracts this away
from test code.
- integration: Black box integration tests. These tests link with
the installable shared jemalloc library, and with the exception of
some utility code and configure-generated macro definitions, they have
no access to jemalloc internals.
- stress: Black box stress tests. These tests link with the installable
shared jemalloc library, as well as with an internal allocator with
symbols prefixed by jet_ (same as for unit tests) that can be used to
allocate data structures that are internal to the test code.
Move existing tests into test/{unit,integration}/ as appropriate.
Split out internal parts of jemalloc_defs.h.in and put them in
jemalloc_internal_defs.h.in. This reduces internals exposure to
applications that #include <jemalloc/jemalloc.h>.
Refactor jemalloc.h header generation so that a single header file
results, and the prototypes can be used to generate jet_ prototypes for
tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in,
jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and
use a shell script to generate a unified jemalloc.h at configure time.
Change the default private namespace prefix from "" to "je_".
Add missing private namespace mangling.
Remove hard-coded private_namespace.h. Instead generate it and
private_unnamespace.h from private_symbols.txt. Use similar logic for
public symbols, which aids in name mangling for jet_ symbols.
Add test_warn() and test_fail(). Replace existing exit(1) calls with
test_fail() calls.
2013-12-01 07:25:42 +08:00
|
|
|
* The JEMALLOC_ARG_CONCAT() wrapper is necessary to pass {0, 0} via a cpp macro
|
2012-03-22 09:33:03 +08:00
|
|
|
* argument.
|
|
|
|
*/
|
Refactor to support more varied testing.
Refactor the test harness to support three types of tests:
- unit: White box unit tests. These tests have full access to all
internal jemalloc library symbols. Though in actuality all symbols
are prefixed by jet_, macro-based name mangling abstracts this away
from test code.
- integration: Black box integration tests. These tests link with
the installable shared jemalloc library, and with the exception of
some utility code and configure-generated macro definitions, they have
no access to jemalloc internals.
- stress: Black box stress tests. These tests link with the installable
shared jemalloc library, as well as with an internal allocator with
symbols prefixed by jet_ (same as for unit tests) that can be used to
allocate data structures that are internal to the test code.
Move existing tests into test/{unit,integration}/ as appropriate.
Split out internal parts of jemalloc_defs.h.in and put them in
jemalloc_internal_defs.h.in. This reduces internals exposure to
applications that #include <jemalloc/jemalloc.h>.
Refactor jemalloc.h header generation so that a single header file
results, and the prototypes can be used to generate jet_ prototypes for
tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in,
jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and
use a shell script to generate a unified jemalloc.h at configure time.
Change the default private namespace prefix from "" to "je_".
Add missing private namespace mangling.
Remove hard-coded private_namespace.h. Instead generate it and
private_unnamespace.h from private_symbols.txt. Use similar logic for
public symbols, which aids in name mangling for jet_ symbols.
Add test_warn() and test_fail(). Replace existing exit(1) calls with
test_fail() calls.
2013-12-01 07:25:42 +08:00
|
|
|
#define THREAD_ALLOCATED_INITIALIZER JEMALLOC_ARG_CONCAT({0, 0})
|
2011-02-14 10:11:54 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#undef JEMALLOC_H_STRUCTS
|
|
|
|
/******************************************************************************/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_H_EXTERNS
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
extern bool opt_abort;
|
|
|
|
extern bool opt_junk;
|
2012-04-06 15:35:09 +08:00
|
|
|
extern size_t opt_quarantine;
|
|
|
|
extern bool opt_redzone;
|
2012-04-06 04:36:17 +08:00
|
|
|
extern bool opt_utrace;
|
2010-01-17 01:53:50 +08:00
|
|
|
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
|
|
|
|
2014-04-16 05:33:50 +08:00
|
|
|
extern bool in_valgrind;
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Number of CPUs. */
|
|
|
|
extern unsigned ncpus;
|
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
/* Protects arenas initialization (arenas, arenas_total). */
|
|
|
|
extern malloc_mutex_t arenas_lock;
|
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.
|
2012-10-12 04:53:15 +08:00
|
|
|
*
|
|
|
|
* arenas[0..narenas_auto) are used for automatic multiplexing of threads and
|
|
|
|
* arenas. arenas[narenas_auto..narenas_total) are only used if the application
|
|
|
|
* takes some action to create them and allocate from them.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
|
|
|
extern arena_t **arenas;
|
2012-10-12 04:53:15 +08:00
|
|
|
extern unsigned narenas_total;
|
|
|
|
extern unsigned narenas_auto; /* Read-only after initialization. */
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
arena_t *arenas_extend(unsigned ind);
|
2012-03-22 09:33:03 +08:00
|
|
|
void arenas_cleanup(void *arg);
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_t *choose_arena_hard(void);
|
2010-09-06 01:35:13 +08:00
|
|
|
void jemalloc_prefork(void);
|
2012-03-14 07:31:41 +08:00
|
|
|
void jemalloc_postfork_parent(void);
|
|
|
|
void jemalloc_postfork_child(void);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
#include "jemalloc/internal/valgrind.h"
|
2012-03-07 06:57:45 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
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"
|
2012-03-22 09:33:03 +08:00
|
|
|
#include "jemalloc/internal/tsd.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"
|
2012-04-06 15:35:09 +08:00
|
|
|
#include "jemalloc/internal/quarantine.h"
|
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
|
|
|
|
/******************************************************************************/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_H_INLINES
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
#include "jemalloc/internal/valgrind.h"
|
2012-03-07 06:57:45 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
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"
|
2012-03-22 09:33:03 +08:00
|
|
|
#include "jemalloc/internal/tsd.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
|
|
|
|
2014-04-17 08:14:33 +08:00
|
|
|
/*
|
|
|
|
* Include arena.h the first time in order to provide inline functions for this
|
|
|
|
* header's inlines.
|
|
|
|
*/
|
|
|
|
#define JEMALLOC_ARENA_INLINE_A
|
|
|
|
#include "jemalloc/internal/arena.h"
|
|
|
|
#undef JEMALLOC_ARENA_INLINE_A
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_tsd_protos(JEMALLOC_ATTR(unused), arenas, arena_t *)
|
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
size_t s2u(size_t size);
|
2012-04-12 09:13:45 +08:00
|
|
|
size_t sa2u(size_t size, size_t alignment);
|
2012-10-12 04:53:15 +08:00
|
|
|
unsigned narenas_total_get(void);
|
2012-04-04 00:28:00 +08:00
|
|
|
arena_t *choose_arena(arena_t *arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
|
2012-03-22 09:33:03 +08:00
|
|
|
/*
|
|
|
|
* Map of pthread_self() --> arenas[???], used for selecting an arena to use
|
|
|
|
* for allocations.
|
|
|
|
*/
|
|
|
|
malloc_tsd_externs(arenas, arena_t *)
|
2013-01-23 00:45:43 +08:00
|
|
|
malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, arenas, arena_t *, NULL,
|
|
|
|
arenas_cleanup)
|
2012-03-22 09:33:03 +08:00
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
/*
|
|
|
|
* Compute usable size that would result from allocating an object with the
|
|
|
|
* specified size.
|
|
|
|
*/
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2010-10-21 08:39:18 +08:00
|
|
|
s2u(size_t size)
|
|
|
|
{
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
if (size <= SMALL_MAXCLASS)
|
2014-04-17 08:14:33 +08:00
|
|
|
return (small_bin2size(small_size2bin(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.
|
|
|
|
*/
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2012-04-12 09:13:45 +08:00
|
|
|
sa2u(size_t size, size_t alignment)
|
2010-10-21 08:39:18 +08:00
|
|
|
{
|
|
|
|
size_t usize;
|
|
|
|
|
2012-04-12 09:13:45 +08:00
|
|
|
assert(alignment != 0 && ((alignment - 1) & alignment) == 0);
|
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-12 09:13:45 +08:00
|
|
|
usize = ALIGNMENT_CEILING(size, alignment);
|
2010-10-21 08:39:18 +08:00
|
|
|
/*
|
|
|
|
* (usize < size) protects against the combination of maximal
|
|
|
|
* alignment and size greater than maximal alignment.
|
|
|
|
*/
|
|
|
|
if (usize < size) {
|
|
|
|
/* size_t overflow. */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-04-02 22:04:34 +08:00
|
|
|
if (usize <= arena_maxclass && alignment <= PAGE) {
|
2012-02-29 08:50:47 +08:00
|
|
|
if (usize <= SMALL_MAXCLASS)
|
2014-04-17 08:14:33 +08:00
|
|
|
return (small_bin2size(small_size2bin(usize)));
|
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
|
2012-04-02 22:04:34 +08:00
|
|
|
* PAGE of SIZE_T_MAX.
|
2010-10-21 08:39:18 +08:00
|
|
|
*
|
|
|
|
* (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.
|
2012-04-12 09:13:45 +08:00
|
|
|
* If the run wouldn't fit within a chunk, round up to a huge
|
|
|
|
* allocation size.
|
2010-10-21 08:39:18 +08:00
|
|
|
*/
|
2012-04-12 09:13:45 +08:00
|
|
|
run_size = usize + alignment - PAGE;
|
2010-10-21 08:39:18 +08:00
|
|
|
if (run_size <= arena_maxclass)
|
|
|
|
return (PAGE_CEILING(usize));
|
|
|
|
return (CHUNK_CEILING(usize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
|
|
|
narenas_total_get(void)
|
|
|
|
{
|
|
|
|
unsigned narenas;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&arenas_lock);
|
|
|
|
narenas = narenas_total;
|
|
|
|
malloc_mutex_unlock(&arenas_lock);
|
|
|
|
|
|
|
|
return (narenas);
|
|
|
|
}
|
|
|
|
|
2012-03-14 02:09:23 +08:00
|
|
|
/* Choose an arena based on a per-thread value. */
|
2010-01-17 01:53:50 +08:00
|
|
|
JEMALLOC_INLINE arena_t *
|
2012-04-04 00:28:00 +08:00
|
|
|
choose_arena(arena_t *arena)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
arena_t *ret;
|
|
|
|
|
2012-04-04 00:28:00 +08:00
|
|
|
if (arena != NULL)
|
|
|
|
return (arena);
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
if ((ret = *arenas_tsd_get()) == NULL) {
|
2010-01-17 01:53:50 +08:00
|
|
|
ret = choose_arena_hard();
|
|
|
|
assert(ret != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2011-02-14 10:11:54 +08:00
|
|
|
#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"
|
2012-05-02 15:30:36 +08:00
|
|
|
/*
|
2014-04-17 08:14:33 +08:00
|
|
|
* Include arena.h the second and third times in order to resolve circular
|
|
|
|
* dependencies with tcache.h.
|
2012-05-02 15:30:36 +08:00
|
|
|
*/
|
|
|
|
#define JEMALLOC_ARENA_INLINE_B
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/arena.h"
|
2012-05-02 15:30:36 +08:00
|
|
|
#undef JEMALLOC_ARENA_INLINE_B
|
2014-04-17 08:14:33 +08:00
|
|
|
#include "jemalloc/internal/tcache.h"
|
|
|
|
#define JEMALLOC_ARENA_INLINE_C
|
|
|
|
#include "jemalloc/internal/arena.h"
|
|
|
|
#undef JEMALLOC_ARENA_INLINE_C
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/hash.h"
|
2012-04-06 15:35:09 +08:00
|
|
|
#include "jemalloc/internal/quarantine.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void *imalloct(size_t size, bool try_tcache, arena_t *arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
void *imalloc(size_t size);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void *icalloct(size_t size, bool try_tcache, arena_t *arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
void *icalloc(size_t size);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void *ipalloct(size_t usize, size_t alignment, bool zero, bool try_tcache,
|
2012-10-12 04:53:15 +08:00
|
|
|
arena_t *arena);
|
2011-03-23 15:37:29 +08:00
|
|
|
void *ipalloc(size_t usize, size_t alignment, bool zero);
|
2012-04-06 15:35:09 +08:00
|
|
|
size_t isalloc(const void *ptr, bool demote);
|
|
|
|
size_t ivsalloc(const void *ptr, bool demote);
|
|
|
|
size_t u2rz(size_t usize);
|
|
|
|
size_t p2rz(const void *ptr);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void idalloct(void *ptr, bool try_tcache);
|
2010-09-21 10:20:48 +08:00
|
|
|
void idalloc(void *ptr);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void iqalloct(void *ptr, bool try_tcache);
|
2012-04-06 15:35:09 +08:00
|
|
|
void iqalloc(void *ptr);
|
2014-01-13 07:05:44 +08:00
|
|
|
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,
|
2012-10-12 04:53:15 +08:00
|
|
|
arena_t *arena);
|
2014-01-13 07:05:44 +08:00
|
|
|
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);
|
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,
|
2014-01-13 07:05:44 +08:00
|
|
|
bool zero);
|
|
|
|
bool ixalloc(void *ptr, size_t size, size_t extra, size_t alignment,
|
|
|
|
bool zero);
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_tsd_protos(JEMALLOC_ATTR(unused), thread_allocated, thread_allocated_t)
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
imalloct(size_t size, bool try_tcache, arena_t *arena)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
|
|
|
|
if (size <= arena_maxclass)
|
2012-10-12 04:53:15 +08:00
|
|
|
return (arena_malloc(arena, size, false, try_tcache));
|
2010-01-17 01:53:50 +08:00
|
|
|
else
|
2014-05-06 06:16:56 +08:00
|
|
|
return (huge_malloc(arena, size, false,
|
|
|
|
huge_dss_prec_get(arena)));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2012-10-12 04:53:15 +08:00
|
|
|
imalloc(size_t size)
|
|
|
|
{
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
return (imalloct(size, true, NULL));
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
icalloct(size_t size, bool try_tcache, arena_t *arena)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (size <= arena_maxclass)
|
2012-10-12 04:53:15 +08:00
|
|
|
return (arena_malloc(arena, size, true, try_tcache));
|
2010-01-17 01:53:50 +08:00
|
|
|
else
|
2014-05-06 06:16:56 +08:00
|
|
|
return (huge_malloc(arena, size, true,
|
|
|
|
huge_dss_prec_get(arena)));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2012-10-12 04:53:15 +08:00
|
|
|
icalloc(size_t size)
|
|
|
|
{
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
return (icalloct(size, true, NULL));
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
ipalloct(size_t usize, size_t alignment, bool zero, bool try_tcache,
|
2012-10-12 04:53:15 +08:00
|
|
|
arena_t *arena)
|
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);
|
2012-04-12 09:13:45 +08:00
|
|
|
assert(usize == sa2u(usize, alignment));
|
2011-03-23 15:37:29 +08:00
|
|
|
|
2012-04-02 22:04:34 +08:00
|
|
|
if (usize <= arena_maxclass && alignment <= PAGE)
|
2012-10-12 04:53:15 +08:00
|
|
|
ret = arena_malloc(arena, usize, zero, try_tcache);
|
2011-03-23 15:37:29 +08:00
|
|
|
else {
|
2012-04-12 09:13:45 +08:00
|
|
|
if (usize <= arena_maxclass) {
|
2012-10-12 04:53:15 +08:00
|
|
|
ret = arena_palloc(choose_arena(arena), usize,
|
|
|
|
alignment, zero);
|
2011-03-23 15:37:29 +08:00
|
|
|
} else if (alignment <= chunksize)
|
2014-05-06 06:16:56 +08:00
|
|
|
ret = huge_malloc(arena, usize, zero,
|
|
|
|
huge_dss_prec_get(arena));
|
2011-03-23 15:37:29 +08:00
|
|
|
else
|
2014-05-06 06:16:56 +08:00
|
|
|
ret = huge_palloc(arena, usize, alignment, zero,
|
|
|
|
huge_dss_prec_get(arena));
|
2011-03-23 15:37:29 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-04-12 09:13:45 +08:00
|
|
|
assert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
return (ret);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2012-10-12 04:53:15 +08:00
|
|
|
ipalloc(size_t usize, size_t alignment, bool zero)
|
|
|
|
{
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
return (ipalloct(usize, alignment, zero, true, NULL));
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
/*
|
|
|
|
* Typical usage:
|
|
|
|
* void *ptr = [...]
|
|
|
|
* size_t sz = isalloc(ptr, config_prof);
|
|
|
|
*/
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2012-04-06 15:35:09 +08:00
|
|
|
isalloc(const void *ptr, bool demote)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
2012-04-03 06:18:24 +08:00
|
|
|
assert(ptr != NULL);
|
2012-04-06 15:35:09 +08:00
|
|
|
/* Demotion only makes sense if config_prof is true. */
|
|
|
|
assert(config_prof || demote == false);
|
2012-04-03 06:18:24 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
2012-04-20 09:28:03 +08:00
|
|
|
if (chunk != ptr)
|
2012-04-06 15:35:09 +08:00
|
|
|
ret = arena_salloc(ptr, demote);
|
2012-04-20 09:28:03 +08:00
|
|
|
else
|
2010-01-17 01:53:50 +08:00
|
|
|
ret = huge_salloc(ptr);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2012-04-06 15:35:09 +08:00
|
|
|
ivsalloc(const void *ptr, bool demote)
|
2010-09-06 01:35:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Return 0 if ptr is not within a chunk managed by jemalloc. */
|
2014-01-03 09:36:38 +08:00
|
|
|
if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == 0)
|
2010-09-06 01:35:13 +08:00
|
|
|
return (0);
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
return (isalloc(ptr, demote));
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
u2rz(size_t usize)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
if (usize <= SMALL_MAXCLASS) {
|
2014-04-17 08:14:33 +08:00
|
|
|
size_t binind = small_size2bin(usize);
|
2012-04-06 15:35:09 +08:00
|
|
|
ret = arena_bin_info[binind].redzone_size;
|
|
|
|
} else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
p2rz(const void *ptr)
|
|
|
|
{
|
|
|
|
size_t usize = isalloc(ptr, false);
|
|
|
|
|
|
|
|
return (u2rz(usize));
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
idalloct(void *ptr, bool try_tcache)
|
2010-09-21 10:20:48 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
2012-04-03 06:18:24 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
|
2010-09-21 10:20:48 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk != ptr)
|
2014-04-06 06:59:08 +08:00
|
|
|
arena_dalloc(chunk, ptr, try_tcache);
|
2012-04-03 06:18:24 +08:00
|
|
|
else
|
2010-12-01 08:50:58 +08:00
|
|
|
huge_dalloc(ptr, true);
|
2010-09-21 10:20:48 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2012-10-12 04:53:15 +08:00
|
|
|
idalloc(void *ptr)
|
|
|
|
{
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
idalloct(ptr, true);
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
iqalloct(void *ptr, bool try_tcache)
|
2012-04-06 15:35:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (config_fill && opt_quarantine)
|
|
|
|
quarantine(ptr);
|
|
|
|
else
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
idalloct(ptr, try_tcache);
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2012-10-12 04:53:15 +08:00
|
|
|
iqalloc(void *ptr)
|
|
|
|
{
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
iqalloct(ptr, true);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
|
|
|
|
2014-01-13 07:05:44 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
size_t usize, copysize;
|
|
|
|
|
|
|
|
usize = sa2u(size + extra, alignment);
|
|
|
|
if (usize == 0)
|
|
|
|
return (NULL);
|
|
|
|
p = ipalloct(usize, alignment, zero, try_tcache_alloc, arena);
|
|
|
|
if (p == NULL) {
|
|
|
|
if (extra == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Try again, without extra this time. */
|
|
|
|
usize = sa2u(size, alignment);
|
|
|
|
if (usize == 0)
|
|
|
|
return (NULL);
|
|
|
|
p = ipalloct(usize, alignment, zero, try_tcache_alloc, arena);
|
|
|
|
if (p == 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(p, ptr, copysize);
|
|
|
|
iqalloct(ptr, try_tcache_dalloc);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
iralloct(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
|
2014-01-13 07:05:44 +08:00
|
|
|
bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
size_t oldsize;
|
|
|
|
|
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size != 0);
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
oldsize = isalloc(ptr, config_prof);
|
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
|
|
|
if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
|
|
|
|
!= 0) {
|
|
|
|
/*
|
2012-04-06 15:35:09 +08:00
|
|
|
* Existing object alignment is inadequate; allocate new space
|
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
|
|
|
* and copy.
|
|
|
|
*/
|
2014-01-13 07:05:44 +08:00
|
|
|
return (iralloct_realign(ptr, oldsize, size, extra, alignment,
|
|
|
|
zero, try_tcache_alloc, try_tcache_dalloc, arena));
|
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
|
|
|
}
|
|
|
|
|
2014-01-13 07:05:44 +08:00
|
|
|
if (size + extra <= arena_maxclass) {
|
|
|
|
return (arena_ralloc(arena, ptr, oldsize, size, extra,
|
|
|
|
alignment, zero, try_tcache_alloc,
|
|
|
|
try_tcache_dalloc));
|
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
|
|
|
} else {
|
2014-05-06 06:16:56 +08:00
|
|
|
return (huge_ralloc(arena, ptr, oldsize, size, extra,
|
2014-03-28 05:46:00 +08:00
|
|
|
alignment, zero, try_tcache_dalloc, huge_dss_prec_get(arena)));
|
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
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2012-03-22 09:33:03 +08:00
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2014-01-13 07:05:44 +08:00
|
|
|
iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (iralloct(ptr, size, extra, alignment, zero, true, true, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
ixalloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero)
|
2012-10-12 04:53:15 +08:00
|
|
|
{
|
2014-01-13 07:05:44 +08:00
|
|
|
size_t oldsize;
|
2012-10-12 04:53:15 +08:00
|
|
|
|
2014-01-13 07:05:44 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size != 0);
|
|
|
|
|
|
|
|
oldsize = isalloc(ptr, config_prof);
|
|
|
|
if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
|
|
|
|
!= 0) {
|
|
|
|
/* Existing object alignment is inadequate. */
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size <= arena_maxclass)
|
|
|
|
return (arena_ralloc_no_move(ptr, oldsize, size, extra, zero));
|
|
|
|
else
|
|
|
|
return (huge_ralloc_no_move(ptr, oldsize, size, extra));
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_tsd_externs(thread_allocated, thread_allocated_t)
|
2013-01-23 00:45:43 +08:00
|
|
|
malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, thread_allocated, thread_allocated_t,
|
2012-03-22 09:33:03 +08:00
|
|
|
THREAD_ALLOCATED_INITIALIZER, malloc_tsd_no_cleanup)
|
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
|
|
|
|
/******************************************************************************/
|
2012-04-16 22:30:26 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_H */
|