Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
index 1e12fd3a..c42a7e10 100644
--- a/doc/jemalloc.xml.in
+++ b/doc/jemalloc.xml.in
@@ -53,11 +53,22 @@
     <para>This manual describes jemalloc @jemalloc_version@.  More information
     can be found at the <ulink
     url="http://jemalloc.net/">jemalloc website</ulink>.</para>
+
+    <para>The following configuration options are enabled in libc's built-in
+    jemalloc: <option>--enable-fill</option>,
+    <option>--enable-lazy-lock</option>, <option>--enable-stats</option>,
+    <option>--enable-utrace</option>, <option>--enable-xmalloc</option>, and
+    <option>--with-malloc-conf=abort_conf:false</option>.
+    Additionally, <option>--enable-debug</option> is enabled in development
+    versions of FreeBSD (controlled by the
+    <constant>MALLOC_PRODUCTION</constant> make variable).</para>
+
   </refsect1>
   <refsynopsisdiv>
     <title>SYNOPSIS</title>
     <funcsynopsis>
-      <funcsynopsisinfo>#include &lt;<filename class="headerfile">jemalloc/jemalloc.h</filename>&gt;</funcsynopsisinfo>
+      <funcsynopsisinfo>#include &lt;<filename class="headerfile">stdlib.h</filename>&gt;
+#include &lt;<filename class="headerfile">malloc_np.h</filename>&gt;</funcsynopsisinfo>
       <refsect2>
         <title>Standard API</title>
         <funcprototype>
@@ -3376,4 +3387,18 @@ malloc_conf = "narenas:1";]]></programlisting></para>
     <para>The <function>posix_memalign()</function> function conforms
     to IEEE Std 1003.1-2001 (<quote>POSIX.1</quote>).</para>
   </refsect1>
+  <refsect1 id="history">
+    <title>HISTORY</title>
+    <para>The <function>malloc_usable_size()</function> and
+    <function>posix_memalign()</function> functions first appeared in FreeBSD
+    7.0.</para>
+
+    <para>The <function>aligned_alloc()</function>,
+    <function>malloc_stats_print()</function>, and
+    <function>mallctl*()</function> functions first appeared in FreeBSD
+    10.0.</para>
+
+    <para>The <function>*allocx()</function> functions first appeared in FreeBSD
+    11.0.</para>
+  </refsect1>
 </refentry>
diff --git a/include/jemalloc/internal/hooks.h b/include/jemalloc/internal/hooks.h
index cd49afcb..85e2a991 100644
--- a/include/jemalloc/internal/hooks.h
+++ b/include/jemalloc/internal/hooks.h
@@ -6,13 +6,6 @@ extern JEMALLOC_EXPORT void (*hooks_libc_hook)();
 
 #define JEMALLOC_HOOK(fn, hook) ((void)(hook != NULL && (hook(), 0)), fn)
 
-#define open JEMALLOC_HOOK(open, hooks_libc_hook)
-#define read JEMALLOC_HOOK(read, hooks_libc_hook)
-#define write JEMALLOC_HOOK(write, hooks_libc_hook)
-#define readlink JEMALLOC_HOOK(readlink, hooks_libc_hook)
-#define close JEMALLOC_HOOK(close, hooks_libc_hook)
-#define creat JEMALLOC_HOOK(creat, hooks_libc_hook)
-#define secure_getenv JEMALLOC_HOOK(secure_getenv, hooks_libc_hook)
 /* Note that this is undef'd and re-define'd in src/prof.c. */
 #define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, hooks_libc_hook)
 
diff --git a/include/jemalloc/internal/jemalloc_internal_decls.h b/include/jemalloc/internal/jemalloc_internal_decls.h
index be70df51..84cd70da 100644
--- a/include/jemalloc/internal/jemalloc_internal_decls.h
+++ b/include/jemalloc/internal/jemalloc_internal_decls.h
@@ -1,6 +1,9 @@
 #ifndef JEMALLOC_INTERNAL_DECLS_H
 #define JEMALLOC_INTERNAL_DECLS_H
 
+#include "libc_private.h"
+#include "namespace.h"
+
 #include <math.h>
 #ifdef _WIN32
 #  include <windows.h>
