mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-112532: Use separate mimalloc heaps for GC objects (gh-113263)
* gh-112532: Use separate mimalloc heaps for GC objects In `--disable-gil` builds, we now use four separate heaps in anticipation of using mimalloc to find GC objects when the GIL is disabled. To support this, we also make a few changes to mimalloc: * `mi_heap_t` and `mi_tld_t` initialization is split from allocation. This allows us to have a `mi_tld_t` per-`PyThreadState`, which is important to keep interpreter isolation, since the same OS thread may run in multiple interpreters (using different PyThreadStates.) * Heap abandoning (mi_heap_collect_ex) can now be called from a different thread than the one that created the heap. This is necessary because we may clear and delete the containing PyThreadStates from a different thread during finalization and after fork(). * Use enum instead of defines and guard mimalloc includes. * The enum typedef will be convenient for future PRs that use the type. * Guarding the mimalloc includes allows us to unconditionally include pycore_mimalloc.h from other header files that rely on things like `struct _mimalloc_thread_state`. * Only define _mimalloc_thread_state in Py_GIL_DISABLED builds
This commit is contained in:
parent
8f5b998706
commit
acf3bcc886
9 changed files with 163 additions and 25 deletions
|
@ -88,19 +88,37 @@ _PyMem_RawFree(void *Py_UNUSED(ctx), void *ptr)
|
|||
void *
|
||||
_PyMem_MiMalloc(void *ctx, size_t size)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = &tstate->mimalloc.heaps[_Py_MIMALLOC_HEAP_MEM];
|
||||
return mi_heap_malloc(heap, size);
|
||||
#else
|
||||
return mi_malloc(size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
_PyMem_MiCalloc(void *ctx, size_t nelem, size_t elsize)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = &tstate->mimalloc.heaps[_Py_MIMALLOC_HEAP_MEM];
|
||||
return mi_heap_calloc(heap, nelem, elsize);
|
||||
#else
|
||||
return mi_calloc(nelem, elsize);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
_PyMem_MiRealloc(void *ctx, void *ptr, size_t size)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = &tstate->mimalloc.heaps[_Py_MIMALLOC_HEAP_MEM];
|
||||
return mi_heap_realloc(heap, ptr, size);
|
||||
#else
|
||||
return mi_realloc(ptr, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -112,20 +130,38 @@ _PyMem_MiFree(void *ctx, void *ptr)
|
|||
void *
|
||||
_PyObject_MiMalloc(void *ctx, size_t nbytes)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = tstate->mimalloc.current_object_heap;
|
||||
return mi_heap_malloc(heap, nbytes);
|
||||
#else
|
||||
return mi_malloc(nbytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
_PyObject_MiCalloc(void *ctx, size_t nelem, size_t elsize)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = tstate->mimalloc.current_object_heap;
|
||||
return mi_heap_calloc(heap, nelem, elsize);
|
||||
#else
|
||||
return mi_calloc(nelem, elsize);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void *
|
||||
_PyObject_MiRealloc(void *ctx, void *ptr, size_t nbytes)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
|
||||
mi_heap_t *heap = tstate->mimalloc.current_object_heap;
|
||||
return mi_heap_realloc(heap, ptr, nbytes);
|
||||
#else
|
||||
return mi_realloc(ptr, nbytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue