1 diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
2 index 26a5e14..2a801b7 100644
3 --- a/doc/jemalloc.xml.in
4 +++ b/doc/jemalloc.xml.in
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>
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
22 <title>SYNOPSIS</title>
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>
28 <title>Standard API</title>
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>
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
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>
45 + <para>The <function>*allocx<parameter/></function> functions first appeared
46 + in FreeBSD 11.0.</para>
49 diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
50 index 654cd08..ad5382d 100644
51 --- a/include/jemalloc/internal/jemalloc_internal.h.in
52 +++ b/include/jemalloc/internal/jemalloc_internal.h.in
54 #include <sys/ktrace.h>
57 +#include "un-namespace.h"
58 +#include "libc_private.h"
60 #define JEMALLOC_NO_DEMANGLE
62 # define JEMALLOC_N(n) jet_##n
63 @@ -42,13 +45,7 @@ static const bool config_fill =
67 -static const bool config_lazy_lock =
68 -#ifdef JEMALLOC_LAZY_LOCK
74 +static const bool config_lazy_lock = true;
75 static const bool config_prof =
78 diff --git a/include/jemalloc/internal/jemalloc_internal_decls.h b/include/jemalloc/internal/jemalloc_internal_decls.h
79 index a601d6e..e7094b2 100644
80 --- a/include/jemalloc/internal/jemalloc_internal_decls.h
81 +++ b/include/jemalloc/internal/jemalloc_internal_decls.h
83 #ifndef JEMALLOC_INTERNAL_DECLS_H
84 #define JEMALLOC_INTERNAL_DECLS_H
86 +#include "libc_private.h"
87 +#include "namespace.h"
92 diff --git a/include/jemalloc/internal/mutex.h b/include/jemalloc/internal/mutex.h
93 index 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 {
98 #ifdef JEMALLOC_LAZY_LOCK
99 extern bool isthreaded;
101 -# undef isthreaded /* Undo private_namespace.h definition. */
102 -# define isthreaded true
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);
112 #endif /* JEMALLOC_H_EXTERNS */
113 diff --git a/include/jemalloc/internal/private_symbols.txt b/include/jemalloc/internal/private_symbols.txt
114 index 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
124 jemalloc_postfork_child
125 diff --git a/include/jemalloc/jemalloc_FreeBSD.h b/include/jemalloc/jemalloc_FreeBSD.h
127 index 0000000..737542e
129 +++ b/include/jemalloc/jemalloc_FreeBSD.h
132 + * Override settings that were generated in jemalloc_defs.h as necessary.
135 +#undef JEMALLOC_OVERRIDE_VALLOC
137 +#ifndef MALLOC_PRODUCTION
138 +#define JEMALLOC_DEBUG
142 + * The following are architecture-dependent, so conditionally define them for
143 + * each supported architecture.
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
153 +# define LG_SIZEOF_PTR 2
154 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
157 +# define LG_SIZEOF_PTR 3
160 +# define LG_SIZEOF_PTR 3
161 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
164 +# define LG_SIZEOF_PTR 3
165 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
168 +# define LG_SIZEOF_PTR 2
171 +# define LG_SIZEOF_PTR 3
175 +# define LG_SIZEOF_PTR 3
177 +# define LG_SIZEOF_PTR 2
180 +#ifdef __powerpc64__
181 +# define LG_SIZEOF_PTR 3
182 +#elif defined(__powerpc__)
183 +# define LG_SIZEOF_PTR 2
186 +#ifndef JEMALLOC_TLS_MODEL
187 +# define JEMALLOC_TLS_MODEL /* Default. */
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
196 +#include <machine/cpu.h>
197 +#include <machine/cpufunc.h>
198 +#define CPU_SPINWAIT cpu_spinwait()
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)
210 +#undef je_posix_memalign
211 +#undef je_aligned_alloc
212 +#undef je_malloc_usable_size
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
244 +#define write _write
245 +#define close _close
246 +#define pthread_mutex_lock _pthread_mutex_lock
247 +#define pthread_mutex_unlock _pthread_mutex_unlock
251 + * Define 'weak' symbols so that an application can have its own versions
252 + * of malloc, calloc, realloc, free, et al.
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);
273 diff --git a/include/jemalloc/jemalloc_rename.sh b/include/jemalloc/jemalloc_rename.sh
274 index f943891..47d032c 100755
275 --- a/include/jemalloc/jemalloc_rename.sh
276 +++ b/include/jemalloc/jemalloc_rename.sh
277 @@ -19,4 +19,6 @@ done
282 +#include "jemalloc_FreeBSD.h"
284 diff --git a/src/jemalloc.c b/src/jemalloc.c
285 index 5a2d324..b6cbb79 100644
289 /******************************************************************************/
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);
296 /* Runtime configuration options. */
297 const char *je_malloc_conf JEMALLOC_ATTR(weak);
299 @@ -2490,6 +2494,107 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr)
301 /******************************************************************************/
303 + * Begin compatibility functions.
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)
311 +#define ALLOCM_SUCCESS 0
312 +#define ALLOCM_ERR_OOM 1
313 +#define ALLOCM_ERR_NOT_MOVED 2
316 +je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
320 + assert(ptr != NULL);
322 + p = je_mallocx(size, flags);
324 + return (ALLOCM_ERR_OOM);
326 + *rsize = isalloc(p, config_prof);
328 + return (ALLOCM_SUCCESS);
332 +je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
335 + bool no_move = flags & ALLOCM_NO_MOVE;
337 + assert(ptr != NULL);
338 + assert(*ptr != NULL);
340 + assert(SIZE_T_MAX - size >= extra);
343 + size_t usize = je_xallocx(*ptr, size, extra, flags);
344 + ret = (usize >= size) ? ALLOCM_SUCCESS : ALLOCM_ERR_NOT_MOVED;
348 + void *p = je_rallocx(*ptr, size+extra, flags);
351 + ret = ALLOCM_SUCCESS;
353 + ret = ALLOCM_ERR_OOM;
355 + *rsize = isalloc(*ptr, config_prof);
361 +je_sallocm(const void *ptr, size_t *rsize, int flags)
364 + assert(rsize != NULL);
365 + *rsize = je_sallocx(ptr, flags);
366 + return (ALLOCM_SUCCESS);
370 +je_dallocm(void *ptr, int flags)
373 + je_dallocx(ptr, flags);
374 + return (ALLOCM_SUCCESS);
378 +je_nallocm(size_t *rsize, size_t size, int flags)
382 + usize = je_nallocx(size, flags);
384 + return (ALLOCM_ERR_OOM);
387 + return (ALLOCM_SUCCESS);
390 +#undef ALLOCM_LG_ALIGN
393 +#undef ALLOCM_NO_MOVE
395 +#undef ALLOCM_SUCCESS
396 +#undef ALLOCM_ERR_OOM
397 +#undef ALLOCM_ERR_NOT_MOVED
400 + * End compatibility functions.
402 +/******************************************************************************/
404 * The following functions are used by threading libraries for protection of
405 * malloc during fork().
407 @@ -2590,4 +2695,11 @@ jemalloc_postfork_child(void)
408 ctl_postfork_child();
412 +_malloc_first_thread(void)
415 + (void)malloc_mutex_first_thread();
418 /******************************************************************************/
419 diff --git a/src/mutex.c b/src/mutex.c
420 index 2d47af9..934d5aa 100644
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));
428 +#pragma weak _pthread_mutex_init_calloc_cb
430 +_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
431 + void *(calloc_cb)(size_t, size_t))
434 + return (((int (*)(pthread_mutex_t *, void *(*)(size_t, size_t)))
435 + __libc_interposing[INTERPOS__pthread_mutex_init_calloc_cb])(mutex,
441 @@ -137,7 +148,7 @@ malloc_mutex_postfork_child(malloc_mutex_t *mutex)
446 +malloc_mutex_first_thread(void)
449 #ifdef JEMALLOC_MUTEX_INIT_CB
450 @@ -151,3 +162,14 @@ mutex_boot(void)
459 +#ifndef JEMALLOC_MUTEX_INIT_CB
460 + return (malloc_mutex_first_thread());
465 diff --git a/src/util.c b/src/util.c
466 index 4cb0d6c..25b61c2 100644
469 @@ -58,6 +58,22 @@ wrtmessage(void *cbopaque, const char *s)
471 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
473 +JEMALLOC_ATTR(visibility("hidden"))
475 +wrtmessage_1_0(const char *s1, const char *s2, const char *s3,
479 + wrtmessage(NULL, s1);
480 + wrtmessage(NULL, s2);
481 + wrtmessage(NULL, s3);
482 + wrtmessage(NULL, s4);
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);
490 * Wrapper around malloc_message() that avoids the need for
491 * je_malloc_message(...) throughout the code.