2012-03-07 06:57:45 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_TYPES
|
|
|
|
|
2015-07-24 04:56:25 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# ifdef _WIN64
|
|
|
|
# define FMT64_PREFIX "ll"
|
|
|
|
# define FMTPTR_PREFIX "ll"
|
|
|
|
# else
|
|
|
|
# define FMT64_PREFIX "ll"
|
|
|
|
# define FMTPTR_PREFIX ""
|
|
|
|
# endif
|
|
|
|
# define FMTd32 "d"
|
|
|
|
# define FMTu32 "u"
|
|
|
|
# define FMTx32 "x"
|
|
|
|
# define FMTd64 FMT64_PREFIX "d"
|
|
|
|
# define FMTu64 FMT64_PREFIX "u"
|
|
|
|
# define FMTx64 FMT64_PREFIX "x"
|
|
|
|
# define FMTdPTR FMTPTR_PREFIX "d"
|
|
|
|
# define FMTuPTR FMTPTR_PREFIX "u"
|
|
|
|
# define FMTxPTR FMTPTR_PREFIX "x"
|
|
|
|
#else
|
|
|
|
# include <inttypes.h>
|
|
|
|
# define FMTd32 PRId32
|
|
|
|
# define FMTu32 PRIu32
|
|
|
|
# define FMTx32 PRIx32
|
|
|
|
# define FMTd64 PRId64
|
|
|
|
# define FMTu64 PRIu64
|
|
|
|
# define FMTx64 PRIx64
|
|
|
|
# define FMTdPTR PRIdPTR
|
|
|
|
# define FMTuPTR PRIuPTR
|
|
|
|
# define FMTxPTR PRIxPTR
|
|
|
|
#endif
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
/* Size of stack-allocated buffer passed to buferror(). */
|
|
|
|
#define BUFERROR_BUF 64
|
|
|
|
|
2012-03-14 04:19:04 +08:00
|
|
|
/*
|
2012-03-22 09:33:03 +08:00
|
|
|
* Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
|
|
|
|
* large enough for all possible uses within jemalloc.
|
2012-03-14 04:19:04 +08:00
|
|
|
*/
|
|
|
|
#define MALLOC_PRINTF_BUFSIZE 4096
|
|
|
|
|
2016-03-28 14:28:39 +08:00
|
|
|
/* Junk fill patterns. */
|
2016-11-23 02:58:58 +08:00
|
|
|
#ifndef JEMALLOC_ALLOC_JUNK
|
|
|
|
# define JEMALLOC_ALLOC_JUNK ((uint8_t)0xa5)
|
|
|
|
#endif
|
|
|
|
#ifndef JEMALLOC_FREE_JUNK
|
|
|
|
# define JEMALLOC_FREE_JUNK ((uint8_t)0x5a)
|
|
|
|
#endif
|
2016-03-28 14:28:39 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
/*
|
|
|
|
* Wrap a cpp argument that contains commas such that it isn't broken up into
|
|
|
|
* multiple arguments.
|
|
|
|
*/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__
|
2012-03-22 09:33:03 +08:00
|
|
|
|
2012-03-24 06:39:07 +08:00
|
|
|
/*
|
|
|
|
* Silence compiler warnings due to uninitialized values. This is used
|
|
|
|
* wherever the compiler fails to recognize that the variable is never used
|
|
|
|
* uninitialized.
|
|
|
|
*/
|
|
|
|
#ifdef JEMALLOC_CC_SILENCE
|
2015-03-22 02:30:02 +08:00
|
|
|
# define JEMALLOC_CC_SILENCE_INIT(v) = v
|
2012-03-24 06:39:07 +08:00
|
|
|
#else
|
2015-03-22 02:30:02 +08:00
|
|
|
# define JEMALLOC_CC_SILENCE_INIT(v)
|
2012-03-24 06:39:07 +08:00
|
|
|
#endif
|
|
|
|
|
2014-09-09 10:18:49 +08:00
|
|
|
#ifdef __GNUC__
|
2015-03-22 02:30:02 +08:00
|
|
|
# define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
# define unlikely(x) __builtin_expect(!!(x), 0)
|
2014-09-09 10:18:49 +08:00
|
|
|
#else
|
2015-03-22 02:30:02 +08:00
|
|
|
# define likely(x) !!(x)
|
|
|
|
# define unlikely(x) !!(x)
|
2014-09-09 10:18:49 +08:00
|
|
|
#endif
|
|
|
|
|
2016-06-18 04:28:39 +08:00
|
|
|
#if !defined(JEMALLOC_INTERNAL_UNREACHABLE)
|
|
|
|
# error JEMALLOC_INTERNAL_UNREACHABLE should have been defined by configure
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define unreachable() JEMALLOC_INTERNAL_UNREACHABLE()
|
|
|
|
|
2015-11-13 03:06:41 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2012-03-07 06:57:45 +08:00
|
|
|
|
2013-10-22 05:56:27 +08:00
|
|
|
/* Use to assert a particular configuration, e.g., cassert(config_debug). */
|
|
|
|
#define cassert(c) do { \
|
2014-09-10 03:26:05 +08:00
|
|
|
if (unlikely(!(c))) \
|
2013-10-22 05:56:27 +08:00
|
|
|
not_reached(); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif /* JEMALLOC_H_TYPES */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_STRUCTS
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_STRUCTS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_EXTERNS
|
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
int buferror(int err, char *buf, size_t buflen);
|
2014-01-07 12:33:48 +08:00
|
|
|
uintmax_t malloc_strtoumax(const char *restrict nptr,
|
|
|
|
char **restrict endptr, int base);
|
2012-05-02 17:08:03 +08:00
|
|
|
void malloc_write(const char *s);
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
|
|
|
|
* point math.
|
|
|
|
*/
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t malloc_vsnprintf(char *str, size_t size, const char *format,
|
2012-03-07 06:57:45 +08:00
|
|
|
va_list ap);
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(3, 4);
|
2012-03-07 06:57:45 +08:00
|
|
|
void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
|
|
|
|
const char *format, va_list ap);
|
|
|
|
void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
|
2015-07-23 06:44:47 +08:00
|
|
|
const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4);
|
|
|
|
void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_EXTERNS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_INLINES
|
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
2016-02-25 02:32:45 +08:00
|
|
|
unsigned ffs_llu(unsigned long long bitmap);
|
|
|
|
unsigned ffs_lu(unsigned long bitmap);
|
|
|
|
unsigned ffs_u(unsigned bitmap);
|
|
|
|
unsigned ffs_zu(size_t bitmap);
|
|
|
|
unsigned ffs_u64(uint64_t bitmap);
|
|
|
|
unsigned ffs_u32(uint32_t bitmap);
|
2016-02-10 08:28:40 +08:00
|
|
|
uint64_t pow2_ceil_u64(uint64_t x);
|
|
|
|
uint32_t pow2_ceil_u32(uint32_t x);
|
|
|
|
size_t pow2_ceil_zu(size_t x);
|
2016-02-25 03:04:51 +08:00
|
|
|
unsigned lg_floor(size_t x);
|
2012-04-30 18:38:26 +08:00
|
|
|
void set_errno(int errnum);
|
|
|
|
int get_errno(void);
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
|
2014-05-29 10:37:02 +08:00
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Sanity check. */
|
2016-02-21 15:41:33 +08:00
|
|
|
#if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \
|
|
|
|
|| !defined(JEMALLOC_INTERNAL_FFS)
|
|
|
|
# error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure
|
2014-05-29 10:37:02 +08:00
|
|
|
#endif
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_llu(unsigned long long bitmap)
|
2016-02-21 15:41:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
return (JEMALLOC_INTERNAL_FFSLL(bitmap));
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_lu(unsigned long bitmap)
|
2014-05-29 10:37:02 +08:00
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFSL(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_u(unsigned bitmap)
|
2014-05-29 10:37:02 +08:00
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFS(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_zu(size_t bitmap)
|
|
|
|
{
|
|
|
|
|
2016-02-25 06:01:47 +08:00
|
|
|
#if LG_SIZEOF_PTR == LG_SIZEOF_INT
|
2016-02-25 02:32:45 +08:00
|
|
|
return (ffs_u(bitmap));
|
2016-02-25 06:01:47 +08:00
|
|
|
#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG
|
|
|
|
return (ffs_lu(bitmap));
|
|
|
|
#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG
|
|
|
|
return (ffs_llu(bitmap));
|
2016-02-25 02:32:45 +08:00
|
|
|
#else
|
|
|
|
#error No implementation for size_t ffs()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_u64(uint64_t bitmap)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if LG_SIZEOF_LONG == 3
|
|
|
|
return (ffs_lu(bitmap));
|
|
|
|
#elif LG_SIZEOF_LONG_LONG == 3
|
|
|
|
return (ffs_llu(bitmap));
|
|
|
|
#else
|
|
|
|
#error No implementation for 64-bit ffs()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
|
|
|
ffs_u32(uint32_t bitmap)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if LG_SIZEOF_INT == 2
|
|
|
|
return (ffs_u(bitmap));
|
|
|
|
#else
|
|
|
|
#error No implementation for 32-bit ffs()
|
|
|
|
#endif
|
|
|
|
return (ffs_u(bitmap));
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
JEMALLOC_INLINE uint64_t
|
|
|
|
pow2_ceil_u64(uint64_t x)
|
2012-03-07 06:57:45 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
x--;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
x |= x >> 32;
|
|
|
|
x++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
JEMALLOC_INLINE uint32_t
|
|
|
|
pow2_ceil_u32(uint32_t x)
|
|
|
|
{
|
|
|
|
|
|
|
|
x--;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
x++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the smallest power of 2 that is >= x. */
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
pow2_ceil_zu(size_t x)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
|
|
|
return (pow2_ceil_u64(x));
|
|
|
|
#else
|
|
|
|
return (pow2_ceil_u32(x));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
#if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2014-05-29 07:11:55 +08:00
|
|
|
lg_floor(size_t x)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
asm ("bsr %1, %0"
|
|
|
|
: "=r"(ret) // Outputs.
|
|
|
|
: "r"(x) // Inputs.
|
|
|
|
);
|
2016-02-25 03:04:51 +08:00
|
|
|
assert(ret < UINT_MAX);
|
|
|
|
return ((unsigned)ret);
|
2014-05-29 07:11:55 +08:00
|
|
|
}
|
2014-09-22 22:54:33 +08:00
|
|
|
#elif (defined(_MSC_VER))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2014-09-22 22:54:33 +08:00
|
|
|
lg_floor(size_t x)
|
|
|
|
{
|
2015-02-05 08:41:55 +08:00
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
assert(x != 0);
|
2014-09-22 22:54:33 +08:00
|
|
|
|
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
2015-02-05 08:41:55 +08:00
|
|
|
_BitScanReverse64(&ret, x);
|
2014-09-22 22:54:33 +08:00
|
|
|
#elif (LG_SIZEOF_PTR == 2)
|
2015-02-05 08:41:55 +08:00
|
|
|
_BitScanReverse(&ret, x);
|
2014-09-22 22:54:33 +08:00
|
|
|
#else
|
2016-02-25 02:32:45 +08:00
|
|
|
# error "Unsupported type size for lg_floor()"
|
2014-09-22 22:54:33 +08:00
|
|
|
#endif
|
2016-02-25 03:04:51 +08:00
|
|
|
assert(ret < UINT_MAX);
|
|
|
|
return ((unsigned)ret);
|
2014-09-22 22:54:33 +08:00
|
|
|
}
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2014-05-29 07:11:55 +08:00
|
|
|
lg_floor(size_t x)
|
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
#if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
|
2014-06-02 13:05:08 +08:00
|
|
|
return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x));
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
|
2014-06-02 13:05:08 +08:00
|
|
|
return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x));
|
2014-05-29 07:11:55 +08:00
|
|
|
#else
|
2016-02-25 02:32:45 +08:00
|
|
|
# error "Unsupported type size for lg_floor()"
|
2014-05-29 07:11:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2014-05-29 07:11:55 +08:00
|
|
|
lg_floor(size_t x)
|
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-06-02 13:05:08 +08:00
|
|
|
x |= (x >> 1);
|
|
|
|
x |= (x >> 2);
|
|
|
|
x |= (x >> 4);
|
|
|
|
x |= (x >> 8);
|
|
|
|
x |= (x >> 16);
|
2016-02-25 02:32:45 +08:00
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
2014-06-02 13:05:08 +08:00
|
|
|
x |= (x >> 32);
|
2014-05-29 07:11:55 +08:00
|
|
|
#endif
|
2016-02-25 02:32:45 +08:00
|
|
|
if (x == SIZE_T_MAX)
|
|
|
|
return ((8 << LG_SIZEOF_PTR) - 1);
|
|
|
|
x++;
|
|
|
|
return (ffs_zu(x) - 2);
|
2014-05-29 07:11:55 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Set error code. */
|
2012-04-30 18:38:26 +08:00
|
|
|
JEMALLOC_INLINE void
|
|
|
|
set_errno(int errnum)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetLastError(errnum);
|
|
|
|
#else
|
|
|
|
errno = errnum;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Get last error code. */
|
2012-04-30 18:38:26 +08:00
|
|
|
JEMALLOC_INLINE int
|
|
|
|
get_errno(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-04-30 18:38:27 +08:00
|
|
|
return (GetLastError());
|
2012-04-30 18:38:26 +08:00
|
|
|
#else
|
2012-04-30 18:38:27 +08:00
|
|
|
return (errno);
|
2012-04-30 18:38:26 +08:00
|
|
|
#endif
|
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_INLINES */
|
|
|
|
/******************************************************************************/
|