1 #define JEMALLOC_MALLOC_IO_C_
2 #include "jemalloc/internal/jemalloc_preamble.h"
3 #include "jemalloc/internal/jemalloc_internal_includes.h"
5 #include "jemalloc/internal/malloc_io.h"
6 #include "jemalloc/internal/util.h"
14 #ifdef not_implemented
15 # undef not_implemented
17 #ifdef assert_not_implemented
18 # undef assert_not_implemented
22 * Define simple versions of assertion macros that won't recurse in case
23 * of assertion failures in malloc_*printf().
25 #define assert(e) do { \
26 if (config_debug && !(e)) { \
27 malloc_write("<jemalloc>: Failed assertion\n"); \
32 #define not_reached() do { \
34 malloc_write("<jemalloc>: Unreachable code reached\n"); \
40 #define not_implemented() do { \
42 malloc_write("<jemalloc>: Not implemented\n"); \
47 #define assert_not_implemented(e) do { \
48 if (unlikely(config_debug && !(e))) { \
53 /******************************************************************************/
54 /* Function prototypes for non-inline static functions. */
56 static void wrtmessage(void *cbopaque, const char *s);
57 #define U2S_BUFSIZE ((1U << (LG_SIZEOF_INTMAX_T + 3)) + 1)
58 static char *u2s(uintmax_t x, unsigned base, bool uppercase, char *s,
60 #define D2S_BUFSIZE (1 + U2S_BUFSIZE)
61 static char *d2s(intmax_t x, char sign, char *s, size_t *slen_p);
62 #define O2S_BUFSIZE (1 + U2S_BUFSIZE)
63 static char *o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p);
64 #define X2S_BUFSIZE (2 + U2S_BUFSIZE)
65 static char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,
68 /******************************************************************************/
70 /* malloc_message() setup. */
72 wrtmessage(void *cbopaque, const char *s) {
73 malloc_write_fd(STDERR_FILENO, s, strlen(s));
76 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
78 JEMALLOC_ATTR(visibility("hidden"))
80 wrtmessage_1_0(const char *s1, const char *s2, const char *s3, const char *s4) {
88 void (*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
89 const char *s4) = wrtmessage_1_0;
90 __sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
93 * Wrapper around malloc_message() that avoids the need for
94 * je_malloc_message(...) throughout the code.
97 malloc_write(const char *s) {
98 if (je_malloc_message != NULL) {
99 je_malloc_message(NULL, s);
106 * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so
110 buferror(int err, char *buf, size_t buflen) {
112 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
113 (LPSTR)buf, (DWORD)buflen, NULL);
115 #elif defined(JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE) && defined(_GNU_SOURCE)
116 char *b = strerror_r(err, buf, buflen);
118 strncpy(buf, b, buflen);
119 buf[buflen-1] = '\0';
123 return strerror_r(err, buf, buflen);
128 malloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base) {
129 uintmax_t ret, digit;
135 if (base < 0 || base == 1 || base > 36) {
143 /* Swallow leading whitespace and get sign, if any. */
147 case '\t': case '\n': case '\v': case '\f': case '\r': case ' ':
161 /* Get prefix, if any. */
164 * Note where the first non-whitespace/sign character is so that it is
165 * possible to tell whether any digits are consumed (e.g., " 0" vs.
171 case '0': case '1': case '2': case '3': case '4': case '5':
182 case '0': case '1': case '2': case '3': case '4':
183 case '5': case '6': case '7': case '8': case '9':
184 case 'A': case 'B': case 'C': case 'D': case 'E':
186 case 'a': case 'b': case 'c': case 'd': case 'e':
211 while ((*p >= '0' && *p <= '9' && (digit = *p - '0') < b)
212 || (*p >= 'A' && *p <= 'Z' && (digit = 10 + *p - 'A') < b)
213 || (*p >= 'a' && *p <= 'z' && (digit = 10 + *p - 'a') < b)) {
214 uintmax_t pret = ret;
226 ret = (uintmax_t)(-((intmax_t)ret));
230 /* No conversion performed. */
237 if (endptr != NULL) {
239 /* No characters were converted. */
240 *endptr = (char *)nptr;
249 u2s(uintmax_t x, unsigned base, bool uppercase, char *s, size_t *slen_p) {
258 s[i] = "0123456789"[x % (uint64_t)10];
263 const char *digits = (uppercase)
265 : "0123456789abcdef";
269 s[i] = digits[x & 0xf];
274 const char *digits = (uppercase)
275 ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
276 : "0123456789abcdefghijklmnopqrstuvwxyz";
278 assert(base >= 2 && base <= 36);
281 s[i] = digits[x % (uint64_t)base];
286 *slen_p = U2S_BUFSIZE - 1 - i;
291 d2s(intmax_t x, char sign, char *s, size_t *slen_p) {
294 if ((neg = (x < 0))) {
297 s = u2s(x, 10, false, s, slen_p);
313 default: not_reached();
319 o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p) {
320 s = u2s(x, 8, false, s, slen_p);
321 if (alt_form && *s != '0') {
330 x2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p) {
331 s = u2s(x, 16, uppercase, s, slen_p);
335 memcpy(s, uppercase ? "0X" : "0x", 2);
341 malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
345 #define APPEND_C(c) do { \
351 #define APPEND_S(s, slen) do { \
353 size_t cpylen = (slen <= size - i) ? slen : size - i; \
354 memcpy(&str[i], s, cpylen); \
358 #define APPEND_PADDED_S(s, slen, width, left_justify) do { \
359 /* Left padding. */ \
360 size_t pad_len = (width == -1) ? 0 : ((slen < (size_t)width) ? \
361 (size_t)width - slen : 0); \
362 if (!left_justify && pad_len != 0) { \
364 for (j = 0; j < pad_len; j++) { \
370 /* Right padding. */ \
371 if (left_justify && pad_len != 0) { \
373 for (j = 0; j < pad_len; j++) { \
378 #define GET_ARG_NUMERIC(val, len) do { \
379 switch ((unsigned char)len) { \
381 val = va_arg(ap, int); \
384 val = va_arg(ap, unsigned int); \
387 val = va_arg(ap, long); \
390 val = va_arg(ap, unsigned long); \
393 val = va_arg(ap, long long); \
396 val = va_arg(ap, unsigned long long); \
399 val = va_arg(ap, intmax_t); \
402 val = va_arg(ap, uintmax_t); \
405 val = va_arg(ap, ptrdiff_t); \
408 val = va_arg(ap, ssize_t); \
411 val = va_arg(ap, size_t); \
413 case 'p': /* Synthetic; used for %p. */ \
414 val = va_arg(ap, uintptr_t); \
426 case '\0': goto label_out;
428 bool alt_form = false;
429 bool left_justify = false;
430 bool plus_space = false;
431 bool plus_plus = false;
434 unsigned char len = '?';
447 assert(!left_justify);
458 default: goto label_width;
466 width = va_arg(ap, int);
473 case '0': case '1': case '2': case '3': case '4':
474 case '5': case '6': case '7': case '8': case '9': {
477 uwidth = malloc_strtoumax(f, (char **)&f, 10);
478 assert(uwidth != UINTMAX_MAX || get_errno() !=
485 /* Width/precision separator. */
494 prec = va_arg(ap, int);
497 case '0': case '1': case '2': case '3': case '4':
498 case '5': case '6': case '7': case '8': case '9': {
501 uprec = malloc_strtoumax(f, (char **)&f, 10);
502 assert(uprec != UINTMAX_MAX || get_errno() !=
521 case 'q': case 'j': case 't': case 'z':
527 /* Conversion specifier. */
534 case 'd': case 'i': {
535 intmax_t val JEMALLOC_CC_SILENCE_INIT(0);
536 char buf[D2S_BUFSIZE];
538 GET_ARG_NUMERIC(val, len);
539 s = d2s(val, (plus_plus ? '+' : (plus_space ?
540 ' ' : '-')), buf, &slen);
541 APPEND_PADDED_S(s, slen, width, left_justify);
545 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
546 char buf[O2S_BUFSIZE];
548 GET_ARG_NUMERIC(val, len | 0x80);
549 s = o2s(val, alt_form, buf, &slen);
550 APPEND_PADDED_S(s, slen, width, left_justify);
554 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
555 char buf[U2S_BUFSIZE];
557 GET_ARG_NUMERIC(val, len | 0x80);
558 s = u2s(val, 10, false, buf, &slen);
559 APPEND_PADDED_S(s, slen, width, left_justify);
562 } case 'x': case 'X': {
563 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
564 char buf[X2S_BUFSIZE];
566 GET_ARG_NUMERIC(val, len | 0x80);
567 s = x2s(val, alt_form, *f == 'X', buf, &slen);
568 APPEND_PADDED_S(s, slen, width, left_justify);
575 assert(len == '?' || len == 'l');
576 assert_not_implemented(len != 'l');
577 val = va_arg(ap, int);
580 APPEND_PADDED_S(buf, 1, width, left_justify);
584 assert(len == '?' || len == 'l');
585 assert_not_implemented(len != 'l');
586 s = va_arg(ap, char *);
587 slen = (prec < 0) ? strlen(s) : (size_t)prec;
588 APPEND_PADDED_S(s, slen, width, left_justify);
593 char buf[X2S_BUFSIZE];
595 GET_ARG_NUMERIC(val, 'p');
596 s = x2s(val, true, false, buf, &slen);
597 APPEND_PADDED_S(s, slen, width, left_justify);
600 } default: not_reached();
613 str[size - 1] = '\0';
618 #undef APPEND_PADDED_S
619 #undef GET_ARG_NUMERIC
623 JEMALLOC_FORMAT_PRINTF(3, 4)
625 malloc_snprintf(char *str, size_t size, const char *format, ...) {
629 va_start(ap, format);
630 ret = malloc_vsnprintf(str, size, format, ap);
637 malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
638 const char *format, va_list ap) {
639 char buf[MALLOC_PRINTF_BUFSIZE];
641 if (write_cb == NULL) {
643 * The caller did not provide an alternate write_cb callback
644 * function, so use the default one. malloc_write() is an
645 * inline function, so use malloc_message() directly here.
647 write_cb = (je_malloc_message != NULL) ? je_malloc_message :
651 malloc_vsnprintf(buf, sizeof(buf), format, ap);
652 write_cb(cbopaque, buf);
656 * Print to a callback function in such a way as to (hopefully) avoid memory
659 JEMALLOC_FORMAT_PRINTF(3, 4)
661 malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,
662 const char *format, ...) {
665 va_start(ap, format);
666 malloc_vcprintf(write_cb, cbopaque, format, ap);
670 /* Print to stderr in such a way as to avoid memory allocation. */
671 JEMALLOC_FORMAT_PRINTF(1, 2)
673 malloc_printf(const char *format, ...) {
676 va_start(ap, format);
677 malloc_vcprintf(NULL, NULL, format, ap);
682 * Restore normal assertion macros, in order to make it possible to compile all
683 * C files as a single concatenation.
687 #undef not_implemented
688 #undef assert_not_implemented
689 #include "jemalloc/internal/assert.h"