2 * Define simple versions of assertion macros that won't recurse in case
3 * of assertion failures in malloc_*printf().
5 #define assert(e) do { \
6 if (config_debug && !(e)) { \
7 malloc_write("<jemalloc>: Failed assertion\n"); \
12 #define not_reached() do { \
14 malloc_write("<jemalloc>: Unreachable code reached\n"); \
19 #define not_implemented() do { \
21 malloc_write("<jemalloc>: Not implemented\n"); \
26 #define JEMALLOC_UTIL_C_
27 #include "jemalloc/internal/jemalloc_internal.h"
29 /******************************************************************************/
30 /* Function prototypes for non-inline static functions. */
32 static void wrtmessage(void *cbopaque, const char *s);
33 #define U2S_BUFSIZE ((1U << (LG_SIZEOF_INTMAX_T + 3)) + 1)
34 static char *u2s(uintmax_t x, unsigned base, bool uppercase, char *s,
36 #define D2S_BUFSIZE (1 + U2S_BUFSIZE)
37 static char *d2s(intmax_t x, char sign, char *s, size_t *slen_p);
38 #define O2S_BUFSIZE (1 + U2S_BUFSIZE)
39 static char *o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p);
40 #define X2S_BUFSIZE (2 + U2S_BUFSIZE)
41 static char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,
44 /******************************************************************************/
46 /* malloc_message() setup. */
48 wrtmessage(void *cbopaque, const char *s)
53 * Use syscall(2) rather than write(2) when possible in order to avoid
54 * the possibility of memory allocation within libc. This is necessary
55 * on FreeBSD; most operating systems do not have this problem though.
57 * syscall() returns long or int, depending on platform, so capture the
58 * unused result in the widest plausible type to avoid compiler
61 UNUSED long result = syscall(SYS_write, STDERR_FILENO, s, strlen(s));
63 UNUSED ssize_t result = write(STDERR_FILENO, s, strlen(s));
67 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
69 JEMALLOC_ATTR(visibility("hidden"))
71 wrtmessage_1_0(const char *s1, const char *s2, const char *s3,
81 void (*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
82 const char *s4) = wrtmessage_1_0;
83 __sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
86 * Wrapper around malloc_message() that avoids the need for
87 * je_malloc_message(...) throughout the code.
90 malloc_write(const char *s)
93 if (je_malloc_message != NULL)
94 je_malloc_message(NULL, s);
100 * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so
104 buferror(int err, char *buf, size_t buflen)
108 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
109 (LPSTR)buf, (DWORD)buflen, NULL);
111 #elif defined(__GLIBC__) && defined(_GNU_SOURCE)
112 char *b = strerror_r(err, buf, buflen);
114 strncpy(buf, b, buflen);
115 buf[buflen-1] = '\0';
119 return (strerror_r(err, buf, buflen));
124 malloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base)
126 uintmax_t ret, digit;
132 if (base < 0 || base == 1 || base > 36) {
140 /* Swallow leading whitespace and get sign, if any. */
144 case '\t': case '\n': case '\v': case '\f': case '\r': case ' ':
158 /* Get prefix, if any. */
161 * Note where the first non-whitespace/sign character is so that it is
162 * possible to tell whether any digits are consumed (e.g., " 0" vs.
168 case '0': case '1': case '2': case '3': case '4': case '5':
177 case '0': case '1': case '2': case '3': case '4':
178 case '5': case '6': case '7': case '8': case '9':
179 case 'A': case 'B': case 'C': case 'D': case 'E':
181 case 'a': case 'b': case 'c': case 'd': case 'e':
203 while ((*p >= '0' && *p <= '9' && (digit = *p - '0') < b)
204 || (*p >= 'A' && *p <= 'Z' && (digit = 10 + *p - 'A') < b)
205 || (*p >= 'a' && *p <= 'z' && (digit = 10 + *p - 'a') < b)) {
206 uintmax_t pret = ret;
221 /* No conversion performed. */
228 if (endptr != NULL) {
230 /* No characters were converted. */
231 *endptr = (char *)nptr;
239 u2s(uintmax_t x, unsigned base, bool uppercase, char *s, size_t *slen_p)
249 s[i] = "0123456789"[x % (uint64_t)10];
254 const char *digits = (uppercase)
256 : "0123456789abcdef";
260 s[i] = digits[x & 0xf];
265 const char *digits = (uppercase)
266 ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
267 : "0123456789abcdefghijklmnopqrstuvwxyz";
269 assert(base >= 2 && base <= 36);
272 s[i] = digits[x % (uint64_t)base];
277 *slen_p = U2S_BUFSIZE - 1 - i;
282 d2s(intmax_t x, char sign, char *s, size_t *slen_p)
288 s = u2s(x, 10, false, s, slen_p);
302 default: not_reached();
308 o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p)
311 s = u2s(x, 8, false, s, slen_p);
312 if (alt_form && *s != '0') {
321 x2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p)
324 s = u2s(x, 16, uppercase, s, slen_p);
328 memcpy(s, uppercase ? "0X" : "0x", 2);
334 malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
340 #define APPEND_C(c) do { \
345 #define APPEND_S(s, slen) do { \
347 size_t cpylen = (slen <= size - i) ? slen : size - i; \
348 memcpy(&str[i], s, cpylen); \
352 #define APPEND_PADDED_S(s, slen, width, left_justify) do { \
353 /* Left padding. */ \
354 size_t pad_len = (width == -1) ? 0 : ((slen < (size_t)width) ? \
355 (size_t)width - slen : 0); \
356 if (!left_justify && pad_len != 0) { \
358 for (j = 0; j < pad_len; j++) \
363 /* Right padding. */ \
364 if (left_justify && pad_len != 0) { \
366 for (j = 0; j < pad_len; j++) \
370 #define GET_ARG_NUMERIC(val, len) do { \
373 val = va_arg(ap, int); \
376 val = va_arg(ap, unsigned int); \
379 val = va_arg(ap, long); \
382 val = va_arg(ap, unsigned long); \
385 val = va_arg(ap, long long); \
388 val = va_arg(ap, unsigned long long); \
391 val = va_arg(ap, intmax_t); \
394 val = va_arg(ap, uintmax_t); \
397 val = va_arg(ap, ptrdiff_t); \
400 val = va_arg(ap, ssize_t); \
403 val = va_arg(ap, size_t); \
405 case 'p': /* Synthetic; used for %p. */ \
406 val = va_arg(ap, uintptr_t); \
418 case '\0': goto label_out;
420 bool alt_form = false;
421 bool left_justify = false;
422 bool plus_space = false;
423 bool plus_plus = false;
426 unsigned char len = '?';
437 assert(!left_justify);
448 default: goto label_width;
456 width = va_arg(ap, int);
463 case '0': case '1': case '2': case '3': case '4':
464 case '5': case '6': case '7': case '8': case '9': {
467 uwidth = malloc_strtoumax(f, (char **)&f, 10);
468 assert(uwidth != UINTMAX_MAX || get_errno() !=
475 /* Width/precision separator. */
483 prec = va_arg(ap, int);
486 case '0': case '1': case '2': case '3': case '4':
487 case '5': case '6': case '7': case '8': case '9': {
490 uprec = malloc_strtoumax(f, (char **)&f, 10);
491 assert(uprec != UINTMAX_MAX || get_errno() !=
509 case 'q': case 'j': case 't': case 'z':
515 /* Conversion specifier. */
524 case 'd': case 'i': {
525 intmax_t val JEMALLOC_CC_SILENCE_INIT(0);
526 char buf[D2S_BUFSIZE];
528 GET_ARG_NUMERIC(val, len);
529 s = d2s(val, (plus_plus ? '+' : (plus_space ?
530 ' ' : '-')), buf, &slen);
531 APPEND_PADDED_S(s, slen, width, left_justify);
535 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
536 char buf[O2S_BUFSIZE];
538 GET_ARG_NUMERIC(val, len | 0x80);
539 s = o2s(val, alt_form, buf, &slen);
540 APPEND_PADDED_S(s, slen, width, left_justify);
544 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
545 char buf[U2S_BUFSIZE];
547 GET_ARG_NUMERIC(val, len | 0x80);
548 s = u2s(val, 10, false, buf, &slen);
549 APPEND_PADDED_S(s, slen, width, left_justify);
552 } case 'x': case 'X': {
553 uintmax_t val JEMALLOC_CC_SILENCE_INIT(0);
554 char buf[X2S_BUFSIZE];
556 GET_ARG_NUMERIC(val, len | 0x80);
557 s = x2s(val, alt_form, *f == 'X', buf, &slen);
558 APPEND_PADDED_S(s, slen, width, left_justify);
565 assert(len == '?' || len == 'l');
566 assert_not_implemented(len != 'l');
567 val = va_arg(ap, int);
570 APPEND_PADDED_S(buf, 1, width, left_justify);
574 assert(len == '?' || len == 'l');
575 assert_not_implemented(len != 'l');
576 s = va_arg(ap, char *);
577 slen = (prec < 0) ? strlen(s) : (size_t)prec;
578 APPEND_PADDED_S(s, slen, width, left_justify);
583 char buf[X2S_BUFSIZE];
585 GET_ARG_NUMERIC(val, 'p');
586 s = x2s(val, true, false, buf, &slen);
587 APPEND_PADDED_S(s, slen, width, left_justify);
590 } default: not_reached();
603 str[size - 1] = '\0';
609 #undef APPEND_PADDED_S
610 #undef GET_ARG_NUMERIC
614 JEMALLOC_FORMAT_PRINTF(3, 4)
616 malloc_snprintf(char *str, size_t size, const char *format, ...)
621 va_start(ap, format);
622 ret = malloc_vsnprintf(str, size, format, ap);
629 malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
630 const char *format, va_list ap)
632 char buf[MALLOC_PRINTF_BUFSIZE];
634 if (write_cb == NULL) {
636 * The caller did not provide an alternate write_cb callback
637 * function, so use the default one. malloc_write() is an
638 * inline function, so use malloc_message() directly here.
640 write_cb = (je_malloc_message != NULL) ? je_malloc_message :
645 malloc_vsnprintf(buf, sizeof(buf), format, ap);
646 write_cb(cbopaque, buf);
650 * Print to a callback function in such a way as to (hopefully) avoid memory
653 JEMALLOC_FORMAT_PRINTF(3, 4)
655 malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,
656 const char *format, ...)
660 va_start(ap, format);
661 malloc_vcprintf(write_cb, cbopaque, format, ap);
665 /* Print to stderr in such a way as to avoid memory allocation. */
666 JEMALLOC_FORMAT_PRINTF(1, 2)
668 malloc_printf(const char *format, ...)
672 va_start(ap, format);
673 malloc_vcprintf(NULL, NULL, format, ap);
678 * Restore normal assertion macros, in order to make it possible to compile all
679 * C files as a single concatenation.
683 #undef not_implemented
684 #include "jemalloc/internal/assert.h"