cpython/Include/internal/pycore_mimalloc.h
Sam Gross c012c8ab7b
gh-115103: Delay reuse of mimalloc pages that store PyObjects (#115435)
This implements the delayed reuse of mimalloc pages that contain Python
objects in the free-threaded build.

Allocations of the same size class are grouped in data structures called
pages. These are different from operating system pages. For thread-safety, we
want to ensure that memory used to store PyObjects remains valid as long as
there may be concurrent lock-free readers; we want to delay using it for
other size classes, in other heaps, or returning it to the operating system.

When a mimalloc page becomes empty, instead of immediately freeing it, we tag
it with a QSBR goal and insert it into a per-thread state linked list of
pages to be freed. When mimalloc needs a fresh page, we process the queue and
free any still empty pages that are now deemed safe to be freed. Pages
waiting to be freed are still available for allocations of the same size
class and allocating from a page prevent it from being freed. There is
additional logic to handle abandoned pages when threads exit.
2024-03-06 09:42:11 -05:00

55 lines
1.4 KiB
C

#ifndef Py_INTERNAL_MIMALLOC_H
#define Py_INTERNAL_MIMALLOC_H
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
#if defined(MIMALLOC_H) || defined(MIMALLOC_TYPES_H)
# error "pycore_mimalloc.h must be included before mimalloc.h"
#endif
typedef enum {
_Py_MIMALLOC_HEAP_MEM = 0, // PyMem_Malloc() and friends
_Py_MIMALLOC_HEAP_OBJECT = 1, // non-GC objects
_Py_MIMALLOC_HEAP_GC = 2, // GC objects without pre-header
_Py_MIMALLOC_HEAP_GC_PRE = 3, // GC objects with pre-header
_Py_MIMALLOC_HEAP_COUNT
} _Py_mimalloc_heap_id;
#include "pycore_pymem.h"
#ifdef WITH_MIMALLOC
# ifdef Py_GIL_DISABLED
# define MI_PRIM_THREAD_ID _Py_ThreadId
# endif
# define MI_DEBUG_UNINIT PYMEM_CLEANBYTE
# define MI_DEBUG_FREED PYMEM_DEADBYTE
# define MI_DEBUG_PADDING PYMEM_FORBIDDENBYTE
#ifdef Py_DEBUG
# define MI_DEBUG 2
#else
# define MI_DEBUG 0
#endif
#include "mimalloc.h"
#include "mimalloc/types.h"
#include "mimalloc/internal.h"
#endif
#ifdef Py_GIL_DISABLED
struct _mimalloc_interp_state {
// When exiting, threads place any segments with live blocks in this
// shared pool for other threads to claim and reuse.
mi_abandoned_pool_t abandoned_pool;
};
struct _mimalloc_thread_state {
mi_heap_t *current_object_heap;
mi_heap_t heaps[_Py_MIMALLOC_HEAP_COUNT];
mi_tld_t tld;
struct llist_node page_list;
};
#endif
#endif // Py_INTERNAL_MIMALLOC_H