]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-148189: Fix miscalculation of type-specific free list memory use (#148190)
authorMazin Sharaf <mazinsharaf@icloud.com>
Sun, 3 May 2026 03:03:13 +0000 (13:03 +1000)
committerGitHub <noreply@github.com>
Sun, 3 May 2026 03:03:13 +0000 (22:03 -0500)
* Fix calculation of PyListObject size in allocator

* Fix size calculation in _PyDict_DebugMallocStats

* Fix memory size calculation in tupleobject.c

Adjusted memory calculation for PyTupleObject freelist entries.

* Revert in tupleobject.c

Removed unnecessary comment regarding memory calculation and the memory calculation itself.

* ðŸ“œðŸ¤– Added by blurb_it.

* Update tupleobject.c

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Misc/NEWS.d/next/Core_and_Builtins/2026-04-08-02-49-07.gh-issue-148189.0KpXID.rst [new file with mode: 0644]
Objects/dictobject.c
Objects/listobject.c
Objects/tupleobject.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-08-02-49-07.gh-issue-148189.0KpXID.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-08-02-49-07.gh-issue-148189.0KpXID.rst
new file mode 100644 (file)
index 0000000..d90e30b
--- /dev/null
@@ -0,0 +1 @@
+Repaired undercount of bytes in type-specific free lists reported by sys._debugmallocstats(). For types that participate in cyclic garbage collection, it was missing two pointers used by GC.
index 09db93b2d318207120956a79b0f4e8b6eaaa2061..5be5baf8fc4cfeece53f38061a888dc0e0599f22 100644 (file)
@@ -443,7 +443,7 @@ _PyDict_DebugMallocStats(FILE *out)
 {
     _PyDebugAllocatorStats(out, "free PyDictObject",
                            _Py_FREELIST_SIZE(dicts),
-                           sizeof(PyDictObject));
+                           _PyType_PreHeaderSize(&PyDict_Type) + sizeof(PyDictObject));
     _PyDebugAllocatorStats(out, "free PyDictKeysObject",
                            _Py_FREELIST_SIZE(dictkeys),
                            sizeof(PyDictKeysObject));
index 685b30bb9eebd6faff05a7d2a3b57fc739673d11..10e25bbdcdcb6c538eca09f8f10e18db67577b2c 100644 (file)
@@ -234,7 +234,7 @@ _PyList_DebugMallocStats(FILE *out)
     _PyDebugAllocatorStats(out,
                            "free PyListObject",
                             _Py_FREELIST_SIZE(lists),
-                           sizeof(PyListObject));
+                           _PyType_PreHeaderSize(&PyList_Type) + sizeof(PyListObject));
 }
 
 PyObject *
index 7757a102677e2cb61185f714c9067c7cfee02616..753c270f525976f8d32c74ec9325dc49fddf677d 100644 (file)
@@ -1286,6 +1286,6 @@ _PyTuple_DebugMallocStats(FILE *out)
         PyOS_snprintf(buf, sizeof(buf),
                       "free %d-sized PyTupleObject", len);
         _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]),
-                               _PyObject_VAR_SIZE(&PyTuple_Type, len));
+                               _PyType_PreHeaderSize(&PyTuple_Type) + _PyObject_VAR_SIZE(&PyTuple_Type, len));
     }
 }