mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-24 13:53:11 +00:00
Define malloc_{write,read}_fd as non-inline global functions
The static inline definition made more sense when these functions just dispatched to a syscall wrapper. Since they acquired a retry loop, a non-inline definition makes more sense.
This commit is contained in:
committed by
Guangli Dai
parent
9fdc1160c5
commit
38b12427b7
@@ -66,94 +66,18 @@ void malloc_cprintf(write_cb_t *write_cb, void *cbopaque, const char *format,
|
|||||||
...) JEMALLOC_FORMAT_PRINTF(3, 4);
|
...) JEMALLOC_FORMAT_PRINTF(3, 4);
|
||||||
void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
|
void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
|
||||||
|
|
||||||
static inline ssize_t
|
ssize_t malloc_write_fd(int fd, const void *buf, size_t count);
|
||||||
malloc_write_fd_syscall(int fd, const void *buf, size_t count) {
|
ssize_t malloc_read_fd(int fd, void *buf, size_t count);
|
||||||
#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
|
|
||||||
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) {
|
|
||||||
#ifndef _WIN32
|
|
||||||
if (errno == EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
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) {
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
#ifndef _WIN32
|
|
||||||
if (errno == EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return result;
|
|
||||||
} else if (result == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bytes_read += result;
|
|
||||||
} while (bytes_read < count);
|
|
||||||
return bytes_read;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
malloc_open(const char *path, int flags) {
|
malloc_open(const char *path, int flags) {
|
||||||
int fd;
|
|
||||||
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
|
||||||
fd = (int)syscall(SYS_open, path, flags);
|
return (int)syscall(SYS_open, path, flags);
|
||||||
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
|
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
|
||||||
fd = (int)syscall(SYS_openat, AT_FDCWD, path, flags);
|
return (int)syscall(SYS_openat, AT_FDCWD, path, flags);
|
||||||
#else
|
#else
|
||||||
fd = open(path, flags);
|
return open(path, flags);
|
||||||
#endif
|
#endif
|
||||||
return fd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
|
|||||||
@@ -760,6 +760,81 @@ malloc_printf(const char *format, ...) {
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ssize_t
|
||||||
|
malloc_write_fd_syscall(int fd, const void *buf, size_t count) {
|
||||||
|
#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.
|
||||||
|
*/
|
||||||
|
return (ssize_t)syscall(SYS_write, fd, buf, count);
|
||||||
|
#else
|
||||||
|
return (ssize_t)write(fd, buf,
|
||||||
|
# ifdef _WIN32
|
||||||
|
(unsigned int)
|
||||||
|
# endif
|
||||||
|
count);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t
|
||||||
|
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) {
|
||||||
|
#ifndef _WIN32
|
||||||
|
if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
bytes_written += result;
|
||||||
|
} while (bytes_written < count);
|
||||||
|
return bytes_written;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t
|
||||||
|
malloc_read_fd_syscall(int fd, void *buf, size_t count) {
|
||||||
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)
|
||||||
|
return (ssize_t)syscall(SYS_read, fd, buf, count);
|
||||||
|
#else
|
||||||
|
return (ssize_t)read(fd, buf,
|
||||||
|
# ifdef _WIN32
|
||||||
|
(unsigned int)
|
||||||
|
# endif
|
||||||
|
count);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
#ifndef _WIN32
|
||||||
|
if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return result;
|
||||||
|
} else if (result == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bytes_read += result;
|
||||||
|
} while (bytes_read < count);
|
||||||
|
return bytes_read;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Restore normal assertion macros, in order to make it possible to compile all
|
* Restore normal assertion macros, in order to make it possible to compile all
|
||||||
* C files as a single concatenation.
|
* C files as a single concatenation.
|
||||||
|
|||||||
Reference in New Issue
Block a user