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