Avoid function prototype incompatibilities.

Add various function attributes to the exported functions to give the
compiler more information to work with during optimization, and also
specify throw() when compiling with C++ on Linux, in order to adequately
match what __THROW does in glibc.

This resolves #237.
This commit is contained in:
Jason Evans 2015-07-10 14:33:00 -07:00
parent dde067264d
commit ae93d6bf36
7 changed files with 100 additions and 49 deletions

View File

@ -301,6 +301,7 @@ case "${host}" in
AC_DEFINE([JEMALLOC_HAS_ALLOCA_H]) AC_DEFINE([JEMALLOC_HAS_ALLOCA_H])
AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ]) AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])
AC_DEFINE([JEMALLOC_THREADED_INIT], [ ]) AC_DEFINE([JEMALLOC_THREADED_INIT], [ ])
AC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])
default_munmap="0" default_munmap="0"
;; ;;
*-*-netbsd*) *-*-netbsd*)

View File

@ -17,5 +17,12 @@
*/ */
#undef JEMALLOC_USABLE_SIZE_CONST #undef JEMALLOC_USABLE_SIZE_CONST
/*
* If defined, specify throw() for the public function prototypes when compiling
* with C++. The only justification for this is to match the prototypes that
* glibc defines.
*/
#undef JEMALLOC_USE_CXX_THROW
/* sizeof(void *) == 2^LG_SIZEOF_PTR. */ /* sizeof(void *) == 2^LG_SIZEOF_PTR. */
#undef LG_SIZEOF_PTR #undef LG_SIZEOF_PTR

View File

@ -30,14 +30,23 @@
*/ */
# define MALLOCX_ARENA(a) ((int)(((a)+1) << 20)) # define MALLOCX_ARENA(a) ((int)(((a)+1) << 20))
#if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)
# define JEMALLOC_CXX_THROW throw()
#else
# define JEMALLOC_CXX_THROW
#endif
#ifdef JEMALLOC_HAVE_ATTR #ifdef JEMALLOC_HAVE_ATTR
# define JEMALLOC_ATTR(s) __attribute__((s)) # define JEMALLOC_ATTR(s) __attribute__((s))
# ifndef JEMALLOC_EXPORT # ifndef JEMALLOC_EXPORT
# define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default")) # define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
# endif # endif
# define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s)) # define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))
# define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s)) # define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
# define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))
# define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline) # define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
# define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
# define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
#elif _MSC_VER #elif _MSC_VER
# define JEMALLOC_ATTR(s) # define JEMALLOC_ATTR(s)
# ifndef JEMALLOC_EXPORT # ifndef JEMALLOC_EXPORT
@ -48,12 +57,18 @@
# endif # endif
# endif # endif
# define JEMALLOC_ALIGNED(s) __declspec(align(s)) # define JEMALLOC_ALIGNED(s) __declspec(align(s))
# define JEMALLOC_SECTION(s) __declspec(allocate(s)) # define JEMALLOC_ALLOC_SIZE(s)
# define JEMALLOC_ALLOC_SIZE2(s1, s2)
# define JEMALLOC_NOINLINE __declspec(noinline) # define JEMALLOC_NOINLINE __declspec(noinline)
# define JEMALLOC_NOTHROW __declspec(nothrow)
# define JEMALLOC_SECTION(s) __declspec(allocate(s))
#else #else
# define JEMALLOC_ATTR(s) # define JEMALLOC_ATTR(s)
# define JEMALLOC_EXPORT # define JEMALLOC_EXPORT
# define JEMALLOC_ALIGNED(s) # define JEMALLOC_ALIGNED(s)
# define JEMALLOC_SECTION(s) # define JEMALLOC_ALLOC_SIZE(s)
# define JEMALLOC_ALLOC_SIZE2(s1, s2)
# define JEMALLOC_NOINLINE # define JEMALLOC_NOINLINE
# define JEMALLOC_NOTHROW
# define JEMALLOC_SECTION(s)
#endif #endif

View File

