1 diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
2 index bc5dbd1..ba182da 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 @@ -2905,4 +2917,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 3f54391..d240256 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 char * const config_malloc_conf = JEMALLOC_CONFIG_MALLOC_CONF;
76 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 2b8ca5d..42d97f2 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 5880996..6e94e03 100644
115 --- a/include/jemalloc/internal/private_symbols.txt
116 +++ b/include/jemalloc/internal/private_symbols.txt
117 @@ -296,7 +296,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..433dab5
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 +# define LG_SIZEOF_PTR 3
189 +#ifndef JEMALLOC_TLS_MODEL
190 +# define JEMALLOC_TLS_MODEL /* Default. */
193 +#define STATIC_PAGE_SHIFT PAGE_SHIFT
194 +#define LG_SIZEOF_INT 2
195 +#define LG_SIZEOF_LONG LG_SIZEOF_PTR
196 +#define LG_SIZEOF_INTMAX_T 3
199 +#include <machine/cpu.h>
200 +#include <machine/cpufunc.h>
201 +#define CPU_SPINWAIT cpu_spinwait()
203 +/* Disable lazy-lock machinery, mangle isthreaded, and adjust its type. */
204 +#undef JEMALLOC_LAZY_LOCK
205 +extern int __isthreaded;
206 +#define isthreaded ((bool)__isthreaded)
211 +#undef je_posix_memalign
212 +#undef je_aligned_alloc
215 +#undef je_malloc_usable_size
224 +#undef je_mallctlnametomib
225 +#undef je_mallctlbymib
226 +#undef je_malloc_stats_print
232 +#define je_malloc __malloc
233 +#define je_calloc __calloc
234 +#define je_posix_memalign __posix_memalign
235 +#define je_aligned_alloc __aligned_alloc
236 +#define je_realloc __realloc
237 +#define je_free __free
238 +#define je_malloc_usable_size __malloc_usable_size
239 +#define je_mallocx __mallocx
240 +#define je_rallocx __rallocx
241 +#define je_xallocx __xallocx
242 +#define je_sallocx __sallocx
243 +#define je_dallocx __dallocx
244 +#define je_sdallocx __sdallocx
245 +#define je_nallocx __nallocx
246 +#define je_mallctl __mallctl
247 +#define je_mallctlnametomib __mallctlnametomib
248 +#define je_mallctlbymib __mallctlbymib
249 +#define je_malloc_stats_print __malloc_stats_print
250 +#define je_allocm __allocm
251 +#define je_rallocm __rallocm
252 +#define je_sallocm __sallocm
253 +#define je_dallocm __dallocm
254 +#define je_nallocm __nallocm
257 +#define write _write
258 +#define close _close
259 +#define pthread_mutex_lock _pthread_mutex_lock
260 +#define pthread_mutex_unlock _pthread_mutex_unlock
264 + * Define 'weak' symbols so that an application can have its own versions
265 + * of malloc, calloc, realloc, free, et al.
267 +__weak_reference(__malloc, malloc);
268 +__weak_reference(__calloc, calloc);
269 +__weak_reference(__posix_memalign, posix_memalign);
270 +__weak_reference(__aligned_alloc, aligned_alloc);
271 +__weak_reference(__realloc, realloc);
272 +__weak_reference(__free, free);
273 +__weak_reference(__malloc_usable_size, malloc_usable_size);
274 +__weak_reference(__mallocx, mallocx);
275 +__weak_reference(__rallocx, rallocx);
276 +__weak_reference(__xallocx, xallocx);
277 +__weak_reference(__sallocx, sallocx);
278 +__weak_reference(__dallocx, dallocx);
279 +__weak_reference(__sdallocx, sdallocx);
280 +__weak_reference(__nallocx, nallocx);
281 +__weak_reference(__mallctl, mallctl);
282 +__weak_reference(__mallctlnametomib, mallctlnametomib);
283 +__weak_reference(__mallctlbymib, mallctlbymib);
284 +__weak_reference(__malloc_stats_print, malloc_stats_print);
285 +__weak_reference(__allocm, allocm);
286 +__weak_reference(__rallocm, rallocm);
287 +__weak_reference(__sallocm, sallocm);
288 +__weak_reference(__dallocm, dallocm);
289 +__weak_reference(__nallocm, nallocm);
291 diff --git a/include/jemalloc/jemalloc_rename.sh b/include/jemalloc/jemalloc_rename.sh
292 index f943891..47d032c 100755
293 --- a/include/jemalloc/jemalloc_rename.sh
294 +++ b/include/jemalloc/jemalloc_rename.sh
295 @@ -19,4 +19,6 @@ done
300 +#include "jemalloc_FreeBSD.h"
302 diff --git a/src/jemalloc.c b/src/jemalloc.c
303 index 0735376..a34b85c 100644
307 /******************************************************************************/
310 +/* Work around <http://llvm.org/bugs/show_bug.cgi?id=12623>: */
311 +const char *__malloc_options_1_0 = NULL;
312 +__sym_compat(_malloc_options, __malloc_options_1_0, FBSD_1.0);
314 /* Runtime configuration options. */
315 const char *je_malloc_conf JEMALLOC_ATTR(weak);
317 @@ -2611,6 +2615,107 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr)
319 /******************************************************************************/
321 + * Begin compatibility functions.
324 +#define ALLOCM_LG_ALIGN(la) (la)
325 +#define ALLOCM_ALIGN(a) (ffsl(a)-1)
326 +#define ALLOCM_ZERO ((int)0x40)
327 +#define ALLOCM_NO_MOVE ((int)0x80)
329 +#define ALLOCM_SUCCESS 0
330 +#define ALLOCM_ERR_OOM 1
331 +#define ALLOCM_ERR_NOT_MOVED 2
334 +je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
338 + assert(ptr != NULL);
340 + p = je_mallocx(size, flags);
342 + return (ALLOCM_ERR_OOM);
344 + *rsize = isalloc(p, config_prof);
346 + return (ALLOCM_SUCCESS);
350 +je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
353 + bool no_move = flags & ALLOCM_NO_MOVE;
355 + assert(ptr != NULL);
356 + assert(*ptr != NULL);
358 + assert(SIZE_T_MAX - size >= extra);
361 + size_t usize = je_xallocx(*ptr, size, extra, flags);
362 + ret = (usize >= size) ? ALLOCM_SUCCESS : ALLOCM_ERR_NOT_MOVED;
366 + void *p = je_rallocx(*ptr, size+extra, flags);
369 + ret = ALLOCM_SUCCESS;
371 + ret = ALLOCM_ERR_OOM;
373 + *rsize = isalloc(*ptr, config_prof);
379 +je_sallocm(const void *ptr, size_t *rsize, int flags)
382 + assert(rsize != NULL);
383 + *rsize = je_sallocx(ptr, flags);
384 + return (ALLOCM_SUCCESS);
388 +je_dallocm(void *ptr, int flags)
391 + je_dallocx(ptr, flags);
392 + return (ALLOCM_SUCCESS);
396 +je_nallocm(size_t *rsize, size_t size, int flags)
400 + usize = je_nallocx(size, flags);
402 + return (ALLOCM_ERR_OOM);
405 + return (ALLOCM_SUCCESS);
408 +#undef ALLOCM_LG_ALIGN
411 +#undef ALLOCM_NO_MOVE
413 +#undef ALLOCM_SUCCESS
414 +#undef ALLOCM_ERR_OOM
415 +#undef ALLOCM_ERR_NOT_MOVED
418 + * End compatibility functions.
420 +/******************************************************************************/
422 * The following functions are used by threading libraries for protection of
423 * malloc during fork().
425 @@ -2717,4 +2822,11 @@ jemalloc_postfork_child(void)
426 ctl_postfork_child();
430 +_malloc_first_thread(void)
433 + (void)malloc_mutex_first_thread();
436 /******************************************************************************/
437 diff --git a/src/mutex.c b/src/mutex.c
438 index 2d47af9..934d5aa 100644
441 @@ -66,6 +66,17 @@ pthread_create(pthread_t *__restrict thread,
442 #ifdef JEMALLOC_MUTEX_INIT_CB
443 JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
444 void *(calloc_cb)(size_t, size_t));
446 +#pragma weak _pthread_mutex_init_calloc_cb
448 +_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
449 + void *(calloc_cb)(size_t, size_t))
452 + return (((int (*)(pthread_mutex_t *, void *(*)(size_t, size_t)))
453 + __libc_interposing[INTERPOS__pthread_mutex_init_calloc_cb])(mutex,
459 @@ -137,7 +148,7 @@ malloc_mutex_postfork_child(malloc_mutex_t *mutex)
464 +malloc_mutex_first_thread(void)
467 #ifdef JEMALLOC_MUTEX_INIT_CB
468 @@ -151,3 +162,14 @@ mutex_boot(void)
477 +#ifndef JEMALLOC_MUTEX_INIT_CB
478 + return (malloc_mutex_first_thread());
483 diff --git a/src/util.c b/src/util.c
484 index 02673c7..116e981 100644
487 @@ -66,6 +66,22 @@ wrtmessage(void *cbopaque, const char *s)
489 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
491 +JEMALLOC_ATTR(visibility("hidden"))
493 +wrtmessage_1_0(const char *s1, const char *s2, const char *s3,
497 + wrtmessage(NULL, s1);
498 + wrtmessage(NULL, s2);
499 + wrtmessage(NULL, s3);
500 + wrtmessage(NULL, s4);
503 +void (*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
504 + const char *s4) = wrtmessage_1_0;
505 +__sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
508 * Wrapper around malloc_message() that avoids the need for
509 * je_malloc_message(...) throughout the code.