diff --git a/include/jemalloc/internal/jemalloc_preamble.h.in b/include/jemalloc/internal/jemalloc_preamble.h.in
index e621fbc8..dbdd5d6b 100644
--- a/include/jemalloc/internal/jemalloc_preamble.h.in
+++ b/include/jemalloc/internal/jemalloc_preamble.h.in
@@ -8,6 +8,9 @@
 #include <sys/ktrace.h>
 #endif
 
+#include "un-namespace.h"
+#include "libc_private.h"
+
 #define JEMALLOC_NO_DEMANGLE
 #ifdef JEMALLOC_JET
 #  undef JEMALLOC_IS_MALLOC
@@ -79,13 +82,7 @@ static const bool config_fill =
     false
 #endif
     ;
-static const bool config_lazy_lock =
-#ifdef JEMALLOC_LAZY_LOCK
-    true
-#else
-    false
-#endif
-    ;
+static const bool config_lazy_lock = true;
 static const char * const config_malloc_conf = JEMALLOC_CONFIG_MALLOC_CONF;
 static const bool config_prof =
 #ifdef JEMALLOC_PROF
diff --git a/include/jemalloc/internal/mutex.h b/include/jemalloc/internal/mutex.h
index 6520c251..0013cbe9 100644
--- a/include/jemalloc/internal/mutex.h
+++ b/include/jemalloc/internal/mutex.h
@@ -121,9 +121,6 @@ struct malloc_mutex_s {
 
 #ifdef JEMALLOC_LAZY_LOCK
 extern bool isthreaded;
-#else
-#  undef isthreaded /* Undo private_namespace.h definition. */
-#  define isthreaded true
 #endif
 
 bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
@@ -131,6 +128,7 @@ bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
 void malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);
 void malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);
 void malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);
+bool malloc_mutex_first_thread(void);
 bool malloc_mutex_boot(void);
 void malloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex);
 
