mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
bpo-35059: Enhance _PyObject_GC_TRACK() macros (GH-20931)
* Rename _PyObject_GC_TRACK_impl() to _PyObject_GC_TRACK() * Rename _PyObject_GC_UNTRACK_impl() to _PyObject_GC_UNTRACK() * Omit filename and lineno parameters if NDEBUG is defined.
This commit is contained in:
parent
2c4928d37e
commit
07923f32b1
1 changed files with 34 additions and 14 deletions
|
@ -53,6 +53,8 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
|
||||||
|
|
||||||
|
|
||||||
/* Tell the GC to track this object.
|
/* Tell the GC to track this object.
|
||||||
|
*
|
||||||
|
* The object must not be tracked by the GC.
|
||||||
*
|
*
|
||||||
* NB: While the object is tracked by the collector, it must be safe to call the
|
* NB: While the object is tracked by the collector, it must be safe to call the
|
||||||
* ob_traverse method.
|
* ob_traverse method.
|
||||||
|
@ -61,20 +63,24 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
|
||||||
* because it's not object header. So we don't use _PyGCHead_PREV() and
|
* because it's not object header. So we don't use _PyGCHead_PREV() and
|
||||||
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
|
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
|
||||||
*
|
*
|
||||||
* The PyObject_GC_Track() function is the public version of this macro.
|
* See also the public PyObject_GC_Track() function.
|
||||||
*/
|
*/
|
||||||
static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
|
static inline void _PyObject_GC_TRACK(
|
||||||
|
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
|
||||||
|
#ifndef NDEBUG
|
||||||
|
const char *filename, int lineno,
|
||||||
|
#endif
|
||||||
PyObject *op)
|
PyObject *op)
|
||||||
{
|
{
|
||||||
_PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op),
|
_PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op),
|
||||||
"object already tracked by the garbage collector",
|
"object already tracked by the garbage collector",
|
||||||
filename, lineno, "_PyObject_GC_TRACK");
|
filename, lineno, __func__);
|
||||||
|
|
||||||
PyGC_Head *gc = _Py_AS_GC(op);
|
PyGC_Head *gc = _Py_AS_GC(op);
|
||||||
_PyObject_ASSERT_FROM(op,
|
_PyObject_ASSERT_FROM(op,
|
||||||
(gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0,
|
(gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0,
|
||||||
"object is in generation which is garbage collected",
|
"object is in generation which is garbage collected",
|
||||||
filename, lineno, "_PyObject_GC_TRACK");
|
filename, lineno, __func__);
|
||||||
|
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
PyGC_Head *generation0 = tstate->interp->gc.generation0;
|
PyGC_Head *generation0 = tstate->interp->gc.generation0;
|
||||||
|
@ -85,9 +91,6 @@ static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
|
||||||
generation0->_gc_prev = (uintptr_t)gc;
|
generation0->_gc_prev = (uintptr_t)gc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define _PyObject_GC_TRACK(op) \
|
|
||||||
_PyObject_GC_TRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
|
|
||||||
|
|
||||||
/* Tell the GC to stop tracking this object.
|
/* Tell the GC to stop tracking this object.
|
||||||
*
|
*
|
||||||
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
|
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
|
||||||
|
@ -95,14 +98,19 @@ static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
|
||||||
*
|
*
|
||||||
* The object must be tracked by the GC.
|
* The object must be tracked by the GC.
|
||||||
*
|
*
|
||||||
* The PyObject_GC_UnTrack() function is the public version of this macro.
|
* See also the public PyObject_GC_UnTrack() which accept an object which is
|
||||||
|
* not tracked.
|
||||||
*/
|
*/
|
||||||
static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
|
static inline void _PyObject_GC_UNTRACK(
|
||||||
|
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
|
||||||
|
#ifndef NDEBUG
|
||||||
|
const char *filename, int lineno,
|
||||||
|
#endif
|
||||||
PyObject *op)
|
PyObject *op)
|
||||||
{
|
{
|
||||||
_PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op),
|
_PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op),
|
||||||
"object not tracked by the garbage collector",
|
"object not tracked by the garbage collector",
|
||||||
filename, lineno, "_PyObject_GC_UNTRACK");
|
filename, lineno, __func__);
|
||||||
|
|
||||||
PyGC_Head *gc = _Py_AS_GC(op);
|
PyGC_Head *gc = _Py_AS_GC(op);
|
||||||
PyGC_Head *prev = _PyGCHead_PREV(gc);
|
PyGC_Head *prev = _PyGCHead_PREV(gc);
|
||||||
|
@ -113,8 +121,20 @@ static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
|
||||||
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
|
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define _PyObject_GC_UNTRACK(op) \
|
// Macros to accept any type for the parameter, and to automatically pass
|
||||||
_PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
|
// the filename and the filename (if NDEBUG is not defined) where the macro
|
||||||
|
// is called.
|
||||||
|
#ifdef NDEBUG
|
||||||
|
# define _PyObject_GC_TRACK(op) \
|
||||||
|
_PyObject_GC_TRACK(_PyObject_CAST(op))
|
||||||
|
# define _PyObject_GC_UNTRACK(op) \
|
||||||
|
_PyObject_GC_UNTRACK(_PyObject_CAST(op))
|
||||||
|
#else
|
||||||
|
# define _PyObject_GC_TRACK(op) \
|
||||||
|
_PyObject_GC_TRACK(__FILE__, __LINE__, _PyObject_CAST(op))
|
||||||
|
# define _PyObject_GC_UNTRACK(op) \
|
||||||
|
_PyObject_GC_UNTRACK(__FILE__, __LINE__, _PyObject_CAST(op))
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef Py_REF_DEBUG
|
#ifdef Py_REF_DEBUG
|
||||||
extern void _PyDebug_PrintTotalRefs(void);
|
extern void _PyDebug_PrintTotalRefs(void);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue