gh-90815: Add mimalloc memory allocator (#109914)

* Add mimalloc v2.12

Modified src/alloc.c to remove include of alloc-override.c and not
compile new handler.

Did not include the following files:

 - include/mimalloc-new-delete.h
 - include/mimalloc-override.h
 - src/alloc-override-osx.c
 - src/alloc-override.c
 - src/static.c
 - src/region.c

mimalloc is thread safe and shares a single heap across all runtimes,
therefore finalization and getting global allocated blocks across all
runtimes is different.

* mimalloc: minimal changes for use in Python:

 - remove debug spam for freeing large allocations
 - use same bytes (0xDD) for freed allocations in CPython and mimalloc
   This is important for the test_capi debug memory tests

* Don't export mimalloc symbol in libpython.
* Enable mimalloc as Python allocator option.
* Add mimalloc MIT license.
* Log mimalloc in Lib/test/pythoninfo.py.
* Document new mimalloc support.
* Use macro defs for exports as done in:
  https://github.com/python/cpython/pull/31164/

Co-authored-by: Sam Gross <colesbury@gmail.com>
Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Dino Viehland 2023-10-30 08:43:11 -07:00 committed by GitHub
parent 4ebf2fae96
commit 05f2f0ac92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 16792 additions and 161 deletions

View file

@ -4502,6 +4502,71 @@ then
fi
AC_MSG_RESULT([$with_doc_strings])
# Check for stdatomic.h, required for mimalloc.
AC_CACHE_CHECK([for stdatomic.h], [ac_cv_header_stdatomic_h], [
AC_LINK_IFELSE(
[
AC_LANG_SOURCE([[
#include <stdatomic.h>
atomic_int int_var;
atomic_uintptr_t uintptr_var;
int main() {
atomic_store_explicit(&int_var, 5, memory_order_relaxed);
atomic_store_explicit(&uintptr_var, 0, memory_order_relaxed);
int loaded_value = atomic_load_explicit(&int_var, memory_order_seq_cst);
return 0;
}
]])
],[ac_cv_header_stdatomic_h=yes],[ac_cv_header_stdatomic_h=no])
])
AS_VAR_IF([ac_cv_header_stdatomic_h], [yes], [
AC_DEFINE(HAVE_STD_ATOMIC, 1,
[Has stdatomic.h with atomic_int and atomic_uintptr_t])
])
# Check for GCC >= 4.7 and clang __atomic builtin functions
AC_CACHE_CHECK([for builtin __atomic_load_n and __atomic_store_n functions], [ac_cv_builtin_atomic], [
AC_LINK_IFELSE(
[
AC_LANG_SOURCE([[
int val;
int main() {
__atomic_store_n(&val, 1, __ATOMIC_SEQ_CST);
(void)__atomic_load_n(&val, __ATOMIC_SEQ_CST);
return 0;
}
]])
],[ac_cv_builtin_atomic=yes],[ac_cv_builtin_atomic=no])
])
AS_VAR_IF([ac_cv_builtin_atomic], [yes], [
AC_DEFINE(HAVE_BUILTIN_ATOMIC, 1, [Has builtin __atomic_load_n() and __atomic_store_n() functions])
])
# --with-mimalloc
AC_MSG_CHECKING([for --with-mimalloc])
AC_ARG_WITH([mimalloc],
[AS_HELP_STRING([--with-mimalloc],
[build with mimalloc memory allocator (default is yes if C11 stdatomic.h is available.)])],
[],
[with_mimalloc="$ac_cv_header_stdatomic_h"]
)
if test "$with_mimalloc" != no; then
if test "$ac_cv_header_stdatomic_h" != yes; then
# mimalloc-atomic.h wants C11 stdatomic.h on POSIX
AC_MSG_ERROR([mimalloc requires stdatomic.h, use --without-mimalloc to disable mimalloc.])
fi
with_mimalloc=yes
AC_DEFINE([WITH_MIMALLOC], [1], [Define if you want to compile in mimalloc memory allocator.])
AC_SUBST([MIMALLOC_HEADERS], ['$(MIMALLOC_HEADERS)'])
fi
AC_MSG_RESULT([$with_mimalloc])
AC_SUBST([WITH_MIMALLOC])
AC_SUBST([MIMALLOC_HEADERS])
# Check for Python-specific malloc support
AC_MSG_CHECKING([for --with-pymalloc])
AC_ARG_WITH(
@ -6530,6 +6595,8 @@ SRCDIRS="\
Modules/cjkcodecs \
Modules/expat \
Objects \
Objects/mimalloc \
Objects/mimalloc/prim \
Parser \
Parser/tokenizer \
Parser/lexer \
@ -6627,49 +6694,6 @@ if test "$ac_cv_gcc_asm_for_x87" = yes; then
esac
fi
# Check for stdatomic.h
AC_CACHE_CHECK([for stdatomic.h], [ac_cv_header_stdatomic_h], [
AC_LINK_IFELSE(
[
AC_LANG_SOURCE([[
#include <stdatomic.h>
atomic_int int_var;
atomic_uintptr_t uintptr_var;
int main(void) {
atomic_store_explicit(&int_var, 5, memory_order_relaxed);
atomic_store_explicit(&uintptr_var, 0, memory_order_relaxed);
int loaded_value = atomic_load_explicit(&int_var, memory_order_seq_cst);
return 0;
}
]])
],[ac_cv_header_stdatomic_h=yes],[ac_cv_header_stdatomic_h=no])
])
AS_VAR_IF([ac_cv_header_stdatomic_h], [yes], [
AC_DEFINE([HAVE_STD_ATOMIC], [1],
[Has stdatomic.h with atomic_int and atomic_uintptr_t])
])
# Check for GCC >= 4.7 and clang __atomic builtin functions
AC_CACHE_CHECK([for builtin __atomic_load_n and __atomic_store_n functions], [ac_cv_builtin_atomic], [
AC_LINK_IFELSE(
[
AC_LANG_SOURCE([[
int val;
int main(void) {
__atomic_store_n(&val, 1, __ATOMIC_SEQ_CST);
(void)__atomic_load_n(&val, __ATOMIC_SEQ_CST);
return 0;
}
]])
],[ac_cv_builtin_atomic=yes],[ac_cv_builtin_atomic=no])
])
AS_VAR_IF([ac_cv_builtin_atomic], [yes], [
AC_DEFINE([HAVE_BUILTIN_ATOMIC], [1],
[Has builtin __atomic_load_n() and __atomic_store_n() functions])
])
# ensurepip option
AC_MSG_CHECKING([for ensurepip])
AC_ARG_WITH([ensurepip],
@ -7401,3 +7425,10 @@ AS_VAR_IF([PY_SUPPORT_TIER], [0], [AC_MSG_WARN([
Platform "$host" with compiler "$ac_cv_cc_name" is not supported by the
CPython core team, see https://peps.python.org/pep-0011/ for more information.
])])
if test "$ac_cv_header_stdatomic_h" != "yes"; then
AC_MSG_NOTICE(m4_normalize([
Your compiler or platform does have a working C11 stdatomic.h. A future
version of Python may require stdatomic.h.
]))
fi