diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 9826ad2935beaa..5d78a679ddc888 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -2,6 +2,7 @@ #include "pycore_fileutils.h" // _Py_write_noraise() #include "pycore_gc.h" // PyGC_Head #include "pycore_hashtable.h" // _Py_hashtable_t +#include "pycore_object.h" // _PyType_PreHeaderSize #include "pycore_pymem.h" // _Py_tracemalloc_config #include "pycore_runtime.h" // _Py_ID() #include "pycore_traceback.h" @@ -1399,19 +1400,13 @@ static PyObject * _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj) /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/ { - PyTypeObject *type; - void *ptr; + uintptr_t ptr; traceback_t *traceback; - type = Py_TYPE(obj); - if (PyType_IS_GC(type)) { - ptr = (void *)((char *)obj - sizeof(PyGC_Head)); - } - else { - ptr = (void *)obj; - } + ptr = (uintptr_t)obj; + ptr -= _PyType_PreHeaderSize(Py_TYPE(obj)); - traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr); + traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, ptr); if (traceback == NULL) Py_RETURN_NONE; diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 6630faa6f4471d..29e55e42059ae5 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -2348,17 +2348,18 @@ _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) PyVarObject * _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) { + size_t presize = _PyType_PreHeaderSize(Py_TYPE(op)); const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); - if (basicsize > (size_t)PY_SSIZE_T_MAX - sizeof(PyGC_Head)) { + if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { return (PyVarObject *)PyErr_NoMemory(); } - PyGC_Head *g = AS_GC(op); - g = (PyGC_Head *)PyObject_Realloc(g, sizeof(PyGC_Head) + basicsize); - if (g == NULL) + char *mem = (char *)op - presize; + mem = PyObject_Realloc(mem, presize + basicsize); + if (mem == NULL) return (PyVarObject *)PyErr_NoMemory(); - op = (PyVarObject *) FROM_GC(g); + op = (PyVarObject *) (mem + presize); Py_SET_SIZE(op, nitems); return op; } diff --git a/Objects/object.c b/Objects/object.c index fae508cae3d693..f534a86257ace5 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2342,15 +2342,9 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, /* Display the traceback where the object has been allocated. Do it before dumping repr(obj), since repr() is more likely to crash than dumping the traceback. */ - void *ptr; - PyTypeObject *type = Py_TYPE(obj); - if (_PyType_IS_GC(type)) { - ptr = (void *)((char *)obj - sizeof(PyGC_Head)); - } - else { - ptr = (void *)obj; - } - _PyMem_DumpTraceback(fileno(stderr), ptr); + char *mem = (char *)obj; + mem -= _PyType_PreHeaderSize(Py_TYPE(obj)); + _PyMem_DumpTraceback(fileno(stderr), mem); /* This might succeed or fail, but we're about to abort, so at least try to provide any extra info we can: */