From 79851a502af5101719c8510eba36ae4842db2632 Mon Sep 17 00:00:00 2001 From: Eddie Elizondo Date: Sat, 3 Jun 2023 22:32:38 -0400 Subject: [PATCH] Remove anonymous union from PyObject --- Include/Python.h | 4 ++-- Include/object.h | 15 +++++---------- Objects/object.c | 4 ++-- Objects/setobject.c | 2 +- Objects/sliceobject.c | 2 +- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Include/Python.h b/Include/Python.h index 45ba2ec12f2ad2..fbf110c21c2494 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -16,14 +16,13 @@ # define _SGI_MP_SOURCE #endif -// stdlib.h, stdio.h, errno.h and string.h headers are not used by Python +// stdlib.h, stdio.h, and errno.h headers are not used by Python // headers, but kept for backward compatibility. They are excluded from the // limited C API of Python 3.11. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 # include # include // FILE* # include // errno -# include // memcpy() #endif #ifndef MS_WINDOWS # include @@ -34,6 +33,7 @@ #include // assert() #include // wchar_t +#include // memcpy() #include "pyport.h" #include "pymacro.h" diff --git a/Include/object.h b/Include/object.h index c2fee85a2c38f6..e34523ead19ec7 100644 --- a/Include/object.h +++ b/Include/object.h @@ -131,14 +131,14 @@ check by comparing the reference count field to the immortality reference count. #define PyObject_HEAD_INIT(type) \ { \ _PyObject_EXTRA_INIT \ - { _Py_IMMORTAL_REFCNT }, \ + _Py_IMMORTAL_REFCNT, \ (type) \ }, #else #define PyObject_HEAD_INIT(type) \ { \ _PyObject_EXTRA_INIT \ - { 1 }, \ + 1, \ (type) \ }, #endif /* Py_BUILD_CORE */ @@ -165,12 +165,7 @@ check by comparing the reference count field to the immortality reference count. */ struct _object { _PyObject_HEAD_EXTRA - union { - Py_ssize_t ob_refcnt; -#if SIZEOF_VOID_P > 4 - PY_UINT32_T ob_refcnt_split[2]; -#endif - }; + Py_ssize_t ob_refcnt; PyTypeObject *ob_type; }; @@ -624,12 +619,12 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) // directly PyObject.ob_refcnt. #if SIZEOF_VOID_P > 4 // Portable saturated add, branching on the carry flag and set low bits - PY_UINT32_T cur_refcnt = op->ob_refcnt_split[PY_BIG_ENDIAN]; + PY_UINT32_T cur_refcnt = _Py_CAST(PY_UINT32_T, op->ob_refcnt); PY_UINT32_T new_refcnt = cur_refcnt + 1; if (new_refcnt == 0) { return; } - op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt; + memcpy(&op->ob_refcnt, &new_refcnt, sizeof(new_refcnt)); #else // Explicitly check immortality against the immortal value if (_Py_IsImmortal(op)) { diff --git a/Objects/object.c b/Objects/object.c index b20e87ef3fb23d..5c925d97faa545 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1876,7 +1876,7 @@ PyTypeObject _PyNone_Type = { PyObject _Py_NoneStruct = { _PyObject_EXTRA_INIT - { _Py_IMMORTAL_REFCNT }, + _Py_IMMORTAL_REFCNT, &_PyNone_Type }; @@ -1979,7 +1979,7 @@ PyTypeObject _PyNotImplemented_Type = { PyObject _Py_NotImplementedStruct = { _PyObject_EXTRA_INIT - { _Py_IMMORTAL_REFCNT }, + _Py_IMMORTAL_REFCNT, &_PyNotImplemented_Type }; diff --git a/Objects/setobject.c b/Objects/setobject.c index 58f0ae73c0c403..a8600934d27f8c 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -2544,6 +2544,6 @@ static PyTypeObject _PySetDummy_Type = { static PyObject _dummy_struct = { _PyObject_EXTRA_INIT - { _Py_IMMORTAL_REFCNT }, + _Py_IMMORTAL_REFCNT, &_PySetDummy_Type }; diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index e6776ac92b669c..0480b31540a887 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -99,7 +99,7 @@ PyTypeObject PyEllipsis_Type = { PyObject _Py_EllipsisObject = { _PyObject_EXTRA_INIT - { _Py_IMMORTAL_REFCNT }, + _Py_IMMORTAL_REFCNT, &PyEllipsis_Type };