1diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
2index abd5e6f..1d7491a 100644
3--- a/doc/jemalloc.xml.in
4+++ b/doc/jemalloc.xml.in
5@@ -51,12 +51,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-dss</option>,
12+    <option>--enable-experimental</option>, <option>--enable-fill</option>,
13+    <option>--enable-lazy-lock</option>, <option>--enable-munmap</option>,
14+    <option>--enable-stats</option>, <option>--enable-tcache</option>,
15+    <option>--enable-tls</option>, <option>--enable-utrace</option>, and
16+    <option>--enable-xmalloc</option>.  Additionally,
17+    <option>--enable-debug</option> is enabled in development versions of
18+    FreeBSD (controlled by the <constant>MALLOC_PRODUCTION</constant> make
19+    variable).</para>
20   </refsect1>
21   <refsynopsisdiv>
22     <title>SYNOPSIS</title>
23     <funcsynopsis>
24       <funcsynopsisinfo>#include &lt;<filename class="headerfile">stdlib.h</filename>&gt;
25-#include &lt;<filename class="headerfile">jemalloc/jemalloc.h</filename>&gt;</funcsynopsisinfo>
26+#include &lt;<filename class="headerfile">malloc_np.h</filename>&gt;</funcsynopsisinfo>
27       <refsect2>
28         <title>Standard API</title>
29         <funcprototype>
30@@ -2180,4 +2191,16 @@ malloc_conf = "lg_chunk:24";]]></programlisting></para>
31     <para>The <function>posix_memalign<parameter/></function> function conforms
32     to IEEE Std 1003.1-2001 (&ldquo;POSIX.1&rdquo;).</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>,
42+    <function>mallctl*<parameter/></function>, and
43+    <function>*allocm<parameter/></function> functions first appeared in
44+    FreeBSD 10.0.</para>
45+  </refsect1>
46 </refentry>
47diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
48index e46ac54..527449d 100644
49--- a/include/jemalloc/internal/jemalloc_internal.h.in
50+++ b/include/jemalloc/internal/jemalloc_internal.h.in
51@@ -1,5 +1,8 @@
52 #ifndef JEMALLOC_INTERNAL_H
53 #define JEMALLOC_INTERNAL_H
54+#include "libc_private.h"
55+#include "namespace.h"
56+
57 #include <math.h>
58 #ifdef _WIN32
59 #  include <windows.h>
60@@ -54,6 +57,9 @@ typedef intptr_t ssize_t;
61 #endif
62 #include <fcntl.h>
63 
64+#include "un-namespace.h"
65+#include "libc_private.h"
66+
67 #define	JEMALLOC_NO_DEMANGLE
68 #include "../jemalloc@install_suffix@.h"
69 
70@@ -95,13 +101,7 @@ static const bool config_fill =
71     false
72 #endif
73     ;
74-static const bool config_lazy_lock =
75-#ifdef JEMALLOC_LAZY_LOCK
76-    true
77-#else
78-    false
79-#endif
80-    ;
81+static const bool config_lazy_lock = true;
82 static const bool config_prof =
83 #ifdef JEMALLOC_PROF
84     true
85diff --git a/include/jemalloc/internal/mutex.h b/include/jemalloc/internal/mutex.h
86index de44e14..564d604 100644
87--- a/include/jemalloc/internal/mutex.h
88+++ b/include/jemalloc/internal/mutex.h
89@@ -43,9 +43,6 @@ struct malloc_mutex_s {
90 
91 #ifdef JEMALLOC_LAZY_LOCK
92 extern bool isthreaded;
93-#else
94-#  undef isthreaded /* Undo private_namespace.h definition. */
95-#  define isthreaded true
96 #endif
97 
98 bool	malloc_mutex_init(malloc_mutex_t *mutex);
99diff --git a/include/jemalloc/internal/private_namespace.h b/include/jemalloc/internal/private_namespace.h
100index 65de316..366676b 100644
101--- a/include/jemalloc/internal/private_namespace.h
102+++ b/include/jemalloc/internal/private_namespace.h
103@@ -216,7 +216,6 @@
104 #define	iralloc JEMALLOC_N(iralloc)
105 #define	irallocx JEMALLOC_N(irallocx)
106 #define	isalloc JEMALLOC_N(isalloc)
107-#define	isthreaded JEMALLOC_N(isthreaded)
108 #define	ivsalloc JEMALLOC_N(ivsalloc)
109 #define	jemalloc_postfork_child JEMALLOC_N(jemalloc_postfork_child)
110 #define	jemalloc_postfork_parent JEMALLOC_N(jemalloc_postfork_parent)
111diff --git a/include/jemalloc/jemalloc.h.in b/include/jemalloc/jemalloc.h.in
112index 31b1304..c3ef2f5 100644
113--- a/include/jemalloc/jemalloc.h.in
114+++ b/include/jemalloc/jemalloc.h.in
115@@ -15,6 +15,7 @@ extern "C" {
116 #define	JEMALLOC_VERSION_GID "@jemalloc_version_gid@"
117 
118 #include "jemalloc_defs@install_suffix@.h"
119+#include "jemalloc_FreeBSD.h"
120 
121 #ifdef JEMALLOC_EXPERIMENTAL
122 #define	ALLOCM_LG_ALIGN(la)	(la)
123diff --git a/include/jemalloc/jemalloc_FreeBSD.h b/include/jemalloc/jemalloc_FreeBSD.h
124new file mode 100644
125index 0000000..e6c8407
126--- /dev/null
127+++ b/include/jemalloc/jemalloc_FreeBSD.h
128@@ -0,0 +1,117 @@
129+/*
130+ * Override settings that were generated in jemalloc_defs.h as necessary.
131+ */
132+
133+#undef JEMALLOC_OVERRIDE_VALLOC
134+
135+#ifndef MALLOC_PRODUCTION
136+#define	JEMALLOC_DEBUG
137+#endif
138+
139+/*
140+ * The following are architecture-dependent, so conditionally define them for
141+ * each supported architecture.
142+ */
143+#undef CPU_SPINWAIT
144+#undef JEMALLOC_TLS_MODEL
145+#undef STATIC_PAGE_SHIFT
146+#undef LG_SIZEOF_PTR
147+#undef LG_SIZEOF_INT
148+#undef LG_SIZEOF_LONG
149+#undef LG_SIZEOF_INTMAX_T
150+
151+#ifdef __i386__
152+#  define LG_SIZEOF_PTR		2
153+#  define CPU_SPINWAIT		__asm__ volatile("pause")
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 CPU_SPINWAIT		__asm__ volatile("pause")
166+#  define JEMALLOC_TLS_MODEL	__attribute__((tls_model("initial-exec")))
167+#endif
168+#ifdef __arm__
169+#  define LG_SIZEOF_PTR		2
170+#endif
171+#ifdef __mips__
172+#ifdef __mips_n64
173+#  define LG_SIZEOF_PTR		3
174+#else
175+#  define LG_SIZEOF_PTR		2
176+#endif
177+#endif
178+#ifdef __powerpc64__
179+#  define LG_SIZEOF_PTR		3
180+#elif defined(__powerpc__)
181+#  define LG_SIZEOF_PTR		2
182+#endif
183+
184+#ifndef JEMALLOC_TLS_MODEL
185+#  define JEMALLOC_TLS_MODEL	/* Default. */
186+#endif
187+
188+#define	STATIC_PAGE_SHIFT	PAGE_SHIFT
189+#define	LG_SIZEOF_INT		2
190+#define	LG_SIZEOF_LONG		LG_SIZEOF_PTR
191+#define	LG_SIZEOF_INTMAX_T	3
192+
193+/* Disable lazy-lock machinery, mangle isthreaded, and adjust its type. */
194+#undef JEMALLOC_LAZY_LOCK
195+extern int __isthreaded;
196+#define	isthreaded		((bool)__isthreaded)
197+
198+/* Mangle. */
199+#undef je_malloc
200+#undef je_calloc
201+#undef je_realloc
202+#undef je_free
203+#undef je_posix_memalign
204+#undef je_malloc_usable_size
205+#undef je_allocm
206+#undef je_rallocm
207+#undef je_sallocm
208+#undef je_dallocm
209+#undef je_nallocm
210+#define	je_malloc		__malloc
211+#define	je_calloc		__calloc
212+#define	je_realloc		__realloc
213+#define	je_free			__free
214+#define	je_posix_memalign	__posix_memalign
215+#define	je_malloc_usable_size	__malloc_usable_size
216+#define	je_allocm		__allocm
217+#define	je_rallocm		__rallocm
218+#define	je_sallocm		__sallocm
219+#define	je_dallocm		__dallocm
220+#define	je_nallocm		__nallocm
221+#define	open			_open
222+#define	read			_read
223+#define	write			_write
224+#define	close			_close
225+#define	pthread_mutex_lock	_pthread_mutex_lock
226+#define	pthread_mutex_unlock	_pthread_mutex_unlock
227+
228+#ifdef JEMALLOC_C_
229+/*
230+ * Define 'weak' symbols so that an application can have its own versions
231+ * of malloc, calloc, realloc, free, et al.
232+ */
233+__weak_reference(__malloc, malloc);
234+__weak_reference(__calloc, calloc);
235+__weak_reference(__realloc, realloc);
236+__weak_reference(__free, free);
237+__weak_reference(__posix_memalign, posix_memalign);
238+__weak_reference(__malloc_usable_size, malloc_usable_size);
239+__weak_reference(__allocm, allocm);
240+__weak_reference(__rallocm, rallocm);
241+__weak_reference(__sallocm, sallocm);
242+__weak_reference(__dallocm, dallocm);
243+__weak_reference(__nallocm, nallocm);
244+#endif
245+
246diff --git a/src/jemalloc.c b/src/jemalloc.c
247index bc350ed..352c98e 100644
248--- a/src/jemalloc.c
249+++ b/src/jemalloc.c
250@@ -8,6 +8,10 @@ malloc_tsd_data(, arenas, arena_t *, NULL)
251 malloc_tsd_data(, thread_allocated, thread_allocated_t,
252     THREAD_ALLOCATED_INITIALIZER)
253 
254+/* Work around <http://llvm.org/bugs/show_bug.cgi?id=12623>: */
255+const char	*__malloc_options_1_0 = NULL;
256+__sym_compat(_malloc_options, __malloc_options_1_0, FBSD_1.0);
257+
258 /* Runtime configuration options. */
259 const char	*je_malloc_conf;
260 bool	opt_abort =
261@@ -471,7 +475,8 @@ malloc_conf_init(void)
262 #endif
263 			    ;
264 
265-			if ((opts = getenv(envname)) != NULL) {
266+			if (issetugid() == 0 && (opts = getenv(envname)) !=
267+			    NULL) {
268 				/*
269 				 * Do nothing; opts is already initialized to
270 				 * the value of the MALLOC_CONF environment
271diff --git a/src/mutex.c b/src/mutex.c
272index 55e18c2..6b6f438 100644
273--- a/src/mutex.c
274+++ b/src/mutex.c
275@@ -66,6 +66,17 @@ pthread_create(pthread_t *__restrict thread,
276 #ifdef JEMALLOC_MUTEX_INIT_CB
277 JEMALLOC_EXPORT int	_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
278     void *(calloc_cb)(size_t, size_t));
279+
280+__weak_reference(_pthread_mutex_init_calloc_cb_stub,
281+    _pthread_mutex_init_calloc_cb);
282+
283+int
284+_pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex,
285+    void *(calloc_cb)(size_t, size_t))
286+{
287+
288+	return (0);
289+}
290 #endif
291 
292 bool
293diff --git a/src/util.c b/src/util.c
294index b3a0114..df1c5d5 100644
295--- a/src/util.c
296+++ b/src/util.c
297@@ -58,6 +58,22 @@ wrtmessage(void *cbopaque, const char *s)
298 
299 JEMALLOC_EXPORT void	(*je_malloc_message)(void *, const char *s);
300 
301+JEMALLOC_ATTR(visibility("hidden"))
302+void
303+wrtmessage_1_0(const char *s1, const char *s2, const char *s3,
304+    const char *s4)
305+{
306+
307+	wrtmessage(NULL, s1);
308+	wrtmessage(NULL, s2);
309+	wrtmessage(NULL, s3);
310+	wrtmessage(NULL, s4);
311+}
312+
313+void	(*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
314+    const char *s4) = wrtmessage_1_0;
315+__sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
316+
317 /*
318  * Wrapper around malloc_message() that avoids the need for
319  * je_malloc_message(...) throughout the code.
320