8000 gh-102304: Consolidate Direct Usage of _Py_RefTotal by ericsnowcurrently · Pull Request #102514 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102304: Consolidate Direct Usage of _Py_RefTotal #102514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _Py_IncRefTotal() and _Py_DecRefTotal().
  • Loading branch information
ericsnowcurrently committed Mar 7, 2023
commit 7bdb1841fe0c00a9573ff0a83cf95fd69dab296d
8 changes: 7 additions & 1 deletion Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
#define _Py_FatalRefcountError(message) \
_Py_FatalRefcountErrorFunc(__func__, (message))


extern Py_ssize_t _Py_RefTotal;
extern void _Py_AddRefTotal(Py_ssize_t);
extern void _Py_IncRefTotal(void);
extern void _Py_DecRefTotal(void);

// Increment reference count by n
static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
{
#ifdef Py_REF_DEBUG
_Py_RefTotal += n;
_Py_AddRefTotal(n);
#endif
op->ob_refcnt += n;
}
Expand Down
22 changes: 18 additions & 4 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,21 @@ you can count such references to the type object.)
*/

#ifdef Py_REF_DEBUG
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
# if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030A0000
extern Py_ssize_t _Py_RefTotal;
# define _Py_INC_REFTOTAL() _Py_RefTotal++
# define _Py_DEC_REFTOTAL() _Py_RefTotal--
# elif defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
extern void _Py_IncRefTotal(void);
extern void _Py_DecRefTotal(void);
# define _Py_INC_REFTOTAL() _Py_IncRefTotal()
# define _Py_DEC_REFTOTAL() _Py_DecRefTotal()
# elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 > 0x030C0000
extern void _Py_IncRefTotal_DO_NOT_USE_THIS(void);
extern void _Py_DecRefTotal_DO_NOT_USE_THIS(void);
# define _Py_INC_REFTOTAL() _Py_IncRefTotal_DO_NOT_USE_THIS()
# define _Py_DEC_REFTOTAL() _Py_DecRefTotal_DO_NOT_USE_THIS()
# endif
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
PyObject *op);
#endif /* Py_REF_DEBUG */
Expand Down Expand Up @@ -519,8 +533,8 @@ static inline void Py_INCREF(PyObject *op)
// Non-limited C API and limited C API for Python 3.9 and older access
// directly PyObject.ob_refcnt.
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
#endif
_Py_INC_REFTOTAL();
#endif // Py_REF_DEBUG
op->ob_refcnt++;
#endif
}
Expand All @@ -539,7 +553,7 @@ static inline void Py_DECREF(PyObject *op) {
static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
{
_Py_DECREF_STAT_INC();
_Py_RefTotal--;
_Py_DEC_REFTOTAL();
if (--op->ob_refcnt != 0) {
if (op->ob_refcnt < 0) {
_Py_NegativeRefcount(filename, lineno, op);
Expand Down
2 changes: 1 addition & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3067,7 +3067,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
PyObject_Realloc(v, PyBytesObject_SIZE + newsize);
if (*pv == NULL) {
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
_Py_DecRefTotal();
#endif
PyObject_Free(v);
PyErr_NoMemory();
Expand Down
10 changes: 5 additions & 5 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ static inline void
dictkeys_incref(PyDictKeysObject *dk)
{
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
_Py_IncRefTotal();
#endif
dk->dk_refcnt++;
}
Expand All @@ -313,7 +313,7 @@ dictkeys_decref(PyDictKeysObject *dk)
{
assert(dk->dk_refcnt > 0);
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
_Py_DecRefTotal();
#endif
if (--dk->dk_refcnt == 0) {
free_keys_object(dk);
Expand Down Expand Up @@ -633,7 +633,7 @@ new_keys_object(uint8_t log2_size, bool unicode)
}
}
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
_Py_IncRefTotal();
#endif
dk->dk_refcnt = 1;
dk->dk_log2_size = log2_size;
Expand Down Expand Up @@ -821,7 +821,7 @@ clone_combined_dict_keys(PyDictObject *orig)
we have it now; calling dictkeys_incref would be an error as
keys->dk_refcnt is already set to 1 (after memcpy). */
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
_Py_IncRefTotal();
#endif
return keys;
}
Expand Down Expand Up @@ -1520,7 +1520,7 @@ dictresize(PyDictObject *mp, uint8_t log2_newsize, int unicode)
// We can not use free_keys_object here because key's reference
// are moved already.
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
_Py_DecRefTotal();
#endif
if (oldkeys == Py_EMPTY_KEYS) {
oldkeys->dk_refcnt--;
Expand Down
55 changes: 54 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,29 @@ _PyObject_CheckConsistency(PyObject *op, int check_content)


#ifdef Py_REF_DEBUG
/* The symbol is only exposed in the API for the sake of extensions
built against the pre-3.12 stable ABI. */
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
Py_ssize_t _Py_RefTotal;

static inline void
reftotal_increment(void)
{
_Py_RefTotal++;
}

static inline void
reftotal_decrement(void)
{
_Py_RefTotal--;
}

void
_Py_AddRefTotal(Py_ssize_t n)
{
_Py_RefTotal += n;
}

Py_ssize_t
_Py_GetRefTotal(void)
{
Expand Down Expand Up @@ -121,6 +142,32 @@ _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
filename, lineno, __func__);
}

/* This is exposed strictly for use in Py_INCREF(). */
PyAPI_FUNC(void)
_Py_IncRefTotal_DO_NOT_USE_THIS(void)
{
reftotal_increment();
}

/* This is exposed strictly for use in Py_DECREF(). */
PyAPI_FUNC(void)
_Py_DecRefTotal_DO_NOT_USE_THIS(void)
{
reftotal_decrement();
}

void
_Py_IncRefTotal(void)
{
reftotal_increment();
}

void
_Py_DecRefTotal(void)
{
reftotal_decrement();
}

#endif /* Py_REF_DEBUG */

void
Expand All @@ -138,12 +185,18 @@ Py_DecRef(PyObject *o)
void
_Py_IncRef(PyObject *o)
{
#ifdef Py_REF_DEBUG
reftotal_increment();
#endif
Py_INCREF(o);
}

void
_Py_DecRef(PyObject *o)
{
#ifdef Py_REF_DEBUG
reftotal_decrement();
#endif
Py_DECREF(o);
}

Expand Down Expand Up @@ -2025,7 +2078,7 @@ void
_Py_NewReference(PyObject *op)
{
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
reftotal_increment();
#endif
new_reference(op);
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ _PyStructSequence_FiniType(PyTypeObject *type)
// Don't use Py_DECREF(): static type must not be deallocated
Py_SET_REFCNT(type, 0);
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
_Py_DecRefTotal();
#endif

// Make sure that _PyStructSequence_InitType() will initialize
Expand Down
2 changes: 1 addition & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
if (sv == NULL) {
*pv = NULL;
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
_Py_DecRefTotal();
#endif
PyObject_GC_Del(v);
return -1;
Expand Down
0