Home | History | Annotate | Line # | Download | only in internal
      1  1.1  christos #ifndef JEMALLOC_INTERNAL_MALLOC_IO_H
      2  1.1  christos #define JEMALLOC_INTERNAL_MALLOC_IO_H
      3  1.1  christos 
      4  1.1  christos #ifdef _WIN32
      5  1.1  christos #  ifdef _WIN64
      6  1.1  christos #    define FMT64_PREFIX "ll"
      7  1.1  christos #    define FMTPTR_PREFIX "ll"
      8  1.1  christos #  else
      9  1.1  christos #    define FMT64_PREFIX "ll"
     10  1.1  christos #    define FMTPTR_PREFIX ""
     11  1.1  christos #  endif
     12  1.1  christos #  define FMTd32 "d"
     13  1.1  christos #  define FMTu32 "u"
     14  1.1  christos #  define FMTx32 "x"
     15  1.1  christos #  define FMTd64 FMT64_PREFIX "d"
     16  1.1  christos #  define FMTu64 FMT64_PREFIX "u"
     17  1.1  christos #  define FMTx64 FMT64_PREFIX "x"
     18  1.1  christos #  define FMTdPTR FMTPTR_PREFIX "d"
     19  1.1  christos #  define FMTuPTR FMTPTR_PREFIX "u"
     20  1.1  christos #  define FMTxPTR FMTPTR_PREFIX "x"
     21  1.1  christos #else
     22  1.1  christos #  include <inttypes.h>
     23  1.1  christos #  define FMTd32 PRId32
     24  1.1  christos #  define FMTu32 PRIu32
     25  1.1  christos #  define FMTx32 PRIx32
     26  1.1  christos #  define FMTd64 PRId64
     27  1.1  christos #  define FMTu64 PRIu64
     28  1.1  christos #  define FMTx64 PRIx64
     29  1.1  christos #  define FMTdPTR PRIdPTR
     30  1.1  christos #  define FMTuPTR PRIuPTR
     31  1.1  christos #  define FMTxPTR PRIxPTR
     32  1.1  christos #endif
     33  1.1  christos 
     34  1.1  christos /* Size of stack-allocated buffer passed to buferror(). */
     35  1.1  christos #define BUFERROR_BUF		64
     36  1.1  christos 
     37  1.1  christos /*
     38  1.1  christos  * Size of stack-allocated buffer used by malloc_{,v,vc}printf().  This must be
     39  1.1  christos  * large enough for all possible uses within jemalloc.
     40  1.1  christos  */
     41  1.1  christos #define MALLOC_PRINTF_BUFSIZE	4096
     42  1.1  christos 
     43  1.1  christos int buferror(int err, char *buf, size_t buflen);
     44  1.1  christos uintmax_t malloc_strtoumax(const char *restrict nptr, const char **restrict endptr,
     45  1.1  christos     int base);
     46  1.1  christos void malloc_write(const char *s);
     47  1.1  christos 
     48  1.1  christos /*
     49  1.1  christos  * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
     50  1.1  christos  * point math.
     51  1.1  christos  */
     52  1.1  christos size_t malloc_vsnprintf(char *str, size_t size, const char *format,
     53  1.1  christos     va_list ap);
     54  1.1  christos size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
     55  1.1  christos     JEMALLOC_FORMAT_PRINTF(3, 4);
     56  1.1  christos /*
     57  1.1  christos  * The caller can set write_cb and cbopaque to null to choose to print with the
     58  1.1  christos  * je_malloc_message hook.
     59  1.1  christos  */
     60  1.1  christos void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
     61  1.1  christos     const char *format, va_list ap);
     62  1.1  christos void malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,
     63  1.1  christos     const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4);
     64  1.1  christos void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
     65  1.1  christos 
     66  1.1  christos static inline ssize_t
     67  1.1  christos malloc_write_fd(int fd, const void *buf, size_t count) {
     68  1.1  christos #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)
     69  1.1  christos 	/*
     70  1.1  christos 	 * Use syscall(2) rather than write(2) when possible in order to avoid
     71  1.1  christos 	 * the possibility of memory allocation within libc.  This is necessary
     72  1.1  christos 	 * on FreeBSD; most operating systems do not have this problem though.
     73  1.1  christos 	 *
     74  1.1  christos 	 * syscall() returns long or int, depending on platform, so capture the
     75  1.1  christos 	 * result in the widest plausible type to avoid compiler warnings.
     76  1.1  christos 	 */
     77  1.1  christos 	long result = syscall(SYS_write, fd, buf, count);
     78  1.1  christos #else
     79  1.1  christos 	ssize_t result = (ssize_t)write(fd, buf,
     80  1.1  christos #ifdef _WIN32
     81  1.1  christos 	    (unsigned int)
     82  1.1  christos #endif
     83  1.1  christos 	    count);
     84  1.1  christos #endif
     85  1.1  christos 	return (ssize_t)result;
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos static inline ssize_t
     89  1.1  christos malloc_read_fd(int fd, void *buf, size_t count) {
     90  1.1  christos #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)
     91  1.1  christos 	long result = syscall(SYS_read, fd, buf, count);
     92  1.1  christos #else
     93  1.1  christos 	ssize_t result = read(fd, buf,
     94  1.1  christos #ifdef _WIN32
     95  1.1  christos 	    (unsigned int)
     96  1.1  christos #endif
     97  1.1  christos 	    count);
     98  1.1  christos #endif
     99  1.1  christos 	return (ssize_t)result;
    100  1.1  christos }
    101  1.1  christos 
    102  1.1  christos #endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */
    103