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