Replace JEMALLOC_ATTR with various different macros when it makes sense

Theses newly added macros will be used to implement the equivalent under
MSVC. Also, move the definitions to headers, where they make more sense,
and for some, are even more useful there (e.g. malloc).
This commit is contained in:
Mike Hommey 2012-04-30 12:38:29 +02:00 committed by Jason Evans
parent 7cdea3973c
commit da99e31105
8 changed files with 56 additions and 82 deletions

View File

@ -82,8 +82,6 @@
/******************************************************************************/ /******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS #ifdef JEMALLOC_H_EXTERNS
extern void (*je_malloc_message)(void *wcbopaque, const char *s);
int buferror(char *buf, size_t buflen); int buferror(char *buf, size_t buflen);
uintmax_t malloc_strtoumax(const char *nptr, char **endptr, int base); uintmax_t malloc_strtoumax(const char *nptr, char **endptr, int base);

View File

@ -36,35 +36,40 @@ extern "C" {
* namespace management, and should be omitted in application code unless * namespace management, and should be omitted in application code unless
* JEMALLOC_NO_DEMANGLE is defined (see below). * JEMALLOC_NO_DEMANGLE is defined (see below).
*/ */
extern const char *je_malloc_conf; extern JEMALLOC_EXPORT const char *je_malloc_conf;
extern void (*je_malloc_message)(void *, const char *); extern JEMALLOC_EXPORT void (*je_malloc_message)(void *cbopaque,
const char *s);
void *je_malloc(size_t size) JEMALLOC_ATTR(malloc); JEMALLOC_EXPORT void *je_malloc(size_t size) JEMALLOC_ATTR(malloc);
void *je_calloc(size_t num, size_t size) JEMALLOC_ATTR(malloc); JEMALLOC_EXPORT void *je_calloc(size_t num, size_t size)
int je_posix_memalign(void **memptr, size_t alignment, size_t size) JEMALLOC_ATTR(malloc);
JEMALLOC_ATTR(nonnull(1)); JEMALLOC_EXPORT int je_posix_memalign(void **memptr, size_t alignment,
void *je_aligned_alloc(size_t alignment, size_t size) JEMALLOC_ATTR(malloc); size_t size) JEMALLOC_ATTR(nonnull(1));
void *je_realloc(void *ptr, size_t size); JEMALLOC_EXPORT void *je_aligned_alloc(size_t alignment, size_t size)
void je_free(void *ptr); JEMALLOC_ATTR(malloc);
JEMALLOC_EXPORT void *je_realloc(void *ptr, size_t size);
JEMALLOC_EXPORT void je_free(void *ptr);
size_t je_malloc_usable_size(const void *ptr); JEMALLOC_EXPORT size_t je_malloc_usable_size(const void *ptr);
void je_malloc_stats_print(void (*write_cb)(void *, const char *), JEMALLOC_EXPORT void je_malloc_stats_print(void (*write_cb)(void *,
void *je_cbopaque, const char *opts); const char *), void *je_cbopaque, const char *opts);
int je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, JEMALLOC_EXPORT int je_mallctl(const char *name, void *oldp,
size_t newlen);
int je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
int je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
size_t *oldlenp, void *newp, size_t newlen); size_t *oldlenp, void *newp, size_t newlen);
JEMALLOC_EXPORT int je_mallctlnametomib(const char *name, size_t *mibp,
size_t *miblenp);
JEMALLOC_EXPORT int je_mallctlbymib(const size_t *mib, size_t miblen,
void *oldp, size_t *oldlenp, void *newp, size_t newlen);
#ifdef JEMALLOC_EXPERIMENTAL #ifdef JEMALLOC_EXPERIMENTAL
int je_allocm(void **ptr, size_t *rsize, size_t size, int flags) JEMALLOC_EXPORT int je_allocm(void **ptr, size_t *rsize, size_t size,
JEMALLOC_ATTR(nonnull(1));
int je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra,
int flags) JEMALLOC_ATTR(nonnull(1)); int flags) JEMALLOC_ATTR(nonnull(1));
int je_sallocm(const void *ptr, size_t *rsize, int flags) JEMALLOC_EXPORT int je_rallocm(void **ptr, size_t *rsize, size_t size,
size_t extra, int flags) JEMALLOC_ATTR(nonnull(1));
JEMALLOC_EXPORT int je_sallocm(const void *ptr, size_t *rsize, int flags)
JEMALLOC_ATTR(nonnull(1)); JEMALLOC_ATTR(nonnull(1));
int je_dallocm(void *ptr, int flags) JEMALLOC_ATTR(nonnull(1)); JEMALLOC_EXPORT int je_dallocm(void *ptr, int flags)
int je_nallocm(size_t *rsize, size_t size, int flags); JEMALLOC_ATTR(nonnull(1));
JEMALLOC_EXPORT int je_nallocm(size_t *rsize, size_t size, int flags);
#endif #endif
/* /*

View File

@ -104,11 +104,17 @@
/* Defined if __attribute__((...)) syntax is supported. */ /* Defined if __attribute__((...)) syntax is supported. */
#undef JEMALLOC_HAVE_ATTR #undef JEMALLOC_HAVE_ATTR
#ifdef JEMALLOC_HAVE_ATTR #ifdef JEMALLOC_HAVE_ATTR
# define JEMALLOC_CATTR(s, a) __attribute__((s)) # define JEMALLOC_ATTR(s) __attribute__((s))
# define JEMALLOC_ATTR(s) JEMALLOC_CATTR(s,) # define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
# define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))
# define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
# define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
#else #else
# define JEMALLOC_CATTR(s, a) a # define JEMALLOC_ATTR(s)
# define JEMALLOC_ATTR(s) JEMALLOC_CATTR(s,) # define JEMALLOC_EXPORT
# define JEMALLOC_ALIGNED(s)
# define JEMALLOC_SECTION(s)
# define JEMALLOC_NOINLINE
#endif #endif
/* Defined if sbrk() is supported. */ /* Defined if sbrk() is supported. */

