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