@@ -626,12 +626,16 @@ _PyMem_PymallocEnabled(void)
626
626
static int
627
627
_PyMem_MimallocEnabled (void )
628
628
{
629
+ #ifdef Py_GIL_DISABLED
630
+ return 1 ;
631
+ #else
629
632
if (_PyMem_DebugEnabled ()) {
630
633
return (_PyMem_Debug .obj .alloc .malloc == _PyObject_MiMalloc );
631
634
}
632
635
else {
633
636
return (_PyObject .malloc == _PyObject_MiMalloc );
634
637
}
638
+ #endif
635
639
}
636
640
#endif // WITH_MIMALLOC
637
641
@@ -1041,20 +1045,35 @@ static bool count_blocks(
1041
1045
* (size_t * )allocated_blocks += area -> used ;
1042
1046
return 1 ;
1043
1047
}
1048
+
1049
+ static Py_ssize_t
1050
+ get_mimalloc_allocated_blocks (PyInterpreterState * interp )
1051
+ {
1052
+ size_t allocated_blocks = 0 ;
1053
+ #ifdef Py_GIL_DISABLED
1054
+ for (PyThreadState * t = interp -> threads .head ; t != NULL ; t = t -> next ) {
1055
+ _PyThreadStateImpl * tstate = (_PyThreadStateImpl * )t ;
1056
+ for (int i = 0 ; i < _Py_MIMALLOC_HEAP_COUNT ; i ++ ) {
1057
+ mi_heap_t * heap = & tstate -> mimalloc .heaps [i ];
1058
+ mi_heap_visit_blocks (heap , false, & count_blocks , & allocated_blocks );
1059
+ }
1060
+ }
1061
+ // TODO(sgross): count blocks in abandoned segments.
1062
+ #else
1063
+ // TODO(sgross): this only counts the current thread's blocks.
1064
+ mi_heap_t * heap = mi_heap_get_default ();
1065
+ mi_heap_visit_blocks (heap , false, & count_blocks , & allocated_blocks );
1066
+ #endif
1067
+ return allocated_blocks ;
1068
+ }
1044
1069
#endif
1045
1070
1046
1071
Py_ssize_t
1047
1072
_PyInterpreterState_GetAllocatedBlocks (PyInterpreterState * interp )
1048
1073
{
1049
1074
#ifdef WITH_MIMALLOC
1050
- // TODO(sgross): this only counts the current thread's blocks.
1051
1075
if (_PyMem_MimallocEnabled ()) {
1052
- size_t allocated_blocks = 0 ;
1053
-
1054
- mi_heap_t * heap = mi_heap_get_default ();
1055
- mi_heap_visit_blocks (heap , false, & count_blocks , & allocated_blocks );
1056
-
1057
- return allocated_blocks ;
1076
+ return get_mimalloc_allocated_blocks (interp );
1058
1077
}
1059
1078
#endif
1060
1079
@@ -1105,7 +1124,7 @@ _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *interp)
1105
1124
1106
1125
static Py_ssize_t get_num_global_allocated_blocks (_PyRuntimeState * );
1107
1126
1108
- /* We preserve the number of blockss leaked during runtime finalization,
1127
+ /* We preserve the number of blocks leaked during runtime finalization,
1109
1128
so they can be reported if the runtime is initialized again. */
1110
1129
// XXX We don't lose any information by dropping this,
1111
1130
// so we should consider doing so.
@@ -1121,16 +1140,6 @@ _Py_FinalizeAllocatedBlocks(_PyRuntimeState *runtime)
1121
1140
static Py_ssize_t
1122
1141
get_num_global_allocated_blocks (_PyRuntimeState * runtime )
1123
1142
{
1124
- #ifdef WITH_MIMALLOC
1125
- if (_PyMem_MimallocEnabled ()) {
1126
- size_t allocated_blocks = 0 ;
1127
-
1128
- mi_heap_t * heap = mi_heap_get_default ();
1129
- mi_heap_visit_blocks (heap , false, & count_blocks , & allocated_blocks );
1130
-
1131
- return allocated_blocks ;
1132
- }
1133
- #endif
1134
1143
Py_ssize_t total = 0 ;
1135
1144
if (_PyRuntimeState_GetFinalizing (runtime ) != NULL ) {
1136
1145
PyInterpreterState * interp = _PyInterpreterState_Main ();
0 commit comments