Fix type warning on Windows.

Add cast since read / write has unsigned return type on windows.
This commit is contained in:
Qi Wang 2018-04-06 11:40:44 -07:00 committed by Qi Wang
parent 4df483f0fd
commit d3e0976a2c
5 changed files with 44 additions and 31 deletions

View File

@ -63,4 +63,40 @@ void malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,
const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4); const char *format, ...) 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
malloc_write_fd(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.
*/
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_read_fd(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;
}
#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */ #endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */

View File

@ -70,20 +70,7 @@ static char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,
/* malloc_message() setup. */ /* malloc_message() setup. */
static void static void
wrtmessage(void *cbopaque, const char *s) { wrtmessage(void *cbopaque, const char *s) {
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write) malloc_write_fd(STDERR_FILENO, s, strlen(s));
/*
* 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
* unused result in the widest plausible type to avoid compiler
* warnings.
*/
UNUSED long result = syscall(SYS_write, STDERR_FILENO, s, strlen(s));
#else
UNUSED ssize_t result = write(STDERR_FILENO, s, strlen(s));
#endif
} }
JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s); JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);

View File

@ -436,7 +436,6 @@ static bool
os_overcommits_proc(void) { os_overcommits_proc(void) {
int fd; int fd;
char buf[1]; char buf[1];
ssize_t nread;
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open) #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
#if defined(O_CLOEXEC) #if defined(O_CLOEXEC)
@ -474,12 +473,7 @@ os_overcommits_proc(void) {
return false; /* Error. */ return false; /* Error. */
} }
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read) ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
nread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf));
#else
nread = read(fd, &buf, sizeof(buf));
#endif
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close) #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
syscall(SYS_close, fd); syscall(SYS_close, fd);
#else #else
@ -543,12 +537,7 @@ init_thp_state(void) {
goto label_error; goto label_error;
} }
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read) ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));
ssize_t nread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf));
#else
ssize_t nread = read(fd, &buf, sizeof(buf));
#endif
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close) #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
syscall(SYS_close, fd); syscall(SYS_close, fd);
#else #else

View File

@ -978,7 +978,7 @@ prof_dump_flush(bool propagate_err) {
cassert(config_prof); cassert(config_prof);
err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end); err = malloc_write_fd(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
if (err == -1) { if (err == -1) {
if (!propagate_err) { if (!propagate_err) {
malloc_write("<jemalloc>: write() failed during heap " malloc_write("<jemalloc>: write() failed during heap "
@ -1471,8 +1471,9 @@ prof_dump_maps(bool propagate_err) {
goto label_return; goto label_return;
} }
} }
nread = read(mfd, &prof_dump_buf[prof_dump_buf_end], nread = malloc_read_fd(mfd,
PROF_DUMP_BUFSIZE - prof_dump_buf_end); &prof_dump_buf[prof_dump_buf_end], PROF_DUMP_BUFSIZE
- prof_dump_buf_end);
} while (nread > 0); } while (nread > 0);
} else { } else {
ret = true; ret = true;

View File

@ -67,7 +67,7 @@ token_error(token_t *token) {
token->col); token->col);
break; break;
} }
UNUSED ssize_t err = write(STDERR_FILENO, UNUSED ssize_t err = malloc_write_fd(STDERR_FILENO,
&token->parser->buf[token->pos], token->len); &token->parser->buf[token->pos], token->len);
malloc_printf("\n"); malloc_printf("\n");
} }