2017-03-04 02:10:08 +08:00
|
|
|
#define JEMALLOC_MALLOC_IO_C_
|
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
|
|
|
|
|
|
|
#ifdef assert
|
|
|
|
# undef assert
|
|
|
|
#endif
|
|
|
|
#ifdef not_reached
|
|
|
|
# undef not_reached
|
|
|
|
#endif
|
|
|
|
#ifdef not_implemented
|
|
|
|
# undef not_implemented
|
|
|
|
#endif
|
|
|
|
#ifdef assert_not_implemented
|
|
|
|
# undef assert_not_implemented
|
|
|
|
#endif
|
|
|
|
|
2015-11-13 03:06:41 +08:00
|
|
|
/*
|
|
|
|
* Define simple versions of assertion macros that won't recurse in case
|
|
|
|
* of assertion failures in malloc_*printf().
|
|
|
|
*/
|
2017-01-20 13:41:41 +08:00
|
|
|
#define assert(e) do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
if (config_debug && !(e)) { \
|
|
|
|
malloc_write("<jemalloc>: Failed assertion\n"); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define not_reached() do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
if (config_debug) { \
|
|
|
|
malloc_write("<jemalloc>: Unreachable code reached\n"); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
2016-03-23 08:54:35 +08:00
|
|
|
unreachable(); \
|
2012-03-07 06:57:45 +08:00
|
|
|
} while (0)
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define not_implemented() do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
if (config_debug) { \
|
|
|
|
malloc_write("<jemalloc>: Not implemented\n"); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2017-03-04 02:10:08 +08:00
|
|
|
#define assert_not_implemented(e) do { \
|
|
|
|
if (unlikely(config_debug && !(e))) { \
|
|
|
|
not_implemented(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
|
|
|
|
2017-03-04 02:10:08 +08:00
|
|
|
static void wrtmessage(void *cbopaque, const char *s);
|
|
|
|
#define U2S_BUFSIZE ((1U << (LG_SIZEOF_INTMAX_T + 3)) + 1)
|
|
|
|
static char *u2s(uintmax_t x, unsigned base, bool uppercase, char *s,
|
2012-03-07 06:57:45 +08:00
|
|
|
size_t *slen_p);
|
2017-03-04 02:10:08 +08:00
|
|
|
#define D2S_BUFSIZE (1 + U2S_BUFSIZE)
|
|
|
|
static char *d2s(intmax_t x, char sign, char *s, size_t *slen_p);
|
|
|
|
#define O2S_BUFSIZE (1 + U2S_BUFSIZE)
|
|
|
|
static char *o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p);
|
|
|
|
#define X2S_BUFSIZE (2 + U2S_BUFSIZE)
|
|
|
|
static char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,
|
2012-03-07 06:57:45 +08:00
|
|
|
size_t *slen_p);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/* malloc_message() setup. */
|
2012-04-30 18:38:29 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
wrtmessage(void *cbopaque, const char *s) {
|
2016-12-04 08:47:36 +08:00
|
|
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)
|
2012-03-27 20:48:58 +08:00
|
|
|
/*
|
|
|
|
* Use syscall(2) rather than write(2) when possible in order to avoid
|
|
|
|
* the possibility of memory allocation within libc. This is necessary
|
|
|
|
* on FreeBSD; most operating systems do not have this problem though.
|
2016-02-25 05:16:51 +08:00
|
|
|
*
|
|
|
|
* syscall() returns long or int, depending on platform, so capture the
|
|
|
|
* unused result in the widest plausible type to avoid compiler
|
|
|
|
* warnings.
|
2012-03-27 20:48:58 +08:00
|
|
|
*/
|
2016-02-25 05:16:51 +08:00
|
|
|
UNUSED long result = syscall(SYS_write, STDERR_FILENO, s, strlen(s));
|
2012-03-27 20:48:58 +08:00
|
|
|
#else
|
2016-02-26 12:51:00 +08:00
|
|
|
UNUSED ssize_t result = write(STDERR_FILENO, s, strlen(s));
|
2012-03-27 20:48:58 +08:00
|
|
|
#endif
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
2012-05-02 19:15:00 +08:00
|
|
|
JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
|
2012-03-07 06:57:45 +08:00
|
|
|
|
2012-05-02 17:08:03 +08:00
|
|
|
/*
|
|
|
|
* Wrapper around malloc_message() that avoids the need for
|
|
|
|
* je_malloc_message(...) throughout the code.
|
|
|
|
*/
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_write(const char *s) {
|
|
|
|
if (je_malloc_message != NULL) {
|
2012-05-02 19:15:00 +08:00
|
|
|
je_malloc_message(NULL, s);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2012-05-02 19:15:00 +08:00
|
|
|
wrtmessage(NULL, s);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-05-02 17:08:03 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
/*
|
|
|
|
* glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so
|
|
|
|
* provide a wrapper.
|
|
|
|
*/
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
buferror(int err, char *buf, size_t buflen) {
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
2015-03-12 07:51:05 +08:00
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
|
2016-02-26 12:51:00 +08:00
|
|
|
(LPSTR)buf, (DWORD)buflen, NULL);
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2015-02-04 01:58:02 +08:00
|
|
|
#elif defined(__GLIBC__) && defined(_GNU_SOURCE)
|
2013-12-09 12:52:21 +08:00
|
|
|
char *b = strerror_r(err, buf, buflen);
|
2012-03-07 06:57:45 +08:00
|
|
|
if (b != buf) {
|
|
|
|
strncpy(buf, b, buflen);
|
|
|
|
buf[buflen-1] = '\0';
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2012-03-07 06:57:45 +08:00
|
|
|
#else
|
2017-01-20 10:15:45 +08:00
|
|
|
return strerror_r(err, buf, buflen);
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-03 14:04:57 +08:00
|
|
|
uintmax_t
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base) {
|
2012-02-03 14:04:57 +08:00
|
|
|
uintmax_t ret, digit;
|
2014-05-29 10:04:06 +08:00
|
|
|
unsigned b;
|
2012-02-03 14:04:57 +08:00
|
|
|
bool neg;
|
|
|
|
const char *p, *ns;
|
|
|
|
|
2014-01-07 12:33:48 +08:00
|
|
|
p = nptr;
|
2012-02-03 14:04:57 +08:00
|
|
|
if (base < 0 || base == 1 || base > 36) {
|
2014-01-07 12:33:48 +08:00
|
|
|
ns = p;
|
2012-04-30 18:38:26 +08:00
|
|
|
set_errno(EINVAL);
|
2014-01-07 12:33:48 +08:00
|
|
|
ret = UINTMAX_MAX;
|
|
|
|
goto label_return;
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
|
|
|
b = base;
|
|
|
|
|
|
|
|
/* Swallow leading whitespace and get sign, if any. */
|
|
|
|
neg = false;
|
|
|
|
while (true) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\t': case '\n': case '\v': case '\f': case '\r': case ' ':
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
neg = true;
|
|
|
|
/* Fall through. */
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
/* Fall through. */
|
|
|
|
default:
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_prefix;
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get prefix, if any. */
|
2012-04-11 06:07:44 +08:00
|
|
|
label_prefix:
|
2012-02-03 14:04:57 +08:00
|
|
|
/*
|
|
|
|
* Note where the first non-whitespace/sign character is so that it is
|
|
|
|
* possible to tell whether any digits are consumed (e.g., " 0" vs.
|
|
|
|
* " -x").
|
|
|
|
*/
|
|
|
|
ns = p;
|
|
|
|
if (*p == '0') {
|
|
|
|
switch (p[1]) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7':
|
2017-01-16 08:56:30 +08:00
|
|
|
if (b == 0) {
|
2012-02-03 14:04:57 +08:00
|
|
|
b = 8;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (b == 8) {
|
2012-02-03 14:04:57 +08:00
|
|
|
p++;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
break;
|
2014-01-07 12:33:48 +08:00
|
|
|
case 'X': case 'x':
|
2012-02-03 14:04:57 +08:00
|
|
|
switch (p[2]) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
|
|
case 'F':
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
|
|
case 'f':
|
2017-01-16 08:56:30 +08:00
|
|
|
if (b == 0) {
|
2012-02-03 14:04:57 +08:00
|
|
|
b = 16;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (b == 16) {
|
2012-02-03 14:04:57 +08:00
|
|
|
p += 2;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-07 12:33:48 +08:00
|
|
|
p++;
|
|
|
|
ret = 0;
|
|
|
|
goto label_return;
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (b == 0) {
|
2012-02-03 14:04:57 +08:00
|
|
|
b = 10;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
|
|
|
|
/* Convert. */
|
|
|
|
ret = 0;
|
|
|
|
while ((*p >= '0' && *p <= '9' && (digit = *p - '0') < b)
|
|
|
|
|| (*p >= 'A' && *p <= 'Z' && (digit = 10 + *p - 'A') < b)
|
|
|
|
|| (*p >= 'a' && *p <= 'z' && (digit = 10 + *p - 'a') < b)) {
|
|
|
|
uintmax_t pret = ret;
|
|
|
|
ret *= b;
|
|
|
|
ret += digit;
|
|
|
|
if (ret < pret) {
|
|
|
|
/* Overflow. */
|
2012-04-30 18:38:26 +08:00
|
|
|
set_errno(ERANGE);
|
2014-01-07 12:33:48 +08:00
|
|
|
ret = UINTMAX_MAX;
|
|
|
|
goto label_return;
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (neg) {
|
2016-10-28 12:31:25 +08:00
|
|
|
ret = (uintmax_t)(-((intmax_t)ret));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
|
2014-01-07 12:33:48 +08:00
|
|
|
if (p == ns) {
|
|
|
|
/* No conversion performed. */
|
|
|
|
set_errno(EINVAL);
|
|
|
|
ret = UINTMAX_MAX;
|
|
|
|
goto label_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
label_return:
|
2012-02-03 14:04:57 +08:00
|
|
|
if (endptr != NULL) {
|
|
|
|
if (p == ns) {
|
|
|
|
/* No characters were converted. */
|
|
|
|
*endptr = (char *)nptr;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2012-02-03 14:04:57 +08:00
|
|
|
*endptr = (char *)p;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2012-02-03 14:04:57 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
static char *
|
2017-01-16 08:56:30 +08:00
|
|
|
u2s(uintmax_t x, unsigned base, bool uppercase, char *s, size_t *slen_p) {
|
2012-03-07 06:57:45 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
i = U2S_BUFSIZE - 1;
|
|
|
|
s[i] = '\0';
|
|
|
|
switch (base) {
|
|
|
|
case 10:
|
|
|
|
do {
|
|
|
|
i--;
|
|
|
|
s[i] = "0123456789"[x % (uint64_t)10];
|
|
|
|
x /= (uint64_t)10;
|
|
|
|
} while (x > 0);
|
|
|
|
break;
|
|
|
|
case 16: {
|
|
|
|
const char *digits = (uppercase)
|
|
|
|
? "0123456789ABCDEF"
|
|
|
|
: "0123456789abcdef";
|
|
|
|
|
|
|
|
do {
|
|
|
|
i--;
|
|
|
|
s[i] = digits[x & 0xf];
|
|
|
|
x >>= 4;
|
|
|
|
} while (x > 0);
|
|
|
|
break;
|
|
|
|
} default: {
|
|
|
|
const char *digits = (uppercase)
|
|
|
|
? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
: "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
assert(base >= 2 && base <= 36);
|
|
|
|
do {
|
|
|
|
i--;
|
|
|
|
s[i] = digits[x % (uint64_t)base];
|
|
|
|
x /= (uint64_t)base;
|
|
|
|
} while (x > 0);
|
|
|
|
}}
|
|
|
|
|
|
|
|
*slen_p = U2S_BUFSIZE - 1 - i;
|
2017-01-20 10:15:45 +08:00
|
|
|
return &s[i];
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-01-16 08:56:30 +08:00
|
|
|
d2s(intmax_t x, char sign, char *s, size_t *slen_p) {
|
2012-03-07 06:57:45 +08:00
|
|
|
bool neg;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if ((neg = (x < 0))) {
|
2012-03-07 06:57:45 +08:00
|
|
|
x = -x;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
s = u2s(x, 10, false, s, slen_p);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (neg) {
|
2012-03-07 06:57:45 +08:00
|
|
|
sign = '-';
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
switch (sign) {
|
|
|
|
case '-':
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!neg) {
|
2012-03-07 06:57:45 +08:00
|
|
|
break;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
/* Fall through. */
|
|
|
|
case ' ':
|
|
|
|
case '+':
|
|
|
|
s--;
|
|
|
|
(*slen_p)++;
|
|
|
|
*s = sign;
|
|
|
|
break;
|
|
|
|
default: not_reached();
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return s;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-01-16 08:56:30 +08:00
|
|
|
o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p) {
|
2012-03-07 06:57:45 +08:00
|
|
|
s = u2s(x, 8, false, s, slen_p);
|
|
|
|
if (alt_form && *s != '0') {
|
|
|
|
s--;
|
|
|
|
(*slen_p)++;
|
|
|
|
*s = '0';
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return s;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-01-16 08:56:30 +08:00
|
|
|
x2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p) {
|
2012-03-07 06:57:45 +08:00
|
|
|
s = u2s(x, 16, uppercase, s, slen_p);
|
|
|
|
if (alt_form) {
|
|
|
|
s -= 2;
|
|
|
|
(*slen_p) += 2;
|
|
|
|
memcpy(s, uppercase ? "0X" : "0x", 2);
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return s;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
|
2012-03-07 06:57:45 +08:00
|
|
|
size_t i;
|
|
|
|
const char *f;
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define APPEND_C(c) do { \
|
2017-01-16 08:56:30 +08:00
|
|
|
if (i < size) { \
|
2012-03-07 06:57:45 +08:00
|
|
|
str[i] = (c); \
|
2017-01-16 08:56:30 +08:00
|
|
|
} \
|
2012-03-07 06:57:45 +08:00
|
|
|
i++; \
|
|
|
|
} while (0)
|
2017-01-20 13:41:41 +08:00
|
|
|
#define APPEND_S(s, slen) do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
if (i < size) { \
|
|
|
|
size_t cpylen = (slen <= size - i) ? slen : size - i; \
|
|
|
|
memcpy(&str[i], s, cpylen); \
|
|
|
|
} \
|
|
|
|
i += slen; \
|
|
|
|
} while (0)
|
2017-01-20 13:41:41 +08:00
|
|
|
#define APPEND_PADDED_S(s, slen, width, left_justify) do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
/* Left padding. */ \
|
|
|
|
size_t pad_len = (width == -1) ? 0 : ((slen < (size_t)width) ? \
|
|
|
|
(size_t)width - slen : 0); \
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!left_justify && pad_len != 0) { \
|
2012-03-07 06:57:45 +08:00
|
|
|
size_t j; \
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < pad_len; j++) { \
|
2012-03-07 06:57:45 +08:00
|
|
|
APPEND_C(' '); \
|
2017-01-16 08:56:30 +08:00
|
|
|
} \
|
2012-03-07 06:57:45 +08:00
|
|
|
} \
|
|
|
|
/* Value. */ \
|
|
|
|
APPEND_S(s, slen); \
|
|
|
|
/* Right padding. */ \
|
|
|
|
if (left_justify && pad_len != 0) { \
|
|
|
|
size_t j; \
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < pad_len; j++) { \
|
2012-03-07 06:57:45 +08:00
|
|
|
APPEND_C(' '); \
|
2017-01-16 08:56:30 +08:00
|
|
|
} \
|
2012-03-07 06:57:45 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
2017-01-20 13:41:41 +08:00
|
|
|
#define GET_ARG_NUMERIC(val, len) do { \
|
2012-03-07 06:57:45 +08:00
|
|
|
switch (len) { \
|
|
|
|
case '?': \
|
|
|
|
val = va_arg(ap, int); \
|
|
|
|
break; \
|
2012-04-19 00:29:44 +08:00
|
|
|
case '?' | 0x80: \
|
|
|
|
val = va_arg(ap, unsigned int); \
|
|
|
|
break; \
|
2012-03-07 06:57:45 +08:00
|
|
|
case 'l': \
|
|
|
|
val = va_arg(ap, long); \
|
|
|
|
break; \
|
2012-04-19 00:29:44 +08:00
|
|
|
case 'l' | 0x80: \
|
|
|
|
val = va_arg(ap, unsigned long); \
|
|
|
|
break; \
|
2012-03-07 06:57:45 +08:00
|
|
|
case 'q': \
|
|
|
|
val = va_arg(ap, long long); \
|
|
|
|
break; \
|
2012-04-19 00:29:44 +08:00
|
|
|
case 'q' | 0x80: \
|
|
|
|
val = va_arg(ap, unsigned long long); \
|
|
|
|
break; \
|
2012-03-07 06:57:45 +08:00
|
|
|
case 'j': \
|
|
|
|
val = va_arg(ap, intmax_t); \
|
|
|
|
break; \
|
2014-01-07 12:33:48 +08:00
|
|
|
case 'j' | 0x80: \
|
|
|
|
val = va_arg(ap, uintmax_t); \
|
|
|
|
break; \
|
2012-03-07 06:57:45 +08:00
|
|
|
case 't': \
|
|
|
|
val = va_arg(ap, ptrdiff_t); \
|
|
|
|
break; \
|
|
|
|
case 'z': \
|
2012-02-03 14:04:57 +08:00
|
|
|
val = va_arg(ap, ssize_t); \
|
2012-03-07 06:57:45 +08:00
|
|
|
break; \
|
2012-04-19 00:29:44 +08:00
|
|
|
case 'z' | 0x80: \
|
|
|
|
val = va_arg(ap, size_t); \
|
|
|
|
break; \
|
2012-03-22 09:33:03 +08:00
|
|
|
case 'p': /* Synthetic; used for %p. */ \
|
|
|
|
val = va_arg(ap, uintptr_t); \
|
|
|
|
break; \
|
2014-05-29 10:04:33 +08:00
|
|
|
default: \
|
|
|
|
not_reached(); \
|
|
|
|
val = 0; \
|
2012-03-07 06:57:45 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
f = format;
|
|
|
|
while (true) {
|
|
|
|
switch (*f) {
|
2012-04-11 06:07:44 +08:00
|
|
|
case '\0': goto label_out;
|
2012-03-07 06:57:45 +08:00
|
|
|
case '%': {
|
|
|
|
bool alt_form = false;
|
|
|
|
bool left_justify = false;
|
|
|
|
bool plus_space = false;
|
|
|
|
bool plus_plus = false;
|
|
|
|
int prec = -1;
|
|
|
|
int width = -1;
|
2012-04-19 00:29:44 +08:00
|
|
|
unsigned char len = '?';
|
2016-03-26 16:19:28 +08:00
|
|
|
char *s;
|
|
|
|
size_t slen;
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
f++;
|
|
|
|
/* Flags. */
|
|
|
|
while (true) {
|
|
|
|
switch (*f) {
|
|
|
|
case '#':
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!alt_form);
|
2012-03-07 06:57:45 +08:00
|
|
|
alt_form = true;
|
|
|
|
break;
|
|
|
|
case '-':
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!left_justify);
|
2012-03-07 06:57:45 +08:00
|
|
|
left_justify = true;
|
|
|
|
break;
|
|
|
|
case ' ':
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!plus_space);
|
2012-03-07 06:57:45 +08:00
|
|
|
plus_space = true;
|
|
|
|
break;
|
|
|
|
case '+':
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!plus_plus);
|
2012-03-07 06:57:45 +08:00
|
|
|
plus_plus = true;
|
|
|
|
break;
|
2012-04-11 06:07:44 +08:00
|
|
|
default: goto label_width;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
/* Width. */
|
2012-04-11 06:07:44 +08:00
|
|
|
label_width:
|
2012-03-07 06:57:45 +08:00
|
|
|
switch (*f) {
|
|
|
|
case '*':
|
|
|
|
width = va_arg(ap, int);
|
|
|
|
f++;
|
2014-01-07 12:33:48 +08:00
|
|
|
if (width < 0) {
|
|
|
|
left_justify = true;
|
|
|
|
width = -width;
|
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9': {
|
2012-02-03 14:04:57 +08:00
|
|
|
uintmax_t uwidth;
|
2012-04-30 18:38:26 +08:00
|
|
|
set_errno(0);
|
2012-02-03 14:04:57 +08:00
|
|
|
uwidth = malloc_strtoumax(f, (char **)&f, 10);
|
2012-04-30 18:38:26 +08:00
|
|
|
assert(uwidth != UINTMAX_MAX || get_errno() !=
|
2012-02-03 14:04:57 +08:00
|
|
|
ERANGE);
|
2012-03-07 06:57:45 +08:00
|
|
|
width = (int)uwidth;
|
|
|
|
break;
|
2014-01-07 12:33:48 +08:00
|
|
|
} default:
|
|
|
|
break;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
2014-01-07 12:33:48 +08:00
|
|
|
/* Width/precision separator. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (*f == '.') {
|
2014-01-07 12:33:48 +08:00
|
|
|
f++;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-01-07 12:33:48 +08:00
|
|
|
goto label_length;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
/* Precision. */
|
|
|
|
switch (*f) {
|
|
|
|
case '*':
|
|
|
|
prec = va_arg(ap, int);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9': {
|
2012-02-03 14:04:57 +08:00
|
|
|
uintmax_t uprec;
|
2012-04-30 18:38:26 +08:00
|
|
|
set_errno(0);
|
2012-02-03 14:04:57 +08:00
|
|
|
uprec = malloc_strtoumax(f, (char **)&f, 10);
|
2012-04-30 18:38:26 +08:00
|
|
|
assert(uprec != UINTMAX_MAX || get_errno() !=
|
|
|
|
ERANGE);
|
2012-03-07 06:57:45 +08:00
|
|
|
prec = (int)uprec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
/* Length. */
|
2012-04-11 06:07:44 +08:00
|
|
|
label_length:
|
2012-03-07 06:57:45 +08:00
|
|
|
switch (*f) {
|
|
|
|
case 'l':
|
|
|
|
f++;
|
|
|
|
if (*f == 'l') {
|
|
|
|
len = 'q';
|
|
|
|
f++;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2012-03-07 06:57:45 +08:00
|
|
|
len = 'l';
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
break;
|
2014-01-07 12:33:48 +08:00
|
|
|
case 'q': case 'j': case 't': case 'z':
|
|
|
|
len = *f;
|
2012-03-07 06:57:45 +08:00
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
/* Conversion specifier. */
|
|
|
|
switch (*f) {
|
2014-01-23 01:00:27 +08:00
|
|
|
case '%':
|
|
|
|
/* %% */
|
|
|
|
APPEND_C(*f);
|
|
|
|
f++;
|
|
|
|
break;
|
2012-03-07 06:57:45 +08:00
|
|
|
case 'd': case 'i': {
|
2012-03-24 06:39:07 +08:00
|
|
|
intmax_t val JEMALLOC_CC_SILENCE_INIT(0);
|
2012-03-07 06:57:45 +08:00
|
|
|
char buf[D2S_BUFSIZE];
|
|
|
|
|
|
|
|
GET_ARG_NUMERIC(val, len);
|
|
|
|
s = d2s(val, (plus_plus ? '+' : (plus_space ?
|
|
|
|
' ' : '-')), buf, &slen);
|
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
} case 'o': {
|
2012-03-24 06:39:07 +08:00
|
|
|
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
|
2012-03-07 06:57:45 +08:00
|
|
|
char buf[O2S_BUFSIZE];
|
|
|
|
|
2012-04-19 00:29:44 +08:00
|
|
|
GET_ARG_NUMERIC(val, len | 0x80);
|
2012-03-07 06:57:45 +08:00
|
|
|
s = o2s(val, alt_form, buf, &slen);
|
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
} case 'u': {
|
2012-03-24 06:39:07 +08:00
|
|
|
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
|
2012-03-07 06:57:45 +08:00
|
|
|
char buf[U2S_BUFSIZE];
|
|
|
|
|
2012-04-19 00:29:44 +08:00
|
|
|
GET_ARG_NUMERIC(val, len | 0x80);
|
2012-03-07 06:57:45 +08:00
|
|
|
s = u2s(val, 10, false, buf, &slen);
|
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
} case 'x': case 'X': {
|
2012-03-24 06:39:07 +08:00
|
|
|
uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
|
2012-03-07 06:57:45 +08:00
|
|
|
char buf[X2S_BUFSIZE];
|
|
|
|
|
2012-04-19 00:29:44 +08:00
|
|
|
GET_ARG_NUMERIC(val, len | 0x80);
|
2012-03-07 06:57:45 +08:00
|
|
|
s = x2s(val, alt_form, *f == 'X', buf, &slen);
|
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
} case 'c': {
|
|
|
|
unsigned char val;
|
|
|
|
char buf[2];
|
|
|
|
|
|
|
|
assert(len == '?' || len == 'l');
|
|
|
|
assert_not_implemented(len != 'l');
|
|
|
|
val = va_arg(ap, int);
|
|
|
|
buf[0] = val;
|
|
|
|
buf[1] = '\0';
|
|
|
|
APPEND_PADDED_S(buf, 1, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
} case 's':
|
|
|
|
assert(len == '?' || len == 'l');
|
|
|
|
assert_not_implemented(len != 'l');
|
|
|
|
s = va_arg(ap, char *);
|
2014-05-29 10:04:06 +08:00
|
|
|
slen = (prec < 0) ? strlen(s) : (size_t)prec;
|
2012-03-07 06:57:45 +08:00
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case 'p': {
|
|
|
|
uintmax_t val;
|
|
|
|
char buf[X2S_BUFSIZE];
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
GET_ARG_NUMERIC(val, 'p');
|
2012-03-07 06:57:45 +08:00
|
|
|
s = x2s(val, true, false, buf, &slen);
|
|
|
|
APPEND_PADDED_S(s, slen, width, left_justify);
|
|
|
|
f++;
|
|
|
|
break;
|
2014-01-23 01:00:27 +08:00
|
|
|
} default: not_reached();
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} default: {
|
|
|
|
APPEND_C(*f);
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
}}
|
|
|
|
}
|
2012-04-11 06:07:44 +08:00
|
|
|
label_out:
|
2017-01-16 08:56:30 +08:00
|
|
|
if (i < size) {
|
2012-03-07 06:57:45 +08:00
|
|
|
str[i] = '\0';
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2012-03-07 06:57:45 +08:00
|
|
|
str[size - 1] = '\0';
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
#undef APPEND_C
|
|
|
|
#undef APPEND_S
|
|
|
|
#undef APPEND_PADDED_S
|
|
|
|
#undef GET_ARG_NUMERIC
|
2017-01-20 10:15:45 +08:00
|
|
|
return i;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(3, 4)
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_snprintf(char *str, size_t size, const char *format, ...) {
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t ret;
|
2012-03-07 06:57:45 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
ret = malloc_vsnprintf(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
|
2017-01-16 08:56:30 +08:00
|
|
|
const char *format, va_list ap) {
|
2012-03-22 09:33:03 +08:00
|
|
|
char buf[MALLOC_PRINTF_BUFSIZE];
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
if (write_cb == NULL) {
|
|
|
|
/*
|
|
|
|
* The caller did not provide an alternate write_cb callback
|
|
|
|
* function, so use the default one. malloc_write() is an
|
|
|
|
* inline function, so use malloc_message() directly here.
|
|
|
|
*/
|
2012-05-02 19:15:00 +08:00
|
|
|
write_cb = (je_malloc_message != NULL) ? je_malloc_message :
|
|
|
|
wrtmessage;
|
2012-03-07 06:57:45 +08:00
|
|
|
cbopaque = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_vsnprintf(buf, sizeof(buf), format, ap);
|
|
|
|
write_cb(cbopaque, buf);
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print to a callback function in such a way as to (hopefully) avoid memory
|
|
|
|
* allocation.
|
|
|
|
*/
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(3, 4)
|
2012-03-07 06:57:45 +08:00
|
|
|
void
|
|
|
|
malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,
|
2017-01-16 08:56:30 +08:00
|
|
|
const char *format, ...) {
|
2012-03-07 06:57:45 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
malloc_vcprintf(write_cb, cbopaque, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print to stderr in such a way as to avoid memory allocation. */
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(1, 2)
|
2012-03-07 06:57:45 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_printf(const char *format, ...) {
|
2012-03-07 06:57:45 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
malloc_vcprintf(NULL, NULL, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2015-11-13 03:06:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore normal assertion macros, in order to make it possible to compile all
|
|
|
|
* C files as a single concatenation.
|
|
|
|
*/
|
|
|
|
#undef assert
|
|
|
|
#undef not_reached
|
|
|
|
#undef not_implemented
|
2017-03-04 02:10:08 +08:00
|
|
|
#undef assert_not_implemented
|
2015-11-13 03:06:41 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|