diff --git a/include/jemalloc/internal/tsd.h b/include/jemalloc/internal/tsd.h
index 0b9841aa..f03eee61 100644
--- a/include/jemalloc/internal/tsd.h
+++ b/include/jemalloc/internal/tsd.h
@@ -122,7 +122,8 @@ struct tsd_s {
 	t use_a_getter_or_setter_instead_##n;
 MALLOC_TSD
 #undef O
-};
+/* AddressSanitizer requires TLS data to be aligned to at least 8 bytes. */
+} JEMALLOC_ALIGNED(16);
 
 /*
  * Wrapper around tsd_t that makes it possible to avoid implicit conversion
diff --git a/include/jemalloc/jemalloc_FreeBSD.h b/include/jemalloc/jemalloc_FreeBSD.h
new file mode 100644
index 00000000..b752b0e7
--- /dev/null
+++ b/include/jemalloc/jemalloc_FreeBSD.h
@@ -0,0 +1,185 @@
+/*
+ * Override settings that were generated in jemalloc_defs.h as necessary.
+ */
+
+#undef JEMALLOC_OVERRIDE_VALLOC
+
+#ifndef MALLOC_PRODUCTION
+#define	JEMALLOC_DEBUG
+#endif
+
+#undef JEMALLOC_DSS
+
+#undef JEMALLOC_BACKGROUND_THREAD
+
+/*
+ * The following are architecture-dependent, so conditionally define them for
+ * each supported architecture.
+ */
+#undef JEMALLOC_TLS_MODEL
+#undef LG_PAGE
+#undef LG_VADDR
+#undef LG_SIZEOF_PTR
+#undef LG_SIZEOF_INT
+#undef LG_SIZEOF_LONG
+#undef LG_SIZEOF_INTMAX_T
+
+#ifdef __i386__
+#  define LG_VADDR		32
+#  define LG_SIZEOF_PTR		2
+#  define JEMALLOC_TLS_MODEL	__attribute__((tls_model("initial-exec")))
+#endif
+#ifdef __ia64__
+#  define LG_VADDR		64
+#  define LG_SIZEOF_PTR		3
+#endif
+#ifdef __sparc64__
+#  define LG_VADDR		64
+#  define LG_SIZEOF_PTR		3
+#  define JEMALLOC_TLS_MODEL	__attribute__((tls_model("initial-exec")))
+#endif
+#ifdef __amd64__
+#  define LG_VADDR		48
+#  define LG_SIZEOF_PTR		3
+#  define JEMALLOC_TLS_MODEL	__attribute__((tls_model("initial-exec")))
+#endif
+#ifdef __arm__
+#  define LG_VADDR		32
+#  define LG_SIZEOF_PTR		2
+#endif
+#ifdef __aarch64__
+#  define LG_VADDR		48
+#  define LG_SIZEOF_PTR		3
+#endif
+#ifdef __mips__
+#ifdef __mips_n64
+#  define LG_VADDR		64
+#  define LG_SIZEOF_PTR		3
+#else
+#  define LG_VADDR		32
+#  define LG_SIZEOF_PTR		2
+#endif
+#endif
+#ifdef __powerpc64__
+#  define LG_VADDR		64
+#  define LG_SIZEOF_PTR		3
+#elif defined(__powerpc__)
+#  define LG_VADDR		32
+#  define LG_SIZEOF_PTR		2
+#endif
+#ifdef __riscv
+#  define LG_VADDR		64
+#  define LG_SIZEOF_PTR		3
+#endif
+
+#ifndef JEMALLOC_TLS_MODEL
+#  define JEMALLOC_TLS_MODEL	/* Default. */
+#endif
+
+#define	LG_PAGE			PAGE_SHIFT
+#define	LG_SIZEOF_INT		2
+#define	LG_SIZEOF_LONG		LG_SIZEOF_PTR
+#define	LG_SIZEOF_INTMAX_T	3
+
+#undef CPU_SPINWAIT
+#include <machine/cpu.h>
+#include <machine/cpufunc.h>
+#define	CPU_SPINWAIT		cpu_spinwait()
+
+/* Disable lazy-lock machinery, mangle isthreaded, and adjust its type. */
+#undef JEMALLOC_LAZY_LOCK
+extern int __isthreaded;
+#define	isthreaded		((bool)__isthreaded)
+
+/* Mangle. */
+#undef je_malloc
+#undef je_calloc
+#undef je_posix_memalign
+#undef je_aligned_alloc
+#undef je_realloc
+#undef je_free
+#undef je_malloc_usable_size
+#undef je_mallocx
+#undef je_rallocx
+#undef je_xallocx
+#undef je_sallocx
+#undef je_dallocx
+#undef je_sdallocx
+#undef je_nallocx
+#undef je_mallctl
+#undef je_mallctlnametomib
+#undef je_mallctlbymib
+#undef je_malloc_stats_print
+#undef je_allocm
+#undef je_rallocm
+#undef je_sallocm
+#undef je_dallocm
+#undef je_nallocm
+#define	je_malloc		__malloc
+#define	je_calloc		__calloc
+#define	je_posix_memalign	__posix_memalign
+#define	je_aligned_alloc	__aligned_alloc
+#define	je_realloc		__realloc
+#define	je_free			__free
+#define	je_malloc_usable_size	__malloc_usable_size
+#define	je_mallocx		__mallocx
+#define	je_rallocx		__rallocx
+#define	je_xallocx		__xallocx
+#define	je_sallocx		__sallocx
+#define	je_dallocx		__dallocx
+#define	je_sdallocx		__sdallocx
+#define	je_nallocx		__nallocx
+#define	je_mallctl		__mallctl
+#define	je_mallctlnametomib	__mallctlnametomib
+#define	je_mallctlbymib		__mallctlbymib
+#define	je_malloc_stats_print	__malloc_stats_print
+#define	je_allocm		__allocm
+#define	je_rallocm		__rallocm
+#define	je_sallocm		__sallocm
+#define	je_dallocm		__dallocm
+#define	je_nallocm		__nallocm
+#define	open			_open
+#define	read			_read
+#define	write			_write
+#define	close			_close
+#define	pthread_join		_pthread_join
+#define	pthread_once		_pthread_once
+#define	pthread_self		_pthread_self
+#define	pthread_equal		_pthread_equal
+#define	pthread_mutex_lock	_pthread_mutex_lock
+#define	pthread_mutex_trylock	_pthread_mutex_trylock
+#define	pthread_mutex_unlock	_pthread_mutex_unlock
+#define	pthread_cond_init	_pthread_cond_init
+#define	pthread_cond_wait	_pthread_cond_wait
+#define	pthread_cond_timedwait	_pthread_cond_timedwait
+#define	pthread_cond_signal	_pthread_cond_signal
+
+#ifdef JEMALLOC_C_
+/*
+ * Define 'weak' symbols so that an application can have its own versions
+ * of malloc, calloc, realloc, free, et al.
+ */
+__weak_reference(__malloc, malloc);
+__weak_reference(__calloc, calloc);
+__weak_reference(__posix_memalign, posix_memalign);
+__weak_reference(__aligned_alloc, aligned_alloc);
+__weak_reference(__realloc, realloc);
+__weak_reference(__free, free);
+__weak_reference(__malloc_usable_size, malloc_usable_size);
+__weak_reference(__mallocx, mallocx);
+__weak_reference(__rallocx, rallocx);
+__weak_reference(__xallocx, xallocx);
+__weak_reference(__sallocx, sallocx);
+__weak_reference(__dallocx, dallocx);
+__weak_reference(__sdallocx, sdallocx);
+__weak_reference(__nallocx, nallocx);
+__weak_reference(__mallctl, mallctl);
+__weak_reference(__mallctlnametomib, mallctlnametomib);
+__weak_reference(__mallctlbymib, mallctlbymib);
+__weak_reference(__malloc_stats_print, malloc_stats_print);
+__weak_reference(__allocm, allocm);
+__weak_reference(__rallocm, rallocm);
+__weak_reference(__sallocm, sallocm);
+__weak_reference(__dallocm, dallocm);
+__weak_reference(__nallocm, nallocm);
+#endif
diff --git a/include/jemalloc/jemalloc_rename.sh b/include/jemalloc/jemalloc_rename.sh
index f9438912..47d032c1 100755
--- a/include/jemalloc/jemalloc_rename.sh
+++ b/include/jemalloc/jemalloc_rename.sh
@@ -19,4 +19,6 @@ done
 
 cat <<EOF
 #endif
