Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f378513

Browse files
committed
mimalloc: enable as allocator option
1 parent 01b1a4a commit f378513

File tree

6 files changed

+256
-10
lines changed

6 files changed

+256
-10
lines changed

Include/cpython/pymem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ typedef enum {
2929
PYMEM_ALLOCATOR_PYMALLOC = 5,
3030
PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6,
3131
#endif
32+
#ifdef WITH_MIMALLOC
33+
PYMEM_ALLOCATOR_MIMALLOC = 7,
34+
PYMEM_ALLOCATOR_MIMALLOC_DEBUG = 8,
35+
#endif
3236
} PyMemAllocatorName;
3337

3438

Include/internal/pycore_pymem_init.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,31 @@ extern void * _PyMem_RawRealloc(void *, void *, size_t);
1818
extern void _PyMem_RawFree(void *, void *);
1919
#define PYRAW_ALLOC {NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
2020

21-
#ifdef WITH_PYMALLOC
21+
#ifdef WITH_MIMALLOC
22+
extern void *_PyMem_MiMalloc(void *ctx, size_t size);
23+
extern void * _PyMem_MiCalloc(void *ctx, size_t nelem, size_t elsize);
24+
extern void *_PyMem_MiRealloc(void *ctx, void *ptr, size_t size);
25+
extern void _PyMem_MiFree(void *ctx, void *ptr);
26+
extern void * _PyObject_MiMalloc(void *ctx, size_t nbytes);
27+
extern void* _PyObject_MiCalloc(void *ctx, size_t nelem, size_t elsize);
28+
extern void* _PyObject_MiRealloc(void *ctx, void *ptr, size_t nbytes);
29+
extern void _PyObject_MiFree(void *ctx, void *ptr);
30+
31+
# define PYMEM_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree}
32+
# define PYOBJ_ALLOC {NULL, _PyObject_MiMalloc, _PyObject_MiCalloc, _PyObject_MiRealloc, _PyObject_MiFree}
33+
34+
#elif defined(WITH_PYMALLOC)
2235
extern void* _PyObject_Malloc(void *, size_t);
2336
extern void* _PyObject_Calloc(void *, size_t, size_t);
2437
extern void _PyObject_Free(void *, void *);
2538
extern void* _PyObject_Realloc(void *, void *, size_t);
2639
# define PYOBJ_ALLOC {NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
40+
# define PYMEM_ALLOC PYOBJ_ALLOC
2741
#else
2842
# define PYOBJ_ALLOC PYRAW_ALLOC
43+
# define PYMEM_ALLOC PYOBJ_ALLOC
2944
#endif // WITH_PYMALLOC
3045

31-
#define PYMEM_ALLOC PYOBJ_ALLOC
3246

3347
extern void* _PyMem_DebugRawMalloc(void *, size_t);
3448
extern void* _PyMem_DebugRawCalloc(void *, size_t, size_t);

Lib/test/support/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,11 @@ def with_pymalloc():
20282028
return _testcapi.WITH_PYMALLOC
20292029

20302030

2031+
def with_mimalloc():
2032+
import _testcapi
2033+
return _testcapi.WITH_MIMALLOC
2034+
2035+
20312036
class _ALWAYS_EQ:
20322037
"""
20332038
Object that is equal to anything.

Lib/test/test_cmd_line.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ def test_xdev(self):
724724
code = "import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())"
725725
with support.SuppressCrashReport():
726726
out = self.run_xdev("-c", code, check_exitcode=False)
727-
if support.with_pymalloc():
727+
if support.with_mimalloc():
728+
alloc_name = "mimalloc_debug"
729+
elif support.with_pymalloc():
728730
alloc_name = "pymalloc_debug"
729731
else:
730732
alloc_name = "malloc_debug"
@@ -803,7 +805,11 @@ def check_pythonmalloc(self, env_var, name):
803805
def test_pythonmalloc(self):
804806
# Test the PYTHONMALLOC environment variable
805807
pymalloc = support.with_pymalloc()
806-
if pymalloc:
808+
mimalloc = support.with_mimalloc()
809+
if mimalloc:
810+
default_name = 'mimalloc_debug' if support.Py_DEBUG else 'mimalloc'
811+
default_name_debug = 'mimalloc_debug'
812+
elif pymalloc:
807813
default_name = 'pymalloc_debug' if support.Py_DEBUG else 'pymalloc'
808814
default_name_debug = 'pymalloc_debug'
809815
else:
@@ -821,6 +827,11 @@ def test_pythonmalloc(self):
821827
('pymalloc', 'pymalloc'),
822828
('pymalloc_debug', 'pymalloc_debug'),
823829
))
830+
if mimalloc:
831+
tests.extend((
832+
('mimalloc', 'mimalloc'),
833+
('mimalloc_debug', 'mimalloc_debug'),
834+
))
824835

825836
for env_var, name in tests:
826837
with self.subTest(env_var=env_var, name=name):

Modules/_testcapi/mem.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,5 +613,14 @@ _PyTestCapi_Init_Mem(PyObject *mod)
613613
return -1;
614614
}
615615

616+
#ifdef WITH_MIMALLOC
617+
v = Py_True;
618+
#else
619+
v = Py_False;
620+
#endif
621+
if (PyModule_AddObjectRef(mod, "WITH_MIMALLOC", v) < 0) {
622+
return -1;
623+
}
624+
616625
return 0;
617626
}

0 commit comments

Comments
 (0)