Skip to content

Commit 8cb8146

Browse files
committed
Make the default option of zero realloc match the system allocator.
1 parent 66c8895 commit 8cb8146

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

configure.ac

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ dnl Define cpp macros in CPPFLAGS, rather than doing AC_DEFINE(macro), since the
638638
dnl definitions need to be seen before any headers are included, which is a pain
639639
dnl to make happen otherwise.
640640
default_retain="0"
641+
zero_realloc_default_free="0"
641642
maps_coalesce="1"
642643
DUMP_SYMS="${NM} -a"
643644
SYM_PREFIX=""
@@ -684,6 +685,7 @@ case "${host}" in
684685
if test "${LG_SIZEOF_PTR}" = "3"; then
685686
default_retain="1"
686687
fi
688+
zero_realloc_default_free="1"
687689
;;
688690
*-*-linux*)
689691
dnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.
@@ -698,6 +700,7 @@ case "${host}" in
698700
if test "${LG_SIZEOF_PTR}" = "3"; then
699701
default_retain="1"
700702
fi
703+
zero_realloc_default_free="1"
701704
;;
702705
*-*-kfreebsd*)
703706
dnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.
@@ -773,6 +776,7 @@ case "${host}" in
773776
if test "${LG_SIZEOF_PTR}" = "3"; then
774777
default_retain="1"
775778
fi
779+
zero_realloc_default_free="1"
776780
;;
777781
*-*-nto-qnx)
778782
abi="elf"
@@ -1395,6 +1399,11 @@ if test "x$default_retain" = "x1" ; then
13951399
AC_DEFINE([JEMALLOC_RETAIN], [ ], [ ])
13961400
fi
13971401

1402+
dnl Indicate whether realloc(ptr, 0) defaults to the "alloc" behavior.
1403+
if test "x$zero_realloc_default_free" = "x1" ; then
1404+
AC_DEFINE([JEMALLOC_ZERO_REALLOC_DEFAULT_FREE], [ ], [ ])
1405+
fi
1406+
13981407
dnl Enable allocation from DSS if supported by the OS.
13991408
have_dss="1"
14001409
dnl Check whether the BSD/SUSv1 sbrk() exists. If not, disable DSS support.

doc/jemalloc.xml.in

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,13 +1578,14 @@ malloc_conf = "xmalloc:true";]]></programlisting>
15781578
<literal>r-</literal>
15791579
</term>
15801580
<listitem><para> Determines the behavior of
1581-
<function>realloc()</function> when passed a value of zero for the new
1582-
size. <quote>alloc</quote> treats this as an allocation of size zero
1583-
(and returns a non-null result except in case of resource exhaustion).
1584-
<quote>free</quote> treats this as a deallocation of the pointer, and
1585-
returns <constant>NULL</constant> without setting
1586-
<varname>errno</varname>. <quote>abort</quote> aborts the process if
1587-
zero is passed. The default is <quote>alloc</quote>.</para>
1581+
<function>realloc()</function> when passed a value of zero for the new
1582+
size. <quote>alloc</quote> treats this as an allocation of size zero
1583+
(and returns a non-null result except in case of resource exhaustion).
1584+
<quote>free</quote> treats this as a deallocation of the pointer, and
1585+
returns <constant>NULL</constant> without setting
1586+
<varname>errno</varname>. <quote>abort</quote> aborts the process if
1587+
zero is passed. The default is <quote>free</quote> on Linux and
1588+
Windows, and <quote>alloc</quote> elsewhere.</para>
15881589

15891590
<para>There is considerable divergence of behaviors across
15901591
implementations in handling this case. Many have the behavior of

include/jemalloc/internal/jemalloc_internal_defs.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,7 @@
421421
/* Darwin VM_MAKE_TAG support */
422422
#undef JEMALLOC_HAVE_VM_MAKE_TAG
423423

424+
/* If defined, realloc(ptr, 0) defaults to "free" instead of "alloc". */
425+
#undef JEMALLOC_ZERO_REALLOC_DEFAULT_FREE
426+
424427
#endif /* JEMALLOC_INTERNAL_DEFS_H_ */

src/jemalloc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ bool opt_cache_oblivious =
112112
;
113113

114114
zero_realloc_action_t opt_zero_realloc_action =
115-
zero_realloc_action_alloc;
115+
#ifdef JEMALLOC_ZERO_REALLOC_DEFAULT_FREE
116+
zero_realloc_action_free
117+
#else
118+
zero_realloc_action_alloc
119+
#endif
120+
;
116121

117122
atomic_zu_t zero_realloc_count = ATOMIC_INIT(0);
118123

0 commit comments

Comments
 (0)