Fix various namespace issues.

This commit is contained in:
Jason Evans 2009-06-22 17:44:33 -07:00
parent c9658dd35c
commit 804c9ec3af

View File

@ -1,4 +1,32 @@
/*- /*-
* Copyright (C) 2009 Facebook, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Facebook, Inc. nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************
*
* Copyright (C) 2006-2008 Jason Evans <jasone@FreeBSD.org>. * Copyright (C) 2006-2008 Jason Evans <jasone@FreeBSD.org>.
* All rights reserved. * All rights reserved.
* *
@ -721,7 +749,7 @@ struct arena_s {
/* /*
* bins is used to store rings of free regions of the following sizes, * bins is used to store rings of free regions of the following sizes,
* assuming a 16-byte quantum, 4kB page size, and default * assuming a 16-byte quantum, 4kB page size, and default
* MALLOC_OPTIONS. * JEMALLOC_OPTIONS.
* *
* bins[i] | size | * bins[i] | size |
* --------+------+ * --------+------+
@ -1037,7 +1065,7 @@ static chunk_stats_t stats_chunks;
/* /*
* Runtime configuration options. * Runtime configuration options.
*/ */
const char *_malloc_options; const char *jemalloc_options;
#ifndef MALLOC_PRODUCTION #ifndef MALLOC_PRODUCTION
static bool opt_abort = true; static bool opt_abort = true;
@ -1185,8 +1213,8 @@ static bool size2bin_init(void);
static bool size2bin_init_hard(void); static bool size2bin_init_hard(void);
static unsigned malloc_ncpus(void); static unsigned malloc_ncpus(void);
static bool malloc_init_hard(void); static bool malloc_init_hard(void);
void _malloc_prefork(void); void jemalloc_prefork(void);
void _malloc_postfork(void); void jemalloc_postfork(void);
/* /*
* End function prototypes. * End function prototypes.
@ -1203,8 +1231,7 @@ wrtmessage(const char *p1, const char *p2, const char *p3, const char *p4)
write(STDERR_FILENO, p4, strlen(p4)); write(STDERR_FILENO, p4, strlen(p4));
} }
#define _malloc_message malloc_message 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;
/* /*
@ -1238,9 +1265,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]; \
_malloc_message(__FILE__, ":", umax2s(__LINE__, \ jemalloc_message(__FILE__, ":", umax2s(__LINE__, \
line_buf), ": Failed assertion: "); \ line_buf), ": Failed assertion: "); \
_malloc_message("\"", #e, "\"\n", ""); \ jemalloc_message("\"", #e, "\"\n", ""); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
@ -1271,13 +1298,6 @@ utrace(const void *addr, size_t len)
} }
#endif #endif
static inline const char *
_getprogname(void)
{
return ("<jemalloc>");
}
#ifdef MALLOC_STATS #ifdef MALLOC_STATS
/* /*
* Print to stderr in such a way as to (hopefully) avoid memory allocation. * Print to stderr in such a way as to (hopefully) avoid memory allocation.
@ -1291,7 +1311,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);
_malloc_message(buf, "", "", ""); jemalloc_message(buf, "", "", "");
} }
#endif #endif
@ -1822,8 +1842,8 @@ 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));
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in munmap(): ", buf, "\n"); ": Error in munmap(): ", buf, "\n");
if (opt_abort) if (opt_abort)
abort(); abort();
} }
@ -1843,8 +1863,8 @@ 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));
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in munmap(): ", buf, "\n"); ": Error in munmap(): ", buf, "\n");
if (opt_abort) if (opt_abort)
abort(); abort();
} }
@ -4221,8 +4241,8 @@ 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.
*/ */
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error initializing arena\n", "", ""); ": Error initializing arena\n", "", "");
if (opt_abort) if (opt_abort)
abort(); abort();
@ -4525,66 +4545,67 @@ malloc_print_stats(void)
if (opt_print_stats) { if (opt_print_stats) {
char s[UMAX2S_BUFSIZE]; char s[UMAX2S_BUFSIZE];
_malloc_message("___ Begin malloc statistics ___\n", "", "", jemalloc_message("___ Begin jemalloc statistics ___\n", "", "",
""); "");
_malloc_message("Assertions ", jemalloc_message("Assertions ",
#ifdef NDEBUG #ifdef NDEBUG
"disabled", "disabled",
#else #else
"enabled", "enabled",
#endif #endif
"\n", ""); "\n", "");
_malloc_message("Boolean MALLOC_OPTIONS: ", jemalloc_message("Boolean JEMALLOC_OPTIONS: ",
opt_abort ? "A" : "a", "", ""); opt_abort ? "A" : "a", "", "");
#ifdef MALLOC_DSS #ifdef MALLOC_DSS
_malloc_message(opt_dss ? "D" : "d", "", "", ""); jemalloc_message(opt_dss ? "D" : "d", "", "", "");
#endif #endif
#ifdef MALLOC_MAG #ifdef MALLOC_MAG
_malloc_message(opt_mag ? "G" : "g", "", "", ""); jemalloc_message(opt_mag ? "G" : "g", "", "", "");
#endif #endif
_malloc_message(opt_junk ? "J" : "j", "", "", ""); jemalloc_message(opt_junk ? "J" : "j", "", "", "");
#ifdef MALLOC_DSS #ifdef MALLOC_DSS
_malloc_message(opt_mmap ? "M" : "m", "", "", ""); jemalloc_message(opt_mmap ? "M" : "m", "", "", "");
#endif #endif
_malloc_message(opt_utrace ? "PU" : "Pu", jemalloc_message(opt_utrace ? "PU" : "Pu",
opt_sysv ? "V" : "v", opt_sysv ? "V" : "v",
opt_xmalloc ? "X" : "x", opt_xmalloc ? "X" : "x",
opt_zero ? "Z\n" : "z\n"); opt_zero ? "Z\n" : "z\n");
_malloc_message("CPUs: ", umax2s(ncpus, s), "\n", ""); jemalloc_message("CPUs: ", umax2s(ncpus, s), "\n", "");
_malloc_message("Max arenas: ", umax2s(narenas, s), "\n", ""); jemalloc_message("Max arenas: ", umax2s(narenas, s), "\n", "");
#ifdef MALLOC_BALANCE #ifdef MALLOC_BALANCE
_malloc_message("Arena balance threshold: ", jemalloc_message("Arena balance threshold: ",
umax2s(opt_balance_threshold, s), "\n", ""); umax2s(opt_balance_threshold, s), "\n", "");
#endif #endif
_malloc_message("Pointer size: ", umax2s(sizeof(void *), s), jemalloc_message("Pointer size: ", umax2s(sizeof(void *), s),
"\n", ""); "\n", "");
_malloc_message("Quantum size: ", umax2s(QUANTUM, s), "\n", ""); jemalloc_message("Quantum size: ", umax2s(QUANTUM, s), "\n",
_malloc_message("Cacheline size (assumed): ", umax2s(CACHELINE, "");
s), "\n", ""); jemalloc_message("Cacheline size (assumed): ",
umax2s(CACHELINE, s), "\n", "");
#ifdef MALLOC_TINY #ifdef MALLOC_TINY
_malloc_message("Tiny 2^n-spaced sizes: [", umax2s((1U << jemalloc_message("Tiny 2^n-spaced sizes: [", umax2s((1U <<
TINY_MIN_2POW), s), "..", ""); TINY_MIN_2POW), s), "..", "");
_malloc_message(umax2s((qspace_min >> 1), s), "]\n", "", ""); jemalloc_message(umax2s((qspace_min >> 1), s), "]\n", "", "");
#endif #endif
_malloc_message("Quantum-spaced sizes: [", umax2s(qspace_min, jemalloc_message("Quantum-spaced sizes: [", umax2s(qspace_min,
s), "..", ""); s), "..", "");
_malloc_message(umax2s(qspace_max, s), "]\n", "", ""); jemalloc_message(umax2s(qspace_max, s), "]\n", "", "");
_malloc_message("Cacheline-spaced sizes: [", umax2s(cspace_min, jemalloc_message("Cacheline-spaced sizes: [",
umax2s(cspace_min, s), "..", "");
jemalloc_message(umax2s(cspace_max, s), "]\n", "", "");
jemalloc_message("Subpage-spaced sizes: [", umax2s(sspace_min,
s), "..", ""); s), "..", "");
_malloc_message(umax2s(cspace_max, s), "]\n", "", ""); jemalloc_message(umax2s(sspace_max, s), "]\n", "", "");
_malloc_message("Subpage-spaced sizes: [", umax2s(sspace_min,
s), "..", "");
_malloc_message(umax2s(sspace_max, s), "]\n", "", "");
#ifdef MALLOC_MAG #ifdef MALLOC_MAG
_malloc_message("Rounds per magazine: ", umax2s(max_rounds, s), jemalloc_message("Rounds per magazine: ", umax2s(max_rounds,
"\n", ""); s), "\n", "");
#endif #endif
_malloc_message("Max dirty pages per arena: ", jemalloc_message("Max dirty pages per arena: ",
umax2s(opt_dirty_max, s), "\n", ""); umax2s(opt_dirty_max, s), "\n", "");
_malloc_message("Chunk size: ", umax2s(chunksize, s), "", ""); jemalloc_message("Chunk size: ", umax2s(chunksize, s), "", "");
_malloc_message(" (2^", umax2s(opt_chunk_2pow, s), ")\n", ""); jemalloc_message(" (2^", umax2s(opt_chunk_2pow, s), ")\n", "");
#ifdef MALLOC_STATS #ifdef MALLOC_STATS
{ {
@ -4665,7 +4686,8 @@ malloc_print_stats(void)
} }
} }
#endif /* #ifdef MALLOC_STATS */ #endif /* #ifdef MALLOC_STATS */
_malloc_message("--- End malloc statistics ---\n", "", "", ""); jemalloc_message("--- End jemalloc statistics ---\n", "", "",
"");
} }
} }
@ -4884,10 +4906,10 @@ malloc_init_hard(void)
/* Get runtime configuration. */ /* Get runtime configuration. */
switch (i) { switch (i) {
case 0: case 0:
if ((linklen = readlink("/etc/malloc.conf", buf, if ((linklen = readlink("/etc/jemalloc.conf", buf,
sizeof(buf) - 1)) != -1) { sizeof(buf) - 1)) != -1) {
/* /*
* Use the contents of the "/etc/malloc.conf" * Use the contents of the "/etc/jemalloc.conf"
* symbolic link's name. * symbolic link's name.
*/ */
buf[linklen] = '\0'; buf[linklen] = '\0';
@ -4900,11 +4922,11 @@ malloc_init_hard(void)
break; break;
case 1: case 1:
if (issetugid() == 0 && (opts = if (issetugid() == 0 && (opts =
getenv("MALLOC_OPTIONS")) != NULL) { getenv("JEMALLOC_OPTIONS")) != NULL) {
/* /*
* Do nothing; opts is already initialized to * Do nothing; opts is already initialized to
* the value of the MALLOC_OPTIONS environment * the value of the JEMALLOC_OPTIONS
* variable. * environment variable.
*/ */
} else { } else {
/* No configuration specified. */ /* No configuration specified. */
@ -4913,12 +4935,12 @@ malloc_init_hard(void)
} }
break; break;
case 2: case 2:
if (_malloc_options != NULL) { if (jemalloc_options != NULL) {
/* /*
* Use options that were compiled into the * Use options that were compiled into the
* program. * program.
*/ */
opts = _malloc_options; opts = jemalloc_options;
} else { } else {
/* No configuration specified. */ /* No configuration specified. */
buf[0] = '\0'; buf[0] = '\0';
@ -5108,8 +5130,8 @@ MALLOC_OUT:
cbuf[0] = opts[j]; cbuf[0] = opts[j];
cbuf[1] = '\0'; cbuf[1] = '\0';
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Unsupported character " ": Unsupported character "
"in malloc options: '", cbuf, "in malloc options: '", cbuf,
"'\n"); "'\n");
} }
@ -5131,7 +5153,7 @@ MALLOC_OUT:
} }
/* Register fork handlers. */ /* Register fork handlers. */
pthread_atfork(_malloc_prefork, _malloc_postfork, _malloc_postfork); pthread_atfork(jemalloc_prefork, jemalloc_postfork, jemalloc_postfork);
#ifdef MALLOC_MAG #ifdef MALLOC_MAG
/* /*
@ -5380,8 +5402,8 @@ malloc(size_t size)
RETURN: RETURN:
if (ret == NULL) { if (ret == NULL) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in malloc(): out of memory\n", "", ": Error in malloc(): out of memory\n", "",
""); "");
abort(); abort();
} }
@ -5405,8 +5427,8 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
if (((alignment - 1) & alignment) != 0 if (((alignment - 1) & alignment) != 0
|| alignment < sizeof(void *)) { || alignment < sizeof(void *)) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in posix_memalign(): " ": Error in posix_memalign(): "
"invalid alignment\n", "", ""); "invalid alignment\n", "", "");
abort(); abort();
} }
@ -5420,8 +5442,8 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
if (result == NULL) { if (result == NULL) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in posix_memalign(): out of memory\n", ": Error in posix_memalign(): out of memory\n",
"", ""); "", "");
abort(); abort();
} }
@ -5474,8 +5496,8 @@ calloc(size_t num, size_t size)
RETURN: RETURN:
if (ret == NULL) { if (ret == NULL) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in calloc(): out of memory\n", "", ": Error in calloc(): out of memory\n", "",
""); "");
abort(); abort();
} }
@ -5509,8 +5531,8 @@ realloc(void *ptr, size_t size)
if (ret == NULL) { if (ret == NULL) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in realloc(): out of " ": Error in realloc(): out of "
"memory\n", "", ""); "memory\n", "", "");
abort(); abort();
} }
@ -5524,8 +5546,8 @@ realloc(void *ptr, size_t size)
if (ret == NULL) { if (ret == NULL) {
if (opt_xmalloc) { if (opt_xmalloc) {
_malloc_message(_getprogname(), jemalloc_message("<jemalloc>",
": (malloc) Error in realloc(): out of " ": Error in realloc(): out of "
"memory\n", "", ""); "memory\n", "", "");
abort(); abort();
} }
@ -5586,7 +5608,7 @@ malloc_usable_size(const void *ptr)
* thread caches. * thread caches.
*/ */
void void
_malloc_thread_cleanup(void) jemalloc_thread_cleanup(void)
{ {
#ifdef MALLOC_MAG #ifdef MALLOC_MAG
@ -5608,7 +5630,7 @@ _malloc_thread_cleanup(void)
*/ */
void void
_malloc_prefork(void) jemalloc_prefork(void)
{ {
bool again; bool again;
unsigned i, j; unsigned i, j;
@ -5656,7 +5678,7 @@ _malloc_prefork(void)
} }
void void
_malloc_postfork(void) jemalloc_postfork(void)
{ {
unsigned i; unsigned i;
arena_t *larenas[narenas]; arena_t *larenas[narenas];