1diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in 2index 26a5e14..2a801b7 100644 3--- a/doc/jemalloc.xml.in 4+++ b/doc/jemalloc.xml.in 5@@ -53,11 +53,23 @@ 6 <para>This manual describes jemalloc @jemalloc_version@. More information 7 can be found at the <ulink 8 url="http://www.canonware.com/jemalloc/">jemalloc website</ulink>.</para> 9+ 10+ <para>The following configuration options are enabled in libc's built-in 11+ jemalloc: <option>--enable-fill</option>, 12+ <option>--enable-lazy-lock</option>, <option>--enable-munmap</option>, 13+ <option>--enable-stats</option>, <option>--enable-tcache</option>, 14+ <option>--enable-tls</option>, <option>--enable-utrace</option>, and 15+ <option>--enable-xmalloc</option>. Additionally, 16+ <option>--enable-debug</option> is enabled in development versions of 17+ FreeBSD (controlled by the <constant>MALLOC_PRODUCTION</constant> make 18+ variable).</para> 19+ 20 </refsect1> 21 <refsynopsisdiv> 22 <title>SYNOPSIS</title> 23 <funcsynopsis> 24- <funcsynopsisinfo>#include <<filename class="headerfile">jemalloc/jemalloc.h</filename>></funcsynopsisinfo> 25+ <funcsynopsisinfo>#include <<filename class="headerfile">stdlib.h</filename>> 26+#include <<filename class="headerfile">malloc_np.h</filename>></funcsynopsisinfo> 27 <refsect2> 28 <title>Standard API</title> 29 <funcprototype> 30@@ -2759,4 +2771,18 @@ malloc_conf = "lg_chunk:24";]]></programlisting></para> 31 <para>The <function>posix_memalign<parameter/></function> function conforms 32 to IEEE Std 1003.1-2001 (“POSIX.1”).</para> 33 </refsect1> 34+ <refsect1 id="history"> 35+ <title>HISTORY</title> 36+ <para>The <function>malloc_usable_size<parameter/></function> and 37+ <function>posix_memalign<parameter/></function> functions first appeared in 38+ FreeBSD 7.0.</para> 39+ 40+ <para>The <function>aligned_alloc<parameter/></function>, 41+ <function>malloc_stats_print<parameter/></function>, and 42+ <function>mallctl*<parameter/></function> functions first appeared in 43+ FreeBSD 10.0.</para> 44+ 45+ <para>The <function>*allocx<parameter/></function> functions first appeared 46+ in FreeBSD 11.0.</para> 47+ </refsect1> 48 </refentry> 49diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in 50index 654cd08..ad5382d 100644 51--- a/include/jemalloc/internal/jemalloc_internal.h.in 52+++ b/include/jemalloc/internal/jemalloc_internal.h.in 53@@ -8,6 +8,9 @@ 54 #include <sys/ktrace.h> 55 #endif 56 57+#include "un-namespace.h" 58+#include "libc_private.h" 59+ 60 #define JEMALLOC_NO_DEMANGLE 61 #ifdef JEMALLOC_JET 62 # define JEMALLOC_N(n) jet_##n 63@@ -42,13 +45,7 @@ static const bool config_fill = 64 false 65 #endif 66 ; 67-static const bool config_lazy_lock = 68-#ifdef JEMALLOC_LAZY_LOCK 69- true 70-#else 71- false 72-#endif 73- ; 74+static const bool config_lazy_lock = true; 75 static const bool config_prof = 76 #ifdef JEMALLOC_PROF 77 true 78diff --git a/include/jemalloc/internal/jemalloc_internal_decls.h b/include/jemalloc/internal/jemalloc_internal_decls.h 79index a601d6e..e7094b2 100644 80--- a/include/jemalloc/internal/jemalloc_internal_decls.h 81+++ b/include/jemalloc/internal/jemalloc_internal_decls.h 82@@ -1,6 +1,9 @@ 83 #ifndef JEMALLOC_INTERNAL_DECLS_H 84 #define JEMALLOC_INTERNAL_DECLS_H 85 86+#include "libc_private.h" 87+#include "namespace.h" 88+ 89 #include <math.h> 90 #ifdef _WIN32 91 # include <windows.h> 92diff --git a/include/jemalloc/internal/mutex.h b/include/jemalloc/internal/mutex.h 93index f051f29..561378f 100644 94--- a/include/jemalloc/internal/mutex.h 95+++ b/include/jemalloc/internal/mutex.h 96@@ -47,15 +47,13 @@ struct malloc_mutex_s { 97 98 #ifdef JEMALLOC_LAZY_LOCK 99 extern bool isthreaded; 100-#else 101-# undef isthreaded /* Undo private_namespace.h definition. */ 102-# define isthreaded true 103 #endif 104 105 bool malloc_mutex_init(malloc_mutex_t *mutex); 106 void malloc_mutex_prefork(malloc_mutex_t *mutex); 107 void malloc_mutex_postfork_parent(malloc_mutex_t *mutex); 108 void malloc_mutex_postfork_child(malloc_mutex_t *mutex); 109+bool malloc_mutex_first_thread(void); 110 bool mutex_boot(void); 111 112 #endif /* JEMALLOC_H_EXTERNS */ 113diff --git a/include/jemalloc/internal/private_symbols.txt b/include/jemalloc/internal/private_symbols.txt 114index a90021a..34904bf 100644 115--- a/include/jemalloc/internal/private_symbols.txt 116+++ b/include/jemalloc/internal/private_symbols.txt 117@@ -280,7 +280,6 @@ iralloct_realign 118 isalloc 119 isdalloct 120 isqalloc 121-isthreaded 122 ivsalloc 123 ixalloc 124 jemalloc_postfork_child 125diff --git a/include/jemalloc/jemalloc_FreeBSD.h b/include/jemalloc/jemalloc_FreeBSD.h 126new file mode 100644 127index 0000000..737542e 128--- /dev/null 129+++ b/include/jemalloc/jemalloc_FreeBSD.h 130@@ -0,0 +1,142 @@ 131+/* 132+ * Override settings that were generated in jemalloc_defs.h as necessary. 133+ */ 134+ 135+#undef JEMALLOC_OVERRIDE_VALLOC 136+ 137+#ifndef MALLOC_PRODUCTION 138+#define JEMALLOC_DEBUG 139+#endif 140+ 141+/* 142+ * The following are architecture-dependent, so conditionally define them for 143+ * each supported architecture. 144+ */ 145+#undef JEMALLOC_TLS_MODEL 146+#undef STATIC_PAGE_SHIFT 147+#undef LG_SIZEOF_PTR 148+#undef LG_SIZEOF_INT 149+#undef LG_SIZEOF_LONG 150+#undef LG_SIZEOF_INTMAX_T 151+ 152+#ifdef __i386__ 153+# define LG_SIZEOF_PTR 2 154+# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec"))) 155+#endif 156+#ifdef __ia64__ 157+# define LG_SIZEOF_PTR 3 158+#endif 159+#ifdef __sparc64__ 160+# define LG_SIZEOF_PTR 3 161+# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec"))) 162+#endif 163+#ifdef __amd64__ 164+# define LG_SIZEOF_PTR 3 165+# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec"))) 166+#endif 167+#ifdef __arm__ 168+# define LG_SIZEOF_PTR 2 169+#endif 170+#ifdef __aarch64__ 171+# define LG_SIZEOF_PTR 3 172+#endif 173+#ifdef __mips__ 174+#ifdef __mips_n64 175+# define LG_SIZEOF_PTR 3 176+#else 177+# define LG_SIZEOF_PTR 2 178+#endif 179+#endif 180+#ifdef __powerpc64__ 181+# define LG_SIZEOF_PTR 3 182+#elif defined(__powerpc__) 183+# define LG_SIZEOF_PTR 2 184+#endif 185+ 186+#ifndef JEMALLOC_TLS_MODEL 187+# define JEMALLOC_TLS_MODEL /* Default. */ 188+#endif 189+ 190+#define STATIC_PAGE_SHIFT PAGE_SHIFT 191+#define LG_SIZEOF_INT 2 192+#define LG_SIZEOF_LONG LG_SIZEOF_PTR 193+#define LG_SIZEOF_INTMAX_T 3 194+ 195+#undef CPU_SPINWAIT 196+#include <machine/cpu.h> 197+#include <machine/cpufunc.h> 198+#define CPU_SPINWAIT cpu_spinwait() 199+ 200+/* Disable lazy-lock machinery, mangle isthreaded, and adjust its type. */ 201+#undef JEMALLOC_LAZY_LOCK 202+extern int __isthreaded; 203+#define isthreaded ((bool)__isthreaded) 204+ 205+/* Mangle. */ 206+#undef je_malloc 207+#undef je_calloc 208+#undef je_realloc 209+#undef je_free 210+#undef je_posix_memalign 211+#undef je_aligned_alloc 212+#undef je_malloc_usable_size 213+#undef je_mallocx 214+#undef je_rallocx 215+#undef je_xallocx 216+#undef je_sallocx 217+#undef je_dallocx 218+#undef je_nallocx 219+#undef je_allocm 220+#undef je_rallocm 221+#undef je_sallocm 222+#undef je_dallocm 223+#undef je_nallocm 224+#define je_malloc __malloc 225+#define je_calloc __calloc 226+#define je_realloc __realloc 227+#define je_free __free 228+#define je_posix_memalign __posix_memalign 229+#define je_aligned_alloc __aligned_alloc 230+#define je_malloc_usable_size __malloc_usable_size 231+#define je_mallocx __mallocx 232+#define je_rallocx __rallocx 233+#define je_xallocx __xallocx 234+#define je_sallocx __sallocx 235+#define je_dallocx __dallocx 236+#define je_nallocx __nallocx 237+#define je_allocm __allocm 238+#define je_rallocm __rallocm 239+#define je_sallocm __sallocm 240+#define je_dallocm __dallocm 241+#define je_nallocm __nallocm 242+#define open _open 243+#define read _read 244+#define write _write 245+#define close _close 246+#define pthread_mutex_lock _pthread_mutex_lock 247+#define pthread_mutex_unlock _pthread_mutex_unlock 248+ 249+#ifdef JEMALLOC_C_ 250+/* 251+ * Define 'weak' symbols so that an application can have its own versions 252+ * of malloc, calloc, realloc, free, et al. 253+ */ 254+__weak_reference(__malloc, malloc); 255+__weak_reference(__calloc, calloc); 256+__weak_reference(__realloc, realloc); 257+__weak_reference(__free, free); 258+__weak_reference(__posix_memalign, posix_memalign); 259+__weak_reference(__aligned_alloc, aligned_alloc); 260+__weak_reference(__malloc_usable_size, malloc_usable_size); 261+__weak_reference(__mallocx, mallocx); 262+__weak_reference(__rallocx, rallocx); 263+__weak_reference(__xallocx, xallocx); 264+__weak_reference(__sallocx, sallocx); 265+__weak_reference(__dallocx, dallocx); 266+__weak_reference(__nallocx, nallocx); 267+__weak_reference(__allocm, allocm); 268+__weak_reference(__rallocm, rallocm); 269+__weak_reference(__sallocm, sallocm); 270+__weak_reference(__dallocm, dallocm); 271+__weak_reference(__nallocm, nallocm); 272+#endif 273diff --git a/include/jemalloc/jemalloc_rename.sh b/include/jemalloc/jemalloc_rename.sh 274index f943891..47d032c 100755 275--- a/include/jemalloc/jemalloc_rename.sh 276+++ b/include/jemalloc/jemalloc_rename.sh 277@@ -19,4 +19,6 @@ done 278 279 cat <<EOF 280 #endif 281+ 282+#include "jemalloc_FreeBSD.h" 283 EOF 284diff --git a/src/jemalloc.c b/src/jemalloc.c 285index 5a2d324..b6cbb79 100644 286--- a/src/jemalloc.c 287+++ b/src/jemalloc.c 288@@ -4,6 +4,10 @@ 289 /******************************************************************************/ 290 /* Data. */ 291 292+/* Work around <http://llvm.org/bugs/show_bug.cgi?id=12623>: */ 293+const char *__malloc_options_1_0 = NULL; 294+__sym_compat(_malloc_options, __malloc_options_1_0, FBSD_1.0); 295+ 296 /* Runtime configuration options. */ 297 const char *je_malloc_conf JEMALLOC_ATTR(weak); 298 bool opt_abort = 299@@ -2490,6 +2494,107 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) 300 */ 301 /******************************************************************************/ 302 /* 303+ * Begin compatibility functions. 304+ */ 305+ 306+#define ALLOCM_LG_ALIGN(la) (la) 307+#define ALLOCM_ALIGN(a) (ffsl(a)-1) 308+#define ALLOCM_ZERO ((int)0x40) 309+#define ALLOCM_NO_MOVE ((int)0x80) 310+ 311+#define ALLOCM_SUCCESS 0 312+#define ALLOCM_ERR_OOM 1 313+#define ALLOCM_ERR_NOT_MOVED 2 314+ 315+int 316+je_allocm(void **ptr, size_t *rsize, size_t size, int flags) 317+{ 318+ void *p; 319+ 320+ assert(ptr != NULL); 321+ 322+ p = je_mallocx(size, flags); 323+ if (p == NULL) 324+ return (ALLOCM_ERR_OOM); 325+ if (rsize != NULL) 326+ *rsize = isalloc(p, config_prof); 327+ *ptr = p; 328+ return (ALLOCM_SUCCESS); 329+} 330+ 331+int 332+je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags) 333+{ 334+ int ret; 335+ bool no_move = flags & ALLOCM_NO_MOVE; 336+ 337+ assert(ptr != NULL); 338+ assert(*ptr != NULL); 339+ assert(size != 0); 340+ assert(SIZE_T_MAX - size >= extra); 341+ 342+ if (no_move) { 343+ size_t usize = je_xallocx(*ptr, size, extra, flags); 344+ ret = (usize >= size) ? ALLOCM_SUCCESS : ALLOCM_ERR_NOT_MOVED; 345+ if (rsize != NULL) 346+ *rsize = usize; 347+ } else { 348+ void *p = je_rallocx(*ptr, size+extra, flags); 349+ if (p != NULL) { 350+ *ptr = p; 351+ ret = ALLOCM_SUCCESS; 352+ } else 353+ ret = ALLOCM_ERR_OOM; 354+ if (rsize != NULL) 355+ *rsize = isalloc(*ptr, config_prof); 356+ } 357+ return (ret); 358+} 359+ 360+int 361+je_sallocm(const void *ptr, size_t *rsize, int flags) 362+{ 363+ 364+ assert(rsize != NULL); 365+ *rsize = je_sallocx(ptr, flags); 366+ return (ALLOCM_SUCCESS); 367+} 368+ 369+int 370+je_dallocm(void *ptr, int flags) 371+{ 372+ 373+ je_dallocx(ptr, flags); 374+ return (ALLOCM_SUCCESS); 375+} 376+ 377+int 378+je_nallocm(size_t *rsize, size_t size, int flags) 379+{ 380+ size_t usize; 381+ 382+ usize = je_nallocx(size, flags); 383+ if (usize == 0) 384+ return (ALLOCM_ERR_OOM); 385+ if (rsize != NULL) 386+ *rsize = usize; 387+ return (ALLOCM_SUCCESS); 388+} 389+ 390+#undef ALLOCM_LG_ALIGN 391+#undef ALLOCM_ALIGN 392+#undef ALLOCM_ZERO 393+#undef ALLOCM_NO_MOVE 394+ 395+#undef ALLOCM_SUCCESS 396+#undef ALLOCM_ERR_OOM 397+#undef ALLOCM_ERR_NOT_MOVED 398+ 399+/* 400+ * End compatibility functions. 401+ */ 402+/******************************************************************************/ 403+/* 404 * The following functions are used by threading libraries for protection of 405 * malloc during fork(). 406 */ 407@@ -2590,4 +2695,11 @@ jemalloc_postfork_child(void) 408 ctl_postfork_child(); 409 } 410 411+void 412+_malloc_first_thread(void) 413+{ 414+ 415+ (void)malloc_mutex_first_thread(); 416+} 417+ 418 /******************************************************************************/ 419diff --git a/src/mutex.c b/src/mutex.c 420index 2d47af9..934d5aa 100644 421--- a/src/mutex.c 422+++ b/src/mutex.c 423@@ -66,6 +66,17 @@ pthread_create(pthread_t *__restrict thread, 424 #ifdef JEMALLOC_MUTEX_INIT_CB 425 JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, 426 void *(calloc_cb)(size_t, size_t)); 427+ 428+#pragma weak _pthread_mutex_init_calloc_cb 429+int 430+_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, 431+ void *(calloc_cb)(size_t, size_t)) 432+{ 433+ 434+ return (((int (*)(pthread_mutex_t *, void *(*)(size_t, size_t))) 435+ __libc_interposing[INTERPOS__pthread_mutex_init_calloc_cb])(mutex, 436+ calloc_cb)); 437+} 438 #endif 439 440 bool 441@@ -137,7 +148,7 @@ malloc_mutex_postfork_child(malloc_mutex_t *mutex) 442 } 443 444 bool 445-mutex_boot(void) 446+malloc_mutex_first_thread(void) 447 { 448 449 #ifdef JEMALLOC_MUTEX_INIT_CB 450@@ -151,3 +162,14 @@ mutex_boot(void) 451 #endif 452 return (false); 453 } 454+ 455+bool 456+mutex_boot(void) 457+{ 458+ 459+#ifndef JEMALLOC_MUTEX_INIT_CB 460+ return (malloc_mutex_first_thread()); 461+#else 462+ return (false); 463+#endif 464+} 465diff --git a/src/util.c b/src/util.c 466index 4cb0d6c..25b61c2 100644 467--- a/src/util.c 468+++ b/src/util.c 469@@ -58,6 +58,22 @@ wrtmessage(void *cbopaque, const char *s) 470 471 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s); 472 473+JEMALLOC_ATTR(visibility("hidden")) 474+void 475+wrtmessage_1_0(const char *s1, const char *s2, const char *s3, 476+ const char *s4) 477+{ 478+ 479+ wrtmessage(NULL, s1); 480+ wrtmessage(NULL, s2); 481+ wrtmessage(NULL, s3); 482+ wrtmessage(NULL, s4); 483+} 484+ 485+void (*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3, 486+ const char *s4) = wrtmessage_1_0; 487+__sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0); 488+ 489 /* 490 * Wrapper around malloc_message() that avoids the need for 491 * je_malloc_message(...) throughout the code. 492