View File

@ -7,7 +7,7 @@
ssize_t opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT; ssize_t opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT;
arena_bin_info_t arena_bin_info[NBINS]; arena_bin_info_t arena_bin_info[NBINS];
JEMALLOC_ATTR(aligned(CACHELINE)) JEMALLOC_ALIGNED(CACHELINE)
const uint8_t small_size2bin[] = { const uint8_t small_size2bin[] = {
#define S2B_8(i) i, #define S2B_8(i) i,
#define S2B_16(i) S2B_8(i) S2B_8(i) #define S2B_16(i) S2B_8(i) S2B_8(i)

View File

@ -9,7 +9,7 @@ malloc_tsd_data(, thread_allocated, thread_allocated_t,
THREAD_ALLOCATED_INITIALIZER) THREAD_ALLOCATED_INITIALIZER)
/* Runtime configuration options. */ /* Runtime configuration options. */
const char *je_malloc_conf JEMALLOC_ATTR(visibility("default")); const char *je_malloc_conf;
#ifdef JEMALLOC_DEBUG #ifdef JEMALLOC_DEBUG
bool opt_abort = true; bool opt_abort = true;
# ifdef JEMALLOC_FILL # ifdef JEMALLOC_FILL
@ -787,8 +787,6 @@ malloc_init_hard(void)
* Begin malloc(3)-compatible functions. * Begin malloc(3)-compatible functions.
*/ */
JEMALLOC_ATTR(malloc)
JEMALLOC_ATTR(visibility("default"))
void * void *
je_malloc(size_t size) je_malloc(size_t size)
{ {
@ -938,8 +936,6 @@ label_return:
return (ret); return (ret);
} }
JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ATTR(visibility("default"))
int int
je_posix_memalign(void **memptr, size_t alignment, size_t size) je_posix_memalign(void **memptr, size_t alignment, size_t size)
{ {
@ -949,8 +945,6 @@ je_posix_memalign(void **memptr, size_t alignment, size_t size)
return (ret); return (ret);
} }
JEMALLOC_ATTR(malloc)
JEMALLOC_ATTR(visibility("default"))
void * void *
je_aligned_alloc(size_t alignment, size_t size) je_aligned_alloc(size_t alignment, size_t size)
{ {
@ -966,8 +960,6 @@ je_aligned_alloc(size_t alignment, size_t size)
return (ret); return (ret);
} }
JEMALLOC_ATTR(malloc)
JEMALLOC_ATTR(visibility("default"))
void * void *
je_calloc(size_t num, size_t size) je_calloc(size_t num, size_t size)
{ {
@ -1043,7 +1035,6 @@ label_return:
return (ret); return (ret);
} }
JEMALLOC_ATTR(visibility("default"))
void * void *
je_realloc(void *ptr, size_t size) je_realloc(void *ptr, size_t size)
{ {
@ -1191,7 +1182,6 @@ label_return:
return (ret); return (ret);
} }
JEMALLOC_ATTR(visibility("default"))
void void
je_free(void *ptr) je_free(void *ptr)
{ {
@ -1226,8 +1216,6 @@ je_free(void *ptr)
*/ */
#ifdef JEMALLOC_OVERRIDE_MEMALIGN #ifdef JEMALLOC_OVERRIDE_MEMALIGN
JEMALLOC_ATTR(malloc)
JEMALLOC_ATTR(visibility("default"))
void * void *
je_memalign(size_t alignment, size_t size) je_memalign(size_t alignment, size_t size)
{ {
@ -1239,8 +1227,6 @@ je_memalign(size_t alignment, size_t size)
#endif #endif
#ifdef JEMALLOC_OVERRIDE_VALLOC #ifdef JEMALLOC_OVERRIDE_VALLOC
JEMALLOC_ATTR(malloc)
JEMALLOC_ATTR(visibility("default"))
void * void *
je_valloc(size_t size) je_valloc(size_t size)
{ {
@ -1269,17 +1255,12 @@ je_valloc(size_t size)
* passed an extra argument for the caller return address, which will be * passed an extra argument for the caller return address, which will be
* ignored. * ignored.
*/ */
JEMALLOC_ATTR(visibility("default")) JEMALLOC_EXPORT void (* const __free_hook)(void *ptr) = je_free;
void (* const __free_hook)(void *ptr) = je_free; JEMALLOC_EXPORT void *(* const __malloc_hook)(size_t size) = je_malloc;
JEMALLOC_EXPORT void *(* const __realloc_hook)(void *ptr, size_t size) =
JEMALLOC_ATTR(visibility("default")) je_realloc;
void *(* const __malloc_hook)(size_t size) = je_malloc; JEMALLOC_EXPORT void *(* const __memalign_hook)(size_t alignment, size_t size) =
je_memalign;
JEMALLOC_ATTR(visibility("default"))
void *(* const __realloc_hook)(void *ptr, size_t size) = je_realloc;
JEMALLOC_ATTR(visibility("default"))
void *(* const __memalign_hook)(size_t alignment, size_t size) = je_memalign;
#endif #endif
/* /*
@ -1290,7 +1271,6 @@ void *(* const __memalign_hook)(size_t alignment, size_t size) = je_memalign;
* Begin non-standard functions. * Begin non-standard functions.
*/ */
JEMALLOC_ATTR(visibility("default"))
size_t size_t
je_malloc_usable_size(const void *ptr) je_malloc_usable_size(const void *ptr)
{ {
@ -1306,7 +1286,6 @@ je_malloc_usable_size(const void *ptr)
return (ret); return (ret);
} }
JEMALLOC_ATTR(visibility("default"))
void void
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)
@ -1315,7 +1294,6 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
stats_print(write_cb, cbopaque, opts); stats_print(write_cb, cbopaque, opts);
} }
JEMALLOC_ATTR(visibility("default"))
int int
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)
@ -1327,7 +1305,6 @@ 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));
} }
JEMALLOC_ATTR(visibility("default"))
int int
je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp) je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
{ {
@ -1338,7 +1315,6 @@ je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
return (ctl_nametomib(name, mibp, miblenp)); return (ctl_nametomib(name, mibp, miblenp));
} }
JEMALLOC_ATTR(visibility("default"))
int int
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)
@ -1374,8 +1350,6 @@ iallocm(size_t usize, size_t alignment, bool zero)
return (imalloc(usize)); return (imalloc(usize));
} }
JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ATTR(visibility("default"))
int int
je_allocm(void **ptr, size_t *rsize, size_t size, int flags) je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
{ {
@ -1444,8 +1418,6 @@ label_oom:
return (ALLOCM_ERR_OOM); return (ALLOCM_ERR_OOM);
} }
JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ATTR(visibility("default"))
int int
je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags) je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
{ {
@ -1555,8 +1527,6 @@ label_oom:
return (ALLOCM_ERR_OOM); return (ALLOCM_ERR_OOM);
} }
JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ATTR(visibility("default"))
int int
je_sallocm(const void *ptr, size_t *rsize, int flags) je_sallocm(const void *ptr, size_t *rsize, int flags)
{ {
@ -1576,8 +1546,6 @@ je_sallocm(const void *ptr, size_t *rsize, int flags)
return (ALLOCM_SUCCESS); return (ALLOCM_SUCCESS);
} }
JEMALLOC_ATTR(nonnull(1))
JEMALLOC_ATTR(visibility("default"))
int int
je_dallocm(void *ptr, int flags) je_dallocm(void *ptr, int flags)
{ {
@ -1605,7 +1573,6 @@ je_dallocm(void *ptr, int flags)
return (ALLOCM_SUCCESS); return (ALLOCM_SUCCESS);
} }
JEMALLOC_ATTR(visibility("default"))
int int
je_nallocm(size_t *rsize, size_t size, int flags) je_nallocm(size_t *rsize, size_t size, int flags)
{ {
@ -1641,8 +1608,7 @@ je_nallocm(size_t *rsize, size_t size, int flags)
void void
jemalloc_prefork(void) jemalloc_prefork(void)
#else #else
JEMALLOC_ATTR(visibility("default")) JEMALLOC_EXPORT void
void
_malloc_prefork(void) _malloc_prefork(void)
#endif #endif
{ {
@ -1663,8 +1629,7 @@ _malloc_prefork(void)
void void
jemalloc_postfork_parent(void) jemalloc_postfork_parent(void)
#else #else
JEMALLOC_ATTR(visibility("default")) JEMALLOC_EXPORT void
void
_malloc_postfork(void) _malloc_postfork(void)
#endif #endif
{ {

View File

@ -48,8 +48,7 @@ pthread_create_once(void)
isthreaded = true; isthreaded = true;
} }
JEMALLOC_ATTR(visibility("default")) JEMALLOC_EXPORT int
int
pthread_create(pthread_t *__restrict thread, pthread_create(pthread_t *__restrict thread,
const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),
void *__restrict arg) void *__restrict arg)
@ -72,6 +71,7 @@ int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
bool bool
malloc_mutex_init(malloc_mutex_t *mutex) malloc_mutex_init(malloc_mutex_t *mutex)
{ {
#ifdef _WIN32 #ifdef _WIN32
if (!InitializeCriticalSectionAndSpinCount(&mutex->lock, if (!InitializeCriticalSectionAndSpinCount(&mutex->lock,
_CRT_SPINCOUNT)) _CRT_SPINCOUNT))
@ -98,7 +98,6 @@ malloc_mutex_init(malloc_mutex_t *mutex)
return (true); return (true);
} }
pthread_mutexattr_destroy(&attr); pthread_mutexattr_destroy(&attr);
#endif #endif
return (false); return (false);
} }

View File

@ -32,7 +32,9 @@ malloc_tsd_no_cleanup(void *arg)
} }
#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32) #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
JEMALLOC_ATTR(visibility("default")) #ifndef _WIN32
JEMALLOC_EXPORT
#endif
void void
_malloc_thread_cleanup(void) _malloc_thread_cleanup(void)
{ {
@ -91,7 +93,7 @@ _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
return (true); return (true);
} }
JEMALLOC_ATTR(section(".CRT$XLY")) JEMALLOC_ATTR(used) JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL, static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL,
DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
#endif #endif

View File

@ -40,8 +40,7 @@ static char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,
/******************************************************************************/ /******************************************************************************/
/* malloc_message() setup. */ /* malloc_message() setup. */
JEMALLOC_CATTR(visibility("hidden"), static) static void
void
wrtmessage(void *cbopaque, const char *s) wrtmessage(void *cbopaque, const char *s)
{ {
@ -57,8 +56,8 @@ wrtmessage(void *cbopaque, const char *s)
#endif #endif
} }
void (*je_malloc_message)(void *, const char *s) JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s) =
JEMALLOC_ATTR(visibility("default")) = wrtmessage; wrtmessage;
/* /*
* glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so