2017-03-04 02:10:08 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_MALLOC_IO_H
|
|
|
|
#define JEMALLOC_INTERNAL_MALLOC_IO_H
|
|
|
|
|
2023-06-10 08:37:47 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
2020-04-18 05:49:20 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_types.h"
|
|
|
|
|
2017-03-04 02:10:08 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# ifdef _WIN64
|
|
|
|
# define FMT64_PREFIX "ll"
|
|
|
|
# define FMTPTR_PREFIX "ll"
|
|
|
|
# else
|
|
|
|
# define FMT64_PREFIX "ll"
|
|
|
|
# define FMTPTR_PREFIX ""
|
|
|
|
# endif
|
|
|
|
# define FMTd32 "d"
|
|
|
|
# define FMTu32 "u"
|
|
|
|
# define FMTx32 "x"
|
|
|
|
# define FMTd64 FMT64_PREFIX "d"
|
|
|
|
# define FMTu64 FMT64_PREFIX "u"
|
|
|
|
# define FMTx64 FMT64_PREFIX "x"
|
|
|
|
# define FMTdPTR FMTPTR_PREFIX "d"
|
|
|
|
# define FMTuPTR FMTPTR_PREFIX "u"
|
|
|
|
# define FMTxPTR FMTPTR_PREFIX "x"
|
|
|
|
#else
|
|
|
|
# include <inttypes.h>
|
|
|
|
# define FMTd32 PRId32
|
|
|
|
# define FMTu32 PRIu32
|
|
|
|
# define FMTx32 PRIx32
|
|
|
|
# define FMTd64 PRId64
|
|
|
|
# define FMTu64 PRIu64
|
|
|
|
# define FMTx64 PRIx64
|
|
|
|
# define FMTdPTR PRIdPTR
|
|
|
|
# define FMTuPTR PRIuPTR
|
|
|
|
# define FMTxPTR PRIxPTR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Size of stack-allocated buffer passed to buferror(). */
|
|
|
|
#define BUFERROR_BUF 64
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
|
|
|
|
* large enough for all possible uses within jemalloc.
|
|
|
|
*/
|
|
|
|
#define MALLOC_PRINTF_BUFSIZE 4096
|
|
|
|
|
2020-04-18 05:49:20 +08:00
|
|
|
write_cb_t wrtmessage;
|
2017-03-04 02:10:08 +08:00
|
|
|
int buferror(int err, char *buf, size_t buflen);
|
|
|
|
uintmax_t malloc_strtoumax(const char *restrict nptr, char **restrict endptr,
|
|
|
|
int base);
|
|
|
|
void malloc_write(const char *s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
|
|
|
|
* point math.
|
|
|
|
*/
|
|
|
|
size_t malloc_vsnprintf(char *str, size_t size, const char *format,
|
|
|
|
va_list ap);
|
|
|
|
size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
|
|
|
|
JEMALLOC_FORMAT_PRINTF(3, 4);
|
2017-07-22 04:34:45 +08:00
|
|
|
/*
|
2019-06-05 02:13:00 +08:00
|
|
|
* The caller can set write_cb to null to choose to print with the
|
2017-07-22 04:34:45 +08:00
|
|
|
* je_malloc_message hook.
|
|
|
|
*/
|
2020-04-18 05:49:20 +08:00
|
|
|
void malloc_vcprintf(write_cb_t *write_cb, void *cbopaque, const char *format,
|
|
|
|
va_list ap);
|
|
|
|
void malloc_cprintf(write_cb_t *write_cb, void *cbopaque, const char *format,
|
|
|
|
...) JEMALLOC_FORMAT_PRINTF(3, 4);
|
2017-03-04 02:10:08 +08:00
|
|
|
void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
|
|
|
|
|
2018-04-07 02:40:44 +08:00
|
|
|
static inline ssize_t
|
2023-08-11 01:43:42 +08:00
|
|
|
malloc_write_fd_syscall(int fd, const void *buf, size_t count) {
|
2018-04-07 02:40:44 +08:00
|
|
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)
|
|
|
|
/*
|
|
|
|
* Use syscall(2) rather than write(2) when possible in order to avoid
|
|
|
|
* the possibility of memory allocation within libc. This is necessary
|
|
|
|
* on FreeBSD; most operating systems do not have this problem though.
|
|
|
|
*
|
|
|
|
* syscall() returns long or int, depending on platform, so capture the
|
|
|
|
* result in the widest plausible type to avoid compiler warnings.
|
|
|
|
*/
|
|
|
|
long result = syscall(SYS_write, fd, buf, count);
|
|
|
|
#else
|
|
|
|
ssize_t result = (ssize_t)write(fd, buf,
|
|
|
|
#ifdef _WIN32
|
|
|
|
(unsigned int)
|
|
|
|
#endif
|
|
|
|
count);
|
|
|
|
#endif
|
|
|
|
return (ssize_t)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t
|
2023-08-11 01:43:42 +08:00
|
|
|
malloc_write_fd(int fd, const void *buf, size_t count) {
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
do {
|
|
|
|
ssize_t result = malloc_write_fd_syscall(fd,
|
|
|
|
&((const byte_t *)buf)[bytes_written],
|
|
|
|
count - bytes_written);
|
|
|
|
if (result < 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
bytes_written += result;
|
|
|
|
} while (bytes_written < count);
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t
|
|
|
|
malloc_read_fd_syscall(int fd, void *buf, size_t count) {
|
2018-04-07 02:40:44 +08:00
|
|
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)
|
|
|
|
long result = syscall(SYS_read, fd, buf, count);
|
|
|
|
#else
|
|
|
|
ssize_t result = read(fd, buf,
|
|
|
|
#ifdef _WIN32
|
|
|
|
(unsigned int)
|
|
|
|
#endif
|
|
|
|
count);
|
|
|
|
#endif
|
|
|
|
return (ssize_t)result;
|
|
|
|
}
|
|
|
|
|
2023-08-11 01:43:42 +08:00
|
|
|
static inline ssize_t
|
|
|
|
malloc_read_fd(int fd, void *buf, size_t count) {
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
do {
|
|
|
|
ssize_t result = malloc_read_fd_syscall(fd,
|
|
|
|
&((byte_t *)buf)[bytes_read], count - bytes_read);
|
|
|
|
if (result < 0) {
|
|
|
|
return result;
|
|
|
|
} else if (result == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bytes_read += result;
|
|
|
|
} while (bytes_read < count);
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2017-03-04 02:10:08 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */
|