Add --with-jemalloc-prefix, which supports API mangling.

Rename jemalloc_options-->malloc_options and jemalloc_message-->malloc_message.
This commit is contained in:
Jason Evans 2009-12-29 00:09:15 -08:00
parent bca042dfeb
commit 90895cf859
7 changed files with 171 additions and 119 deletions

View File

@ -26,6 +26,11 @@ any of the following arguments (not a definitive list) to 'configure':
Embed one or more library paths, so that Crux's internal shared library can Embed one or more library paths, so that Crux's internal shared library can
find the libraries it is linked to. This works only on ELF-based systems. find the libraries it is linked to. This works only on ELF-based systems.
--with-jemalloc-prefix=<prefix>
Prefix all public APIs with <prefix>, so that, for example, malloc()
becomes <prefix>malloc(). This makes it possible to use jemalloc at the
same time as the system allocator.
--enable-debug --enable-debug
Enable assertions and validation code. This incurs a substantial Enable assertions and validation code. This incurs a substantial
performance hit, but is very useful during application development. performance hit, but is very useful during application development.

View File

@ -205,7 +205,7 @@ AC_SUBST([RPATH])
dnl Support optional additions to rpath. dnl Support optional additions to rpath.
AC_ARG_WITH([rpath], AC_ARG_WITH([rpath],
[AS_HELP_STRING([--with-rpath=<rpath>], [colon-separated rpath (ELF systems only)])], [AS_HELP_STRING([--with-rpath=<rpath>], [Colon-separated rpath (ELF systems only)])],
if test "x$with_rpath" = "xno" ; then if test "x$with_rpath" = "xno" ; then
RPATH_EXTRA= RPATH_EXTRA=
else else
@ -234,6 +234,30 @@ AC_PATH_PROG([AR], [ar], , [$PATH])
AC_PATH_PROG([LD], [ld], , [$PATH]) AC_PATH_PROG([LD], [ld], , [$PATH])
AC_PATH_PROG([AUTOCONF], [autoconf], , [$PATH]) AC_PATH_PROG([AUTOCONF], [autoconf], , [$PATH])
dnl Do not prefix public APIs by default.
AC_ARG_WITH([jemalloc_prefix],
[AS_HELP_STRING([--with-jemalloc-prefix=<prefix>], [Prefix to prepend to all public APIs])],
if test "x$with_jemalloc_prefix" = "xno" ; then
JEMALLOC_PREFIX=
else
JEMALLOC_PREFIX="$with_jemalloc_prefix"
fi,
JEMALLOC_PREFIX=
)
if test "x$JEMALLOC_PREFIX" != "x" ; then
AC_DEFINE([JEMALLOC_PREFIX], [ ])
jemalloc_prefix=$JEMALLOC_PREFIX
AC_SUBST([jemalloc_prefix])
AC_DEFINE_UNQUOTED([malloc], [${JEMALLOC_PREFIX}malloc])
AC_DEFINE_UNQUOTED([calloc], [${JEMALLOC_PREFIX}calloc])
AC_DEFINE_UNQUOTED([posix_memalign], [${JEMALLOC_PREFIX}posix_memalign])
AC_DEFINE_UNQUOTED([realloc], [${JEMALLOC_PREFIX}realloc])
AC_DEFINE_UNQUOTED([free], [${JEMALLOC_PREFIX}free])
AC_DEFINE_UNQUOTED([malloc_usable_size], [${JEMALLOC_PREFIX}malloc_usable_size])
AC_DEFINE_UNQUOTED([malloc_options], [${JEMALLOC_PREFIX}malloc_options])
AC_DEFINE_UNQUOTED([malloc_message], [${JEMALLOC_PREFIX}malloc_message])
fi
dnl Do not compile with debugging by default. dnl Do not compile with debugging by default.
AC_ARG_ENABLE([debug], AC_ARG_ENABLE([debug],
[AS_HELP_STRING([--enable-debug], [Build debugging code])], [AS_HELP_STRING([--enable-debug], [Build debugging code])],
@ -601,6 +625,7 @@ AC_MSG_RESULT([abs_objroot : ${abs_objroot}])
AC_MSG_RESULT([]) AC_MSG_RESULT([])
AC_MSG_RESULT([bins : ${bins}]) AC_MSG_RESULT([bins : ${bins}])
AC_MSG_RESULT([]) AC_MSG_RESULT([])
AC_MSG_RESULT([JEMALLOC_PREFIX : ${JEMALLOC_PREFIX}])
AC_MSG_RESULT([autogen : ${enable_autogen}]) AC_MSG_RESULT([autogen : ${enable_autogen}])
AC_MSG_RESULT([debug : ${enable_debug}]) AC_MSG_RESULT([debug : ${enable_debug}])
AC_MSG_RESULT([stats : ${enable_stats}]) AC_MSG_RESULT([stats : ${enable_stats}])

View File

@ -35,7 +35,7 @@
.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD: head/lib/libc/stdlib/malloc.3 182225 2008-08-27 02:00:53Z jasone $ .\" $FreeBSD: head/lib/libc/stdlib/malloc.3 182225 2008-08-27 02:00:53Z jasone $
.\" .\"
.Dd June 22, 2009 .Dd November 13, 2009
.Dt JEMALLOC 3 .Dt JEMALLOC 3
.Os .Os
.Sh NAME .Sh NAME
@ -45,28 +45,28 @@
.Lb libjemalloc .Lb libjemalloc
.Sh SYNOPSIS .Sh SYNOPSIS
.In stdlib.h .In stdlib.h
.Ft void *
.Fn malloc "size_t size"
.Ft void *
.Fn calloc "size_t number" "size_t size"
.Ft int
.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
.Ft void *
.Fn realloc "void *ptr" "size_t size"
.Ft void
.Fn free "void *ptr"
.In jemalloc.h .In jemalloc.h
.Ft size_t .Ft void *
.Fn malloc_usable_size "const void *ptr" .Fn @jemalloc_prefix@malloc "size_t size"
.Ft const char * .Ft void *
.Va jemalloc_options ; .Fn @jemalloc_prefix@calloc "size_t number" "size_t size"
.Ft int
.Fn @jemalloc_prefix@posix_memalign "void **ptr" "size_t alignment" "size_t size"
.Ft void *
.Fn @jemalloc_prefix@realloc "void *ptr" "size_t size"
.Ft void .Ft void
.Fo \*(lp*jemalloc_message\*(rp .Fn @jemalloc_prefix@free "void *ptr"
.Ft size_t
.Fn @jemalloc_prefix@malloc_usable_size "const void *ptr"
.Ft const char *
.Va @jemalloc_prefix@malloc_options ;
.Ft void
.Fo \*(lp*@jemalloc_prefix@malloc_message\*(rp
.Fa "const char *p1" "const char *p2" "const char *p3" "const char *p4" .Fa "const char *p1" "const char *p2" "const char *p3" "const char *p4"
.Fc .Fc
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn malloc .Fn @jemalloc_prefix@malloc
function allocates function allocates
.Fa size .Fa size
bytes of uninitialized memory. bytes of uninitialized memory.
@ -75,7 +75,7 @@ The allocated space is suitably aligned
for storage of any type of object. for storage of any type of object.
.Pp .Pp
The The
.Fn calloc .Fn @jemalloc_prefix@calloc
function allocates space for function allocates space for
.Fa number .Fa number
objects, objects,
@ -83,14 +83,14 @@ each
.Fa size .Fa size
bytes in length. bytes in length.
The result is identical to calling The result is identical to calling
.Fn malloc .Fn @jemalloc_prefix@malloc
with an argument of with an argument of
.Dq "number * size" , .Dq "number * size" ,
with the exception that the allocated memory is explicitly initialized with the exception that the allocated memory is explicitly initialized
to zero bytes. to zero bytes.
.Pp .Pp
The The
.Fn posix_memalign .Fn @jemalloc_prefix@posix_memalign
function allocates function allocates
.Fa size .Fa size
bytes of memory such that the allocation's base address is an even multiple of bytes of memory such that the allocation's base address is an even multiple of
@ -103,7 +103,7 @@ must be a power of 2 at least as large as
.Fn sizeof "void *" . .Fn sizeof "void *" .
.Pp .Pp
The The
.Fn realloc .Fn @jemalloc_prefix@realloc
function changes the size of the previously allocated memory referenced by function changes the size of the previously allocated memory referenced by
.Fa ptr .Fa ptr
to to
@ -117,7 +117,7 @@ Upon success, the memory referenced by
.Fa ptr .Fa ptr
is freed and a pointer to the newly allocated memory is returned. is freed and a pointer to the newly allocated memory is returned.
Note that Note that
.Fn realloc .Fn @jemalloc_prefix@realloc
may move the memory allocation, resulting in a different return value than may move the memory allocation, resulting in a different return value than
.Fa ptr . .Fa ptr .
If If
@ -125,13 +125,13 @@ If
is is
.Dv NULL , .Dv NULL ,
the the
.Fn realloc .Fn @jemalloc_prefix@realloc
function behaves identically to function behaves identically to
.Fn malloc .Fn @jemalloc_prefix@malloc
for the specified size. for the specified size.
.Pp .Pp
The The
.Fn free .Fn @jemalloc_prefix@free
function causes the allocated memory referenced by function causes the allocated memory referenced by
.Fa ptr .Fa ptr
to be made available for future allocations. to be made available for future allocations.
@ -142,18 +142,18 @@ is
no action occurs. no action occurs.
.Pp .Pp
The The
.Fn malloc_usable_size .Fn @jemalloc_prefix@malloc_usable_size
function returns the usable size of the allocation pointed to by function returns the usable size of the allocation pointed to by
.Fa ptr . .Fa ptr .
The return value may be larger than the size that was requested during The return value may be larger than the size that was requested during
allocation. allocation.
The The
.Fn malloc_usable_size .Fn @jemalloc_prefix@malloc_usable_size
function is not a mechanism for in-place function is not a mechanism for in-place
.Fn realloc ; .Fn @jemalloc_prefix@realloc ;
rather it is provided solely as a tool for introspection purposes. rather it is provided solely as a tool for introspection purposes.
Any discrepancy between the requested allocation size and the size reported by Any discrepancy between the requested allocation size and the size reported by
.Fn malloc_usable_size .Fn @jemalloc_prefix@malloc_usable_size
should not be depended on, since such behavior is entirely should not be depended on, since such behavior is entirely
implementation-dependent. implementation-dependent.
.Sh TUNING .Sh TUNING
@ -168,7 +168,7 @@ of the file referenced by the symbolic link named
the value of the environment variable the value of the environment variable
.Ev JEMALLOC_OPTIONS , .Ev JEMALLOC_OPTIONS ,
and the string pointed to by the global variable and the string pointed to by the global variable
.Va jemalloc_options .Va @jemalloc_prefix@malloc_options
will be interpreted, in that order, from left to right as flags. will be interpreted, in that order, from left to right as flags.
.Pp .Pp
Each flag is a single letter, optionally prefixed by a non-negative base 10 Each flag is a single letter, optionally prefixed by a non-negative base 10
@ -236,14 +236,14 @@ will prevent any dirty unused pages from accumulating.
@roff_mag@@roff_tls@option for related tuning information. @roff_mag@@roff_tls@option for related tuning information.
@roff_fill@.It J @roff_fill@.It J
@roff_fill@Each byte of new memory allocated by @roff_fill@Each byte of new memory allocated by
@roff_fill@.Fn malloc @roff_fill@.Fn @jemalloc_prefix@malloc
@roff_fill@or @roff_fill@or
@roff_fill@.Fn realloc @roff_fill@.Fn @jemalloc_prefix@realloc
@roff_fill@will be initialized to 0xa5. @roff_fill@will be initialized to 0xa5.
@roff_fill@All memory returned by @roff_fill@All memory returned by
@roff_fill@.Fn free @roff_fill@.Fn @jemalloc_prefix@free
@roff_fill@or @roff_fill@or
@roff_fill@.Fn realloc @roff_fill@.Fn @jemalloc_prefix@realloc
@roff_fill@will be initialized to 0x5a. @roff_fill@will be initialized to 0x5a.
@roff_fill@This is intended for debugging and will impact performance @roff_fill@This is intended for debugging and will impact performance
@roff_fill@negatively. @roff_fill@negatively.
@ -292,7 +292,7 @@ The default value is 128 bytes.
@roff_mag@@roff_tls@the expense of increased memory usage. @roff_mag@@roff_tls@the expense of increased memory usage.
@roff_stats@.It U @roff_stats@.It U
@roff_stats@Generate a verbose trace log via @roff_stats@Generate a verbose trace log via
@roff_stats@.Fn jemalloc_message @roff_stats@.Fn @jemalloc_prefix@malloc_message
@roff_stats@for all allocation operations. @roff_stats@for all allocation operations.
@roff_sysv@.It V @roff_sysv@.It V
@roff_sysv@Attempting to allocate zero bytes will return a @roff_sysv@Attempting to allocate zero bytes will return a
@ -313,16 +313,16 @@ The default value is 128 bytes.
@roff_xmalloc@This option should be set at compile time by including the @roff_xmalloc@This option should be set at compile time by including the
@roff_xmalloc@following in the source code: @roff_xmalloc@following in the source code:
@roff_xmalloc@.Bd -literal -offset indent @roff_xmalloc@.Bd -literal -offset indent
@roff_xmalloc@jemalloc_options = "X"; @roff_xmalloc@@jemalloc_prefix@malloc_options = "X";
@roff_xmalloc@.Ed @roff_xmalloc@.Ed
@roff_fill@.It Z @roff_fill@.It Z
@roff_fill@Each byte of new memory allocated by @roff_fill@Each byte of new memory allocated by
@roff_fill@.Fn malloc @roff_fill@.Fn @jemalloc_prefix@malloc
@roff_fill@or @roff_fill@or
@roff_fill@.Fn realloc @roff_fill@.Fn @jemalloc_prefix@realloc
@roff_fill@will be initialized to 0. @roff_fill@will be initialized to 0.
@roff_fill@Note that this initialization only happens once for each byte, so @roff_fill@Note that this initialization only happens once for each byte, so
@roff_fill@.Fn realloc @roff_fill@.Fn @jemalloc_prefix@realloc
@roff_fill@calls do not zero memory that was previously allocated. @roff_fill@calls do not zero memory that was previously allocated.
@roff_fill@This is intended for debugging and will impact performance @roff_fill@This is intended for debugging and will impact performance
@roff_fill@negatively. @roff_fill@negatively.
@ -467,7 +467,7 @@ If the
option is set, all warnings are treated as errors. option is set, all warnings are treated as errors.
.Pp .Pp
The The
.Va jemalloc_message .Va @jemalloc_prefix@malloc_message
variable allows the programmer to override the function which emits variable allows the programmer to override the function which emits
the text strings forming the errors and warnings if for some reason the text strings forming the errors and warnings if for some reason
the the
@ -480,9 +480,9 @@ All messages are prefixed by
.Dq <jemalloc>: . .Dq <jemalloc>: .
.Sh RETURN VALUES .Sh RETURN VALUES
The The
.Fn malloc .Fn @jemalloc_prefix@malloc
and and
.Fn calloc .Fn @jemalloc_prefix@calloc
functions return a pointer to the allocated memory if successful; otherwise functions return a pointer to the allocated memory if successful; otherwise
a a
.Dv NULL .Dv NULL
@ -492,10 +492,10 @@ is set to
.Er ENOMEM . .Er ENOMEM .
.Pp .Pp
The The
.Fn posix_memalign .Fn @jemalloc_prefix@posix_memalign
function returns the value 0 if successful; otherwise it returns an error value. function returns the value 0 if successful; otherwise it returns an error value.
The The
.Fn posix_memalign .Fn @jemalloc_prefix@posix_memalign
function will fail if: function will fail if:
.Bl -tag -width Er .Bl -tag -width Er
.It Bq Er EINVAL .It Bq Er EINVAL
@ -508,7 +508,7 @@ Memory allocation error.
.El .El
.Pp .Pp
The The
.Fn realloc .Fn @jemalloc_prefix@realloc
function returns a pointer, possibly identical to function returns a pointer, possibly identical to
.Fa ptr , .Fa ptr ,
to the allocated memory to the allocated memory
@ -520,16 +520,16 @@ is set to
.Er ENOMEM .Er ENOMEM
if the error was the result of an allocation failure. if the error was the result of an allocation failure.
The The
.Fn realloc .Fn @jemalloc_prefix@realloc
function always leaves the original buffer intact function always leaves the original buffer intact
when an error occurs. when an error occurs.
.Pp .Pp
The The
.Fn free .Fn @jemalloc_prefix@free
function returns no value. function returns no value.
.Pp .Pp
The The
.Fn malloc_usable_size .Fn @jemalloc_prefix@malloc_usable_size
function returns the usable size of the allocation pointed to by function returns the usable size of the allocation pointed to by
.Fa ptr . .Fa ptr .
.Sh ENVIRONMENT .Sh ENVIRONMENT
@ -552,7 +552,7 @@ ln -s 'A' /etc/jemalloc.conf
To specify in the source that a program does no return value checking To specify in the source that a program does no return value checking
on calls to these functions: on calls to these functions:
.Bd -literal -offset indent .Bd -literal -offset indent
jemalloc_options = "X"; @jemalloc_prefix@malloc_options = "X";
.Ed .Ed
.Sh SEE ALSO .Sh SEE ALSO
.Xr madvise 2 , .Xr madvise 2 ,
@ -563,15 +563,15 @@ jemalloc_options = "X";
.Xr getpagesize 3 .Xr getpagesize 3
.Sh STANDARDS .Sh STANDARDS
The The
.Fn malloc , .Fn @jemalloc_prefix@malloc ,
.Fn calloc , .Fn @jemalloc_prefix@calloc ,
.Fn realloc .Fn @jemalloc_prefix@realloc
and and
.Fn free .Fn @jemalloc_prefix@free
functions conform to functions conform to
.St -isoC . .St -isoC .
.Pp .Pp
The The
.Fn posix_memalign .Fn @jemalloc_prefix@posix_memalign
function conforms to function conforms to
.St -p1003.1-2001 . .St -p1003.1-2001 .

View File

@ -137,8 +137,6 @@
******************************************************************************* *******************************************************************************
*/ */
#include "jemalloc_defs.h"
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdlib/malloc.c,v 1.183 2008/12/01 10:20:59 jasone Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdlib/malloc.c,v 1.183 2008/12/01 10:20:59 jasone Exp $");
#endif #endif
@ -167,6 +165,10 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/malloc.c,v 1.183 2008/12/01 10:20:59 jas
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <pthread.h> #include <pthread.h>
#define JEMALLOC_MANGLE
#include "jemalloc.h"
#ifdef JEMALLOC_LAZY_LOCK #ifdef JEMALLOC_LAZY_LOCK
#include <dlfcn.h> #include <dlfcn.h>
#endif #endif
@ -1045,7 +1047,7 @@ static chunk_stats_t stats_chunks;
/* /*
* Runtime configuration options. * Runtime configuration options.
*/ */
const char *jemalloc_options; const char *malloc_options;
#ifdef JEMALLOC_DEBUG #ifdef JEMALLOC_DEBUG
static bool opt_abort = true; static bool opt_abort = true;
@ -1225,7 +1227,7 @@ wrtmessage(const char *p1, const char *p2, const char *p3, const char *p4)
return; return;
} }
void (*jemalloc_message)(const char *p1, const char *p2, const char *p3, void (*malloc_message)(const char *p1, const char *p2, const char *p3,
const char *p4) = wrtmessage; const char *p4) = wrtmessage;
/* /*
@ -1259,9 +1261,9 @@ umax2s(uintmax_t x, char *s)
# define assert(e) do { \ # define assert(e) do { \
if (!(e)) { \ if (!(e)) { \
char line_buf[UMAX2S_BUFSIZE]; \ char line_buf[UMAX2S_BUFSIZE]; \
jemalloc_message("<jemalloc>: ", __FILE__, ":", \ malloc_message("<jemalloc>: ", __FILE__, ":", \
umax2s(__LINE__, line_buf)); \ umax2s(__LINE__, line_buf)); \
jemalloc_message(": Failed assertion: ", "\"", #e, \ malloc_message(": Failed assertion: ", "\"", #e, \
"\"\n"); \ "\"\n"); \
abort(); \ abort(); \
} \ } \
@ -1308,7 +1310,7 @@ malloc_printf(const char *format, ...)
va_start(ap, format); va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap); vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap); va_end(ap);
jemalloc_message(buf, "", "", ""); malloc_message(buf, "", "", "");
} }
#endif #endif
@ -1328,7 +1330,7 @@ get_pthread_create_fptr(void)
pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create"); pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
if (pthread_create_fptr == NULL) { if (pthread_create_fptr == NULL) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in dlsym(RTLD_NEXT, \"pthread_create\")\n", "", ": Error in dlsym(RTLD_NEXT, \"pthread_create\")\n", "",
""); "");
abort(); abort();
@ -1864,7 +1866,7 @@ pages_map(void *addr, size_t size)
char buf[STRERROR_BUF]; char buf[STRERROR_BUF];
strerror_r(errno, buf, sizeof(buf)); strerror_r(errno, buf, sizeof(buf));
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in munmap(): ", buf, "\n"); ": Error in munmap(): ", buf, "\n");
if (opt_abort) if (opt_abort)
abort(); abort();
@ -1885,7 +1887,7 @@ pages_unmap(void *addr, size_t size)
char buf[STRERROR_BUF]; char buf[STRERROR_BUF];
strerror_r(errno, buf, sizeof(buf)); strerror_r(errno, buf, sizeof(buf));
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in munmap(): ", buf, "\n"); ": Error in munmap(): ", buf, "\n");
if (opt_abort) if (opt_abort)
abort(); abort();
@ -4335,7 +4337,7 @@ arenas_extend(unsigned ind)
* by using arenas[0]. In practice, this is an extremely unlikely * by using arenas[0]. In practice, this is an extremely unlikely
* failure. * failure.
*/ */
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error initializing arena\n", "", ""); ": Error initializing arena\n", "", "");
if (opt_abort) if (opt_abort)
abort(); abort();
@ -4647,79 +4649,79 @@ malloc_print_stats(void)
if (opt_print_stats) { if (opt_print_stats) {
char s[UMAX2S_BUFSIZE]; char s[UMAX2S_BUFSIZE];
jemalloc_message("___ Begin jemalloc statistics ___\n", "", "", malloc_message("___ Begin jemalloc statistics ___\n", "", "",
""); "");
jemalloc_message("Assertions ", malloc_message("Assertions ",
#ifdef NDEBUG #ifdef NDEBUG
"disabled", "disabled",
#else #else
"enabled", "enabled",
#endif #endif
"\n", ""); "\n", "");
jemalloc_message("Boolean JEMALLOC_OPTIONS: ", malloc_message("Boolean JEMALLOC_OPTIONS: ",
opt_abort ? "A" : "a", "", ""); opt_abort ? "A" : "a", "", "");
#ifdef JEMALLOC_DSS #ifdef JEMALLOC_DSS
jemalloc_message(opt_dss ? "D" : "d", "", "", ""); malloc_message(opt_dss ? "D" : "d", "", "", "");
#endif #endif
#ifdef JEMALLOC_MAG #ifdef JEMALLOC_MAG
jemalloc_message(opt_mag ? "G" : "g", "", "", ""); malloc_message(opt_mag ? "G" : "g", "", "", "");
#endif #endif
#ifdef JEMALLOC_FILL #ifdef JEMALLOC_FILL
jemalloc_message(opt_junk ? "J" : "j", "", "", ""); malloc_message(opt_junk ? "J" : "j", "", "", "");
#endif #endif
#ifdef JEMALLOC_DSS #ifdef JEMALLOC_DSS
jemalloc_message(opt_mmap ? "M" : "m", "", "", ""); malloc_message(opt_mmap ? "M" : "m", "", "", "");
#endif #endif
jemalloc_message("P", "", "", ""); malloc_message("P", "", "", "");
#ifdef JEMALLOC_STATS #ifdef JEMALLOC_STATS
jemalloc_message(opt_utrace ? "U" : "u", "", "", ""); malloc_message(opt_utrace ? "U" : "u", "", "", "");
#endif #endif
#ifdef JEMALLOC_SYSV #ifdef JEMALLOC_SYSV
jemalloc_message(opt_sysv ? "V" : "v", "", "", ""); malloc_message(opt_sysv ? "V" : "v", "", "", "");
#endif #endif
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
jemalloc_message(opt_xmalloc ? "X" : "x", "", "", ""); malloc_message(opt_xmalloc ? "X" : "x", "", "", "");
#endif #endif
#ifdef JEMALLOC_FILL #ifdef JEMALLOC_FILL
jemalloc_message(opt_zero ? "Z" : "z", "", "", ""); malloc_message(opt_zero ? "Z" : "z", "", "", "");
#endif #endif
jemalloc_message("\n", "", "", ""); malloc_message("\n", "", "", "");
jemalloc_message("CPUs: ", umax2s(ncpus, s), "\n", ""); malloc_message("CPUs: ", umax2s(ncpus, s), "\n", "");
jemalloc_message("Max arenas: ", umax2s(narenas, s), "\n", ""); malloc_message("Max arenas: ", umax2s(narenas, s), "\n", "");
#ifdef JEMALLOC_BALANCE #ifdef JEMALLOC_BALANCE
jemalloc_message("Arena balance threshold: ", malloc_message("Arena balance threshold: ",
umax2s(opt_balance_threshold, s), "\n", ""); umax2s(opt_balance_threshold, s), "\n", "");
#endif #endif
jemalloc_message("Pointer size: ", umax2s(sizeof(void *), s), malloc_message("Pointer size: ", umax2s(sizeof(void *), s),
"\n", ""); "\n", "");
jemalloc_message("Quantum size: ", umax2s(QUANTUM, s), "\n", malloc_message("Quantum size: ", umax2s(QUANTUM, s), "\n",
""); "");
jemalloc_message("Cacheline size (assumed): ", malloc_message("Cacheline size (assumed): ",
umax2s(CACHELINE, s), "\n", ""); umax2s(CACHELINE, s), "\n", "");
#ifdef JEMALLOC_TINY #ifdef JEMALLOC_TINY
jemalloc_message("Tiny 2^n-spaced sizes: [", umax2s((1U << malloc_message("Tiny 2^n-spaced sizes: [", umax2s((1U <<
TINY_MIN_2POW), s), "..", ""); TINY_MIN_2POW), s), "..", "");
jemalloc_message(umax2s((qspace_min >> 1), s), "]\n", "", ""); malloc_message(umax2s((qspace_min >> 1), s), "]\n", "", "");
#endif #endif
jemalloc_message("Quantum-spaced sizes: [", umax2s(qspace_min, malloc_message("Quantum-spaced sizes: [", umax2s(qspace_min,
s), "..", ""); s), "..", "");
jemalloc_message(umax2s(qspace_max, s), "]\n", "", ""); malloc_message(umax2s(qspace_max, s), "]\n", "", "");
jemalloc_message("Cacheline-spaced sizes: [", malloc_message("Cacheline-spaced sizes: [",
umax2s(cspace_min, s), "..", ""); umax2s(cspace_min, s), "..", "");
jemalloc_message(umax2s(cspace_max, s), "]\n", "", ""); malloc_message(umax2s(cspace_max, s), "]\n", "", "");
jemalloc_message("Subpage-spaced sizes: [", umax2s(sspace_min, malloc_message("Subpage-spaced sizes: [", umax2s(sspace_min,
s), "..", ""); s), "..", "");
jemalloc_message(umax2s(sspace_max, s), "]\n", "", ""); malloc_message(umax2s(sspace_max, s), "]\n", "", "");
#ifdef JEMALLOC_MAG #ifdef JEMALLOC_MAG
jemalloc_message("Rounds per magazine: ", umax2s(max_rounds, malloc_message("Rounds per magazine: ", umax2s(max_rounds,
s), "\n", ""); s), "\n", "");
#endif #endif
jemalloc_message("Max dirty pages per arena: ", malloc_message("Max dirty pages per arena: ",
umax2s(opt_dirty_max, s), "\n", ""); umax2s(opt_dirty_max, s), "\n", "");
jemalloc_message("Chunk size: ", umax2s(chunksize, s), "", ""); malloc_message("Chunk size: ", umax2s(chunksize, s), "", "");
jemalloc_message(" (2^", umax2s(opt_chunk_2pow, s), ")\n", ""); malloc_message(" (2^", umax2s(opt_chunk_2pow, s), ")\n", "");
#ifdef JEMALLOC_STATS #ifdef JEMALLOC_STATS
{ {
@ -4800,7 +4802,7 @@ malloc_print_stats(void)
} }
} }
#endif /* #ifdef JEMALLOC_STATS */ #endif /* #ifdef JEMALLOC_STATS */
jemalloc_message("--- End jemalloc statistics ---\n", "", "", malloc_message("--- End jemalloc statistics ---\n", "", "",
""); "");
} }
} }
@ -5034,12 +5036,12 @@ malloc_init_hard(void)
} }
break; break;
case 2: case 2:
if (jemalloc_options != NULL) { if (malloc_options != NULL) {
/* /*
* Use options that were compiled into the * Use options that were compiled into the
* program. * program.
*/ */
opts = jemalloc_options; opts = malloc_options;
} else { } else {
/* No configuration specified. */ /* No configuration specified. */
buf[0] = '\0'; buf[0] = '\0';
@ -5241,7 +5243,7 @@ MALLOC_OUT:
cbuf[0] = opts[j]; cbuf[0] = opts[j];
cbuf[1] = '\0'; cbuf[1] = '\0';
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Unsupported character " ": Unsupported character "
"in malloc options: '", cbuf, "in malloc options: '", cbuf,
"'\n"); "'\n");
@ -5302,7 +5304,7 @@ MALLOC_OUT:
*/ */
if (nbins > 256) { if (nbins > 256) {
char line_buf[UMAX2S_BUFSIZE]; char line_buf[UMAX2S_BUFSIZE];
jemalloc_message("<jemalloc>: Too many size classes (", malloc_message("<jemalloc>: Too many size classes (",
umax2s(nbins, line_buf), " > 256)\n", ""); umax2s(nbins, line_buf), " > 256)\n", "");
abort(); abort();
} }
@ -5411,7 +5413,7 @@ MALLOC_OUT:
#ifdef JEMALLOC_MAG #ifdef JEMALLOC_MAG
if (pthread_key_create(&mag_rack_tsd, thread_cleanup) != 0) { if (pthread_key_create(&mag_rack_tsd, thread_cleanup) != 0) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in pthread_key_create()\n", "", ""); ": Error in pthread_key_create()\n", "", "");
abort(); abort();
} }
@ -5554,7 +5556,7 @@ RETURN:
if (ret == NULL) { if (ret == NULL) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in malloc(): out of memory\n", "", ": Error in malloc(): out of memory\n", "",
""); "");
abort(); abort();
@ -5581,7 +5583,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
|| alignment < sizeof(void *)) { || alignment < sizeof(void *)) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in posix_memalign(): " ": Error in posix_memalign(): "
"invalid alignment\n", "", ""); "invalid alignment\n", "", "");
abort(); abort();
@ -5598,7 +5600,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
if (result == NULL) { if (result == NULL) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in posix_memalign(): out of memory\n", ": Error in posix_memalign(): out of memory\n",
"", ""); "", "");
abort(); abort();
@ -5658,7 +5660,7 @@ RETURN:
if (ret == NULL) { if (ret == NULL) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in calloc(): out of memory\n", "", ": Error in calloc(): out of memory\n", "",
""); "");
abort(); abort();
@ -5700,7 +5702,7 @@ realloc(void *ptr, size_t size)
if (ret == NULL) { if (ret == NULL) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in realloc(): out of " ": Error in realloc(): out of "
"memory\n", "", ""); "memory\n", "", "");
abort(); abort();
@ -5717,7 +5719,7 @@ realloc(void *ptr, size_t size)
if (ret == NULL) { if (ret == NULL) {
#ifdef JEMALLOC_XMALLOC #ifdef JEMALLOC_XMALLOC
if (opt_xmalloc) { if (opt_xmalloc) {
jemalloc_message("<jemalloc>", malloc_message("<jemalloc>",
": Error in realloc(): out of " ": Error in realloc(): out of "
"memory\n", "", ""); "memory\n", "", "");
abort(); abort();

View File

@ -28,24 +28,21 @@
******************************************************************************* *******************************************************************************
*/ */
#ifndef JEMALLOC_H_
#define JEMALLOC_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifndef JEMALLOC_H_
#define JEMALLOC_H_
#include "jemalloc_defs.h" #include "jemalloc_defs.h"
size_t malloc_usable_size(const void *ptr); size_t malloc_usable_size(const void *ptr);
extern const char *jemalloc_options; extern const char *malloc_options;
extern void (*jemalloc_message)(const char *p1, const char *p2, extern void (*malloc_message)(const char *p1,
const char *p3, const char *p4); const char *p2, const char *p3, const char *p4);
#endif /* JEMALLOC_H_ */
#ifdef __cplusplus #ifdef __cplusplus
}; };
#endif #endif
#endif /* JEMALLOC_H_ */

View File

@ -36,6 +36,29 @@
*/ */
#undef JEMALLOC_VERSION #undef JEMALLOC_VERSION
/*
* If JEMALLOC_PREFIX is defined, it will cause all public APIs to be prefixed.
* This makes it possible, with some care, to use multiple allocators
* simultaneously.
*
* In many cases it is more convenient to manually prefix allocator function
* calls than to let macros do it automatically, particularly when using
* multiple allocators simultaneously. Define JEMALLOC_MANGLE before
* #include'ing jemalloc.h in order to cause name mangling that corresponds to
* the API prefixing.
*/
#undef JEMALLOC_PREFIX
#if (defined(JEMALLOC_PREFIX) && defined(JEMALLOC_MANGLE))
#undef malloc
#undef calloc
#undef posix_memalign
#undef realloc
#undef free
#undef malloc_usable_size
#undef malloc_options
#undef malloc_message
#endif
/* /*
* Hyper-threaded CPUs may need a special instruction inside spin loops in * Hyper-threaded CPUs may need a special instruction inside spin loops in
* order to yield to another virtual CPU. * order to yield to another virtual CPU.

View File

@ -74,7 +74,7 @@
#include <gdfontl.h> #include <gdfontl.h>
#include <gdfontg.h> #include <gdfontg.h>
#include "jemalloc_defs.h" #include "jemalloc.h"
#ifndef JEMALLOC_DEBUG #ifndef JEMALLOC_DEBUG
# define NDEBUG # define NDEBUG
#endif #endif