Skip to content

Commit 8af10a5

Browse files
Include the legacy total in _Py_GetGlobalRefTotal().
1 parent 1da6857 commit 8af10a5

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Include/cpython/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
1414
/* These are useful as debugging aids when chasing down refleaks. */
1515
PyAPI_FUNC(Py_ssize_t) _Py_GetGlobalRefTotal(void);
1616
# define _Py_GetRefTotal() _Py_GetGlobalRefTotal()
17+
PyAPI_FUNC(Py_ssize_t) _Py_GetLegacyRefTotal(void);
1718
#endif
1819

1920

Include/internal/pycore_object_state.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010

1111
struct _py_object_runtime_state {
1212
#ifdef Py_REF_DEBUG
13+
Py_ssize_t last_legacy_reftotal;
1314
Py_ssize_t reftotal;
1415
#else
1516
int _not_used;

Objects/object.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ _PyObject_CheckConsistency(PyObject *op, int check_content)
5656
#ifdef Py_REF_DEBUG
5757
/* We keep the legacy symbol around for backward compatibility. */
5858
Py_ssize_t _Py_RefTotal;
59+
60+
static inline Py_ssize_t
61+
get_legacy_reftotal(void)
62+
{
63+
return _Py_RefTotal;
64+
}
5965
#endif
6066

6167
#ifdef Py_REF_DEBUG
@@ -83,7 +89,10 @@ reftotal_add(Py_ssize_t n)
8389
static inline Py_ssize_t
8490
get_global_reftotal(void)
8591
{
86-
return REFTOTAL;
92+
Py_ssize_t last = _PyRuntime.object_state.last_legacy_reftotal;
93+
Py_ssize_t legacy = get_legacy_reftotal();
94+
_PyRuntime.object_state.last_legacy_reftotal = legacy;
95+
return REFTOTAL + legacy - last;
8796
}
8897

8998
#undef REFTOTAL
@@ -93,6 +102,7 @@ _PyDebug_PrintTotalRefs(void) {
93102
fprintf(stderr,
94103
"[%zd refs, %zd blocks]\n",
95104
get_global_reftotal(), _Py_GetAllocatedBlocks());
105+
/* It may be helpful to also print the "legacy" reftotal separately. */
96106
}
97107
#endif /* Py_REF_DEBUG */
98108

@@ -179,12 +189,19 @@ _Py_AddRefTotal(Py_ssize_t n)
179189
reftotal_add(n);
180190
}
181191

192+
/* This includes the legacy total. */
182193
Py_ssize_t
183194
_Py_GetGlobalRefTotal(void)
184195
{
185196
return get_global_reftotal();
186197
}
187198

199+
Py_ssize_t
200+
_Py_GetLegacyRefTotal(void)
201+
{
202+
return get_legacy_reftotal();
203+
}
204+
188205
#endif /* Py_REF_DEBUG */
189206

190207
void

0 commit comments

Comments
 (0)