From a09f55c87d799e6e0e64972838ca6768027e9174 Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Mon, 20 Sep 2010 16:05:41 -0700 Subject: [PATCH] Wrap strerror_r(). Create the buferror() function, which wraps strerror_r(). This is necessary because glibc provides a non-standard strerror_r(). --- .../jemalloc/internal/jemalloc_internal.h.in | 5 +++-- jemalloc/src/chunk_mmap.c | 11 ++++++----- jemalloc/src/chunk_swap.c | 7 ++++--- jemalloc/src/jemalloc.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in b/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in index 04bc56fa..514ef5c2 100644 --- a/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in +++ b/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in @@ -101,8 +101,8 @@ extern void (*JEMALLOC_P(malloc_message))(void *wcbopaque, const char *s); # define JEMALLOC_INLINE static inline #endif -/* Size of stack-allocated buffer passed to strerror_r(). */ -#define STRERROR_BUF 64 +/* Size of stack-allocated buffer passed to buferror(). */ +#define BUFERROR_BUF 64 /* Minimum alignment of allocations is 2^LG_QUANTUM bytes. */ #ifdef __i386__ @@ -289,6 +289,7 @@ extern unsigned narenas; arena_t *arenas_extend(unsigned ind); arena_t *choose_arena_hard(void); +int buferror(int errnum, char *buf, size_t buflen); void jemalloc_prefork(void); void jemalloc_postfork(void); diff --git a/jemalloc/src/chunk_mmap.c b/jemalloc/src/chunk_mmap.c index a3d09e9a..bc367559 100644 --- a/jemalloc/src/chunk_mmap.c +++ b/jemalloc/src/chunk_mmap.c @@ -28,7 +28,8 @@ static pthread_key_t mmap_unaligned_tsd; static void *pages_map(void *addr, size_t size, bool noreserve); static void pages_unmap(void *addr, size_t size); -static void *chunk_alloc_mmap_slow(size_t size, bool unaligned, bool noreserve); +static void *chunk_alloc_mmap_slow(size_t size, bool unaligned, + bool noreserve); static void *chunk_alloc_mmap_internal(size_t size, bool noreserve); /******************************************************************************/ @@ -57,9 +58,9 @@ pages_map(void *addr, size_t size, bool noreserve) * We succeeded in mapping memory, but not in the right place. */ if (munmap(ret, size) == -1) { - char buf[STRERROR_BUF]; + char buf[BUFERROR_BUF]; - strerror_r(errno, buf, sizeof(buf)); + buferror(errno, buf, sizeof(buf)); malloc_write(": Error in munmap(): "); malloc_write(buf); malloc_write("\n"); @@ -79,9 +80,9 @@ pages_unmap(void *addr, size_t size) { if (munmap(addr, size) == -1) { - char buf[STRERROR_BUF]; + char buf[BUFERROR_BUF]; - strerror_r(errno, buf, sizeof(buf)); + buferror(errno, buf, sizeof(buf)); malloc_write(": Error in munmap(): "); malloc_write(buf); malloc_write("\n"); diff --git a/jemalloc/src/chunk_swap.c b/jemalloc/src/chunk_swap.c index ed9e414d..ee038ba9 100644 --- a/jemalloc/src/chunk_swap.c +++ b/jemalloc/src/chunk_swap.c @@ -294,9 +294,10 @@ chunk_swap_enable(const int *fds, unsigned nfds, bool prezeroed) void *addr = mmap((void *)((uintptr_t)vaddr + voff), sizes[i], PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fds[i], 0); if (addr == MAP_FAILED) { - char buf[STRERROR_BUF]; + char buf[BUFERROR_BUF]; - strerror_r(errno, buf, sizeof(buf)); + + buferror(errno, buf, sizeof(buf)); malloc_write( ": Error in mmap(..., MAP_FIXED, ...): "); malloc_write(buf); @@ -304,7 +305,7 @@ chunk_swap_enable(const int *fds, unsigned nfds, bool prezeroed) if (opt_abort) abort(); if (munmap(vaddr, voff) == -1) { - strerror_r(errno, buf, sizeof(buf)); + buferror(errno, buf, sizeof(buf)); malloc_write(": Error in munmap(): "); malloc_write(buf); malloc_write("\n"); diff --git a/jemalloc/src/jemalloc.c b/jemalloc/src/jemalloc.c index 40c3a637..0b60ce60 100644 --- a/jemalloc/src/jemalloc.c +++ b/jemalloc/src/jemalloc.c @@ -139,6 +139,25 @@ choose_arena_hard(void) return (ret); } +/* + * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so + * provide a wrapper. + */ +int +buferror(int errnum, char *buf, size_t buflen) +{ +#ifdef _GNU_SOURCE + char *b = strerror_r(errno, buf, buflen); + if (b != buf) { + strncpy(buf, b, buflen); + buf[buflen-1] = '\0'; + } + return (0); +#else + return (strerror_r(errno, buf, buflen)); +#endif +} + static void stats_print_atexit(void) {