mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-24 13:53:11 +00:00
Add malloc_open() / malloc_close() reentrancy safe helpers
This commit is contained in:
committed by
Qi Wang
parent
60f472f367
commit
8c2e15d1a5
@@ -134,4 +134,25 @@ malloc_read_fd(int fd, void *buf, size_t count) {
|
|||||||
return bytes_read;
|
return bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int malloc_open(const char *path, int flags) {
|
||||||
|
int fd;
|
||||||
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
|
||||||
|
fd = (int)syscall(SYS_open, path, flags);
|
||||||
|
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
|
||||||
|
fd = (int)syscall(SYS_openat, AT_FDCWD, path, flags);
|
||||||
|
#else
|
||||||
|
fd = open(path, flags);
|
||||||
|
#endif
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int malloc_close(int fd) {
|
||||||
|
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
|
||||||
|
return (int)syscall(SYS_close, fd);
|
||||||
|
#else
|
||||||
|
return close(fd);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */
|
#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */
|
||||||
|
|||||||
61
src/pages.c
61
src/pages.c
@@ -651,36 +651,13 @@ os_overcommits_proc(void) {
|
|||||||
int fd;
|
int fd;
|
||||||
char buf[1];
|
char buf[1];
|
||||||
|
|
||||||
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
|
#if defined(O_CLOEXEC)
|
||||||
#if defined(O_CLOEXEC)
|
fd = malloc_open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
|
||||||
fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY |
|
|
||||||
O_CLOEXEC);
|
|
||||||
#else
|
|
||||||
fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
|
|
||||||
if (fd != -1) {
|
|
||||||
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
|
|
||||||
#if defined(O_CLOEXEC)
|
|
||||||
fd = (int)syscall(SYS_openat,
|
|
||||||
AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
|
|
||||||
#else
|
|
||||||
fd = (int)syscall(SYS_openat,
|
|
||||||
AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY);
|
|
||||||
if (fd != -1) {
|
|
||||||
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
#if defined(O_CLOEXEC)
|
fd = malloc_open("/proc/sys/vm/overcommit_memory", O_RDONLY);
|
||||||
fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
|
if (fd != -1) {
|
||||||
#else
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
||||||
fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
|
}
|
||||||
if (fd != -1) {
|
|
||||||
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
@@ -688,11 +665,7 @@ os_overcommits_proc(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
|
ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
|
||||||
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
|
malloc_close(fd);
|
||||||
syscall(SYS_close, fd);
|
|
||||||
#else
|
|
||||||
close(fd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (nread < 1) {
|
if (nread < 1) {
|
||||||
return false; /* Error. */
|
return false; /* Error. */
|
||||||
@@ -741,29 +714,17 @@ init_thp_state(void) {
|
|||||||
static const char sys_state_never[] = "always madvise [never]\n";
|
static const char sys_state_never[] = "always madvise [never]\n";
|
||||||
char buf[sizeof(sys_state_madvise)];
|
char buf[sizeof(sys_state_madvise)];
|
||||||
|
|
||||||
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
|
int fd = malloc_open(
|
||||||
int fd = (int)syscall(SYS_open,
|
|
||||||
"/sys/kernel/mm/transparent_hugepage/enabled", O_RDONLY);
|
"/sys/kernel/mm/transparent_hugepage/enabled", O_RDONLY);
|
||||||
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
|
|
||||||
int fd = (int)syscall(SYS_openat,
|
|
||||||
AT_FDCWD, "/sys/kernel/mm/transparent_hugepage/enabled", O_RDONLY);
|
|
||||||
#else
|
|
||||||
int fd = open("/sys/kernel/mm/transparent_hugepage/enabled", O_RDONLY);
|
|
||||||
#endif
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
goto label_error;
|
goto label_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
|
ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
|
||||||
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
|
malloc_close(fd);
|
||||||
syscall(SYS_close, fd);
|
if (nread < 0) {
|
||||||
#else
|
|
||||||
close(fd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
goto label_error;
|
goto label_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strncmp(buf, sys_state_madvise, (size_t)nread) == 0) {
|
if (strncmp(buf, sys_state_madvise, (size_t)nread) == 0) {
|
||||||
init_system_thp_mode = thp_mode_default;
|
init_system_thp_mode = thp_mode_default;
|
||||||
|
|||||||
Reference in New Issue
Block a user