Skip to content

Commit f3808bf

Browse files
committed
gh-101430: Update tracemalloc to handle presize properly.
1 parent ecfd2d3 commit f3808bf

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :mod:`tracemalloc` to handle presize of object properly. Patch by
2+
Dong-hee Na.

Modules/_tracemalloc.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pycore_fileutils.h" // _Py_write_noraise()
33
#include "pycore_gc.h" // PyGC_Head
44
#include "pycore_hashtable.h" // _Py_hashtable_t
5+
#include "pycore_object.h" // _PyType_PreHeaderSize
56
#include "pycore_pymem.h" // _Py_tracemalloc_config
67
#include "pycore_runtime.h" // _Py_ID()
78
#include "pycore_traceback.h"
@@ -1404,8 +1405,9 @@ _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
14041405
traceback_t *traceback;
14051406

14061407
type = Py_TYPE(obj);
1408+
const size_t presize = _PyType_PreHeaderSize(type);
14071409
if (PyType_IS_GC(type)) {
1408-
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
1410+
ptr = (void *)((char *)obj - presize);
14091411
}
14101412
else {
14111413
ptr = (void *)obj;
@@ -1725,8 +1727,9 @@ _PyTraceMalloc_NewReference(PyObject *op)
17251727

17261728
uintptr_t ptr;
17271729
PyTypeObject *type = Py_TYPE(op);
1730+
const size_t presize = _PyType_PreHeaderSize(type);
17281731
if (PyType_IS_GC(type)) {
1729-
ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
1732+
ptr = (uintptr_t)((char *)op - presize);
17301733
}
17311734
else {
17321735
ptr = (uintptr_t)op;

0 commit comments

Comments
 (0)