@ -7,44 +7,52 @@ extern JEMALLOC_EXPORT const char *@je_@malloc_conf;
extern JEMALLOC_EXPORT void (*@je_@malloc_message)(void *cbopaque, extern JEMALLOC_EXPORT void (*@je_@malloc_message)(void *cbopaque,
const char *s); const char *s);
JEMALLOC_EXPORT void *@je_@malloc(size_t size) JEMALLOC_ATTR(malloc); JEMALLOC_EXPORT void *@je_@malloc(size_t size) JEMALLOC_CXX_THROW
JEMALLOC_EXPORT void *@je_@calloc(size_t num, size_t size) JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1) JEMALLOC_NOTHROW;
JEMALLOC_ATTR(malloc); JEMALLOC_EXPORT void *@je_@calloc(size_t num, size_t size) JEMALLOC_CXX_THROW
JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT int @je_@posix_memalign(void **memptr, size_t alignment, JEMALLOC_EXPORT int @je_@posix_memalign(void **memptr, size_t alignment,
size_t size) JEMALLOC_ATTR(nonnull(1)); size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void *@je_@aligned_alloc(size_t alignment, size_t size) JEMALLOC_EXPORT void *@je_@aligned_alloc(size_t alignment, size_t size)
JEMALLOC_ATTR(malloc); JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(2)
JEMALLOC_EXPORT void *@je_@realloc(void *ptr, size_t size); JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void @je_@free(void *ptr); JEMALLOC_EXPORT void *@je_@realloc(void *ptr, size_t size) JEMALLOC_CXX_THROW
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void @je_@free(void *ptr) JEMALLOC_CXX_THROW
JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void *@je_@mallocx(size_t size, int flags) JEMALLOC_EXPORT void *@je_@mallocx(size_t size, int flags)
JEMALLOC_ATTR(malloc); JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void *@je_@rallocx(void *ptr, size_t size, int flags); JEMALLOC_EXPORT void *@je_@rallocx(void *ptr, size_t size, int flags)
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT size_t @je_@xallocx(void *ptr, size_t size, size_t extra, JEMALLOC_EXPORT size_t @je_@xallocx(void *ptr, size_t size, size_t extra,
int flags); int flags) JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT size_t @je_@sallocx(const void *ptr, int flags) JEMALLOC_EXPORT size_t @je_@sallocx(const void *ptr, int flags)
JEMALLOC_ATTR(pure); JEMALLOC_ATTR(pure) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void @je_@dallocx(void *ptr, int flags); JEMALLOC_EXPORT void @je_@dallocx(void *ptr, int flags) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void @je_@sdallocx(void *ptr, size_t size, int flags); JEMALLOC_EXPORT void @je_@sdallocx(void *ptr, size_t size, int flags)
JEMALLOC_NOTHROW;
JEMALLOC_EXPORT size_t @je_@nallocx(size_t size, int flags) JEMALLOC_EXPORT size_t @je_@nallocx(size_t size, int flags)
JEMALLOC_ATTR(pure); JEMALLOC_ATTR(pure) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT int @je_@mallctl(const char *name, void *oldp, JEMALLOC_EXPORT int @je_@mallctl(const char *name, void *oldp,
size_t *oldlenp, void *newp, size_t newlen); size_t *oldlenp, void *newp, size_t newlen) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT int @je_@mallctlnametomib(const char *name, size_t *mibp, JEMALLOC_EXPORT int @je_@mallctlnametomib(const char *name, size_t *mibp,
size_t *miblenp); size_t *miblenp) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT int @je_@mallctlbymib(const size_t *mib, size_t miblen, JEMALLOC_EXPORT int @je_@mallctlbymib(const size_t *mib, size_t miblen,
void *oldp, size_t *oldlenp, void *newp, size_t newlen); void *oldp, size_t *oldlenp, void *newp, size_t newlen) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT void @je_@malloc_stats_print(void (*write_cb)(void *, JEMALLOC_EXPORT void @je_@malloc_stats_print(void (*write_cb)(void *,
const char *), void *@je_@cbopaque, const char *opts); const char *), void *@je_@cbopaque, const char *opts) JEMALLOC_NOTHROW;
JEMALLOC_EXPORT size_t @je_@malloc_usable_size( JEMALLOC_EXPORT size_t @je_@malloc_usable_size(
JEMALLOC_USABLE_SIZE_CONST void *ptr); JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW JEMALLOC_NOTHROW;
#ifdef JEMALLOC_OVERRIDE_MEMALIGN #ifdef JEMALLOC_OVERRIDE_MEMALIGN
JEMALLOC_EXPORT void * @je_@memalign(size_t alignment, size_t size) JEMALLOC_EXPORT void *@je_@memalign(size_t alignment, size_t size)
JEMALLOC_ATTR(malloc); JEMALLOC_ATTR(malloc);
#endif #endif
#ifdef JEMALLOC_OVERRIDE_VALLOC #ifdef JEMALLOC_OVERRIDE_VALLOC
JEMALLOC_EXPORT void * @je_@valloc(size_t size) JEMALLOC_ATTR(malloc); JEMALLOC_EXPORT void *@je_@valloc(size_t size) JEMALLOC_CXX_THROW
JEMALLOC_ATTR(malloc);
#endif #endif

View File

@ -1395,7 +1395,8 @@ imalloc_body(size_t size, tsd_t **tsd, size_t *usize)
return (imalloc(*tsd, size)); return (imalloc(*tsd, size));
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1) JEMALLOC_NOTHROW
je_malloc(size_t size) je_malloc(size_t size)
{ {
void *ret; void *ret;
@ -1529,7 +1530,8 @@ label_oom:
goto label_return; goto label_return;
} }
int JEMALLOC_EXPORT int
JEMALLOC_ATTR(nonnull(1)) JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW
je_posix_memalign(void **memptr, size_t alignment, size_t size) je_posix_memalign(void **memptr, size_t alignment, size_t size)
{ {
int ret = imemalign(memptr, alignment, size, sizeof(void *)); int ret = imemalign(memptr, alignment, size, sizeof(void *));
@ -1538,7 +1540,8 @@ je_posix_memalign(void **memptr, size_t alignment, size_t size)
return (ret); return (ret);
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW
je_aligned_alloc(size_t alignment, size_t size) je_aligned_alloc(size_t alignment, size_t size)
{ {
void *ret; void *ret;
@ -1591,7 +1594,8 @@ icalloc_prof(tsd_t *tsd, size_t usize)
return (p); return (p);
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2) JEMALLOC_NOTHROW
je_calloc(size_t num, size_t size) je_calloc(size_t num, size_t size)
{ {
void *ret; void *ret;
@ -1735,7 +1739,8 @@ isfree(tsd_t *tsd, void *ptr, size_t usize, tcache_t *tcache)
JEMALLOC_VALGRIND_FREE(ptr, rzsize); JEMALLOC_VALGRIND_FREE(ptr, rzsize);
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW
je_realloc(void *ptr, size_t size) je_realloc(void *ptr, size_t size)
{ {
void *ret; void *ret;
@ -1798,7 +1803,8 @@ je_realloc(void *ptr, size_t size)
return (ret); return (ret);
} }
void JEMALLOC_EXPORT void
JEMALLOC_NOTHROW
je_free(void *ptr) je_free(void *ptr)
{ {
@ -1818,7 +1824,8 @@ je_free(void *ptr)
*/ */
#ifdef JEMALLOC_OVERRIDE_MEMALIGN #ifdef JEMALLOC_OVERRIDE_MEMALIGN
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc)
je_memalign(size_t alignment, size_t size) je_memalign(size_t alignment, size_t size)
{ {
void *ret JEMALLOC_CC_SILENCE_INIT(NULL); void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
@ -1830,7 +1837,8 @@ je_memalign(size_t alignment, size_t size)
#endif #endif
#ifdef JEMALLOC_OVERRIDE_VALLOC #ifdef JEMALLOC_OVERRIDE_VALLOC
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc)
je_valloc(size_t size) je_valloc(size_t size)
{ {
void *ret JEMALLOC_CC_SILENCE_INIT(NULL); void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
@ -2024,7 +2032,8 @@ imallocx_no_prof(tsd_t *tsd, size_t size, int flags, size_t *usize)
return (p); return (p);
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1) JEMALLOC_NOTHROW
je_mallocx(size_t size, int flags) je_mallocx(size_t size, int flags)
{ {
tsd_t *tsd; tsd_t *tsd;
@ -2121,7 +2130,8 @@ irallocx_prof(tsd_t *tsd, void *oldptr, size_t old_usize, size_t size,
return (p); return (p);
} }
void * JEMALLOC_EXPORT void *
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW
je_rallocx(void *ptr, size_t size, int flags) je_rallocx(void *ptr, size_t size, int flags)
{ {
void *p; void *p;
@ -2266,7 +2276,8 @@ ixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,
return (usize); return (usize);
} }
size_t JEMALLOC_EXPORT size_t
JEMALLOC_ALLOC_SIZE(2) JEMALLOC_NOTHROW
je_xallocx(void *ptr, size_t size, size_t extra, int flags) je_xallocx(void *ptr, size_t size, size_t extra, int flags)
{ {
tsd_t *tsd; tsd_t *tsd;
@ -2307,7 +2318,8 @@ label_not_resized:
return (usize); return (usize);
} }
size_t JEMALLOC_EXPORT size_t
JEMALLOC_ATTR(pure) JEMALLOC_NOTHROW
je_sallocx(const void *ptr, int flags) je_sallocx(const void *ptr, int flags)
{ {
size_t usize; size_t usize;
@ -2323,7 +2335,8 @@ je_sallocx(const void *ptr, int flags)
return (usize); return (usize);
} }
void JEMALLOC_EXPORT void
JEMALLOC_NOTHROW
je_dallocx(void *ptr, int flags) je_dallocx(void *ptr, int flags)
{ {
tsd_t *tsd; tsd_t *tsd;
@ -2358,7 +2371,8 @@ inallocx(size_t size, int flags)
return (usize); return (usize);
} }
void JEMALLOC_EXPORT void
JEMALLOC_NOTHROW
je_sdallocx(void *ptr, size_t size, int flags) je_sdallocx(void *ptr, size_t size, int flags)
{ {
tsd_t *tsd; tsd_t *tsd;
@ -2383,7 +2397,8 @@ je_sdallocx(void *ptr, size_t size, int flags)
isfree(tsd, ptr, usize, tcache); isfree(tsd, ptr, usize, tcache);
} }
size_t JEMALLOC_EXPORT size_t
JEMALLOC_ATTR(pure) JEMALLOC_NOTHROW
je_nallocx(size_t size, int flags) je_nallocx(size_t size, int flags)
{ {
@ -2395,7 +2410,8 @@ je_nallocx(size_t size, int flags)
return (inallocx(size, flags)); return (inallocx(size, flags));
} }
int JEMALLOC_EXPORT int
JEMALLOC_NOTHROW
je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
size_t newlen) size_t newlen)
{ {
@ -2406,7 +2422,8 @@ je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
return (ctl_byname(name, oldp, oldlenp, newp, newlen)); return (ctl_byname(name, oldp, oldlenp, newp, newlen));
} }
int JEMALLOC_EXPORT int
JEMALLOC_NOTHROW
je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp) je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
{ {
@ -2416,7 +2433,8 @@ je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
return (ctl_nametomib(name, mibp, miblenp)); return (ctl_nametomib(name, mibp, miblenp));
} }
int JEMALLOC_EXPORT int
JEMALLOC_NOTHROW
je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen) void *newp, size_t newlen)
{ {
@ -2427,7 +2445,8 @@ je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen)); return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen));
} }
void JEMALLOC_EXPORT void
JEMALLOC_NOTHROW
je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque, je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
const char *opts) const char *opts)
{ {
@ -2435,7 +2454,8 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
stats_print(write_cb, cbopaque, opts); stats_print(write_cb, cbopaque, opts);
} }
size_t JEMALLOC_EXPORT size_t
JEMALLOC_NOTHROW
je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr)
{ {
size_t ret; size_t ret;

View File

@ -23,7 +23,7 @@ TEST_BEGIN(test_bitmap_init)
bitmap_info_init(&binfo, i); bitmap_info_init(&binfo, i);
{ {
size_t j; size_t j;
bitmap_t *bitmap = malloc(sizeof(bitmap_t) * bitmap_t *bitmap = (bitmap_t *)malloc(sizeof(bitmap_t) *
bitmap_info_ngroups(&binfo)); bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo); bitmap_init(bitmap, &binfo);
@ -46,7 +46,7 @@ TEST_BEGIN(test_bitmap_set)
bitmap_info_init(&binfo, i); bitmap_info_init(&binfo, i);
{ {
size_t j; size_t j;
bitmap_t *bitmap = malloc(sizeof(bitmap_t) * bitmap_t *bitmap = (bitmap_t *)malloc(sizeof(bitmap_t) *
bitmap_info_ngroups(&binfo)); bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo); bitmap_init(bitmap, &binfo);
@ -69,7 +69,7 @@ TEST_BEGIN(test_bitmap_unset)
bitmap_info_init(&binfo, i); bitmap_info_init(&binfo, i);
{ {
size_t j; size_t j;
bitmap_t *bitmap = malloc(sizeof(bitmap_t) * bitmap_t *bitmap = (bitmap_t *)malloc(sizeof(bitmap_t) *
bitmap_info_ngroups(&binfo)); bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo); bitmap_init(bitmap, &binfo);
@ -98,7 +98,7 @@ TEST_BEGIN(test_bitmap_sfu)
bitmap_info_init(&binfo, i); bitmap_info_init(&binfo, i);
{ {
ssize_t j; ssize_t j;
bitmap_t *bitmap = malloc(sizeof(bitmap_t) * bitmap_t *bitmap = (bitmap_t *)malloc(sizeof(bitmap_t) *
bitmap_info_ngroups(&binfo)); bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo); bitmap_init(bitmap, &binfo);

View File

@ -4,7 +4,7 @@ static rtree_node_elm_t *
node_alloc(size_t nelms) node_alloc(size_t nelms)
{ {
return (calloc(nelms, sizeof(rtree_node_elm_t))); return ((rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t)));
} }
static void static void