+
+#include "jemalloc_FreeBSD.h"
 EOF
diff --git a/src/jemalloc.c b/src/jemalloc.c
index f93c16fa..e0ad297b 100644
--- a/src/jemalloc.c
+++ b/src/jemalloc.c
@@ -21,6 +21,10 @@
 /******************************************************************************/
 /* Data. */
 
+/* Work around <http://llvm.org/bugs/show_bug.cgi?id=12623>: */
+const char	*__malloc_options_1_0 = NULL;
+__sym_compat(_malloc_options, __malloc_options_1_0, FBSD_1.0);
+
 /* Runtime configuration options. */
 const char	*je_malloc_conf
 #ifndef _WIN32
@@ -3160,6 +3164,103 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) {
  */
 /******************************************************************************/
 /*
+ * Begin compatibility functions.
+ */
+
+#define	ALLOCM_LG_ALIGN(la)	(la)
+#define	ALLOCM_ALIGN(a)		(ffsl(a)-1)
+#define	ALLOCM_ZERO		((int)0x40)
+#define	ALLOCM_NO_MOVE		((int)0x80)
+
+#define	ALLOCM_SUCCESS		0
+#define	ALLOCM_ERR_OOM		1
+#define	ALLOCM_ERR_NOT_MOVED	2
+
+int
+je_allocm(void **ptr, size_t *rsize, size_t size, int flags) {
+	assert(ptr != NULL);
+
+	void *p = je_mallocx(size, flags);
+	if (p == NULL) {
+		return (ALLOCM_ERR_OOM);
+	}
+	if (rsize != NULL) {
+		*rsize = isalloc(tsdn_fetch(), p);
+	}
+	*ptr = p;
+	return ALLOCM_SUCCESS;
+}
+
+int
+je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags) {
+	assert(ptr != NULL);
+	assert(*ptr != NULL);
+	assert(size != 0);
+	assert(SIZE_T_MAX - size >= extra);
+
+	int ret;
+	bool no_move = flags & ALLOCM_NO_MOVE;
+
+	if (no_move) {
+		size_t usize = je_xallocx(*ptr, size, extra, flags);
+		ret = (usize >= size) ? ALLOCM_SUCCESS : ALLOCM_ERR_NOT_MOVED;
+		if (rsize != NULL) {
+			*rsize = usize;
+		}
+	} else {
+		void *p = je_rallocx(*ptr, size+extra, flags);
+		if (p != NULL) {
+			*ptr = p;
+			ret = ALLOCM_SUCCESS;
+		} else {
+			ret = ALLOCM_ERR_OOM;
+		}
+		if (rsize != NULL) {
+			*rsize = isalloc(tsdn_fetch(), *ptr);
+		}
+	}
+	return ret;
+}
+
+int
+je_sallocm(const void *ptr, size_t *rsize, int flags) {
+	assert(rsize != NULL);
+	*rsize = je_sallocx(ptr, flags);
+	return ALLOCM_SUCCESS;
+}
+
+int
+je_dallocm(void *ptr, int flags) {
+	je_dallocx(ptr, flags);
+	return ALLOCM_SUCCESS;
+}
+
+int
+je_nallocm(size_t *rsize, size_t size, int flags) {
+	size_t usize = je_nallocx(size, flags);
+	if (usize == 0) {
+		return ALLOCM_ERR_OOM;
+	}
+	if (rsize != NULL) {
+		*rsize = usize;
+	}
+	return ALLOCM_SUCCESS;
+}
+
+#undef ALLOCM_LG_ALIGN
+#undef ALLOCM_ALIGN
+#undef ALLOCM_ZERO
+#undef ALLOCM_NO_MOVE
+
+#undef ALLOCM_SUCCESS
+#undef ALLOCM_ERR_OOM
+#undef ALLOCM_ERR_NOT_MOVED
+
+/*
+ * End compatibility functions.
+ */
+/******************************************************************************/
+/*
  * The following functions are used by threading libraries for protection of
  * malloc during fork().
  */
