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
|
|
|
|
|
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
|
|
|
|
|
2015-08-13 07:38:39 +08:00
|
|
|
#define JEMALLOC_GNUC_PREREQ(major, minor) \
|
|
|
|
(!defined(__clang__) && \
|
|
|
|
(__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))))
|
2015-08-13 07:46:09 +08:00
|
|
|
#ifndef __has_builtin
|
|
|
|
# define __has_builtin(builtin) (0)
|
|
|
|
#endif
|
2015-08-13 07:38:39 +08:00
|
|
|
#define JEMALLOC_CLANG_HAS_BUILTIN(builtin) \
|
|
|
|
(defined(__clang__) && __has_builtin(builtin))
|
|
|
|
|
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)
|
2015-08-13 07:38:39 +08:00
|
|
|
# if JEMALLOC_GNUC_PREREQ(4, 6) || \
|
|
|
|
JEMALLOC_CLANG_HAS_BUILTIN(__builtin_unreachable)
|
2015-08-02 03:06:12 +08:00
|
|
|
# define unreachable() __builtin_unreachable()
|
2015-08-13 07:38:39 +08:00
|
|
|
# else
|
|
|
|
# define unreachable()
|
|
|
|
# endif
|
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)
|
2015-08-02 03:06:12 +08:00
|
|
|
# define unreachable()
|
2014-09-09 10:18:49 +08:00
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int malloc_vsnprintf(char *str, size_t size, const char *format,
|
|
|
|
va_list ap);
|
|
|
|
int 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
|
2014-05-29 10:37:02 +08:00
|
|
|
int jemalloc_ffsl(long bitmap);
|
|
|
|
int jemalloc_ffs(int 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);
|
2014-05-29 07:11:55 +08:00
|
|
|
size_t 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. */
|
2014-05-29 10:37:02 +08:00
|
|
|
#if !defined(JEMALLOC_INTERNAL_FFSL) || !defined(JEMALLOC_INTERNAL_FFS)
|
|
|
|
# error Both JEMALLOC_INTERNAL_FFSL && JEMALLOC_INTERNAL_FFS should have been defined by configure
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE int
|
|
|
|
jemalloc_ffsl(long bitmap)
|
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFSL(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE int
|
|
|
|
jemalloc_ffs(int bitmap)
|
|
|
|
{
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFS(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
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__))
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
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.
|
|
|
|
);
|
|
|
|
return (ret);
|
|
|
|
}
|
2014-09-22 22:54:33 +08:00
|
|
|
#elif (defined(_MSC_VER))
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
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
|
|
|
|
# error "Unsupported type sizes for lg_floor()"
|
|
|
|
#endif
|
2015-02-05 08:41:55 +08:00
|
|
|
return (ret);
|
2014-09-22 22:54:33 +08:00
|
|
|
}
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
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
|
|
|
|
# error "Unsupported type sizes for lg_floor()"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
JEMALLOC_INLINE size_t
|
|
|
|
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);
|
2014-05-29 07:11:55 +08:00
|
|
|
#if (LG_SIZEOF_PTR == 3 && LG_SIZEOF_PTR == LG_SIZEOF_LONG)
|
2014-06-02 13:05:08 +08:00
|
|
|
x |= (x >> 32);
|
|
|
|
if (x == KZU(0xffffffffffffffff))
|
|
|
|
return (63);
|
|
|
|
x++;
|
2014-05-29 10:37:02 +08:00
|
|
|
return (jemalloc_ffsl(x) - 2);
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (LG_SIZEOF_PTR == 2)
|
2014-06-02 13:05:08 +08:00
|
|
|
if (x == KZU(0xffffffff))
|
|
|
|
return (31);
|
|
|
|
x++;
|
2014-05-29 10:37:02 +08:00
|
|
|
return (jemalloc_ffs(x) - 2);
|
2014-05-29 07:11:55 +08:00
|
|
|
#else
|
|
|
|
# error "Unsupported type sizes for lg_floor()"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#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 */
|
|
|
|
/******************************************************************************/
|