@@ -1023,6 +1023,16 @@ get_state(void)
1023
1023
#define narenas_highwater (state->mgmt.narenas_highwater)
1024
1024
#define raw_allocated_blocks (state->mgmt.raw_allocated_blocks)
1025
1025
1026
+ #ifdef WITH_MIMALLOC
1027
+ static bool count_blocks (
1028
+ const mi_heap_t * heap , const mi_heap_area_t * area ,
1029
+ void * block , size_t block_size , void * allocated_blocks )
1030
+ {
1031
+ * (size_t * )allocated_blocks += area -> used ;
1032
+ return 1 ;
1033
+ }
1034
+ #endif
1035
+
1026
1036
Py_ssize_t
1027
1037
_PyInterpreterState_GetAllocatedBlocks (PyInterpreterState * interp )
1028
1038
{
@@ -1034,6 +1044,28 @@ _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *interp)
1034
1044
"the interpreter doesn't have its own allocator" );
1035
1045
}
1036
1046
#endif
1047
+
1048
+ #ifdef WITH_MIMALLOC
1049
+ // TODO(sgross): this only counts the current thread's blocks.
1050
+ if (_PyMem_MimallocEnabled ()) {
1051
+ size_t allocated_blocks = 0 ;
1052
+
1053
+ PyThreadState * tstate = PyInterpreterState_ThreadHead (interp );
1054
+ while (tstate != NULL ) {
1055
+ mi_heap_tag_t tags [] = {
1056
+ mi_heap_tag_default ,
1057
+ mi_heap_tag_obj ,
1058
+ };
1059
+ for (size_t i = 0 ; i != sizeof (tags )/sizeof (* tags ); i ++ ) {
1060
+ mi_heap_t * heap = tstate -> heaps [i ];
1061
+ mi_heap_visit_blocks (heap , false, & count_blocks , & allocated_blocks );
1062
+ }
1063
+ tstate = PyThreadState_Next (tstate );
1064
+ }
1065
+ return allocated_blocks ;
1066
+ }
1067
+ #endif
1068
+
1037
1069
OMState * state = & interp -> obmalloc ;
1038
1070
1039
1071
Py_ssize_t n = raw_allocated_blocks ;
0 commit comments