@@ -3323,4 +3424,11 @@ jemalloc_postfork_child(void) {
 	ctl_postfork_child(tsd_tsdn(tsd));
 }
 
+void
+_malloc_first_thread(void)
+{
+
+	(void)malloc_mutex_first_thread();
+}
+
 /******************************************************************************/
diff --git a/src/malloc_io.c b/src/malloc_io.c
index 7bdc13f9..c8802c70 100644
--- a/src/malloc_io.c
+++ b/src/malloc_io.c
@@ -75,6 +75,20 @@ wrtmessage(void *cbopaque, const char *s) {
 
 JEMALLOC_EXPORT void	(*je_malloc_message)(void *, const char *s);
 
+JEMALLOC_ATTR(visibility("hidden"))
+void
+wrtmessage_1_0(const char *s1, const char *s2, const char *s3, const char *s4) {
+
+	wrtmessage(NULL, s1);
+	wrtmessage(NULL, s2);
+	wrtmessage(NULL, s3);
+	wrtmessage(NULL, s4);
+}
+
+void	(*__malloc_message_1_0)(const char *s1, const char *s2, const char *s3,
+    const char *s4) = wrtmessage_1_0;
+__sym_compat(_malloc_message, __malloc_message_1_0, FBSD_1.0);
+
 /*
  * Wrapper around malloc_message() that avoids the need for
  * je_malloc_message(...) throughout the code.
diff --git a/src/mutex.c b/src/mutex.c
index 30222b3e..b2c36283 100644
--- a/src/mutex.c
+++ b/src/mutex.c
@@ -41,6 +41,17 @@ pthread_create(pthread_t *__restrict thread,
 #ifdef JEMALLOC_MUTEX_INIT_CB
 JEMALLOC_EXPORT int	_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
     void *(calloc_cb)(size_t, size_t));
+
+#pragma weak _pthread_mutex_init_calloc_cb
+int
+_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
+    void *(calloc_cb)(size_t, size_t))
+{
+
+	return (((int (*)(pthread_mutex_t *, void *(*)(size_t, size_t)))
+	    __libc_interposing[INTERPOS__pthread_mutex_init_calloc_cb])(mutex,
+	    calloc_cb));
+}
 #endif
 
 void
@@ -131,6 +142,16 @@ mutex_addr_comp(const witness_t *witness1, void *mutex1,
 }
 
 bool
+malloc_mutex_first_thread(void) {
+
+#ifndef JEMALLOC_MUTEX_INIT_CB
+	return (malloc_mutex_first_thread());
+#else
+	return (false);
+#endif
+}
+
+bool
 malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
     witness_rank_t rank, malloc_mutex_lock_order_t lock_order) {
 	mutex_prof_data_init(&mutex->prof_data);