1 diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
2 index c4a44e3..4626e9b 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 @@ -2961,4 +2973,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/arena.h b/include/jemalloc/internal/arena.h
50 index b1de2b6..da6b6d2 100644
51 --- a/include/jemalloc/internal/arena.h
52 +++ b/include/jemalloc/internal/arena.h
53 @@ -718,8 +718,13 @@ arena_miscelm_get_mutable(arena_chunk_t *chunk, size_t pageind)
54 JEMALLOC_ALWAYS_INLINE const arena_chunk_map_misc_t *
55 arena_miscelm_get_const(const arena_chunk_t *chunk, size_t pageind)
57 +#if 1 /* Work around gcc bug. */
58 + arena_chunk_t *mchunk = (arena_chunk_t *)chunk;
60 + return (arena_miscelm_get_mutable(mchunk, pageind));
62 return (arena_miscelm_get_mutable((arena_chunk_t *)chunk, pageind));
66 JEMALLOC_ALWAYS_INLINE size_t
67 @@ -778,8 +783,13 @@ arena_mapbitsp_get_mutable(arena_chunk_t *chunk, size_t pageind)
68 JEMALLOC_ALWAYS_INLINE const size_t *
69 arena_mapbitsp_get_const(const arena_chunk_t *chunk, size_t pageind)
71 +#if 1 /* Work around gcc bug. */
72 + arena_chunk_t *mchunk = (arena_chunk_t *)chunk;
74 + return (arena_mapbitsp_get_mutable(mchunk, pageind));
76 return (arena_mapbitsp_get_mutable((arena_chunk_t *)chunk, pageind));
80 JEMALLOC_ALWAYS_INLINE size_t
81 diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
82 index 8f82edd..78e2df2 100644
83 --- a/include/jemalloc/internal/jemalloc_internal.h.in
84 +++ b/include/jemalloc/internal/jemalloc_internal.h.in
86 #include <sys/ktrace.h>
89 +#include "un-namespace.h"
90 +#include "libc_private.h"
92 #define JEMALLOC_NO_DEMANGLE
94 # define JEMALLOC_N(n) jet_##n
95 @@ -42,13 +45,7 @@ static const bool config_fill =
99 -static const bool config_lazy_lock =
100 -#ifdef JEMALLOC_LAZY_LOCK
106 +static const bool config_lazy_lock = true;
107 static const char * const config_malloc_conf = JEMALLOC_CONFIG_MALLOC_CONF;
108 static const bool config_prof =
110 diff --git a/include/jemalloc/internal/jemalloc_internal_decls.h b/include/jemalloc/internal/jemalloc_internal_decls.h
111 index 2b8ca5d..42d97f2 100644
112 --- a/include/jemalloc/internal/jemalloc_internal_decls.h
113 +++ b/include/jemalloc/internal/jemalloc_internal_decls.h
115 #ifndef JEMALLOC_INTERNAL_DECLS_H
116 #define JEMALLOC_INTERNAL_DECLS_H
118 +#include "libc_private.h"
119 +#include "namespace.h"
123 # include <windows.h>
124 diff --git a/include/jemalloc/internal/mutex.h b/include/jemalloc/internal/mutex.h
125 index 5221799..60ab041 100644
126 --- a/include/jemalloc/internal/mutex.h
127 +++ b/include/jemalloc/internal/mutex.h
128 @@ -52,9 +52,6 @@ struct malloc_mutex_s {
130 #ifdef JEMALLOC_LAZY_LOCK
131 extern bool isthreaded;
133 -# undef isthreaded /* Undo private_namespace.h definition. */
134 -# define isthreaded true
137 bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
138 @@ -62,6 +59,7 @@ bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
139 void malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);
140 void malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);
141 void malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);
142 +bool malloc_mutex_first_thread(void);
143 bool malloc_mutex_boot(void);
145 #endif /* JEMALLOC_H_EXTERNS */
146 diff --git a/include/jemalloc/internal/private_symbols.txt b/include/jemalloc/internal/private_symbols.txt
147 index f2b6a55..69369c9 100644
148 --- a/include/jemalloc/internal/private_symbols.txt
149 +++ b/include/jemalloc/internal/private_symbols.txt
150 @@ -311,7 +311,6 @@ iralloct_realign
157 jemalloc_postfork_child
158 diff --git a/include/jemalloc/jemalloc_FreeBSD.h b/include/jemalloc/jemalloc_FreeBSD.h
160 index 0000000..c58a8f3
162 +++ b/include/jemalloc/jemalloc_FreeBSD.h
165 + * Override settings that were generated in jemalloc_defs.h as necessary.
168 +#undef JEMALLOC_OVERRIDE_VALLOC
170 +#ifndef MALLOC_PRODUCTION
171 +#define JEMALLOC_DEBUG
177 + * The following are architecture-dependent, so conditionally define them for
178 + * each supported architecture.
180 +#undef JEMALLOC_TLS_MODEL
181 +#undef STATIC_PAGE_SHIFT
182 +#undef LG_SIZEOF_PTR
183 +#undef LG_SIZEOF_INT
184 +#undef LG_SIZEOF_LONG
185 +#undef LG_SIZEOF_INTMAX_T
188 +# define LG_SIZEOF_PTR 2
189 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
192 +# define LG_SIZEOF_PTR 3
195 +# define LG_SIZEOF_PTR 3
196 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
199 +# define LG_SIZEOF_PTR 3
200 +# define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
203 +# define LG_SIZEOF_PTR 2
206 +# define LG_SIZEOF_PTR 3
210 +# define LG_SIZEOF_PTR 3
212 +# define LG_SIZEOF_PTR 2
215 +#ifdef __powerpc64__
216 +# define LG_SIZEOF_PTR 3
217 +#elif defined(__powerpc__)
218 +# define LG_SIZEOF_PTR 2
221 +# define LG_SIZEOF_PTR 3
224 +#ifndef JEMALLOC_TLS_MODEL
225 +# define JEMALLOC_TLS_MODEL /* Default. */
228 +#define STATIC_PAGE_SHIFT PAGE_SHIFT
229 +#define LG_SIZEOF_INT 2
230 +#define LG_SIZEOF_LONG LG_SIZEOF_PTR
231 +#define LG_SIZEOF_INTMAX_T 3
234 +#include <machine/cpu.h>
235 +#include <machine/cpufunc.h>
236 +#define CPU_SPINWAIT cpu_spinwait()
238 +/* Disable lazy-lock machinery, mangle isthreaded, and adjust its type. */
239 +#undef JEMALLOC_LAZY_LOCK
240 +extern int __isthreaded;
241 +#define isthreaded ((bool)__isthreaded)
246 +#undef je_posix_memalign
247 +#undef je_aligned_alloc
250 +#undef je_malloc_usable_size
259 +#undef je_mallctlnametomib
260 +#undef je_mallctlbymib
261 +#undef je_malloc_stats_print
267 +#define je_malloc __malloc
268 +#define je_calloc __calloc
269 +#define je_posix_memalign __posix_memalign
270 +#define je_aligned_alloc __aligned_alloc
271 +#define je_realloc __realloc
272 +#define je_free __free
273 +#define je_malloc_usable_size __malloc_usable_size
274 +#define je_mallocx __mallocx
275 +#define je_rallocx __rallocx
276 +#define je_xallocx __xallocx
277 +#define je_sallocx __sallocx
278 +#define je_dallocx __dallocx
279 +#define je_sdallocx __sdallocx
280 +#define je_nallocx __nallocx
281 +#define je_mallctl __mallctl
282 +#define je_mallctlnametomib __mallctlnametomib
283 +#define je_mallctlbymib __mallctlbymib
284 +#define je_malloc_stats_print __malloc_stats_print
285 +#define je_allocm __allocm
286 +#define je_rallocm __rallocm
287 +#define je_sallocm __sallocm
288 +#define je_dallocm __dallocm
289 +#define je_nallocm __nallocm
292 +#define write _write
293 +#define close _close
294 +#define pthread_mutex_lock _pthread_mutex_lock
295 +#define pthread_mutex_unlock _pthread_mutex_unlock
299 + * Define 'weak' symbols so that an application can have its own versions
300 + * of malloc, calloc, realloc, free, et al.
302 +__weak_reference(__malloc, malloc);
303 +__weak_reference(__calloc, calloc);
304 +__weak_reference(__posix_memalign, posix_memalign);
305 +__weak_reference(__aligned_alloc, aligned_alloc);
306 +__weak_reference(__realloc, realloc);
307 +__weak_reference(__free, free);
308 +__weak_reference(__malloc_usable_size, malloc_usable_size);
309 +__weak_reference(__mallocx, mallocx);
310 +__weak_reference(__rallocx, rallocx);
311 +__weak_reference(__xallocx, xallocx);
312 +__weak_reference(__sallocx, sallocx);
313 +__weak_reference(__dallocx, dallocx);
314 +__weak_reference(__sdallocx, sdallocx);
315 +__weak_reference(__nallocx, nallocx);
316 +__weak_reference(__mallctl, mallctl);
317 +__weak_reference(__mallctlnametomib, mallctlnametomib);
318 +__weak_reference(__mallctlbymib, mallctlbymib);
319 +__weak_reference(__malloc_stats_print, malloc_stats_print);
320 +__weak_reference(__allocm, allocm);
321 +__weak_reference(__rallocm, rallocm);
322 +__weak_reference(__sallocm, sallocm);
323 +__weak_reference(__dallocm, dallocm);
324 +__weak_reference(__nallocm, nallocm);
326 diff --git a/include/jemalloc/jemalloc_rename.sh b/include/jemalloc/jemalloc_rename.sh
327 index f943891..47d032c 100755
328 --- a/include/jemalloc/jemalloc_rename.sh
329 +++ b/include/jemalloc/jemalloc_rename.sh
330 @@ -19,4 +19,6 @@ done
335 +#include "jemalloc_FreeBSD.h"
337 diff --git a/src/jemalloc.c b/src/jemalloc.c
338 index 5d1f493..46dd1d1 100644
342 /******************************************************************************/
345 +/* Work around <http://llvm.org/bugs/show_bug.cgi?id=12623>: */
346 +const char *__malloc_options_1_0 = NULL;
347 +__sym_compat(_malloc_options, __malloc_options_1_0, FBSD_1.0);
349 /* Runtime configuration options. */
350 const char *je_malloc_conf JEMALLOC_ATTR(weak);
352 @@ -2673,6 +2677,107 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr)
354 /******************************************************************************/
356 + * Begin compatibility functions.
359 +#define ALLOCM_LG_ALIGN(la) (la)
360 +#define ALLOCM_ALIGN(a) (ffsl(a)-1)
361 +#define ALLOCM_ZERO ((int)0x40)
362 +#define ALLOCM_NO_MOVE ((int)0x80)
364 +#define ALLOCM_SUCCESS 0
365 +#define ALLOCM_ERR_OOM 1
366 +#define ALLOCM_ERR_NOT_MOVED 2
369 +je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
373 + assert(ptr != NULL);
375 + p = je_mallocx(size, flags);
377 + return (ALLOCM_ERR_OOM);
379 + *rsize = isalloc(tsdn_fetch(), p, config_prof);
381 + return (ALLOCM_SUCCESS);
385 +je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
388 + bool no_move = flags & ALLOCM_NO_MOVE;
390 + assert(ptr != NULL);
391 + assert(*ptr != NULL);
393 + assert(SIZE_T_MAX - size >= extra);
396 + size_t usize = je_xallocx(*ptr, size, extra, flags);
397 + ret = (usize >= size) ? ALLOCM_SUCCESS : ALLOCM_ERR_NOT_MOVED;
401 + void *p = je_rallocx(*ptr, size+extra, flags);
404 + ret = ALLOCM_SUCCESS;
406 + ret = ALLOCM_ERR_OOM;
408 + *rsize = isalloc(tsdn_fetch(), *ptr, config_prof);
414 +je_sallocm(const void *ptr, size_t *rsize, int flags)
417 + assert(rsize != NULL);
418 + *rsize = je_sallocx(ptr, flags);
419 + return (ALLOCM_SUCCESS);
423 +je_dallocm(void *ptr, int flags)
426 + je_dallocx(ptr, flags);
427 + return (ALLOCM_SUCCESS);
431 +je_nallocm(size_t *rsize, size_t size, int flags)
435 + usize = je_nallocx(size, flags);
437 + return (ALLOCM_ERR_OOM);
440 + return (ALLOCM_SUCCESS);
443 +#undef ALLOCM_LG_ALIGN
446 +#undef ALLOCM_NO_MOVE
448 +#undef ALLOCM_SUCCESS
449 +#undef ALLOCM_ERR_OOM
450 +#undef ALLOCM_ERR_NOT_MOVED
453 + * End compatibility functions.
455 +/******************************************************************************/
457 * The following functions are used by threading libraries for protection of
458 * malloc during fork().
460 @@ -2814,4 +2919,11 @@ jemalloc_postfork_child(void)
461 ctl_postfork_child(tsd_tsdn(tsd));
465 +_malloc_first_thread(void)
468 + (void)malloc_mutex_first_thread();
471 /******************************************************************************/
472 diff --git a/src/mutex.c b/src/mutex.c
473 index a1fac34..a24e420 100644
476 @@ -66,6 +66,17 @@ pthread_create(pthread_t *__restrict thread,
477 #ifdef JEMALLOC_MUTEX_INIT_CB
478 JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
479 void *(calloc_cb)(size_t, size_t));
481 +#pragma weak _pthread_mutex_init_calloc_cb
483 +_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
484 + void *(calloc_cb)(size_t, size_t))
487 + return (((int (*)(pthread_mutex_t *, void *(*)(size_t, size_t)))
488 + __libc_interposing[INTERPOS__pthread_mutex_init_calloc_cb])(mutex,
494 @@ -140,7 +151,7 @@ malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex)
498 -malloc_mutex_boot(void)
499 +malloc_mutex_first_thread(void)
502 #ifdef JEMALLOC_MUTEX_INIT_CB
503 @@ -154,3 +165,14 @@ malloc_mutex_boot(void)
509 +malloc_mutex_boot(void)
512 +#ifndef JEMALLOC_MUTEX_INIT_CB
513 + return (malloc_mutex_first_thread());
518 diff --git a/src/util.c b/src/util.c
519 index a1c4a2a..04f9153 100644
522 @@ -67,6 +67,22 @@ wrtmessage(void *cbopaque, const char *s)
524 JEMALLOC_EXPORT void (*je_malloc_message)(void *, const char *s);
526 +JEMALLOC_ATTR(visibility("hidden"))
528 +wrtmessage_1_0(const char *s1, const char *s2, const char *s3,
532 + wrtmessage(NULL, s1);
533 + wrtmessage(NULL, s2);
534 + wrtmessage(NULL, s3);
535 + wrtmessage(NULL, s4);
538 +void (*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
539 + const char *s4) = wrtmessage_1_0;
540 +__sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
543 * Wrapper around malloc_message() that avoids the need for
544 * je_malloc_message(...) throughout the code.