summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-02-03 16:55:05 (GMT)
committerGitHub <noreply@github.com>2020-02-03 16:55:04 (GMT)
commit49932fec62c616ec88da52642339d83ae719e924 (patch)
treeb489d2b97cf56c0b51a15f27ecf4064c5084d9d8 /Objects
parent24e5ad4689de9adc8e4a7d8c08fe400dcea668e6 (diff)
downloadcpython-49932fec62c616ec88da52642339d83ae719e924.zip
cpython-49932fec62c616ec88da52642339d83ae719e924.tar.gz
cpython-49932fec62c616ec88da52642339d83ae719e924.tar.bz2
bpo-39542: Simplify _Py_NewReference() (GH-18332)
* Remove _Py_INC_REFTOTAL and _Py_DEC_REFTOTAL macros: modify directly _Py_RefTotal. * _Py_ForgetReference() is no longer defined if the Py_TRACE_REFS macro is not defined. * Remove _Py_NewReference() implementation from object.c: unify the two implementations in object.h inline function. * Fix Py_TRACE_REFS build: _Py_INC_TPALLOCS() macro has been removed.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c6
-rw-r--r--Objects/dictobject.c26
-rw-r--r--Objects/object.c21
-rw-r--r--Objects/tupleobject.c9
-rw-r--r--Objects/unicodeobject.c6
5 files changed, 40 insertions, 28 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 00151b8..5334eca 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2948,8 +2948,12 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
return (*pv == NULL) ? -1 : 0;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
- _Py_DEC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
+#ifdef Py_TRACE_REFS
_Py_ForgetReference(v);
+#endif
*pv = (PyObject *)
PyObject_REALLOC(v, PyBytesObject_SIZE + newsize);
if (*pv == NULL) {
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 87f88ab..ae6b50f 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -311,7 +311,9 @@ static void free_keys_object(PyDictKeysObject *keys);
static inline void
dictkeys_incref(PyDictKeysObject *dk)
{
- _Py_INC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal++;
+#endif
dk->dk_refcnt++;
}
@@ -319,7 +321,9 @@ static inline void
dictkeys_decref(PyDictKeysObject *dk)
{
assert(dk->dk_refcnt > 0);
- _Py_DEC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
if (--dk->dk_refcnt == 0) {
free_keys_object(dk);
}
@@ -563,7 +567,9 @@ static PyDictKeysObject *new_keys_object(Py_ssize_t size)
return NULL;
}
}
- _Py_INC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal++;
+#endif
dk->dk_refcnt = 1;
dk->dk_size = size;
dk->dk_usable = usable;
@@ -687,10 +693,12 @@ clone_combined_dict(PyDictObject *orig)
}
/* Since we copied the keys table we now have an extra reference
- in the system. Manually call _Py_INC_REFTOTAL to signal that
+ in the system. Manually call increment _Py_RefTotal to signal that
we have it now; calling dictkeys_incref would be an error as
keys->dk_refcnt is already set to 1 (after memcpy). */
- _Py_INC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal++;
+#endif
return (PyObject *)new;
}
@@ -1249,13 +1257,15 @@ dictresize(PyDictObject *mp, Py_ssize_t minsize)
assert(oldkeys->dk_lookup != lookdict_split);
assert(oldkeys->dk_refcnt == 1);
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
if (oldkeys->dk_size == PyDict_MINSIZE &&
- numfreekeys < PyDict_MAXFREELIST) {
- _Py_DEC_REFTOTAL;
+ numfreekeys < PyDict_MAXFREELIST)
+ {
keys_free_list[numfreekeys++] = oldkeys;
}
else {
- _Py_DEC_REFTOTAL;
PyObject_FREE(oldkeys);
}
}
diff --git a/Objects/object.c b/Objects/object.c
index 2154d11..085605a 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -232,9 +232,11 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
_PyObject_ASSERT(self,
(!PyType_IS_GC(Py_TYPE(self))
|| _PyObject_GC_IS_TRACKED(self)));
- /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
- * we need to undo that. */
- _Py_DEC_REFTOTAL;
+ /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
+ _Py_RefTotal, so we need to undo that. */
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
return -1;
}
@@ -1804,19 +1806,6 @@ _PyTypes_Init(void)
#ifdef Py_TRACE_REFS
-
-void
-_Py_NewReference(PyObject *op)
-{
- if (_Py_tracemalloc_config.tracing) {
- _PyTraceMalloc_NewReference(op);
- }
- _Py_INC_REFTOTAL;
- op->ob_refcnt = 1;
- _Py_AddToAllObjects(op, 1);
- _Py_INC_TPALLOCS(op);
-}
-
void
_Py_ForgetReference(PyObject *op)
{
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 2708b9a..8c8c66f 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -902,10 +902,15 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
- _Py_DEC_REFTOTAL;
- if (_PyObject_GC_IS_TRACKED(v))
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
+ if (_PyObject_GC_IS_TRACKED(v)) {
_PyObject_GC_UNTRACK(v);
+ }
+#ifdef Py_TRACE_REFS
_Py_ForgetReference((PyObject *) v);
+#endif
/* DECREF items deleted by shrinkage */
for (i = newsize; i < oldsize; i++) {
Py_CLEAR(v->ob_item[i]);
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 8e1161e..5f10437 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1037,8 +1037,12 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
_PyUnicode_UTF8(unicode) = NULL;
_PyUnicode_UTF8_LENGTH(unicode) = 0;
}
- _Py_DEC_REFTOTAL;
+#ifdef Py_REF_DEBUG
+ _Py_RefTotal--;
+#endif
+#ifdef Py_TRACE_REFS
_Py_ForgetReference(unicode);
+#endif
new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size);
if (new_unicode == NULL) {